From d52341f06bfbd850d5cb10d187edf067ee5e7b8c Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Thu, 6 Feb 2025 01:05:48 -0800 Subject: [PATCH] Update rl.py --- unsloth/models/rl.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index 13c2a62f1d..ef2fcb5675 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -362,6 +362,9 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): spaces = source.find("def") source = source.split("\n") source = "\n".join(x[spaces:] for x in source) + + # Replace function name with _unsloth_... + source = source.replace("def ", "def _unsloth_", 1) changed[function] = source pass @@ -372,8 +375,8 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): # Patch all functions for function in changed: - exec(changed[function]) - exec(f"trl.trainer.{trainer_file}.{RLTrainer_name}.{function} = {function}") + exec(changed[function], locals(), globals()) + exec(f"trl.trainer.{trainer_file}.{RLTrainer_name}.{function} = _unsloth_{function}", locals(), globals()) pass pass