Skip to content

Review follow-ups: fused LoRA dropout/bias under compile and 4-bit, Q-GaLore offload, GRPO truncated support, CP save on transformers 4.x - #14226

Merged
danielhanchen merged 9 commits into
mainfrom
dh/fu-rev3
Oct 19, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
dh/fu-rev3

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 (Fused LoRA kernels: support lora_dropout and lora_bias #13898).
    • Graph breaks: before torch 2.11 the fused LoRA function runs with Dynamo disabled. Fused LoRA kernels: support lora_dropout and lora_bias #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 Fused LoRA kernels: support lora_dropout and lora_bias #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 (Optimizer state offload: keep inactive state on the host for unchunked Q-GaLore weight_quant steps #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 (GRPO: score old and reference logprobs over the truncated sampling support when opted in #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 (Context parallel: gather the FSDP2 state dict in save_model on transformers 4.x #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

#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 #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 #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 Fused LoRA kernels: support lora_dropout and lora_bias #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.

…o global dequant scratch

Before torch 2.11 the fused LoRA Functions run behind torch._dynamo.disable, so routing
lora_dropout > 0 or lora_B bias projections into them while compiling replaced a PEFT
forward Dynamo could trace with a graph break (fullgraph=True raised). Those
configurations now stay on PEFT while compiling there; plain LoRA routing and eager
fusion are unchanged.

The dropout/bias path (forward, and the MLP down projection backward) also dequantized
4-bit weights into the per-device global scratch, keeping one dense weight resident that
PEFT never held. It now dequantizes into a fresh tensor (matmul_lora gains
use_global_buffer, default True, so the plain path keeps the scratch).
Unchunked weight_quant Q-GaLore selected the parameters to restore inside the
offloader's step pre hook, before step pre hooks registered later had run. A
later hook that created a missing gradient left that parameter's projector and
moments on the host, and the step then crashed on a device mismatch. Select the
active parameters from the step's closure instead, after every pre hook, as the
chunked path already does.
…y is not the sampler

UNSLOTH_GRPO_TRUNCATED_SUPPORT=1 rebuilds each policy's own top_k / top_p / min_p kept set and always
keeps the scored token. With a reference model (beta != 0) or old logps from rollouts reused across
optimizer steps, those sets differ from the sampler's, so the implied probabilities do not sum to 1
and the PPO ratio / KL inputs are wrong. Only bind the kept set while every scored policy is the
rollout policy; otherwise warn once and score the full vocabulary, which the zoo loss then follows.
… save_model on transformers 4.x

Clearing the installed parallelism config sent transformers 4.x save_model to its FSDP branch,
which writes nothing for SHARDED_STATE_DICT and otherwise gathers the whole model, frozen base
weights included, through Accelerator.get_state_dict. Keep the config and gather the way
Unsloth's FSDP2 save does: trainable plus still-saved frozen parameters for an adapter, rank 0
writes, the other ranks skip _save. transformers 5 keeps the previous path.
…; truncated support refuses an unsynced vLLM sampler
@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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 ⚠️ Failed 2026-10-19T17:27:02.824910Z 9432b89 New commits
🔒 Security Review ⚠️ Failed 2026-10-19T08:59:14.877670Z 7af734b 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7af734b37b

ℹ️ 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".

Comment on lines +2616 to +2617
_support_mismatch = _unsloth_grpo_support_mismatch(self) if _support_kwargs else []
if _support_mismatch:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Gate truncated support before the efficient early return

When UNSLOTH_GRPO_TRUNCATED_SUPPORT=1 is used with beta=0, num_iterations=1, and an unsynced vLLM sampler (for example LoRA or quantized training without fast_inference), this check is never reached: the current-policy call passes compute_efficient=True and returns at line 2532, while there is no reference/old-logprob call to enter this block. Consequently _unsloth_truncated_support_refused remains unset and the accumulated-loss path can continue truncated-support scoring after the policy diverges from the sampler, producing the unnormalized PPO ratios this change intends to prevent. Apply the mismatch gate on the efficient/current-policy path as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7a0f56d, with the zoo side in unslothai/unsloth-zoo#1874 (8dc935801). The compute_efficient branch now calls _unsloth_grpo_refuse_unscored_support before returning. It runs the same mismatch check (an unsynced vLLM sampler, beta != 0, reused rollouts) and marks the trainer refused. The zoo loss used to drop the kept set only when old / ref logps were present, so it now also honours that refusal and scores the full vocabulary. Tests: the unsloth helper test covers the unsynced sampler, a synced sampler and the opt-in being off; the zoo test checks that a refused trainer scores exactly the full-vocabulary logprobs.

@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: 7a0f56d259

ℹ️ 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".

# Conflicts:
#	tests/test_optimizer_state_offload.py
danielhanchen added a commit to danielhanchen/unsloth-zoo-staging that referenced this pull request Oct 19, 2026
…rs unsloth's refusal (unslothai#1874)

## Summary

Two routing replay (#13916) problems that still reproduce on main:

1. **`probs_renorm` 0 / 0.** When every forced expert's probability
underflows to 0, renormalising gave NaN weights and NaN router
gradients. Those rows now keep their natural routing and weights.
2. **Recompiles on every compiled call.** The diagnostic counters
(`_STATE.stats[...] += 1`) in the router hook, the decoder pre-hook and
replay bookkeeping became Dynamo guards, so a compiled router or decoder
recompiled on every call. They are now skipped while compiling. They are
only used for `routing_replay_stats()`.

3. **GRPO truncated support with no old / ref logps.** With
`UNSLOTH_GRPO_TRUNCATED_SUPPORT=1`, `beta = 0` and the efficient path,
no old or reference logprobs are scored, so the loss used the current
policy's own kept set even when unsloth had found the scored policy is
not the sampler (for example vLLM weight sync skipped for a LoRA /
quantized model without `fast_inference`). The loss now honours
unsloth's `_unsloth_truncated_support_refused` and scores the full
vocabulary. Companion: unslothai/unsloth#14226.

The matching NaN for `softmax_renorm` (finite logits far below the
natural top-k) is fixed in
unslothai#1868; this PR merges
cleanly with it.

Default routing outputs and gradients are unchanged.

## Tests

`tests/test_routing_replay_compile.py` (new, CPU):

| Test | main | this PR |
|---|---|---|
| compiled router, 4 calls | recompiles `[2, 1, 1, 1]` | `[2, 0, 0, 0]`
|
| hooked decoder inside a compiled model | recompiles | no recompile |
| underflowed forced probabilities | NaN weights | natural routing,
finite gradients |

All routing replay test files: 39 passed.


`tests/test_grpo_truncated_support_paths.py::test_refused_trainer_scores_the_full_vocabulary`:
fails on main, passes here; with the GRPO truncated support tests, 37
passed.

## Not covered here

- No vLLM end-to-end GRPO run with routing replay.
- The one-time formula detection (`.item()`) and the host-side replay
matching still break `fullgraph = True`; this PR only removes the
per-call recompiles.
@danielhanchen
danielhanchen merged commit 103ba52 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/fu-rev3 branch October 19, 2026 19:14
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