Repository navigation
Description
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
- 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 + PASSintomax(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. - Widen the MMA. After 1,
ISSUEis the floor, and it is the floor becauseM128_N64runs 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.) - 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. HalvesPASS. Worth having after 1 and 2, whenPASSis what is left; on its own it leavesISSUEserialized and buys ~32%. - 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
feitreim commentedon Aug 5, 2026
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_*.flash_qflash_kvSame-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 (
SYNCfor the pass warps, a newIDLEfor the issue warp). [32,1024,24,128], ticks per tile visit:Two effects, and they explain the whole asymmetry:
ISSUEgot ~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.PASSgot 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:
PASSis 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
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.Open first, and it gates the whole remedy: a warp of a second warpgroup must be able to
tcgen05.ldthe 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'sTmemTile::tiledocuments its contract as "all 32 lanes of the warp owning TMEM rowsrow..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.PᵀanddSᵀagainstdS) and twosub_col_assignbroadcasts against A's twosub_row_assign. Thestore_tiledelta 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.MmaShape::M128_N64still runs the tensor core at ~30% of the SM's peak while busy, but withISSUEat 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 —SCOREDis still ~100 ticks — so the TMEM columns for a 128-key tile are there for the taking whenever this becomes the binding constraint.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.
Merge pull request #95 from feitreim/flash-bwd-probe
Merge pull request #96 from feitreim/flash-bwd-issue-warp
The backward's register pass splits across two warpgroups over the sa…
feitreim commentedon Aug 5, 2026
Phase 3: remedy 3 shipped, and it answers #96's open question rather than confirming its guess
#97 (the ferro
e26c75cpin bump) and #98 (the pass split). Both against this issue's own instrument,--kernel flash-attn probe.The remedy list, updated
M128_N64STAT400 → 299); the rest is the fixed partWhat the split did
./run.sh flash-attn flash, [32,1024,24,128], two independent container pairs:#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
PASSfalls ×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, twosub_colbroadcasts, anexp2, 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_GROUPS1 → 2, both on #97's base with the corrected probe: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,
IDLE154. The two roles are level and the issue warp is now marginally the longer one, somax(issue, pass)stopped moving.ISSUEalso 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 (
PASS2 985 againstISSUE2 012,IDLE1 350), so it has room that A does not.Four warpgroups was tried and lost
Since B still has pass to give,
PASS_GROUPS = 4was the obvious next step. Built and measured rather than argued:Seventeen warps is 544 threads,
65536 / 544is the allowance, ptxas allocated 96 and spilled both kernels. B's further 6% and A's 12% cancel. Two is the number. Branch kept atflash-bwd-pass-split-4wg. Note also thattile_x8requiresN % 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.maxntidrise 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 unrollingRegTile::mask.)Where the front stands
Clean standalone profiles of the two trees, §13.9 shape:
backward.attention.flash_qbackward.attention.flash_kvbackward.attention.flash_dot(
flash_qmoves +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 attentionis 57.01 → 43.80 → ~36.8 ms over phases 1–3, andflash_kvremains 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:
SCOREDat 487, so that trade wants re-pricing before it is taken.PASSbroken 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.
Merge pull request #97 from feitreim/flash-bwd-pin-bump
Merge pull request #98 from feitreim/flash-bwd-pass-split
feitreim commentedon Aug 5, 2026
Phase 0 for remedy 2: the issue side is the tensor core,
N=64is why, and kernel A has 3.5% of headroom on that side aloneReproduced on
main @ d6b99eawith #95's instrument, then extended it. Branch:flash-q-issue-forensics. Two additions:ISSUEsplit into what it is issuing (TMA + SMMA + GMMA == ISSUE, a partition) andPASSsplit into its three kinds of work (TREAD + ARITH + STORE == PASS), plusFILL(the item's firstscored.wait) andGAP(measured acrosspipeline::run's own per-item work rather than derived).tcgen05.mmawalks into a resident accumulator, with everything butNheld fixed.The baseline reproduces #98's table to the digit: backward q, [32,1024,24,128], pass warps 2133 against the issue warp 2210,
ISSUE1706,PASS1455,IDLE154, shipped 0.684 ms.1. The cadence, and it settles remedy 2's premise
./run.sh flash-attn probe, 256 rounds a variant.ticks/unitis perM128_N64_K16-equivalent of work, so the last column is comparable across widths:mma_abtM128_N64K=128mma_abtM128_N128K=128mma_abtM128_N256K=128mma_abM128_N64×2 K=64 — dQ as shippedmma_abM128_N128K=64 — dQ in one bandTwo things fall out, and the first is the one phase 0 was asked for:
issue/instrequalsretire/instrin 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.ISSUEis the tensor core, measured, not inferred from a scaling ratio.N = 64runs the tensor core at 57% of whatN = 128does — 55.7 ticks/unit against 32.0 — andN = 256adds nothing overN = 128. That is #94's "M128_N64's own rate" pinned to a number: the waste is 43%, the fix isN = 128, andN = 256is not worth the tensor memory.2. Where
ISSUEgoesbackward q, [32,1024,24,128] (instrumented run;
ISSUEreads 1504 here against the clean 1706 — see the caveat below):tma_load+expect_txS = Q·KᵀanddP = dY·VᵀdQ += dS·KTMA issue is 3.9% of
ISSUEand 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
PASS1455 → 1853 and loweredISSUE1706 → 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
PASSgoes — and it is not the TMEM readPASSTmemTile::tilereads a chunk−lse2,exp2,−D, multiply, scaleI expected the read to be the floor — a
[32, 16]chunk is fourtcgen05.ldwith atcgen05.wait::ldafter every one of them (fragmentwaits 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 — 8192exp2per 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
scored.wait, i.e. the item's TMA fill.x8drain,store_rowspipeline::run's two boundaries and the barrier re-arm28% of kernel A is per-item and not per-visit. #95's
residualhad said something was outside the columns;GAPnow says how much of it is the harness (1624) andFILLsays the rest is the pipeline fill, which the per-visitSCOREDmean had been smearing across every visit (fit across the three shapes:SCOREDis ~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 ofISSUEleaves 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=128band today.mma_absplits a[K, HD]B operand into twoN=64bands because it addresses each stacked subtile with its own descriptor;mma_atbalready 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 fourmma_abcall sites in the file: the forward'sO = P·V, kernel A'sdQ += dS·K, and kernel B'sdV += Pᵀ·dY/dK += dSᵀ·Q. For kernel A that isISSUE1706 → ~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 —shapestops 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:
Sdouble-buffered at[128, 128](256 columns) +dPsingle (128) + the dQ accumulator (128) is exactly 512, and the dP dependency can be paid by issuingdP(i+1)immediately after the barrier, ahead ofdQ(i).MAX_DYNAMIC_SMEM's 227. The binding term is the second dS slot: at one slot it fits (224 KiB) andpass(i)then waits outdQ(i−1)anddP(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.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
FILLat 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:
mma_abthroughmn_walk, one instruction per K chunk over the whole[M, N]band; andTmemTile::fragmentissuing its two loads before waiting once instead of waiting after each (the batchingtile_x8_batchedalready establishes, applied to the.x1path).mma_abcall sites atM128_N128, and kernel A's pass reading itsdPchunk before the score chunk's arithmetic rather than after, so the two TMEM reads' latencies overlap.mainon 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 anEPIsplit 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.Ratchet backward kv to its measured 138 registers (#94)
feitreim commentedon Aug 5, 2026
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]d6b99eaClean standalone profiles, §13.9 shape:
flash_q12.8085 → 11.4146 (−10.9%),forward.attention.flash15.2249 → 14.5198 (−4.6%),flash_kv19.0367 → 18.7030.backward attentionis 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 modelgreen; 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 = 128band. The cadence probe settled the premise:issue/instr == retire/instrin every variant, soISSUEis the tensor core's retirement rate and a second issuer is worth exactly zero; andM128_N64costs 55.7 ticks per unit of work againstM128_N128's 32.0.mma_abwas banding its MN-majorB64 wide whenmma_atbhad been covering 128 in one instruction all along (ferro-kittens#194/#195), so all four call sites — the forward'sO = P·V,dQ += dS·K,dV += Pᵀ·dY,dK += dSᵀ·Q— halve their instruction count for the same sum. Kernel A'sGMMAcolumn: 357 → 184.2. The dQ drain moved to the top of the next item. Phase 0 found 28% of kernel A outside the visit —
FILL3534 +EPI3470 +GAP1624 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 atscored.wait(0)waiting out its TMA fill. Staging the row and draining it one item later puts the store inside the wait.EPI3470 → 495 (theaccumulated.waitthat cannot cross the barrier re-arm) andFILL3534 → 1132.Also in:
TmemTile::fragmentissues both itstcgen05.ldbefore waiting once instead of waiting after each —TREAD381 → 303, and it is whyflash_kvmoves at all.What I said I would do and did not
The phase-0 comment listed "kernel A's pass reading its
dPchunk before the score chunk's arithmetic". I dropped it: withfragmentbatching its own pair,scores.tile()andgradients.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 needsTmemTile::tileto batch across its fragments — 4 issues behind 1 wait for a[32, 16]chunk — which is a real ferro change with a liveness argument, andTREADat 303 of a 2687-tick pass-warp column does not justify it yet.Where kernel A is now
Per-visit, [32,1024,24,128]:
ISSUE1384 =TMA61 +SMMA1142 +GMMA180, andPASS1858 =TREAD311 +ARITH1247 +STORE299. Both of the big terms are roofs rather than slack:SMMAis 1142 ticks ofM128_N64at 43% off the tensor core's rate. Widening it means a 128-key visit, which needs 256 KiB of shared memory againstMAX_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 issuingdP(i+1)ahead ofdQ(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.ARITHis 1247 ticks, of which ~512 isexp2throughput — 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
FILLis 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:
PASS2933 againstISSUE1564,IDLE1642, and anEPIof 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.Pin ferro-kittens at the merged f64a27b (#94)
Merge pull request #100 from feitreim/flash-q-issue-forensics
feitreim commentedon Aug 5, 2026
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 @ f15b471with the in-tree instrument, then extended it three ways. Branch:flash-kv-forensics.TMA + SMMA + GMMA == ISSUEand the per-itemFILL/GAP/EPI, which until now the probe only filled for kernel A — kernel B'sresidualhad been carrying all of it undifferentiated.EPIis split intoEWAIT + ETREAD + ESTORE, in both kernels: the wait for the last gradient MMA, the accumulator'stcgen05.ld, and thestore_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.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
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 482is 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
EPIsplit is the new fact and it is flat across all three shapes — 6 482 / 6 504 / 6 492, withEWAIT829/827/828,ETREAD649/652/649 andESTORE5 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.ESTOREis 77% of it. I expected the twotcgen05.ldbands 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 fp32store_rowsof 128 KiB, and kernel A's ownESTOREat 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 23does not look like #100'sISSUE 1 564 / IDLE 1 642. Their sums are 3 211 and 3 206 — five ticks apart, and every pass-side column reproduces (PASS2 890 against 2 933,SCORED588/582,STAT290/299,SYNC56/56). What moved is where the issue warp's wait is charged: with theTMA/SMMA/GMMAaccumulation added, the issue warp arrives at the block barrier last and back-pressures against the MMA pipe atmma_abinstead of idling at the barrier.That makes
GMMA 2113a 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:TMA 61 + SMMA 1013 + GMMA ≈ 2 × 182, about 1 440 ticks, since kernel A pays 182 for the identicalmma_abwalk and kernel B issues two of them;IDLEreading 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
IDLEintoGMMA— an instrument that is measuring itself. It is left in the file behindphase_probe::KV_PASS_SPLIT, off, with the one number it was built for on the record: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 asdSᵀ— 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 aresub_col_assignwhere A's aresub_row_assign, and that its store ofPᵀ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 653ticks of drain against a 4 511-tick fill window the pass warps already spend idling atscored.wait(0).EWAIT829 stays behind, becausepipeline::runre-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 (
accumulateisstep != 0) and are issued after the block barrier at the bottom of the pass's first step, by which timetile_x8has retired the drain'stcgen05.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 wasfill 4 511plus5 653at the tail, so −4 511 ticks of a 46 638-tick item, about −9.7% — worth ~1.8 ms of theflash_kvprofile row. Measured numbers to follow; if it does not land there, this comment is what it is measured against.The dK/dV epilogue is the next item's opening move, and the probe say…
feitreim commentedon Aug 5, 2026
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 (
ETREADis 649 of 6 482 ticks) — it is expensive because it is 128 KiB of fp32store_rows(ESTORE5 004) sitting in front of a fill window 3.5× kernel A's.EPI6 482 → 838,FILL4 511 → 350,DRAIN0 → 6 607backward.attention.flash_kv18.1238 → 17.5028 (−3.4%)flash_kv−0.3985 and −0.6128, throttled 2.3×backward attentionon that pair is 35.155 → 34.756. (My ownmain-side standalone readsflash_kv18.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 attentionis 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%.M128_N64N=128accumulations landed; the score MMAs' 43% is 29 KiB of shared memory out of reachSTATis 290 of a 3 964-tick visit and no longer a leading termThe 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 252ARITHticks areexp2against 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:
TREAD 241 / ARITH 2 660 / STORE 449against kernel A's254 / 1 252 / 318— the second swizzled store ofPᵀis worth 131 ticks and the TMEM read is flat, on identical operation counts. The only candidate I can name is that ferro'scol_map_assignreads its operandSLOTS × VALUEStimes whererow_map_assignhoists it toSLOTS, which is exactly thesub_col_assign/sub_row_assigndifference 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.ESTOREis 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]BaseLdtmband'sstore_rowsputs 8 rows × 32 bytes behind each store instruction, which is the fragment layout's own coalescing and not a call-site choice.TMA 61 + SMMA 1 013 + 2 × GMMA 182 ≈ 1 440against 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.
Merge pull request #102 from feitreim/flash-kv-forensics
feitreim commentedon Aug 5, 2026
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).