Repository navigation
GRPO: keep TRL's old logprob gate when using vLLM - #13856
Conversation
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: Didn't find any major issues. What shall we delve into next? 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". |
|
pre-commit.ci run |
|
@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". |
|
pre-commit.ci run |
… path (unslothai#13968) ## Summary unslothai#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 = 0` there is no reference forward either, so after unslothai#13856 nothing latched the precision before the loss ran. The zoo loss then initialises `_autocast_dtype` from the process-wide `ACCELERATE_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 the `compute_efficient` branch as well. It is a `hasattr` check after the first call, so there is no extra forward and no measurable cost; the scoring forward stays skipped. ## Tests - New `tests/test_grpo_efficient_autocast_latch.py`: executes the real `_get_per_token_logps_and_entropies` and `_unsloth_grpo_autocast` sources 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.
…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>
Unsloth rewrote TRL's old logprob gate in
_generate_and_score_completionsfromgradient_accumulation_steps % generate_every != 0 or (self.use_vllm and self.vllm_importance_sampling_correction)to... or (self.use_vllm). As a result, every vLLM GRPO step ran an extra no-grad forward over the whole batch, even fully on-policy with importance sampling correction off. That is the Unsloth default. In that case the old logprobs equal the current ones detached, so the forward bought nothing.This drops the rewrite and keeps TRL's gate. Every consumer of
old_per_token_logpswas checked across TRL 0.22.2 to 1.15, Unsloth and unsloth-zoo:per_token_logps.detach(), as TRL does.Steps where generation and optimisation are misaligned still compute old logprobs through TRL's own condition.
On a 3-step Qwen3-0.6B LoRA GRPO run with vLLM, the change removes one no-grad forward per training step. With the correction off, loss and grad norm are bitwise identical to main on the steps where vLLM sampled the same completions. With the correction on, forward counts, loss, grad norm and the importance sampling metrics are bitwise identical on every step.
tests/test_grpo_old_logprob_gate.pyapplies the replacement to the installed TRL source and checks the gate against a truth table. It fails on main and passes here.