feat: add Phi-3 training chat template with generation markers#5526
Conversation
Add training chat template for Microsoft Phi-3 with {% generation %} markers
for SFT assistant-only loss masking. Part of huggingface#5471.
Built by Rudrendu Paul, developed with Claude Code
qgallouedec
left a comment
There was a problem hiding this comment.
thanks, can you add phi3 to chat_template/README as well
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Per @qgallouedec review: add phi3 entry to chat_templates/README.md. Fix prefix-preserving issue flagged by Cursor — remove else/eos_token branch that breaks prefix preservation (matching llama3/qwen2_5 pattern). Merge upstream/main to resolve conflict. Built by Rudrendu Paul, developed with Claude Code
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 18f5cfa. Configure here.
|
Thanks @qgallouedec for the approval! I'll add Phi-3 to Re: the Cursor Bugbot finding — it's a valid call. The |
|
Both follow-ups addressed:
Happy to adjust anything further. |

What does this PR do?
Adds a training-compatible chat template for Microsoft Phi-3 models (e.g.
microsoft/Phi-3-mini-4k-instruct), following the same pattern introduced for LLaMA 3 in #5493, GPT-OSS, and Qwen2.5 in #5522.Files added:
trl/chat_templates/phi3.jinja— exact copy of the official Phi-3 tokenizer template (sourced frommicrosoft/Phi-3-mini-4k-instruct)trl/chat_templates/phi3_training.jinja— training variant with{% generation %}/{% endgeneration %}markers wrapping all assistant output for assistant-only loss masking in SFTChanges to existing files:
trl/chat_template_utils.py: loads both templates, registers Phi-3 inget_training_chat_template(), updates docstring to mention Phi-3tests/test_chat_template_utils.py: addstrl-internal-testing/tiny-Phi3ForCausalLMtoTestGetTrainingChatTemplateThe training template handles the Phi-3 assistant message format (
<|assistant|>\n{content}<|end|>\n), wrapping the content in{% generation %}/{% endgeneration %}markers so SFT trainers can apply loss only on assistant tokens.Closes part of #5471 (tracking issue for training chat templates).
Before submitting
AI writing disclosure
I used AI assistance (Claude Code) to draft and implement this change.
Who can review?
@qgallouedec
Note
Low Risk
Low risk: adds a new model-specific training chat template and wires it into template selection logic, with minimal impact on existing models. Main risk is incorrect template matching/formatting for Phi-3 affecting SFT masking for that model only.
Overview
Adds Phi-3 support to
get_training_chat_template()by loading newphi3.jinjaand aphi3_training.jinjavariant that wraps assistant output (and final EOS when appropriate) in{% generation %}markers for assistant-only loss masking.Updates documentation to describe the new Phi-3 training template, and extends
TestGetTrainingChatTemplateto include a Phi-3 tiny tokenizer in the existing behavior/masking test suite.Reviewed by Cursor Bugbot for commit 91f5b79. Bugbot is set up for automated code reviews on this repo. Configure here.