Skip to content

Pass Q-GaLore optimizer options to bitsandbytes by name - #10874

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
vineethsaivs:fix-qgalore-bnb-arguments
Sep 14, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
vineethsaivs:fix-qgalore-bnb-arguments

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Q-GaLore freezes projected parameters using bitsandbytes 0.50.2's FP32 state fallback, despite nonzero gradients.

Cause: Removed positional options now bind percentile_clipping=100 to max_unorm and block_wise=True to skip_zeros. Norm clipping against the zero-filled update buffer erases the update.
Fix: Pass optimizer arguments by name. Forward legacy options only when supported and reject unsupported nondefaults. Add a CPU job for the excluded Q-GaLore suite.
Test: Three CPU update regressions fail before; all 31 tests pass afterward with bitsandbytes 0.50.2, including AdamW comparisons and legacy-constructor forwarding. Changed-file pre-commit and the workflow guard pass.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the positional super().init call in unsloth/optimizers/q_galore_adamw.py still binds percentile_clipping and block_wise by position, which silently changes the update on newer bitsandbytes, and switching to keywords looks like the right fix. Will get this reviewed.

test_default_optimizer_updates_match_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 four parametrisations run, and all
four fail. Both are versions pyproject.toml allows
(`bitsandbytes>=0.45.5,!=0.46.0,!=0.48.0`, no upper bound), and neither is the
0.50.2 this PR's own workflow pins, so CI would not have caught it. Measured on
CPU with torch 2.10.0, before this commit:

  bnb 0.45.5   25 passed, 6 skipped
  bnb 0.47.0   4 failed, 25 passed, 2 skipped
  bnb 0.49.2   4 failed, 25 passed, 2 skipped
  bnb 0.50.2   31 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, and 31 passed on
0.50.2 on both CPU and CUDA.

The two options tests are unaffected: they only construct the optimizer, which
works on every version in the range.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the positional super().init call in unsloth/optimizers/q_galore_adamw.py is what misbinds percentile_clipping and block_wise on newer bitsandbytes, and passing by name is the right fix. Could you note the oldest bitsandbytes version you tested this against so I can check the guard path before review?

@danielhanchen

Copy link
Copy Markdown
Member

This is the most serious of the three. On current bitsandbytes, Q-GaLore on main does not train at all, and this PR is what fixes it. Verified on hardware. I pushed one fix to your tests.

What is actually broken

bitsandbytes 0.50.0 removed percentile_clipping and block_wise from Optimizer2State.__init__ (PR #1871, listed under breaking changes). Positions 10 and 11 became max_unorm and skip_zeros. The positional call on main still has the right arity, so nothing raises. I instrumented the constructor to print what bitsandbytes actually receives:

main, bitsandbytes 0.50.2
  min_8bit_size      = 4096
  max_unorm          = 100    <-- MISBOUND, intended 0.0
  skip_zeros         = True   <-- MISBOUND, intended False

max_unorm > 0 turns on the 32-bit unorm clipping path, where the kernel computes

update_scale = sqrtf(unorm[0]);
if (update_scale > max_unorm * param_norm) update_scale = (max_unorm * param_norm) / update_scale;

and step() sets p.data = torch.zeros_like(grad) before the update for every projected parameter. So param_norm is exactly 0 by construction, the threshold is 0, update_scale is 0, and the parameter receives no update. Ever.

Measured, 5 steps, max absolute change in a GaLore-projected parameter, bitsandbytes 0.50.2 on a B200:

shape main this PR
(32, 32) PROJECTED_PARAM_FROZEN, 0 PROJECTED_PARAM_MOVED, 0.389693
(128, 128) PROJECTED_PARAM_FROZEN, 0 PROJECTED_PARAM_MOVED, 0.247117

Same picture in a real 30 step training loop, where the projected layer is frozen and only the non-projected layer learns:

final loss
main 0.65780
this PR 0.43312

Does it break old installs

No, and I checked rather than assumed. The positional call was correct on every release up to and including 0.49.2. Argument binding after this PR, same probe:

bitsandbytes has percentile_clipping / block_wise main this PR
0.45.5 yes correct correct
0.47.0 yes correct correct
0.49.2 yes correct correct
0.50.2 no misbound correct

The introspection branch does the right thing in both directions: on old bitsandbytes the two options are forwarded by name, on new bitsandbytes a non-default value raises instead of being silently misapplied.

That new ValueError cannot surprise an existing user, either. The only production caller is UnslothTrainer._create_q_galore_optimizer at unsloth/trainer.py:585, which passes lr, weight_decay, betas and eps and never percentile_clipping or block_wise, so it can only take the default path. Nothing else in the repo constructs this optimizer, and studio/ has zero references to galore, so there is no Studio blast radius.

What I pushed

Your test_default_optimizer_updates_match_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, all four parametrisations run, and all four fail. Both are versions pyproject.toml allows, and neither is the 0.50.2 this PR's own 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 25 passed, 6 skipped
0.47.0 4 failed, 25 passed, 2 skipped
0.49.2 4 failed, 25 passed, 2 skipped
0.50.2 31 passed

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. All four versions are green after it, and 31 passed on 0.50.2 on both CPU and CUDA.

I also added a bitsandbytes version axis to q-galore-tests.yml (0.45.5 / 0.49.2 / 0.50.2), which is the change that would have caught both this and the original misbinding. Pinning one version to test a constructor whose signature moves across the supported range is what let this reach main in the first place.

Your two options tests are fine as written and needed no change.

Merge note

#10842 needed the same guard fix, so both branches now add the same requires_bnb_optimizer helper to tests/utils/test_q_galore.py. Whichever merges second hits a one hunk conflict there; keep one copy of the helper and both sets of tests. I did that merge locally with all three PRs 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.

This one should go in first of the three. Good find.

@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:38:20.681466Z 03ed73b Manual request
🔒 Security Review ✅ Completed 2026-09-14T03:11:12.946646Z 9be3842 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 9be3842134

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

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

Reviewed commit: 9be3842134

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. 🚀

Reviewed commit: 1dc51f942f

ℹ️ 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".

# Conflicts:
#	.github/workflows/q-galore-tests.yml
The bitsandbytes version axis that used to live in a workflow of its own now lives
where the repo already keeps this kind of check. tests/version_compat/
test_bitsandbytes_pinned_symbols.py is parametrised over 0.45.5, 0.47.0, 0.49.2 and
main, reads the upstream source directly and needs no install, so it covers the same
ground for free in a job that already runs.

The new case reads Optimizer2State.__init__'s parameter list at each tag and asserts
this repo does not pass into it positionally. 0.50.0 removed percentile_clipping and
block_wise, so positions 10 and 11 became max_unorm and skip_zeros; arity still
matched and nothing raised, which is why the misbinding reached main at all.

It fails against the positional call on main and passes here, so it belongs with this
fix rather than ahead of it.
@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.

The bitsandbytes version axis that workflow provided has not been dropped, it has moved somewhere better. tests/version_compat/test_bitsandbytes_pinned_symbols.py is already parametrised over 0.45.5, 0.47.0, 0.49.2 and main, reads the upstream source directly and needs no install, so it covers the same ground inside a job that already runs. The new case reads Optimizer2State.__init__'s parameter list at each tag and asserts this repo does not pass into it positionally.

It lives here rather than in #10898 because it fails against the positional call still on main and passes against this PR's fix, so it belongs with the fix. Verified both ways: 4 failed on main, 8 passed here.

42 passed locally, plus the version-compat cases.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8779e74722

ℹ️ 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".

)
for name in ("percentile_clipping", "block_wise"):
if name in call:
assert f"{name} = " in call or f'"{name}"' in call, (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Inspect the actual superclass call for keyword forwarding

This assertion is already satisfied by the quoted names in the preceding legacy_kwargs tuple, regardless of how super().__init__ receives those values. If a later edit retains that tuple but passes percentile_clipping or block_wise positionally again, every tag will still pass this version-compat test while the silent Q-GaLore update freeze returns; parse or isolate the superclass call before checking for keyword arguments.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct, and it was a tautology. The slice ran from class QGaLoreAdamW8bit to the next decorator, so it covered the legacy_kwargs table where both names appear as quoted strings. That alone satisfied the "{name}" branch, so the assertion passed no matter how super().init was actually invoked. A regression to a positional call would have kept all four tags green, which is the one thing this test existed to prevent.

Fixed in 2a668df by locating the call with ast instead of grepping the body. It now asserts on the call node itself: everything after "adam" and params must be a keyword, and an option bitsandbytes has dropped at that tag must not be passed at all.

The difference is measurable, and the old version could not show it. Against the positional constructor still on main all four tags now fail; against this PR's constructor all four pass.

pre-commit-ci Bot and others added 3 commits September 14, 2026 04:18
The keyword assertion was tautological. It searched the whole class body, which also
contains the legacy_kwargs table where "percentile_clipping" and "block_wise" appear as
quoted strings, so the check was satisfied by that table alone and passed whether or not
super().__init__ actually used keywords. A regression to a positional call would have
kept every tag green.

Now the call itself is located with ast and asserted on: everything after "adam" and
params must be a keyword, and an option bitsandbytes has dropped at that tag must not be
passed at all. Checked both ways, which the previous version could not do: against the
positional call still on main all four tags fail, and against this PR's constructor all
four pass.
# Conflicts:
#	tests/utils/test_q_galore.py
@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: 03ed73b0e1

ℹ️ 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 9f18247 into unslothai:main Sep 14, 2026
17 of 22 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