Repository navigation
The dY staging pass keeps the row dot it was already holding - #114
Merged
Merged
Conversation
`flash_attention_backward_dot_bf16` and `stage_attention_heads_bf16` open the flash backward by reading the same fp32 `[N, D]` gradient back to back: one reduces the per-row `sum_d dy * y`, the other de-interleaves dY into the packed-bf16 head panels the tcgen05 kernels stream with TMA. The dot needs exactly the values the staging pass already has in registers. `stage_attention_dy_dot_bf16` does both in one traversal. One warp owns each `(row, head)` and each lane owns pairs `lane` and `lane + 32` — the twin's elements `i` and `i + 64` — so the twin's first tree step is the lane's own add and the rest is a butterfly over the same pairings. The staged words are the same arithmetic on the same bytes. Both outputs are the composition's bit for bit, which is what the new third-tier gate asserts. Only the staged arm has a dY panel; the oracle arms keep the standalone dot.
The staged words matched on the first run; the dot was two ulp off. Nothing separates the fused kernel's two products from the add that consumes them, so the backend contracted them into an fma the twin cannot reach — its products go through shared memory first. Spelling the mul and the add rn makes the two kernels compute the same number, and the third-tier gate now passes exactly.
`compare_profile` builds a retained baseline beside the mounted candidate and runs them back to back, but only for `bin/profile`. Throughput is claimed with `bin/train.rs`, and SPEC 11 wants the pair from one container, so give train the same shape: clone the ref, rewrite both trees' compile-time batch, run.
… it correct Fusion raises liveness, and a kernel that crosses an occupancy step can lose on the clock while winning on traffic — so the parity harness now prints what ptxas gave all three kernels and how many blocks the driver will resident. `bench_util::function_profile` already existed for exactly this; ncu's counter library does not load on the B200 container, so the driver is the readout.
Owner
Author
|
Heads-up on overlap: there is a |
This was referenced Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fusion item A3 from #107. The flash backward opened with two kernels reading
the same fp32
[N, D]gradient back to back:The dot needs exactly the numbers the staging pass already holds in registers.
stage_attention_dy_dot_bf16produces both in one traversal, sodyis readonce instead of twice: 4.50 GiB/step and 12 launches, as #107 priced it.
Only the staged arm has a dY panel to write. The
Wideoracle arms keep thestandalone
backward_dot, which is why both kernels stay.The bit-exact part
check_fused_staging(#88/#90) set the precedent: when everything a fusiondrops was storage between two kernels, the substitution owes exact parity,
not a tolerance. That is reachable here for the dot too, and the new gate
asserts it — every staged word and every dot bit.
The reduction is what makes it work. The twin walks a shared-memory tree over
HD = 128lanes:P[i] += P[i+64], then 32, 16, … The fused pass gives each(row, head)one warp and each lane pairslaneandlane + 32— the twin'selements
iandi + 64. So the twin's first tree step is the lane's own add,and the five
shfl.bflysteps that follow pair the same indices the remainingtree levels do. Lane 0 ends holding the twin's number, not an equivalent one.
One thing had to be said out loud. The first device run matched every staged
word and missed the dot by two ulp: nothing separates the fused kernel's two
products from the add that consumes them, so the backend contracted them into
an
fmathe twin cannot reach (its products go through shared memory first).float::mul_rn_f32/float::add_rn_f32spell the twin's arithmetic, and thegate then passes exactly.
Measured
bin/train.rs, B=16, B200, same shard and seed, step 1 outside the timingwindow. Three baseline/candidate pairs, each pair inside one container:
dc6f533Call it ~+1.4%. Note the baseline itself reads 78.8k / 81.9k / 83.3k across
the three containers — a 5.7% spread on identical code. That is the whole
reason SPEC §11 wants the pair from one container, and it is why the between
container numbers in #107's thread should not be differenced.
Where the time goes
compare_profile, both binaries in one container (modal run modal_app.py::main --kernel model --baseline-ref dc6f533):backward.attention.flash_dotbackward.attention.stage_bf16backward.attention.stage_dot_bf16The traffic argument does not explain this, and it is worth saying so.
4.50 GiB/step at the B200's HBM rate is worth roughly 0.6 ms; the measured
saving is 5.83 ms. Read it as achieved bandwidth instead:
flash_dotstage_bf16Half the win is the halved traffic. The other half is that
flash_dot's shapewas the problem: 786 432 blocks of 128 lanes, each spending seven
__syncthreadson a shared-memory tree to emit one float, at 1.4 TB/s. A warpper row with five shuffles and no barrier is simply a better way to reduce 128
elements, and the fused pass ends up faster than the staging kernel alone
(1.81 ms vs 2.53 ms) despite reading the
ypanel the staging kernel nevertouched — it also does the
%//decode of the head layout once per twopairs instead of once per pair.
Registers and occupancy
Fusion raises liveness, so the harness now asks the driver directly
(
bench_util::function_profile, which already existed for this; ncu's counterlibrary does not load on the B200 container, so it is the driver or nothing):
+4 registers over the staging kernel, no spill, and 8 × 256 = 2048
threads/SM — the same ceiling both kernels it replaces already sat at. No
occupancy step is crossed. (Numbers are the
flash-attncrate's composition.)Gates
modal run modal_app.py::main --kernel flash-attn— the new third-tier gatepasses at
(1,128,2),(2,256,3),(1,1024,4), asserting the fused kernel'sstaged words and dot bits against
stage_attention_heads_bf16+flash_attention_backward_dot_bf16exactly. Every existing flash gatestill green.
modal run modal_app.py::main --kernel model— all 15 end-to-end gates greenat the existing tolerances (
BF16_ATOL = 3e-3,BF16_RTOL = 3e-2).Nothing loosened.
Since both device outputs are bit-for-bit the twin's, the trajectory can only
move through the trainer's existing atomics nondeterminism (#108 measured that
at |Δloss| = 0.077 over 100 steps for main against itself); the logged losses
across the six runs above are consistent with that and step 1 is identical to
the last digit in all of them.
compare_traincompare_profilebuilds a retained baseline beside the mounted candidate andruns them back to back, but only for
bin/profile. A throughput claim is madewith
bin/train.rs, and SPEC §11 wants the pair from one container, so thisadds the same shape for train: clone the ref, rewrite both trees' compile-time
Bthe waybatch_sweepdoes, run both.Two operational notes for whoever uses it next. It refuses a baseline pinning a
different cuda-oxide revision rather than silently installing one —
compare_profileis the entry point that handles historical refs. And withseveral agents running against the same Modal app, the CLI log stream can
interleave another run's output into yours: check that the log holds exactly
two
training shard=lines at the step count you asked for before differencingit. One of my pairs had to be discarded for this.
Not in this PR
A3 is the smallest of #107's three attention-adjacent items. A8 (stop
materializing the V head panel, 6.75 GiB) is untouched and still needs flash's
TMA descriptor over the fp32 qkv panel. The
Widearms keep the standalonebackward_dotbecause they have no dY panel to fold it into — the fp32 tiledand per-row oracle paths are unchanged, which is what keeps them oracles.