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
Studio: train Phi-4-mini on its own chat template (#17312)
Nilay5ab7b904dfce
diff --git a/studio/backend/tests/test_phi4_mini_own_chat_template.py b/studio/backend/tests/test_phi4_mini_own_chat_template.pynew file mode 100644index 00000000000..a5380929cf5--- /dev/null+++ b/studio/backend/tests/test_phi4_mini_own_chat_template.py@@ -0,0 +1,70 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import pytest+from datasets import Dataset++from utils.datasets import apply_chat_template_to_dataset+from utils.datasets.completion_masking import lookup_manual_markers+from utils.datasets.model_mappings import MODEL_TO_TEMPLATE_MAPPER++_PHI4_MINI_TEMPLATE = (+ "{% for message in messages %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|end|>' }}"+ "{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>' }}{% else %}{{ eos_token }}{% endif %}"+)++_CONVERSATION = [+ {"role": "user", "content": "What is 2+2?"},+ {"role": "assistant", "content": "4"},+]+++def _tokenizer():+ from tokenizers import Tokenizer, models, pre_tokenizers+ from transformers import PreTrainedTokenizerFast++ vocab = {"[UNK]": 0, "<|endoftext|>": 1, "<|user|>": 2, "<|assistant|>": 3, "<|end|>": 4}+ backend = Tokenizer(models.WordLevel(vocab, unk_token = "[UNK]"))+ backend.pre_tokenizer = pre_tokenizers.Whitespace()+ tokenizer = PreTrainedTokenizerFast(+ tokenizer_object = backend,+ unk_token = "[UNK]",+ eos_token = "<|endoftext|>",+ pad_token = "<|endoftext|>",+ )+ tokenizer.chat_template = _PHI4_MINI_TEMPLATE+ return tokenizer+++ "name",+ [+ "unsloth/Phi-4-mini-instruct",+ "microsoft/Phi-4-mini-instruct",+ "unsloth/Phi-4-mini-instruct-unsloth-bnb-4bit",+ "unsloth/phi-4-mini-reasoning",+ "microsoft/Phi-4-mini-reasoning",+ ],+)+def test_phi4_mini_trains_on_its_own_chat_template(name):+ assert lookup_manual_markers(name) == (None, None, None)+ tokenizer = _tokenizer()+ expected = tokenizer.apply_chat_template(_CONVERSATION, tokenize = False)+ result = apply_chat_template_to_dataset(+ {+ "dataset": Dataset.from_dict({"messages": [_CONVERSATION]}),+ "final_format": "chatml_messages",+ "chat_column": "messages",+ "is_standardized": True,+ },+ tokenizer,+ model_name = name,+ num_proc = 1,+ )+ assert result["success"], result.get("errors")+ assert result["dataset"][0]["text"] == expected+++def test_phi4_keeps_the_phi4_template():+ assert MODEL_TO_TEMPLATE_MAPPER["unsloth/phi-4"] == "phi-4"+ assert MODEL_TO_TEMPLATE_MAPPER["unsloth/phi-4-reasoning"] == "phi-4"diff --git a/studio/backend/utils/datasets/model_mappings.py b/studio/backend/utils/datasets/model_mappings.pyindex 84ec18830ba..a5528396f7b 100644--- a/studio/backend/utils/datasets/model_mappings.py+++ b/studio/backend/utils/datasets/model_mappings.py@@ -32,14 +32,6 @@ TEMPLATE_TO_MODEL_MAPPER = {"unsloth/phi-4-reasoning-plus","microsoft/Phi-4-reasoning-plus","unsloth/phi-4-reasoning-plus-bnb-4bit",- "unsloth/phi-4-mini-reasoning-unsloth-bnb-4bit",- "unsloth/phi-4-mini-reasoning",- "microsoft/Phi-4-mini-reasoning",- "unsloth/phi-4-mini-reasoning-bnb-4bit",- "unsloth/Phi-4-mini-instruct-unsloth-bnb-4bit",- "unsloth/Phi-4-mini-instruct",- "microsoft/Phi-4-mini-instruct",- "unsloth/Phi-4-mini-instruct-bnb-4bit",),"mistral": ("unsloth/mistral-7b-instruct-v0.1-bnb-4bit",