Repository navigation
Description
Tracking issue for the passes that traverse the same Q/K/V bytes back to back around flash attention. Baseline: main @ ea8e147, standalone ./run.sh model profile, 401.81 ms full step (398.14 ms in kernels) at the §13.9 shape (L=12, B=12, T=2048, D=3072, H=24, HD=128, E=8, K=2).
Measured state
| row | ms | % step |
|---|---|---|
forward.qkv_proj.split |
7.4433 | 1.85% |
forward.q_rope |
8.5304 | 2.12% |
forward.k_rope |
8.5300 | 2.12% |
forward.attention.stage_bf16 |
7.9263 | 1.97% |
backward.attention.stage_bf16 |
10.6321 | 2.65% |
backward.q_rope |
8.5950 | 2.14% |
backward.k_rope |
8.5831 | 2.14% |
backward.qkv_proj.join |
7.0409 | 1.75% |
| total | 67.28 | 16.74% |
The chain is: the qkv GEMM writes an fp32 [N,3D] panel → split_group3 reads and writes it → RoPE reads and writes Q and K → stage_attention_heads_bf16 reads them and writes packed bf16 → flash reads the bf16. Four traversals of the same bytes where one does. The backward mirrors it: flash backward writes fp32 dQ/dK/dV → two rotation passes → join_group3 → the weight GEMM quantizes.
Diagnosis, in two parts
1. RoPE was not bandwidth-bound at all. Per layer a rotation moves 604 MB and took 0.71 ms — 850 GB/s, roughly an eighth of what the staging pass beside it achieves on comparable traffic. bench.rs's bench_rope comment already named the mechanism (#70): the kernel is one thread per element, and each thread computes a powf, a sin and a cos. Two threads redo the same angle for the two halves of one rotated pair, and each loads both halves, so the pass is 2× the compulsory reads on top of ~6 SFU-class ops per element.
The angles are a function of (position, pair) only — T × HD floats, 1 MiB at the training shape, L2-resident and re-read N·H/T times. Nothing about them needed to be in the inner loop.
2. Everything between the GEMM and flash is pure storage. By §7.1's own question — is anything accumulated or compared in this buffer — the split output, the rotated Q/K, and the fp32 acts.q/k/v triple are all storage between two kernels whose only consumer quantizes them. §7.1 flagged q/k/v as deferred candidates for exactly this reason.
Plan
- RoPE's angles come from a table, and the backward rotation folds into the join (RoPE reads its angles from a table, and the backward rotation folds into the join (#88) #89). Table built host-side with
nn::Rope's own expression, so the CPU reference and every device consumer share one set of angles. Rotation kernels become one thread per pair.join_group3_ropesubstitutes forrope_backwardtwice +join_group3; gated bit-exactly against that composition. - Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (#88) #90).
stage_qkv_heads_bf16reads the fp32[N,3D]panel and writes the three head-major bf16 panels — split, rotate, scale, quantize and relayout in one pass. The panels become the per-block saved activation (§7.1 decision change), so the backward re-stages only dY.
Both are exact substitutions: every intermediate dropped was fp32 storage between two kernels, so the fused kernels reproduce the composition bit for bit, and that is what the gates assert rather than a tolerance.
Not attempted, and why
The qkv GEMM epilogue. The owner's framing allowed either the epilogue or the flash operand load path. The load path was taken: the epilogue is a Blackwell pipeline stage (TMEM drain → smem swizzle → TMA store) that #80's whole campaign put at the dominant cost, its tile mapping is [256,256] MN-major rather than head-major, and the rotation crosses column pairs inside a tile. The staging pass already reads exactly these bytes and already writes exactly the layout flash wants, so the fusion costs one kernel there and nothing anywhere else. Revisit only if the staging row becomes the residual.
Activity
feitreim commentedon Aug 5, 2026
Both phases shipped: #89 (table + fused backward join) and #90 (fused rotate-and-stage, staged panels as the saved activation, stacked on #89).
Cumulative, same container, reverse order
BASELINE_REF=<stack> ./run.sh model profilewith main mounted as the candidate, i.e. the delta below is what going back to main costs, negated. Running it this way round is the order check on the two forward-order pairs in #89 and #90.ea8e147forward.qkv_proj.splitforward.q_ropeforward.k_ropeforward.attention.stage_bf16forward.attention.stage_qkvbackward.attention.stage_bf16backward.q_ropebackward.k_ropebackward.qkv_proj.joinBoth orders agree: the two forward-order pairs sum to −55.2 ms, this reverse-order pair reads −61.9 ms, same rows and same sign. The ~10 ms the step moves beyond the eight rows is nine GEMM rows each drifting 0.4–2.4 ms in the same direction in both runs — consistent enough to be real (the freed 5.4 GiB is the only candidate) but not something either PR claims.
End to end
--kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100, same shard:What is left of it
12.26 ms across three rows, against 63.68:
forward.attention.stage_qkv5.04 — one pass, reading[N,3D]fp32 and writing[N,3D/2]words. This is the compulsory traffic; it is not obviously improvable without moving it into the qkv GEMM epilogue, which is the option this issue deliberately declined.backward.attention.stage_bf162.53 — dY, one operand, same shape. Same story.backward.qkv_proj.join4.68 — reads dQ/dK/dV fp32 and writes the fp32[N,3D]panel thatbackward.qkv_proj.weight_gemmthen quantizes twice (Every step GEMM gets a cuBLASLt denominator at its exact shape and mode, and the fold, not the walk, is what lags #79's note). This is the remaining fusion here:join_group3_ropecould write packed bf16 directly and delete both quantizes, on the same §7.1 argument that carried q/k/v —scratch.qkvon the backward side is written by one kernel and read only by consumers that quantize it. Worth its own issue; it also shrinks thebackward.qkv_proj.weight_gemmrow rather than the join.feitreim commentedon Aug 5, 2026
Correction and the cleaner numbers. Both
compare_profilecontainers 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:ea8e147forward.qkv_proj.splitforward.q_ropeforward.k_ropeforward.attention.stage_bf16forward.attention.stage_qkvbackward.attention.stage_bf16backward.q_ropebackward.k_ropebackward.qkv_proj.joinRows 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.gemm12.19 → 12.24,forward.attention.flash14.61 → 14.34,backward.attention.flash_kv26.44 → 27.46,backward.qkv_proj.weight_gemm16.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.
Merge pull request #89 from feitreim/rope-table