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

+12 -3
View File
@@ -20,6 +20,12 @@
#include <string>
#include <vector>
/* 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)