Skip to content

Convert the flash attention kernels to HD=128 #42

Closed

Description

@feitreim

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 *_tiled kernels size their SMEM tiles by TILE_HD = 64 (gpu/flash-attn/src/lib.rs);
  • the tcgen05 kernels (gpu/flash-attn/src/tcgen05.rs) bake HD = 64 into TILE_BYTES = TILE * HD * 2, every SMEM plan, the TMA panel geometry, the MMA instruction descriptors, and the swizzled stmatrix store addressing;
  • gpu/model/src/lib.rs gates the tcgen05 path on tcgen05_attention_eligible(T, HD) := T % FLASH_TILE == 0 && HD == FLASH_HD, and HD != TILE_HD currently falls through to the per-row oracles (per_row_flash_config dispatch 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:

  1. TILE = 64 rows × 128 columns — TILE_BYTES returns 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 has M64? verify — if MMA is M128-only, pair two query tiles per MMA). Grid doubles in query tiles, which at B·H = 288 workstreams is fine for occupancy.
  2. 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:

  1. flash-attn in-crate parity: tcgen05 kernels vs the per-row oracle and the fp32 tiled mid-tier (which convert first), forward and both backwards, including a T large enough to force multiple key tiles and the correction path.
  2. gpu/model aligned parity + overfit gates exercising the tcgen05 attention path at HD=128 (tcgen05_attention_eligible asserted active).
  3. §10.1 same-container profile at the canonical L=12 B=12 T=2048 config: expect forward.attention.flash + backward.attention.flash_q/kv to 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

Activity

  1. added
    enhancement
    on Jul 23, 2026
  2. feitreim commented on Jul 23, 2026

    @feitreim
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions