Skip to content

Support assistant-only loss for the DeepSeek-V4.1-Flash chat templates - #8569

Open
albertvillanova wants to merge 1 commit into
mainfrom
support-deepseek-v4-1-assistant-only-loss
Open

albertvillanova wants to merge 1 commit into
mainfrom
support-deepseek-v4-1-assistant-only-loss

Conversation

@albertvillanova

@albertvillanova albertvillanova commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

This PR makes assistant_only_loss=True work for the chat templates shipped by deepseek-ai/DeepSeek-V4.1-Flash and deepseek-ai/DeepSeek-V4.1-Flash-1017.

Part of #7962.

Motivation

TRL does not store these templates, and they have no {% generation %} markers, so get_training_chat_template raises and SFTTrainer with assistant_only_loss=True fails at init.

Changes

  • Store the two templates as deepseekv4_1_flash.jinja and deepseekv4_1_flash_1017.jinja, and add a training template for each that wraps the assistant output (reasoning and </think>, content, tool calls and <|end▁of▁sentence|>) with {% generation %} markers. The generation prompt (<|Assistant|><think>, or </think> with thinking off) stays outside.
  • In the DeepSeek-V4.1-Flash training template, don't count a user message holding only tool results as a new user turn, as the DeepSeek-V4.1-Flash-1017 template does. In the original, appending a tool result when no tools are passed re-renders the earlier assistant turn without its thinking block (drop_thinking), which breaks prefix-preservation.
  • Run the training template suite with both templates on the tiny DeepSeek-V3 tokenizer, as for DeepSeek-V3.1 and V3.2-Exp. transformers has no DeepSeek-V4.1 model yet (Add DeepSeek-V4.1-Flash transformers#50919), so there is no tiny V4.1 model; the tiny V3 tokenizer lacks some special tokens of the V4.1 template (e.g. <|System|>), but not the ones at the boundaries of the trained span.
  • Document the templates and add both repos to the chat template drift check.

On the real tokenizers of both checkpoints, the training templates render like the originals (with and without thinking, with tools, multi-turn), the trained span of the last turn starts right after the generation prompt, and it ends with <|end▁of▁sentence|>, the checkpoints' stop token.


Note

Low Risk
Adds new Jinja templates and registry entries following existing DeepSeek patterns; impact is limited to users training those checkpoints with TRL’s template swapping.

Overview
Adds DeepSeek-V4.1-Flash and DeepSeek-V4.1-Flash-1017 to TRL’s bundled chat templates so get_training_chat_template can swap in patched variants when assistant_only_loss=True (SFT) or tools are used (GRPO).

Reference templates (deepseekv4_1_flash.jinja, deepseekv4_1_flash_1017.jinja) mirror the Hub checkpoints. Training templates wrap assistant spans (reasoning, content, tool calls, <|end▁of▁sentence|>) in {% generation %} / {% endgeneration %}. The Flash training template also treats user messages that only carry tool results as not starting a new user turn, fixing prefix-preservation when drop_thinking would otherwise re-render prior assistant turns without thinking.

Wiring in chat_template_utils.py, docs, drift check repos, and pytest cases for the tiny DeepSeek-V3 tokenizer complete the integration.

Reviewed by Cursor Bugbot for commit 89c2749. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

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.

Pages changed in this PR:

@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 Albert, LGTM

),
pytest.param(
("trl-internal-testing/tiny-DeepseekV3ForCausalLM", deepseekv4_1_flash_chat_template),
id="deepseekv4.1-flash",

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.

To match the ids in #8563 (deepseek-v3, deepseek-v3-v5, ...):

Suggested change
id="deepseekv4.1-flash",
id="deepseek-v4.1-flash",

),
pytest.param(
("trl-internal-testing/tiny-DeepseekV3ForCausalLM", deepseekv4_1_flash_1017_chat_template),
id="deepseekv4.1-flash-1017",

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.

Suggested change
id="deepseekv4.1-flash-1017",
id="deepseek-v4.1-flash-1017",

This branch has not been deployed

No deployments
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.

2 participants