Repository navigation
assistant_only_loss with Unsloth chat templates: fall back to response markers - #13204
Conversation
…TRL has no training template
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58279b8ba2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| f"{i}except ValueError:\n" | ||
| f"{i} self.chat_template = None\n" | ||
| f"{i} self._unsloth_assistant_mask_fallback = True\n" | ||
| f"{i} print('Unsloth: TRL has no training chat template for this tokenizer, so assistant_only_loss masks non-assistant tokens with train_on_responses_only markers.')" |
There was a problem hiding this comment.
Require the zoo version that consumes this fallback flag
When the installed unsloth_zoo is the currently permitted 2026.10.3 release, sft_prepare_dataset never reads _unsloth_assistant_mask_fallback (that support is only in the referenced zoo #1663), yet pyproject.toml still allows unsloth_zoo>=2026.10.3. Consequently, after this handler suppresses TRL's error, a conversational dataset that also contains a rendered text column is tokenized without assistant_masks, so assistant_only_loss=True silently trains on user/tool tokens as well; a messages-only dataset instead fails later during preparation. Please raise the dependency floor to the first zoo release containing the consumer, or implement/gate the masking locally.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 35d8f5d: the rewrite only applies when the installed unsloth_zoo's sft_prepare_dataset reads the flag; with an older Zoo, TRL's error stands. Test added.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 35d8f5dd59
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| f"{i}except ValueError:\n" | ||
| f"{i} self.chat_template = None\n" | ||
| f"{i} self._unsloth_assistant_mask_fallback = True\n" |
There was a problem hiding this comment.
Keep the marker fallback out of vision SFT
When assistant_only_loss=True is used with a conversational VLM dataset containing image/images and an unsupported Unsloth template, this catch also suppresses TRL's error even though the Zoo marker fallback is a text-dataset preparation path. TRL skips that preparation for vision datasets and its vision collator rebuilds labels from return_assistant_tokens_mask=True; because self.chat_template is now None, the original marker-less template produces no assistant mask, leaving the batch fully masked instead of training assistant turns. Gate this fallback off for vision datasets or configure a vision collator that applies the response markers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d30ef20: for a ProcessorMixin processing class the rewrite re-raises TRL's error, so vision SFT keeps today's behaviour. Test added.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d30ef20f73
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| f"{i} self.chat_template = get_training_chat_template(processing_class)\n" | ||
| f"{i}except ValueError:\n" | ||
| # Vision datasets skip Zoo's text preparation and mask from TRL's template, so keep TRL's error. | ||
| f"{i} if any(c.__name__ == 'ProcessorMixin' for c in type(processing_class).__mro__): raise\n" |
There was a problem hiding this comment.
Gate the fallback on the dataset instead of the processor
This proxy misclassifies both supported cases: a VLM trained on a text-only conversational dataset still uses a ProcessorMixin, so the marker fallback is unnecessarily rejected even though Zoo preparation runs; conversely, a vision dataset may use a bare tokenizer, so this check suppresses TRL's error and again leaves the vision path without valid assistant masks. Fresh evidence beyond the prior thread is unsloth/models/rl.py:2087-2117, which explicitly makes preparation/collation depend on _is_vision_dataset, and unsloth/models/rl.py:3516-3530, which explicitly supports VLM vision datasets passed a bare tokenizer. Check self._is_vision_dataset here rather than inferring the dataset type from processing_class.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 1af02a4: the fallback now re-raises when self._is_vision_dataset is set (TRL sets it before the template swap), instead of keying on the processor class.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1af02a4fa8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| f"{i} if getattr(self, '_is_vision_dataset', False): raise\n" | ||
| f"{i} self.chat_template = None\n" | ||
| f"{i} self._unsloth_assistant_mask_fallback = True\n" |
There was a problem hiding this comment.
Keep the fallback off when dataset preparation is skipped
With dataset_kwargs={"skip_prepare_dataset": True}, this catch still suppresses TRL's error even though SFTTrainer.__init__ bypasses _prepare_dataset, so Zoo never consumes _unsloth_assistant_mask_fallback. For a pretokenized conversational dataset containing messages and input_ids but no prebuilt masked labels, the default language-modeling collator consequently uses every input token as a label and silently trains on user/tool tokens despite assistant_only_loss=True; preserve TRL's error when preparation is skipped, or require/apply masked labels in that path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 0815f1f: with dataset_kwargs skip_prepare_dataset the fallback re-raises TRL's error, since Zoo's preparation never runs. Test added.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
pre-commit.ci run |
SFTConfig(assistant_only_loss=True)fails on every Unsloth chat template: TRL swaps in a training template only for templates it knows by exact text and raisesThe chat template is not training-compatibleotherwise (unsloth/Qwen3, Llama-3.2, gpt-oss, and the templates fromget_chat_template).With this PR, when TRL has no training template,
SFTTrainer.__init__sets_unsloth_assistant_mask_fallbackinstead of raising, skips TRL's stop-token check for that case, and the dataset is masked withtrain_on_responses_onlymarkers (unslothai/unsloth-zoo#1663). Templates TRL recognises keep TRL's path, and with the TRL tool-template PR Unsloth's Qwen3 templates get TRL's own training template. No chat template is edited, so GGUF / Ollama export is unchanged.This is what SFT on agent trajectories needs: a
messages+toolsdataset of multi-turn tool calls, trained on the assistant's tool calls and answers only.Evidence
train_on_responses_only: tool calls and answers trained, tool results and user turns masked in every run.tests/test_sft_assistant_mask_fallback.py: 4 CPU tests, TRL 1.13 and 1.15.messagesdatasets (as today); text SFT / GRPO losses identical across all four unsloth x zoo combinations.Related: #10580