Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Generic efficient GRPO

Daniel Han33f20f028922
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 9db8abdd430..d0e3aeb144c 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -1131,6 +1131,7 @@ def CausalLM_fast_forward(fast_forward_inference):
if os.environ.get("UNSLOTH_RETURN_HIDDEN_STATES", "0") == "1":
if num_logits_to_keep != 0:
hidden_states = hidden_states[:, -num_logits_to_keep:, :]
+ hidden_states.__is_hidden_state = True
return CausalLMOutputWithPast(
loss = None,
logits = hidden_states,
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index ebc3549273e..0056f275093 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -248,10 +248,11 @@ RL_FUNCTIONS["grpo_trainer"].append(grpo_trainer__move_model_to_vllm)
# Edit _get_per_token_logps to handle mixed precision
def grpo_trainer__get_per_token_logps(function_name, function):
- if function_name != "_get_per_token_logps": return function
+ if function_name != "_get_per_token_logps": return function
- def _get_per_token_logps(self, model, input_ids, attention_mask, logits_to_keep, calc_logprob_flag = None):
- if os.environ.get('UNSLOTH_USE_NEW_MODEL', '0') == '0' and not calc_logprob_flag:
+ def _get_per_token_logps(self, model, input_ids, attention_mask, logits_to_keep):
+ if True: #os.environ.get('UNSLOTH_USE_NEW_MODEL', '0') == '0':
+ print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
return None # Unsloth efficient GRPO
# Otherwise, calculate normally:
if not hasattr(self, '_autocast_dtype'):