From 32519753842792e49078b5882916a04210c737e1 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Fri, 16 Oct 2026 12:48:57 -0500 Subject: [PATCH] arch/gemma4_bf16: the embedding scale as the reference rounds it, 73.5 and not 73.32 Gemma4TextScaledWordEmbedding multiplies the looked-up rows by `embed_scale.to(weight.dtype)`, which is sqrt(5376) rounded to bf16: 73.5. gemma4_bf16 multiplied the trunk's embedding by the unrounded 73.32, and the assistant head's entry, which reads the trunk's embedding module, did the same. The first layer's norm hides a 0.25% scale, but the residual stream every later layer adds to does not. arch/gemma4 has taken the bf16 value since it was written (53.0 at 2816). Both now take embed_scale(n_embd), the bf16-rounded value. arch_test's two checks of the scale expect 73.5, and its three pinned gemma4_bf16 graphs move with the `mult` they record; no other model's pin moves. On 4x R9700 at tp 4 the replies move against main's, as a 0.25% scale on the residual stream would (7 of 7 long prompts, drafter off). A drafter trained on the reference's hidden states tracks the corrected stream better: z-lab's DFlash drafter (the next change) accepts 2.88 tokens a step over seven 2-6K-token prompts against 2.71 with the scale unrounded, and 5.21 against 4.91 on a code prompt (270 against 259 tok/s). Co-Authored-By: Claude Opus 5.5 --- arch/gemma4_bf16/gemma4_bf16.cpp | 15 ++++++++++++--- tests/arch_test.cpp | 16 +++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index a5e4c9bc..7b2cabbe 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -20,6 +20,12 @@ #include #include +/* sqrt(n_embd) rounded to bf16: the reference's embedding module multiplies the looked-up rows by + * `embed_scale.to(bf16)`, 73.5 at 5376 and not 73.32. */ +static double embed_scale(int64_t n) { + return rad_bf16_to_f32(rad_f32_to_bf16((float)std::sqrt((double)n))); +} + namespace gemma4_bf16 { using namespace rad::arch; @@ -828,7 +834,7 @@ int Gemma4Head::declare(RadBuilder* b, Names& nm, const RadModelMeta* meta, cons RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n_embd", n_trunk), RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype), RAD_INT("vocab_offset", g.vocab_off), - RAD_F64("mult", std::sqrt((double)n_trunk))), + RAD_F64("mult", embed_scale(n_trunk))), RAD_WEIGHTS(w_tok)), {}, {b_e}); if (g.world > 1) @@ -1264,12 +1270,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* The embedding is scaled by sqrt(n_embd) at the lookup, not in the table (as in llama.cpp's * build_inp_embd and HF's ScaledWordEmbedding), so the plain table stays tied to the head. - * Without it the token signal in the residual stream would be ~73x too small. */ + * Without it the token signal in the residual stream would be ~73x too small. The scale is + * sqrt(n_embd) rounded to bf16, as the reference multiplies by it (embed_scale: 73.5 + * at 5376, not 73.32). The first norm hides the difference but the residual stream does not, + * and the DFlash drafter was trained on the reference's hidden states. */ m.op_embed = rw(b, RAD_OP(b, "embed_lookup", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n_embd", g.n_embd), RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype), RAD_INT("vocab_offset", g.vocab_off), - RAD_F64("mult", std::sqrt((double)g.n_embd))), + RAD_F64("mult", embed_scale(g.n_embd))), RAD_WEIGHTS(m.w_tok)), {}, {m.b_x}); if (g.world > 1) diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 09c6a90c..c57ac30c 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -5502,10 +5502,11 @@ TEST(gemma4_final_norm_softcap_and_vocab) { CHECK(find_map(b, "output.weight") != nullptr); /* The embedding is scaled by sqrt(n_embd) at lookup (as the reference's build_inp_embd and - * HF's ScaledWordEmbedding do), not baked into the table, so the table stays tied to the head. */ + * HF's ScaledWordEmbedding do), not baked into the table, so the table stays tied to the head. + * HF multiplies by `embed_scale.to(bf16)`, so sqrt(5376) = 73.32 is 73.5. */ const RecOp* el = find_op(b, "embed_lookup", 0); CHECK(el != nullptr); - CHECK_NEAR(pflt(el, "mult"), std::sqrt(5376.0), 1e-9); + CHECK_NEAR(pflt(el, "mult"), 73.5, 1e-9); } TEST(gemma4_every_op_declares_the_buffers_it_touches) { @@ -5806,11 +5807,12 @@ TEST(gemma4_assistant_is_a_serial_head_with_no_cache_of_its_own) { REQUIRE(mpre != nullptr); CHECK_EQ(mpre->src[0], std::string("draft.pre_projection.weight")); - /* The entry embeds through the trunk's table with the trunk's scale. */ + /* The entry embeds through the trunk's table with the trunk's scale, sqrt(5376) rounded to + * bf16 as the reference's embedding module holds it. */ const RecOp* el = find_op(b, "embed_lookup", 1); REQUIRE(el != nullptr); CHECK_EQ(pint(el, "n_embd"), 5376); - CHECK_NEAR(pflt(el, "mult"), std::sqrt(5376.0), 1e-9); + CHECK_NEAR(pflt(el, "mult"), 73.5, 1e-9); CHECK(el->w.size() == 1 && find_op(b, "embed_lookup", 0)->w == el->w); for (const auto& o : b.ops) @@ -9017,13 +9019,13 @@ struct ArchCases { lfm2_bf16::step, 0, {}, 0x1775b188dc4a5120ull); add("gemma4_bf16", [this, sc](RadBuilder* b) { auto c = sc(1, 0); return gemma4_bf16::declare(b, &g4, &c); }, - gemma4_bf16::step, 0, {}, 0x0cbeb3ccf57a622cull); + gemma4_bf16::step, 0, {}, 0xafc23087c634a537ull); add("gemma4_bf16, w4a8", [this, sc, g4w4](RadBuilder* b) { g4w4(b); auto c = sc(1, 0); return gemma4_bf16::declare(b, &g4, &c); }, - gemma4_bf16::step, 0, {}, 0x0fbef815d409efd1ull); + gemma4_bf16::step, 0, {}, 0x719fa07788f8b98aull); add("gemma4_bf16, w4a8, two ranks", [this, sc, g4w4](RadBuilder* b) { g4w4(b); auto c = sc(2, 0); return gemma4_bf16::declare(b, &g4, &c); }, - nullptr, 0, {}, 0x3d3d4c65aeefe59aull); + nullptr, 0, {}, 0x93b174ab52cc0d31ull); add("gemma4", [this, sc](RadBuilder* b) { auto c = sc(1, 0); return gemma4::declare(b, &g4m, &c); }, gemma4::step, 0, {}, 0x0f227f5853b6488cull);