libr4d, arch: the 31B and DFlash2 with fewer launches a step, same bytes

Attention reads q and the gqa-8 drafter's KV in place (attn_paged_qrow); the 31B's attention
prologue stores k and v itself (qkv_norm_rope_kv_store); DFlash2's norm/rope/cache-store chains
and add-then-norm pairs run as one launch each. Carries the launch-cliff padding until that PR
merges.

Squashed from:

- libr4d: attn_paged_qrow on the split-KV decode kernel -- h256/gqa2, and h128/gqa8 by two groups a kv head
- arch: the 31B's sliding layers and its drafter read through attn_paged_qrow where a card serves it
- libr4d: step past the full-card launch cliff in qkv_norm_rope and the row norms
- libr4d: qkv_norm_rope describes its operands, so rad-kbench and rad-tune can time it
- libr4d, arch/gemma4_bf16: qkv_norm_rope_kv_store -- the 31B's prologue stores k and v itself
- libr4d, arch/common: qkv_norm_rope takes v_norm; DFlash2's query pass folds its q/k chain
- libr4d, arch/common: qkv_norm_rope takes n_head 0; DFlash2's context pass folds its key chain
- libr4d, arch/common: add_rmsnorm; DFlash2 issues each residual add with the norm after it

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:48:20 +02:00
1 parent 04e5480387
commit 015cba8837
20 files changed
+1332 -164

No files matched your search

+16 -7
View File
@@ -764,13 +764,22 @@ The grid's groups never cross a row, so the codes are those of three separate ru
the replies are too. On libr4d the skinny GEMM picks its in-block K split from N (k and v alone
split K in two, the fused GEMM does not), so k and v can round differently in the last bit and a
long greedy reply can part from the old container's late. `qkv_norm_rope` and `kv_store` work on
the row's column slices in place. A global layer's
attention reads q in the row (`attn_paged_qrow`, the windowed kernel that serves head_dim 512 under
`attn_paged` too); a sliding layer's reads it there in a one-row step, and a taller step copies q
out with a `cast` for libr4d's h256/gqa2 kernels, which take 0.26-0.71 of the windowed kernel's
time. Without `qkv_norm_rope` (a CPU-only engine) q, k and v are copied out for the unfused
prologue. A container that holds the three apart serves as before: the plugin asks the weight's
encoding, which rad-convert answers from the recipe and a container from its entries.
the row's column slices in place. Attention reads q in the row through `attn_paged_qrow`: a global
layer at every height (the windowed kernel, which serves head_dim 512 under `attn_paged` too), a
sliding layer up to 32 query rows a sequence (libr4d's h256/gqa2 split-KV decode with q at a pitch,
so a verify step included); a taller sliding step (a prefill) copies q out with a `cast` for the
h256/gqa2 prefill kernel, which takes 0.43-0.67 of the windowed kernel's time. Without
`qkv_norm_rope` (a CPU-only engine) q, k and v are copied out for the unfused prologue. A container
that holds the three apart serves as before: the plugin asks the weight's encoding, which
rad-convert answers from the recipe and a container from its entries.
**The 31B's drafter at gqa 8.** z-lab's 31B drafter is 64 heads over 8 kv heads. Where a card
serves `attn_paged_qrow` at head_dim 128, gqa 8 and q_len 16 (libr4d's gqa-4 split-KV decode with
each kv head read by two query groups), the drafter's cache holds each kv head once and attention
runs at gqa 8. The op has no host rows, so the plugin asks it before the drafter's KV group, whose
head count follows the answer. Elsewhere the cache holds each kv head twice and attention runs at
gqa 4, the kv heads copied into place with a `cast` a head and copy (80 launches a draft step at
tp 4).
**The 31B with a DFlash2 drafter.** RedHatAI's `gemma-4-31B-it-speculator.dflash2` is z-lab's
DFlash2 architecture trained with vLLM's speculators library: five Qwen3-shaped layers at 5376 (32
+29 -3
View File
@@ -449,7 +449,11 @@ dense block by a `cast`. It has its own name because other `attn_paged` kernels
(libr4d's with `RAD_E_STRIDE`, at the first step), and under `attn_paged` the first library in
`--kernels` wins the op, an order the architecture cannot see. Asked for by name, the op resolves to
a library that reads the pitch or to nothing, and the architecture copies `q` for `attn_paged` in
the second case.
the second case. In libr4d the windowed kernels serve it at every height; the split-KV decode kernel
serves it at h256/gqa2 over its decode band (q_len up to 32, ahead of the windowed rows there), and
at h128/gqa8 up to q_len 16 by reading each kv head for two query groups of 4 (a drafter's cache
then holds each kv head once). Since no host library declares the op, a plugin that asks for it
learns whether a card serves the geometry.
libref has no row for any of them, deliberately. An architecture asks for a fusion and declares
the chain where it does not resolve (`rad_op_resolved`); a reference row would make it resolve
@@ -821,6 +825,7 @@ bytes).
| `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt `head_dim`/opt `ladder`/opt | `positions` → `cos_sin` |
| `qk_norm_rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `wadd`:f64/opt `v_norm`/opt `sections`/opt `comps`/opt | `qkv`:inout, `positions`?, `q_w`w?, `k_w`w? |
| `qkv_norm_rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `ladder`/opt `wadd`:f64/opt `v_from_k`/opt `v_norm`/opt `dtype` | `q`:inout, `k`:inout, `v`:inout, `positions`, `q_w`w, `k_w`w |
| `qkv_norm_rope_kv_store` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `ladder`/opt `wadd`:f64/opt `v_from_k`/opt `dtype` `v_norm`/opt `block_size` `kv_dtype` `k_scale`:f64/opt `v_scale`:f64/opt | `q`:inout, `k`:inout, `v`:inout, `positions`, `q_w`w, `k_w`w, `slot_mapping`, `kv_cache`:inout |
**`attn_paged`**'s band is `q_len`, not `M`: one query row at decode, `1 + n_spec` at a speculative
verify, thousands in a prefill chunk, and libr4d has a different kernel for each band.
@@ -937,9 +942,19 @@ the same reason: its angles must stay `rope`'s exactly.
**`qkv_norm_rope`** (device-only) is the same prologue over separate `q`, `k` and `v` projections
(Gemma 4's text attention, three GEMMs): the q and k norms, the unit-gain value norm and the rotary
in one dispatch. `v_from_k` 1 takes v from the raw k before the k norm (the k == v global class).
in one dispatch. `v_from_k` 1 takes v from the raw k before the k norm (the k == v global class);
`v_norm` 0 leaves v unnormed (a Qwen3-shaped layer: two norms and two rotaries). `n_head` 0 is the
k (and v) heads alone, `q` and `q_w` passed but neither read nor written: a pass that writes only
keys and values (a block drafter's context pass).
The rotary parameters are `rope`'s, `ladder` included. Byte-identical to `rmsnorm_p`, two
`rmsnorm`s and two `rope`s.
`rmsnorm`s and two `rope`s (one each at `n_head` 0).
**`qkv_norm_rope_kv_store`** (device-only) is `qkv_norm_rope` followed by `kv_store` in one launch:
the block of each k head (and of each v head, or under `v_from_k` the k head's block for both) ends
holding its token's finished head, so it also writes it into the paged cache at the token's slot.
Its parameters are the two ops' own, its operands the prologue's followed by `slot_mapping` and
`kv_cache`. Byte-identical to the pair at either cache width: the store converts each element as
`kv_store` does, and q, k and v are still written in place as the prologue leaves them.
| op | params | operands |
|---|---|---|
@@ -2829,6 +2844,17 @@ Blocks are `block_size` rows starting at row 0, so a tap never reaches into the
`[T, 2, taps, NG]` coefficient projection. `out` must not alias `x`. libr4d's kernel serves
`taps` 2, `group_size` 16 and a power-of-two `block_size`; libref and libavx any.
`add_rmsnorm` (device-only) is the drafter's residual add and the norm after it in one pass:
| op | params | operands |
|---|---|---|
| `add_rmsnorm` | `M`(range) `n` `eps`:f64 `dtype` `wadd`:f64/opt `mult`:f64/opt | `x`:inout, `a`, `w`w → `y` |
It writes `x + a` into `x`, rounded as `add` rounds it, then `y = rmsnorm(x) * (w + wadd) * mult`
over the `x` it wrote, at `rmsnorm`'s width and column partition: byte-identical to `add` followed
by `rmsnorm`, on both outputs. That is where it differs from `rmsnorm_add`, which normalises the
unrounded sum: a drafter whose trained graph is the pair keeps its bits.
`dflash_select` is the greedy walk over the candidate lattice: `cand`/`unary` are `row_topk`'s two
output planes at one row per draft position, `hp` the selector's rank-256 query, and `anchor` the
step's own token ids (`anchor_stride` is the block width, because a draft pass carries `block` ids a