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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix VLM GRPO matmul shape mismatch in _get_per_token_logps_and_entropies (#4301)
Daniel Han11449208f41c
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.pyindex 314feb5d2ae..805086324eb 100755--- a/unsloth/models/rl_replacements.py+++ b/unsloth/models/rl_replacements.py@@ -26,7 +26,11 @@ import torchimport inspectimport linecachefrom collections import defaultdict-from unsloth_zoo.rl_replacements import RL_REPLACEMENTS, left_pack_padding+from unsloth_zoo.rl_replacements import (+ RL_REPLACEMENTS,+ left_pack_padding,+ chunked_selective_log_softmax,+)from unsloth_zoo.utils import Versionfrom trl import __version__ as trl_version_rawfrom importlib.metadata import version as importlib_version@@ -859,6 +863,18 @@ def grpo_trainer__get_per_token_logps_and_entropies(function_name, function)::, -(logits_to_keep + max_left_pad + 1) :, :]logits_chunk = logits_chunk[:, :-1, :]+ logprobs_chunk = (+ chunked_hidden_states_selective_log_softmax(+ logits_chunk,+ lm_head,+ completion_input_ids_chunk,+ chunks = input_ids_chunk.shape[0] * multiplier,+ logit_scale_multiply = logit_scale_multiply,+ logit_scale_divide = logit_scale_divide,+ logit_softcapping = logit_softcapping,+ temperature = temperature,+ )+ )else:# Essentially, for VLMs we do not go via the optimized path in models/,# so we don't encounter the Flash Attn left-padding issue.@@ -876,17 +892,27 @@ def grpo_trainer__get_per_token_logps_and_entropies(function_name, function):completion_input_ids_chunk = input_ids_chunk[:, -logits_to_keep:]-- logprobs_chunk = chunked_hidden_states_selective_log_softmax(- logits_chunk,- lm_head,- completion_input_ids_chunk,- chunks = input_ids_chunk.shape[0] * multiplier,- logit_scale_multiply = logit_scale_multiply,- logit_scale_divide = logit_scale_divide,- logit_softcapping = logit_softcapping,- temperature = temperature,- )+ # Guard: check if model returned hidden states or logits+ if logits_chunk.shape[-1] == lm_head.shape[1]:+ logprobs_chunk = (+ chunked_hidden_states_selective_log_softmax(+ logits_chunk,+ lm_head,+ completion_input_ids_chunk,+ chunks = input_ids_chunk.shape[0] * multiplier,+ logit_scale_multiply = logit_scale_multiply,+ logit_scale_divide = logit_scale_divide,+ logit_softcapping = logit_softcapping,+ temperature = temperature,+ )+ )+ else:+ # Model returned logits directly - scaling/softcapping already applied by model forward+ logprobs_chunk = chunked_selective_log_softmax(+ logits_chunk,+ completion_input_ids_chunk,+ temperature,+ )# This is needed to avoid race conditions with GPT OSS offload_embbed=True# However, it seems that this line does not slow down or disrupt models.device_synchronize()