From f74c48eb5801632149a1d82eed8625092f61dc60 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Tue, 23 Jun 2026 01:27:29 -0700 Subject: [PATCH] Fix GRPOTrainer evaluate() crash without prior training (#6523) * Fix GRPOTrainer evaluate() crash when called without prior training 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. * GRPO eval: fall back accumulation steps to 1 so standalone eval_loss is not underreported * Tighten code comments (no logic change) --- unsloth/models/rl_replacements.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py index ab18a5ec93..ba4b4b4415 100644 --- a/unsloth/models/rl_replacements.py +++ b/unsloth/models/rl_replacements.py @@ -1530,7 +1530,11 @@ def grpo_trainer_compute_loss(function_name, function): num_items_in_batch = inputs.get("num_items_in_batch", None) sampling_per_token_logps = inputs.get("sampling_per_token_logps", None) tool_mask = inputs.get("tool_mask", None) - current_gradient_accumulation_steps = self.current_gradient_accumulation_steps + # Missing when evaluate() runs standalone; eval does not accumulate, so + # fall back to 1 to avoid underreporting eval_loss (#2464). + current_gradient_accumulation_steps = getattr( + self, "current_gradient_accumulation_steps", 1 + ) num_processes = self.accelerator.num_processes input_ids = torch.cat([prompt_ids, completion_ids], dim = 1)