Skip to content

Design B tile-pairing: recover the wasted tensor-core half in the flash-attn backward kernels - #51

Merged
feitreim merged 1 commit into
mainfrom
perf/flash-m128-pairing
Jul 23, 2026
Merged

feitreim merged 1 commit into
mainfrom
perf/flash-m128-pairing

Conversation

@feitreim

Copy link
Copy Markdown
Owner

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 every M128_N64 MMA so all 128 accumulator rows are live. M64 is 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)

  • The paired operand A is stored as two stacked [128, 64] SWIZZLE_128B subtiles — tile A in accumulator rows 0..63, tile B in 64..127, both real (no PHANTOM_PAD). load_q_paired places tile B at +SUBTILE_BYTES within 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_paired walks A at the TILE_BYTES subtile stride (its 128-row subtiles) against the shared unpaired B (K/Q/V/dY) at SUBTILE_BYTES — the only delta from score_mma. The grad_mma (dQ/dK/dV) is reused unchanged: its A-operand [128,64] now has 128 real rows.
  • The drains run a 128-thread warpgroup: warps 0–1 reach TMEM lanes 0..63, warps 2–3 reach 64..127 ((warp % 4)*32 .. +32). Passing the actual block warp_id (0..3) makes tmem_row = warp_id*32 cover 0..127 for free.
  • Causal edge compares the key/query column against the row within its own tile (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.)
  • Paired tiles are adjacent, so their 128 rows are contiguous in the global tensors → merge_output_tile, store_grad_tile, and the LSE/dot staging reuse unchanged.

Per-kernel

kernel pairs streams mask (per warp-half)
backward dQ two query tiles (2p, 2p+1) key tiles 0..=tile_b future tile (key > my_tile) → dS = 0; key == my_tile → diagonal
backward dK/dV two key tiles (2p, 2p+1) query tiles key_a.. query before this key (q < my_key) → masked; q == my_key → diagonal

Block grows 64→128, grid halves, plans drop PHANTOM_PAD (FLASH_BACKWARD_Q_SMEM 7·TILE_BYTES, _KV_SMEM 8·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_eligible tightens to T % 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 a b_active path.

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 to origin/main); score_mma_paired/load_q_paired are kept as the shared helpers the backwards use. Details posted as a comment on #47.

Gates (B200)

  1. ./run.sh flash-attn flash — green. Backward vs the staged-bf16 CPU reference over T ∈ {128,256,1024} (T=1024 = multi-key-tile + correction), gradients bit-for-bit the same tolerance as the unpaired kernels:

    dQ dK dV
    T=128 7.66e-4 7.32e-4 2.93e-3
    T=256 5.31e-4 6.70e-4 2.86e-3
    T=1024 6.46e-4 7.26e-4 2.79e-3

    All three forwards still green (persistent restored to 1.927 ms / 227.4 TFLOP/s); probes green.

  2. ./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):

unpaired (main) paired speedup
backward 9.28 ms / 118.1 TFLOP/s 5.42 ms / 202.0 TFLOP/s 1.71×

Forward unchanged (persistent 227.4 TFLOP/s).

BASELINE_REF=main model A/B (§13.9, B=12 T=2048, 12 blocks): incomplete. The baseline profiled fully — backward.attention.flash_q 66.5 ms, flash_kv 78.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 vs origin/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.

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.
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