Skip to content

Fix CPU LAMB trust ratio: use the bias-corrected update norm - #2172

Open
chaitanya0305 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
chaitanya0305:fix/cpu-lamb-trust-ratio
Open

chaitanya0305 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
chaitanya0305:fix/cpu-lamb-trust-ratio

Conversation

@chaitanya0305

Copy link
Copy Markdown

Summary

On CPU, 32-bit LAMB measures the trust ratio on the wrong vector. It clips updates that the CUDA, Triton (XPU) and default (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||. Here u is the bias-corrected Adam update m_hat / (sqrt(v_hat) + eps). The CUDA precondition kernel (kPreconditionOptimizer32bit2State, ADAM case), the Triton kernel and the default backend all compute the norm on this corrected update.

The CPU kernel (_optimizer_update_32bit_cpu, adam/lamb branch) computed it on the uncorrected quotient instead:

update = state1 / (state2.sqrt() + eps * correction2)

That quotient equals u * correction2 / correction1. On step 1 with betas=(0.9, 0.999), that factor is sqrt(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 of max_unorm * ||p||. That is the normal LAMB regime with the default max_unorm=1.0.

Fix

Compute the bias-corrected update directly and apply it with -lr:

update = (state1 / correction1) / (state2.sqrt() / correction2 + eps)
update_scale = _compute_update_norm_and_scale(update, unorm_vec, max_unorm, param_norm)
p_float.add_(update, alpha=-lr * update_scale)

When no clipping happens, -lr * update is algebraically the same step as the old step_size * state1 / (sqrt(state2) + eps * correction2). So plain Adam on CPU, which shares this branch with max_unorm=0, does not change. Only the norm used for the trust ratio changes.

Repro (before the fix)

import torch
from bitsandbytes.backends.cpu.ops import _optimizer_update_32bit_cpu
import bitsandbytes.backends.default.ops as D

torch.manual_seed(0)
n = 1000
p0, g = torch.randn(n), torch.randn(n) * 0.01
for which in ("cpu", "default"):
    p, s1, s2, unorm = p0.clone(), torch.zeros(n), torch.zeros(n), torch.zeros(1)
    for t in range(1, 11):
        pn = p.norm().item()
        if which == "cpu":
            _optimizer_update_32bit_cpu("lamb", g, p, s1, s2, unorm, 1.0, pn, 0.9, 0.999, 0.0, 0.0, 1e-8, 0.0, t, 0.1, 1.0)
        else:
            unorm.zero_()
            D._optimizer_precondition_32bit(g, p, s1, s2, unorm, 0.9, 0.999, 1e-8, 0.0, t, 0.1, 1.0, 3)
            D._optimizer_update_32bit(g, p, s1, s2, unorm, 1.0, pn, 0.9, 0.999, 0.0, 0.0, 1e-8, 0.0, t, 0.1, 1.0, 3)
    print(which, (p - p0).norm().item())
# cpu 6.22, default 31.62  (step 1 alone: 1.03 vs 3.16)

Tests

I added test_lamb32bit_trust_ratio to tests/test_optim.py. It runs 10 steps of bnb.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 no torch.optim LAMB to compare against.

  • Before: the 2 CPU cases fail (100% of elements mismatch). The 2 MPS cases pass, since MPS uses the default kernel.
  • After: all 4 pass.
  • pytest tests/test_optim.py -k "cpu and (optimizer32bit or lion32bit)": 78 passed, 72 skipped.
  • pre-commit run on 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 == 0 case. This one is about which update norm the CPU kernel measures.

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

No deployments
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