Repository navigation
Stop dividing the GRPO eval loss by the accumulation steps - #11337
danielhanchen merged 2 commits into
Conversation
Resolve the divisor through a helper that returns 1 when the model is not training, reading `model.training` the way TRL does, and ship it to the generated trainer.
|
Confirmed this hits rl_replacements.py:2790, where the divisor is still read unconditionally and flows into the loss at lines 2909 and 3030, so an in-training evaluation reports eval_loss scaled by the training accumulation window. Will get this reviewed. |
|
@codex review |
|
Verified on a B200. issue: REAL | PR as submitted: CORRECT, no changes needed | merge: YES The fix reproduces and resolves cleanly against an independent oracle, and the training path is bitwise unchanged. SetupQwen3-0.6B + LoRA, Because the policy-gradient term is zero in eval here (ratio is 1, group advantages sum to zero), the true eval objective is exactly Before / after
head/base eval ratio is 4.000000 at GA=4 and 1.000000 at GA=1, at both eval points.
No training regressionAt both GA=1 and GA=4, base and head produce bitwise identical train losses and grad-norms: The generated The invariant the fix rests onI instrumented every It holds. Across 32 calls in the GA=4 head run:
Where this leaves the divergenceTRL main guards the same division at Not coveredMulti-GPU, DDP, DeepSpeed and FSDP, where One suggestion, not a blocker: the new tests are a truth table for the helper plus a structural assertion, and the thing that can actually break in future is the mode invariant above, not the helper. A fake-trainer test asserting Separately and out of scope for this PR: All CI green. Merging. |
|
Thanks for the oracle run, the On the mode-invariant test: I will add it as a follow-up rather than push to this head. It is a forward guard on Taking the |
The helper decides between 1 and the accumulation window by reading model.training, so its correctness rests on an assumption the truth-table tests cannot see: that an eval pass still reports model.training == False by the time compute_loss runs. That is not free in unsloth. grpo_trainer__generate_and_score_completions injects an unconditional self.model.for_training(...) into the generated _generate_and_score_completions, which fires on eval batches too, and the only thing that puts the flag back is the finally in _wrap_grpo_generate_and_score, which restores inference mode solely when it entered in it and swallows exceptions from the restore. If that restore regresses, the helper quietly starts dividing eval_loss again while every existing test stays green. This file asserts the property directly: eval mode survives generation and the divisor comes back 1, training mode survives and the window is kept, the restore still happens when generation raises, and the wrapper does not stack. Verified by removing the for_inference() call in rl.py, which turns three of the six red. Both the helper and the wrapper are lifted with ast from the shipped source, matching _grpo_dispatch_source and _rl_source, so the test stays CPU-only and does not import unsloth.
|
Pushed
So if that restore ever regresses, the helper quietly resumes dividing I checked it actually bites rather than just passing. Removing the Same |
|
Thanks for writing that test, it pins the right thing: the helper is only correct while |
|
Review record for #11337, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#53. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1 — reviewed mirror
|
…#11337) * Stop dividing the GRPO eval loss by the accumulation steps Resolve the divisor through a helper that returns 1 when the model is not training, reading `model.training` the way TRL does, and ship it to the generated trainer. * Pin the eval-mode invariant the GRPO divisor helper depends on The helper decides between 1 and the accumulation window by reading model.training, so its correctness rests on an assumption the truth-table tests cannot see: that an eval pass still reports model.training == False by the time compute_loss runs. That is not free in unsloth. grpo_trainer__generate_and_score_completions injects an unconditional self.model.for_training(...) into the generated _generate_and_score_completions, which fires on eval batches too, and the only thing that puts the flag back is the finally in _wrap_grpo_generate_and_score, which restores inference mode solely when it entered in it and swallows exceptions from the restore. If that restore regresses, the helper quietly starts dividing eval_loss again while every existing test stays green. This file asserts the property directly: eval mode survives generation and the divisor comes back 1, training mode survives and the window is kept, the restore still happens when generation raises, and the wrapper does not stack. Verified by removing the for_inference() call in rl.py, which turns three of the six red. Both the helper and the wrapper are lifted with ast from the shipped source, matching _grpo_dispatch_source and _rl_source, so the test stays CPU-only and does not import unsloth. --------- Co-authored-by: Daniel Han <[email protected]>
…_evaluate (unslothai#14112) GRPO `compute_loss` picked the metrics bucket from `control.should_evaluate`, which is still False for `eval_on_start=True` and a standalone `trainer.evaluate()`. Those eval passes logged `kl`, `completion_length` and `clip_ratio/*` into the train bucket, so `eval_*` got none and step-1 `train/completion_length` averaged in the eval batch. TRL uses `"train" if self.model.training else "eval"`; this does the same. unslothai#11337 already moved the eval loss divisor to `model.training`. Test: new `tests/test_grpo_metrics_bucket.py` lifts the shipped `compute_loss`, stubs the zoo loss, and checks the bucket for train, eval_on_start and a scheduled eval. On main 2 of 4 fail (`[] == [4.0]`, train length `6.0` instead of `8.0`); with the fix all 4 pass. CPU only, no GRPO run on a GPU. --------- Co-authored-by: Daniel Han <[email protected]>
eval_lossfrom a GRPO run is smaller than the train loss by exactlygradient_accumulation_steps, and moves when that setting changes.The patched
compute_lossreadsself.current_gradient_accumulation_stepsunconditionally. TRL uses it in train mode and 1.0 in eval, since an eval pass accumulates nothing, andTrainernever clears the attribute after the training loop sets it. The existing fallback only covers a standaloneevaluate(), where the attribute is missing altogether. Resolve the divisor through a helper that returns 1 when the model is not training, readingmodel.trainingthe way TRL does, and ship it to the generated trainer.Test:
python -m pytest tests/test_grpo_eval_accumulation.py -q. Eight CPU tests cover both modes, the missing attribute and a trainer with no model, plus a structural check thatcompute_lossno longer reads the attribute directly. They fail before and pass after; 42 tests pass across the three GRPO source-lift files. Ruff 0.15.18 and the pinned kwarg-spacing formatter pass. Executed on Apple M2 Pro CPU; no GPU or distributed run. The helper is lifted with ast, so the test does not import unsloth.