Skip to content

feat: add Gemma/Gemma2 training chat templates with generation markers#5523

Merged
qgallouedec merged 11 commits into
huggingface:mainfrom
ps-abhi:gemma-training-template
Apr 22, 2026
Merged

feat: add Gemma/Gemma2 training chat templates with generation markers#5523
qgallouedec merged 11 commits into
huggingface:mainfrom
ps-abhi:gemma-training-template

Conversation

@ps-abhi

@ps-abhi ps-abhi commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds {% generation %} markers for Gemma/Gemma2 training chat templates. Part of #5471.

  • gemma.jinja: unmodified reference template (from google/gemma-7b-it).
  • gemma_training.jinja: training variant. Split the unified output line into role-specific branches so <start_of_turn>model\n stays outside the generation block and assistant content + <end_of_turn>\n goes inside.
  • Modified get_training_chat_template to return the training variant.
  • Gemma and Gemma2 ship identical chat templates, so one file + one branch covers both.
  • New TestGetTrainingChatTemplateGemma class, parametrized over both tiny fixtures, asserting text equivalence vs the original and mask correctness across multi-turn conversations.

One extra change worth flagging: reordered the and in the no-patching-needed check so the substring check runs before is_chat_template_prefix_preserving. Gemma's template raises TemplateError on tool-role probes, and without the reorder the Gemma branch is unreachable. Happy to move this to a separate PR if preferred.

Before submitting

AI writing disclosure

  • No AI usage: the PR was written entirely by a human.
  • AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • AI-generated: the PR was mostly or fully generated by an AI tool.

Who can review?

@qgallouedec (created #5471)


Note

Medium Risk
Medium risk because it changes chat-template selection logic and tool-calling detection fallbacks, which can affect how prompts/masks are generated during training across multiple model families.

Overview
Adds Gemma/Gemma2 support to TRL’s chat-template patching by introducing a reference gemma.jinja and a new gemma_training.jinja that wraps only the assistant-generated portion in {% generation %} markers (keeping <start_of_turn>model\n outside) for correct assistant_only_loss masking.

Updates get_training_chat_template() to recognize Gemma templates and to skip prefix-preservation checks for templates that don’t support tool messages, and hardens supports_tool_calling() with a TypeError fallback for templates that reject dict tool arguments (notably DeepSeek-V3). Tests and docs are adjusted accordingly (new Gemma/Gemma2 fixtures in TestGetTrainingChatTemplate, skips for tool-less templates, and removal of the DeepSeek tool-calling xfail).

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

Comment thread tests/test_chat_template_utils.py Outdated
@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.

@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 b4e5485. 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 3256995 into huggingface:main Apr 22, 2026
1 check 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