Repository navigation
The AdamW write-back clears the gradient it consumed and emits the master's transpose (#99) - #101
Merged
Merged
Conversation
…ster's transpose (#99)
… 3.93 ms against 0.90
Owner
Author
|
100-step training smoke,
Step 1 is identical to the last digit, which is what the bit-identity gate predicts: before any gradient has been zeroed by the new path, the two builds are the same computation. Everything after diverges only through the MoE scatter atomics, and stays inside the documented ~2.9% smoke noise floor — largest deviation 0.84%, and the sign alternates rather than drifting. |
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.
Closes the first two remedies of #99. Phase 0 measured the optimizer front at 37.30 ms — 11.1% of a 336.74 ms step — at 1.9× its HBM roofline, and found that two of its three passes only exist because the AdamW kernel throws away what they go back to global memory to fetch. This deletes both.
What changed
The AdamW write-back clears the gradient it consumed. Every AdamW kernel already reads each gradient word; it now stores zero over it.
GpuDense::zero_graddrops out of the training and profile steps entirely — 100filllaunches and 15.7 M blocks, gone. The precondition is exact: every bufferzero_gradcleared has exactly one AdamW consumer, and gradient buffers are allocated zeroed, so the invariant "gradients are zero when backward starts" is maintained by the optimizer instead of by a pass in front of it.zero_gradsurvives as an API — a backward checked on its own still needs a way back to a known-zero start, and Muon's four hidden matrices are not AdamW-updated.The AdamW write-back emits the master's transpose beside it.
sync_computere-read a master the optimizer had written microseconds earlier, only to transpose it.adamw_bf16_master_transposedwalks the same 64×64 tiletranspose_bf16_pairsdoes, runs the update on the tile, and stages the rounded result in shared memory so both global stores stay coalesced. The master re-read — 8.46 GB — is deleted outright, along with 48 launches.GpuLinear,GpuGroupedLinearandGpuExpertFfngrow anadamw_stepthat owns this, so the optimizer can no longer forget to sync a compute copy;sync_computestays for checkpoint resume, which refills masters in place, and for Muon.Two things did not fuse, both measured rather than assumed — see Negatives below.
Measured
Same container,
--baseline-ref d6b99ea, run in both build orders, plus a standalone pair. The row is the instrument; the untouched remainder is quoted so you can see the container was not doing the work.Standalone full step: 336.742 → 327.674 ms (−2.7%). The two A/B containers were throttled ~2.4× on the compute-bound rows and barely at all on these memory-bound ones, which is why their absolute front is lower than the standalone one; all three agree on the delta to within the container spread.
Per row, from the standalone pair:
optimizer.experts.gate_up.adamwoptimizer.experts.down.adamwoptimizer.qkv_proj.adamwoptimizer.o_proj.adamwoptimizer.embedding.adamwoptimizer.lm_head.adamwoptimizer.lm_head.sync_w_toptimizer.experts.sync_computeoptimizer.qkv_proj.sync_computeoptimizer.o_proj.sync_computezero_grad.*(10 rows)The front is now at 1.4× its roofline rather than 1.9×: 121.6 GB (the same traffic minus the 8.46 GB master re-read) in 27.285 ms, 4457 GB/s average, against 17.4 ms at a 7 TB/s stream rate.
Negatives, preserved
The lm-head keeps its split write-back. Fusing its transpose measured 3.9292 ms against 0.6764 + 0.2235 for the split pair. Its
[3072, 50432]master has a 98 KiB row stride, so a fused tile's four operand streams each touch 64 rows spread over 6.4 MiB and the read side collapses; every other master's stride is 6–18 KiB. The kernel is deleted rather than kept unused; the doc comment onGpuBf16Head::sync_computerecords why.Clearing the gradient first, rather than last, costs 4×. The packed-gradient write-back originally zeroed its word before reading the moments, and measured 2.83 ms against 0.66. Nothing proves to the compiler that the gradient does not alias the moments, so a store placed ahead of their loads orders every one of them behind it and the update loses its memory-level parallelism. Both helpers now clear last. This is worth knowing before anyone fuses a clear into another kernel.
Numerics
Bit-identical, and gated as such rather than by tolerance.
check_adamw_master_transposedingpu/tensor-gpu/src/main.rsruns the flat and the fused-transposed write-back over the same 128×128 input andassert_eq!s the master words and both moments as words, in both rounding modes, then checks the emitted transpose against the transpose of the master it emitted. It holds because the tile walk changes which thread owns which element but not the arithmetic: every AdamW kernel now routes through oneadamw_element, andround_master's stochastic stream is keyed on the global element index, which the tile reproduces exactly.check_adamw_masteradditionally asserts every gradient comes back zeroed. SPEC decision #20 is untouched — no gradient or moment changes dtype, and fp32 accumulation stays fp32.Gates
--kernel model: 14/14 green, including the stochastic MoE overfit assert (2.949803 → 0.000073), the Muon Newton–Schulz parity, andcheckpoint v5 resumes bit-identically.--kernel ops: green.--kernel tensor-gpu: green, with the new bit-identity parity gate above.KernelBudgetentries exist for the tensor/model kernels (enforce_kernel_budgetsis wired up only ingemmandflash-attn), so there is no ratchet to update here.One check needed a real change rather than a mechanical one: the dense parity check runs Muon over the exact same gradients the AdamW step just saw, which is precisely what an AdamW step that clears its gradients breaks.
DenseGradients::save/restoresnapshots them and puts them back, which states the new contract instead of hiding it.What is left on #99
Remedy 3 — a compile-time-bounded, vectorized walk for the remaining flat elementwise kernels — is untouched.
fillstill runs at ~1.6 TB/s and still costs 0.98 ms in the router's three zeroing passes outside the optimizer. The four tiny per-block AdamW launches (remedy 4) still cost 0.30 ms for 10 MB of traffic.