Add idefics2 training chat template#6267
Conversation
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
…late-test feat: add Idefics2 training chat template
There was a problem hiding this comment.
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) andidefics2_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-Idefics2ForConditionalGenerationand 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.
|
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. |
|
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! |
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:
<end_of_utterance>, with{% generation %}/{% endgeneration %}markers.TestGetTrainingChatTemplateparameter fortrl-internal-testing/tiny-Idefics2ForConditionalGeneration.This enables assistant-only loss masking for Idefics2 via
return_assistant_tokens_mask=Truewhen SFT support for VLM assistant-only loss is available.Part of #5471.
Before submitting
{% generation %}chat templates for common model families #5471AI 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.
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.jinjaandidefics2_training.jinjatemplates are added; the training variant splits assistant turns and wraps assistant text (through<end_of_utterance>) in{% generation %}/{% endgeneration %}forreturn_assistant_tokens_mask=True/ assistant-only SFT loss.chat_template_utils.pyloads both templates and maps a matching processor to the training template.TestGetTrainingChatTemplategains a case fortiny-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.