Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep the GRPO eval batch a multiple of num_generations (#12362)

Vineeth Sai Varikuntla1f65728b2a0a
diff --git a/tests/version_compat/test_trl_fake_train_cpu.py b/tests/version_compat/test_trl_fake_train_cpu.py
index 0cfca979d76..897210c3bce 100644
--- a/tests/version_compat/test_trl_fake_train_cpu.py
+++ b/tests/version_compat/test_trl_fake_train_cpu.py
@@ -335,6 +335,44 @@ def test_dpo_trains_on_cpu(tmp_path):
DPOTrainer(model = model, processing_class = tok, args = cfg, train_dataset = ds).train()
+def test_grpo_evaluates_with_an_explicit_eval_batch_size(tmp_path):
+ """Shrinking an explicit eval batch of 8 to the train batch of 2 split num_generations = 4 groups."""
+ from datasets import Dataset
+ from trl import GRPOConfig, GRPOTrainer
+
+ model, tok = _load_plain()
+ _guard_finite_logits(model)
+ ds = Dataset.from_list([{"prompt": "hi there"}] * 8)
+ cfg = GRPOConfig(
+ output_dir = str(tmp_path / "ci_grpo_eval"),
+ per_device_train_batch_size = 2,
+ gradient_accumulation_steps = 2,
+ per_device_eval_batch_size = 8,
+ num_generations = 4,
+ eval_strategy = "steps",
+ max_completion_length = 8,
+ report_to = "none",
+ temperature = 1.0,
+ beta = 0.0,
+ save_strategy = "no",
+ use_cpu = True,
+ use_vllm = False,
+ fp16 = False,
+ bf16 = False,
+ optim = "adamw_torch",
+ )
+ trainer = GRPOTrainer(
+ model = model,
+ processing_class = tok,
+ reward_funcs = [lambda completions, **k: [float(len(c)) for c in completions]],
+ args = cfg,
+ train_dataset = ds,
+ eval_dataset = ds,
+ )
+ assert trainer.args.per_device_eval_batch_size == 8
+ assert "eval_loss" in trainer.evaluate()
+
+
def test_grpo_trains_on_cpu_through_the_patched_batch_sampler(tmp_path):
"""The canary above proves the GRPO trainer runs. It does NOT prove Unsloth's own
``get_batch_samples`` runs, because ``_load_plain`` never goes through the loader that
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index cf34582ffb4..8a353f475ee 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -2359,10 +2359,12 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
)
extra_args += check_ga
+ # GRPO evaluates whole groups, so never shrink the eval batch below what num_generations divides.
eval_changes = (
"if getattr(args, 'eval_strategy', 'no') != 'no':\n"
" eval_bsz = getattr(args, 'per_device_eval_batch_size', 8)\n"
- " if eval_bsz == 8 and args.per_device_train_batch_size < eval_bsz: args.per_device_eval_batch_size = args.per_device_train_batch_size\n"
+ " eval_generations = getattr(args, 'num_generations_eval', None) or getattr(args, 'num_generations', None) or 1\n"
+ " if eval_bsz == 8 and args.per_device_train_batch_size < eval_bsz and (args.per_device_train_batch_size * args.world_size) % eval_generations == 0: args.per_device_eval_batch_size = args.per_device_train_batch_size\n"
" if getattr(args, 'eval_accumulation_steps', None) is None and ga_steps is not None: args.eval_accumulation_steps = ga_steps\n"
"fp16_full_eval = getattr(args, 'fp16_full_eval', False)\n"
"if type(fp16_full_eval) is not bool: fp16_full_eval = False\n"