Archived
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
210 lines
7.7 KiB
C++
210 lines
7.7 KiB
C++
/* gen_config_test.cpp -- the sampling a checkpoint states for itself.
|
|
*
|
|
* SamplingParams' defaults are an untruncated sampler. A model tuned with a truncated tail and
|
|
* served that way occasionally draws a token from far down its distribution; end-of-turn is one
|
|
* such token, so the symptom is a reply that ends early and looks like a dropped request. A wrong
|
|
* default is silent, hence a tested component.
|
|
*
|
|
* No model and no container: the three sources (a string, a file and a key lookup) are reached
|
|
* directly.
|
|
*/
|
|
#include "rad_test.h"
|
|
|
|
#include "gen_config.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
using namespace rad;
|
|
|
|
namespace {
|
|
|
|
/* The container's metadata table as gen_sampling_from_kv sees it: bare field names and string
|
|
* values, as rad-convert writes free-form metadata. */
|
|
std::function<const char*(const char*)> kv_of(const std::map<std::string, std::string>& m) {
|
|
return [&m](const char* k) -> const char* {
|
|
auto it = m.find(k);
|
|
return it == m.end() ? nullptr : it->second.c_str();
|
|
};
|
|
}
|
|
|
|
std::string write_temp(const char* name, const std::string& body) {
|
|
const std::string path = radtest::tmp_path(std::string(name) + ".json");
|
|
FILE* f = std::fopen(path.c_str(), "w");
|
|
if (!f) return std::string();
|
|
std::fwrite(body.data(), 1, body.size(), f);
|
|
std::fclose(f);
|
|
return path;
|
|
}
|
|
|
|
/* Qwen3.8-Flash-Next's generation_config.json, verbatim. */
|
|
const char* const kQwenGenConfig = R"({
|
|
"bos_token_id": 248044,
|
|
"do_sample": true,
|
|
"eos_token_id": [248046, 248044],
|
|
"pad_token_id": 248044,
|
|
"temperature": 1.0,
|
|
"top_k": 20,
|
|
"top_p": 0.95
|
|
})";
|
|
|
|
} /* namespace */
|
|
|
|
TEST(gen_config_reads_a_real_generation_config) {
|
|
GenSampling g;
|
|
CHECK(gen_sampling_parse(kQwenGenConfig, "test", &g));
|
|
CHECK_NEAR(g.temp, 1.0f, 1e-6);
|
|
CHECK_EQ(g.top_k, 20);
|
|
CHECK_NEAR(g.top_p, 0.95f, 1e-6);
|
|
CHECK_NEAR(g.min_p, -1.0f, 1e-6); /* not in the file, so not claimed */
|
|
|
|
/* Overlaid on the built-in defaults, it truncates the tail. */
|
|
const SamplingParams sp = g.onto(SamplingParams{});
|
|
CHECK_NEAR(sp.temp, 1.0f, 1e-6);
|
|
CHECK_EQ(sp.top_k, 20);
|
|
CHECK_NEAR(sp.top_p, 0.95f, 1e-6);
|
|
CHECK_NEAR(sp.min_p, 0.0f, 1e-6); /* untouched: the struct's default */
|
|
CHECK(!sp.greedy());
|
|
}
|
|
|
|
/* A field a source did not state is not claimed, so sources stack: a sidecar that sets only
|
|
* top_k keeps the temperature underneath it. */
|
|
TEST(gen_config_claims_only_what_a_source_states) {
|
|
GenSampling g;
|
|
CHECK(gen_sampling_parse(R"({"top_k": 40})", "test", &g));
|
|
CHECK_EQ(g.top_k, 40);
|
|
CHECK(g.temp < 0.0f);
|
|
CHECK(g.top_p < 0.0f);
|
|
|
|
SamplingParams base;
|
|
base.temp = 0.6f;
|
|
base.top_p = 0.8f;
|
|
const SamplingParams sp = g.onto(base);
|
|
CHECK_NEAR(sp.temp, 0.6f, 1e-6);
|
|
CHECK_NEAR(sp.top_p, 0.8f, 1e-6);
|
|
CHECK_EQ(sp.top_k, 40);
|
|
|
|
GenSampling none;
|
|
CHECK(!gen_sampling_parse(R"({"bos_token_id": 1})", "test", &none));
|
|
CHECK(!none.any());
|
|
}
|
|
|
|
/* `do_sample: false` is greedy and transformers ignores every other sampler field under it. */
|
|
TEST(gen_config_reads_do_sample_false_as_greedy) {
|
|
GenSampling g;
|
|
CHECK(gen_sampling_parse(R"({"do_sample": false, "temperature": 0.7, "top_k": 20})",
|
|
"test", &g));
|
|
CHECK_NEAR(g.temp, 0.0f, 1e-6);
|
|
CHECK_EQ(g.top_k, -1); /* not claimed under greedy */
|
|
CHECK(g.onto(SamplingParams{}).greedy());
|
|
|
|
/* do_sample true claims nothing by itself. */
|
|
GenSampling t;
|
|
CHECK(gen_sampling_parse(R"({"do_sample": true, "temperature": 0.7})", "test", &t));
|
|
CHECK_NEAR(t.temp, 0.7f, 1e-6);
|
|
}
|
|
|
|
/* transformers spells "no top_k" as -1 and this sampler spells it 0. */
|
|
TEST(gen_config_translates_top_k_minus_one_to_off) {
|
|
GenSampling g;
|
|
CHECK(gen_sampling_parse(R"({"top_k": -1})", "test", &g));
|
|
CHECK_EQ(g.top_k, 0);
|
|
}
|
|
|
|
/* The startup line names what top_k 0 does: the sampler draws from its 1024 most likely
|
|
* candidates, which is neither "off" nor the whole vocabulary. */
|
|
TEST(the_sampling_line_says_what_top_k_zero_samples_from) {
|
|
SamplingParams sp;
|
|
sp.top_k = 0;
|
|
const std::string zero = GenSampling::describe(sp);
|
|
CHECK(has(zero, "top_k 0 (the 1024 most likely tokens"));
|
|
CHECK(!has(zero, "(off)"));
|
|
sp.top_k = 20;
|
|
const std::string twenty = GenSampling::describe(sp);
|
|
CHECK(has(twenty, "top_k 20, top_p"));
|
|
}
|
|
|
|
/* An out-of-range value is dropped on its own, not refused: the config ships with the model,
|
|
* and one implausible entry should not discard the rest. */
|
|
TEST(gen_config_drops_an_out_of_range_field_and_keeps_the_rest) {
|
|
GenSampling g;
|
|
CHECK(gen_sampling_parse(R"({"temperature": 9.0, "top_p": 0.95, "top_k": 20})", "test", &g));
|
|
CHECK(g.temp < 0.0f); /* 9.0 is out of range, so not claimed */
|
|
CHECK_NEAR(g.top_p, 0.95f, 1e-6);
|
|
CHECK_EQ(g.top_k, 20);
|
|
|
|
/* top_p is a half-open range: 0 is not a nucleus, 1 is. */
|
|
GenSampling z;
|
|
CHECK(!gen_sampling_parse(R"({"top_p": 0.0})", "test", &z));
|
|
GenSampling one;
|
|
CHECK(gen_sampling_parse(R"({"top_p": 1.0})", "test", &one));
|
|
CHECK_NEAR(one.top_p, 1.0f, 1e-6);
|
|
}
|
|
|
|
TEST(gen_config_survives_text_that_is_not_a_config) {
|
|
GenSampling g;
|
|
CHECK(!gen_sampling_parse("{not json", "test", &g));
|
|
CHECK(!gen_sampling_parse("[1, 2, 3]", "test", &g));
|
|
CHECK(!gen_sampling_parse("", "test", &g));
|
|
CHECK(!g.any());
|
|
}
|
|
|
|
/* The container's copy, where rad-convert flattened every value to a string, and the original
|
|
* file resolve to the same sampler. */
|
|
TEST(gen_config_reads_the_container_table_as_the_file) {
|
|
GenSampling from_file;
|
|
CHECK(gen_sampling_parse(kQwenGenConfig, "test", &from_file));
|
|
|
|
const std::map<std::string, std::string> table = {
|
|
{ "do_sample", "1" },
|
|
{ "temperature", "1" },
|
|
{ "top_k", "20" },
|
|
{ "top_p", "0.95" },
|
|
};
|
|
GenSampling from_kv;
|
|
CHECK(gen_sampling_from_kv(kv_of(table), &from_kv));
|
|
CHECK_NEAR(from_kv.temp, from_file.temp, 1e-6);
|
|
CHECK_EQ(from_kv.top_k, from_file.top_k);
|
|
CHECK_NEAR(from_kv.top_p, from_file.top_p, 1e-6);
|
|
|
|
/* An empty table states nothing rather than stating zeros. */
|
|
GenSampling empty;
|
|
CHECK(!gen_sampling_from_kv(kv_of({}), &empty));
|
|
CHECK(!empty.any());
|
|
|
|
/* A value that is not a number is dropped, and the rest of the table still reads. */
|
|
GenSampling junk;
|
|
CHECK(gen_sampling_from_kv(kv_of({ { "temperature", "warm" }, { "top_k", "20" } }), &junk));
|
|
CHECK(junk.temp < 0.0f);
|
|
CHECK_EQ(junk.top_k, 20);
|
|
}
|
|
|
|
TEST(gen_config_reads_a_file_and_treats_absence_as_silence) {
|
|
const std::string path = write_temp("sidecar", kQwenGenConfig);
|
|
CHECK(!path.empty());
|
|
GenSampling g;
|
|
CHECK(gen_sampling_from_file(path, /*required=*/false, &g));
|
|
CHECK_EQ(g.top_k, 20);
|
|
|
|
/* A checkpoint without a generation config is served from the layers underneath, not
|
|
* refused: a missing file claims nothing and returns false. */
|
|
GenSampling missing;
|
|
CHECK(!gen_sampling_from_file("/nonexistent/generation_config.json", false, &missing));
|
|
CHECK(!missing.any());
|
|
}
|
|
|
|
/* The sidecar is the container's path with its extension replaced. The extension starts at the
|
|
* last dot in the basename, so a dot in a directory name is not one. */
|
|
TEST(gen_config_derives_the_sidecar_path) {
|
|
CHECK_EQ(gen_sidecar_path("/m/q38-flashnext-unc-w4-mtp.rad"),
|
|
std::string("/m/q38-flashnext-unc-w4-mtp.generation.json"));
|
|
CHECK_EQ(gen_sidecar_path("model.rad"), std::string("model.generation.json"));
|
|
CHECK_EQ(gen_sidecar_path("/a.b/model"), std::string("/a.b/model.generation.json"));
|
|
CHECK_EQ(gen_sidecar_path("/a.b/model.rad"), std::string("/a.b/model.generation.json"));
|
|
}
|
|
|
|
RAD_TEST_MAIN()
|