From 44aecb0ab89383cbcbbdde88cc5b7992194473ba Mon Sep 17 00:00:00 2001 From: mici81 Date: Sun, 18 Oct 2026 17:41:03 +0000 Subject: [PATCH] arch/gemma3: review fixes -- calibration declares no fused junction, so no container writes zero Grams; docs corrected On an MXFP4 container the fused junctions skip the bf16 activations the tap reads, and q/k/v and gate_up Grams came out zero without a word (down likewise behind gelu_mul_quant_i8). Under RADIANCE_CALIB_DIR no junction or fused down-quantiser is declared, so any container calibrates, unfused; checked on the production MXFP4 container. RADIANCE_G3_CALIB_LAYERS refuses trailing characters. Docs: which trunk fuses what, the four-stream numbers' two workloads, which sizes are tested. arch_test's world-2 check counts the post-norm op. Co-Authored-By: Claude Opus 5.5 --- arch/gemma3/gemma3.cpp | 21 ++++++++++----------- docs/ARCHITECTURES.md | 26 ++++++++++++++++---------- tests/arch_test.cpp | 3 ++- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 245ee4a8..058635f8 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -576,7 +576,8 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.gu}, {w.ffn}); - if (ln_down.quant && !ln_down.shared_in && rad_op_declared(b, "gelu_mul_quant_i8")) + // Not under calibration: the tap reads the bf16 activation this fusion never writes. + if (ln_down.quant && !ln_down.shared_in && !calib_dir() && rad_op_declared(b, "gelu_mul_quant_i8")) op_gq = rw(b, RAD_OP(b, "gelu_mul_quant_i8", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), @@ -1058,7 +1059,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* The fused junctions, used only when every one resolves and every linear a junction feeds * reads its int8 pair (see gemma4_bf16). */ - if (g.n_layer > 0) { + // Neither junction family under calibration: the tap reads each linear's bf16 input, which + // the fused junctions never write (an MXFP4 container wrote zero Grams for q/k/v and gate_up). + if (g.n_layer > 0 && !calib_dir()) { bool ok = true; for (const Layer& lay : m.layers) ok = ok && lay.attn.ln_q.quant && lay.attn.ln_k.shared_in && lay.attn.ln_v.shared_in && @@ -1089,7 +1092,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } /* ---- the MXFP4 trunk's junctions, when every block's inputs are E4M3 pairs it can write. */ - if (g.n_layer > 0 && !m.fuse) { + if (g.n_layer > 0 && !m.fuse && !calib_dir()) { bool ok = true; for (const Layer& lay : m.layers) ok = ok && lay.attn.ln_q.mx && !lay.attn.ln_q.shared_in && lay.attn.ln_k.shared_in && @@ -1119,19 +1122,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } /* ---- the calibration tap. It reads the bf16 input of every linear, which the fused junctions - * never write, so it needs an unfused container (bf16, i8a8). */ + * never write, so under it none is declared (above): any container calibrates, unfused. */ if (calib_dir()) { - if (m.fuse) { - fprintf(stderr, "radiance: gemma3: calibration reads each linear's bf16 input, which this " - "container's fused junctions do not write; calibrate on the bf16 or an " - "i8a8 container\n"); - return RAD_E_INVAL; - } m.calib_lo = 0; m.calib_hi = g.n_layer; if (const char* s = std::getenv("RADIANCE_G3_CALIB_LAYERS")) { long long lo = 0, hi = 0; - if (std::sscanf(s, "%lld:%lld", &lo, &hi) != 2 || lo < 0 || hi > g.n_layer || lo >= hi) { + int used = 0; + if (std::sscanf(s, "%lld:%lld%n", &lo, &hi, &used) != 2 || s[used] != '\0' || lo < 0 || + hi > g.n_layer || lo >= hi) { fprintf(stderr, "radiance: gemma3: RADIANCE_G3_CALIB_LAYERS '%s' is not lo:hi within " "0:%lld\n", s, (long long)g.n_layer); return RAD_E_INVAL; diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 07f9ee9c..415f04a0 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -563,8 +563,10 @@ under a tenth of the weight bytes, and the dense arm's 2112 is not whole 128-blo # `gemma3`: Gemma 3 text (TranslateGemma) `arch/gemma3` serves the language model of a `Gemma3ForConditionalGeneration` checkpoint (the -vision tower and projector are not mapped, so never converted): TranslateGemma 27B, and the Gemma 3 -text sizes with its config keys. It is `gemma4_bf16`'s block structure with Gemma 3's differences; +vision tower and projector are not mapped, so never converted). Written and measured for +TranslateGemma 27B; another Gemma 3 size with the same config keys should declare (4B and 12B have +a 256 head, served by the h256 rows) but is untested, and the 1B, a `Gemma3ForCausalLM`, keeps its +tensors under names this plugin does not map. It is `gemma4_bf16`'s block structure with Gemma 3's differences; the reference is transformers' `modeling_gemma3` (4.57). ``` @@ -617,16 +619,19 @@ above), which matters little for translation prompts, whose shared prefix is a l | `codes=u4 zero=u8 group=128 scale=f16` | `gemm_nt_q` w4a8i8_asym | int8, a scale per (row, 128) | A linear reading the same activation as the one before it (k and v after q, up after gate) reuses -its codes. On an MXFP4 or int8 trunk the q and k norms and both rotaries are one `qkv_norm_rope` at -`v_norm` 0, and each junction (the branch's post-norm, the residual add, the next norm and the next -linear's quantiser) is one `ar_rmsnorm_post_quant_fp8` at two ranks on the exact wire, the branch's -all-reduce in the same launch, or a post-norm and `rmsnorm_quant_fp8` otherwise. Where a row is -missing the plugin declares the chain, and the startup notes say which form each took. +its codes. On every trunk the q and k norms and both rotaries are one `qkv_norm_rope` at `v_norm` 0. +On an MXFP4 trunk each junction (the branch's post-norm, the residual add, the next norm and the +next linear's E4M3 quantiser) is one `ar_rmsnorm_post_quant_fp8` at two ranks on the exact wire, +the branch's all-reduce in the same launch, or a post-norm and `rmsnorm_quant_fp8` otherwise; a +w4a8 / w8a8 trunk takes `gemma4_bf16`'s fused junctions; an i8a8 or asymmetric-int4 trunk runs the +chain. Where a row is missing the plugin declares the chain, and the startup notes say which form +each took. Under calibration no junction is fused (below). **Calibration.** Serve a calibration container (int8, `translategemma-27b-i8a8.recipe`: KL 0.003 against bf16, and it fits two 32 GB cards) with `RADIANCE_CALIB_DIR` set, push a corpus of the prompts you will serve through prefill, and each linear's input Gram is written once -`RADIANCE_CALIB_TOKENS` have passed. `RADIANCE_G3_CALIB_LAYERS=lo:hi` limits the tap to a layer +`RADIANCE_CALIB_TOKENS` have passed; the junctions run unfused meanwhile, so the tap sees every +linear's bf16 input from any container. `RADIANCE_G3_CALIB_LAYERS=lo:hi` limits the tap to a layer slice, so the 21504-wide down projections of 62 layers can be taken in passes. At two ranks the column-split linears (o, down) see their rank's columns only and write `.part`; libquant assembles the parts block diagonally (docs/TOOLS.md). Give every prompt a distinct first line, or @@ -650,8 +655,9 @@ and SiLU; target layers 1, 16, 30, 44, 59; mask token ``, 6; block 8; w `qwen35_fp8` serves Qwen's: the trunk copies the residual stream after each tapped layer's junction into `dflash_src`, the probe fixes the depth at block - 1, and `rad_block_dflash2.h` applies the embedding scale through `embed_lookup`'s `mult` (only when it is not 1, so other drafters' graphs are -unchanged). On held-out translation prompts it accepts 3.3 tokens a verify step: 152 tok/s a stream -and 364 at four, against 56 and 175 without it. +unchanged). On 64 held-out translation pieces it accepts 3.3 tokens a verify step: 152 tok/s a +stream and 364 at four, against 56 and 175 without it on the same pieces (the table's 205 at four +is one 800-token translation run four times). --- diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 57ace73d..b4e70477 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6040,7 +6040,8 @@ TEST(gemma3_steps_lint_clean_at_one_and_two_ranks) { gemma3::step(&c, &batch); CHECK_LINT(b, &c); CHECK(c.step_fail.empty()); - CHECK_EQ(count_op(b, "all_reduce") + count_op(b, "ar_rmsnorm_quant_fp8") > 0, world == 2); + CHECK_EQ(count_op(b, "all_reduce") + count_op(b, "ar_rmsnorm_quant_fp8") + + count_op(b, "ar_rmsnorm_post_quant_fp8") > 0, world == 2); } }