Skip to content

Add Gemma 3 training chat template#5685

Merged
qgallouedec merged 3 commits into
huggingface:mainfrom
hwanython:feat/gemma3-training-chat-template
May 4, 2026
Merged

Add Gemma 3 training chat template#5685
qgallouedec merged 3 commits into
huggingface:mainfrom
hwanython:feat/gemma3-training-chat-template

Conversation

@hwanython

@hwanython hwanython commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds gemma3_training.jinja so that Gemma 3 can be trained with SFTConfig(assistant_only_loss=True) once the VLM gate is lifted (tracked separately).

Part of #5471. Follows the pattern of #5523 (Gemma/Gemma2).

Changes

File Change
trl/chat_templates/gemma3.jinja New — original template, for identity comparison in get_training_chat_template()
trl/chat_templates/gemma3_training.jinja New — training variant with {% generation %} / {% endgeneration %} around assistant output
trl/chat_template_utils.py Register the two constants, add a branch in get_training_chat_template()
tests/test_chat_template_utils.py Add gemma3 to TestGetTrainingChatTemplate parametrize (uses existing tiny-Gemma3ForConditionalGeneration fixture)
trl/chat_templates/README.md Document the two new templates

Verification

Per @qgallouedec on #5471: Gemma 3 doesn't support tool calling, so
prefix-preservation isn't required — only {% generation %} /
{% endgeneration %} markers.

pytest tests/test_chat_template_utils.py locally: 232 passed, 30 skipped.
All 12 TestGetTrainingChatTemplate cases run on gemma3; 2 skips
(test_new_chat_template_is_prefix_preserving,
test_behavior_unchanged_with_tools_with_system_message) are the
expected pytest.skip for tool-less templates.

Out of scope

The VLM guard in sft_trainer.py (the raise ValueError for
assistant_only_loss on VLMs) is intentionally not touched — per #5471
Notes, that is tracked separately.

Before submitting

  • Did you read the contributor guideline?
  • Did you make sure your changes have a corresponding new test?
  • Did you make sure to update the documentation with your changes?
  • Did you run all the tests with pytest tests/?
  • Did you write any new necessary tests?

Who can review?

@qgallouedec @casinca


Note

Low Risk
Low risk: adds new Gemma 3 chat template assets and wires them into template selection, with a small conditional branch and a test/doc update.

Overview
Adds Gemma 3 support to TRL’s training chat-template swapping by introducing gemma3.jinja (identity reference) and gemma3_training.jinja, which wraps assistant output in {% generation %} / {% endgeneration %} and keeps the <start_of_turn>model prompt cue outside the generation block for correct assistant_only_loss masking.

Registers the new templates in get_training_chat_template(), extends the chat-template docs/README to list Gemma 3, and adds Gemma 3 coverage to TestGetTrainingChatTemplate.

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

@casinca

casinca commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

I can't really "approve" but imho lgtm 👍

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

lgtm!

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

@qgallouedec qgallouedec merged commit 909d0c1 into huggingface:main May 4, 2026
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.

4 participants