gemma4: a GPTQ calibration tap and recipe for the 31B's w4a8 container, whose tool calls then close their strings

Rounded to nearest, the 31B's w4a8 trunk leaves streamed tool calls inside their string arguments:
after `sku:<|"|>MS-110` it prefers `**,` (58%) to the string's close (41%; bf16 ~1.0), and
BetterBench's order request stalled in 10 of 20 tries. This adds what it takes to make the same
container (same grid, kernels and size) with the trunk's int4 codes chosen by GPTQ against the bf16
model's own activations.

- arch/gemma4: with RADIANCE_CALIB_DIR set, the plugin serving the 31B's bf16 checkpoint sums the
  Gram of every dense linear's bf16 input over the corpus a server is fed (gram_accum into a
  PERSIST [K, K] f32 buffer a tap) and writes the files at RADIANCE_CALIB_TOKENS, as arch/gemma3's
  tap does: q|k|v's and gate|up's input whole from rank 0, o_proj's and down's this rank's columns
  as `<stem>.part`, which libquant places on the diagonal. The files carry the names the w4a8
  container gives the weights (gemma4_bf16's attn_qkv, attn_o, ffn_gate_up, ffn_down), so a GPTQ
  recipe converting that container reads them with calib=DIR. RADIANCE_G4_CALIB_LAYERS=lo:hi records
  a slice of layers (the 31B's taps are ~364 MiB a layer a rank at --tp 4, and 60 layers of them do
  not fit beside the bf16 weights). Under the tap a step runs whole and on one lane; without
  RADIANCE_CALIB_DIR nothing changes.
- data/recipes/gemma4-31b-dense-bf16-w4a8g-df2.recipe: the -df2 recipe with `gptq codes=i4
  group=128 scale=f16 rule=search calib=$CALIB` on the trunk's four linears.
- docs/GEMMA4-31B-GPTQ.md: the route, the corpus, how to run it, and the measurements.

Measured (1.11.0, the same tap and recipe): by GPTQ the close is 99.8% and no try stalls; KL against
bf16 on the model's own turns falls from 0.341 to 0.251 (the w8a8 container 0.226), at the same
speed. The convert takes 75 minutes with OpenBLAS in the image (#220; an estimated days without). A
container made this way is on Hugging Face as Ashlyn4202025/gemma4-31b-w4a8-gptq-dflash2, and with
the block-16 drafter as Ashlyn4202025/gemma4-31b-w4a8-gptq-dflash2-b16.

Tests: engine_e2e takes the tiny Gemma 4 (shaped like the 31B) through the whole route at --tp 2:
the tap's files, by name and width on each rank; rad-convert with the two shipped recipes, every
trunk linear fitted and none rounded to nearest; and the GPTQ container's KL against the bf16 model
on held-out documents more than three times below the round-to-nearest one's (0.0018 against
0.0105). With rank 0's and rank 1's .part blocks swapped the margin is 2.2 times, which fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-26 09:22:45 +01:00
1 parent 66c86b9f89
commit d8b9ce3a32
7 files changed
+404 -3

No files matched your search

+108
View File
@@ -141,6 +141,33 @@ struct Kind {
int64_t kv_dim() const { return nkv * hd; }
};
/* ---- the GPTQ calibration tap (rad_arch.h's calib_*): the Gram of one bf16 linear's input,
* summed over a corpus in a PERSIST [K, K] f32 buffer. Declared only with RADIANCE_CALIB_DIR set,
* for the dense layers RADIANCE_G4_CALIB_LAYERS names ("lo:hi", default every layer): the 31B's
* four taps a layer are ~364 MiB a rank at --tp 4, so its 60 layers do not fit beside the bf16
* weights and a calibration runs in slices of layers, the corpus pushed through once per slice. */
struct G4Tap {
rad_buf h = 0;
rad_op op = 0;
int64_t K = 0;
int declare(RadBuilder* b, Names& nm, int64_t max_tok, const char* name, rad_buf a, int64_t k) {
K = k;
h = decl_b(b, nm.f("%s.gram", name), RAD_F32, { k, k }, RAD_BUF_PERSIST);
if (!h) return RAD_E_INVAL;
op = rw(b, RAD_OP(b, "gram_accum",
RAD_PARAMS(RAD_RANGE("M", 1, max_tok), RAD_INT("n", k),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ a, h }, { h });
return op ? RAD_OK : RAD_E_INVAL;
}
/* gram_accum takes a, a_scale?, h: no scale for a bf16 input. */
void step(RadCtx* c, rad_buf a, int64_t T, bool live) const {
if (op && live) RAD_ISSUE_N(c, op, T, brows(a, T), RAD_NONE, brows(h, K));
}
};
struct Layer {
int full = 0;
/* attention */
@@ -187,6 +214,10 @@ struct Layer {
rad_weight w_next = 0;
/* the drafter's tap, on a layer the drafter reads (see declare) */
rad_op op_tap = 0;
/* Calibration: the attention's input (q, k and v share it), its output (this rank's columns,
* o_proj's input), the dense arm's input and its gated product (this rank's columns, down's
* input). */
G4Tap tap_in{}, tap_o{}, tap_gu{}, tap_dn{};
};
struct Model {
@@ -227,6 +258,12 @@ struct Model {
int tap_layer[MAX_TAPS] = {};
rad_buf b_dfsrc = 0;
/* The calibration tap: on while `tap_live`, over layers [calib_lo, calib_hi). Written by step,
* which owns the corpus count. */
bool calib = false, tap_live = false;
int64_t calib_lo = 0, calib_hi = 0;
long long calib_rows = 0;
Dflash1 df{};
rad_kvgroup kv_draft = 0;
bool have_df = false;
@@ -1223,6 +1260,42 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
* ~120 us per layer on 4x R9700), so it hides behind the experts' bandwidth. No collective
* rides it, so the reductions stay on lane 0. Its buffers leave the arena's sharing. */
m.arms = m.moe;
/* ---- the calibration tap. It reads each dense linear's bf16 input where trunk issues the
* linear, so a calibration runs every step whole and on one lane. The files are named after
* the weights the 31B's w4a8 container quantises (gemma4_bf16: blk.N.attn_qkv, attn_o,
* ffn_gate_up, ffn_down), which a GPTQ recipe reads with calib=DIR: the Hessians of the bf16
* model, whose activations a quantised container's own tap would see through its quantisers.
* The routed arm is not tapped. */
if (calib_dir()) {
m.overlap = false;
m.arms = false;
m.calib_lo = 0;
m.calib_hi = (int64_t)m.layers.size();
if (const char* s = std::getenv("RADIANCE_G4_CALIB_LAYERS")) {
long long lo = 0, hi = 0;
int used = 0;
if (std::sscanf(s, "%lld:%lld%n", &lo, &hi, &used) != 2 || s[used] != '\0' || lo < 0 ||
hi > (long long)m.layers.size() || lo >= hi) {
fprintf(stderr, "radiance: gemma4: RADIANCE_G4_CALIB_LAYERS '%s' is not lo:hi within "
"0:%zu\n", s, m.layers.size());
return RAD_E_INVAL;
}
m.calib_lo = lo;
m.calib_hi = hi;
}
for (int64_t l = m.calib_lo; l < m.calib_hi; ++l) {
Layer& y = m.layers[(size_t)l];
const Kind& k = y.full ? m.kf : m.ks;
const int i = (int)l;
RAD_ARCH_TRY(y.tap_in.declare(b, nm, T, nm.f("blk.%d.attn_in", i), m.b_h, E));
RAD_ARCH_TRY(y.tap_o.declare(b, nm, T, nm.f("blk.%d.attn_o", i),
y.full ? m.b_attf : m.b_atts, k.q()));
RAD_ARCH_TRY(y.tap_gu.declare(b, nm, T, nm.f("blk.%d.ffn_gate_up", i), m.b_h, E));
RAD_ARCH_TRY(y.tap_dn.declare(b, nm, T, nm.f("blk.%d.ffn_down", i), m.b_act, m.n_ff));
}
m.calib = true;
m.tap_live = true;
}
if (m.overlap) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_o));
if (m.overlap || m.arms) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_ff2));
if (m.arms)
@@ -1464,8 +1537,10 @@ static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) {
RAD_ISSUE(c, y.op_q, brows(m.b_h, T), RAD_W(y.w_q), brows(m.b_qf, T));
RAD_ISSUE(c, y.op_k, brows(m.b_h, T), RAD_W(y.w_k), brows(m.b_kf, T));
}
y.tap_in.step(c, m.b_h, T, m.tap_live);
attend(c, m, y, li, batch);
RAD_ISSUE(c, y.op_o, brows(y.full ? m.b_attf : m.b_atts, T), RAD_W(y.w_o), brows(m.b_o, T));
y.tap_o.step(c, y.full ? m.b_attf : m.b_atts, T, m.tap_live);
if (y.op_ar_o) RAD_ISSUE_N(c, y.op_ar_o, T * E, brows(m.b_o, T), RAD_NONE);
if (y.op_join_r) {
RAD_ISSUE(c, y.op_join_r, brows(m.b_o, T), RAD_W(y.w_apost), brows(m.b_x, T),
@@ -1485,9 +1560,11 @@ static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) {
if (m.arms)
lanes_ok = lanes_ok && rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK;
RAD_ISSUE(c, y.op_gu, brows(m.b_h, T), RAD_W(y.w_gu), brows(m.b_gu, T));
y.tap_gu.step(c, m.b_h, T, m.tap_live);
RAD_ISSUE(c, y.op_act, bcol(m.b_gu, 0, m.n_ff, T), bcol(m.b_gu, m.n_ff, m.n_ff, T),
brows(m.b_act, T));
RAD_ISSUE(c, y.op_dn, brows(m.b_act, T), RAD_W(y.w_dn), brows(m.b_ff2, T));
y.tap_dn.step(c, m.b_act, T, m.tap_live);
if (m.arms) lanes_ok = rad_lane(c, 0) == RAD_OK && lanes_ok;
if (m.moe) {
routed(c, m, y, T, 0, T, T);
@@ -1668,6 +1745,36 @@ static void trunk_overlap(RadCtx* c, const Model& m, const RadBatch* batch, int6
"the first");
}
/* The tap's rows, and its files once the corpus reached RADIANCE_CALIB_TOKENS. The input of q|k|v
* and of gate|up is whole on every rank, so rank 0 writes it, under the name of the linear that
* reads it. The column-split linears (o, down) see this rank's columns only: each rank writes its
* block as `<stem>.part`, which libquant's calib_sum places on the diagonal in rank order (the
* cross-rank products are never formed on one card). */
static void calib_count(RadCtx* c, Model& m, int64_t T) {
m.calib_rows += T;
if (m.calib_rows < calib_target()) return;
const int rk = rad_rank(c);
bool ok = true;
for (int64_t l = m.calib_lo; l < m.calib_hi; ++l) {
const Layer& y = m.layers[(size_t)l];
char stem[96];
if (rk == 0) {
std::snprintf(stem, sizeof stem, "blk.%d.attn_qkv", (int)l);
ok = calib_write(c, y.tap_in.h, y.tap_in.K, m.calib_rows, stem, 0) && ok;
std::snprintf(stem, sizeof stem, "blk.%d.ffn_gate_up", (int)l);
ok = calib_write(c, y.tap_gu.h, y.tap_gu.K, m.calib_rows, stem, 0) && ok;
}
std::snprintf(stem, sizeof stem, "blk.%d.attn_o.part", (int)l);
ok = calib_write(c, y.tap_o.h, y.tap_o.K, m.calib_rows, stem, 0) && ok;
std::snprintf(stem, sizeof stem, "blk.%d.ffn_down.part", (int)l);
ok = calib_write(c, y.tap_dn.h, y.tap_dn.K, m.calib_rows, stem, 0) && ok;
}
m.tap_live = false;
std::fprintf(stderr, "radiance: gemma4: rank %d wrote the Hessians of layers %lld:%lld after "
"%lld tokens to %s%s\n", rk, (long long)m.calib_lo, (long long)m.calib_hi,
m.calib_rows, calib_dir(), ok ? "" : ", with errors");
}
static void step(RadCtx* c, const RadBatch* batch) {
const Model& m = g_model[rad_rank(c)];
if (batch->draft_pass != 0) {
@@ -1695,6 +1802,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
const int64_t cut = split_at(m, T);
if (cut > 0) trunk_overlap(c, m, batch, cut);
else trunk(c, m, batch);
if (m.calib && m.tap_live) calib_count(c, g_model[rad_rank(c)], T);
if (m.op_onorm) RAD_ISSUE(c, m.op_onorm, brows(m.b_x, T), RAD_W(m.w_onorm), brows(m.b_h, T));
if (batch->n_out > 0) {