Repository navigation
Follow-ups to the GRPO reward locals and FSDP2 mixed precision merges - #14023
Merged
Merged
Conversation
…h stores reduce_dtype None
…out tensors alive in a cycle
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. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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
to Etherll/unsloth
that referenced
this pull request
Oct 19, 2026
…before 1.6 (unslothai#14123) Follow-ups from a re-review of the merged GRPO changes (unslothai#13968, unslothai#13891, unslothai#14023). ## Changes 1. **Latch the efficient-path autocast under vLLM only** (`rl_replacements.py`, unslothai#13968). unslothai#13968 latched the trainer precision whenever the efficient path skipped the scoring forward. That latch is right for ordinary trainers (it keeps each trainer's own precision when several run in one process), so it stays; only a model forced to float32 without vLLM now records "no loss autocast" on the trainer instead (what it effectively got before unslothai#13856, without depending on process-wide env that a later model may change). Before unslothai#13856 restored TRL's old-logprob gate, only vLLM runs always ran that forward, so only they latched. Without vLLM and with `beta = 0`, nothing latched and the loss ran without autocast. Latching there now wraps models forced to float32 (Gemma3 on a T4, or `dtype = float16`) in fp16 / bf16 autocast, which changes the numerics. On a tiny Gemma3 with no vLLM and `beta = 0`, the step 1 grad norm went from 0.8288910 to 0.8287503 with LoRA, and from 1.1988286 to 1.1986356 with full finetuning. With this change both runs are bitwise equal to the code before unslothai#13856 again. vLLM runs and every non-forced trainer latch exactly as on main. 2. **Zero-variance exclusion on TRL before 1.6** (`_unsloth_grpo_sample_weight`, unslothai#13891, opt-in `UNSLOTH_GRPO_ZERO_VARIANCE_EXCLUDE=1`). TRL 1.6.0 added `unscorable_mask`: a completion no reward function scored is left out of its group's statistics. TRL 0.22.2 to 1.5.1 instead score it 0 through `nansum`, so a group like `[None, 1, 1, 1]` has variance and real advantages. The exclusion treated such a completion as missing on every TRL version, so these groups were dropped on older TRL. It now follows the installed TRL: missing on 1.6+, scored 0 before 1.6. 3. **Test only:** `test_grpo_reward_locals_cycle.py` always skipped once unsloth was imported. It now reads TRL's own GRPO source. It fails on unslothai#14023's parent and passes on the merge. The default path is unchanged: change 2 is opt-in, and change 1 only affects forced float32 models without vLLM that skip the scoring forward, which now behave as they did before unslothai#13856. ## Tests - New tests fail on current main and pass here: `test_efficient_call_without_vllm_leaves_forced_float32_unlatched` and `test_unscored_completion_counts_as_zero_on_trl_before_1_6` (main: 2 failed, 36 passed; this branch: 38 passed). `test_efficient_call_without_vllm_still_latches_an_ordinary_trainer` keeps the per-trainer latch for everything else. - Neighbours `test_grpo_autocast_per_trainer.py`, `test_grpo_old_logprob_gate.py`, `test_grpo_autocast_disabled.py` and `test_grpo_sample_weight.py`: 94 passed, 1 skipped. - TRL reference: `trl/trainer/grpo_trainer.py` at v0.22.2 (line 1392), v1.3.0 (2183) and v1.5.1 (2201) uses `nansum` for every completion; v1.6.0 (2168-2171) and v1.15.0 (2682-2685) add `unscorable_mask`. --------- 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.
Two follow-ups to PRs merged this week, each found by re-checking the merged code against its parent.
Changes
GRPO: the reward locals alias no longer keeps rollout tensors alive (follow-up to #13891)
The reward rewrite (TRL 0.24 and newer) binds
_unsloth_reward_locals = locals()in_generate_and_score_completions. #13891 added a laterlocals().get('tool_mask')call in the same frame. On CPython 3.12 and older,locals()returns the frame's one dict and refreshes it, so the second call stores that dict inside itself. The cycle keeps every local of the step, including the concatenated prompt and completion ids and the reward tensors, alive after the method returns, until the cyclic GC happens to run. That is GPU memory carried into the training step and the next rollout. Python 3.13 is not affected (PEP 667). The alias is now deleted right after the reward call, before anything else callslocals().FSDP2: the automatic mixed precision repair also runs on torch 2.6 (follow-up to #13865)
#13865 switches accelerate's automatic FSDP2 policy to fp32 gradient reduction when it matches the compute dtype. torch 2.6
MixedPrecisionPolicy.__post_init__storesreduce_dtype = Nonewhen it equalsparam_dtype, so thereduce_dtype != computecheck returned early and torch 2.6 kept reducing fp16 gradients in fp16.Nonemeans the same thing there (reduce in the compute dtype), so it now counts as a match.Tests
tests/test_grpo_reward_locals_cycle.py: rewrites the installed TRL_generate_and_score_completionsand checks the alias is deleted before any laterlocals()call (fails on main), plus a frame lifetime check. A standalone repro of the pattern on CPython 3.12 keeps the object alive before the change and frees it after; CPython 3.13 frees it either way.test_torch_26_normalized_auto_policy_still_reduces_in_fp32(fp16 and bf16): fails on main, passes here.tests/test_grpo_*.pyandtests/test_fsdp2_*.py: 380 passed, 25 skipped.test_fsdp2_default_optim.pyfails the same way on main and on the pre-week base in this environment, so it is left out here.