Skip to content

The HD-wide accumulations are one N=128 band, and the dQ drain is the next item's opening move (#94) - #100

Merged
feitreim merged 5 commits into
mainfrom
flash-q-issue-forensics
Aug 5, 2026
Merged

feitreim merged 5 commits into
mainfrom
flash-q-issue-forensics

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Remedy 2 of #94, and the forensics that retargeted it. Phase 0 is posted on the issue; this is what it licensed. Needs ferro-kittens#195 (pinned here at 1eefabb, to be re-pinned to the merged main rev).

What phase 0 established, in one line each

  • The issue warp is back-pressured, not tolled. In every cadence variant issue/instr == retire/instr, so ISSUE is the tensor core's retirement rate. A second issue warp is priced at exactly zero and is not in this PR.
  • N = 64 runs the tensor core at 57% of N = 128 — 55.7 against 32.0 ticks per M128_N64_K16-equivalent, with N = 256 no better than 128.
  • ISSUE is 4% TMA, 72% score MMAs, 24% gradient MMA. Barrier arms do not show.
  • PASS is 21% TMEM read, 61% arithmetic, 18% store — not the read, which is what I expected.
  • 28% of kernel A is per-item, not per-visit: FILL 3534 + EPI 3470 + GAP 1624 of a 30 108-tick item.
  • Kernel A's ceiling from issue-side work alone was −3.5% of the visit, because the pass warps were at 2133 against the issue warp's 2210. So this PR is not only issue-side work.

The change

1. Every HD-wide accumulation is one N = 128 band instead of two N = 64 ones (ferro-kittens#195). An MN-major B reaches its second stacked subtile through the descriptor's leading offset, which is how mma_atb has always covered a 128-wide MN in one instruction; mma_ab was the last caller banding it by hand. MMA_SHAPE splits into the two shapes that were always there:

const SCORE_SHAPE: MmaShape = MmaShape::M128_N64;   // N is the streamed tile's keys
const OUTPUT_SHAPE: MmaShape = MmaShape::M128_N128; // N is the accumulator's HD

Four call sites take OUTPUT_SHAPE: the forward's O = P·V, kernel A's dQ += dS·K, kernel B's dV += Pᵀ·dY and dK += dSᵀ·Q. Each halves its instruction count and runs the tensor core at 32.1 ticks/unit instead of 48.2. Kernel A's GMMA column: 357 → 184 ticks a visit.

2. The dQ drain is the next item's opening move, not this item's last. EPI was 3470 ticks an item, of which the wait for the last gradient MMA was 513 and the .x8 drain plus store_rows was 2957. The wait cannot move — pipeline::run re-arms the barrier set at the item boundary — but the drain can, and one line below where it lands the pass warps were spending 3534 ticks idle at scored.wait(0) waiting out the next item's TMA fill. So the item stages its row and the next item drains it, with a flush after the persistent loop for the last one. That is pipeline::Job's own documented deferred-store shape, and the hand-off to the next item's first gradient MMA (which overwrites the accumulator) is the block barrier the pass already takes after its first tile.

3. TmemTile::fragment issues both loads before waiting (also ferro-kittens#195). tcgen05.wait::ld retires prior loads plural, so a wait after each meant the .x1 path never had more than one in flight. TREAD 381 → 303.

4. The instrument, which belongs in main on its own merits the way #95's did. phase_probe gains a partition of ISSUE (TMA + SMMA + GMMA) and of PASS (TREAD + ARITH + STORE), plus FILL, GAP, EPI and DRAIN per item; and src/bin/probe.rs gains the MMA cadence table — one warp, one CTA, chained walks into a resident accumulator, N the only thing that moves. It keeps the banded mma_ab walk locally so the comparison survives the ferro change.

What it buys

./run.sh flash-attn probe times the shipped kernels alongside the instrumented ones, so each container gives an independent same-container reading of both. Three shapes, three containers, three tree states:

shape d6b99ea + wide band + deferred drain Δ
[32,1024,24,128] backward q 0.684 0.641 0.618 −9.6%
backward kv 1.081 1.078 1.068 −1.2%
[12,2048,24,128] backward q 0.861 0.788 0.776 −9.9%
backward kv 1.356 1.349 1.328 −2.1%
[32,512,24,128] backward q 0.238 0.230 0.221 −7.1%
backward kv 0.369 0.354 0.358 −3.0%

./run.sh flash-attn flash, [32,1024,24,128]:

d6b99ea this PR Δ
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%

The forward moves too, and it is the same one-line change: its O = P·V was two N = 64 bands.

The budget it leaves

[32,1024,24,128], the instrumented column, d6b99ea against this PR:

before after
SCORED 488 217
GRAD 121 126
STAT 18 448 the load_row_vec the drain now sits in front of
PASS 1455 1858 probe-inflated; the splits below are the same run
pass warps / visit 2133 2687
FEED 268 419
ISSUE 1706 1384
IDLE 154 336
issue warp / visit 2210 2220
per item: FILL 3534 1132 the drain went in here
per item: EPI 3470 495 the wait that cannot move
per item: DRAIN — 3673
span / item 30 866 27 860

ISSUE splits TMA 61 / SMMA 1142 / GMMA 180 and PASS splits TREAD 311 / ARITH 1247 / STORE 299.

Two honest readings of that table. The item got 10% shorter and that is the result; the visit columns got longer, because moving 3.6k ticks of store into the item's head displaces work the fill used to absorb and the probe charges the residue to STAT and FEED. And kernel A is issue-bound again by a hair — 2220 against 2687 is pass-bound on paper, but IDLE 336 against SYNC 38 says the pass warps are the ones with slack once the per-item terms are taken out.

Where kernel A now sits: SMMA is 1142 of a 1384-tick ISSUE, all of it M128_N64 at 43% off the tensor core's rate, and widening it needs a 128-key tile that shared memory forbids by 29 KiB (priced in the phase-0 comment). ARITH is 1247 of PASS, and ~512 ticks of that is exp2 against the SFU's 16 lanes/SM/cycle, which no scheduling removes. The next real lever on this kernel is neither.

Gates

  • --kernel flash-attn --bin flash — green, and byte-identical to d6b99ea: dq 7.299e-4 / 5.203e-4 / 6.458e-4, dk 7.322e-4 / 7.437e-4 / 7.084e-4, dv 2.933e-3 / 2.717e-3 / 2.911e-3, y ≤ 1.469e-3, lse ≤ 9.537e-7 — the same digits The backward's register pass splits across two warpgroups over the same TMEM lanes (#94) #98 published. The forced-correction shape corrects on 100.00% of its 32 eligible visits and every other shape on 0.00%. It is byte-identical for a reason and not by luck: the wide band issues the same products over the same K chunks in the same order into the same accumulator columns, the deferred drain is the same drain later, and the batched tcgen05.ld pair is the same two loads.
  • --kernel flash-attn — green (the two-oracle gate), dq ≤ 2.726e-3, dk ≤ 1.730e-3, dv ≤ 5.377e-3.
  • --kernel ops — green.
  • --kernel model — green, 14/14 including both MoE substitutions, aligned_tcgen05_linears, checkpoint v5 and all four overfit gates. The MoE overfit lands 2.949803 → 0.000031, the recorded value exactly.
  • ptxas / KernelBudget: forward 128 / 0 and backward q 128 / 0, both unchanged; backward kv 140 → 138 / 0, ratcheted. Zero spill everywhere. The two mma_ab walks of kernel B each held two banded operand descriptors live where they now hold one.

Same-container A/B, both orders

--baseline-ref d6b99ea, and throttled containers — the full step reads 764–776 ms where a clean standalone profile of either tree reads 339–344 (2.3x, the hazard #96 named). The row deltas are the measurement, not the percentages.

row baseline d6b99ea candidate (this PR) Δ
forward.attention.flash 27.7389 27.3804 −0.3585
backward.attention.flash_q 27.0589 26.4490 −0.6099
backward.attention.flash_kv 48.3771 47.8440 −0.5331
backward.attention.flash_dot 5.1068 5.0945 −0.0123
full step 766.4504 764.4800 −1.9704

Order reversed — this branch retained and profiled first at b1d8925, d6b99ea passed as the candidate, so the sign of every row flips and the Δ column below is still "what this PR saves":

row this PR (retained) d6b99ea (candidate) Δ
forward.attention.flash 27.5095 28.0034 −0.4939
backward.attention.flash_q 26.6185 27.6763 −1.0578
backward.attention.flash_kv 47.9115 49.0353 −1.1238
backward.attention.flash_dot 5.1075 5.1695 −0.0620
full step 768.8183 776.3439 −7.5256

Order does not change a sign: flash_q −0.61 one way and −1.06 the other, forward −0.36 and −0.49, flash_kv −0.53 and −1.12, flash_dot flat both ways. The named rows sum to −1.51 against a −1.97 step and −2.74 against a −7.53 step; the step total is the noisier of the two at this resolution and the row is what carries the claim. As in #98, the milliseconds survive the throttle better than the percentages do — what these changes remove is instruction issue and a store, neither of which is clock-throttled the way the surrounding kernel is.

Clean standalone profiles

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

row d6b99ea this PR Δ
forward.attention.flash 15.2249 14.5198 −0.7051 (−4.6%)
backward.attention.flash_q 12.8085 11.4146 −1.3939 (−10.9%)
backward.attention.flash_kv 19.0367 18.7030 −0.3337 (−1.8%)
backward.attention.flash_dot 5.9707 5.9693 −0.0014
backward attention 37.816 36.087 −1.729
attention, forward and backward 53.041 50.607 −2.434
full step 343.9142 339.0440 −4.870

backward attention is 57.01 → 43.80 → 36.77 → 36.09 ms over #94's four phases, and the forward joins the ledger for the first time since #68.

Training smoke

--kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100 — 74 040 tokens/s, 32.88% MFU over 99 measured steps, loss 11.107 → 8.114 on #98's own curve (8.280 / 8.148 / 7.878 at steps 40/50/70). Against #98's 74 663 / 33.15% that is −0.8%, inside the ~2.9% the MoE scatter atomics give this run, and it shared its GPU with two other jobs besides. This gate is "it still trains"; the rows above are what carries the claim.

What this does not do

No second issue warp — the cadence prices it at zero, and that is the one thing this PR could have been about that it deliberately is not. BACKWARD_STAGES stays 3, GRADIENT_STAGES stays 2, PASS_GROUPS stays 2, SCORE_CHUNK stays 16, and the key tile stays 64 because 128 needs 256 KiB of shared memory against 227. Kernel B keeps its own epilogue: it drains two accumulators where A drains one, its EPI is 6347 ticks an item, and deferring it is the same change again on a kernel that is not this issue's target — worth its own increment, not this one's diff.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Merge order

This PR pins ferro-kittens at 1eefabb — the head of ferro-kittens#195's branch, not a main rev. It needs re-pinning to whatever squash-merge lands on ferro main, in all four gpu/*/Cargo.toml (kittens exposes cuda_device types, so a mixed pin is a type error, not a slow build) plus their four Cargo.locks. Nothing else in the diff moves with it; the generated code at the merged rev is identical.

--kernel gemm was not re-run: the ferro change is mma_ab and TmemTile::fragment, and gpu/gemm calls neither (its walks are mma_walk_cg2 and its drain is tile_x8). Its pin moves only so the four crates agree.

@feitreim
feitreim merged commit 29c399e into main Aug 5, 2026
@feitreim
feitreim deleted the flash-q-issue-forensics branch August 5, 2026 21:54
@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Re-pinned to the merged ferro main — 7f9875e

ferro-kittens#195 squash-merged as f64a27b. All four gpu/*/Cargo.toml and their four Cargo.locks now pin it, verified to a single unique rev across the four manifests.

The merged tree is byte-identical to the branch head everything above was measured against — git diff 1eefabb f64a27b is empty, so the generated code cannot differ and no measurement in this PR is stale. Re-ran the load-bearing gate anyway rather than argue it:

--kernel flash-attn --bin flash on the re-pinned tree — green, and every number reproduces:

  • ptxas 128 / 0, 128 / 0, 138 / 0 at .maxntid 128 / 288 / 288 — identical to the pre-repin run.
  • Parity byte-identical, same digits again: 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. Forced-correction 100.00% of 32 eligible visits, 0.00% everywhere else.
  • Bench [32,1024,24,128]: forward 0.793, backward q 0.621, backward kv 1.033, both 1.653 — against 0.789 / 0.617 / 1.032 / 1.662 pre-repin, i.e. inside container noise, and against d6b99ea's 0.843 / 0.682 / 1.085 / 1.768.

The --kernel ops / --kernel model / A/B / standalone results in the PR body stand unchanged on the identical-tree argument.

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