From d8a29af1cbb3db86fa5c8c5b29ef0e27d176d4ff Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sat, 21 Jun 2025 22:14:21 -0700 Subject: [PATCH] Fix DAPO, TRL 0.19.0 --- unsloth/models/rl.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index 69785be206..8ebb5c824b 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -471,6 +471,7 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): "num_generations" : 8, "top_k" : None, "vllm_mode" : "colocate", + "generation_kwargs" : {}, } for k, v in replacements.items(): x = f"{k}( = [^,\n]{{1,}})?,\n" @@ -510,6 +511,8 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): # Check for loss_type = dr_grpo and scale_rewards for GRPO if "loss_type" in call_args and "scale_rewards" in call_args: + # See https://github.com/huggingface/trl/issues/3130#issuecomment-2746947835 + # DAPO uses per token loss so BNPO loss used check_dr_grpo = \ "if loss_type.lower() == 'dr_grpo':\n"\ " loss_type = 'dr_grpo'\n"\ @@ -519,13 +522,16 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): " if scale_rewards == None:\n"\ " scale_rewards = True\n"\ " elif scale_rewards == True:\n"\ - " print('The Dr GRPO paper recommends setting `scale_rewards` to False! Will override. Set it to `None` to force False.')\n"\ + " print('Unsloth: The Dr GRPO paper recommends setting `scale_rewards` to False! Will override. Set it to `None` to force False.')\n"\ " scale_rewards = False\n"\ "elif loss_type.lower() == 'dapo':\n"\ - " print('The DAPO paper recommends `mask_truncated_completions = True`')\n"\ - " print('The DAPO paper recommends `epsilon_high = 0.28`')\n"\ + " print('Unsloth: The DAPO paper recommends `mask_truncated_completions = True`')\n"\ + " print('Unsloth: The DAPO paper recommends `epsilon_high = 0.28`')\n"\ + " print('Unsloth: The DAPO paper recommends setting `beta = 0.0` to remove the KL term')\n"\ " mask_truncated_completions = True\n"\ " epsilon_high = 0.28\n"\ + " beta = 0.0\n"\ + " loss_type = 'bnpo'" "\n" extra_args += check_dr_grpo pass