Fix DAPO, TRL 0.19.0

This commit is contained in:
Daniel Han 2025-06-21 22:14:21 -07:00
commit d8a29af1cb

View file

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