Skip to content

feat: add Phi-3 training chat template with generation markers#5526

Merged
qgallouedec merged 7 commits into
huggingface:mainfrom
RudrenduPaul:feat/phi-3-training-chat-template
Apr 22, 2026
Merged

feat: add Phi-3 training chat template with generation markers#5526
qgallouedec merged 7 commits into
huggingface:mainfrom
RudrenduPaul:feat/phi-3-training-chat-template

Conversation

@RudrenduPaul

@RudrenduPaul RudrenduPaul commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

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 from microsoft/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 SFT

Changes to existing files:

  • trl/chat_template_utils.py: loads both templates, registers Phi-3 in get_training_chat_template(), updates docstring to mention Phi-3
  • tests/test_chat_template_utils.py: adds trl-internal-testing/tiny-Phi3ForCausalLM to TestGetTrainingChatTemplate

The 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

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you update the documentation with your changes?
  • Did you write any new necessary tests?

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 new phi3.jinja and a phi3_training.jinja variant 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 TestGetTrainingChatTemplate to 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.

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
Comment thread trl/chat_templates/phi3_training.jinja

@qgallouedec qgallouedec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, can you add phi3 to chat_template/README as well

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread trl/chat_templates/phi3_training.jinja
@RudrenduPaul

Copy link
Copy Markdown
Contributor Author

Thanks @qgallouedec for the approval! I'll add Phi-3 to chat_templates/README in the next push.

Re: the Cursor Bugbot finding — it's a valid call. The {% else %}{{ eos_token }}{% endif %} block in phi3_training.jinja does break the prefix-preserving property, and the other training templates (llama3_training, qwen2_5_training, qwen3_training, gptoss_training) all omit that else branch for exactly that reason. I'll align phi3_training.jinja with the rest of the training-template family by removing the else branch, so EOS is only emitted via the per-message <|end|> and <|endoftext|> markers already in the template. Will push the update along with the README change.

@RudrenduPaul

Copy link
Copy Markdown
Contributor Author

Both follow-ups addressed:

  1. Phi-3 added to chat_templates/README — the template entry is now in the README alongside Llama 3, Qwen2.5, and DeepSeek-V3.
  2. {% else %}{{ eos_token }}{% endif %} branch removedphi3_training.jinja now omits the else branch, matching the prefix-preserving pattern of llama3_training, qwen2_5_training, and qwen3_training.

Happy to adjust anything further.

@qgallouedec qgallouedec merged commit 644d173 into huggingface:main Apr 22, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants