Skip to content

[Common] Compute the capturable FusedAdam bias corrections in FP32 - #3957

Open
maxwbuckley wants to merge 2 commits into
NVIDIA:mainfrom
maxwbuckley:fp32-bias-correction
Open

maxwbuckley wants to merge 2 commits into
NVIDIA:mainfrom
maxwbuckley:fp32-bias-correction

Conversation

@maxwbuckley

@maxwbuckley maxwbuckley commented Nov 27, 2026 •

Copy link
Copy Markdown

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 from adam.cu and 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 FP64 pow while 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

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring
  • Performance

Changes

  • common/multi_tensor/adam.cu: a new bias_correction_factor(beta, step) helper returns -expm1f(step * logf(beta)). AdamCapturableFunctor and AdamCapturableMasterFunctor use it in place of 1 - 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:
    • kernel: times one step of regular, capturable, and capturable-with-master-weights FusedAdam. The capturable steps are captured in a CUDA graph and replayed, as with Megatron's optimizer_cuda_graph=true.
    • e2e: times a full training step of a te.TransformerLayer stack (BF16 params, FP32 master weights) with the graph-captured optimizer step.

Why not 1 - powf(beta, step)

Plain powf is fast, but 1 - beta^step cancels when beta is close to 1, so it loses accuracy for the usual beta2 values. -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:

beta 1 - powf(beta, t) max error -expm1f(t * logf(beta)) max error
0.9 0.9 ulp 0.8 ulp
0.95 1.3 ulp 0.9 ulp
0.99 7.1 ulp 1.0 ulp
0.999 55.7 ulp 1.0 ulp
0.9999 827.6 ulp 1.8 ulp

The 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_close and 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)

Shape Variant main this PR Change
32 × 16M FP32 regular (control) 10,564 10,563 0.0%
capturable 10,737 10,631 −1.0%
capturable + master 11,253 11,153 −0.9%
512 × 64K FP32 capturable 682 631 −7.4%
capturable + master 790 721 −8.8%
2 × 1M FP32 capturable 36.9 32.8 −11.1%
capturable + master 39.4 35.7 −9.5%

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.TransformerLayer stack, BF16 + FP32 master weights, in-place zero_grad + forward + backward + graph-captured optimizer step)

Model Optimizer step: main Optimizer step: this PR Training step: main Training step: this PR
8 layers, hidden 2048, seq 1024, batch 4 (403M params) 8,435 8,355 (−0.9%) 65,148 65,199 (within noise)
4 layers, hidden 4096, seq 512, batch 1 (806M params) 16,886 16,736 (−0.9%) 34,546 34,408 (−0.4%)

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.py would be welcome.

Code size: cuobjdump -sass of adam.cu.o for 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

How to reproduce (supporting context, skippable)
python benchmarks/benchmark_fused_adam.py                                   # 32 x 16M
python benchmarks/benchmark_fused_adam.py --n-tensors 512 --numel 65536 --iters 100
python benchmarks/benchmark_fused_adam.py --n-tensors 2 --numel 1048576 --iters 200
python benchmarks/benchmark_fused_adam.py --mode e2e --layers 8 --hidden 2048 --seq 1024 --batch 4
python benchmarks/benchmark_fused_adam.py --mode e2e --layers 4 --hidden 4096 --seq 512 --batch 1

Accuracy check: for each beta, compare -torch.expm1(t * torch.log(beta_f32)) and 1 - torch.pow(beta_f32, t) against (1 - torch.pow(beta_f32.double(), t.double())).float(), with t = 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:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

🤖 Generated with Claude Code

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>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 27, 2026
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; the previous gradient-clearing issue is fixed.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous gradient-clearing issue is fixed.

Summary

The PR computes capturable FusedAdam bias corrections in FP32 and adds kernel and training benchmarks.

  • Capturable FusedAdam kernels calculate bias corrections in FP32.
  • A new benchmark compares FusedAdam modes and graph-captured training steps.

Reviews (2) · Last reviewed commit: "Zero gradients in place in the e2e bench..." · Reviewed by Greptile

Comment thread benchmarks/benchmark_fused_adam.py Resolved
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 lxcxjxhx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 is 1e-1 scale there, so 2 ulp is ~2e-8 relative), or
  • the absolute bound, "the correction differs by at most X in absolute terms, and the relative difference stays below 1e-6", which is what actually matters since the correction multiplies m / (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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants