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

+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);
}