feat: add DeepSeek-V3 training chat template with generation markers#5527
Merged
qgallouedec merged 3 commits intoApr 14, 2026
Merged
Conversation
Add training chat template for DeepSeek-V3 with {% generation %} markers
for SFT assistant-only loss masking. Part of huggingface#5471.
Built by Rudrendu Paul, developed with Claude Code
|
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. |
Member
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
24 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds a training-compatible chat template for DeepSeek-V3 models (e.g.
deepseek-ai/DeepSeek-V3), following the same pattern introduced for LLaMA 3 in #5493, Qwen2.5 in #5522, and GPT-OSS.Files added:
trl/chat_templates/deepseekv3.jinja— exact copy of the official DeepSeek-V3 tokenizer template (sourced fromtrl-internal-testing/tiny-DeepseekV3ForCausalLM)trl/chat_templates/deepseekv3_training.jinja— training variant with{% generation %}/{% endgeneration %}markers wrapping all assistant output (plain content, tool-call, and post-tool-output branches) for assistant-only loss masking in SFTChanges to existing files:
trl/chat_template_utils.py: loads both templates, registers DeepSeek-V3 inget_training_chat_template(), updates docstringtests/test_chat_template_utils.py: addstrl-internal-testing/tiny-DeepseekV3ForCausalLMtoTestGetTrainingChatTemplateThe training template handles all three assistant message cases: (1) plain text response, (2) tool calls with optional content, (3) response after tool outputs — all wrapped in the generation markers.
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
Medium Risk
Medium risk because it changes template selection and rendering behavior for tool-calling/prefix checks; mistakes could subtly alter prompts or masking during training for DeepSeek-V3 users.
Overview
Adds DeepSeek-V3 support to
get_training_chat_template()by introducingdeepseekv3.jinja(identity match) and a newdeepseekv3_training.jinjavariant that wraps assistant output in{% generation %}markers and JSON-serializes tool-callarguments.Hardens prefix-preservation checks/tests around tool calling by adding a TypeError fallback when templates reject dict
arguments, and expands the test matrix to includetiny-DeepseekV3ForCausalLMwith a baseline workaround for the unpatched upstream template behavior.Reviewed by Cursor Bugbot for commit da51df2. Bugbot is set up for automated code reviews on this repo. Configure here.