Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Vineeth Sai Varikuntla677a988dd5a9
diff --git a/tests/utils/test_q_galore.py b/tests/utils/test_q_galore.py
index ccf4910bf2c..cc583a52fd2 100644
--- a/tests/utils/test_q_galore.py
+++ b/tests/utils/test_q_galore.py
@@ -54,6 +54,34 @@ _adamw_mod = _load_module(
)
make_q_galore_param_groups = _adamw_mod.make_q_galore_param_groups
+
+_BNB_OPTIMIZER_BACKEND = {}
+
+
+def requires_bnb_optimizer(device):
+ """Skip unless bitsandbytes can really run an optimizer step on ``device``.
+
+ Not `supported_torch_devices`: it lists "cpu" from 0.46.0 but the CPU kernels landed in
+ 0.50.0, so gating on it fails these tests on 0.47.x/0.49.x, which pyproject allows.
+ The probe drives bitsandbytes' own AdamW32bit, so a real regression in the code under
+ test still fails rather than turning into a skip.
+ """
+ available = _BNB_OPTIMIZER_BACKEND.get(device)
+ if available is None:
+ try:
+ import bitsandbytes
+
+ probe = nn.Parameter(torch.ones(2, device = device))
+ probe.grad = torch.zeros(2, device = device)
+ bitsandbytes.optim.AdamW32bit([probe], lr = 0.0).step()
+ available = True
+ except Exception:
+ available = False
+ _BNB_OPTIMIZER_BACKEND[device] = available
+ if not available:
+ pytest.skip(f"This bitsandbytes version cannot run an optimizer step on {device}")
+
+
# ======================================================================
# Projector tests
# ======================================================================
@@ -316,6 +344,24 @@ class TestParamGroupHelper:
# ======================================================================
+def test_optimizer_bias_correction_matches_adamw():
+ pytest.importorskip("bitsandbytes")
+ device = "cuda" if torch.cuda.is_available() else "cpu"
+ requires_bnb_optimizer(device)
+
+ param = nn.Parameter(torch.ones(2, device = device))
+ reference = nn.Parameter(param.detach().clone())
+ optimizer = _adamw_mod.QGaLoreAdamW8bit([param], lr = 0.1, weight_decay = 0.0)
+ expected_optimizer = torch.optim.AdamW([reference], lr = 0.1, weight_decay = 0.0)
+ # Non-projected parameters use AdamW; small tensors use full-precision states.
+ for values in ([0.1, 0.2], [0.4, -0.2], [-0.05, 0.3]):
+ param.grad = torch.tensor(values, device = device)
+ reference.grad = param.grad.clone()
+ optimizer.step()
+ expected_optimizer.step()
+ torch.testing.assert_close(param, reference)
+
+
class TestQGaLoreIntegration:
"""Integration tests that work without bitsandbytes on CPU."""
diff --git a/unsloth/optimizers/q_galore_adamw.py b/unsloth/optimizers/q_galore_adamw.py
index acb968886b6..36aac62c1d5 100644
--- a/unsloth/optimizers/q_galore_adamw.py
+++ b/unsloth/optimizers/q_galore_adamw.py
@@ -201,8 +201,6 @@ class QGaLoreAdamW8bit(Optimizer2State):
# dequantizes before the next forward pass.
p.data = torch.empty(1, dtype = p.data.dtype, device = p.data.device)
- state["step"] += 1
-
if torch.cuda.is_available():
torch.cuda.synchronize()