libr4d: mla_attn_rows_bf16 -- one key load for all 64 heads of a query row

A third mla_attn row, at 64 heads. A workgroup takes one query row and all its heads, four waves
to each group of sixteen, and loads each 16-token key chunk once for all of them, where the
matrix row loads it once a tile of sixteen. The scores are K Q^T, tokens as the tile's rows and
heads as its columns, so a head's running maximum and denominator stay in its lane; the waves of
a group each take a quarter of the latent's dot products and add the four partials in one order,
and the probabilities reach the value product through a permlanex16 swap instead of LDS. The
next chunk's keys are fetched into registers while the current one is multiplied, two barriers a
chunk. A row's bytes do not depend on the other rows of the batch. Under 32 rows the launcher
runs the matrix row's kernel; r4d_mla_rows_pin_form, a test hook, pins the element-wise key load,
32 heads a workgroup, the rows kernel at any row count, or the matrix row's kernel at every row
count (the form a KL measurement needs to tell this kernel's share of a row split's difference).

Measured on gfx1201 (r4d_selftest --perfmla, one sequence past the DSA budget at 16k of
context, 513 blocks): 512 rows x 64 heads take 3.9 ms, 35 TFLOP/s, against 12.3 ms on the
matrix row, and the 4096 rows x 8 heads they stand for in a TP8 prefill chunk 14.0 ms on the
paired union tile. Against the wave row the output stays within the matrix row's tolerance
(max abs 2.4e-4 at 513 blocks).

r4d_selftest checks it against libref at 1 to 33 rows, 32 and 64 heads, every fill and both key
loads, and times it against the matrix row (--case mla_attn_rows_bf16, --perfmla at 64 heads).
r4d_emu_mla runs it on the host against an f64 softmax, its forms bit for bit against each
other, a run of rows against the same rows of the whole batch, and a mixed TP step (decodes, a
verify window, a prefill past the budget, a fresh chunk) split by heads over the matrix row and
by rows over this one, at runs that do and do not divide the step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 committed 2026-10-22 09:58:07 +00:00
1 parent 123c439684
commit 87701cb6b8
8 files changed
+645 -66

No files matched your search

+13 -1
View File
@@ -2836,7 +2836,7 @@ contributes nothing, and a row with no visible key outputs zero. The softmax is
rounded once. `scale` is positive and finite and comes from the original query/key head width,
not the latent width (GLM: `1/sqrt(256)` against a latent of 512).
libr4d has two `mla_attn` rows, both at `latent_dim` 512, `block_size` 4 and up to 64 heads. The
libr4d has three `mla_attn` rows, all at `latent_dim` 512, `block_size` 4 and up to 64 heads. The
matrix row, `mla_attn_matrix_bf16`, ranks above the wave row and takes heads in multiples of 8: the
heads are the WMMA tile's rows, so all of them share each latent load, and the probabilities enter
it as bf16 high and low parts with f32 accumulation. At a multiple of 16 a tile holds 16 heads of
@@ -2848,6 +2848,18 @@ by table position, with a second pass where the two differ. Other head counts ta
workgroups and merge the f32 partials before narrowing, into the fixed scratch their hooks
reserve; without scratch they compute unsplit.
At exactly 64 heads the rows row, `mla_attn_rows_bf16`, ranks above both. A workgroup takes one
query row and all its heads, four waves to each group of sixteen, and loads each 16-token key chunk
once for all of them, where the matrix row loads it once a tile of sixteen. The scores are
`K Q^T`, tokens as the tile's rows and heads as its columns, so a head's running maximum and
denominator stay in its lane; the four waves of a group each take a quarter of the latent's dot
products and add the four partials in one order, and the probabilities reach the value product
through a lane swap instead of LDS. The next chunk's keys are fetched while the current one is
multiplied. A row's output does not depend on the other rows of the batch, so a run of rows gives
the bytes the whole batch would. Under 32 rows (a decode step at one rank) too few workgroups fill the card, and its launcher
runs the matrix row's kernel instead. It also takes multiples of 32 heads (two groups a
workgroup), which no constraint routes to it.
`mla_project` moves a query into the latent and a latent result out of it, through the bf16
weight `b` `[heads*2*head_dim, latent_dim]` that stores each head's key rows, then its value rows.
`mode=absorb` multiplies `a` `[M, heads, head_dim]` by the head's key block as stored,