Repository navigation
feat: add Mistral training chat template with generation markers - #6689
Open
RudrenduPaul wants to merge 1 commit into
Open
RudrenduPaul wants to merge 1 commit into
RudrenduPaul wants to merge 1 commit into
Conversation
Adds a training-compatible variant of the Mistral chat template (as shipped
by mistralai/Mistral-7B-Instruct-v0.3) with {% generation %} / {% endgeneration
%} markers, enabling return_assistant_tokens_mask=True for assistant-only
loss masking in SFT training. No prefix-preservation fix is needed: the
generic supports_tool_calling() probe omits tool_call.id, which this
template requires (9-char alphanumeric), so it already raises and is
correctly classified as not supporting tool calling.
Also adds the tiny-model generator script for
trl-internal-testing/tiny-MistralForCausalLM-0.3 so the new dispatch branch
can get automated test coverage once a maintainer pushes it.
Contributes to huggingface#5471.
5 of 6 tasks
This branch has not been deployed
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 a training-compatible variant of the Mistral chat template (as shipped by
mistralai/Mistral-7B-Instruct-v0.3and related checkpoints) with{% generation %}/{% endgeneration %}markers, enablingreturn_assistant_tokens_mask=Truefor assistant-only loss masking in SFT training.Contributes to #5471 (Tracking: add
{% generation %}chat templates for common model families).Files changed:
trl/chat_templates/mistral.jinja— original Mistral chat template (source of truth for the exact-match comparison inget_training_chat_template()); verified byte-identical to the livemistralai/Mistral-7B-Instruct-v0.3tokenizer'schat_templatetrl/chat_templates/mistral_training.jinja— training variant: same text output as the original, with{% generation %}/{% endgeneration %}wrapping both assistant branches (the[TOOL_CALLS] ...branch and the plain-content branch)trl/chat_template_utils.py— loads both templates; adds a Mistral branch (alphabetically between LLaMA 3 and Phi-3) inget_training_chat_template(); updates the docstring's supported-model listscripts/generate_tiny_models/for_causal_lm/mistral_for_causal_lm_0_3.py— generator script for atrl-internal-testing/tiny-MistralForCausalLM-0.3fixture, following the existingmistral_for_causal_lm_0_1.py/_0_2.pypatternTemplate design: Mistral's v0.3 template supports tool calling (
[AVAILABLE_TOOLS]/[TOOL_CALLS]/[TOOL_RESULTS]), but everytool_call.id(andtool_call_id) must be a 9-character alphanumeric string or the template raises. TRL'ssupports_tool_calling()probe uses a generic tool call with noid, so it hits that raise and correctly classifies this template as not supporting tool calling — which meansget_training_chat_template()'sprefix_okcheck short-circuits toTrueand no prefix-preservation fix is required, only the generation markers.Testing note: No
trl-internal-testing/tiny-Mistral*fixture currently carries the v0.3-era tool-calling template (the existingtiny-MistralForCausalLM-0.1/-0.2fixtures carry the older, simplerv0.1/v0.2template, which has no tool-calling support at all and predates this one). I've included the generator script above so a maintainer can pushtrl-internal-testing/tiny-MistralForCausalLM-0.3(same flow as noted earlier in #5471 for other new-model fixtures); happy to add theTestGetTrainingChatTemplate/TestSupportsToolCallingparametrize entries in a follow-up once it's live.In the meantime, I validated the new template directly against the real
mistralai/Mistral-7B-Instruct-v0.3tokenizer, mirroring the existing test suite's own checks:mistral.jinjais byte-identical to the livechat_templatesupports_tool_calling()isFalse(as expected, see above)get_training_chat_template()returns the new patched template, which carries{% generation %}markerstool_calls, and system-message + tools conversationsis_chat_template_stop_token_trained()passesreturn_assistant_tokens_mask=Trueproduces masks that decode to exactly the assistant-authored spans, for both plain-content and tool-call turnsBefore submitting
{% generation %}chat templates for common model families #5471trl-internal-testing/tiny-MistralForCausalLM-0.3is pushedAI writing disclosure
Who can review?
@qgallouedec (opened #5471)
Note
Low Risk
Changes are limited to chat-template assets, routing in
chat_template_utils.py, and a dev-only tiny model script; no runtime training or auth paths are modified.Overview
Adds Mistral Instruct v0.3 chat templates so TRL can patch SFT training the same way as other model families (#5471).
mistral.jinjamirrors the upstreammistralai/Mistral-7B-Instruct-v0.3template ([INST],[AVAILABLE_TOOLS],[TOOL_CALLS],[TOOL_RESULTS]).mistral_training.jinjais identical for rendered text but wraps both assistant paths (plain content andtool_calls) in{% generation %}/{% endgeneration %}forreturn_assistant_tokens_mask=True. No prefix-preservation patch is added becausesupports_tool_calling()fails on the stock probe (missing 9-chartool_call.id).get_training_chat_template()inchat_template_utils.pyloads both templates and returns the training variant when the tokenizer template matchesmistral.jinja; docs intrl/chat_templates/README.mdare updated accordingly.A
mistral_for_causal_lm_0_3.pytiny-model generator script is added for a futuretrl-internal-testing/tiny-MistralForCausalLM-0.3fixture; automated test parametrization is not in this PR.Reviewed by Cursor Bugbot for commit e1c0895. Bugbot is set up for automated code reviews on this repo. Configure here.