Repository navigation
Fix GRPOTrainer evaluate() crash without prior training - #6523
Conversation
GRPOTrainer.compute_loss read self.current_gradient_accumulation_steps directly. That attribute is only set by the transformers training loop, so calling trainer.evaluate() standalone (no prior trainer.train()) raised AttributeError. Read it via getattr with a fallback to args.gradient_accumulation_steps so standalone evaluation works.
There was a problem hiding this comment.
Code Review
This pull request updates the compute_loss function in unsloth/models/rl_replacements.py to safely retrieve current_gradient_accumulation_steps using getattr with a fallback to self.args.gradient_accumulation_steps or 1. This prevents potential attribute errors when running evaluation standalone without a prior training loop. There are no review comments, so I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b41633c92e
ℹ️ 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".
| current_gradient_accumulation_steps = getattr( | ||
| self, | ||
| "current_gradient_accumulation_steps", | ||
| getattr(self.args, "gradient_accumulation_steps", 1), | ||
| ) |
There was a problem hiding this comment.
Use unit normalization for standalone evaluation
When evaluate() is the first trainer call and gradient_accumulation_steps > 1, this fallback passes that training-only value into the GRPO loss helpers, which divide GRPO/BNPO/DR_GRPO losses by current_gradient_accumulation_steps. The eval loop does not perform gradient accumulation, so standalone eval_loss is underreported by that factor, which can mislead eval-only metrics or model selection; the missing-attribute fallback should be 1 for eval mode rather than args.gradient_accumulation_steps.
Useful? React with 👍 / 👎.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8f37bce5a4
ℹ️ 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".
| current_gradient_accumulation_steps = getattr( | ||
| self, "current_gradient_accumulation_steps", 1 | ||
| ) |
There was a problem hiding this comment.
Preserve accumulation scaling outside standalone eval
Using attribute existence as the normalizer switch is not safe: on supported Transformers versions that do not create current_gradient_accumulation_steps, this fallback is also used during trainer.train(), so GRPO/BNPO/DR-GRPO loss receives 1 instead of the configured gradient_accumulation_steps; after a training step, evaluation can conversely reuse the stale training value even though eval does not accumulate. With gradient_accumulation_steps > 1, this mis-scales losses/gradients, so force 1 only for eval mode and otherwise fall back to self.args.gradient_accumulation_steps.
Useful? React with 👍 / 👎.
|
The replacement is a verbatim compute_loss body registered under grpo_trainer, and I reproduced the mechanism: pre-fix self.current_gradient_accumulation_steps raises AttributeError when evaluate() runs with no prior train(); getattr(..., 1) returns 1 standalone and the real value during training. Eval doesn't accumulate, so 1 is the right normalizer (matches upstream TRL). LGTM ✅ |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
@codex review |
|
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". |
Summary
Fixes a crash when running evaluation with
GRPOTrainer. Callingtrainer.evaluate()standalone (without a priortrainer.train()) raised:This is one of the evaluation scenarios reported in #2464 ("cannot use evaluation ... or trainer.evaluate without getting this error").
Root cause
GRPOTrainer.compute_lossreadsself.current_gradient_accumulation_stepsdirectly:That attribute is set by the transformers training loop (
Trainer, on each step). Whenevaluate()is called without a priortrain(), the training loop never runs, so the attribute does not exist andcompute_lossraisesAttributeErroras soon as the eval loop calls it throughprediction_step.Fix
Read the value defensively and fall back to
1when the training loop has not set it yet:During training the attribute is always present, so the value and behavior are identical to before. The fallback only affects the standalone-eval path that currently crashes. Eval does not accumulate gradients, so
1is the correct normalizer there; this matches upstream TRL, which scales bycurrent_gradient_accumulation_steps if mode == "train" else 1.0in its own GRPO loss path.Backwards compatibility
self.current_gradient_accumulation_stepsexists, so this is a no-op for the training path (same value, same throughput).1matches the value upstream TRL uses for the non-train path, so eval loss scaling is consistent.getattr; no new dependencies, no signature changes. Works on the NVIDIA, CPU and vLLM paths and across transformers / TRL versions, since it only touches attribute lookup.Before / after
Minimal GRPO setup on
unsloth/Qwen2.5-0.5B-Instructwith aneval_dataset.Before:
After:
Test matrix
All verified on a single GPU with
unsloth/Qwen2.5-0.5B-InstructLoRA + 4bit:trainer.evaluate()standalone (no prior train): crashed before, passes now.trainer.train()thentrainer.evaluate(): passes, finite train loss and eval metrics.eval_strategy="steps",eval_steps=1): passes.per_device_train_batch_size2 and 4,num_generations2 and 4: pass.use_vllm=Trueanduse_vllm=False: pass.Cross-version: the fix lives inside Unsloth's own
compute_lossbody, which theGRPO rewriter substitutes wholesale rather than regex-matching TRL source, and
grpo_traineris discovered undertrl.traineron every supported TRL. Verifiedthat the compiled
UnslothGRPOTrainercarries thegetattr(..., 1)fallback andthat standalone
evaluate()returns finiteeval_losson:Fixes #2464