Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions unsloth/models/rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,21 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"):
extra_args += check_dr_grpo

# Check GRPO num_generations mismatch
if "per_device_train_batch_size" in call_args and "num_generations" in call_args:
if (
"per_device_train_batch_size" in call_args
and "num_generations" in call_args
and "steps_per_generation" in call_args
and "generation_batch_size" in call_args
):
# if world size is not set by accelerate or torchrun at this point it will be 1
check_num_generations = (
"if (per_device_train_batch_size // num_generations) * num_generations != per_device_train_batch_size:\n"
" print('Unsloth: We now expect `per_device_train_batch_size` to be a multiple of `num_generations`.\\n"
"if steps_per_generation is None and generation_batch_size is None:\n"
" ga = gradient_accumulation_steps\n"
" world_size = int(os.environ.get('WORLD_SIZE', '1'))\n"
" if (ga * world_size * per_device_train_batch_size) % num_generations != 0:\n"
" print('Unsloth: We now expect `per_device_train_batch_size` * `gradient_accumulation_steps` * `world_size` to be a multiple of `num_generations`.\\n"
"We will change the batch size of ' + str(per_device_train_batch_size) + ' to the `num_generations` of ' + str(num_generations))\n"
" per_device_train_batch_size = num_generations\n"
" per_device_train_batch_size = num_generations\n"
"\n"
)
extra_args += check_num_generations
Comment on lines 825 to 842
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While this new relaxed check is a good improvement, it appears the old, stricter check from grpo_trainer_fix_batch_size in unsloth/models/rl_replacements.py is still being applied after this new logic.

The old check unconditionally forces per_device_train_batch_size to be a multiple of num_generations, which counteracts the goal of this pull request.

To ensure this change is effective, the old validation logic should be removed from the grpo_trainer_fix_batch_size function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No this is incorrect. It only forces the constraint in a specific case.

Expand Down
1 change: 1 addition & 0 deletions unsloth/models/rl_replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ def compute_loss(
# TRL warns if batch size is not a multiple of num_generations -> fix this.
def grpo_trainer_fix_batch_size(RLTrainer_source, RLConfig_source):
if "divisible by the number of generations" not in RLTrainer_source:
# in later trl versions this doesn't exist anymore
return ""
if "num_generations" not in RLConfig_source:
return ""
Expand Down