Skip to content

Optimizer state offload: keep inactive state on the host for unchunked Q-GaLore weight_quant steps - #14025

Merged
danielhanchen merged 2 commits into
mainfrom
dh/qgalore-inactive-state
Oct 16, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/qgalore-inactive-state

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #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.

…aLore steps

Weight-quantized Q-GaLore steps unchunked, and the pre-step hook restored every
parameter's state to the device. Its step still skips parameters without a
gradient, so restore only the active ones, as the single chunk path does.
@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-16T16:28:23.361721Z f2c9905 Manual request
🔒 Security Review ⚠️ Failed 2026-10-16T16:26:44.263632Z f2c9905 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: f2c9905ad1

ℹ️ 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 15e4bc8 into main Oct 16, 2026
@danielhanchen
danielhanchen deleted the dh/qgalore-inactive-state branch October 16, 2026 17:30
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 19, 2026
…-GaLore offload, GRPO truncated support, CP save on transformers 4.x (unslothai#14226)

## Summary

Fixes for four problems found when re-reviewing recently merged PRs.
Each one still reproduced on main, and each has a test that fails on
main and passes here. Default configs are unchanged.

1. **Fused LoRA with dropout or a `lora_B` bias (unslothai#13898).**
- **Graph breaks:** before torch 2.11 the fused LoRA function runs with
Dynamo disabled. unslothai#13898 moved dropout and bias projections from PEFT
onto that path, so compiling them hit a graph break each and `fullgraph
= True` failed. While compiling on torch < 2.11 those projections now
run PEFT again, as they did before unslothai#13898. Plain LoRA, eager fusion and
torch 2.11+ are unchanged.
- **Memory:** on 4-bit weights the dropout and bias path dequantized
into the shared global buffer, so one dense weight stayed resident; PEFT
never keeps one. That path now dequantizes into a temporary, including
in eval, where dropout is a no-op. `matmul_lora` gets a
`use_global_buffer` keyword, default `True`, so every other caller is
unchanged.
2. **Q-GaLore optimizer state offload (unslothai#14025).** If a step pre-hook
registered after the offloader created a gradient, that parameter's
state stayed on CPU and the step crashed with a device mismatch. State
is now restored inside the step, after every pre-hook has run.
3. **GRPO truncated-support scoring (unslothai#13900).** This needs the scored
policy to be the sampler. That is not the case with a reference model
(`beta != 0`), with rollouts reused across optimizer steps, or when vLLM
weight sync is skipped (LoRA or quantized without `fast_inference`). In
those cases the new policy's implied probabilities did not sum to 1,
which made the PPO ratio wrong. Those cases now warn once and score the
full vocabulary; on-policy runs with `beta = 0` keep the feature.
4. **Context-parallel save on transformers 4.x (unslothai#14028).** With
`SHARDED_STATE_DICT` the sharded save wrote no adapter, and with
`FULL_STATE_DICT` it gathered the frozen base weights. The save now
gathers only the adapter and rank 0 writes it. Transformers 5 is
unchanged.

The routing replay part of the same review is in
unslothai/unsloth-zoo#1874.

## Tests

| Test | main | this PR |
|---|---|---|
| `tests/test_fused_lora_dropout_bias_compile_memory.py` | 18 failed |
18 passed |
| `tests/test_fused_lora_dropout_bias.py` (eval keeps the dropout route)
| failed | passed |
| `tests/test_optimizer_state_offload.py` | 2 failed | 38 passed |
| `tests/test_grpo_truncated_support_policy_gate.py` | 4 failed | passed
|
| `tests/test_context_parallel_fsdp2_save_gloo.py` (transformers 4.57.6,
2 gloo ranks) | 2 failed | passed |

unslothai#13898 base vs head, Llama block at 4096 / 11008, 3 arms. "PEFT" is main
with fused dropout and bias turned off, i.e. the routing before unslothai#13898:

| torch | Graph breaks on dropout / bias configs (main / this PR) |
4-bit dropout / bias peak MiB (main / PEFT / this PR) |
|---|---|---|
| 2.10, T4 | 6 / 0, fullgraph fails on main | 293.0 to 294.3 / 209.8 to
211.0 / 207.0 to 208.3 |
| 2.13, B200 | 0 / 0 | 340.6 to 341.1 / 257.5 to 257.7 / 254.6 to 255.1
|
| 2.14, T4 | 0 / 0 | 292.9 to 293.3 / 209.7 to 210.0 / 206.9 to 207.3 |

16-bit and default configs give the same memory and graph break counts
in all three arms. Plain LoRA on 2.10 has 6 graph breaks in every arm;
that is older than unslothai#13898 and is left as is.

## Not covered here

- Torch 2.11 and 2.12 were not run. Items 2 to 4 ran on torch 2.13 only.
- No full-model training or throughput run; the unslothai#13898 memory numbers
are per block.
- No vLLM end-to-end GRPO run, no NCCL multi-GPU save, no `push_to_hub`.
- Off-policy truncated support falls back to full-vocabulary scoring
rather than the sampler's kept token set.
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