Repository navigation
Vision SFT: assistant_only_loss uses Unsloth's responses-only masking - #17942
Conversation
… in UnslothVisionDataCollator
Codex Review Summary: ✅ CompletedThis 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 |
There was a problem hiding this comment.
💡 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".
| " )\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" |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
|
@codex review |
There was a problem hiding this comment.
💡 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".
| " 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" |
There was a problem hiding this comment.
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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
…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 |
Fixes #17847.
assistant_only_loss=Trueon 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:UnslothVisionDataCollator(images inline inmessages, 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.The SFT trainer now routes the flag to Unsloth's
train_on_responses_onlybefore TRL's__init__, on every transformers version:UnslothVisionDataCollatoris used.args.assistant_only_lossis 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 sameSFTConfigstill masks, and a later text run on it gets the flag back.assistant_only_lossare unchanged.Results
Kept labels with
assistant_only_loss=Trueon a 2-turn image chat, identical on transformers 5.17 and 5.18, each followed by one training step:A red square.<end_of_turn>\nIt is red.<end_of_turn>\nA red square.<|im_end|>It is red.<|im_end|>A red square.<|im_end|>\n<think>\n\n</think>\n\nIt is red.<|im_end|>\n, same as mainOn a GPU, the issue's own setup (no collator, an
imagescolumn,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 inversion-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).