Skip to content

Fix Q-GaLore quantization of single-sign groups - #10841

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/qgalore-single-sign-quantization-20260912
Sep 14, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/qgalore-single-sign-quantization-20260912

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Q-GaLore can erase constant projection vectors and clip single-sign groups: INT4 quantization turns [1, 2] into [1, 1].

Cause: The scale excludes zero, but the zero point is clamped to the unsigned integer range.
Fix: Include zero in both quantizers' ranges.
Test: python -m pytest --confcutdir=tests/utils tests/utils/test_q_galore.py -q: 10 failed / 24 passed before, 34 passed after. Covers INT4/INT8, deterministic/stochastic rounding, grouped quantization, and a rank-one gradient round trip.

A dedicated CPU job now 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-single-sign-quantization-20260912 branch 2 times, most recently from 1cf562c to f71616c Compare September 13, 2026 03:20
@vineethsaivs
vineethsaivs force-pushed the fix/qgalore-single-sign-quantization-20260912 branch from f71616c to c5b8b09 Compare September 13, 2026 03:31
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits unsloth/optimizers/q_galore_projector.py where a group with no sign change gets a zero point clamped outside its own range and collapses to a single value, and including zero in both quantizers is the right fix. Will get this reviewed.

… allow

Two changes to the new job.

Run it across three bitsandbytes versions instead of one. pyproject.toml allows
`bitsandbytes>=0.45.5,!=0.46.0,!=0.48.0` with no upper bound, and
`Optimizer2State.__init__` is not signature-stable across that range: 0.50.0
removed `percentile_clipping` and `block_wise`, so positions 10 and 11 became
`max_unorm` and `skip_zeros`. A job pinned to a single version tests one point of
that range. The matrix picks the three that differ:

  0.45.5  the floor, and the version 8 pyproject extras hard-pin
  0.49.2  last release with percentile_clipping / block_wise
  0.50.2  first release without them, and the first with CPU optimizer kernels

Drop `--confcutdir=tests/utils`. tests/conftest.py is the GPU-free harness, and
this file loads unsloth/optimizers/*.py through importlib specs rather than
importing the unsloth package, so it neither needs the spoof nor is disturbed by
it. Checked both ways: 42 passed with the repo conftest, 42 passed without. The
flag made this the only job in the repo running under a different conftest
boundary, for no effect.
@danielhanchen

Copy link
Copy Markdown
Member

Reviewed and verified on hardware. The bug is real, the fix is correct, and I measured no regression anywhere. I pushed one change to the branch, to the workflow only.

What happens before and after

The quantizer is AWQ's pseudo_quantize_tensor: the scale comes from the data span (max - min), but the zero point is then clamped into the code range [0, 2**n_bit - 1]. For a group that never crosses zero those two disagree and the clamp wins silently.

GaLoreProjector defaults to group_size = -1, so the whole projection matrix is one group. A rank-1 SVD of a near-constant gradient gives a constant singular vector with all entries the same sign, max == min, the scale hits the 1e-5 floor, and the stored projection dequantizes to all zeros. The optimizer then projects onto nothing.

End to end, project_back(project(grad)) relative error, INT4 and INT8, on a B200:

case main this PR
rank 1, constant gradient, n_bit 4 1.0 1.19e-07
rank 1, constant gradient, n_bit 8 1.0 1.19e-07
rank 2, constant gradient, n_bit 4 0.998967 0.044
rank 2, constant gradient, n_bit 8 0.999997 0.00262762
rank 1, random gradient, n_bit 4 0.649629 0.638375
rank 2, random gradient, n_bit 8 0.355742 0.3342

A relative error of 1.0 means the gradient is entirely destroyed. Note the random rows: the ordinary path gets slightly better, not worse.

Does it break anything

No. I swept 132 round-trip cases, the cartesian product of 11 input shapes (all positive near zero, all positive far from zero, all negative, constant positive, constant negative, all zero, mixed sign, mixed asymmetric, tiny magnitudes, large magnitudes, single value) x n_bit in {4, 8} x q_group_size in {-1, 4} x dtype in {float32, float16, bfloat16}, against a float64 reference, on CPU and CUDA:

[cpu]  132 cases: better=84 worse=0 unchanged=48
[cuda] 132 cases: better=84 worse=0 unchanged=48

Zero cases get worse on either device. Stochastic rounding stays unbiased over 4000 seeded draws.

On the alternative fix

The other way to make the scale and the zero point agree is to stop clamping the zero point. zeros is returned as a float tensor and never cast to uint8, so a negative zero point is representable, and it would keep full resolution for single-sign groups instead of costing a factor of b/(b-a).

I checked, and it is the worse option. scales and zeros inherit the dtype of the projection matrix, which _get_orthogonal_matrix casts back to the parameter dtype. For a constant group the unclamped zero point is about -35000, where bf16 spacing is ~256 and fp16 ~32, so (code - zeros) * scales would be garbage in exactly the degenerate case this is meant to fix. Clamping the range keeps zeros inside [0, 15] and is safe in every dtype. You picked the right one.

The cost is real but bounded and it is strictly an improvement over what is there. A narrow group far from zero, [100.0, 100.1] at INT4, goes from max error 100 (dequantizes to roughly zero) to max error 0.0999985 (right magnitude, one code). Worth knowing, not worth blocking.

What I pushed

Two changes to .github/workflows/q-galore-tests.yml, no change to your source or tests.

Dropped --confcutdir=tests/utils. tests/conftest.py is the GPU-free harness, and this file loads unsloth/optimizers/*.py through importlib specs rather than importing the unsloth package, so it neither needs the spoof nor is disturbed by it. Checked both ways: 42 passed with the repo conftest, 42 passed without. The flag made this the only job in the repo running under a different conftest boundary, for no effect.

Added a bitsandbytes version axis, 0.45.5 / 0.49.2 / 0.50.2. pyproject.toml allows bitsandbytes>=0.45.5,!=0.46.0,!=0.48.0 with no upper bound, and Optimizer2State.__init__ is not signature-stable across that range. A job pinned to one version tests one point of it. This matters for #10842 and #10874, which touch that constructor, and all three of these PRs add this same workflow file.

One merge note: all three add an identical q-galore-tests.yml, so whichever lands first supplies it and the other two merge as no-ops on that file. I confirmed all three still merge together cleanly, and the merged 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.

Approach is right, fix is right. Good catch on a bug that silently zeroed the projection.

@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-14T03:23:06.630743Z 8ebccd4 Manual request
🔒 Security Review ✅ Completed 2026-09-14T03:10:06.875008Z 3865b20 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: 3865b204df

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. More of your lovely PRs please.

Reviewed commit: 8ebccd49eb

ℹ️ 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
@danielhanchen
danielhanchen merged commit 3bdb5a4 into unslothai:main Sep 14, 2026
22 of 25 checks passed
danielhanchen added a commit that referenced this pull request Sep 14, 2026
…their own (#10898)

#10841 added .github/workflows/q-galore-tests.yml to give tests/utils/test_q_galore.py
a home, because `Repo tests (CPU)` passes --ignore=tests/utils and the file therefore
ran in no job at all. A whole workflow is the wrong shape for that. This drops it and
adds the file to the Bucket-A list in consolidated-tests-ci.yml, which exists for
exactly this case: CPU-runnable tests that live inside the --ignore'd directories.

Added in both places that list has to stay in sync, the collect-only sanity step and
the Bucket-A pytest step. That job already installs bitsandbytes>=0.45, so the
optimizer tests resolve the same way they did in the standalone workflow.

The deleted workflow ran pytest with --confcutdir=tests/utils. Nothing needs it:
test_q_galore.py loads unsloth/optimizers/*.py through importlib specs rather than
importing the unsloth package, so the GPU-spoof conftest neither helps nor hinders it.
Checked, 34 passed under the repo conftest, which is how the Bucket-A job runs it.

Also corrected the Bucket-A count in the header comment and the summary echo. Both
said 21; the list held 23 before this change and 24 after.
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