Skip to content

Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (#88) - #90

Closed
feitreim wants to merge 1 commit into
rope-tablefrom
stage-qkv-rope
Closed

feitreim wants to merge 1 commit into
rope-tablefrom
stage-qkv-rope

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Phase 2 of #88. Stacked on #89 (rope-table) — base that first; the diff below is against it.

#89 made the rotations cheap. This makes them free: on the tcgen05 path there is no rotation pass at all, no split, and no staging pass either — one kernel reads the projection's fp32 [N,3D] panel and writes the three packed-bf16 head panels flash streams.

The kernel

stage_qkv_heads_bf16 substitutes for split_group3 + two rope_forward passes + three stage_attention_heads_bf16 launches. Per packed output pair it reads three fp32 couples (Q, K, V at their group strides) and one (cos, sin) couple, rotates Q and K, folds softmax_scale · log2(e) into Q, quantizes RNE, and writes three words at the head-major index the existing staging pass already computed. Reads and writes stay the coalesced runs the unfused staging had; the table adds 8 bytes a lane out of a 1 MiB L2-resident buffer.

Everything it drops was fp32 storage between two kernels — an fp32 expression stored and reloaded as fp32 is a no-op — so the words it writes are the composition's bit for bit, and --kernel flash-attn asserts that at zero tolerance at three tile-aligned shapes rather than at a tolerance. The unfused kernels stay as the oracles it is checked against (§11); stage_attention_heads_bf16 keeps its own bit-exact CPU mirror and is still what stages dY.

The storage change (SPEC §7.1)

acts.q/k/v were [N,D] fp32 ×3, 10.1 GiB live, and §7.1 already had them flagged as deferred candidates: their only consumer was the staging pass that quantizes them. They are now the staged panels themselves — GpuBlockActs::attention is an AttentionOperands enum, Staged on the tcgen05 contract and Wide (the fp32 triple, the split, the two rotation passes) for every shape its oracle kernels serve. That is the documented decision change, written up in §7.1 with the audit's own question applied: q/k/v hold nothing accumulated or compared, RoPE is a pointwise rotation rather than an accumulation, and it now happens before the single rounding instead of between two of them.

Two consequences beyond the forward:

  • The backward stops re-staging Q/K/V. They are per-block saved activations the forward already wrote, so FlashAttentionScratch is down to dY — a backward temporary, one buffer for every block — and its correction counts.
  • −5.4 GiB of workspace. 10.86 GiB of fp32 triples becomes 5.43 GiB of packed panels.

Nothing else reads the panels (checked); the CPU-parity reference compares model outputs, not attention operands, and needed no change.

Measurement

Same-container A/B against #89's head, §13.9 shape. (Same caveat as #89: these compare_profile containers run the tcgen05 GEMMs 3–6× slower than a clean one — absolute step 810 ms where a standalone profile of ea8e147 reads 401.8 ms and the training smoke below reproduces the brief's 62.4k tok/s exactly. Both halves saw it; the row deltas are the claim.)

row baseline (#89) candidate Δ
forward.qkv_proj.split 7.2301 — −7.2301
forward.q_rope 2.5332 — −2.5332
forward.k_rope 2.5356 — −2.5356
forward.attention.stage_bf16 7.4171 — −7.4171
forward.attention.stage_qkv — 5.0431 +5.0431
backward.attention.stage_bf16 9.9074 2.4968 −7.4106
full step 809.5306 780.7185 −28.8121 (−3.56%)

Four forward rows totalling 19.72 ms become one 5.04 ms row. The fused pass is cheaper than the staging pass alone was: it reads the same bytes as three coalesced runs out of one buffer instead of three kernels each walking a different one.

Backward staging drops to a quarter — three of its four operands are now panels the forward already wrote.

Rows touched sum to −22.09 against a −28.81 step. The remaining −6.7 is nine GEMM rows each drifting −0.3 to −0.9, all the same direction; I am not claiming them. If it is not drift it is the freed VRAM, and the honest statement is that this change is worth the −22.09 it can account for.

VRAM: 134.8 → 129.4 GiB used, free headroom 43.5 → 49.0 GiB (+12.6%) — the 5.4 GiB the fp32 triples cost.

Gates

  • --kernel flash-attn — green, including the new zero-tolerance fused-vs-composition staging gate at [1,128,2], [2,256,3], [1,1024,4].
  • --kernel flash-attn --bin flash — green: forced-correction gate (ramped-key reference) passes, and every KernelBudget lands exactly on its pin — forward 128/0, backward q 182/0, backward kv 190/0. No rise, nothing to ratchet.
  • --kernel model — green. Note this exercises the Wide arm only (its shapes are T=4), which is the same coverage the staging path has always had in that binary; the Staged arm is gated by --kernel flash-attn and by the training smoke.
  • Training smoke: --kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100, same shard, main vs this stack:
main ea8e147 #89 + #90
step=1 11.106886 11.106920
step=10 8.991953 9.006647
step=50 8.107739 8.075316
step=90 7.835263 7.650436
step=100 8.119326 8.000919
tokens/s 62345.2 73109.9 (+17.3%)
MFU 27.68% 32.46% (+4.78 pp)
step time 394.2 ms 336.2 ms (−14.7%)

Trajectories are indistinguishable — they differ from step 1 in the fifth decimal, which is the MoE scatter-atomic nondeterminism, and neither run is systematically above the other. The baseline run reproducing the brief's 62.4k tok/s / 27.70% MFU to three digits is also what says the compare_profile containers above, not this pair, are the anomalous ones.

  • No GEMM epilogue touched, so no --kernel gemm.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Note on the shas: both branches were rustfmt-ed after the measurements above. The diff from the measured commit is line wrapping only (git diff -w reduces it to nothing but a joined argument list) — measured 7e948cf / 29511f3, now 1ce7cee / 7782e5c.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Correction and the cleaner numbers. Both compare_profile containers above were throttled (absolute step ~810 ms where a standalone profile of the same baseline reads 401.8), and the ~10 ms of same-direction GEMM drift I attributed there to the freed VRAM is an artifact of that, not a real effect. Standalone profiles of both trees — one container each, no contention, ./run.sh model profile — say so:

row main ea8e147 #89+#90 Δ
forward.qkv_proj.split 7.4433 — −7.4433
forward.q_rope 8.5304 — −8.5304
forward.k_rope 8.5300 — −8.5300
forward.attention.stage_bf16 7.9263 — −7.9263
forward.attention.stage_qkv — 5.3946 +5.3946
backward.attention.stage_bf16 10.6321 2.7417 −7.8904
backward.q_rope 8.5950 — −8.5950
backward.k_rope 8.5831 — −8.5831
backward.qkv_proj.join 7.0409 5.0046 −2.0363
the eight rows 67.2811 13.1409 −54.14 (−80.5%)
full step 401.8066 348.7404 −53.07 (−13.2%)

Rows sum to −54.14 against a −53.07 step — a 1.1 ms cross-check, where the throttled pairs disagreed by 6–10. Every neighbouring row is flat: forward.qkv_proj.gemm 12.19 → 12.24, forward.attention.flash 14.61 → 14.34, backward.attention.flash_kv 26.44 → 27.46, backward.qkv_proj.weight_gemm 16.97 → 17.17. Nothing outside the data path moved, which is the claim these PRs should be read as making.

These are two separate containers, so they are corroboration and not the §10.1 gate; the gate is the three same-container pairs (both orders), which agree in sign and rows, and the same-shard training smoke (62345 → 73110 tok/s, 27.68% → 32.46% MFU).

The residual is 13.14 ms in three rows, and #91 is the one of them that is still a fusion rather than compulsory traffic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant