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.

Inject model reference for dynamic token_type_ids detection in SFTTrainer (#4012)

Daniel Hana2f4f04ea5d9
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 5ce9e5bfff4..32edcebaf8f 100755
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -1369,6 +1369,14 @@ def _patch_trl_rl_trainers(trainer_file = "grpo_trainer"):
)
RLTrainer_source = RLTrainer_source.replace(_sig_vlm_old, _sig_vlm_new)
+ # Inject model reference before _prepare_dataset for dynamic
+ # token_type_ids detection in sft_prepare_dataset
+ _prep_pattern = r"([ \t]*)train_dataset = self\._prepare_dataset\("
+ _prep_replacement = r"\1self._unsloth_model_ref = model\n\1train_dataset = self._prepare_dataset("
+ RLTrainer_source = re.sub(
+ _prep_pattern, _prep_replacement, RLTrainer_source, count = 1
+ )
+
# Silence TRL's noisy batch_size=1 + padding-free warning (handles both
# the original "anihilate" typo and the corrected "annihilate" spelling)
for _typo in ("anihilate", "annihilate"):