Repository navigation
Fix CPU LAMB trust ratio: use the bias-corrected update norm - #2172
Open
chaitanya0305 wants to merge 1 commit into
Open
chaitanya0305 wants to merge 1 commit into
chaitanya0305 wants to merge 1 commit into
Conversation
The CPU 32-bit kernel measured the LAMB trust ratio on state1 / sqrt(state2), which is larger than the bias-corrected Adam update by sqrt(1 - beta2**t) / (1 - beta1**t). The CUDA, Triton and default kernels use the corrected update, so CPU LAMB clipped steps that the other backends leave alone (about 3.2x smaller on the first step with the default betas). Compute m_hat / (sqrt(v_hat) + eps) directly and apply it with -lr. The unclipped step (plain Adam on CPU) is unchanged.
This branch has not been deployed
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.
Summary
On CPU, 32-bit
LAMBmeasures the trust ratio on the wrong vector. It clips updates that the CUDA, Triton (XPU) anddefault(MPS) kernels leave alone, so CPU training takes much smaller steps.With the default betas, the first step on CPU is about 3.2x smaller than on the other backends. The gap carries through early training. In the repro below, after 10 steps the parameter has moved about 5x less on CPU.
Root cause
LAMB scales the step by
max_unorm * ||p|| / ||u||when||u|| > max_unorm * ||p||. Hereuis the bias-corrected Adam updatem_hat / (sqrt(v_hat) + eps). The CUDA precondition kernel (kPreconditionOptimizer32bit2State,ADAMcase), the Triton kernel and thedefaultbackend all compute the norm on this corrected update.The CPU kernel (
_optimizer_update_32bit_cpu,adam/lambbranch) computed it on the uncorrected quotient instead:That quotient equals
u * correction2 / correction1. On step 1 withbetas=(0.9, 0.999), that factor issqrt(0.001) / 0.1 ≈ 3.16. So||u||is overstated, and the clip fires (or fires harder) whenever the true update norm is within about 3x ofmax_unorm * ||p||. That is the normal LAMB regime with the defaultmax_unorm=1.0.Fix
Compute the bias-corrected update directly and apply it with
-lr:When no clipping happens,
-lr * updateis algebraically the same step as the oldstep_size * state1 / (sqrt(state2) + eps * correction2). So plain Adam on CPU, which shares this branch withmax_unorm=0, does not change. Only the norm used for the trust ratio changes.Repro (before the fix)
Tests
I added
test_lamb32bit_trust_ratiototests/test_optim.py. It runs 10 steps ofbnb.optim.LAMB(with and without weight decay) against a small pure-PyTorch reference that follows the CUDA kernel math. It checks both states and the parameter after every step.LAMB was not covered by
test_optimizer32bit, because there is notorch.optimLAMB to compare against.defaultkernel.pytest tests/test_optim.py -k "cpu and (optimizer32bit or lion32bit)": 78 passed, 72 skipped.pre-commit runon the changed files passes.I tested on macOS arm64 (CPU and MPS). I have no CUDA or XPU hardware, but neither path is touched.
This is separate from #2117 (LAMB/LARS freezing zero-norm parameters). That PR is about the
param_norm == 0case. This one is about which update norm the CPU kernel measures.