Repository navigation
Description
Convert the flash attention kernels to HD=128
Conversion, not dual support. The trained model's head width is now 128 (SPEC §13.9: L=12, D=3072, H=24, HD=128). Every flash kernel that today specializes on HD == 64 should specialize on HD == 128 instead — the head width stays a single compile-time constant, there is no runtime dispatch between two fast paths, and the 64-wide specializations are deleted, not preserved. The only head-width-generic code that survives is the per-row oracle kernels (flash_attention_forward / _backward_q / _backward_kv), which already take head_dim at runtime and remain the parity reference.
1. Motivation
Post-multi-block profile (B200, L=12 B=12 T=2048 H=24 HD=128, per-row oracle fallback): attention is 97% of the 22.2 s step (forward ≈ 3.5 s, backward dQ ≈ 5.5 s, backward dK/dV ≈ 5.4 s at B=8; proportionally larger at B=12). Every other kernel in the model — all 12 blocks of tcgen05 GEMMs, MoE routing, lm-head, optimizer — totals ≈ 410 ms. Converting attention back onto the tcgen05 path at its demonstrated HD=64 efficiency projects the full step at ~500–700 ms, i.e. a ~30–40× whole-step win sitting behind this one conversion.
The fallback exists because both kernel generations hardwire 64:
- the fp32
*_tiledkernels size their SMEM tiles byTILE_HD = 64(gpu/flash-attn/src/lib.rs); - the tcgen05 kernels (
gpu/flash-attn/src/tcgen05.rs) bakeHD = 64intoTILE_BYTES = TILE * HD * 2, every SMEM plan, the TMA panel geometry, the MMA instruction descriptors, and the swizzledstmatrixstore addressing; gpu/model/src/lib.rsgates the tcgen05 path ontcgen05_attention_eligible(T, HD) := T % FLASH_TILE == 0 && HD == FLASH_HD, andHD != TILE_HDcurrently falls through to the per-row oracles (per_row_flash_configdispatch added with the multi-block refactor).
2. Inventory of baked-in 64s (the conversion surface)
| Site | What changes |
|---|---|
tcgen05.rs: const HD: usize = 64 |
→ 128. Everything below follows from it. |
TILE_BYTES = TILE * HD * 2 and all five SMEM plans |
doubled per tile; see §3 budget table — the persistent forward and the kv backward do not fit at TILE = 128 and must re-tile |
TMA staging panels [B*H, T, HD] + create_flash_head_tma_map |
panel row is 256 B instead of 128 B; box/swizzle geometry must be re-derived (see §4) |
stage_attention_heads_bf16 (lib.rs, PAIRS_PER_ROW = TILE_HD / 2) |
packs 128-wide head panels |
MMA descriptors: S = Q·Kᵀ reduction depth = HD |
K-dim 64 → 128 (either a K=128 descriptor if the shape table allows, or two chained 64-deep MMAs with enable_d) |
O += P·V output width = HD |
N-dim 64 → 128: TMEM output segment doubles; the softmax-warpgroup register drain (tcgen05_ld_16x256b) and correction path cover 2× the columns |
host.rs: FLASH_HD, FLASH_TILE, all *_SMEM_BYTES, *_BLOCK_THREADS, launch configs |
mirror the new constants; launch validation asserts 128 |
fp32 *_tiled kernels: TILE_HD = 64 |
→ 128, with FWD_BQ/BK etc. re-sized so the static SMEM stays under the 48 KiB static limit (or move to dynamic SMEM); these remain the mid-tier oracle for the tcgen05 kernels |
gpu/model/src/lib.rs: tcgen05_attention_eligible |
head_dim == 128. The per-row dispatch branch remains only as the non-aligned-shape oracle path (tiny-T parity configs), not as a second supported head width |
gpu/flash-attn/src/main.rs + bin/bench.rs harnesses (const HD) |
parity/bench shapes move to 128 |
gpu/model/src/main.rs aligned gates (HD = 64 in the tiny-model consts) |
aligned parity/overfit configs move to HD = 128 (e.g. D=256, H=2) so the tcgen05 attention path is actually exercised by the gate again |
3. SMEM budget at HD=128 (227 KiB/SM)
At TILE = 128: TILE_BYTES = 128·128·2 = 32 KiB.
| Kernel | Plan | At TILE=128 | Fits? |
|---|---|---|---|
| sync forward | 5 × TILE_BYTES |
160 KiB | ✅ |
| pipelined forward | (3 + 2·STAGES) × TILE_BYTES |
288 KiB @ 3 stages | ❌ (224 KiB @ 2 stages — knife-edge) |
| persistent forward | (2 + 2·STAGES + 4) × TILE_BYTES |
384 KiB | ❌ |
| backward dQ | 6 × TILE_BYTES |
192 KiB | ✅ |
| backward dK/dV | 8 × TILE_BYTES |
256 KiB | ❌ |
Two candidate shapes for the misfits — implementer's choice, but pick one and convert everything to it rather than keeping per-kernel geometry:
TILE = 64rows × 128 columns —TILE_BYTESreturns to 16 KiB and every existing plan fits unchanged (persistent = 192 KiB). Twice the key-tile iterations; MMA M-dim drops to 64 (shape table hasM64? verify — if MMA is M128-only, pair two query tiles per MMA). Grid doubles in query tiles, which atB·H = 288workstreams is fine for occupancy.- Keep
TILE = 128, split HD into two 64-wide panels — S-MMA runs K=64×2 chained accumulations, O and V handled as two column panels. SMEM for K/V doubles but P subtiles don't; per-plan math needs redoing case by case. More invasive in the softmax/correction path (two segment drains).
Option 1 is the conservative default: it preserves the one-tile-one-workstream structure and every barrier/pipeline invariant, only shrinking the row dimension.
4. Swizzle caution
At HD=64 a head-panel row is 64 bf16 = 128 B = exactly one SWIZZLE_128B period, so row index ≡ swizzle phase. At HD=128 a row is 256 B — the manual swizzled stmatrix writes and the TMA box geometry no longer agree by accident. Re-derive against the absolute-phase rule (physical address bits [9:7], see swizzle_probe), and re-run the probe before trusting parity numbers.
5. Gates
Standard ladder, all at HD=128:
flash-attnin-crate parity: tcgen05 kernels vs the per-row oracle and the fp32 tiled mid-tier (which convert first), forward and both backwards, including aTlarge enough to force multiple key tiles and the correction path.gpu/modelaligned parity + overfit gates exercising the tcgen05 attention path at HD=128 (tcgen05_attention_eligibleasserted active).- §10.1 same-container profile at the canonical
L=12 B=12 T=2048config: expectforward.attention.flash+backward.attention.flash_q/kvto fall from ≈ 21.6 s combined to the tens-of-milliseconds range, and the full step from ≈ 22.2 s to well under 1 s.
6. Non-goals
- No HD=64 fast path survives. If a future model wants 64 again, that's a config change to these same constants, not a second code path.
- No FA4 pipeline-architecture changes beyond what the re-tiling forces (stage counts, tile rows); scheduling/correction design stays as landed in FA4-shaped flash attention: warp-specialized tcgen05/TMA pipeline (forward first) #35.
- Per-row oracle kernels stay head-dim-generic and untouched.
Activity
feitreim commentedon Jul 23, 2026
Completed in #46 (merged as 2b22e54, SPEC 7e13). All three gate rungs green on B200: in-crate parity (CPU reference + cross-oracle vs per-row and fp32 tiled), all gpu/model gates at HD=128, and the profile — attention ≈15 ms/block vs ≈1.8 s/block on the per-row fallback (~120×), full step 81 ms/block. Remaining follow-ups (persistent forward stream-B regression, M64 operand-K mispairing) tracked in #47.
feat: multi-block MoE model — L-deep GpuBlock stack with split acts/s…