Skip to content

The backward's MMA issue moves to a fifth warp, off the register pass's critical path (#94) - #96

Merged
feitreim merged 2 commits into
mainfrom
flash-bwd-issue-warp
Aug 5, 2026
Merged

feitreim merged 2 commits into
mainfrom
flash-bwd-issue-warp

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Remedy 1 of #94, stacked on #95 (the probe it is argued from and measured with). Base is flash-bwd-probe; the diff against it is tcgen05.rs, host.rs, the probe kept in step, and two pins.

The finding, in one line

#95's phase budget put 99% of a backward tile visit in warp 0's serial chain: the leader issued the tile's MMAs (2 108 / 2 628 ticks, 42–49% of the visit) and then ran its share of the register pass (1 641 / 2 708), while warps 1–3 sat at the block barrier waiting for it. Every actual wait in the loop was ≤ 7% — SCORED, the warpgroup waiting the score MMAs, was 91 ticks flat at every shape in both kernels.

The change

One more warp. FLASH_BACKWARD_BLOCK goes 128 → 160; warp 4 issues every TMA and every MMA and runs no pass, warps 0–3 run the pass and drain the accumulator and issue nothing. Both roles run key_tiles iterations and meet at the same single sync_threads per tile, so the barrier counts match whichever branch a warp is in:

issue warp                          pass warps (0-3)
  refill, score_mmas(i+1)             scored.wait(i), register pass -> dS
  ---------------- sync_threads ----------------
  gradient mma(i)                     (next tile)

Every ring, every semaphore, BACKWARD_STAGES, GRADIENT_STAGES, the shared plan, the TMEM layout and the arithmetic are untouched. The only change is which warp calls mma_* and tma_load, and leader moving from thread 0 to ISSUE_WARP * 32.

This is not the warp specialization #75 deleted. That was 192 threads with a separate TMA warp and MMA warp, six barrier sets, and a per-query statistic re-staged through a shared ring because specialization had removed the block sync it hid behind. It lost because of that structure, not because a dedicated issuer is wrong — and #95's budget is what says which.

Numbers

./run.sh flash-attn flash, [32,1024,24,128], base and branch measured in clean containers:

base this PR
forward 0.832 ms 0.832 ms untouched, to the digit
backward q 1.094 ms 0.681 ms −37.7%
backward kv 1.566 ms 1.404 ms −10.3%
both backwards 2.659 ms 2.088 ms −21.5%

Clean standalone profile, §13.9 shape, against the base's own standalone profile:

row base this PR Δ
backward.attention.flash_q 21.3278 12.0008 −9.3270 (−43.7%)
backward.attention.flash_kv 29.7978 25.9788 −3.8190 (−12.8%)
backward.attention.flash_dot 5.8842 5.8181 −0.0661
forward.attention.flash 14.5538 15.3318 +0.7780 (untouched; container drift)
backward attention 57.010 43.798 −13.212 (−23.2%)
full step 355.8166 346.2896 −9.5270

Backward attention goes from 16.0% of the step to 12.6%. These are two containers, and the untouched forward drifting +0.78 is the scale of that; the same-container A/B below is the claim.

Same-container A/B, --baseline-ref 9060469, marked === baseline === / === candidate === pair. This is a throttled container (784 ms absolute where a standalone profile of the same base reads 355.82 — 2.20×, the known hazard), so the row deltas are the measurement:

row base this PR Δ
backward.attention.flash_q 34.8131 27.3228 −7.4903
backward.attention.flash_kv 59.3172 55.7274 −3.5898
backward.attention.flash_dot 5.0894 5.0646 −0.0248
forward.attention.flash 27.8190 27.8106 −0.0084
full step 783.9489 772.0171 −11.9318

Named rows sum to −11.11 against a −11.93 step — they account for the whole of it. The unmarked pair the same run emits reproduces it independently: flash_q 34.7574 → 27.9273 (−6.83), flash_kv 59.4506 → 56.7879 (−2.66).

With the order reversed (this branch profiled first, main as the candidate — compare_profile always runs the retained ref first, so the branch is passed as --baseline-ref):

row this PR main Δ
backward.attention.flash_q 26.5830 33.6965 −7.1135
backward.attention.flash_kv 53.9742 57.2021 −3.2279
backward.attention.flash_dot 5.0160 5.0077 +0.0083
forward.attention.flash 27.7026 27.6061 +0.0965
full step 763.8668 773.8053 −9.9385

Order does not matter: −7.49 / −3.59 one way and −7.11 / −3.23 the other, with the named rows accounting for the step delta in both (−11.11 of −11.93, and −10.34 of −9.94).

The honest claim is the ~−11 to −13 ms the named rows carry, ~3.2–3.7% of a clean step.

Why kernel A converts and kernel B mostly does not — the probe again

#95's instrument moved with the kernel (the two roles are now two warps, so it reports each warp's own wait at the shared barrier: SYNC for the pass warps, the new IDLE for the issue warp). Same shape, before and after:

ISSUE PASS IDLE SYNC visit
backward q, before 2 108 1 641 — 52 4 390
backward q, after 1 354 2 329 946 38 2 565
backward kv, before 2 628 2 708 — 54 6 281
backward kv, after 1 768 5 314 4 004 48 ~5 980

Two things move at once, and they are the whole result:

  1. ISSUE gets cheaper — 2 108 → 1 354 and 2 628 → 1 768. The MMA issue was contending with the register pass; alone in its own warp it runs ~35% faster.
  2. PASS gets more expensive — 1 641 → 2 329 (×1.42) and 2 708 → 5 314 (×1.96). Before, warp 0 measured a pass it largely ran alone, because warps 1–3 had finished theirs and were waiting; now all four run it together and contend.

Kernel A wins because ×1.42 is much less than the serial sum it replaces. Kernel B barely wins because ×1.96 nearly eats it: its pass does two swizzled operand stores where A's does one (Pᵀ and dSᵀ against dS alone) and two sub_col_assign broadcasts, and it parallelizes across four warps worse than A's does. Why it parallelizes worse is not established here — it is the next question, not a claim in this PR.

IDLE of 946 and 4 004 says the same thing from the issue warp's side: both kernels are now pass-bound, kernel B overwhelmingly. PASS is 90.4% and 89.2% of the pass warps' column. That retargets #94's remedy list — remedy 3 (split the pass across two warpgroups) moves ahead of remedy 2, and kernel B is where it is worth the most.

Gates

  • --kernel flash-attn --bin flash — green. Parity is byte-identical to the base at every gated shape: dq 7.299e-4 / 5.203e-4 / 6.458e-4, dk 7.322e-4 / 7.437e-4 / 7.084e-4, dv 2.933e-3 / 2.717e-3 / 2.911e-3, y ≤ 1.469e-3, lse ≤ 9.537e-7 — the same digits as the baseline run, which is the point: no arithmetic changed, only which warp issues. The forced-correction shape still corrects on 100.00% of its 32 eligible visits and every other shape on 0.00%.
  • --kernel flash-attn — green (the two-oracle gate).
  • --kernel model — green, all 14 checks, including both MoE substitutions, aligned_tcgen05_linears, checkpoint v5 and all four overfit gates. The MoE overfit lands 2.949803 → 0.000031 against the recorded 0.000028; the backward's arithmetic is unchanged, so that is the scatter-atomic nondeterminism and nothing else.
  • --kernel ops — green.
  • ptxas / KernelBudget: forward 128 / 0 unchanged; backward q 182 → 189, backward kv 190 → 198, both still zero spill. Pins ratcheted to the measured numbers. The rise is .maxntid, as it was in One flash-attention backward per direction, at one block barrier a key tile (#69) #75 and The flash forward rescales O inside its own tensor-memory segment, and the last .local frame in the module goes to zero #77: the kernels declare 160 threads where they declared 128, so ptxas' budget went from 65536/128 to 65536/160 — 409 per thread against 512 — and it spent seven and eight more of a smaller allowance. Occupancy is unmoved: residency is pinned at 1 CTA/SM by BACKWARD_TMEM_COLUMNS = 512 either way, and 160 threads is five warps of the SM's 64 slots.
  • Training smoke: --kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100 — 75 553 tokens/s, 33.55% MFU over 99 measured steps, loss 11.107 → 8.010. Against the ~73k / 32.5% this front opened on that is +3.5%, which is barely outside the ~2.9% main-vs-main spread MoE atomics give this run — the row-level A/B above is what carries the claim, and this is the "it still trains" gate.

What this does not do

BACKWARD_STAGES stays 3 and GRADIENT_STAGES stays 2 — #95 priced the whole feed at ≤7% of a visit and this PR does not touch it. MmaShape::M128_N64 stays, so the tensor core still runs at ~30% of the SM's peak while busy; that is #94's remedy 2 and it is no longer the leading term now that ISSUE is off the critical path and IDLE is 946 / 4 004. The register pass is what is left, at ~90% of both kernels.

@feitreim
feitreim changed the base branch from flash-bwd-probe to main August 5, 2026 17:06
@feitreim
feitreim merged commit 741f55b into main Aug 5, 2026
@feitreim
feitreim deleted the flash-bwd-issue-warp branch August 5, 2026 17:06
feitreim added a commit that referenced this pull request Aug 5, 2026
…me TMEM lanes (#94)

Remedy 3 of #94. `FLASH_BACKWARD_BLOCK` goes 160 -> 288: where warps 0-3 ran
the whole register pass over the accumulator's 128 TMEM lanes, warps 0-7 now
run it over the *same* 128 lanes with the score band's columns split between
the two warpgroups. Warp 8 still issues every TMA and every MMA and runs no
pass, as #96 left it. Every ring, every semaphore, `BACKWARD_STAGES`,
`GRADIENT_STAGES`, the shared plan, the TMEM layout and the arithmetic are
untouched.

The contract is ferro-kittens#193's: a warp addresses the 32 TMEM lanes at
`warp_id() % 4` and its warpgroup does not matter. It is spelled
`tmem::warp_lanes()` and never `32 * warp_id`, because from warpgroup 1 the
latter names a lane past the 128 that exist and reaching past the bound does
not fault -- it silently reads the reader's own quadrant at the same offset.

The accumulator drain splits with the pass, so `OutBand` is gone and `OutHalf`
is the widest fp32 value in the module.

`backward kv` -27 to -28% across four containers, `backward q` flat: A spends
its pass saving on issue slack and lands on `MmaShape::M128_N64`'s roof, which
is #94's remedy 2 arriving. B's pass halves cleanly, which answers #96's open
question -- it was latency-bound, not throughput-bound. Registers fall 192 ->
128 and 200 -> 140 at zero spill. Parity is byte-identical: the column split
changes no summation order.
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