Automatically add generation tags to chat template for assistant_only_loss=True training (TRL Issue #4879)#4900
Draft
Neelectric wants to merge 4 commits intohuggingface:mainfrom
Draft
Conversation
This is a first attempt at implementing feature request huggingface#4879, leaning heavily on https://github.com/HarryMayne/qwen_3_chat_templates
…in add_generation_tags()
qgallouedec
reviewed
Jan 26, 2026
| # https://huggingface.co/Qwen/Qwen3-8B/discussions/14 and https://github.com/HarryMayne/qwen_3_chat_templates | ||
| if tokenizer.chat_template == qwen3_chat_template: | ||
| tokenizer.chat_template = qwen3_chat_template_all_assistant | ||
| # probably want to add support here for most other popular model families like Llama 2/3/3.1, OLMo 2/3/3.1 etc... is there a way to do this fully automatically? |
Member
There was a problem hiding this comment.
I don't think we can do it 100% automatically, but I think that there are not thousands of different chat templates out there, if we support the top 5 chat templates, we may covers a good majority of use cases
Member
|
This looks solid so far. A couple things still missing:
Also, in Ideally, this feature wouldn’t mutate the tokenizer: we can avoid that by calling |
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?
This is a very first step at implementing the feature requested in #4879. For now the approach would be trying to recognise the model type and then replacing the chat template with a 'fixed' version. This would be helpful for the most common models (Qwen, Llama, OLMo etc) but I wonder if there's a more fancy way to go about this that automatically recognises when the assistant starts messaging, which we would want to train on.
Implements #4879 (feature request)
Before submitting
Pull Request section?
to it if that's the case.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@qgallouedec