Skip to content

Fix persistent flash forward stream-B regression (HD=128); restore it as the model forward - #48

Merged
feitreim merged 1 commit into
mainfrom
fix/flash-hd128-followups
Jul 23, 2026
Merged

feitreim merged 1 commit into
mainfrom
fix/flash-hd128-followups

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Fixes the persistent (phase-3) forward's stream-B regression from the HD=128 conversion (#46), re-includes it in both parity gates, and switches the model back onto it — it profiles faster than the pipelined forward it temporarily replaced.

Partially addresses #47 — lands item 1 (stream-B regression). Item 2 (M64 operand-K mispairing) is left open with findings posted as a comment on the issue.

Root cause: a TMEM lane-access / warp-placement bug

Stream B (query tile 2p+1) output exactly 0 and a too-large LSE; stream A was correct. It was not a TMEM-column overlap or an operand bug — the column layout (S_A 0..64, S_B 64..128, O_A 128..256, O_B 256..384) is correct and fits 512.

A tcgen05.ld warp can only reach TMEM lanes (warp_id % 4) * 32 .. +32. A 64-row tile's M128 accumulator keeps its real rows in lanes 0..63 (the phantom rows 64..127 are never drained). When the conversion dropped TILE from 128 to 64, each stream shrank from a full 4-warp warpgroup to 2 warps, and the dispatch left stream B on warps 2–3 (warpgroup-0 positions 2–3 → hardware lanes 64..127). So stream B's softmax drained the phantom rows: the garbage inflated the running max, every P = exp2(s − m_ref) underflowed to ~0, and y came out 0 with a corrupted LSE — which is why both y and lse were wrong, not just y. The stale launch_bounds(320) over a 192-thread block was the tell: at HD=64/TILE=128 the streams were warps 0–3 and 4–7, each naturally draining all 128 lanes.

Fix: move stream B to warps 4–5 (warpgroup-1 positions 0–1 → lanes 0..63) and put the TMA/MMA warps on 2–3. A pure relabel of the dispatch; the block stays 192 threads and every per-stream offset is unchanged.

Model switched back to persistent (it's faster)

Kernel bench [32,1024,24,128]: persistent 1.937 ms / 226.2 TFLOP/s vs pipelined 2.425 ms / 180.7 TFLOP/s vs sync 3.526 ms / 124.2. So flash_attention_forward_into is restored to forward_persistent + flash_persistent_config (reverting 3e2c823).

Item 2 (M64 operand-K mispairing): not addressed

Not cracked and not shipped — the M128-over-64-row workaround stays. It's a genuine operand-K addressing issue, distinct from the item-1 lane bug; doc research + analysis are written up in a comment on #47 for a future pass. (transpose_b_probe is retained.)

Gates (B200)

  • ./run.sh flash-attn flash — sync + pipelined + persistent forwards and both backward kernels vs the staged-bf16 CPU reference over T ∈ {128,256,384,512,1024}, B·H up to 152; probes green (swizzle, score_mma 1.9e-6, exp2/log2). Persistent max abs: y 1.5e-3, lse 7.3e-5.

  • ./run.sh flash-attn — cross-oracle parity incl. persistent vs per-row and tiled oracles: y 2.4e-3, lse 1.2e-3.

  • ./run.sh model — all 15 gates green with the model on the persistent forward (Dense fwd/bwd+AdamW, Muon, Newton–Schulz, expert/MoE parity, MoE overfit 2.94→5e-6, AdamW/Muon tiny overfit →0, block linears overfit →7e-6/0, checkpoint v3 bit-identical).

  • BASELINE_REF=main ./run.sh model profile — apples-to-apples vs main (both HD=128; baseline main runs the pipelined forward, candidate runs persistent) at §13.9 (B=12 T=2048 D=3072 H=24 HD=128, single block):

    kernel baseline main (pipelined) candidate (persistent)
    forward.attention.flash 3.073 ms 2.450 ms (−20%)
    backward.attention.flash_q 5.764 ms 5.765 ms
    backward.attention.flash_kv 6.777 ms 6.708 ms
    full step 81.70 ms 80.51 ms

    Only the forward changed (backward kernels are identical on both branches); the persistent forward saves ~0.62 ms/block on attention.

Stream B ran on warps 2-3 (warpgroup-0 positions 2-3), but a tcgen05.ld
warp reaches only TMEM lanes (warp%4)*32..+32 and a 64-row tile's M128
accumulator keeps its real rows in lanes 0..63. Stream B thus drained the
undrained phantom rows (lanes 64..127): the inflated max underflowed every
P=exp2(s-m_ref) to ~0, so y=0 with a corrupted LSE. The TILE 128->64
conversion shrank each stream from a 4-warp warpgroup to 2 warps and left
B off a warpgroup boundary.

Move stream B to warps 4-5 (warpgroup-1 positions 0-1 -> lanes 0..63) and
the TMA/MMA warps to 2-3; pure relabel, block stays 192. Re-include
persistent in both parity harnesses and switch the model forward back to
forward_persistent (it profiles ~20% faster than pipelined:
forward.attention.flash 3.07 -> 2.45 ms at the sec-13.9 shape).

Partially addresses #47 (item 1); item 2 (M64 operand-K mispairing) left
open with findings in an issue comment.
@feitreim
feitreim merged commit 09accb8 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