Repository navigation
[Common] Compute the capturable FusedAdam bias corrections in FP32 - #3957
maxwbuckley wants to merge 2 commits into
Conversation
The capturable Adam kernels compute 1 - beta^step on the device, because step lives in device memory. pow(float, int) promotes to double, so each block's thread 0 ran an FP64 pow while the rest of the block waited at the __syncthreads(). FP64 is slow on GPUs with reduced FP64 throughput. Compute it in FP32 as -expm1f(step * logf(beta)) instead. Plain 1 - powf(beta, step) is not accurate enough here: for beta close to 1 the subtraction cancels (up to ~830 ulp off the FP64 result for beta=0.9999). The expm1 form stays within 2 ulp of the FP64 result for beta in [0.9, 0.9999] and step up to 1e6. This removes all FP64 instructions from adam.cu (7992 SASS instructions over the 72 capturable kernel instantiations for sm_120). Add benchmarks/benchmark_fused_adam.py, which times the regular and capturable steps (graph-captured, as with Megatron's optimizer_cuda_graph) and a small TransformerLayer training step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Max Buckley <maxwbuckley@gmail.com>
[Medium impact] The PR appears safe to merge; the previous gradient-clearing issue is fixed. SummaryThe PR computes capturable FusedAdam bias corrections in FP32 and adds kernel and training benchmarks.
Reviews (2) · Last reviewed commit: "Zero gradients in place in the e2e bench..." · Reviewed by Greptile |
Each training step accumulated into the previous step's gradients. Zero them in place so the graph-captured optimizer step keeps reading the same gradient addresses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Max Buckley <maxwbuckley@gmail.com>
lxcxjxhx
left a comment
There was a problem hiding this comment.
Checked the numeric claim, since "within 2 ulp" is the only thing standing between this and a silent accuracy regression.
The rewrite is sound, and the 2-ulp bound holds in the regime Adam actually uses — but not in the whole space. Using 1 - beta**step as the reference (what adam.cu:397 computes today) and -expm1f(step * logf(beta)) as the replacement:
| beta | step | 1 - beta**step |
-expm1f(...) |
ulp |
|---|---|---|---|---|
| 0.9 | 1 | 0.099999999999999978 | 0.100000024 | 3 |
| 0.9 | 2 | 0.18999999999999995 | 0.190000057 | 4 |
| 0.95 | 1 | 0.050000000000000044 | 0.0500000119 | 3 |
| 0.98 | 1 | 0.020000000000000018 | 0.0199999791 | 11 |
| 0.999 | 1 | 0.0010000000000000009 | 0.000999987009 | 112 |
| 0.999999 | 1 | 1.0000000000287557e-06 | 1.01327896e-06 | 117000 |
So for the standard beta1 = 0.9 / beta2 = 0.999 schedule the correction moves by 3 and 112 ulp respectively at step = 1, and the error grows as 1 - beta**step shrinks — because that is exactly where 1 - exp(y) cancels in FP32 and expm1 is supposed to be the better formulation:
beta=0.999 step=1 old=0.0010000000000000009 new=0.000999987009 ulp=112
beta=0.999 step=5 old=0.004990009995000988 new=0.00498994533 ulp=139
That is not an argument against the change — -expm1f is the right function and 112 ulp of a 1e-3 quantity is ~1e-9 absolute, far below any optimizer's sensitivity. It is an argument for the PR body saying which bound it means: as written ("within 2 ulp of the FP64 result") a reader will check beta=0.9, step=1 and get 3, or beta2=0.999 and get 112, and conclude the claim is wrong.
Concretely I would state it as one of:
- "within 2 ulp for
beta <= 0.9" (true; the bias correction is1e-1scale there, so 2 ulp is ~2e-8 relative), or - the absolute bound, "the correction differs by at most
Xin absolute terms, and the relative difference stays below 1e-6", which is what actually matters since the correction multipliesm / (1 - beta**step).
Where I may be measuring something slightly different from you: expm1f and logf are libdevice/host implementations and I evaluated the identity with numpy's float32 log/expm1, so the last digit of every row above is my library, not yours. The shape of the result — error grows as 1 - beta**step → 0 — is a property of the identity, not of the implementation.
What I did not do: run the kernel or the new benchmark (no GB300 here, and the file is CUDA). I also did not check the __shared__/broadcast structure of the change, only the arithmetic it replaces.
One request: the benchmark is +185 lines and the kernel change is +13/−4. If the performance number in the description (1–11%) comes from that benchmark, a line in the body saying which --variant and --n-tensors produced it would let a reviewer reproduce the figure rather than take it on trust.
Description
TL;DR: The capturable FusedAdam kernels still run an FP64
pow()for the bias corrections, now once per block since #3872. Computing it in FP32 as-expm1f(step * logf(beta))removes every FP64 instruction fromadam.cuand makes the capturable step 1–11% faster. The values stay within 2 ulp of the FP64 result.The capturable step was about 1.9x slower than the regular step on GB300 (#3777). #3782 and #3872 closed most of that gap. The remaining difference in the bias-correction code is that
pow(float beta, int step)promotes to double. Thread 0 of every block therefore runs an FP64powwhile the other threads wait at the__syncthreads(). On GPUs with reduced FP64 throughput (GB300, and consumer parts such as the RTX 5090), that wait shows up in the step time.Type of change
Changes
common/multi_tensor/adam.cu: a newbias_correction_factor(beta, step)helper returns-expm1f(step * logf(beta)).AdamCapturableFunctorandAdamCapturableMasterFunctoruse it in place of1 - pow(beta, *step). The non-capturable path is unchanged: it computes the corrections on the host.benchmarks/benchmark_fused_adam.py(new) has two modes:optimizer_cuda_graph=true.te.TransformerLayerstack (BF16 params, FP32 master weights) with the graph-captured optimizer step.Why not
1 - powf(beta, step)Plain
powfis fast, but1 - beta^stepcancels when beta is close to 1, so it loses accuracy for the usualbeta2values.-expm1(step * log(beta))avoids the subtraction. The table compares each form with the current FP64 result, cast to FP32, for every step from 1 to 1e6:1 - powf(beta, t)max error-expm1f(t * logf(beta))max errorThe results are not bit-identical to the current kernel. In those runs 98.7–100% of steps give exactly the same value, and the rest are within the bounds above. The regular (non-capturable) path still computes the corrections in double on the host, so capturable and regular can now differ by a few ulp in the bias correction. The existing tests compare them with
assert_closeand pass.Performance
Setup: RTX 5090 (sm_120), CUDA 13.2, PyTorch 2.15, built for
NVTE_CUDA_ARCHS=120.Method: base
main(37be77c) and this branch were run interleaved, two rounds each. Values are medians, in µs per step. The regular kernel is untouched and serves as a control.Kernel mode (
python benchmarks/benchmark_fused_adam.py --n-tensors N --numel M)The gain is largest where there are many blocks per byte, i.e. many small tensors such as the biases and norm weights in real models. On the large shape the change removes about 60% of the remaining capturable-vs-regular gap (173 µs → 68 µs).
End to end (
--mode e2e:te.TransformerLayerstack, BF16 + FP32 master weights, in-placezero_grad+ forward + backward + graph-captured optimizer step)The end-to-end effect is proportional to the optimizer's share of the step. The share is large here because each run uses a single GPU with small batches; with data-parallel sharding of optimizer state it is smaller. I haven't measured on GB300. Its FP64 throughput is lower relative to FP32, so I would expect the per-block FP64 wait to cost at least as much there. Numbers from a GB300 run with
benchmarks/benchmark_fused_adam.pywould be welcome.Code size:
cuobjdump -sassofadam.cu.ofor sm_120 drops from 153,456 to 110,624 instructions. All 7,992 FP64 instructions (DFMA, DADD, DSETP, DMUL, F2F.F64) are gone; they were spread across the 72 capturable kernel instantiations.Testing
pytest tests/pytorch/test_fused_optimizer.py: 42 passed, 3 skipped. This includestest_capturable_large_parameter_multi_stepfrom [Common] Compute the capturable FusedAdam bias corrections once per block #3872.cpplinton the touched files.How to reproduce (supporting context, skippable)
Accuracy check: for each beta, compare
-torch.expm1(t * torch.log(beta_f32))and1 - torch.pow(beta_f32, t)against(1 - torch.pow(beta_f32.double(), t.double())).float(), witht = arange(1, 1e6 + 1)on the GPU.In the kernel benchmark, the regular variant is slower than capturable on the 512-tensor shape. That is host launch overhead in the un-graphed regular step, not kernel time.
Checklist:
🤖 Generated with Claude Code