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)
This commit is contained in:
Daniel Han 2026-06-23 01:27:29 -07:00 committed by GitHub
commit f74c48eb58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)