radiance 1.2.1

- A checkpoint restore that found its snapshot slot given back stopped the engine (#33): a
  snapshot handed to an admission is now kept until its step has run, a lost one re-prefills
  that request instead, and a failed restore names the check and the rank.
- The headroom warning had no fixed point (#32): it now checks the room left for a kernel's first
  launch and names the --gpu-headroom-mib value that clears it.
- reasoning_effort (#31): the levels the chat template takes are probed at startup and published
  in /server_info; a level outside them is a 400 on `reasoning_effort` with `supported`;
  --reasoning-effort-map rewrites a client's level.
- rad-convert reads published quantised releases (#29): AMD Quark MXFP4 byte for byte, and
  AutoRound (auto_round:auto_gptq) / GPTQ int4 and int8, with Flash-Next checkpoints that ship
  their experts one by one.
- tool_choice "required" and a named function are served by a call grammar for the whole reply.
This commit is contained in:
Deadcode committed 2026-10-07 10:14:00 +02:00
1 parent e95d397288
commit d23c3e0d17
37 files changed
+1595 -147

No files matched your search

+71
View File
@@ -918,6 +918,77 @@ TEST(the_generated_grammar_accepts_what_the_vendored_one_accepts) {
log_set_level(Log::Warn);
}
/* A REQUIRED CALL IS THE WHOLE REPLY: from the generation prompt the sampler feeds it, the
* reasoning block and then the calls, with no prose before them and nothing after them -- so a
* complete grammar admits only the end of the turn and the reply stops at the last call. A named
* function admits that function alone, and `parallel_tool_calls: false` one call. */
TEST(a_required_call_grammar_is_the_whole_reply) {
log_set_level(Log::Error);
static const SimpleVocab vocab = [] {
SimpleVocab v;
for (int c = 32; c < 127; ++c) v.add(std::string(1, (char)c));
v.add("\n");
return v;
}();
int checked = 0;
for (const Model& m : models()) {
Bench* b = bench(m, ChatRequest{});
if (!b || b->prompt().grammar.empty()) continue;
const std::string gp = b->prompt().generation_prompt;
auto grammar = [&](bool parallel, const std::string& only) {
ChatGrammarRequest gq;
gq.tools = &agent_tools();
gq.generation_prompt = gp;
gq.parallel_tool_calls = parallel;
gq.require_call = true;
gq.only_tool = only;
ChatGrammar g;
std::string why;
CHECK_OK(chat_format_grammar(b->format(), gq, &g, &why));
CHECK(!g.lazy);
CHECK(g.trigger_words.empty());
return g.text;
};
/* 1 complete, 0 admitted but unfinished or refused. The sampler feeds the generation
* prompt first (SamplingParams::grammar_prefix), so the reply is read after it. */
auto admits = [&](const std::string& gbnf, const std::string& reply) {
std::unique_ptr<GbnfGrammar> gr;
std::string err;
if (GbnfGrammar::create(gbnf, "root", &vocab, false, {}, {}, &gr, &err) < 0) return -1;
if (gr->accept_str(gp) < 0 || gr->accept_str(reply) < 0) return 0;
return gr->complete() ? 1 : 0;
};
const std::string any = grammar(true, ""), one = grammar(false, ""),
read = grammar(true, "read");
const std::string T = "Thinking it over.\n</think>\n\n";
const std::string bash = C(m, "bash", { { "command", "ls" } });
const std::string rd = C(m, "read", { { "path", "/a" } });
CHECK_EQ(admits(any, T + bash), 1);
CHECK_EQ(admits(any, T + bash + "\n" + rd), 1);
CHECK_EQ(admits(any, T + "The answer is 42."), 0);
CHECK_EQ(admits(any, T + "Checking.\n" + bash), 0);
CHECK_EQ(admits(any, T + bash + "\nDone now."), 0);
CHECK_EQ(admits(any, T), 0);
CHECK_EQ(admits(one, T + bash), 1);
CHECK_EQ(admits(one, T + bash + "\n" + rd), 0);
CHECK_EQ(admits(read, T + rd), 1);
CHECK_EQ(admits(read, T + bash), 0);
ChatGrammarRequest gq;
gq.tools = &agent_tools();
gq.require_call = true;
gq.only_tool = "nope";
ChatGrammar g;
std::string why;
CHECK(chat_format_grammar(b->format(), gq, &g, &why) == RAD_E_INVAL);
CHECK(why.find("nope") != std::string::npos);
++checked;
}
CHECK(checked > 0);
log_set_level(Log::Warn);
}
/* ================================================================== the description */
TEST(the_derived_formats_are_the_ones_in_service) {
+285
View File
@@ -10,6 +10,7 @@
#include "format/checkpoint.h"
#include "rad_plugin.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -335,3 +336,287 @@ TEST(what_is_not_served_as_it_is_says_why) {
}
RAD_TEST_MAIN()
/* ================================================================== imported quantisations */
namespace {
/* A GPTQ-packed weight as auto_gptq's packer writes it: code (row n, column k) is the signed value
* `q[n][k]`, stored unsigned with zero point 2^(bits-1) as field k % (32/bits) of int32 word
* [k / (32/bits), n]; `qzeros` holds the v1 zero point (less one) in every field; `scales` is
* f16 [K / group, N]. */
struct Gptq {
std::vector<std::vector<int>> q; /* [N][K] signed codes */
std::vector<float> scale; /* [G * N], row g of the scale tensor first */
int64_t N = 0, K = 0, group = 0;
int bits = 4;
};
Gptq make_gptq(int64_t N, int64_t K, int64_t group, int bits, int seed) {
Gptq g;
g.N = N; g.K = K; g.group = group; g.bits = bits;
const int lo = -(1 << (bits - 1)), span = 1 << bits;
g.q.assign((size_t)N, std::vector<int>((size_t)K));
uint32_t x = 2463534242u ^ (uint32_t)seed;
for (auto& row : g.q)
for (int& v : row) {
x ^= x << 13; x ^= x >> 17; x ^= x << 5;
v = lo + (int)(x % (uint32_t)span);
}
/* f16 scales with low mantissa bits set and both signs, so the bf16 rounding and the sign
* are both seen. */
for (int64_t i = 0; i < (K / group) * N; ++i)
g.scale.push_back((i % 3 == 1 ? -1.0f : 1.0f) * (0.0123f + 0.00071f * (float)i));
return g;
}
void add_gptq(std::vector<T>* ts, const std::string& base, const Gptq& g, bool with_gidx = false) {
const int64_t per = 32 / g.bits, G = g.K / g.group;
T qw{ base + ".qweight", "I32", { g.K / per, g.N }, {} };
qw.bytes.assign((size_t)(g.K / per * g.N * 4), 0);
for (int64_t n = 0; n < g.N; ++n)
for (int64_t k = 0; k < g.K; ++k) {
const uint32_t u = (uint32_t)(g.q[(size_t)n][(size_t)k] + (1 << (g.bits - 1)));
uint32_t* w = (uint32_t*)qw.bytes.data() + (k / per) * g.N + n;
*w |= u << ((k % per) * g.bits);
}
T sc{ base + ".scales", "F16", { G, g.N }, {} };
sc.bytes.resize((size_t)(G * g.N * 2));
for (int64_t i = 0; i < G * g.N; ++i) rad_store_f32(sc.bytes.data(), RAD_F16, i, g.scale[(size_t)i]);
T qz{ base + ".qzeros", "I32", { G, g.N * g.bits / 32 }, {} };
qz.bytes.resize((size_t)(G * g.N * g.bits / 32 * 4));
for (size_t i = 0; i < qz.bytes.size() / 4; ++i)
((uint32_t*)qz.bytes.data())[i] = g.bits == 4 ? 0x77777777u : 0x7f7f7f7fu;
ts->push_back(qw);
ts->push_back(sc);
ts->push_back(qz);
if (with_gidx) {
T gi{ base + ".g_idx", "I32", { g.K }, {} };
gi.bytes.resize((size_t)g.K * 4);
for (int64_t k = 0; k < g.K; ++k) ((int32_t*)gi.bytes.data())[k] = (int32_t)(k / g.group);
ts->push_back(gi);
}
}
/* The scale the f16 tensor actually holds. */
float f16_of(float v) {
uint16_t h;
rad_store_f32(&h, RAD_F16, 0, v);
return rad_load_f32(&h, RAD_F16, 0);
}
const char* kAutoRound =
"{\"model_type\":\"qwen4_exp\",\"quantization_config\":{\"quant_method\":\"auto-round\","
"\"packing_format\":\"auto_round:auto_gptq\",\"bits\":4,\"group_size\":128,\"sym\":true}}";
} /* namespace */
/* AUTOROUND'S GPTQ EXPORT IS IMPORTED: gate and up -- each its own GPTQ-packed int4 weight --
* become one `i4*bf16[1x128]` weight whose codes are the shipped ones, signed, row by row, and
* whose scales are the shipped f16 ones rounded to bf16. Read as f32 it is the shipped value:
* the code times the f16 scale. Codeberg #29. */
TEST(an_autoround_int4_weight_is_imported_as_its_codes_and_scales) {
const Gptq gate = make_gptq(8, 256, 128, 4, 1), up = make_gptq(16, 256, 128, 4, 2);
std::vector<T> ts;
add_gptq(&ts, "e.0.gate_proj", gate);
add_gptq(&ts, "e.0.up_proj", up, /*with_gidx=*/true);
const std::string dir = write_checkpoint(ts, kAutoRound);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
CHECK(ck.imports() == Checkpoint::Import::Gptq);
CHECK_EQ(ck.meta().quant, std::string("auto-round"));
Program prog;
prog.name_map.push_back(map("gu", RAD_MAP_CONCAT, { "e.0.gate_proj.weight", "e.0.up_proj.weight" }));
CkptWeight w;
std::string why;
REQUIRE_EQ(ckpt_resolve(ck, prog, "gu", false, &w, &why), RAD_OK);
CHECK(!w.trivial);
REQUIRE(w.imported);
CHECK_EQ(w.rows, (int64_t)24);
CHECK_EQ(w.cols, (int64_t)256);
REQUIRE_EQ(w.enc.n_planes, 2);
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_I4);
CHECK_EQ(w.enc.plane[1].dtype, (uint32_t)RAD_BF16);
CHECK_EQ(w.enc.plane[1].block[0], (int64_t)1);
CHECK_EQ(w.enc.plane[1].block[1], (int64_t)128);
CHECK(rad_enc_valid(&w.enc));
/* every row, both pieces: codes, scales and the f32 value */
std::vector<uint8_t> codes((size_t)(24 * 128)), scales((size_t)(24 * 2 * 2));
REQUIRE_EQ(ckpt_plane_rows(w, 0, 0, 24, codes.data(), &why), RAD_OK);
REQUIRE_EQ(ckpt_plane_rows(w, 1, 0, 24, scales.data(), &why), RAD_OK);
std::vector<float> f((size_t)(24 * 256));
REQUIRE_EQ(ckpt_rows_f32(w, 0, 24, f.data(), &why), RAD_OK);
int bad_code = 0, bad_scale = 0, bad_f = 0;
for (int64_t r = 0; r < 24; ++r) {
const Gptq& g = r < 8 ? gate : up;
const int64_t n = r < 8 ? r : r - 8;
for (int64_t k = 0; k < 256; ++k) {
const int q = g.q[(size_t)n][(size_t)k];
if ((int)rad_load_f32(codes.data(), RAD_I4, r * 256 + k) != q) ++bad_code;
const float s = f16_of(g.scale[(size_t)((k / 128) * g.N + n)]);
if (f[(size_t)(r * 256 + k)] != (float)q * s) ++bad_f;
}
for (int64_t c = 0; c < 2; ++c) {
const float s = f16_of(g.scale[(size_t)(c * g.N + n)]);
if (rad_load_f32(scales.data(), RAD_BF16, r * 2 + c) !=
rad_load_f32(std::vector<uint16_t>{ rad_f32_to_bf16(s) }.data(), RAD_BF16, 0))
++bad_scale;
}
}
CHECK_EQ(bad_code, 0);
CHECK_EQ(bad_scale, 0);
CHECK_EQ(bad_f, 0);
/* a part of the plane, from the middle of the second piece */
std::vector<uint8_t> part(128);
REQUIRE_EQ(ckpt_plane_rows(w, 0, 13, 1, part.data(), &why), RAD_OK);
CHECK_EQ(std::memcmp(part.data(), codes.data() + 13 * 128, 128), 0);
remove_checkpoint(dir);
}
/* AN INT8 PER-CHANNEL WEIGHT (one scale a row, g_idx all zero) is `i8*bf16[1x128]` with its row's
* scale in every block. */
TEST(a_gptq_int8_per_channel_weight_repeats_its_scale) {
const Gptq g = make_gptq(4, 384, 384, 8, 3);
std::vector<T> ts;
add_gptq(&ts, "o_proj", g, /*with_gidx=*/true);
const std::string dir = write_checkpoint(ts, kAutoRound);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
Program prog;
prog.name_map.push_back(map("o", RAD_MAP_COPY, { "o_proj.weight" }));
CkptWeight w;
std::string why;
REQUIRE_EQ(ckpt_resolve(ck, prog, "o", false, &w, &why), RAD_OK);
REQUIRE(w.imported);
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_I8);
std::vector<uint8_t> codes(4 * 384), scales(4 * 3 * 2);
REQUIRE_EQ(ckpt_plane_rows(w, 0, 0, 4, codes.data(), &why), RAD_OK);
REQUIRE_EQ(ckpt_plane_rows(w, 1, 0, 4, scales.data(), &why), RAD_OK);
int bad = 0;
for (int64_t n = 0; n < 4; ++n) {
for (int64_t k = 0; k < 384; ++k)
if ((int8_t)codes[(size_t)(n * 384 + k)] != g.q[(size_t)n][(size_t)k]) ++bad;
for (int c = 0; c < 3; ++c)
if (rad_load_f32(scales.data(), RAD_BF16, n * 3 + c) !=
rad_load_f32(scales.data(), RAD_BF16, n * 3))
++bad;
}
CHECK_EQ(bad, 0);
remove_checkpoint(dir);
}
/* WHAT AN IMPORT CANNOT READ IT REFUSES BY NAME: an asymmetric zero point, an act-order group
* index, a group narrower than the served 128, and a packing it does not know. */
TEST(an_import_refuses_what_it_cannot_read) {
auto refused = [](void (*edit)(std::vector<T>*), const char* config, const char* expect) {
Gptq g = make_gptq(8, 256, 128, 4, 4);
std::vector<T> ts;
add_gptq(&ts, "x", g, true);
edit(&ts);
const std::string dir = write_checkpoint(ts, config);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
Program prog;
prog.name_map.push_back(map("x", RAD_MAP_COPY, { "x.weight" }));
CkptWeight w;
std::string why;
const int rc = ckpt_resolve(ck, prog, "x", false, &w, &why);
CHECK(rc < 0);
CHECK(why.find(expect) != std::string::npos);
remove_checkpoint(dir);
};
refused([](std::vector<T>* ts) { (*ts)[2].bytes[0] = 0x76; }, kAutoRound, "symmetric");
refused([](std::vector<T>* ts) { ((int32_t*)(*ts)[3].bytes.data())[0] = 1; }, kAutoRound,
"act-order");
/* group 64: four scale rows for 256 columns */
{
Gptq g = make_gptq(8, 256, 64, 4, 5);
std::vector<T> ts;
add_gptq(&ts, "x", g);
const std::string dir = write_checkpoint(ts, kAutoRound);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
Program prog;
prog.name_map.push_back(map("x", RAD_MAP_COPY, { "x.weight" }));
CkptWeight w;
std::string why;
CHECK(ckpt_resolve(ck, prog, "x", false, &w, &why) < 0);
CHECK(why.find("multiple of 128") != std::string::npos);
remove_checkpoint(dir);
}
/* AutoRound's own packing is another layout, and the checkpoint is not imported at all. */
{
const std::string dir = write_checkpoint(
{ bf16("y", { 2, 2 }, 0) },
"{\"model_type\":\"qwen4_exp\",\"quantization_config\":{\"quant_method\":"
"\"auto-round\",\"packing_format\":\"auto_round\"}}");
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
CHECK(ck.imports() == Checkpoint::Import::None);
CHECK(ck.import_refusal().find("auto_round:auto_gptq") != std::string::npos);
remove_checkpoint(dir);
}
}
/* QUARK'S MXFP4 IS `fp4_e2m1*e8m0[1x32]` BYTE FOR BYTE, and so trivial: the U8 codes and U8
* exponents are the planes, gate and up join along rows, and the f32 value is the e2m1 code times
* 2^(e - 127). */
TEST(a_quark_mxfp4_weight_is_its_codes_and_exponents) {
const int64_t K = 64;
auto codes = [&](const std::string& n, int64_t N, int seed) {
T t{ n, "U8", { N, K / 2 }, {} };
for (int64_t i = 0; i < N * K / 2; ++i) t.bytes.push_back((uint8_t)((i * 37 + seed) & 0xff));
return t;
};
auto exps = [&](const std::string& n, int64_t N, int seed) {
T t{ n, "U8", { N, K / 32 }, {} };
for (int64_t i = 0; i < N * K / 32; ++i) t.bytes.push_back((uint8_t)(120 + (i + seed) % 9));
return t;
};
const T g = codes("g.weight", 2, 1), gs = exps("g.weight_scale", 2, 1);
const T u = codes("u.weight", 3, 5), us = exps("u.weight_scale", 3, 4);
const std::string dir = write_checkpoint(
{ g, gs, u, us, bf16("n.weight", { 4 }, 1.0f) },
"{\"model_type\":\"qwen3_5\",\"quantization_config\":{\"quant_method\":\"quark\","
"\"global_quant_config\":{\"weight\":{\"dtype\":\"fp4\",\"qscheme\":\"per_group\","
"\"group_size\":32,\"scale_format\":\"e8m0\"}},\"layer_quant_config\":{}}}");
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
CHECK(ck.imports() == Checkpoint::Import::QuarkMx);
Program prog;
prog.name_map.push_back(map("gu", RAD_MAP_CONCAT, { "g.weight", "u.weight" }));
prog.name_map.push_back(map("n", RAD_MAP_COPY, { "n.weight" }));
CkptWeight w;
std::string why;
REQUIRE_EQ(ckpt_resolve(ck, prog, "gu", false, &w, &why), RAD_OK);
REQUIRE(w.trivial);
CHECK(!w.imported);
CHECK_EQ(w.rows, (int64_t)5);
CHECK_EQ(w.cols, K);
REQUIRE_EQ(w.enc.n_planes, 2);
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_FP4E2M1);
CHECK_EQ(w.enc.plane[1].dtype, (uint32_t)RAD_E8M0);
CHECK_EQ(w.enc.plane[1].block[1], (int64_t)32);
CHECK(rad_enc_valid(&w.enc));
std::vector<uint8_t> cp(5 * K / 2), sp(5 * K / 32);
REQUIRE_EQ(ckpt_plane_rows(w, 0, 0, 5, cp.data(), &why), RAD_OK);
REQUIRE_EQ(ckpt_plane_rows(w, 1, 0, 5, sp.data(), &why), RAD_OK);
CHECK_EQ(std::memcmp(cp.data(), g.bytes.data(), g.bytes.size()), 0);
CHECK_EQ(std::memcmp(cp.data() + g.bytes.size(), u.bytes.data(), u.bytes.size()), 0);
CHECK_EQ(std::memcmp(sp.data() + gs.bytes.size(), us.bytes.data(), us.bytes.size()), 0);
std::vector<float> f((size_t)K);
REQUIRE_EQ(ckpt_rows_f32(w, 3, 1, f.data(), &why), RAD_OK); /* u's row 1 */
int bad = 0;
for (int64_t k = 0; k < K; ++k) {
const uint8_t b = u.bytes[(size_t)(K / 2 + k / 2)];
const float code = rad_fp4e2m1_to_f32((uint8_t)(k % 2 ? b >> 4 : b & 15));
const float sc = std::ldexp(1.0f, (int)us.bytes[(size_t)(K / 32 + k / 32)] - 127);
if (f[(size_t)k] != code * sc) ++bad;
}
CHECK_EQ(bad, 0);
/* an excluded layer is a plain tensor */
REQUIRE_EQ(ckpt_resolve(ck, prog, "n", false, &w, &why), RAD_OK);
CHECK(w.trivial && rad_enc_is(&w.enc, "plain"));
remove_checkpoint(dir);
}
+80
View File
@@ -1332,6 +1332,86 @@ TEST(checkpoint_retention_is_budgeted_and_keeps_the_tip) {
CHECK(has_512);
}
/* A SNAPSHOT A LOOKUP HANDED OUT THIS STEP KEEPS ITS SLOT until the next step begins. The
* admission it was handed to restores from the slot ahead of the step, and a later admission in
* the same step reserves its own checkpoint -- which, on a full store, runs retention and then
* evicts. Either taking the handed snapshot leaves the restore naming a slot the store gave back,
* and the engine stopped on it ("could not restore ... from checkpoint slot 1: invalid
* argument"). Geometric backoff over {64..512} releases 192, so the hit is made to land there. */
TEST(a_snapshot_handed_out_this_step_survives_retention) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/8));
const auto a = tok_run(4242, 16 * 33);
publish(f, 1, a, 16 * 32);
BlockHash parent;
std::vector<BlockHash> hs;
for (int b = 0; b < 33; ++b) {
parent = chain_block_hash(parent, a.data() + b * 16, 16, nullptr, 0, b * 16);
hs.push_back(parent);
}
std::vector<int32_t> slot_at(9, -1);
for (int i = 1; i <= 8; ++i) {
CHECK_OK(f.pc.reserve_checkpoint(1, i * 64, hs[i * 4 - 1], &slot_at[(size_t)i]));
f.pc.commit_checkpoint(hs[i * 4 - 1]);
}
CHECK_EQ(f.kv.free_checkpoint_slots(), 0ll);
/* A prompt that leaves the chain after 200 tokens: the deepest snapshot under it is 192. */
f.pc.begin_step();
auto b = a;
b.resize(16 * 20);
for (int i = 200; i < (int)b.size(); ++i) b[(size_t)i] = 900000 + i;
std::vector<std::vector<int32_t>> hit;
const CacheHit h = f.pc.lookup(b, {}, &hit);
REQUIRE_EQ(h.n_linear_tokens, 192);
REQUIRE_EQ(h.checkpoint_slot, slot_at[3]);
/* The same step's next reservation finds the store full. */
BlockHash other = hs[3];
other.lo ^= 0x5eedull;
int32_t s9 = -1;
CHECK_OK(f.pc.reserve_checkpoint(2, 64, other, &s9));
CHECK(s9 >= 0);
CHECK(s9 != h.checkpoint_slot);
CHECK(f.kv.checkpoint_held(h.checkpoint_slot));
bool kept_192 = false;
for (int64_t p : f.pc.checkpoints(1)) if (p == 192) kept_192 = true;
CHECK(kept_192);
}
/* AND THE EVICTION AFTER RETENTION passes it over too. With one slot, the handed snapshot is the
* only thing a reservation could take, so the reservation is refused instead: that chunk goes
* without a checkpoint, and the restore still has its state. */
TEST(a_snapshot_handed_out_this_step_is_not_evicted) {
Fixture f;
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/1));
const auto a = tok_run(5151, 16 * 9);
publish(f, 1, a, 16 * 8);
BlockHash parent;
std::vector<BlockHash> hs;
for (int b = 0; b < 9; ++b) {
parent = chain_block_hash(parent, a.data() + b * 16, 16, nullptr, 0, b * 16);
hs.push_back(parent);
}
int32_t s1 = -1;
CHECK_OK(f.pc.reserve_checkpoint(1, 64, hs[3], &s1));
f.pc.commit_checkpoint(hs[3]);
f.pc.begin_step();
std::vector<std::vector<int32_t>> hit;
const CacheHit h = f.pc.lookup(a, {}, &hit);
REQUIRE_EQ(h.checkpoint_slot, s1);
int32_t s2 = -1;
CHECK_EQ(f.pc.reserve_checkpoint(1, 128, hs[7], &s2), RAD_E_FULL);
CHECK(f.kv.checkpoint_held(s1));
CHECK_EQ(f.pc.stats().ckpt_evictions, 0ll);
f.pc.begin_step();
CHECK_OK(f.pc.reserve_checkpoint(1, 128, hs[7], &s2));
}
TEST(a_dead_sessions_tip_is_evictable_so_the_slot_pool_cannot_wedge) {
/* One checkpoint each from more DISTINCT sessions than there are slots. Every one of them is
* its session's tip, so a rule that made a tip unevictable would wedge the pool here: the
+34
View File
@@ -280,6 +280,11 @@ struct FakeKV : IKVManager {
it->second.dirty[(size_t)g] = INT64_MAX;
return d;
}
/* Snapshot slots the store has given back; every other slot is held. */
std::vector<int32_t> ck_gone;
bool checkpoint_held(int32_t slot) const override {
return std::find(ck_gone.begin(), ck_gone.end(), slot) == ck_gone.end();
}
/* Blocks an elastic pool has handed back to the card: still carved and free, but not
* available to hand out until the pool grows again. free_blocks() is bounded by that and
* held_blocks() is not, which is the difference an elastic pool presents. */
@@ -876,6 +881,35 @@ TEST(a_hit_with_no_checkpoint_slot_reuses_nothing) {
CHECK_EQ(f.sch.metrics().linear_cached_tokens, 0);
}
/* A RESTORE FROM A SLOT THE STORE GAVE BACK IS NEVER BUILT. The cache keeps a handed-out snapshot
* until the step that restores it has run, so this is a guard: a step that finds one is abandoned,
* the request gives its hit back and goes to the queue, and the next step prefills it from a fresh
* lookup rather than the engine stopping on the restore. */
TEST(a_restore_from_a_slot_the_store_gave_back_is_prefilled_again) {
Fixture f;
f.build();
CHECK_OK(f.start());
f.pc.seed(304);
f.pc.lin = 256;
f.pc.src = 3;
f.kv.ck_gone = {3};
const int64_t free_before = f.kv.free_blocks(0);
const uint64_t a = f.submit(1000, 8);
CHECK(f.sch.step() == nullptr);
CHECK_EQ(f.sch.find(a)->n_computed, 0);
CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Waiting);
CHECK_EQ(f.kv.free_blocks(0), free_before); /* the adopted blocks went back */
/* The next lookup finds no snapshot, and the request runs from zero. */
f.pc.src = -1;
f.kv.ck_gone.clear();
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
CHECK_EQ(b->ctx_lens[0], 0);
CHECK_EQ(f.sch.plan().e[0].ckpt_restore, -1);
}
TEST(a_hit_that_no_checkpoint_covers_replays_from_zero) {
Fixture f;
f.build();
+153 -17
View File
@@ -103,6 +103,14 @@ struct FakeChatTemplate : IChatTemplate {
if (why) *why = "the template does not take " + refuse_kwarg;
return RAD_E_UNSUPPORTED;
}
/* And one with a set of its own, as Qwen3.8's xhigh|medium|low. */
if (!efforts.empty() && opt.template_kwargs.count("reasoning_effort")) {
const std::string& v = opt.template_kwargs.at("reasoning_effort");
if (std::find(efforts.begin(), efforts.end(), v) == efforts.end()) {
if (why) *why = "Jinja Exception: Unexpected reasoning effort " + v;
return RAD_E_UNSUPPORTED;
}
}
std::string s;
if (!tools.empty()) s += "[tools:" + std::to_string(tools.size()) + "]";
if (!opt.enable_thinking) s += "[nothink]";
@@ -143,6 +151,7 @@ struct FakeChatTemplate : IChatTemplate {
std::shared_ptr<IReplyParser> real_parser;
std::string refusal;
std::string refuse_kwarg;
std::vector<std::string> efforts; /* JSON text, as the template context carries them */
/* The reasoning block a render reports. FakeTokenizer makes a one-byte marker one token. */
std::string think_open, think_close;
bool think_starts_open = false;
@@ -845,23 +854,6 @@ TEST(unsupported_parameters_are_400_and_name_themselves) {
CHECK_EQ(r3.status, 400);
CHECK(r3.body.find("temperature") != std::string::npos);
/* A forced tool choice is refused rather than approximated: the template's forced-call
* grammar does not terminate, so honouring it would mean a reply that runs to max_tokens. */
HttpResponse r4 = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}],
"tools":[{"type":"function","function":{"name":"f"}}],
"tool_choice":"required"})");
CHECK_EQ(r4.status, 400);
CHECK(r4.body.find("tool_choice") != std::string::npos);
/* ...and the object form is the same refusal, not a different one. */
HttpResponse r4b = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}],
"tools":[{"type":"function","function":{"name":"f"}}],
"tool_choice":{"type":"function","function":{"name":"f"}}})");
CHECK_EQ(r4b.status, 400);
CHECK(r4b.body.find("tool_choice") != std::string::npos);
/* logprobs without sampler support is refused rather than answered with nulls. */
HttpResponse r5 = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}],
@@ -1275,6 +1267,109 @@ TEST(a_template_default_the_template_refuses_is_found_before_a_caller_sends_one)
CHECK(kw_why.empty() && eff_why.empty());
}
/* AN EFFORT THE TEMPLATE REFUSES IS NAMED AS ONE. The template's own set is probed at startup, a
* request outside it is answered as OpenAI answers an unsupported value -- `reasoning_effort`,
* `unsupported_value`, the set in `supported` -- and /server_info publishes the set beforehand.
* Codeberg #31: the 400 blamed `messages` and carried the set only inside a Jinja traceback. */
TEST(an_effort_the_template_refuses_is_answered_with_the_set_it_takes) {
Fixture f;
f.tmpl.efforts = { "\"xhigh\"", "\"medium\"", "\"low\"" };
const std::vector<std::string> set = f.srv->probe_reasoning_efforts();
REQUIRE_EQ(set.size(), 3u);
CHECK_EQ(set[0], std::string("low"));
CHECK_EQ(set[1], std::string("medium"));
CHECK_EQ(set[2], std::string("xhigh"));
OaiLimits lim = f.limits();
lim.reasoning_efforts = set;
for (const char* extra : { R"("reasoning_effort":"high")",
R"("chat_template_kwargs":{"reasoning_effort":"high"})" }) {
OaiRequest r;
ApiError e;
const std::string body =
std::string(R"({"messages":[{"role":"user","content":"hi"}],)") + extra + "}";
CHECK(parse_chat_request(body, f.odeps(), lim, &r, &e) < 0);
CHECK_EQ(e.status, 400);
CHECK_EQ(e.param, std::string("reasoning_effort"));
CHECK_EQ(e.code, std::string("unsupported_value"));
CHECK(!has(e.message, "Jinja"));
CHECK(has(e.message, "'xhigh'"));
const json env = json::parse(e.dump());
REQUIRE(env["error"]["supported"].is_array());
CHECK_EQ(env["error"]["supported"].size(), 5u);
CHECK_EQ(env["error"]["supported"][0].get<std::string>(), std::string("none"));
CHECK_EQ(env["error"]["supported"][4].get<std::string>(), std::string("xhigh"));
}
chat_with(f, lim, R"("reasoning_effort":"xhigh")");
chat_with(f, lim, R"("reasoning_effort":"none")");
const json j = json::parse(f.srv->router().call("GET", "/server_info").body);
const json& caps = j["capabilities"]["reasoning_efforts"];
REQUIRE(caps.is_array());
CHECK_EQ(caps.size(), 5u);
CHECK_EQ(caps[2].get<std::string>(), std::string("low"));
}
/* A TEMPLATE THAT TAKES ANY WORD has no set to publish or to refuse with, and a template that
* refuses for another reason keeps blaming `messages` with its own sentence. */
TEST(a_template_that_takes_any_effort_publishes_no_set) {
Fixture f;
CHECK(f.srv->probe_reasoning_efforts().empty());
const json j = json::parse(f.srv->router().call("GET", "/server_info").body);
CHECK(j["capabilities"]["reasoning_efforts"].is_null());
f.tmpl.refuse_kwarg = "custom";
OaiLimits lim = f.limits();
lim.reasoning_efforts = { "low" };
OaiRequest r;
ApiError e;
CHECK(parse_chat_request(R"({"messages":[{"role":"user","content":"hi"}],
"reasoning_effort":"low","chat_template_kwargs":{"custom":1}})",
f.odeps(), lim, &r, &e) < 0);
CHECK_EQ(e.param, std::string("messages"));
CHECK(has(e.message, "does not take custom"));
}
/* --reasoning-effort-map: the deployment's word replaces the request's before the template sees
* it, in either spelling; a word mapped to "none" turns thinking off; a target the template
* refuses is found at startup. */
TEST(a_mapped_effort_reaches_the_template_as_the_deployments_word) {
Fixture f;
f.tmpl.efforts = { "\"xhigh\"", "\"medium\"", "\"low\"" };
OaiLimits lim = f.limits();
lim.reasoning_effort_map = { { "high", "xhigh" }, { "minimal", "low" }, { "max", "none" } };
chat_with(f, lim, R"("reasoning_effort":"high")");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"xhigh\""));
chat_with(f, lim, R"("chat_template_kwargs":{"reasoning_effort":"high"})");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"xhigh\""));
chat_with(f, lim, R"("reasoning_effort":"medium")");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"medium\""));
chat_with(f, lim, R"("reasoning_effort":"max")");
CHECK(!f.tmpl.last_opt.enable_thinking);
CHECK(!f.tmpl.last_opt.template_kwargs.count("reasoning_effort"));
/* The map is read before "none" and "minimal" are, so it can name either. */
chat_with(f, lim, R"("reasoning_effort":"minimal")");
CHECK(f.tmpl.last_opt.enable_thinking);
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"low\""));
auto check = [&](const std::map<std::string, std::string>& m) {
FakeScheduler sched;
FakeTokenizer tok;
Deps d;
d.sched = &sched;
d.tok = &tok;
d.chat = &f.tmpl;
ServerOptions o;
o.reasoning_effort_map = m;
Server srv(d, o);
const json j = json::parse(srv.router().call("GET", "/server_info").body);
CHECK_EQ(j["request_defaults"]["reasoning_effort_map"].size(), m.size());
return srv.check_reasoning_effort_map();
};
CHECK(check({ { "high", "xhigh" }, { "max", "none" } }).empty());
CHECK(has(check({ { "high", "ultra" } }), "high=ultra"));
}
TEST(server_info_reports_what_a_request_that_leaves_a_field_out_gets) {
FakeScheduler sched;
FakeTokenizer tok;
@@ -1358,6 +1453,47 @@ TEST(parallel_tool_calls_false_reaches_the_template) {
CHECK_EQ(f.tmpl.last_n_tools, 1);
}
/* A forced choice reaches the template as "required", with the function it names; the grammar
* that holds the reply to it is built from that (chatparse_test). */
TEST(a_forced_tool_choice_reaches_the_template) {
Fixture f;
f.sched.reply = "TOOL:a:{}";
HttpResponse r = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}],"max_tokens":128,
"tools":[{"type":"function","function":{"name":"a"}}],
"tool_choice":"required"})");
CHECK_EQ(r.status, 200);
CHECK_EQ(f.tmpl.last_opt.tool_choice, std::string("required"));
CHECK(f.tmpl.last_opt.tool_name.empty());
HttpResponse r2 = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}],"max_tokens":128,
"tools":[{"type":"function","function":{"name":"a"}},
{"type":"function","function":{"name":"b"}}],
"tool_choice":{"type":"function","function":{"name":"b"}}})");
CHECK_EQ(r2.status, 200);
CHECK_EQ(f.tmpl.last_opt.tool_choice, std::string("required"));
CHECK_EQ(f.tmpl.last_opt.tool_name, std::string("b"));
CHECK_EQ(f.tmpl.last_n_tools, 2);
}
/* What cannot be forced is a 400 naming tool_choice: a call with no tools, a function the request
* does not offer, and a call that would have to share the whole reply with a document. */
TEST(a_forced_tool_choice_that_cannot_be_kept_is_refused) {
Fixture f;
auto refused = [&](const std::string& body) {
HttpResponse r = f.post("/v1/chat/completions", body);
return r.status == 400 && r.body.find("tool_choice") != std::string::npos;
};
CHECK(refused(R"({"messages":[{"role":"user","content":"hi"}],"tool_choice":"required"})"));
CHECK(refused(R"({"messages":[{"role":"user","content":"hi"}],
"tools":[{"type":"function","function":{"name":"a"}}],
"tool_choice":{"type":"function","function":{"name":"zz"}}})"));
CHECK(refused(R"({"messages":[{"role":"user","content":"hi"}],
"tools":[{"type":"function","function":{"name":"a"}}],
"tool_choice":"required","response_format":{"type":"json_object"}})"));
}
/* A grammar the compiler cannot run is a 400 at admission, not a request that dies mid-stream. */
TEST(a_grammar_that_cannot_be_run_is_refused_at_the_door) {
Fixture f;