Repository navigation
Fix optimizer state offload crash with Q-GaLore weight quantization - #13979
Merged
Merged
Conversation
Q-GaLore with weight_quant=True replaces p.data with a one element placeholder inside step, so the chunked offload path crashed restoring the hidden full size gradients. Treat such optimizers as not chunk safe; they still get state offload, stepped over all parameters at once.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
added a commit
that referenced
this pull request
Oct 16, 2026
…13994) ## Summary `Repo tests (CPU, studio)` fails on PRs based on main since #13979 (for example #13976): ``` tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe E assert not ['test_optimizer_state_offload.py:590: torch.cuda.is_available()'] ``` #13979 added a new CUDA case to `tests/test_optimizer_state_offload.py` with the `torch.cuda.is_available()` guard, the same pattern #13982 replaced in that file. The zoo tests' CUDA spoof patches `is_available` to True for the whole process, so on a CPU runner that case un-skips and fails. The guard test caught it first. The product fix in #13979 is fine; only the new guard is wrong. ## Change That case now uses `has_real_cuda()` like the other seven in the file. The file already imports it. ## Testing - `tests/python/test_accelerator_skip_guards.py` + `tests/test_optimizer_state_offload.py` on CPU: 41 passed, 17 skipped. - On main without this change, the guard test fails on line 590 as above.
danielhanchen
added a commit
to Etherll/unsloth
that referenced
this pull request
Oct 16, 2026
…d Q-GaLore weight_quant steps (unslothai#14025) Follow-up to unslothai#13979. With optimizer state offload on and a Q-GaLore group using `weight_quant=True`, the step cannot be split into chunks (`weight_quant` swaps `p.data` inside `step`). The unchunked path then moved every parameter's optimizer state to the GPU before the step, including parameters with no gradient that step. Q-GaLore, like the other known optimizers, never touches those, so the transfer only cost VRAM and copy time. Changes (`unsloth/optimizers/state_offload.py`): - The "this optimizer class only updates parameters with a gradient" check moves out of `_chunk_safe` into `_grad_only_step`. `_chunk_safe` returns the same values as before. - On the unchunked path, with no closure and a known optimizer class (torch.optim, bitsandbytes, transformers, unsloth.optimizers), only parameters with a gradient have their state restored. - A closure or an unknown optimizer class still restores everything. The flag-off path and the chunked path are unchanged. Tests (`tests/test_optimizer_state_offload.py`): - `test_weight_quant_q_galore_restores_only_active_state` (CPU): only the active parameter is restored; a closure or an unknown optimizer class still restores all. - `test_q_galore_weight_quant_inactive_state_stays_on_host` (CUDA, real `QGaLoreAdamW8bit`): three 256x256 parameters with a gradient on only the first. On main all three states are on the GPU during the step; here the other two stay on the CPU. - Both fail on main. The file: 36 passed on GPU. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With
UNSLOTH_OFFLOAD_OPTIMIZER_STATE=1and more than one chunk, Q-GaLore withweight_quant=Truecrashed on the first step:The chunked step hides gradients between chunks and restores them afterwards, but Q-GaLore weight quantization swaps
p.datafor a one element placeholder insidestep, so the original gradient can no longer be assigned back.Change
_chunk_safenow returns False when any param group setsweight_quant. Such optimizers use the existing unchunked route: state is restored, one normal step runs, and state is offloaded again. Offloading between steps still applies.Tests
test_weight_quant_q_galore_steps_unchunked(CPU) checks the gate.test_q_galore_multi_chunk_offload[False|True](CUDA, bitsandbytes) runs the realQGaLoreAdamW8bitwith a 256 KiB chunk budget for two steps and checks INT8 weights, scales and gradients match a run without offload bitwise, with state left on CPU. It failed with the error above before the change.tests/test_optimizer_state_offload.py,tests/test_formatter_fixed_point.py,tests/test_source_read_encoding.py: 64 passed, 2 skipped on GPU.#13870: opt-in fused preference loss, trainable head, short-sequence peak VRAM
Verdict: the regression is real, but it comes from the zoo kernel and zoo PR #1824 already fixes it. With #1824 applied, the leftover short-sequence overhead is a small, bounded trade-off on the unsloth side. That overhead is set by the 256 MiB fp32 chunk budget, does not grow with vocab x hidden, and also exists for frozen heads. I made no change to the unsloth gate and created no branch. I made a throwaway worktree
wt_pm_fused_pref_gate, then removed it and deleted its branch without committing anything.