Skip to content

The AdamW write-back clears the gradient it consumed and emits the master's transpose (#99) - #101

Merged
feitreim merged 6 commits into
mainfrom
optimizer/fuse-zero-and-transpose
Aug 5, 2026
Merged

feitreim merged 6 commits into
mainfrom
optimizer/fuse-zero-and-transpose

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

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_grad drops out of the training and profile steps entirely — 100 fill launches and 15.7 M blocks, gone. The precondition is exact: every buffer zero_grad cleared 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_grad survives 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_compute re-read a master the optimizer had written microseconds earlier, only to transpose it. adamw_bf16_master_transposed walks the same 64×64 tile transpose_bf16_pairs does, 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, GpuGroupedLinear and GpuExpertFfn grow an adamw_step that owns this, so the optimizer can no longer forget to sync a compute copy; sync_compute stays 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.

baseline front candidate front Δ untouched remainder ratio
A/B, baseline built first 34.587 26.039 −8.548 1.0016
A/B, candidate built first 26.176 34.734 −8.559 0.9991
standalone pair 37.303 27.285 −10.018 1.0035

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:

row d6b99ea branch Δ
optimizer.experts.gate_up.adamw 10.8209 14.8565 +4.0356
optimizer.experts.down.adamw 5.4425 7.3803 +1.9378
optimizer.qkv_proj.adamw 1.6097 2.1990 +0.5893
optimizer.o_proj.adamw 0.6058 0.7979 +0.1921
optimizer.embedding.adamw 0.7460 0.8182 +0.0722
optimizer.lm_head.adamw 0.6764 0.7076 +0.0312
optimizer.lm_head.sync_w_t 0.2235 0.2284 +0.0049
norms + router (4 rows) 0.3086 0.2974 −0.0112
optimizer.experts.sync_compute 5.1576 — −5.1576
optimizer.qkv_proj.sync_compute 0.5723 — −0.5723
optimizer.o_proj.sync_compute 0.2483 — −0.2483
zero_grad.* (10 rows) 10.8916 — −10.8916
total 37.303 27.285 −10.018

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 on GpuBf16Head::sync_compute records 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_transposed in gpu/tensor-gpu/src/main.rs runs the flat and the fused-transposed write-back over the same 128×128 input and assert_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 one adamw_element, and round_master's stochastic stream is keyed on the global element index, which the tile reproduces exactly. check_adamw_master additionally 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, and checkpoint v5 resumes bit-identically.
  • --kernel ops: green.
  • --kernel tensor-gpu: green, with the new bit-identity parity gate above.
  • Full-step A/B in both build orders plus a standalone pair, table above.
  • No KernelBudget entries exist for the tensor/model kernels (enforce_kernel_budgets is wired up only in gemm and flash-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/restore snapshots 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. fill still 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.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

100-step training smoke, --shard /data/wiki-train-00000.tok --steps 100, both refs:

step d6b99ea branch Δ
1 11.106920 11.106920 0.00%
10 9.009066 9.007109 −0.02%
20 8.435464 8.506166 +0.84%
30 8.216640 8.206599 −0.12%
40 8.250344 8.203398 −0.57%
50 8.135303 8.131739 −0.04%
60 8.286264 8.290232 +0.05%
70 7.856686 7.862406 +0.07%
80 7.769155 7.791328 +0.29%
90 7.787453 7.754160 −0.43%
100 8.072871 8.027816 −0.56%

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.

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