Repository navigation
Design B tile-pairing: recover the wasted tensor-core half in the flash-attn backward kernels - #51
Merged
Merged
Conversation
Recover the half of the tensor core wasted on M128-over-64-row phantom rows in the HD=128 backward kernels by PAIRING two adjacent 64-row tiles into every M128_N64 MMA: operand A is stored as two stacked [128,64] subtiles (tile A in accumulator rows 0..63, tile B in 64..127, both real), score_mma_paired walks A at the TILE_BYTES subtile stride against the shared unpaired B at SUBTILE_BYTES, and a 128-thread warpgroup drains all 128 rows (warps 0-1 -> lanes 0..63, warps 2-3 -> 64..127). The causal edge masks against the row WITHIN its own tile (row & 63). Paired tiles are adjacent so their 128 rows are contiguous in the global tensors and merge_output_tile/store_grad_tile/LSE staging reuse unchanged. Applied to backward dQ (pair query tiles) and dK/dV (pair key tiles): block 64->128, grid halved, plans drop PHANTOM_PAD (7/8 * TILE_BYTES). Eligibility tightens to T % 128 == 0. Gradients keep the same tolerance as the unpaired kernels (dQ 7.7e-4, dK 7.3e-4, dV 2.9e-3); all 15 model gates green. Kernel bench [32,1024,24,128] backward: 9.28 ms / 118 TFLOP/s -> 5.42 ms / 202 TFLOP/s (1.71x). Forward NOT paired: the same pairing on the persistent forward is correct but regresses (226 -> 170 TFLOP/s) because fusing the pair into one warpgroup loses the two-warpgroup ping-pong that makes it fast; the forward stays on ping-pong. score_mma_paired / load_q_paired are the shared helpers kept for the backward kernels. Partially addresses #47.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recovers the half of the tensor core wasted on
M128-over-64-row phantom rows in the HD=128 flash-attention kernels, via Design B (issue #47 item 2): pair two adjacent 64-row tiles into everyM128_N64MMA so all 128 accumulator rows are live.M64is a proven dead end (the SS operand read broadcasts A-rows across subpartition pairs — only 32 distinct rows, SBO-invariant; see the #47 comments), so pairing is the only route.Partially addresses #47— lands on both backward kernels (a measured win); the forward pairing regresses and is left on the fast ping-pong (see below).Mechanics (shared by both backwards)
Ais stored as two stacked[128, 64]SWIZZLE_128Bsubtiles — tile A in accumulator rows 0..63, tile B in 64..127, both real (noPHANTOM_PAD).load_q_pairedplaces tile B at+SUBTILE_BYTESwithin each subtile; a 64-row half is a whole number of swizzle periods (8 rows), so the two box loads reproduce exactly the rows-64..127 swizzle of one 128-row tile.score_mma_pairedwalksAat theTILE_BYTESsubtile stride (its 128-row subtiles) against the shared unpairedB(K/Q/V/dY) atSUBTILE_BYTES— the only delta fromscore_mma. Thegrad_mma(dQ/dK/dV) is reused unchanged: itsA-operand[128,64]now has 128 real rows.(warp % 4)*32 .. +32). Passing the actual blockwarp_id(0..3) makestmem_row = warp_id*32cover 0..127 for free.row & 63) — the stacked rows 64..127 hold a second tile that restarts its causal count. (This was the one non-obvious bug found & fixed during bring-up.)merge_output_tile,store_grad_tile, and the LSE/dot staging reuse unchanged.Per-kernel
(2p, 2p+1)0..=tile_bkey > my_tile) →dS = 0;key == my_tile→ diagonal(2p, 2p+1)key_a..q < my_key) → masked;q == my_key→ diagonalBlock grows 64→128, grid halves, plans drop
PHANTOM_PAD(FLASH_BACKWARD_Q_SMEM7·TILE_BYTES,_KV_SMEM8·TILE_BYTES). Warp→lane map: warps 0–1 → rows 0..63, warps 2–3 → rows 64..127; the leader (tid 0) issues TMA/MMA.Tile-count parity
Pairing needs an even 64-row-tile count.
tcgen05_attention_eligibletightens toT % 128 == 0; odd-tile shapes fall to the per-row oracle (whole flash path, fwd+bwd). Canonical T=2048 and every gate shape already satisfy it, so nothing regresses to the oracle in practice. The synchronous backwards have no odd-tail machinery, so this is cleaner than threading ab_activepath.Forward: NOT paired (documented non-win)
The identical pairing on the persistent forward is correct (parity green on all shapes) but regresses: kernel bench
[32,1024,24,128]226 → 170 TFLOP/s. Fusing the tile pair into one 128-thread warpgroup sacrifices the two-warpgroup ping-pong (7e14) that made the persistent forward fast — the forward is scheduling-bound, not MMA-bound, so halving the MMA count loses more than it gains. The forward therefore stays on ping-pong (this branch reverts it toorigin/main);score_mma_paired/load_q_pairedare kept as the shared helpers the backwards use. Details posted as a comment on #47.Gates (B200)
./run.sh flash-attn flash— green. Backward vs the staged-bf16 CPU reference overT ∈ {128,256,1024}(T=1024 = multi-key-tile + correction), gradients bit-for-bit the same tolerance as the unpaired kernels:All three forwards still green (persistent restored to 1.927 ms / 227.4 TFLOP/s); probes green.
./run.sh model— all 15 gates green (Dense fwd/bwd+AdamW, Muon, Newton–Schulz, expert/MoE parity, MoE overfit 2.94→1.1e-5, block-linears overfit, checkpoint v4 bit-identical).Perf
Kernel bench
[32,1024,24,128](direct A/B, same harness):Forward unchanged (persistent 227.4 TFLOP/s).
BASELINE_REF=mainmodel A/B (§13.9, B=12 T=2048, 12 blocks): incomplete. The baseline profiled fully —backward.attention.flash_q66.5 ms,flash_kv78.7 ms, full step 748.8 ms — but the candidate side could not run: the Modal workspace was disabled mid-run (spend limit). The 1.71× kernel-bench number is the direct measured evidence; the model-shape candidate A/B is a follow-up once Modal is back.Files
gpu/flash-attn/src/tcgen05.rs—score_mma_paired,load_q_paired,backward_q_tile_paired,backward_kv_tile_paired, and the two rewritten paired backward kernels; backward SMEM plans; forward untouched vsorigin/main.gpu/flash-attn/src/host.rs— paired backward launch config (grid(T/128,H,B), 128 threads) + SMEM constants.gpu/model/src/lib.rs—tcgen05_attention_eligible→T % 128 == 0.SPEC.md— §13 entry 7e15.