Skip to content

feat: add Qwen2.5 training chat template with generation markers#5522

Merged
qgallouedec merged 5 commits into
huggingface:mainfrom
RudrenduPaul:feat/qwen2-5-training-chat-template
Apr 12, 2026
Merged

feat: add Qwen2.5 training chat template with generation markers#5522
qgallouedec merged 5 commits into
huggingface:mainfrom
RudrenduPaul:feat/qwen2-5-training-chat-template

Conversation

@RudrenduPaul

@RudrenduPaul RudrenduPaul commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a training-compatible chat template for Qwen2.5 models (e.g. Qwen/Qwen2.5-0.5B-Instruct), following the same pattern introduced for LLaMA 3 in #5493 and GPT-OSS.

Files added:

  • trl/chat_templates/qwen2_5.jinja — exact copy of the official Qwen2.5 ChatML tokenizer template (sourced from Qwen/Qwen2.5-0.5B-Instruct)
  • trl/chat_templates/qwen2_5_training.jinja — training variant with {% generation %} / {% endgeneration %} markers wrapping all assistant output (both plain and tool-call branches) for assistant-only loss masking in SFT

Changes to existing files:

  • trl/chat_template_utils.py: loads both templates, registers Qwen2.5 in get_training_chat_template(), updates docstring
  • tests/test_chat_template_utils.py: adds trl-internal-testing/tiny-Qwen2ForCausalLM-2.5 to TestGetTrainingChatTemplate

The training template handles all three assistant message cases: (1) plain text response, (2) tool calls with content, (3) tool calls without content — all wrapped in the generation markers.

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: additive template support and a small selector change, covered by existing chat-template compatibility tests. Main risk is mismatching the upstream Qwen2.5 template, which could affect prompt rendering for those models.

Overview
Adds Qwen2.5 support to get_training_chat_template() by loading a new qwen2_5.jinja base template and returning a new qwen2_5_training.jinja variant that wraps all assistant output (including tool-call branches) in {% generation %} / {% endgeneration %} for assistant-only loss masking.

Extends TestGetTrainingChatTemplate to include a Qwen2.5 tiny tokenizer, ensuring the patched template remains prefix-preserving and behavior-compatible with the original rendering.

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

RudrenduPaul and others added 2 commits April 10, 2026 23:23
Adds qwen2_5.jinja (official Qwen2.5 ChatML template) and
qwen2_5_training.jinja (training variant with {% generation %} /
{% endgeneration %} markers for assistant-only loss masking in SFT).

Registers both in chat_template_utils.py and adds Qwen2.5 to the
TestGetTrainingChatTemplate test suite.

Built by Rudrendu Paul, developed with Claude Code
@qgallouedec

Copy link
Copy Markdown
Member

@codex review

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

Comment thread trl/chat_template_utils.py Outdated
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Comment thread trl/chat_template_utils.py
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>

@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 814cb0d. Configure here.

Comment thread trl/chat_template_utils.py

@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!

@qgallouedec qgallouedec merged commit d6d5efc into huggingface:main Apr 12, 2026
1 check passed
@aazizyan aazizyan mentioned this pull request May 7, 2026
8 tasks
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