Skip to content

The backward's register pass splits across two warpgroups over the same TMEM lanes (#94) - #98

Merged
feitreim merged 1 commit into
mainfrom
flash-bwd-pass-split
Aug 5, 2026
Merged

feitreim merged 1 commit into
mainfrom
flash-bwd-pass-split

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Remedy 3 of #94, stacked on #97 (the ferro e26c75c pin bump it needs and is measured against). Base is flash-bwd-pin-bump; the diff against it is tcgen05.rs, the probe kept in step, one host.rs doc line and two pins.

The change

FLASH_BACKWARD_BLOCK 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, split by column: PASS_GROUPS = 2 warpgroups, PASS_COLUMNS = TILE / 2 of the score band each. Warp 8 still issues every TMA and every MMA and runs no pass, exactly as #96 left it.

let band = warp_lanes();              // 32 * (warp_id % 4) — the same 32 lanes from either warpgroup
let group = warp_id / DRAIN_WARPS;    // which half of the columns
...
let mut column = group * PASS_COLUMNS;
let last = column + PASS_COLUMNS;
while column < last { /* unchanged, to the character */ }

The whole of it rests on ferro-kittens#193: a warp addresses the 32 TMEM lanes at warp_id() % 4, its warpgroup does not matter, and it addresses no others. That was measured on a B200 at these kernels' own shapes — the [32, 16] chunk this pass reads, the [32, 128] band it drained, the [32, 64] half it drains now.

It is spelled tmem::warp_lanes(), never 32 * warp_id. From warpgroup 1 the latter names a lane past the 128 that exist, and crossing that bound does not fault: it silently reads the lane at the same offset inside the reader's own quadrant. #193's table is where that is established and its const { assert!(M <= 32) } guards are what make the taller spelling of the same mistake a compile error.

No new fence and no new barrier. The pass warps write dS (and P) through the generic proxy into shared memory and meet the issue warp at the one sync_threads a tile that #75 left there; nothing here stores to tensor memory, so store_wait has nothing to cover that it did not already cover in the forward.

The drain splits with the pass: each warpgroup takes DRAIN_COLUMNS = HD / 2 of its accumulator band rather than all HD, so OutBand is gone and OutHalf — the [32, 64] the forward already used — is the widest fp32 value in the module.

What it buys

./run.sh flash-attn flash, [32,1024,24,128]. Two independent container pairs, because backward kv has a ±3.5% spread across containers and one pair could not tell −27% from it:

#97 base this PR Δ second pair
forward 0.840 0.839 — 0.832 → 0.843 (untouched)
backward q 0.680 0.682 +0.3% 0.680 → 0.683
backward kv 1.500 1.077 −28.2% 1.499 → 1.092 (−27.1%)
both backwards 2.181 1.763 −19.2%

Kernel A does not move and kernel B does. That is the opposite of what #96 predicted from IDLE alone, and the probe says exactly why.

The probe — and #96's open question, answered

#95's instrument moved with the kernel. Same shape, PASS_GROUPS 1 against 2, both against the #97 base:

backward q

before after
SCORED 497 487
GRAD 107 121
STAT 11 18
PASS 1 993 1 455 ×0.73
SYNC 38 51
pass warps / visit 2 646 2 132 −19%
FEED 267 268
RECYCLE 79 82
ISSUE 1 498 1 706 ×1.14
IDLE 299 154
issue warp / visit 2 143 2 210

backward kv

before after
SCORED 534 582
GRAD 106 119
STAT 400 299 ×0.75
PASS 5 234 2 985 ×0.57
SYNC 48 56
pass warps / visit 6 321 4 042 −36%
FEED 276 335
RECYCLE 61 62
ISSUE 1 864 2 012
IDLE 3 675 1 350
issue warp / visit 5 875 3 760

1. Why kernel B's pass parallelized worse — the question #96 left open

#96 measured B's pass growing ×1.96 when the four pass warps started running concurrently against kernel A's ×1.42, and said "why it parallelizes worse is not established here". The natural reading was that B's pass had saturated a per-SM port: it does two swizzled store_tiles where A does one, and shared-memory store throughput is shared across the whole SM. That reading predicts the split buys B nothing — the same total work over the same saturated port takes the same time however many warps carry it.

It is wrong, and the split is the experiment that says so. B's PASS falls ×0.57 against an ideal ×0.50. Its pass was never throughput-bound; it was latency-bound, with a long dependent chain per chunk — two TMEM reads, two sub_col broadcasts, an exp2, two swizzled stores — and four warps on four sub-partitions could not cover it. Halving each warp's chain covers it, and 43% of the pass goes away.

The ×1.96 re-reads as its converse: B's per-warp chain was long enough that four of them collided on issue slots at all, where A's shorter one mostly did not. It was a symptom of the same thing the split fixes, not of a wall.

Kernel A's is the pass with a floor: ×0.73 for twice the warps, so roughly 900 of its 1 993 ticks do not scale with warp count at all. Whatever that is, it is A's and not B's, and it is now the smaller question of the two.

2. Why kernel A is flat anyway — remedy 2 arrives

A converted its pass saving into slack and had nowhere to spend it. Before, the pass warps' visit was 2 646 against the issue warp's 2 143 — pass-bound by ~500 ticks, IDLE 299. After, it is 2 132 against 2 210, IDLE 154: the two roles are level and the issue warp is now marginally the longer one. ISSUE itself rose ×1.14 (1 498 → 1 706), which is the price of the ninth warp — nine warps over four sub-partitions puts warps 0, 4 and 8 on sub-partition 0, so the issuer shares its scheduler with two pass warps where it shared with one.

So kernel A is now MMA-issue bound, at MmaShape::M128_N64's own ~30%-of-SM-peak issue rate. That is #94's remedy 2, exactly where #94 said it would surface once remedy 1 took ISSUE off the pass's critical path. Kernel A is not slower and it is not faster; it is now sitting on a different roof, and the next increment on it is a wider MMA rather than more warps.

3. Kernel B's statistic is half-paid by the same change

STAT 400 → 299. B's load_col_vec is indexed by column, so a warpgroup that runs half the chunks makes half the loads. #94's remedy 4 was a separate item; remedy 3 pays a quarter of it as a side effect, and what is left is the fixed part.

Four warpgroups is not the answer, measured

B is still pass-bound after this PR (PASS 2 985 against ISSUE 2 012, IDLE 1 350), so PASS_GROUPS = 4 is the obvious next question. It was built and run rather than argued about:

PASS_GROUPS = 2 PASS_GROUPS = 4
backward q 0.682 0.761 (+11.6%)
backward kv 1.077 1.011 (−6.1%)
both backwards 1.763 1.772
ptxas 128 / 0 and 140 / 0 96 / 32 and 96 / 40 — spilled

Seventeen warps is 544 threads, so ptxas' allowance is 65536 / 544, it allocated 96 and spilled both kernels. B gains another 6% and A loses 12%, and the two cancel. Two warpgroups is the number, and the branch is kept for the record at flash-bwd-pass-split-4wg. (tile_x8 also requires N % 64, so the drain can only ever split two ways however wide the pass split goes — at four groups two of the warpgroups drain and two do not.)

Gates

  • --kernel flash-attn --bin flash — green. Parity is byte-identical to the All four gpu/* ferro-kittens pins to e26c75c, and the one thing in that range that is not additive (#94) #97 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. That is the gate this change deserves and it holds for a reason: the column split changes no summation order. Every chunk is the same arithmetic on the same inputs into the same dS columns; only the warp that runs it changed, and every reduction — the exp2, the dP − D product, the gradient MMAs — is per-element or per-MMA and indifferent to it. The forced-correction shape still corrects on 100.00% of its 32 eligible visits and every other shape on 0.00%. (The four-warpgroup branch above reproduces the same digits a third time, at a third width.)
  • --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.000036 against the base run's 0.000031 and the recorded 0.000028 — the scatter-atomic nondeterminism, unchanged arithmetic.
  • --kernel ops — green.
  • --kernel gemm — 134 / 168 / 146 at All four gpu/* ferro-kittens pins to e26c75c, and the one thing in that range that is not additive (#94) #97, untouched here.
  • ptxas / KernelBudget: forward 128 / 0 unchanged; backward q 192 → 128, backward kv 200 → 140, both zero spill, at .maxntid 288. Pins ratcheted down. This is the first .maxntid rise in this file that gave registers back, and it is liveness rather than budget: ptxas' allowance did fall, from 65536/160 = 409 to 65536/288 = 227, and the allocation fell a hundred below it because a pass warp now runs PASS_COLUMNS of chunks where it ran TILE, and the epilogue drains [32, 64] where it drained [32, 128] — the one place either kernel held 128 fp32 registers at once. Residency is unmoved: BACKWARD_TMEM_COLUMNS = 512 pins 1 CTA/SM either way, and nine warps is nine of the SM's 64 slots.

Same-container A/B, both orders

--baseline-ref 0897acd — the pin-bump commit, so nothing below includes #97's effects. compare_profile always profiles the retained ref first, so the reversed run passes this branch as --baseline-ref instead.

Throttled containers, and stated as such: the full step reads 767–779 ms where a clean standalone profile of either tree reads 334–335 (2.3×, the hazard #96 named). The row deltas are the measurement.

row #97 base this PR Δ
forward.attention.flash 27.6997 27.7545 +0.0548
backward.attention.flash_q 26.8873 27.0833 +0.1960
backward.attention.flash_kv 54.8934 48.4868 −6.4066
backward.attention.flash_dot 5.0635 5.0859 +0.0224
full step 770.7169 766.7864 −3.9305

With the order reversed (this branch retained and profiled first, #97 as the candidate) — at tag ab-ref-flash-bwd-pass-split, which is this branch's tree before the last commit reworded two const assert messages and added the four-warpgroup note; no generated code differs:

row this PR #97 base Δ
forward.attention.flash 27.6950 27.5523 −0.1427
backward.attention.flash_q 27.0457 27.1151 +0.0694
backward.attention.flash_kv 47.9825 55.7752 +7.7927
backward.attention.flash_dot 5.0864 5.1304 +0.0440
full step 768.5220 778.9636 +10.4416

Order does not matter: flash_kv −6.41 one way and −7.79 the other, flash_q +0.20 and +0.07 — flat, as the bench says. The unmarked pair each run also emits reproduces it independently: −6.13 and −6.27 on flash_kv. Named rows sum to −6.13 against a −3.93 step and −7.76 against a −10.44 step; the step total is the noisier of the two at this resolution and the row is what carries the claim.

Clean standalone profiles

./run.sh model profile, §13.9 shape, one clean container each:

row #97 base this PR Δ
forward.attention.flash 14.8829 14.8640 −0.0189
backward.attention.flash_q 11.3122 12.3578 +1.0456
backward.attention.flash_kv 25.3826 18.5822 −6.8004 (−26.8%)
backward.attention.flash_dot 5.6638 5.8331 +0.1693
backward attention 42.359 36.773 −5.586
full step 334.2696 335.2879 +1.0183

The flash_kv row's −26.8% is the bench's −28% and the probe's −27%, a third instrument agreeing. Two things in this table are container drift and not results, and saying so is the point of showing it: flash_q moves +1.05 where the same-container A/B and three bench pairs all put it flat, and the step total moves the wrong way because the non-attention remainder is 277.03 against 283.65 — 2.4% apart between two containers, which is more than the 5.6 ms the attention rows gave back. Two standalone containers are not a ±7 ms instrument for the step; the same-container A/B and the bench are.

Worth one note across the two kinds of container: flash_kv gives back 6.4, 6.8 and 7.8 ms whether the row reads 55 ms throttled or 25 ms clean. What the split removes is not itself clock-throttled, which is why the percentage looks so different in the two settings and the milliseconds do not.

Training smoke

--kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100 — 74 663 tokens/s, 33.15% MFU over 99 measured steps, loss 11.107 → 8.088 on the same curve #96 ran (9.002 / 8.467 / 8.258 at steps 10/20/30). Against #96's 75 553 / 33.55% that is −1.2%, well inside the ~2.9% main-vs-main spread the MoE atomics give this run, and this gate is "it still trains" — the row-level A/B above is what carries the claim.

An instrument fix that came with it

phase_probe.rs still had leader: threadIdx_x() == 0 where #96 moved the shipped kernels' to ISSUE_WARP * 32. The probe therefore had a pass warp doing each item's head — its TMA loads and its first score MMA — and charged the wait to a warp whose column is never published. Per item rather than per visit, which is why it stayed inside the probe's 1% overhead band and why #95's conclusions are unaffected; still not what ships. Both probe kernels are corrected, so FEED and ISSUE above include the item head where #96's published figures did not — the before column in the tables was re-measured with the corrected probe so that both columns are the same instrument.

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 nothing here touches it. MmaShape::M128_N64 stays, and it is now kernel A's binding roof rather than a term in a sum. The forward is untouched: it launches one warpgroup, and 32 * warp_id() and warp_lanes() are the same number there, so re-spelling it would move its schedule for no reason.

…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