Skip to content

GRPO: latch autocast under vLLM only, zero-variance exclusion on TRL before 1.6 - #14123

Merged
danielhanchen merged 9 commits into
mainfrom
dh/v2-grpo-followups
Oct 19, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
dh/v2-grpo-followups

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Follow-ups from a re-review of the merged GRPO changes (#13968, #13891, #14023).

Changes

  1. 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, 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 GRPO: keep TRL's old logprob gate when using vLLM #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, GRPO: opt-in zero-variance group exclusion and prompt-level loss aggregation #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 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

  • 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.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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-18T17:29:15.123128Z fc7a77a Manual request
🔒 Security Review ⚠️ Failed 2026-10-18T11:39:27.604048Z b6a89ae 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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/models/rl_replacements.py Outdated
Comment on lines +2480 to +2481
if getattr(self, "use_vllm", False):
_unsloth_grpo_autocast(self)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@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 them coming!

Reviewed commit: 3c160122c9

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/models/rl_replacements.py Outdated
Comment on lines +1197 to +1200
and not getattr(self, "use_vllm", False)
and _unsloth_grpo_autocast_forced_float32(self)
):
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 733d802: a forced float32 trainer without vLLM now records autocast off on itself, so later env changes no longer reach it; the tiny Gemma3 probe still gives the pre-#13856 grad norms bitwise.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

3 similar comments
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/models/rl_replacements.py Outdated
Comment on lines +1197 to +1199
not hasattr(self, "_autocast_enabled")
and not getattr(self, "use_vllm", False)
and _unsloth_grpo_autocast_forced_float32(self)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@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: fc7a77a23e

ℹ️ 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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant