Skip to content

assistant_only_loss with Unsloth chat templates: fall back to response markers - #13204

Merged
danielhanchen merged 6 commits into
mainfrom
sft-tool-trajectories
Oct 10, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
sft-tool-trajectories

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

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 raises The chat template is not training-compatible otherwise (unsloth/Qwen3, Llama-3.2, gpt-oss, and the templates from get_chat_template).

With this PR, when TRL has no training template, SFTTrainer.__init__ sets _unsloth_assistant_mask_fallback instead of raising, skips TRL's stop-token check for that case, and the dataset is masked with train_on_responses_only markers (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 + tools dataset of multi-turn tool calls, trained on the assistant's tool calls and answers only.

Evidence

  • GPU SFT, 2 steps each, Qwen3-0.6B and Llama-3.2-1B with stock, upstream and Unsloth templates, plus 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.
  • Version skew (TRL 1.13): new unsloth + current zoo still raises a clear error at init for messages datasets (as today); text SFT / GRPO losses identical across all four unsloth x zoo combinations.
  • Regression smoke A/B (merge base vs this stack, TRL 1.13): 0 regressions.

Related: #10580

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-10T07:34:57.622117Z 2e37ae8 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-10T05:30:28.492154Z 58279b8 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1037 to +1040
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.')"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1046 to +1048
f"{i}except ValueError:\n"
f"{i} self.chat_template = None\n"
f"{i} self._unsloth_assistant_mask_fallback = True\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d30ef20: for a ProcessorMixin processing class the rewrite re-raises TRL's error, so vision SFT keeps today's behaviour. Test added.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread unsloth/models/rl_replacements.py Outdated
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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread unsloth/models/rl_replacements.py Outdated
Comment on lines +1048 to +1050
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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 0815f1f: with dataset_kwargs skip_prepare_dataset the fallback re-raises TRL's error, since Zoo's preparation never runs. Test added.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 0815f1f81d

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@danielhanchen
danielhanchen merged commit e7a1a91 into main Oct 10, 2026
@danielhanchen
danielhanchen deleted the sft-tool-trajectories branch October 10, 2026 14:22
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.

1 participant