Skip to content

Convert the flash attention kernels to HD=128 - #46

Merged
feitreim merged 13 commits into
mainfrom
perf/flash-hd128
Jul 23, 2026
Merged

feitreim merged 13 commits into
mainfrom
perf/flash-hd128

Conversation

@feitreim

@feitreim feitreim commented Jul 23, 2026 •

Copy link
Copy Markdown
Owner

Converts every flash-attention kernel from HD == 64 to HD == 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 = 64 rows, so TILE_BYTES returns 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_128B subtiles. 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 manual stmatrix P/dS writes need no re-derivation.

  • HD-deep MMAs (S = Q·Kᵀ, the backward Sᵀ/dP) walk 8 K=16 chunks across the two subtiles.
  • HD-wide MMAs (O = P·V, the gradient MMAs) split into two N=64 accumulations, one per V/K subtile.
  • Softmax scale is the 1/√128 literal (0.088388…); a .sqrt() would pull in libdevice and reject the pure-PTX path.
  • TMA descriptor box stays 64 columns; each panel is two loads (HD offsets 0 and 64) into stacked subtiles.

Swizzle re-derivation

Because every SMEM tile stays 64-wide (128-byte rows), row ≡ phase holds inside each subtile exactly as at HD=64, and the existing absolute-phase handling (phase = (base >> 7) & 7, chunk ^ ((row + phase) & 7)) is unchanged. The swizzle_probe / check_swizzle_layout harness 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 tcgen05 M64 shape 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 real Q·Kᵀ. The lane→row map itself is fine (M64 packs 16-row groups into subpartition halves, lane = 2×row).

The fix keeps the proven M128 shape over the 64-row tiles: the tensor core computes 128 M-rows, the unused rows 64..128 stream a TILE_BYTES phantom tail past each operand's base, and the drain reads rows 0..63 with the original lane = row fragment map — no M64 semantics anywhere. One extra TILE_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, over T ∈ {128, 256, 384, 512, 1024} and B·H up to 152 workstreams; T=1024 forces multiple key tiles and the correction path:

quantity max abs error
y 1.1e-3 (1.5e-3 at [4,256,38])
lse 7.2e-5
dq 7.7e-4
dk 7.3e-4
dv 2.9e-3

swizzle probe ✓, score_mma (2-subtile M128) probe 1.9e-6, exp2 7.5e-5 / log2 1.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, over T ∈ {128,256,1024}:

quantity max abs error (vs per-row / tiled)
forward y 2.4e-3
forward lse 1.2e-3
dq 2.7e-3
dk 1.7e-3
dv 5.4e-3

(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 to D=512, H=4, HD=128 (four heads keep the overfit converging; D/H=128 matches TILE_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):

kernel this branch (tcgen05) per-row oracle baseline (issue §1)
forward.attention.flash 2.98 ms ≈ forward.attention part of ~1.8 s/block
backward.attention.flash_q 5.52 ms
backward.attention.flash_kv 6.57 ms
combined attention ≈15 ms/block ≈1.8 s/block (≈21.6 s across 12 blocks)
full step 81 ms/block ≈22.2 s (12 blocks)

A ~120× win on the attention kernels. (A same-container BASELINE_REF=main A/B is not apples-to-apples here: main's profile.rs is 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:

  • Persistent (phase-3) forward deferred. Its two-Q-tile ping-pong has a stream-B regression under this conversion (stream A correct, stream B outputs 0). It's excluded from the parity gate (both bin/flash.rs and main.rs) and the model runs on the pipelined (phase-2) forward meanwhile. Sync and pipelined forwards + both backwards are the verified paths.
  • The M64→M128-over-64-row workaround wastes half the MMA rows; still ~120× faster than the per-row fallback. Revisiting M64 (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.

@feitreim
feitreim merged commit 2b22e54 into main 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.
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