Add Gemma 3 training chat template#5685
Merged
qgallouedec merged 3 commits intoMay 4, 2026
Merged
Conversation
Contributor
|
I can't really "approve" but imho lgtm 👍 |
|
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. |
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
gemma3_training.jinjaso that Gemma 3 can be trained withSFTConfig(assistant_only_loss=True)once the VLM gate is lifted (tracked separately).Part of #5471. Follows the pattern of #5523 (Gemma/Gemma2).
Changes
trl/chat_templates/gemma3.jinjaget_training_chat_template()trl/chat_templates/gemma3_training.jinja{% generation %}/{% endgeneration %}around assistant outputtrl/chat_template_utils.pyget_training_chat_template()tests/test_chat_template_utils.pygemma3toTestGetTrainingChatTemplateparametrize (uses existingtiny-Gemma3ForConditionalGenerationfixture)trl/chat_templates/README.mdVerification
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.pylocally: 232 passed, 30 skipped.All 12
TestGetTrainingChatTemplatecases run ongemma3; 2 skips(
test_new_chat_template_is_prefix_preserving,test_behavior_unchanged_with_tools_with_system_message) are theexpected
pytest.skipfor tool-less templates.Out of scope
The VLM guard in
sft_trainer.py(theraise ValueErrorforassistant_only_losson VLMs) is intentionally not touched — per #5471Notes, that is tracked separately.
Before submitting
pytest 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) andgemma3_training.jinja, which wraps assistant output in{% generation %}/{% endgeneration %}and keeps the<start_of_turn>modelprompt cue outside the generation block for correctassistant_only_lossmasking.Registers the new templates in
get_training_chat_template(), extends the chat-template docs/README to list Gemma 3, and adds Gemma 3 coverage toTestGetTrainingChatTemplate.Reviewed by Cursor Bugbot for commit fc8a46d. Bugbot is set up for automated code reviews on this repo. Configure here.