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 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-21 09:24:25 +02:00
1 parent 21d6b7836e
commit 3251975384
2 files changed
+21 -10

No files matched your search

+9 -7
View File
@@ -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);