Skip to content

The dK/dV epilogue is the next item's opening move, and the probe says which of its three parts that was worth (#94) - #102

Merged
feitreim merged 1 commit into
mainfrom
flash-kv-forensics
Aug 5, 2026
Merged

feitreim merged 1 commit into
mainfrom
flash-kv-forensics

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Phase 4 of #94, on kernel B. Phase 0 is posted on the issue; this is what it licensed, and the forensics that licensed it are half the diff.

#100 closed with kernel B named as the next increment on the strength of one number it had not decomposed — EPI 6 347 ticks an item. So the first thing here is the decomposition, because #98 already has one refuted reading of this kernel on the record ("saturated store port") and a second guess was not worth taking.

Phase 0, in four lines

  • The epilogue is the global store. EPI splits EWAIT 829 + ETREAD 649 + ESTORE 5 004. The two tcgen05.ld bands — the thing that makes kernel B's epilogue structurally different from kernel A's — are 10% of it. It is 128 KiB of fp32 store_rows, at a per-byte rate no worse than kernel A's 64 KiB.
  • Its fill window is 3.5× kernel A's. FILL 4 511 against 1 306, because a kernel-B item opens by TMA-ing 64 KiB of resident K and V plus three staged query tiles while the pass warps sit at scored.wait(0).
  • 27% of the kernel is not in the visit at all — FILL 4 511 + GAP 1 609 + EPI 6 482 of a 46 638-tick item.
  • The ISSUE/IDLE split is instrument-sensitive and the column total is not. The clean instrument reads ISSUE 3 188 / IDLE 23 where The HD-wide accumulations are one N=128 band, and the dQ drain is the next item's opening move (#94) #100 read 1 564 / 1 642; the sums are 3 211 and 3 206, and every pass-side column reproduces. Kernel B's irreducible issue work is TMA 61 + SMMA 1 013 + 2 × GMMA 182 ≈ 1 440, so the pass has ~1 000 ticks of room before the issue side binds — not the 2 100 an IDLE reading implies.

The change

The dK/dV drain is the next item's opening move. BackwardKvStream gains staged_row / staged_head and a drain, the item's bottom keeps only the accumulated.wait that cannot cross pipeline::run's barrier re-arm, and a flush after the persistent loop settles the last item. It is BackwardQStream::drain with a second band, and the hazard resolves identically: 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 issue warp's job.drain call is a predicated no-op — it never stages a row — which is what keeps the out-of-quadrant group = 2 it would compute from ever addressing anything.

And the instrument, which belongs in main for #95's and #100's reason. Kernel B gets the TMA + SMMA + GMMA partition of ISSUE and the per-item FILL / GAP that only kernel A had; both kernels get EPI split into EWAIT + ETREAD + ESTORE. Kernel B's PASS split is in the file behind phase_probe::KV_PASS_SPLIT, off, because there it is not free — see the last section.

What it buys

The tick budget is the container-independent instrument, so it leads. Kernel B, per item, main @ f15b471 against this PR:

[32,1024,24,128] [12,2048,24,128] [32,512,24,128]
main this PR main this PR main this PR
EPI 6 482 838 6 504 836 6 492 834
FILL 4 511 350 3 947 355 4 477 509
DRAIN — 6 607 — 6 550 — 6 657
GAP 1 609 1 273 1 572 1 261 1 556 1 237
span / item 46 638 44 087 75 698 72 889 30 892 28 472
−5.5% −3.7% −7.8%

EPI falls to EWAIT exactly, and FILL falls to nothing: the drain went into the window and the window is gone. The drain itself costs +954 ticks where it landed (5 653 → 6 607) — its stores now contend with the TMA fill they are hiding behind — so the item saves 10 993 → 7 795, not the whole 5 653. That is the honest reading of why the shortest shape moves most: the fill window is the same size at every shape and the item it is amortized over is not.

Kernel A's columns are unchanged in the same pair of runs (EPI 493/492, DRAIN 3 955/3 952, span 28 276/27 827).

Clean standalone profiles, §13.9 shape (L=12, B=12, T=2048, D=3072, H=24, HD=128, E=8, K=2), two containers whose full steps agree to 0.4%:

row main f15b471 this PR Δ
forward.attention.flash 14.5477 14.4738 −0.0739
backward.attention.flash_q 11.2004 11.3339 +0.1335
backward.attention.flash_kv 18.1238 17.5028 −0.6210 (−3.4%)
backward.attention.flash_dot 5.8310 5.9190 +0.0880
backward attention 35.155 34.756 −0.399
full step 327.4231 328.7744 +1.35

−3.4% on the row against −3.7% on the tick budget at the same shape, which is the agreement that carries this: two instruments, one clock-free.

Same-container A/B, both orders

--baseline-ref f15b471, throttled containers — the full step reads 754–756 ms where the standalone pair above reads 327–329, i.e. 2.3×, the hazard #96 named. The rows are the measurement.

row baseline f15b471 candidate (this PR) Δ
forward.attention.flash 27.4645 27.5124 +0.0479
backward.attention.flash_q 26.6127 26.4565 −0.1562
backward.attention.flash_kv 48.0492 47.6507 −0.3985
backward.attention.flash_dot 5.1183 5.0950 −0.0233
full step 755.5718 754.3225 −1.2493

Order reversed — this branch retained and profiled first at 6357e36 (tree-identical to the head here), f15b471 passed as the candidate, so the Δ column is still "what this PR saves":

row this PR (retained) f15b471 (candidate) Δ
forward.attention.flash 27.0970 27.2493 −0.1523
backward.attention.flash_q 26.2121 26.3799 −0.1678
backward.attention.flash_kv 46.8523 47.4651 −0.6128
backward.attention.flash_dot 5.0401 5.0732 −0.0331
full step 746.8555 749.6394 −2.7839

Both orders put flash_kv down (−0.3985 and −0.6128) and the named rows account for the step delta each way to within the noise on the untouched ones.

The throttled A/B under-reports this change relative to both clean instruments (−0.8% and −1.3% on the row against −3.4%), and it is worth saying why rather than picking the number I like: what this PR moves is a store into a TMA fill, and a container 2.3× off its clocks stretches both. The A/B agrees on the sign in both orders; the standalone pair and the tick budget agree on the size.

./run.sh flash-attn flash, [32,1024,24,128] — forward 0.793, backward q 0.617, backward kv 1.039, both 1.655, against #100's post-repin 0.793 / 0.621 / 1.033 / 1.653. That is a cross-container comparison and it reads flat; the probe's own shipped-kernel column, which times the shipped kernel beside the instrumented one, reads backward kv 1.063 → 1.037, 1.307 → 1.273 and 0.354 → 0.329 across the three shapes. Both are cross-container and neither is the instrument the span column is.

Gates

What is left in kernel B, and one thing I did not build

With the item-level work banked, kernel B's remaining cost is the visit, and the visit is the pass: PASS 2 974 of a 3 595-tick pass column, against an issue column whose irreducible part is ~1 440.

The probe answers what the pass is made of, and the answer costs something to get — kernel B's seven stops a chunk are +8.9% against its shipped self (kernel A's three are +3.4%) and move 1 600 ticks a visit out of IDLE and into GMMA. So it ships behind KV_PASS_SPLIT = false, 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 arithmetic, not the second store_tile — the extra swizzled store of Pᵀ is worth 131 ticks and the TMEM read is flat. The two passes issue the same operations on the same shapes; the difference is that kernel B's two subtracts are sub_col_assign where A's are sub_row_assign, and ferro's col_map_assign reads its operand SLOTS × VALUES times where row_map_assign hoists it to SLOTS. Filed as ferro-kittens#196 with the measurement and the six-line mirror-image fix, explicitly unverified — it is a hypothesis with a symptom, not an attribution, and it wants a cadence pair before it wants a PR. Not in this branch, and this branch does not depend on it.

The other residual is the one this PR could not remove: ESTORE is still 5 928 ticks an item, now hidden rather than paid, and 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 layout's own coalescing and not a call-site choice.

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