Repository navigation
Convert the flash attention kernels to HD=128 - #46
Merged
Merged
Conversation
…-half stride (<<17)
…tom-read pad; sync+pipelined+backward parity green
… persistent in main gate; correct M128_N64 doc comments
This was referenced Jul 23, 2026
feitreim
added a commit
that referenced
this pull request
Jul 23, 2026
…lock model Integrates the two parallel lines: the L=12 multi-block MoE refactor and the tcgen05 flash HD=128 conversion. Canonical profile/train config is L=12 D=3072 H=24 HD=128 FF=4096 B=12 T=2048 C=6144; tiny gates keep the D=512/H=4/HD=128 shapes and raised overfit budgets from #46.
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.
Converts every flash-attention kernel from
HD == 64toHD == 128(SPEC §13.9:L=12, D=3072, H=24, HD=128). Single compile-time head width, no dual-head dispatch, the 64-wide fast paths deleted; only the head-dim-generic per-row oracle kernels stay.Closes #42.Tiling decision (issue §3 Option 1)
TILE = 64rows, soTILE_BYTESreturns to 16 KiB and all five SMEM plans fit the 227 KiB/SM budget with the #35 pipeline/barrier structure intact. Each 128-wide operand is stored as two stacked 64-wide (128-byte-row)SWIZZLE_128Bsubtiles. This is the key refinement over a naive[TILE, 128](256-byte-row) tile: the swizzle phase still equals the row index inside each subtile — the coincidence HD=64 gave for free (issue §4) — so the manualstmatrixP/dS writes need no re-derivation.S = Q·Kᵀ, the backwardSᵀ/dP) walk 8 K=16 chunks across the two subtiles.O = P·V, the gradient MMAs) split into two N=64 accumulations, one per V/K subtile.1/√128literal (0.088388…); a.sqrt()would pull in libdevice and reject the pure-PTX path.Swizzle re-derivation
Because every SMEM tile stays 64-wide (128-byte rows),
row ≡ phaseholds inside each subtile exactly as at HD=64, and the existing absolute-phase handling (phase = (base >> 7) & 7,chunk ^ ((row + phase) & 7)) is unchanged. Theswizzle_probe/check_swizzle_layoutharness confirms it empirically on the new layout:TMA 128B placement matches chunk ^ ((row + 1) & 7).MMA shape: the M64 wall and the M128-over-64-row fix
The natural shape for a 64-row tile is
M64_N64. Empirically (isolated with row-, column-, and K-encoded TMEM probes), the tcgen05M64shape mis-pairs operand A/B K-indices for accumulator rows ≥16 across multi-chunk K accumulation — a single K-chunk or a uniform operand hides it, which is why the failure only surfaces on a realQ·Kᵀ. The lane→row map itself is fine (M64packs 16-row groups into subpartition halves, lane = 2×row).The fix keeps the proven
M128shape over the 64-row tiles: the tensor core computes 128 M-rows, the unused rows 64..128 stream aTILE_BYTESphantom tail past each operand's base, and the drain reads rows 0..63 with the originallane = rowfragment map — noM64semantics anywhere. One extraTILE_BYTES(PHANTOM_PAD) per SMEM plan keeps the phantom reads in bounds; the garbage they pull lands in accumulator rows that are never drained.Gate results (B200)
1. flash-attn in-crate parity (
./run.sh flash-attn flash) — sync + pipelined forward and both backward kernels vs the staged-bf16 CPU reference, overT ∈ {128, 256, 384, 512, 1024}andB·Hup to 152 workstreams;T=1024forces multiple key tiles and the correction path:swizzle probe ✓, score_mma (2-subtile M128) probe 1.9e-6,
exp27.5e-5 /log21.9e-7. Kernel bench [32,1024,24,128]: pipelined forward 181 TFLOP/s, sync forward 125, sync backward 118.The in-crate cross-oracle gate (
./run.sh flash-attn, issue §5) checks tcgen05 against both the per-row oracle (naive) and the fp32 tiled mid-tier, overT ∈ {128,256,1024}:(per-row and tiled oracles also cross-check each other; persistent excluded, see below.)
2. gpu/model gates (
./run.sh model) at HD=128 — all pass: dense fwd/bwd + AdamW, Muon, Newton–Schulz, expert fwd/bwd, and MoE substitution match CPU; aligned tcgen05 MoE overfits (2.94 → 1e-6), fused AdamW / Muon tiny-batch overfit (→ 0), aligned block linears match CPU and overfit (→ 1e-5 / 0), checkpoint v3 resumes bit-identically. The tiny gates moved toD=512, H=4, HD=128(four heads keep the overfit converging;D/H=128matchesTILE_HD, which is now the single supported head width for the tiled kernels).3. §10.1 profile at the §13.9 shape (
B=12 T=2048 D=3072 H=24 HD=128, single block, tcgen05 pipelined):A ~120× win on the attention kernels. (A same-container
BASELINE_REF=mainA/B is not apples-to-apples here: main'sprofile.rsis HD=64, and main's HD=128 model path is the per-row oracle the issue already documents at 21.6 s.)Caveats / open issues
Both tracked in #47:
bin/flash.rsandmain.rs) and the model runs on the pipelined (phase-2) forward meanwhile. Sync and pipelined forwards + both backwards are the verified paths.M64→M128-over-64-row workaround wastes half the MMA rows; still ~120× faster than the per-row fallback. RevisitingM64(or a documented operand layout that pairs correctly) would recover that. Flash HD=128 follow-ups: persistent stream-B regression + M64 operand-K mispairing (M128 waste) #47 records the full probe evidence for the K-mispairing.SPEC §13 gets a 7e13 milestone entry in this PR.