gemma4k, arch/gemma4: norm_add_norm_route -- the attention join writes the routed arm's inputs

The 26B-A4B's router reads rmsnorm(x) * w_r * hidden^-0.5 and its experts rmsnorm(x) * w_e,
quantised to E4M3 for the fp8 gate_up: an `rmsnorm` and an `rmsnorm_quant_fp8` a layer, each
reading back the x norm_add_norm has just written and taking the sum of squares it has just taken.
At decode that is 60 launches a step (8.3 and 7.8 us under --profile-ops on 4x R9700); at a
prefill, eleven passes of each a layer per 20K-token request.

norm_add_norm_route is norm_add_norm, then from the norm factor it already holds the router's row
by rmsnorm's expression, ((x * s) * w_r) * mult, and the experts' codes as rmsnorm_quant_fp8 writes
them: the bf16-rounded norm, a scale of amax * (1 / 448) a 128 columns (1 for an all-zero group),
the value times its reciprocal clamped to +-448, one v_cvt_pk_fp8_f32. Element i is thread i % 256
at slot i / 256, so a group is four waves at one slot -- a wave's max, then the four through LDS.
Its own name rather than optional operands on norm_add_norm: the codes need whole 128-column
groups, which the plain join does not, and the arch falls back cleanly where it does not resolve.
A device row and a host arm (one helper with norm_add_norm's), the schema, OPS.md.

arch/gemma4 asks for it on every layer whose gate_up is block fp8 -- the user's fp8gu container,
every layer -- and then issues neither op_rnorm nor op_enorm; it declares both still, and
norm_add_norm and the chain behind it, for where it does not resolve. The routed arm's two norm
gains are declared ahead of the attention join now, which names them. The overlap takes either
join. A startup note counts the layers that take it.

The same rounding as the chain, but for the order a sum of squares is added in, as every join here:
g4k_emu holds the kernel to a chain summed in its block order bit for bit -- x, y, the router's
row, every code and scale -- at the 26B-A4B's 2816 at four heights, at 128, 3072, 3200 and 6144,
and with an all-zero group; against the host arm, no code more than a step off. The emulator gains
v_cvt_pk_fp8_f32, through the project's saturating encoder. arch_test: with fp8 gate_up experts the
route op resolves on all six layers, a decode step issues it and no rmsnorm_quant_fp8 and one
rmsnorm fewer a layer; refused, norm_add_norm and both launches; a tp-4 prefill through the
overlap issues it a slice and keeps every reduction ordered.

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 efff69be1d
commit 7a1ae22690
11 files changed
+449 -43

No files matched your search

+7 -4
View File
@@ -542,10 +542,13 @@ expert parity, so a rank holds an even number of experts.
**Kernels.** `--kernels gemma4k,libr4d,libavx`, **gemma4k first.** It serves all of the trunk's
attention -- the sliding layers (h256, gqa 2) and the full layers at gqa 8, 4 and 2 (one or two,
four, eight ranks) -- plus `pair_argmax` and the fusions: the two sandwich-norm joins
(`norm_add_norm`, `join_add_scale`), `expert_scale`, and `attn_paged_qrow`, the sliding layers'
attention reading q where the fused qkv projection left it. The plugin declares each one's unfused
form where nothing serves it -- the rmsnorm / add chain, `embed_lookup` + `mul`, a `cast` lifting q
for `attn_paged` -- and the startup notes say which were taken. The drafter's residual add, norm
(`norm_add_norm`, `join_add_scale`), `norm_add_norm_route` -- on a layer whose gate_up is block
fp8, the attention join writing the router's input and the experts' fp8 codes from the norm it
already takes, in place of an `rmsnorm` and an `rmsnorm_quant_fp8` -- `expert_scale`, and
`attn_paged_qrow`, the sliding layers' attention reading q where the fused qkv projection left it.
The plugin declares each one's unfused form where nothing serves it -- the rmsnorm / add chain,
`embed_lookup` + `mul`, a `cast` lifting q for `attn_paged` -- and the startup notes say which
were taken. The drafter's residual add, norm
and fp8 quantiser of x are one libr4d `rmsnorm_quant_fp8` launch an input (ten a draft pass), and
the `add` / `rmsnorm` / `quant_act_fp8` chain where nothing serves it. libr4d has h256/gqa2 and
h512/gqa8 rows as well, written for the dense 31B; with gemma4k first the 26B-A4B runs on the
+1 -1
View File
@@ -1859,7 +1859,7 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct at al
| `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) |
| `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) |
| `RADIANCE_G4K_WMMA` | `0` keeps `gemma4k`'s prefill attention on its scalar kernel instead of the WMMA one. An A/B switch and a way out if the matrix path misbehaves on a card; read once |
| `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `join_add_scale`), `embed_lookup` + `mul` for `expert_scale`, and a `cast` lifting q for `attn_paged` for `attn_paged_qrow`. An A/B switch; read at declare |
| `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `norm_add_norm_route`, `join_add_scale`), `embed_lookup` + `mul` for `expert_scale`, and a `cast` lifting q for `attn_paged` for `attn_paged_qrow`. An A/B switch; read at declare |
| `RADIANCE_GEMMA4_OVERLAP` | `0` runs every `arch/gemma4` prefill chunk whole, its all-reduces on the compute stream, instead of as two slices whose all-reduces ride a second stream beside the other slice's compute. An A/B switch; read at declare |
There is also a family of instruments for engine development — `RADIANCE_DEBUG_*` (residual probes,
+9
View File
@@ -299,6 +299,7 @@ rounding.
| op | params | operands |
|---|---|---|
| `norm_add_norm` | `M`(range) `n` `eps`:f64 `dtype` | `a`, `w_a`w, `x`:inout, `w_y`w → `y` |
| `norm_add_norm_route` | `M`(range) `n` `eps`:f64 `dtype` `mult`:f64 `group` | `a`, `w_a`w, `x`:inout, `w_y`w → `y`; `w_r`w → `r`; `w_e`w → `q`, `qs` |
| `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 |
| `attn_paged_qrow` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` |
@@ -315,6 +316,14 @@ differ from the chain only in the order a sum of squares is added in: with the s
bit-identical to it, and against a chain summed in another block width a norm factor can differ in
its last bit.
`norm_add_norm_route` is `norm_add_norm` and, from the same norm of the `x` it writes, a MoE
layer's two routed inputs: `r = rmsnorm(x) * w_r * mult` -- `rmsnorm`'s expression with `mult` --
and `q`, `qs` = `rmsnorm_quant_fp8` of `x` under `w_e`: E4M3 codes of the bf16-rounded norm, an
f32 scale per (row, `group` columns) of `amax / 448` (1 for an all-zero group), the value over it
clamped to +-448. `n` is whole groups and `group` 128. In Gemma 4 it replaces the router's
`rmsnorm` and the experts' `rmsnorm_quant_fp8`, two launches a layer that read back the `x` the
join had just written.
`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