Skip to content

Vision SFT: assistant_only_loss uses Unsloth's responses-only masking - #17942

Merged
danielhanchen merged 3 commits into
mainfrom
vision-assistant-only-loss
Nov 16, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
vision-assistant-only-loss

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #17847.

assistant_only_loss=True on a vision SFT run either trained on every token or hit TRL's "Assistant-only loss for vision datasets requires transformers>=5.18.0" refusal:

  • With UnslothVisionDataCollator (images inline in messages, the documented Unsloth format), the collator builds its own labels and never read the flag. Qwen2-VL kept 50 / 56 tokens, Gemma3 41 / 299, including the user turns.
  • With no collator (the issue's setup), TRL refuses below 5.18. From 5.18 it relies on processor assistant masks.

The SFT trainer now routes the flag to Unsloth's train_on_responses_only before TRL's __init__, on every transformers version:

  • The user / assistant markers are auto-detected from the chat template (or taken from the tokenizer if already set), and the masker is attached to the collator. With no collator, an UnslothVisionDataCollator is used.
  • args.assistant_only_loss is then set to False so TRL neither refuses nor masks again. The request is remembered on the config, so a second trainer built on the same SFTConfig still masks, and a later text run on it gets the flag back.
  • A collator that already masks is not masked twice.
  • If the markers cannot be detected, nothing changes and TRL behaves exactly as before, with one line explaining how to pass the markers explicitly.
  • Text runs and runs without assistant_only_loss are unchanged.

Results

Kept labels with assistant_only_loss=True on a 2-turn image chat, identical on transformers 5.17 and 5.18, each followed by one training step:

model kept
Gemma3 A red square.<end_of_turn>\nIt is red.<end_of_turn>\n
Qwen2-VL A red square.<|im_end|>It is red.<|im_end|>
Qwen3-VL same as Qwen2-VL
Qwen3 (text, control) A red square.<|im_end|>\n<think>\n\n</think>\n\nIt is red.<|im_end|>\n, same as main

On a GPU, the issue's own setup (no collator, an images column, SFTConfig(assistant_only_loss=True)) trains on transformers 5.17 and 5.18 for Qwen2-VL, Qwen3-VL and Gemma3, keeping only the assistant text above (10 / 56, 10 / 105 and 12 / 299 tokens). On main the same setup on 5.17 stops with TRL's "requires transformers>=5.18.0" error.

AMD gfx1151 Linux and Windows: the earlier head of this PR masks only the assistant text on tiny Qwen2.5-VL and Gemma 3 and trains, with no regression in the tiny SFT run.

Tests

tests/version_compat/test_sft_assistant_only_loss_vision_routing.py (8 tests, added to both fake-run lists in version-compat-ci.yml, under 10 s each): main fails 6 on 5.17 and 5.18; this PR passes 8 / 8 on both. The CPU fake-train, GRPO, padding-free, config, preference and ORPO suites are unchanged (269 passed, 5 skipped on both versions).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T08:35:49.772440Z 08ef2ef Manual request
ℹ️ 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

@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: 8b536dee46

ℹ️ 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.py Outdated
" )\n"
# The collator masks now; TRL never applies the flag to a custom collator, and below transformers 5.18 it
# refuses it for vision datasets.
" args.assistant_only_loss = False\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.

P2 Badge Preserve the masking request when reusing an SFTConfig

When an SFTConfig(assistant_only_loss=True) is reused to construct another trainer with a fresh vision collator, the first construction permanently clears the flag on the caller's config. The second construction therefore skips installing the response masker and silently trains on user/system tokens. The backwards-compatible trainer wrapper preserves existing SFTConfig instances, so it does not isolate this mutation. Preserve the original masking request separately or clear the flag only on a trainer-local config so subsequent runs retain the requested loss semantics.

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 ef913ed: the request is now also recorded on the config (args._unsloth_assistant_only_loss), so a second trainer built on the same SFTConfig with a fresh collator still installs the masker, and a later text-only trainer on that config gets assistant_only_loss handed back to TRL. New test test_a_reused_config_still_masks_on_the_next_trainer fails before and passes now (transformers 5.17 and 5.18).

@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: ef913ed61d

ℹ️ 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.py Outdated
" if not getattr(data_collator, 'train_on_responses_only', None):\n"
" from unsloth_zoo.dataset_utils import train_on_responses_only as _unsloth_tro\n"
" try:\n"
" data_collator.train_on_responses_only = _unsloth_tro(None, tokenizer = __tokenizer, return_function = 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.

P2 Badge Use the collator's processor when processing_class is omitted

When constructing SFTTrainer with assistant_only_loss=True and a vision collator but without an explicit processing_class, __tokenizer is None: this generated block runs before TRL's initializer automatically loads the processor. Consequently, the masker raises “No chat_template to auto-detect from” even though data_collator.processor already contains a usable chat template. Use the collator's processor for this masking operation, or defer it until processing-class resolution, so the supported automatic-processor construction path works.

Useful? React with 👍 / 👎.

…L init, including runs without a collator

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 08ef2ef2cb

ℹ️ 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
danielhanchen merged commit 501ad46 into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the vision-assistant-only-loss branch November 16, 2026 09:58
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…ake-run floor red on main) (#17967)

## Summary

`GRPO fake-run (CPU spoof, TRL floor)` is red on main (seen on the
staging run of #17966): every test in
`tests/version_compat/test_sft_assistant_only_loss_vision_routing.py`,
added to that lane by #17942, fails on the floor pins (TRL 0.18.2,
transformers 4.52.4, torch 2.6.0):

```
AttributeError: 'SFTConfig' object has no attribute 'assistant_only_loss'
AttributeError: 'Qwen2VLProcessor' object has no attribute 'pad_token'
ValueError: The checkpoint you are trying to load has model type `qwen3_vl` but Transformers does not recognize this architecture.
AssertionError: assert [[151644, 894...]] == [[-100, -100, ...]]
```

TRL added `SFTConfig.assistant_only_loss` in 0.20. On TRL 0.18.2 the
flag does not exist (`dataclasses.fields(trl.SFTConfig)` has only
`completion_only_loss`), so a user cannot ask for assistant-only loss
and #17942's routing has nothing to act on. The test still builds
configs with the flag and expects masked labels, so every case fails on
that lane. #17942's code is fine; the test assumed a TRL with the flag.
Separately, Qwen3-VL does not exist in transformers 4.52, so that
parameter cannot load there.

## Change

Test only,
`tests/version_compat/test_sft_assistant_only_loss_vision_routing.py`:

- Skip the module, with the reason, when the installed TRL's `SFTConfig`
has no `assistant_only_loss` field.
- `_load` skips a model whose architecture the installed transformers
does not know (it already skipped Hub fetch failures), instead of
failing the parameter.

## Testing

| lane | before | after |
|---|---|---|
| floor: TRL 0.18.2, transformers 4.52.4, torch 2.6.0 | 8 failed | 1
skipped (module, reason printed) |
| TRL 1.15, transformers 5.17 | 8 passed | 8 passed |
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.

[Feature] Bump the upper version bound of transformers to 5.18

1 participant