Repository navigation
GRPO: latch the trainer's autocast precision on the efficient logprob path - #13968
Conversation
… path #13856 skips the old-policy scoring forward under vLLM when importance sampling correction is off. That forward was where the trainer latched its own precision, so with beta = 0 nothing latched it and the zoo loss fell back to the process-wide ACCELERATE_MIXED_PRECISION, which another trainer built in the same process may have set. Latch it on the compute_efficient call too.
|
@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 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a7fc4eeb64
ℹ️ 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 compute_efficient: | ||
| # Latch this trainer's precision even when no scoring forward runs (vLLM, beta = 0): the loss | ||
| # otherwise falls back to the process-wide precision, which another trainer may have set. | ||
| _unsloth_grpo_autocast(self) |
There was a problem hiding this comment.
Latch precision in the legacy logprob replacement too
For supported older TRL releases that expose _get_per_token_logps rather than _get_per_token_logps_and_entropies, this new latch is never reached. compute_loss selects the former whenever it exists, but that replacement returns None at line 2353 before its _unsloth_grpo_autocast call. Consequently, the same vLLM + beta = 0 + disabled importance-sampling scenario still uses the process-wide precision when multiple trainers coexist under those supported TRL versions; add the latch before the efficient return in the legacy replacement as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in 792b01e: the legacy _get_per_token_logps replacement now latches the trainer's precision before its efficient return as well, with a test that runs its real source under a conflicting shared precision.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
…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>
Summary
#13856 stopped running the old-policy scoring forward under vLLM when importance sampling correction is off (the old logprobs were never read). That forward was also where the trainer latched its own autocast precision through
_unsloth_grpo_autocast(self).With
beta = 0there is no reference forward either, so after #13856 nothing latched the precision before the loss ran. The zoo loss then initialises_autocast_dtypefrom the process-wideACCELERATE_MIXED_PRECISION, which another trainer built in the same process may have set (for example a bf16 GRPO trainer created after an fp16 one).This PR calls
_unsloth_grpo_autocast(self)on thecompute_efficientbranch as well. It is ahasattrcheck after the first call, so there is no extra forward and no measurable cost; the scoring forward stays skipped.Tests
tests/test_grpo_efficient_autocast_latch.py: executes the real_get_per_token_logps_and_entropiesand_unsloth_grpo_autocastsources with a bf16 trainer while the shared env says fp16. Fails on main (no latch), passes here; an existing latch is left untouched.test_grpo_autocast_per_trainer.py,test_grpo_old_logprob_gate.py,test_grpo_autocast_disabled.py: 59 passed in total with the new test.test_formatter_fixed_point.py,test_source_read_encoding.py: pass.