Skip to content

Advance the Q-GaLore Adam step counter once per update - #10842

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
vineethsaivs:fix/qgalore-adam-step-count-20260912
Sep 14, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
vineethsaivs:fix/qgalore-adam-step-count-20260912

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Q-GaLore uses Adam bias-correction steps 1, 3, 5 instead of 1, 2, 3. Three constant-gradient updates leave a parameter at 0.72876 instead of 0.70000.

Cause: bitsandbytes update_step() increments the counter, then Q-GaLore increments it again. Projection refreshes also run twice as often.
Fix: Let bitsandbytes own the increment.
Test: python -m pytest --confcutdir=tests/utils tests/utils/test_q_galore.py -q: regression fails before; 25 passed after with actual bitsandbytes CPU updates compared against torch.optim.AdamW.

A dedicated CPU job runs this previously excluded file with Python 3.12, torch 2.10.0 and bitsandbytes 0.50.2. Core/Backend failures also occur on unchanged main.

@vineethsaivs
vineethsaivs force-pushed the fix/qgalore-adam-step-count-20260912 branch from bab8bd1 to aa6d51a Compare September 13, 2026 03:21
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the counter in unsloth/optimizers/q_galore_adamw.py is advanced both inside update_step and again at the end of the loop, so bias correction and the projection refresh gap both run off doubled steps. Will get this reviewed.

test_optimizer_bias_correction_matches_adamw gates itself on
`"cpu" in bitsandbytes.supported_torch_devices`. That attribute does not answer
the question. It does not exist before 0.46.0, and from 0.46.0 through 0.49.x it
already lists "cpu" even though the CPU optimizer kernels only landed in 0.50.0.

So on 0.47.x and 0.49.x the guard passes, the test runs, and it fails. Both are
versions pyproject.toml allows (`bitsandbytes>=0.45.5,!=0.46.0,!=0.48.0`, no
upper bound). Measured on CPU with torch 2.10.0, before this commit:

  bnb 0.45.5   24 passed, 1 skipped
  bnb 0.47.0   1 failed, 24 passed
  bnb 0.49.2   1 failed, 24 passed
  bnb 0.50.2   25 passed

The failure is the environment, not the code under test:

  0.45.5  NameError: name 'str2optimizer32bit' is not defined
  0.47.0  AttributeError: 'NoneType' object has no attribute 'shape'
  0.49.2  AttributeError: 'NoneType' object has no attribute 'shape'

requires_bnb_optimizer() runs one optimizer step and finds out instead. It drives
bitsandbytes' own AdamW32bit rather than QGaLoreAdamW8bit, so a genuine
regression in the code under test still fails the test rather than quietly
becoming a skip. After this commit all four versions are green, with the
optimizer case skipped on the three that cannot run it, and 25 passed on 0.50.2
on both CPU and CUDA.
@danielhanchen

Copy link
Copy Markdown
Member

Verified. The double increment is real, the removal is the right fix, and the effect is larger than the description suggests. I pushed one fix to your test.

Before and after

Optimizer2State.update_step opens with state["step"] += 1 in every bitsandbytes release from 0.43.x through 0.50.x, and step() here calls self.update_step(...) for every parameter that has a gradient. So the extra state["step"] += 1 at the end of the loop counted every update twice. Measured, five opt.step() calls:

main this PR
state["step"] after 5 steps, non-projected 10 5
state["step"] after 5 steps, projected 10 5

That feeds two things: Adam bias correction, and the update_proj_gap schedule via projector.project(p.grad, state["step"]), so projections were also refreshing at twice the intended rate.

The bias correction damage, against torch.optim.AdamW on the same gradients, 6 steps, max absolute divergence:

main this PR
weight_decay 0.0 5.41e-02 1.49e-05
weight_decay 0.01 5.45e-02 5.59e-04
gradients containing exact zeros 2.88e-02 9.00e-06

5.4e-02 at lr 0.1 is about half a step of error. After the fix it sits at 1.5e-05, which is 8-bit state quantization noise.

Does it break anything

No. A 30 step training loop still converges, with and without projection quantization, losses finite throughout. I ran the full file 30 times on a B200 to check for order dependence: 30 out of 30 green, no flakiness.

What I pushed

Your test_optimizer_bias_correction_matches_adamw gates itself on "cpu" in bitsandbytes.supported_torch_devices. That attribute does not answer the question. It does not exist before 0.46.0, and from 0.46.0 through 0.49.x it already lists "cpu" even though the CPU optimizer kernels only landed in 0.50.0. So on 0.47.x and 0.49.x the guard passes, the test runs, and it fails. Both are versions pyproject.toml allows, and neither is the 0.50.2 this PR's workflow pins, so CI would not have caught it.

Measured on CPU with torch 2.10.0, before my commit:

bitsandbytes result
0.45.5 24 passed, 1 skipped
0.47.0 1 failed, 24 passed
0.49.2 1 failed, 24 passed
0.50.2 25 passed

The failure is the environment, not your code:

0.45.5  NameError: name 'str2optimizer32bit' is not defined
0.47.0  AttributeError: 'NoneType' object has no attribute 'shape'
0.49.2  AttributeError: 'NoneType' object has no attribute 'shape'

I replaced the guard with requires_bnb_optimizer(), which runs one optimizer step and finds out. It drives bitsandbytes' own AdamW32bit rather than QGaLoreAdamW8bit, so a genuine regression in the code under test still fails the test rather than quietly becoming a skip. All four versions are green after it, and 25 passed on 0.50.2 on both CPU and CUDA.

I also added a bitsandbytes version axis to q-galore-tests.yml, which is what would have caught this.

Merge note

#10874 needed the same guard fix, so both branches now add the same requires_bnb_optimizer helper to this file. Whichever merges second hits a one hunk conflict in tests/utils/test_q_galore.py; the resolution is to keep one copy of the helper and both sets of tests. I did that merge locally and the result is 42 passed on CUDA and on CPU with bitsandbytes 0.50.2, 35 passed 7 skipped on 0.45.5, 0.47.0 and 0.49.2.

Worth landing this together with #10874. They are independent, but #10874 is the one that makes projected parameters move at all on current bitsandbytes, and the accuracy claim here is only fully visible once both are in.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits q_galore_adamw.py where state["step"] is advanced again after update_step has already done it, so bias correction and the projection refresh both run on doubled steps. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-14T04:04:12.236823Z b561451 Manual request
🔒 Security Review ✅ Completed 2026-09-14T03:10:56.822676Z 1705fdf Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 1705fdf4a7

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 1705fdf4a7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
# Conflicts:
#	.github/workflows/q-galore-tests.yml
@danielhanchen

Copy link
Copy Markdown
Member

Rebased on main now that #10841 has landed, and dropped the workflow from this PR. The standalone q-galore-tests.yml is being removed in #10898, which folds tests/utils/test_q_galore.py into the Bucket-A list in consolidated-tests-ci.yml instead. That is the job that already exists for CPU-runnable tests sitting inside the --ignored directories.

This PR no longer touches CI at all, so it is now just the step-counter fix and its test. That also means merge order with #10898 does not matter: only one PR owns the workflow removal.

35 passed locally.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: b56145135e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 677a988 into unslothai:main Sep 14, 2026
19 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants