gemma4k, arch/gemma4: expert_scale -- per_expert_scale on the routing weights in one launch

Gemma 4 multiplies each renormalised routing weight by per_expert_scale of the expert it chose.
In the conventional vocabulary that is an `embed_lookup` of the [n_expert, 1] table by the ids
and a `mul` into the weights -- two launches a MoE layer, a few dozen elements each at decode
(--profile-ops: ~0.26 and ~0.22 ms a step across the 30 layers). gemma4k's `expert_scale` is the
pair as one kernel, ew = bf16(ew * w_e[id]), with an id outside the table reading 0 as
embed_lookup's missed row does: one product rounded once, so the pair's value bit for bit.

Like the joins it has no libref row on purpose, so arch/gemma4 falls back to the pair wherever
gemma4k is not loaded, and RADIANCE_GEMMA4_FUSE=0 turns it off with them. 30 launches a step the
fewer.

g4k_emu: device and host rows against the pair over 32 shapes -- top-k 1 and 8, pitched rows,
pads and one-past-the-end ids, rows past M untouched -- every element equal. arch_test: a decode
step scales in one launch, and in two with it refused. docs/OPS.md: the row and its paragraph;
GUIDE: what the switch now covers.

Not yet run on the cards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-12 14:40:04 -05:00
1 parent 29f981fd6c
commit 0d95046f6f
11 files changed
+289 -25

No files matched your search

+9 -3
View File
@@ -294,12 +294,13 @@ rounding.
-log(max_timescale) / max(n/2 - 1, 1))`, each step in f32 as the reference's torch expressions
are, then rounded.
### The sandwich-norm joins — no reference row
### gemma4k's fusions: the sandwich-norm joins and `expert_scale` — no reference row
| op | params | operands |
|---|---|---|
| `norm_add_norm` | `M`(range) `n` `eps`:f64 `dtype` | `a`, `w_a`w, `x`:inout, `w_y`w → `y` |
| `join_add_scale` | `M`(range) `n` `eps`:f64 `dtype` | `d`, `w_d`w, `r`, `w_r`w, `w_j`w, `x`:inout, `s`w, `w_n`w? → `y`? |
| `expert_scale` | `M`(range) `n_expert` `top_k` `dtype` | `ids`, `w_e`w, `ew`:inout |
Gemma's sandwich norms, each one kernel in place of a chain of `rmsnorm`, `add` and `mul_weight`.
`norm_add_norm` is `t = rmsnorm(a) * w_a; x += t; y = rmsnorm(x) * w_y` -- a post-norm, the
@@ -312,11 +313,16 @@ the activation dtype at the same point, so the two differ from the chain only in
of squares is added in: with the same order they are bit-identical to it, and against a chain
summed in another block width a norm factor can differ in its last bit.
**libref has no row for either, on purpose.** An architecture asks for a join and declares the
`expert_scale` is `ew[t, k] = ew[t, k] * w_e[ids[t, k]]` over `[M, top_k]`, `w_e` an `[n_expert]`
table and an id outside it reading 0: Gemma 4's per_expert_scale on the renormalised routing
weights, in place of an `embed_lookup` of the table by the ids and a `mul` into the weights. One
product rounded once, so the pair's value bit for bit.
**libref has no row for any of them, on purpose.** An architecture asks for a join and declares the
chain where it does not resolve (`rad_op_resolved`), and a reference row would resolve it
everywhere -- so a model loaded without the library that serves the join would resolve it to the
reference and be refused at serve, instead of running the chain on libr4d or libavx. The
`gemma4k` kernel library declares both (device and host rows); with no oracle, its kernels are held
`gemma4k` kernel library declares them (device and host rows); with no oracle, its kernels are held
to the chain bit for bit by `tests/g4k_emu`, and no row declares `replaces`, because rad-kbench
assembles a replaced chain from the fusing library's own rows and gemma4k has none of the chain's
ops.