Skip to content

Flash-attention backward: 57 ms/step, and the visit is one warp's serial chain of MMA issue then register pass #94

Closed

Description

@feitreim

Tracking issue for the flash-attention backward, the largest remaining kernel-time block in the training step, opened after the attention data-path fusion (#89/#92/#93) and the GEMM campaign (#80/#82–#86).

Phase 1 is done and posted below: the budget comes first, per the #80 campaign's twice-proven lesson. The answer is not what any of the standing hypotheses said, so the remedy list at the bottom is rewritten around it rather than inherited.

Baseline — main @ 9060469, clean standalone containers

./run.sh model profile, §13.9 shape (L=12, B=12, T=2048, D=3072, H=24, HD=128, E=8, K=2):

row ms % step
backward.attention.flash_kv 29.7978 8.37%
backward.attention.flash_q 21.3278 5.99%
backward.attention.flash_dot 5.8842 1.65%
backward attention 57.01 16.02%
forward.attention.flash 14.5538 4.09%
full step 355.8166 100%

These agree with #93's own main-side standalone numbers (29.57 / 21.28) to about 1%, so the fusion did not move them and the baseline is pinned.

./run.sh flash-attn flash, same day, clean container — parity green at every gated shape, corrections 0.00% everywhere, ptxas 128 / 182 / 190, zero spill (the #77 pins, exact):

kernel ms
forward 0.832
backward q 1.094
backward kv 1.566
both backwards 2.659

The two roofs, and why neither is the story

Per causal (query tile, key tile) pair each backward kernel issues its score MMAs and its gradient MMAs; per key-tile visit (a 128-row block against a 64-row tile) that is 3 [128, 64, 128]-equivalent MMAs in kernel A and 4 in kernel B. At [32,1024,24,128] there are 55 296 visits over 148 SMs at 1 CTA/SM:

ns/visit MMA floor of tensor peak DRAM compulsory of 8 TB/s
backward q 2 925 414 ns 14.1% 1.21 GB / 151 µs 13.8%
backward kv 4 154 552 ns 13.3% 1.61 GB / 201 µs 13.0%

Both kernels sit at ~13–14% of both roofs. The forward is the same story (0.832 ms is ~279 TFLOP/s issued). So the time is in neither — it is in between, and nothing before this said where.

The phase budget

80-pingpong-probe's pattern ported to flash on flash-bwd-probe: gpu/flash-attn/src/phase_probe.rs is the two shipped kernels copied with clock64 on every phase, two thin #[kernel] entry points in tcgen05::kernels, and src/bin/probe.rs to run it. The stopwatch costs under 1% — probe vs shipped is −0.7% / +0.8% / −0.7% / +1.0% / −0.6% / +0.9% across the six measurements — so these are the shipped kernels' own numbers, not an instrument's.

Ticks are the CTA's own SM clock, divided by that CTA's key-tile visits. Three shapes:

backward q (query-parallel, dQ)

shape visits/item ISSUE PASS FEED RECYCLE SCORED GRAD STAT SYNC Σ visit
[32,1024,24] 9 2 108 1 641 244 63 91 91 11 52 4 301 4 390
[12,2048,24] 17 2 160 1 608 164 76 91 98 5 52 4 254 —
[32,512,24] 5 2 025 1 695 428 42 91 79 20 52 4 432 —

backward kv (key-parallel, dK/dV)

shape visits/item ISSUE PASS FEED RECYCLE SCORED GRAD STAT SYNC Σ
[32,1024,24] 9 2 628 2 708 249 63 91 91 397 54 6 281
[12,2048,24] 17 2 679 2 553 152 77 91 98 396 52 6 098
[32,512,24] 5 2 545 3 023 563 42 91 79 398 60 6 801

Σ against the shipped kernel's own ns/visit implies an SM clock of 1.47–1.58 GHz at the two real shapes — a plausible loaded B200 clock, which is the check that says the columns are the whole visit and nothing is hiding outside them.

What it says

1. Every wait is nearly zero. The overlap #75 built works completely. SCORED — the warpgroup waiting the score MMAs of the tile it is about to pass over — is 91 ticks, flat, at every shape and in both kernels. GRAD is 79–98, RECYCLE 42–77, SYNC 52–60, FEED 152–563 (and FEED falls as the stream deepens, which is the ring filling). Against a 4 300–6 800 tick visit, the pipeline is not the problem at all: the tensor core is always ahead of the register pass, by a wide margin.

That also prices two standing hypotheses at zero: operand staging (FEED + RECYCLE ≤ 7%, and falling with depth) and any deeper ring. BACKWARD_STAGES = 3 is already more depth than the kernel uses — the GEMM campaign's six-stage lesson does not transfer, because flash's feed was never short.

2. The visit is warp 0's serial chain: issue the MMAs, then run the register pass.

ISSUE is the leader's own MMA issue — not a wait — and it is 2 108 / 2 628 ticks, 42–49% of the visit. It is not overlapped with the register pass, it is added to it, because the leader is one of the four pass warps:

if leader { ...refill, score_mmas(i+1)... }   // warp 0 only, ~2 100 ticks
scored.wait(i); ...register pass...           // all four warps, ~1 640 ticks
sync_threads();                               // warps 1-3 have been here for ~2 100 ticks
if leader { gradient mma }                     // warp 0 only

Warps 1–3 finish the pass and sit at the block barrier while warp 0 is still working. The instrument reports from warp 0, which arrives last — that is why its own SYNC reads 52 rather than 2 100, and it is the confirmation rather than a contradiction: FEED + RECYCLE + ISSUE + SCORED + GRAD + STAT + PASS + SYNC = 4 301 against a 4 390-tick visit. 99% of the visit is one warp's serial chain.

ISSUE scales with the MMA instruction count, which is what says it is the tensor core's own cadence and not descriptor bookkeeping: kernel A issues 24 tcgen05.mma per visit and kernel B 32, a ratio of 1.333, and the measured ISSUE ratio is 1.247. Per instruction it is ~88 ticks against a ~26-tick full-rate figure — the tensor core, while busy, delivers 4.4 / 4.8 TFLOP/s per SM, 29–32% of the SM's peak. That is MmaShape::M128_N64's own rate: a quarter-width instruction costs what a wide one costs to push.

3. The register pass is the other half — 1 641 / 2 708 ticks, 38–43% of the visit — and it is running with four warps on an SM, one per sub-partition, so every dependent latency in it is fully exposed. Nothing else in the budget is over 6%.

4. Kernel B pays 397 ticks a visit for its statistic, against kernel A's 11. Kernel A hoists load_row_vec to the item head because its band indexes the statistic by row; kernel B's transposed band indexes it by column, so load_col_vec runs per SCORE_CHUNK inside the pass. 6.3% of kernel B, ~1.9 ms of the flash_kv step row.

Remedies, in leverage order

  1. Take the MMA issue off the pass warps' critical path — the whole of finding 2. One extra warp (160 threads) that does nothing but issue, while warps 0–3 do nothing but pass, turns ISSUE + PASS into max(ISSUE, PASS). Predicted floor: kernel A 4 301 → ~2 200 ticks (−49%), kernel B 6 281 → ~3 100 (−50%). This is not the warp specialization One flash-attention backward per direction, at one block barrier a key tile (#69) #75 deleted: that was 192 threads with a separate TMA warp, an MMA warp, six barrier sets and a shared LSE ring re-staged because specialization had removed the block sync. This adds one warp and keeps every barrier and every ring exactly as they are.
  2. Widen the MMA. After 1, ISSUE is the floor, and it is the floor because M128_N64 runs the tensor core at 29–32%. A 128-key tile would halve the instruction count for the same work. It needs tensor-memory columns kernel A does not have at two score buffers — but finding 1 says the second score buffer is buying 91 ticks of slack out of 4 300, so single-buffering is now a measured trade rather than the design-deleting one One flash-attention backward per direction, at one block barrier a key tile (#69) #75 called it. (One flash-attention backward per direction, at one block barrier a key tile (#69) #75's "2 CTAs/SM in kernel A" item is the same arithmetic pointed at occupancy instead.)
  3. Split the pass across two warpgroups. 256 threads, each warpgroup taking half the score band's columns; the TMEM sub-partition a warp reads is warp_id % 4, so both warpgroups address the same lanes and only the columns differ. Halves PASS. Worth having after 1 and 2, when PASS is what is left; on its own it leaves ISSUE serialized and buys ~32%.
  4. Hoist kernel B's statistic. 6% of flash_kv, and it is a shape question — the streamed query tile's 64 columns of LSE and dot could be staged once per tile instead of once per chunk.

Explicitly not on the list, priced by the probe at ≤7% combined: deeper operand rings, BACKWARD_STAGES, TMA staging, and the fused-backward recompute argument (#75's "what remains" item 1) — a fused kernel would cut MMA work by 40%, but MMA issue is only 42–49% of the visit and the fusion costs cross-CTA gradient traffic and determinism, so remedies 1–3 dominate it at far lower risk.

Acceptance

Per increment: --kernel flash-attn and --kernel flash-attn --bin flash green (parity including the backward max-abs-error checks and the forced-correction gate), --kernel ops and --kernel model green, KernelBudget pins ratcheted to measured counts, same-container full-step A/B in both orders with the flash rows cross-checked against the step delta, and a training smoke run. Done when the backward attention rows are no longer the largest block in the step, or the residual is attributed to a documented roof.

Probe and logs: flash-bwd-probe. The instrument belongs in main on its own merits whatever happens to the kernels, in the way #85's did.

Activity

  1. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Remedy 1 shipped, and the budget it leaves

    #95 (the instrument) and #96 (remedy 1) are open, stacked in that order. Remedy 1 is one extra warp that issues every TMA and MMA and runs no register pass, and it is parity-byte-identical at every gated shape — no arithmetic changed, only which warp calls mma_*.

    before after
    bench, backward q 1.094 ms 0.681 ms −37.7%
    bench, backward kv 1.566 ms 1.404 ms −10.3%
    bench, both 2.659 ms 2.088 ms −21.5%
    standalone flash_q 21.3278 ms 12.0008 ms −43.7%
    standalone flash_kv 29.7978 ms 25.9788 ms −12.8%
    backward attention 57.010 ms (16.0% of step) 43.798 ms (12.6%) −13.21 ms

    Same-container A/B in both orders agrees: flash_q −7.49 / −7.11, flash_kv −3.59 / −3.23, and the named rows account for the step delta each way. Training smoke 75 553 tok/s / 33.55% MFU.

    The post-change budget, and what it retargets

    The probe moved with the kernel — the two roles are two warps now, so each reports its own wait at the shared barrier (SYNC for the pass warps, a new IDLE for the issue warp). [32,1024,24,128], ticks per tile visit:

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

    Two effects, and they explain the whole asymmetry:

    1. ISSUE got ~35% cheaper (2 108 → 1 354, 2 628 → 1 768). The MMA issue had been contending with the register pass in the same warp; alone it runs faster. So the original 2 108/2 628 slightly overstated the tensor core's own cadence.
    2. PASS got more expensive — ×1.42 in kernel A, ×1.96 in kernel B. Before the split, warp 0 measured a pass it largely ran alone, because warps 1–3 had already finished theirs and were waiting at the barrier. Now all four run it concurrently and contend.

    Kernel A converts because ×1.42 is far less than the serial sum it replaces; kernel B barely converts because ×1.96 nearly eats it.

    Both kernels are now pass-bound, kernel B overwhelmingly: PASS is 90.4% and 89.2% of the pass warps' column, and the issue warp idles 946 / 4 004 ticks a visit at the barrier. Everything else in the budget is ≤ 7%, still.

    The remedy list, rewritten

    1. Take the MMA issue off the pass warps' critical path — done, The backward's MMA issue moves to a fifth warp, off the register pass's critical path (#94) #96.
    2. Split the register pass across more warps (was remedy 3, now the leading term, and kernel B is where it pays). 256 pass threads plus the issue warp, each warpgroup taking half the score band's 64 columns. The measured 1→4-warp scaling (×1.96 for 4× the work, ~51% marginal efficiency) says 4→8 is worth having but sub-linear — call it −15 to −25% on kernel B rather than −50%.
      Open first, and it gates the whole remedy: a warp of a second warpgroup must be able to tcgen05.ld the same TMEM lanes as its opposite number in the first. The ISA maps a warp to a TMEM sub-partition by its rank within its warpgroup, which would make this work, but ferro's TmemTile::tile documents its contract as "all 32 lanes of the warp owning TMEM rows row..row + M" and no kernel in either repo has two warpgroups over one accumulator. That wants a ferro device test before a kernel is written against it.
    3. Why kernel B's pass parallelizes worse than kernel A's — unestablished, and it is worth establishing before 2, because it may be the same thing 2 would run into. B's pass does two swizzled operand stores where A's does one (Pᵀ and dSᵀ against dS) and two sub_col_assign broadcasts against A's two sub_row_assign. The store_tile delta grew ×2.8 under concurrency where the rest grew ×1.5, which points at the shared-memory store path, but that is an inference from two numbers and not a measurement.
    4. Widen the MMA (was remedy 2). MmaShape::M128_N64 still runs the tensor core at ~30% of the SM's peak while busy, but with ISSUE at 1 354/1 768 against a 2 565/5 980-tick visit it is no longer a leading term. The second score buffer remains cheap to give up — SCORED is still ~100 ticks — so the TMEM columns for a 128-key tile are there for the taking whenever this becomes the binding constraint.
    5. Hoist kernel B's statistic — unchanged at 396 ticks, now 6.6% of its pass column.

    Neither roof has moved: at 0.681 / 1.404 ms the two kernels are at ~22% and ~15% of the tensor-core roof and ~22% / ~14% of the DRAM roof, so there is still a large gap and it is still in the register pass.

  2. added 3 commits that reference this issue on Aug 5, 2026
  3. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Phase 3: remedy 3 shipped, and it answers #96's open question rather than confirming its guess

    #97 (the ferro e26c75c pin bump) and #98 (the pass split). Both against this issue's own instrument, --kernel flash-attn probe.

    The remedy list, updated

    # remedy state
    1 MMA issue off the pass warps done (#96), −13.2 ms
    3 split the pass across two warpgroups done (#98), backward kv −27 to −28%, backward q flat
    2 widen the MMA past M128_N64 now kernel A's binding roof, and the leading term for it
    4 hoist kernel B's per-chunk statistic a quarter paid by remedy 3 (STAT 400 → 299); the rest is the fixed part

    What the split did

    ./run.sh flash-attn flash, [32,1024,24,128], two independent container pairs:

    before after
    forward 0.840 / 0.832 0.839 / 0.843
    backward q 0.680 / 0.680 0.682 / 0.683
    backward kv 1.500 / 1.499 1.077 / 1.092
    both backwards 2.181 1.763

    #96's open question, closed

    #96 measured kernel B's pass growing ×1.96 when four warps started running it concurrently against kernel A's ×1.42, and said why was not established. The standing reading was a saturated per-SM port — B does two swizzled store_tiles where A does one, and shared-memory store throughput is SM-wide. That reading predicts the split buys B nothing.

    It is wrong. B's PASS falls ×0.57 under the split against an ideal ×0.50; A's falls only ×0.73. B's pass was latency-bound all along — a long dependent chain per chunk (two TMEM reads, two sub_col broadcasts, an exp2, two swizzled stores) that four warps on four sub-partitions could not cover. The ×1.96 was a symptom of that chain being long enough for four warps to collide on issue slots at all, not of a wall. Kernel A is the one with a floor: roughly 900 of its 1 993 pass ticks do not scale with warp count, and that is now the smaller open question of the two.

    Per-visit budget at [32,1024,24,128], PASS_GROUPS 1 → 2, both on #97's base with the corrected probe:

    q before q after kv before kv after
    SCORED 497 487 534 582
    GRAD 107 121 106 119
    STAT 11 18 400 299
    PASS 1 993 1 455 5 234 2 985
    SYNC 38 51 48 56
    pass warps / visit 2 646 2 132 6 321 4 042
    FEED 267 268 276 335
    RECYCLE 79 82 61 62
    ISSUE 1 498 1 706 1 864 2 012
    IDLE 299 154 3 675 1 350
    issue warp / visit 2 143 2 210 5 875 3 760

    Why kernel A is flat, and where remedy 2 now sits

    A spent its pass saving on slack it cannot use. Before: pass warps 2 646 against the issue warp's 2 143 — pass-bound by ~500 ticks. After: 2 132 against 2 210, IDLE 154. The two roles are level and the issue warp is now marginally the longer one, so max(issue, pass) stopped moving. ISSUE also rose ×1.14, which is the ninth warp's price: 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.

    Kernel A is therefore MMA-issue bound, at MmaShape::M128_N64's ~30%-of-SM-peak rate, exactly where phase 1 said remedy 2 would surface once remedy 1 landed. It is not slower and not faster; it is on a different roof, and more warps cannot move it.

    Kernel B is still pass-bound (PASS 2 985 against ISSUE 2 012, IDLE 1 350), so it has room that A does not.

    Four warpgroups was tried and lost

    Since B still has pass to give, PASS_GROUPS = 4 was the obvious next step. Built and measured rather than argued:

    2 groups 4 groups
    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, 140 / 0 96 / 32, 96 / 40 — spilled

    Seventeen warps is 544 threads, 65536 / 544 is the allowance, ptxas allocated 96 and spilled both kernels. B's further 6% and A's 12% cancel. Two is the number. Branch kept at flash-bwd-pass-split-4wg. Note also that tile_x8 requires N % 64, so the accumulator drain can only ever split two ways however wide the pass split goes.

    Registers went down

    backward q 192 → 128, backward kv 200 → 140, zero spill, at .maxntid 288. The first .maxntid rise in this file that gave registers back: the allowance fell 409 → 227 and the allocation fell a hundred below it, because a pass warp runs half the chunks and the epilogue drains [32, 64] where it drained [32, 128]. (#97's own bump moved them the other way first, 189 → 192 / 198 → 200, bisected to ferro #191 unrolling RegTile::mask.)

    Where the front stands

    Clean standalone profiles of the two trees, §13.9 shape:

    row #97 base #98
    backward.attention.flash_q 11.3122 12.3578
    backward.attention.flash_kv 25.3826 18.5822 (−26.8%)
    backward.attention.flash_dot 5.6638 5.8331
    backward attention 42.359 36.773

    (flash_q moves +1.05 here where the same-container A/B and three bench pairs put it flat — that row drifts ±5% between containers, and the same-container A/B, both orders, is what carries it: flash_kv −6.41 and −7.79 ms, flash_q +0.20 and +0.07.)

    backward attention is 57.01 → 43.80 → ~36.8 ms over phases 1–3, and flash_kv remains the largest single row: what is left in it is a pass that is still the visit's biggest term after halving.

    The next two questions, in order:

    1. Remedy 2 for kernel A — a 128-key tile halves the MMA instruction count, and A is now bound by exactly that. Phase 1 priced the second score buffer it needs at 91 ticks of slack out of 4 300; the current budget puts SCORED at 487, so that trade wants re-pricing before it is taken.
    2. What kernel A's ~900 non-scaling pass ticks are. The instrument would need PASS broken into its TMEM reads, its arithmetic and its swizzled store to say, which is a probe change and not a kernel one.

    Remedy 4 is no longer worth its own increment on the evidence above.

  4. added 2 commits that reference this issue on Aug 5, 2026
  5. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Phase 0 for remedy 2: the issue side is the tensor core, N=64 is why, and kernel A has 3.5% of headroom on that side alone

    Reproduced on main @ d6b99ea with #95's instrument, then extended it. Branch: flash-q-issue-forensics. Two additions:

    1. ISSUE split into what it is issuing (TMA + SMMA + GMMA == ISSUE, a partition) and PASS split into its three kinds of work (TREAD + ARITH + STORE == PASS), plus FILL (the item's first scored.wait) and GAP (measured across pipeline::run's own per-item work rather than derived).
    2. An MMA cadence probe — one warp, one CTA, no operand traffic: chained tcgen05.mma walks into a resident accumulator, with everything but N held fixed.

    The baseline reproduces #98's table to the digit: backward q, [32,1024,24,128], pass warps 2133 against the issue warp 2210, ISSUE 1706, PASS 1455, IDLE 154, shipped 0.684 ms.

    1. The cadence, and it settles remedy 2's premise

    ./run.sh flash-attn probe, 256 rounds a variant. ticks/unit is per M128_N64_K16-equivalent of work, so the last column is comparable across widths:

    variant instrs issue/instr retire/instr ticks/unit drained chain
    mma_abt M128_N64 K=128 2048 55.7 55.7 55.7 756
    mma_abt M128_N128 K=128 2048 63.9 64.1 32.0 885
    mma_abt M128_N256 K=128 2048 127.8 128.1 32.0 1252
    mma_ab M128_N64×2 K=64 — dQ as shipped 2048 48.0 48.2 48.2 659
    mma_ab M128_N128 K=64 — dQ in one band 1024 63.9 64.1 32.1 486

    Two things fall out, and the first is the one phase 0 was asked for:

    issue/instr equals retire/instr in every row. The issuing thread is not paying a front-end toll and is not running ahead — it is back-pressured against a full MMA pipe, and its issue rate is the tensor core's retirement rate. A second issue warp is therefore priced at exactly zero, and so is anything that makes descriptor bookkeeping cheaper. ISSUE is the tensor core, measured, not inferred from a scaling ratio.

    N = 64 runs the tensor core at 57% of what N = 128 does — 55.7 ticks/unit against 32.0 — and N = 256 adds nothing over N = 128. That is #94's "M128_N64's own rate" pinned to a number: the waste is 43%, the fix is N = 128, and N = 256 is not worth the tensor memory.

    2. Where ISSUE goes

    backward q, [32,1024,24,128] (instrumented run; ISSUE reads 1504 here against the clean 1706 — see the caveat below):

    ticks instrs ticks/instr
    TMA — two tma_load + expect_tx 59 2 30
    SMMA — S = Q·Kᵀ and dP = dY·Vᵀ 1087 16 68
    GMMA — dQ += dS·K 357 8 45

    TMA issue is 3.9% of ISSUE and barrier arms do not show at all. 72% is the two score MMAs and 24% the gradient MMA, and the per-instruction figures agree with the standalone cadence (68 vs 55.7, 45 vs 48.0). There is nothing in the issue column but MMAs.

    Caveat, and it is a finding of its own: adding clocks to the pass raised PASS 1455 → 1853 and lowered ISSUE 1706 → 1504. Nine warps over four sub-partitions puts warps 0, 4 and 8 on sub-partition 0, so the issuer and two pass warps share a scheduler — slowing the pass speeds the issue. The two roles are not independent, which is why the split above is quoted as a partition of its own run and not spliced into the clean one.

    3. Where PASS goes — and it is not the TMEM read

    ticks share of PASS
    TREAD — the two TmemTile::tile reads a chunk 381 21%
    ARITH — mask, −lse2, exp2, −D, multiply, scale 1135 61%
    STORE — convert and swizzled store 336 18%

    I expected the read to be the floor — a [32, 16] chunk is four tcgen05.ld with a tcgen05.wait::ld after every one of them (fragment waits per issue, twice per fragment, twice per chunk). It is not: 381 ticks. The pass is arithmetic, and a good part of that arithmetic is a throughput floor rather than a latency one — 8192 exp2 per visit per CTA against the SFU's 16 lanes/SM/cycle is ~512 ticks a visit that no amount of warps or reordering removes.

    4. What is not in the visit at all

    per item ticks share of a 30 108-tick span
    FILL — the first scored.wait, i.e. the item's TMA fill 3534 11.7%
    EPI — accumulator wait, .x8 drain, store_rows 3224 10.7%
    GAP — pipeline::run's two boundaries and the barrier re-arm 1624 5.4%

    28% of kernel A is per-item and not per-visit. #95's residual had said something was outside the columns; GAP now says how much of it is the harness (1624) and FILL says the rest is the pipeline fill, which the per-visit SCORED mean had been smearing across every visit (fit across the three shapes: SCORED is ~78 ticks in steady state plus ~3700 of item head).

    5. The arithmetic that decides the remedy

    Kernel A's ceiling from issue-side work alone is −3.5% of the visit. The visit is max(issue 2210, pass 2133); deleting all of ISSUE leaves 2133. That is the honest number and it is the reason this comment is not a design for one.

    What the cadence does license, at no structural cost:

    The gradient MMA can be one N=128 band today. mma_ab splits a [K, HD] B operand into two N=64 bands because it addresses each stacked subtile with its own descriptor; mma_atb already reaches MN columns 64..128 through the descriptor's leading offset and covers the whole band in one instruction. Same walk, same K order, same products — byte-identical, measured at 48.2 → 32.1 ticks/unit, 1.50×. It applies to all four mma_ab call sites in the file: the forward's O = P·V, kernel A's dQ += dS·K, and kernel B's dV += Pᵀ·dY / dK += dSᵀ·Q. For kernel A that is ISSUE 1706 → ~1578 and the issue warp 2210 → ~2082, so the visit becomes pass-bound at 2133 and the kernel moves ~2%. It also happens to fix ferro-kittens#175's wart — shape stops being the one argument in the module that is not the operands' logical product.

    The score MMAs' 43% waste needs a 128-key tile, and shared memory forbids it. Priced rather than argued:

    • Tensor memory fits: S double-buffered at [128, 128] (256 columns) + dP single (128) + the dQ accumulator (128) is exactly 512, and the dP dependency can be paid by issuing dP(i+1) immediately after the barrier, ahead of dQ(i).
    • Shared memory does not. A 128-key visit needs Q 32K + dY 32K + K 2×32K + V 2×32K + dS 2×32K = 256 KiB against MAX_DYNAMIC_SMEM's 227. The binding term is the second dS slot: at one slot it fits (224 KiB) and pass(i) then waits out dQ(i−1) and dP(i) post-barrier — ~1000 ticks of exposed tensor core a visit, which eats the whole win (projected −3%). Splitting dS into 64-key half-slots does not help: double buffering needs four of them, which is the same 64 KiB.
    • Projected if it fit: ISSUE −51% per 128 keys, per-visit fixed costs halved, −25% on the kernel. It is the largest thing on the board and it is 29 KiB out of reach.

    Also worth putting on the record, because #94's roof table understates it: the per-item traffic is Q 32K + dY 32K + dQ 64K + 32K per key tile, which over the §13.9-shaped [32,1024,24] run is ~2.5 GB, ~315 µs at 8 TB/s against a 682 µs kernel — 46% of the DRAM roof, not 13.8%. The compulsory figure ignored the causal K/V re-read, and FILL at 3534 ticks an item is that re-read showing up as latency.

    What I am building

    Not a second issue warp — the cadence prices it at zero. The increment is:

    1. ferro-kittens: mma_ab through mn_walk, one instruction per K chunk over the whole [M, N] band; and TmemTile::fragment issuing its two loads before waiting once instead of waiting after each (the batching tile_x8_batched already establishes, applied to the .x1 path).
    2. oxide-train: the four mma_ab call sites at M128_N128, and kernel A's pass reading its dP chunk before the score chunk's arithmetic rather than after, so the two TMEM reads' latencies overlap.
    3. The instrument itself, which belongs in main on its own merits the way A stopwatch on every phase of a flash backward tile visit, and what it finds (#94) #95's did, plus an EPI split so the deferred-drain question (10.7% of kernel A sitting in an epilogue that could overlap the next item's fill) has a number before anyone designs for it.

    Every part of that is byte-identical to d6b99ea, and I will say so with the gate rather than the argument.

  6. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Phase 4: remedy 2, as much of it as the shared-memory budget allows — #100, and ferro-kittens#195 under it

    Phase 0 is above. What it licensed, measured:

    ./run.sh flash-attn flash, [32,1024,24,128] d6b99ea #100 Δ
    forward 0.843 0.789 −6.4%
    backward q 0.682 0.617 −9.5%
    backward kv 1.085 1.032 −4.9%
    both backwards 1.768 1.662 −6.0%

    Clean standalone profiles, §13.9 shape: flash_q 12.8085 → 11.4146 (−10.9%), forward.attention.flash 15.2249 → 14.5198 (−4.6%), flash_kv 19.0367 → 18.7030. backward attention is 57.01 → 43.80 → 36.77 → 36.09, and the forward joins the ledger for the first time since #68.

    Byte-identical at every gated shape; --kernel ops / --kernel model green; ptxas 128 / 128 / 138 (kv ratcheted from 140), zero spill.

    The two things, and why neither is a second issue warp

    1. The gradient and output MMAs are one N = 128 band. The cadence probe settled the premise: issue/instr == retire/instr in every variant, so ISSUE is the tensor core's retirement rate and a second issuer is worth exactly zero; and M128_N64 costs 55.7 ticks per unit of work against M128_N128's 32.0. mma_ab was banding its MN-major B 64 wide when mma_atb had been covering 128 in one instruction all along (ferro-kittens#194/#195), so all four call sites — the forward's O = P·V, dQ += dS·K, dV += Pᵀ·dY, dK += dSᵀ·Q — halve their instruction count for the same sum. Kernel A's GMMA column: 357 → 184.

    2. The dQ drain moved to the top of the next item. Phase 0 found 28% of kernel A outside the visit — FILL 3534 + EPI 3470 + GAP 1624 of a 30 108-tick item — and the two big ones are adjacent: the pass warps end an item storing 64 KiB and open the next one idling at scored.wait(0) waiting out its TMA fill. Staging the row and draining it one item later puts the store inside the wait. EPI 3470 → 495 (the accumulated.wait that cannot cross the barrier re-arm) and FILL 3534 → 1132.

    Also in: TmemTile::fragment issues both its tcgen05.ld before waiting once instead of waiting after each — TREAD 381 → 303, and it is why flash_kv moves at all.

    What I said I would do and did not

    The phase-0 comment listed "kernel A's pass reading its dP chunk before the score chunk's arithmetic". I dropped it: with fragment batching its own pair, scores.tile() and gradients.tile() are each issue-issue-wait twice, and reordering the second call above the first's arithmetic leaves four serialized waits either way. The reordering that would pay needs TmemTile::tile to batch across its fragments — 4 issues behind 1 wait for a [32, 16] chunk — which is a real ferro change with a liveness argument, and TREAD at 303 of a 2687-tick pass-warp column does not justify it yet.

    Where kernel A is now

    Per-visit, [32,1024,24,128]: ISSUE 1384 = TMA 61 + SMMA 1142 + GMMA 180, and PASS 1858 = TREAD 311 + ARITH 1247 + STORE 299. Both of the big terms are roofs rather than slack:

    1. SMMA is 1142 ticks of M128_N64 at 43% off the tensor core's rate. Widening it means a 128-key visit, which needs 256 KiB of shared memory against MAX_DYNAMIC_SMEM's 227. Priced in phase 0: tensor memory does fit (S double-buffered at [128,128] + dP single + the accumulator is exactly 512, with the dP dependency paid by issuing dP(i+1) ahead of dQ(i) after the barrier), and the binding term is the second dS slot — at one slot it fits and the post-barrier tensor core goes fully exposed, which eats the whole win. 29 KiB out of reach, and that is remedy 2's remainder.
    2. ARITH is 1247 ticks, of which ~512 is exp2 throughput — 8192 of them a visit against the SFU's 16 lanes/SM/cycle. No amount of warps or reordering moves that.

    And one roof the issue's own table understates: per item kernel A reads Q + dY + 32 KiB a key tile and writes 64 KiB, which over the §13.9-shaped run is ~2.5 GB, ~315 µs at 8 TB/s against a 682 µs kernel — 46% of the DRAM roof, not 13.8%. The compulsory figure ignored the causal K/V re-read, and FILL is that re-read showing up as latency. A kernel at 46% of DRAM has about 2× left in it, not 7×.

    Kernel B still has the room A does not: PASS 2933 against ISSUE 1564, IDLE 1642, and an EPI of 6347 ticks an item — two accumulators drained where A drains one — which is #100's change again on a kernel it deliberately did not touch beyond what was structurally shared. That is the obvious next increment, and it is not remedy 2.

  7. added 2 commits that reference this issue on Aug 5, 2026
  8. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Phase 0 for kernel B: the epilogue is the global store, not the tensor-memory read — and it is 5 004 of 6 482 ticks an item

    Reproduced on main @ f15b471 with the in-tree instrument, then extended it three ways. Branch: flash-kv-forensics.

    1. Kernel B gets kernel A's two splits. TMA + SMMA + GMMA == ISSUE and the per-item FILL / GAP / EPI, which until now the probe only filled for kernel A — kernel B's residual had been carrying all of it undifferentiated.
    2. EPI is split into EWAIT + ETREAD + ESTORE, in both kernels: the wait for the last gradient MMA, the accumulator's tcgen05.ld, and the store_rows. That is the decomposition the deferred-drain question needs before anyone designs for it, and it is the one The HD-wide accumulations are one N=128 band, and the dQ drain is the next item's opening move (#94) #100 left undone.
    3. Kernel B's pass split (TREAD/ARITH/STORE) is behind a const that is off, and that is a finding of its own — see the caveat at the bottom.

    Clean instrument, ./run.sh flash-attn probe, [32,1024,24,128]. Probe vs shipped is +0.9% for kernel B and +4.9% for kernel A, so kernel B's columns below are the shipped kernel's own.

    The budget

    kernel A (dQ) kernel B (dK/dV)
    pass warps / visit 2 702 3 964
    SCORED 235 588
    GRAD 126 140
    STAT 468 290
    PASS 1 832 2 890
    SYNC 41 56
    issue warp / visit 2 269 3 595
    FEED / RECYCLE 457 / 78 330 / 54
    ISSUE 1 396 3 188
    — TMA 61 61
    — SMMA 1 153 1 013
    — GMMA 182 2 113
    IDLE 337 23
    per item
    FILL 1 306 4 511
    GAP 1 279 1 609
    EPI 493 6 482
    DRAIN 3 955 —
    — EWAIT 493 829
    — ETREAD 166 649
    — ESTORE 3 788 5 004
    span / item 28 276 46 638

    1. 27% of kernel B is not in the visit at all, and 11% of it is one store

    FILL 4 511 + GAP 1 609 + EPI 6 482 is 12 602 ticks of a 46 638-tick item — 27%, against kernel A's 6 733 of 28 276 (24%, and A already banked its share in #100).

    The EPI split is the new fact and it is flat across all three shapes — 6 482 / 6 504 / 6 492, with EWAIT 829/827/828, ETREAD 649/652/649 and ESTORE 5 004/5 024/5 014. A per-item fixed cost that does not move with stream length, which is what it should be: the item writes 128 KiB of dK and dV whether it streamed 2 query tiles or 16.

    ESTORE is 77% of it. I expected the two tcgen05.ld bands to be the term worth naming — two accumulators drained where kernel A drains one — and they are 649 ticks. The tensor-memory read is 10% of the epilogue. It is the fp32 store_rows of 128 KiB, and kernel A's own ESTORE at 3 788 for 64 KiB says the per-byte rate is if anything better here; there are simply twice as many bytes.

    So the "two accumulators make this different" worry prices out at 649 ticks. What is actually different about kernel B's epilogue is that it is twice as big and sits in front of a fill window that is 3.5× kernel A's (4 511 against 1 306).

    2. The issue warp's slack moved columns, and the sum is the invariant

    ISSUE 3 188 / IDLE 23 does not look like #100's ISSUE 1 564 / IDLE 1 642. Their sums are 3 211 and 3 206 — five ticks apart, and every pass-side column reproduces (PASS 2 890 against 2 933, SCORED 588/582, STAT 290/299, SYNC 56/56). What moved is where the issue warp's wait is charged: with the TMA/SMMA/GMMA accumulation added, the issue warp arrives at the block barrier last and back-pressures against the MMA pipe at mma_ab instead of idling at the barrier.

    That makes GMMA 2113 a slack reading rather than a cost, and the caveat is #100's own: the issuer and two pass warps share a sub-partition, so the two roles are not independent and neither split can be spliced into the other's run. The invariants that survive both readings:

    • the issue warp's column is 3 595 against the pass warps' 3 964 — kernel B is pass-bound, by ~370 ticks a visit;
    • its irreducible issue work is TMA 61 + SMMA 1013 + GMMA ≈ 2 × 182, about 1 440 ticks, since kernel A pays 182 for the identical mma_ab walk and kernel B issues two of them;
    • so the pass has roughly 1 000 ticks a visit of room before the issue side becomes the binding term, and not the 2 100 the IDLE reading suggested.

    3. What the pass split says, quoted as its own run

    Turned on, kernel B's seven stops a chunk cost +8.9% against its shipped self (kernel A's three cost +3.4%) and move 1 600 ticks a visit out of IDLE into GMMA — an instrument that is measuring itself. It is left in the file behind phase_probe::KV_PASS_SPLIT, off, with the one number it was built for on the record:

    TREAD ARITH STORE
    kernel A 254 1 252 318
    kernel B 241 2 660 449

    The ~1 000 ticks kernel B's pass carries over kernel A's are the arithmetic, not the second store_tile. The extra swizzled store — Pᵀ as well as dSᵀ — is worth 131 ticks. The two passes issue the same number of register operations on the same shapes (mask, subtract, exp2, subtract, multiply, scale, over a [32, 16] chunk); what differs is that kernel B's two subtracts are sub_col_assign where A's are sub_row_assign, and that its store of Pᵀ sits between the two halves of the chain. That is the open question this comment does not answer, and at ~1 000 ticks of a 3 964-tick visit it is the largest thing left in kernel B after the item-level work.

    What I am building

    The deferred dK/dV drain — #100's move on the kernel it deliberately did not touch. The arithmetic is on the table above and it is not the transfer-by-analogy the issue warned against: ETREAD 649 + ESTORE 5 004 = 5 653 ticks of drain against a 4 511-tick fill window the pass warps already spend idling at scored.wait(0). EWAIT 829 stays behind, because pipeline::run re-arms the barrier set at the item boundary and the last gradient MMA has to be waited out before the item ends — the same term that reads 493 in kernel A.

    The hazard is kernel A's and resolves the same way: the next item's first gradient MMAs overwrite both accumulators (accumulate is step != 0) and are issued after the block barrier at the bottom of the pass's first step, by which time tile_x8 has retired the drain's tcgen05.ld. The last item's debt is settled by a flush after the persistent loop.

    Projected: the item's head becomes max(drain 5 653, fill 4 511) where it was fill 4 511 plus 5 653 at the tail, so −4 511 ticks of a 46 638-tick item, about −9.7% — worth ~1.8 ms of the flash_kv profile row. Measured numbers to follow; if it does not land there, this comment is what it is measured against.

  9. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Phase 4 on kernel B: the deferred dK/dV drain — #102, and what is left after it

    Phase 0 is above. It said the thing #100 assumed would transfer does transfer, but not for the reason the analogy suggested: kernel B's epilogue is not expensive because it drains two accumulators (ETREAD is 649 of 6 482 ticks) — it is expensive because it is 128 KiB of fp32 store_rows (ESTORE 5 004) sitting in front of a fill window 3.5× kernel A's.

    measured
    EPI 6 482 → 838, FILL 4 511 → 350, DRAIN 0 → 6 607 the drain went into the window and the window is gone
    span/item −5.5% / −3.7% / −7.8% over the three shapes the clock-free instrument
    backward.attention.flash_kv 18.1238 → 17.5028 (−3.4%) clean standalone pair, §13.9
    same-container A/B both orders flash_kv −0.3985 and −0.6128, throttled 2.3×
    parity byte-identical, forced correction 100.00%
    ptxas 128 / 128 / 142, ratcheted, zero spill

    backward attention on that pair is 35.155 → 34.756. (My own main-side standalone reads flash_kv 18.1238 where #100's read 18.7030 — same tree, different container, which is the ±5% drift this row has had all along. The pair is what carries the delta.)

    The ledger

    backward attention is 57.01 → 43.80 → 36.77 → 36.09 → ~34.8 ms over phases 1–4, from 16.0% of the step to about 10.6%.

    # remedy state
    1 MMA issue off the pass warps done (#96), −13.2 ms
    3 split the pass across two warpgroups done (#98), kernel B −27%
    2 widen the MMA past M128_N64 done as far as it goes (#100) — the N=128 accumulations landed; the score MMAs' 43% is 29 KiB of shared memory out of reach
    5 kernel A's deferred dQ drain done (#100)
    5′ kernel B's deferred dK/dV drain done (#102)
    4 hoist kernel B's per-chunk statistic a quarter paid by remedy 3; STAT is 290 of a 3 964-tick visit and no longer a leading term

    The residuals, and I think they are the whole of what is left

    Kernel A's three are already on this issue: the score MMA cannot widen without a 128-key tile and that needs 256 KiB of shared against MAX_DYNAMIC_SMEM's 227; ~512 of its 1 252 ARITH ticks are exp2 against the SFU's 16 lanes/SM/cycle; and its real DRAM traffic is 46% of the 8 TB/s roof, not the 13.8% the opening table said.

    Kernel B's, after #102:

    1. Its pass carries ~1 000 ticks a visit over kernel A's, and it is arithmetic. TREAD 241 / ARITH 2 660 / STORE 449 against kernel A's 254 / 1 252 / 318 — the second swizzled store of Pᵀ is worth 131 ticks and the TMEM read is flat, on identical operation counts. The only candidate I can name is that ferro's col_map_assign reads its operand SLOTS × VALUES times where row_map_assign hoists it to SLOTS, which is exactly the sub_col_assign / sub_row_assign difference between the two kernels. ferro-kittens#196, filed with the measurement and a six-line fix and explicitly unverified — it is a symptom with a hypothesis, not an attribution, and it wants a cadence pair before it wants a PR. At ~25% of kernel B's pass column it is the largest single thing left in either kernel.
    2. ESTORE is 5 928 ticks an item and is now hidden rather than paid. Hiding it is all there is to do with it: a [32, 64] BaseLdtm band's store_rows puts 8 rows × 32 bytes behind each store instruction, which is the fragment layout's own coalescing and not a call-site choice.
    3. The issue warp has ~2 100 ticks a visit of slack wherever the instrument charges it, so nothing on the issue side is worth building — its irreducible work is TMA 61 + SMMA 1 013 + 2 × GMMA 182 ≈ 1 440 against a 3 595-tick column.

    Close or keep

    I'd close this once #102 merges, and open a successor for residual 1 alone. This issue's own title — "57 ms/step, and the visit is one warp's serial chain of MMA issue then register pass" — is answered: the serial chain was split (#96), the pass was split (#98), the accumulations were widened and kernel A's item-level work banked (#100), and kernel B's is banked here. Every term still standing is either a measured roof (shared memory, the SFU, DRAM at 46%) or a library question in another repo. Five phases of tracking issue is a poor container for one cross-repo hypothesis; a fresh issue that states it plainly is a better one.

  10. added a commit that references this issue on Aug 5, 2026
  11. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Closing with #102 merged (main 3e11448). Backward attention 57.01 → 43.80 → 36.77 → 36.09 → ~34.8 ms across the five phases (#96, #98, #100, #102) — 16.0% → ~10.6% of the step. Every remaining term is a measured roof or a library question, parked with numbers in #103 (plus ferro-kittens#196 for kernel B's ARITH gap).

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions