Skip to content

Follow-ups to the GRPO reward locals and FSDP2 mixed precision merges - #14023

Merged
danielhanchen merged 3 commits into
mainfrom
dh/postmerge-fixes
Oct 16, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/postmerge-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 later locals().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 calls locals().

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__ stores reduce_dtype = None when it equals param_dtype, so the reduce_dtype != compute check returned early and torch 2.6 kept reducing fp16 gradients in fp16. None means 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_completions and checks the alias is deleted before any later locals() 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_*.py and tests/test_fsdp2_*.py: 380 passed, 25 skipped. test_fsdp2_default_optim.py fails the same way on main and on the pre-week base in this environment, so it is left out here.

@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:18:04.965411Z 6a3de9d Manual request
🔒 Security Review ⚠️ Failed 2026-10-16T16:15:17.684033Z 6a3de9d 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

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 6a3de9d626

ℹ️ 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 029bc4a into main Oct 16, 2026
@danielhanchen
danielhanchen deleted the dh/postmerge-fixes branch October 16, 2026 18:30
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>
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