Skip to content

Fix optimizer state offload crash with Q-GaLore weight quantization - #13979

Merged
danielhanchen merged 1 commit into
mainfrom
dh/offload_qgalore_weight_quant
Oct 16, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/offload_qgalore_weight_quant

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

With UNSLOTH_OFFLOAD_OPTIMIZER_STATE=1 and more than one chunk, Q-GaLore with weight_quant=True crashed on the first step:

RuntimeError: attempting to assign a gradient of size '[256, 256]' to a tensor of size '[1]'

The chunked step hides gradients between chunks and restores them afterwards, but Q-GaLore weight quantization swaps p.data for a one element placeholder inside step, so the original gradient can no longer be assigned back.

Change

  • _chunk_safe now returns False when any param group sets weight_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.
  • Nothing changes when the env var is unset, and other optimizers are unaffected.

Tests

  • test_weight_quant_q_galore_steps_unchunked (CPU) checks the gate.
  • test_q_galore_multi_chunk_offload[False|True] (CUDA, bitsandbytes) runs the real QGaLoreAdamW8bit with 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.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 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-10-16T07:24:49.949562Z d02c3a7 Manual request
🔒 Security Review ⚠️ Failed 2026-10-16T07:23:34.679033Z d02c3a7 PR opened
ℹ️ 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. Delightful!

Reviewed commit: d02c3a74a8

ℹ️ 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 ee84654 into main Oct 16, 2026
@danielhanchen
danielhanchen deleted the dh/offload_qgalore_weight_quant branch October 16, 2026 08:33
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant