Repository navigation
GRPO: latch autocast under vLLM only, zero-variance exclusion on TRL before 1.6 - #14123
Conversation
The efficient logprob call latched the trainer's autocast precision on every GRPO run. That restored the latch vLLM lost when the old-policy forward was skipped, but it also reached runs without vLLM, where nothing had latched before when nothing was scored (beta = 0, aligned steps). For a forced float32 model (Gemma3 on a T4, or dtype = torch.float16) the loss used to run with no autocast there. With the latch it ran under float16 autocast, so the patched float32 attention ran in float16 and grad norms moved. Latch on the efficient path only under vLLM, which matches what every run did before the TRL gate came back.
…eward 0 on TRL before 1.6 TRL before 1.6 sums an unscored completion's rewards to 0 (nansum), so a group such as [None, 1, 1, 1] has nonzero std and real advantages. The opt-in exclusion always treated the row as missing, as TRL 1.6 and newer do, and dropped that group from the loss. The NaN masking now follows the installed TRL.
Importing unsloth replaces GRPOTrainer with the compiled, wrapped trainer, so inspect.getsource on the class returned the wrapper, the rewrite never anchored and the test always skipped. Parse the TRL module file instead.
|
@codex review |
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. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b6a89aef0d
ℹ️ 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".
| if getattr(self, "use_vllm", False): | ||
| _unsloth_grpo_autocast(self) |
There was a problem hiding this comment.
Preserve per-trainer autocast outside vLLM
When use_vllm=False, beta=0, and no old-policy scoring forward runs, this efficient call is the only opportunity to copy the trainer's precision settings into _autocast_dtype and _autocast_enabled. Skipping it for every non-vLLM trainer makes grpo_accumulated_loss fall back to the process-wide ACCELERATE_MIXED_PRECISION, so multiple trainers or an explicitly differing environment can silently run fp16 instead of bf16, disable autocast, or OOM despite self.args requesting otherwise; the legacy replacement now has the same regression. Please limit the bypass to the forced-float32 non-vLLM case that needs it rather than all non-vLLM training.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 3c16012: the latch is skipped only for a forced float32 model without vLLM; every other non-vLLM trainer still latches its own precision, with a test.
…d float32 models [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f59208ed1f
ℹ️ 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".
| and not getattr(self, "use_vllm", False) | ||
| and _unsloth_grpo_autocast_forced_float32(self) | ||
| ): | ||
| return None |
There was a problem hiding this comment.
Preserve the forced-float32 loss state before returning
When a forced-float32, non-vLLM trainer reaches this no-scoring path after another model/trainer has reset the process-wide UNSLOTH_FORCE_FLOAT32 and ACCELERATE_MIXED_PRECISION values, this return leaves both autocast latch fields absent, so grpo_accumulated_loss falls back to the newer trainer's flags and can enable fp16 for the first model—the exact path this branch intends to keep in full float32. Fresh evidence after the reported fix is that _unsloth_grpo_autocast_forced_float32(self) now correctly reads the first model's stamp here, but its result is discarded rather than recorded for the downstream loss; latch an explicit disabled/full-float32 state instead of returning unlatched.
Useful? React with 👍 / 👎.
|
@codex review |
3 similar comments
|
@codex review |
|
@codex review |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 733d802cc8
ℹ️ 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".
| not hasattr(self, "_autocast_enabled") | ||
| and not getattr(self, "use_vllm", False) | ||
| and _unsloth_grpo_autocast_forced_float32(self) |
There was a problem hiding this comment.
Keep bf16 autocast for forced-family full finetuning
When a model stamped _unsloth_forced_float32 is full-finetuned on a bf16-capable device without vLLM and reaches the no-scoring path (for example, beta = 0), this condition treats the family stamp as proof that training was actually forced to float32. However, rl.py deliberately permits these models to use bf16 during full finetuning and records args.mixed_precision = "bf16"; this branch ignores that setting and latches _autocast_enabled = False, causing the loss forward to run in full float32 and potentially OOM. Restrict this bypass to trainers whose selected precision is actually "no", while allowing _unsloth_grpo_autocast to preserve bf16.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in fc7a77a: the unscored path now takes the ordinary latch first and only records autocast off when that latch forced float16 on a float32 model; a forced-family bf16 full finetune keeps bf16, with a test.
|
@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". |
Follow-ups from a re-review of the merged GRPO changes (#13968, #13891, #14023).
Changes
Latch the efficient-path autocast under vLLM only (
rl_replacements.py, GRPO: latch the trainer's autocast precision on the efficient logprob path #13968).GRPO: latch the trainer's autocast precision on the efficient logprob path #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 GRPO: keep TRL's old logprob gate when using vLLM #13856, without depending on process-wide env that a later model may change). Before GRPO: keep TRL's old logprob gate when using vLLM #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, ordtype = 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 GRPO: keep TRL's old logprob gate when using vLLM #13856 again. vLLM runs and every non-forced trainer latch exactly as on main.Zero-variance exclusion on TRL before 1.6 (
_unsloth_grpo_sample_weight, GRPO: opt-in zero-variance group exclusion and prompt-level loss aggregation #13891, opt-inUNSLOTH_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 throughnansum, 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.Test only:
test_grpo_reward_locals_cycle.pyalways skipped once unsloth was imported. It now reads TRL's own GRPO source. It fails on Follow-ups to the GRPO reward locals and FSDP2 mixed precision merges #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 #13856.
Tests
test_efficient_call_without_vllm_leaves_forced_float32_unlatchedandtest_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_trainerkeeps the per-trainer latch for everything else.test_grpo_autocast_per_trainer.py,test_grpo_old_logprob_gate.py,test_grpo_autocast_disabled.pyandtest_grpo_sample_weight.py: 94 passed, 1 skipped.trl/trainer/grpo_trainer.pyat v0.22.2 (line 1392), v1.3.0 (2183) and v1.5.1 (2201) usesnansumfor every completion; v1.6.0 (2168-2171) and v1.15.0 (2682-2685) addunscorable_mask.