Skip to content

Add idefics2 training chat template#6267

Open
nathon-lee wants to merge 8 commits into
huggingface:mainfrom
nathon-lee:add-idefics2-training-chat-template
Open

Add idefics2 training chat template#6267
nathon-lee wants to merge 8 commits into
huggingface:mainfrom
nathon-lee:add-idefics2-training-chat-template

Conversation

@nathon-lee

@nathon-lee nathon-lee commented Jul 3, 2026

Copy link
Copy Markdown

What does this PR do?

Adds Idefics2 support to TRL’s bundled training chat templates so get_training_chat_template() can return a training-compatible template for Idefics2 processors.

This PR adds:

  • idefics2.jinja: a reference copy of the Idefics2 chat template for identity matching.
  • idefics2_training.jinja: a training variant that wraps assistant output, including the trailing <end_of_utterance>, with {% generation %} / {% endgeneration %} markers.
  • Wiring in chat_template_utils.py so Idefics2 is recognized and mapped to the training template.
  • A TestGetTrainingChatTemplate parameter for trl-internal-testing/tiny-Idefics2ForConditionalGeneration.
  • Documentation updates listing Idefics2 as a supported chat-template family.

This enables assistant-only loss masking for Idefics2 via return_assistant_tokens_mask=True when SFT support for VLM assistant-only loss is available.

Part of #5471.

Before submitting

AI writing disclosure

We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.

  • 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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag @ qgallouedec since this contributes to #5471.

cc @qgallouedec @ kashif @namin @albertvillanova — would appreciate a review when you have time.


Note

Low Risk
Additive template and identity-matching logic following the existing Idefics3 pattern; no auth or core training-path refactors.

Overview
Adds Idefics2 to TRL’s bundled chat-template support so get_training_chat_template() can swap in a training-compatible template for Idefics2 processors (same pattern as Idefics3).

New idefics2.jinja and idefics2_training.jinja templates are added; the training variant splits assistant turns and wraps assistant text (through <end_of_utterance>) in {% generation %} / {% endgeneration %} for return_assistant_tokens_mask=True / assistant-only SFT loss. chat_template_utils.py loads both templates and maps a matching processor to the training template. TestGetTrainingChatTemplate gains a case for tiny-Idefics2ForConditionalGeneration, and docs list Idefics2 among supported families.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Idefics2 to TRL’s bundled chat-template families so get_training_chat_template() can return a training-compatible Idefics2 template with {% generation %} / {% endgeneration %} markers (enabling correct return_assistant_tokens_mask=True behavior once VLM assistant-only SFT is supported).

Changes:

  • Add idefics2.jinja (reference template) and idefics2_training.jinja (training variant wrapping assistant output in generation markers).
  • Wire Idefics2 into get_training_chat_template() via exact-template identity matching.
  • Extend the existing training-template test matrix to include trl-internal-testing/tiny-Idefics2ForConditionalGeneration and update documentation listings.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
trl/chat_templates/README.md Document Idefics2 reference + training template additions.
trl/chat_templates/idefics2.jinja Add reference Idefics2 template for identity matching.
trl/chat_templates/idefics2_training.jinja Add training variant with generation markers around assistant content.
trl/chat_template_utils.py Load new templates and map Idefics2 reference → training template in get_training_chat_template().
tests/test_chat_template_utils.py Add Idefics2 VLM to TestGetTrainingChatTemplate parametrization.
docs/source/chat_templates.md List Idefics2 as a recognized family and document the training template.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bot-ci-comment

bot-ci-comment Bot commented Jul 3, 2026

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.

@nathon-lee

Copy link
Copy Markdown
Author

Hi @qgallouedec, when you have a chance, could you please take a look at this PR? I believe it's ready for review. Thank you!

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