From d9601bd14aee392c6958ed9cf807df69ec15b073 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sun, 22 Jun 2025 04:54:18 -0700 Subject: [PATCH] logits / temperature --- unsloth/models/rl.py | 11 +++++++++++ unsloth/models/rl_replacements.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index dde09e08e2..889bbd4807 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -568,6 +568,17 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"): extra_args += check_num_generations pass + # Check temperature must not be <= 0. Also stop if >= 10 + if "temperature" in call_args: + check_temperature = \ + "if temperature <= 0:\n"\ + " raise MathError('Unsloth: Please set a positive non-zero temperature since your results will be wrong.')\n"\ + "elif temperature >= 10:\n"\ + " raise MathError('Unsloth: Please set a positive non-zero temperature less than 10, since sampling will be quite erratic.')\n"\ + "\n" + extra_args += check_temperature + pass + # Edit config with anything extra if trainer_file in RL_CONFIG_CHANGES: process_extra_args = RL_CONFIG_CHANGES[trainer_file] diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py index b6f02af946..8efaf3fd1f 100644 --- a/unsloth/models/rl_replacements.py +++ b/unsloth/models/rl_replacements.py @@ -270,6 +270,8 @@ def grpo_trainer__get_per_token_logps(function_name, function): # See https://github.com/huggingface/trl/issues/2770 # logits = logits[:, -logits_to_keep:] # return logits + # See https://huggingface.co/blog/the_n_implementation_details_of_rlhf_with_ppo#policy-training-implementation-details + # logits = logits / self.temperature # logps = selective_log_softmax(logits, input_ids) # row_indices, col_indices = torch.where(logps < -20) @@ -358,6 +360,7 @@ def grpo_trainer_compute_loss(function_name, function): epsilon_high = self.epsilon_high, max_completion_length = self.args.max_completion_length, delta = self.args.delta, + temperature = self.args.temperature, ) else: if hasattr(self.args, "loss_type"): @@ -374,6 +377,7 @@ def grpo_trainer_compute_loss(function_name, function): epsilon_high = self.epsilon_high, max_completion_length = self.args.max_completion_length, delta = self.args.delta, + temperature = self.args.temperature, ) else: # to ensure backwards compatibility with trl 0.15.2 and maybe even 0.17 @@ -385,6 +389,7 @@ def grpo_trainer_compute_loss(function_name, function): advantages, old_hidden_states, n_chunks = self.args.unsloth_num_chunks, + temperature = self.args.temperature, ) # Log the metrics