Skip to content

Fix GRPOTrainer evaluate() crash without prior training - #6523

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
danielhanchen:fix-eval-faketensor-2464
Jun 23, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
danielhanchen:fix-eval-faketensor-2464

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes a crash when running evaluation with GRPOTrainer. Calling trainer.evaluate() standalone (without a prior trainer.train()) raised:

AttributeError: 'UnslothGRPOTrainer' object has no attribute 'current_gradient_accumulation_steps'

This is one of the evaluation scenarios reported in #2464 ("cannot use evaluation ... or trainer.evaluate without getting this error").

Root cause

GRPOTrainer.compute_loss reads self.current_gradient_accumulation_steps directly:

current_gradient_accumulation_steps = self.current_gradient_accumulation_steps

That attribute is set by the transformers training loop (Trainer, on each step). When evaluate() is called without a prior train(), the training loop never runs, so the attribute does not exist and compute_loss raises AttributeError as soon as the eval loop calls it through prediction_step.

Fix

Read the value defensively and fall back to 1 when the training loop has not set it yet:

current_gradient_accumulation_steps = getattr(
    self, "current_gradient_accumulation_steps", 1
)

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 1 is the correct normalizer there; this matches upstream TRL, which scales by current_gradient_accumulation_steps if mode == "train" else 1.0 in its own GRPO loss path.

Backwards compatibility

  • During training, self.current_gradient_accumulation_steps exists, so this is a no-op for the training path (same value, same throughput).
  • The eval fallback of 1 matches the value upstream TRL uses for the non-train path, so eval loss scaling is consistent.
  • One-line semantic change behind 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-Instruct with an eval_dataset.

Before:

trainer.evaluate()
  -> AttributeError: 'UnslothGRPOTrainer' object has no attribute 'current_gradient_accumulation_steps'

After:

=== EVAL ONLY (no prior train) ===
=== EVAL METRICS === {'eval_loss': 0.188..., 'eval_runtime': ...}
=== SUCCESS ===

Test matrix

All verified on a single GPU with unsloth/Qwen2.5-0.5B-Instruct LoRA + 4bit:

  • trainer.evaluate() standalone (no prior train): crashed before, passes now.
  • trainer.train() then trainer.evaluate(): passes, finite train loss and eval metrics.
  • Inline eval during training (eval_strategy="steps", eval_steps=1): passes.
  • per_device_train_batch_size 2 and 4, num_generations 2 and 4: pass.
  • use_vllm=True and use_vllm=False: pass.
  • bf16: pass.

Cross-version: the fix lives inside Unsloth's own compute_loss body, which the
GRPO rewriter substitutes wholesale rather than regex-matching TRL source, and
grpo_trainer is discovered under trl.trainer on every supported TRL. Verified
that the compiled UnslothGRPOTrainer carries the getattr(..., 1) fallback and
that standalone evaluate() returns finite eval_loss on:

  • transformers 4.57.6 + trl 0.22.2
  • transformers 4.57.6 + trl 0.27.1
  • transformers 5.12.1 + trl 1.6.0 (torch 2.12)

Fixes #2464

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@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: 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".

Comment on lines +1535 to +1539
current_gradient_accumulation_steps = getattr(
self,
"current_gradient_accumulation_steps",
getattr(self.args, "gradient_accumulation_steps", 1),
)

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

@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: 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".

Comment on lines +1535 to +1537
current_gradient_accumulation_steps = getattr(
self, "current_gradient_accumulation_steps", 1
)

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

@oobabooga

Copy link
Copy Markdown
Member

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 ✅

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen
danielhanchen merged commit f74c48e into unslothai:main Jun 23, 2026
46 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: f157cba4cb

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

Labels

None yet

Projects

None yet

2 participants