Repository navigation
Make the GRPO canary run through Unsloth's own batch sampler - #11126
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 security review |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: abba707ce4
ℹ️ 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".
| # has that same problem, since it also needs the names up front. | ||
| trainer_attributes_before = dict(Trainer.__dict__) | ||
|
|
||
| patch_gradient_accumulation_fix(Trainer) |
There was a problem hiding this comment.
Restore Trainer if installing the patch raises
When a latest/main Transformers change makes patch_gradient_accumulation_fix raise after it has already replaced get_batch_samples or another method, this call is still outside the try/finally, so the partially patched base Trainer leaks into the remaining files in the same pytest invocation. In particular, test_trl_loss_normalization_contract.py can then inspect Unsloth's wrapper instead of the upstream implementation, causing misleading follow-on results in the compatibility job; include patch installation and its assertion inside the protected region so every exit restores the snapshot.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and it is the one path the snapshot did not cover. Fixed in 4f2c5d8.
patch_gradient_accumulation_fix and its assertion now sit inside the try, so every exit restores, including the one where the patcher itself raises partway through.
I proved it with a negative control rather than by reading the code, and my first attempt at that control was wrong in a way worth stating: I raised after the patch but still inside the try, so the finally ran anyway and the run looked identical to the fixed one. The faithful control is a patcher that replaces the methods and then raises, called from outside the protected region. With it:
outside the try 1 failed, 9 passed, 2 skipped
SKIPPED test_trl_loss_normalization_contract.py:209:
Trainer.training_step source unavailable
inside the try 1 failed, 10 passed, 1 skipped (that skip gone)
So the leak is real on that path and the fix closes it. The clean run is unchanged at 14 passed / 1 skipped across both files.
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
The fast fake GRPO run, and what it is proved to catchStatus of the ask for a fast GRPO validation run: this PR is it, and it is verified two ways rather The new test takes two real GRPO steps on CPU, through Two corrections were made to this PR after the first version, both found by trying to make the test
What it would have caught: unsloth-zoo#1217 rewrote a list safe membership test into |
4f2c5d8 to
7a1e4f5
Compare
|
Rebased onto current The three Studio files were a pre-commit.ci autofix, and nothing was lost by dropping them
All of it is on The five jobsFour were red on
All six files now pass at this head: 390 passed, 24 skipped. The fifth is different and is not fixed by the rebase.
Not adding the transformers/TRL pin test here
That is #11137's subject, which lifts the ceiling to 1.13.0 with a seven-version evidence table, so it is not being duplicated here. One residual worth naming there: lifting a ceiling does not forbid a resolver from picking |
The existing GRPO canary runs a real UnslothGRPOTrainer for two steps, but _load_plain never goes through the loader that calls patch_gradient_accumulation_fix, so Trainer.get_batch_samples stays stock transformers and the zoo function is never entered. Measured: 0 calls. That is how unsloth-zoo#1217 killed every GRPO run at step 0 for a day without any lane noticing. TRL's GRPO collator is the identity, so a batch is a list of dicts, and the rewrite from a membership test to .get raised AttributeError. This installs the patch the way a real run does, asserts it took, takes real steps, and asserts at least one batch arrived as a list, since a canary that only sees the SFT dict shape cannot catch a regression that assumes one. Fails against zoo e2e81b7, passes against 2dcbe9c, 6.3 seconds for the file.
patch_gradient_accumulation_fix mutates the Trainer class, and in the grpo-fake-run workflow this file shares a pytest process with test_trl_loss_normalization_contract.py, which reads Trainer.get_batch_samples and Trainer.training_step to check what upstream transformers returns. The finally block restored get_batch_samples to the already-patched Zoo function and left training_step, __init__ and compute_loss wrapped, so that later file was inspecting Unsloth's methods and calling them transformers. Measured, not argued: at the previous head test_transformers_training_step_still_keys_off_model_accepts_loss_kwargs SKIPPED with 'Trainer.training_step source unavailable', because the generated wrapper has no retrievable source. With this change it runs and passes, so the four-file CI set goes from 137 passed / 33 skipped to 138 passed / 32 skipped. Snapshot the whole class dict rather than naming attributes: a hand-written list falls behind the next attribute the patcher touches, and monkeypatch needs the names up front too. The restore is then asserted by identity, so the canary proves its own cleanup instead of assuming it.
patch_gradient_accumulation_fix and its assertion sat outside the try, so a transformers change that made the patcher raise AFTER replacing one method would skip the finally entirely and leak a partially patched Trainer into every later file in the same pytest invocation. That is the leak the snapshot exists to prevent, reachable through the one path the snapshot did not cover. Negative control, with a patcher that replaces the methods and then raises: called outside the try, test_trl_loss_normalization_contract.py reports 9 passed / 2 skipped with 'Trainer.training_step source unavailable'; called inside, 10 passed / 1 skipped and that skip is gone. Clean run is unchanged at 14 passed / 1 skipped across both files.
The post-cleanup check compared the two Trainer class dicts with ==, which reads as an identity check but is not one: dict equality compares values with ==, so a wrapper that merely compares equal to the method it replaced passes. It also prints several hundred entries and no verdict when it does fail. Compare with is, per attribute, and name the ones that leaked or were not put back.
1ad8e5a to
c83bf08
Compare
|
Rebased onto current
Raw log of the GRPO lane, against Both arms re-measured after the rebase, same file, only
The rest of the red is main's, and it is filedFive jobs are still red. Every one of them reproduces on a clean checkout of which accounts for The fifth, None of the five run this PR's change: the three The P2 fix is real, and checked rather than taken on trustVerified by making
Also tightened the post-cleanup check itself. It compared the two class dicts with |
…ai#11126) * Make the GRPO canary run through Unsloth's own batch sampler The existing GRPO canary runs a real UnslothGRPOTrainer for two steps, but _load_plain never goes through the loader that calls patch_gradient_accumulation_fix, so Trainer.get_batch_samples stays stock transformers and the zoo function is never entered. Measured: 0 calls. That is how unsloth-zoo#1217 killed every GRPO run at step 0 for a day without any lane noticing. TRL's GRPO collator is the identity, so a batch is a list of dicts, and the rewrite from a membership test to .get raised AttributeError. This installs the patch the way a real run does, asserts it took, takes real steps, and asserts at least one batch arrived as a list, since a canary that only sees the SFT dict shape cannot catch a regression that assumes one. Fails against zoo e2e81b7, passes against 2dcbe9c, 6.3 seconds for the file. * Restore every Trainer attribute the GRPO canary patches, not just one patch_gradient_accumulation_fix mutates the Trainer class, and in the grpo-fake-run workflow this file shares a pytest process with test_trl_loss_normalization_contract.py, which reads Trainer.get_batch_samples and Trainer.training_step to check what upstream transformers returns. The finally block restored get_batch_samples to the already-patched Zoo function and left training_step, __init__ and compute_loss wrapped, so that later file was inspecting Unsloth's methods and calling them transformers. Measured, not argued: at the previous head test_transformers_training_step_still_keys_off_model_accepts_loss_kwargs SKIPPED with 'Trainer.training_step source unavailable', because the generated wrapper has no retrievable source. With this change it runs and passes, so the four-file CI set goes from 137 passed / 33 skipped to 138 passed / 32 skipped. Snapshot the whole class dict rather than naming attributes: a hand-written list falls behind the next attribute the patcher touches, and monkeypatch needs the names up front too. The restore is then asserted by identity, so the canary proves its own cleanup instead of assuming it. * Install the GRPO patch inside the protected region, not before it patch_gradient_accumulation_fix and its assertion sat outside the try, so a transformers change that made the patcher raise AFTER replacing one method would skip the finally entirely and leak a partially patched Trainer into every later file in the same pytest invocation. That is the leak the snapshot exists to prevent, reachable through the one path the snapshot did not cover. Negative control, with a patcher that replaces the methods and then raises: called outside the try, test_trl_loss_normalization_contract.py reports 9 passed / 2 skipped with 'Trainer.training_step source unavailable'; called inside, 10 passed / 1 skipped and that skip is gone. Clean run is unchanged at 14 passed / 1 skipped across both files. * Drop the unused inspect import from the GRPO canary * Name the attribute the GRPO canary failed to restore The post-cleanup check compared the two Trainer class dicts with ==, which reads as an identity check but is not one: dict equality compares values with ==, so a wrapper that merely compares equal to the method it replaced passes. It also prints several hundred entries and no verdict when it does fail. Compare with is, per attribute, and name the ones that leaked or were not put back. --------- Co-authored-by: danielhanchen <[email protected]>
Summary
The GRPO canary in
tests/version_compat/test_trl_fake_train_cpu.pyproves the trainer runs.It does not prove Unsloth's own
get_batch_samplesruns, and that gap let a total GRPObreakage sit on unsloth-zoo main for a day.
This adds one test, 6 seconds, that takes real GRPO steps through the patched batch sampler.
What happened
unsloth-zoo#1217 rewrote a list-safe membership test into a
.get:TRL's GRPO trainer uses an identity collator, so every element of
batch_samplesis alist of dicts, never a dict.
inon a list is simplyFalse;.getraises. Every GRPOrun died at step 0:
Fixed in unsloth-zoo#1254. It never reached PyPI: the regression landed 2026-09-15 and the
released
unsloth_zoo 2026.9.3is from 2026-09-09, so no user was affected. It was found byaccident, while verifying an unrelated PR, not by a gate.
Why the existing lanes could not catch it
test_trl_grpo_fake_run.pytest_trl_fake_train_cpu.py::test_sft_trains_on_cpu.train(), but an SFT collator hands back dictstest_trl_fake_train_cpu.py::test_grpo_trains_on_cpu.train()The third is the surprising one. It does run a real
UnslothGRPOTrainerfor two steps, but_load_plain()loads a plain HF model and never goes through the loader that callspatch_gradient_accumulation_fix, soTrainer.get_batch_samplesstays stock transformers.Measured by wrapping the zoo function and counting: 0 calls, with
Trainer.get_batch_samplesstill resolving toTrainer.get_batch_samples.So the canary was exercising TRL's trainer, not Unsloth's batch sampler.
What this adds
test_grpo_trains_on_cpu_through_the_patched_batch_sampler:patch_gradient_accumulation_fix(Trainer), the way a real run does,that last assertion the test would still pass on a harness that only ever produced the SFT
dict shape, which is exactly how this class of regression hides.
Evidence
Both arms, same test file, only
unsloth_zoochanged:e2e81b7(#1217, the regression)AttributeError: 'list' object has no attribute 'get'atloss_utils.py:4022dcbe9c(#1254, fixed)The existing three canaries pass on both, which is the point: they are blind to this.
Cost
6.3 seconds for the whole file, CPU only, no GPU, tiny random-weight model
(
hf-internal-testing/tiny-random-LlamaForCausalLM), reusing the fixtures already in thefile. The
grpo-fake-runlane inversion-compat-ci.ymlalready clones unsloth-zoo at mainand already runs this file, so it is covered with no workflow change.