Skip to content

The flash forward rescales O inside its own tensor-memory segment, and the last .local frame in the module goes to zero - #77

Merged
feitreim merged 2 commits into
mainfrom
feat/flash-frame-relook
Aug 4, 2026
Merged

feitreim merged 2 commits into
mainfrom
feat/flash-frame-relook

Conversation

@feitreim

@feitreim feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Closes out the flash half of the .local attribution the rewrite wave (#74/#75) opened and the ferro pin bump (#76) half-answered.

The census

modal run modal_app.py::main --kernel flash-attn --ptx at ferro c648c67, sm_100a. Depots in flash.ptx, plus what ptxas reports:

kernel LLVM depot regs / spill before #180
flash_backward_q none 182 / 0 244 / 528
flash_backward_kv none 190 / 0 244 / 528
flash_forward __local_depot2[560], 405 ld/st.local 193 / 560 244 / 1136

ferro #180 did all of the backwards' frames and none of the forward's — their 528 B was the pipeline::Job struct reached through the library's mover walks, and unrolling those took it whole. That is why the pin bump realized 15.7% and 22.0% on the two backwards and only 9.8% on the forward.

The forward's 560 B was out_acc: the RegTile<32, 128, BaseLdtm> output accumulator the conditional correction carried from the first key tile to the epilogue, address-taken because reg::online_rescale and the local merge_output_tile both wrote it through &mut. Nothing about it was hot — the correction fires on 0.00% of tile visits at every gated shape, including 49152 eligible ones at the bench shape. What cost was the two unconditional touches, the tile-0 rescale and the epilogue merge, each walking 128 depot slots once per work item.

The fix

tcgen05.st was absent when the correction scheme was designed and is TmemTile::store_tile now, so the rescale happens where the accumulator lives. A correction drains the O segment, scales it, stores it back and leaves enable_d on; the segment keeps accumulating instead of restarting, and the epilogue drains it once, 64 columns at a time. merge_output_tile is gone.

Same arithmetic in the same order, applied to the accumulator rather than to a copy of it — so parity is unchanged to the digit.

Numbers

B200, [32,1024,24,128], main and this branch measured back to back.

main (ferro c648c67) this PR
forward 1.212 ms, 180.8 TFLOP/s 0.832 ms, 263.2 TFLOP/s −31.4%
backward q 1.095 ms 1.095 ms untouched
backward kv 1.553 ms 1.540 ms untouched (noise)

ptxas: forward 128 regs / 0 B against 193 / 560. Occupancy is unchanged — the forward plan is 114836 B and still 2 CTA/SM.

Parity is byte-identical to the main run at every gated shape: y ≤ 1.469e-3, lse ≤ 9.537e-7, dq ≤ 7.299e-4, dk ≤ 7.437e-4, dv ≤ 2.933e-3, corrections 0.00% everywhere.

All three pins ratchet to the measured numbers (128/0, 182/0, 190/0), so the gate is exact and every frame in the module is now zero.

The width is the lesson

Four formulations, one container, same shape:

form ptxas forward
resident &mut accumulator (main) 193 / 560 1.212 ms
segment drained whole into a short-lived &mut band 191 / 1072 0.996
the same band by value 253 / 1024 over the pin, never timed
64 columns at a time 128 / 0 0.832

Two things that a frame census alone would have got wrong:

  1. The frame grew and the time fell 18%. What costs is the local traffic in the loop, not the bytes in the depot.
  2. A 128-register fp32 band cannot be transformed on the way out of tensor memory, whichever way it is spelled. By &mut it is address-taken; by value the drain and the result are live together and ptxas allocates 253. Only the 64-wide form is both zero-frame and fastest — ferro #181's streaming fix, one axis over from what SCORE_CHUNK already found on the score band.

The width by itself is not the problem: both backward kernels drain the full [32, 128] into store_rows and carry 0 B. It is the transform in between.

Findings posted to ferro-kittens#166 (the standing .local umbrella). No ferro change is required — TmemTile::store_tile + store_wait supplied the whole fix, and its device-tested round trip is what made it writable without a probe of my own.

…d the last .local frame in the module goes to zero

A PTX census after the ferro c648c67 pin (#76) found exactly one LLVM depot
left in tcgen05.rs: `flash_forward`, 560 B and 405 `ld/st.local`, with neither
backward kernel carrying one at all. ferro #180 unrolled the library's mover
walks and that was the whole of the backwards' 528 B; the forward's was its
own, and it was `out_acc` — the 128-register output accumulator the conditional
correction carried from the first key tile to the epilogue, address-taken
because `online_rescale` and `merge_output_tile` both wrote it through `&mut`.

Nothing about it was hot: the correction fires on 0.00% of tile visits at every
gated shape. What cost was the two unconditional touches, the tile-0 rescale
and the epilogue merge, each walking 128 depot slots once per work item.

`tcgen05.st` was absent when that correction scheme was designed and is
`TmemTile::store_tile` now, so the rescale happens where the accumulator lives:
a correction drains the O segment, scales it, stores it back and leaves
`enable_d` on, instead of restarting the segment and keeping the running output
in registers. The epilogue drains once. Forward 1.212 -> 0.832 ms at
[32,1024,24,128] on B200, 180.8 -> 263.2 TFLOP/s, 128 regs / 0 B frame against
193 / 560, with parity identical to the digit at all five gated shapes — the
rescale is the same arithmetic in the same order, applied to the accumulator
rather than to a copy of it.

The width is the lesson, and it is ferro #181's. Three formulations were
measured in the same container before the fourth: the resident `&mut`
accumulator, 560 B and 1.212 ms; the segment drained whole into a short-lived
`&mut` band, 1072 B and 0.996 — the frame grew and the time fell 18%, because
what costs is the traffic in the loop and not the bytes in the depot; that band
by value, where the drain and the rescaled result are live together, 253
registers and over the ptxas pin. Only 64 columns at a time is both zero-frame
and fastest. A 128-register fp32 band cannot be transformed on the way out of
tensor memory whichever way it is spelled — and both backward kernels drain the
full 128 into `store_rows` at 0 B, so it is the transform in between and not
the width by itself.

All three ptxas pins ratchet to the measured numbers: forward 128/0, backward q
182/0, backward kv 190/0. Every frame in the module is now zero.
…d a negative control proving the case can fail

Every forward parity shape reports 0.00% corrections, which is the number the
old scheme's checklist reported and read as good news. It is also the reason
the byte-identical parity in the previous commit is genuine for the epilogue
drain and vacuous for the correction: not one of the five shapes executes the
mid-stream path at all, so nothing gated the segment being drained, scaled,
`tcgen05.st` back and accumulated onto with `enable_d` still on. Uniform
operands cannot climb CORRECTION_THRESHOLD; a gate has to make them.

`CORRECTION_KEY_GAIN` scales key `p`'s staged row by `1 + 7*(p / FLASH_TILE)`.
A score is linear in K, so that multiplies the score by the same factor and a
tile's row max is its gain times the unramped one — the climb per key tile is
~7.7 base-2, just above the threshold, which is the side of it worth testing:
the correction fires on every eligible visit and the output mass accumulated
before each one is the largest the threshold permits. Q and V stay uniform, and
the ramp is applied before staging so the CPU reference — which reads the
staged operands — needs no ramp of its own. At [1,512,2] it corrects on 32 of
32 eligible visits (100.00%) with y <= 1.156e-3 and lse <= 2.842e-4 against
tolerances left exactly where they were, and the count is asserted nonzero so a
ramp that stopped tripping the threshold fails loudly instead of going quiet.

The gate was then checked against a negative control, because a test that
cannot fail is the same hole wearing a different shape. With the two
`rescale_half` calls deleted and the vote, the counter, `store_wait` and
`enable_d` all left standing, the five uniform shapes pass byte-identical and
the ramped one fails on y by 0.114 against a 0.0062 tolerance. Only y catches
it: lse is unmoved, because `running_sum` is rescaled in registers either way
and it is only the O segment that goes stale.
@feitreim

feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner Author

The mid-stream rescale now has a gate, and the gate has a negative control

You were right, and it was worse than "untested": the five uniform shapes correct on 0.00% of eligible visits, so all of them together gated the epilogue drain and none of them entered the mid-stream path. Pushed 6c37d25.

The case

CORRECTION_KEY_GAIN = 7.0 scales key p's staged row by 1 + 7·(p / FLASH_TILE). A score is linear in K, so that multiplies the score by the same factor and a tile's row max is its gain times the unramped one — the climb per key tile is ~7.7 base-2, just above CORRECTION_THRESHOLD. That is the side of the threshold worth testing: the correction fires constantly and the output mass accumulated before each one is the largest the threshold permits, rather than an already-negligible term a broken rescale could hide under. Q and V stay uniform, and the ramp is applied before staging, so the CPU reference — which reads the staged operands through staged_value — needs no ramp of its own.

Result at [1,512,2,128], tolerances left exactly where they were:

tcgen05 forward parity against ramped-key staged-bf16 CPU reference [1,512,2,128]
  y         max abs error: 1.156e-3     (tolerance 5.0e-3)
  lse       max abs error: 2.842e-4     (tolerance 1.0e-3)
  corrections: 32 of 32 eligible tile visits (100.00%)

print_correction_rate now returns the total and the call site asserts it nonzero, so a ramp that stopped tripping the threshold fails loudly instead of going quiet.

The negative control

A gate that cannot fail is the same hole in a new shape, so I ran one: the two rescale_half calls deleted, everything else — the vote, the counter, store_wait, enable_d — left standing.

shape broken kernel
all five uniform pass, byte-identical (y 1.115e-3 / 1.058e-3 / 1.058e-3 / 1.058e-3 / 1.469e-3)
ramped [1,512,2] fails — y mismatch at 16640: tcgen05=-0.36177123, reference=-0.24735637, error=0.114414856, tolerance=0.006236782

0.114 against 0.0062 is 18× over. That is the direct measurement of what the old suite was missing.

One detail worth keeping: only y catches it — lse is unmoved. running_sum is rescaled in registers either way; it is only the O segment that goes stale. So the assert that matters is y at a shape that actually corrects, and lse agreeing is not evidence of anything here.

Full suite

One modal run modal_app.py::main --kernel flash-attn --bin flash, everything green, pins unchanged:

  forward             128 regs,    0 spill bytes, maxntid 128
  backward q          182 regs,    0 spill bytes, maxntid 128
  backward kv         190 regs,    0 spill bytes, maxntid 128
✓ tcgen05 parity passed
tcgen05 forward [32,1024,24,128]: 0.833 ms, 262.8 TFLOP/s
tcgen05 backward kv [32,1024,24,128]: 1.559 ms, 562.0 TFLOP/s
tcgen05 backward q [32,1024,24,128]: 1.097 ms, 599.2 TFLOP/s

The timed numbers stand (0.832 / 0.833 across two runs). SPEC's #77 entry records the gate, the 100% rate and the negative control.

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