Repository navigation
Support for Seq2Seq Models (T5, T5Gemma, etc.) - #4226
Conversation
This comment has been minimized.
This comment has been minimized.
11fd64c to
1cca720
Compare
a04276e to
6170f18
Compare
This comment was marked as low quality.
This comment was marked as low quality.
…ough AutoModelForSeq2SeqLM Main moved FastModel and get_peft_model to vision.py, so the llama.py edits are dropped. Text encoder-decoders now load via AutoModelForSeq2SeqLM, get SEQ_2_SEQ_LM LoRA, and default target_modules cover every Linear leaf (T5 q/k/v/o/wi/wo, BART fc1/fc2). Voxtral, Qwen2-Audio, Granite Speech and T5Gemma2 keep their multimodal route.
…where the class refuses it The compiler's causal-mask stub left T5's decoder bidirectional under eager attention (loss 1.16 vs 0.26 in plain transformers 4.57.6). T5 4.x also declares no sdpa, and its fullgraph-compiled layer inlines a compiler-disabled attention, so compile is partial there.
|
@codex security review |
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: Didn't find any major issues. Bravo. 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". |
# Conflicts: # unsloth/models/llama.py
# Conflicts: # unsloth/models/llama.py
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 24a7ff6dd5
ℹ️ 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".
| ) | ||
| from transformers import AutoModelForSeq2SeqLM | ||
|
|
||
| auto_model = AutoModelForSeq2SeqLM |
There was a problem hiding this comment.
Preserve the configured pad token for seq2seq generation
When a text seq2seq model has distinct padding and EOS IDs (for example, T5 uses pad 0 and EOS 1), this new route ultimately installs unsloth_base_fast_generate, which defaults pad_token_id to config.eos_token_id instead of the model's configured pad ID. Calls such as model.generate(input_ids) on padded batches therefore infer the attention mask using the wrong token, allowing encoder padding to affect generation, and completed sequences are padded with EOS. The seq2seq path needs to preserve config.pad_token_id in the generation wrapper.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5378a0d: for text seq2seq models the generate wrapper now defaults pad_token_id to config.pad_token_id. Reproduced on T5 (pad 0, EOS 1): finished rows in a padded batch were filled with 1; now 0. Causal/VLM paths still default to EOS.
| and getattr(model_config, "model_type", None) in ("t5", "mt5", "umt5") | ||
| and _is_text_seq2seq_config(model_config) | ||
| ): | ||
| os.environ["UNSLOTH_COMPILE_DISABLE"] = "partial" |
There was a problem hiding this comment.
Restore the compile setting after compiling T5
On Transformers 4.x, loading T5/MT5/UMT5 permanently writes UNSLOTH_COMPILE_DISABLE=partial into the process environment. If another causal or multimodal model is subsequently loaded in the same process, it inherits this unrelated setting; for example, _compiles_decode explicitly disables compiled decoding whenever the variable remains partial, even when UNSLOTH_COMPILE_DECODE=1. Preserve and restore the prior environment value after the T5-specific compilation instead of leaking it to later models.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: the variable only lowers compile on a later model loaded in the same process after a T5 load on transformers 4.x (speed, not correctness), and it mirrors the existing DISABLE_COMPILE_MODEL_NAMES handling. Restoring it after load would also change the runtime compile setting for the T5 model that is still loaded.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
Replacement for #3153 due to Studio rebasing. Resolves #719, resolves #643.
Problem
FastModel.from_pretrainedpicksAutoModelForCausalLMor the image-text auto class. Text encoder-decoders end inForConditionalGeneration, so they are treated as VLMs:ValueError: Unrecognized configuration class T5Config for this kind of AutoModel: AutoModelForImageTextToTextBartForCausalLM/MarianForCausalLM(encoder dropped), then training fails withExpected input batch_size (13) to match target batch_size (12)Changes
_is_text_seq2seq_config(vision.py): true when transformers maps the config underAutoModelForSeq2SeqLM, under no multimodal auto class, and the config has no vision / audio / text / encoder sub-config. Over all 718 transformers 5.17 config types it selects exactly the 21 text encoder-decoders (t5, mt5, umt5, longt5, t5gemma, bart, mbart, marian, pegasus, m2m_100, ...); Voxtral, Qwen2-Audio, Granite Speech, Qwen3-ASR, Seamless and T5Gemma2 keep their current route, also on transformers 4.57.6.FastModel.from_pretrainedloads those withAutoModelForSeq2SeqLM.transformers 4.x: keep the decoder causal mask when compiling a text seq2seq model (the stub left T5's decoder bidirectional under eager attention: loss 1.16 vs 0.26 in plain transformers 4.57.6), load with eager attention when the class declares no sdpa, and compile T5 partially (its fullgraph layer inlines a compiler-disabled attention).
get_peft_modelusesTaskType.SEQ_2_SEQ_LMfor them, does not letfinetune_vision_layers=False(theFastLanguageModeldefault) filter out the encoder, and withtarget_modules=Nonetargets every Linear leaf except the LM head, since the default regex misses T5'sq/k/v/o/wi/woand BART'sfc1/fc2.FastLanguageModelworks too: an omittedtarget_modules(not an explicit copy of the causal default) is replaced, and routing uses the passed model's config rather than the last load's global flag.finetune_attention_modules/finetune_mlp_modulesalone target only that family (T5q/k/v/oorwi/wo, BART attention orfc1/fc2).Tokenizers stay right padded (BART / Marian encoder positions are absolute, left padding made batched output batch dependent), generate defaults
pad_token_idto the model's pad (T5: 0, not EOS 1), adapter-hosted tokenizers are kept on reload, andfast_inference=Trueraises a clear error since the vLLM path is causal only.Results (B200, transformers 5.17, peft 0.21.1)
Loss parity vs plain transformers + PEFT (same LoRA targets, dropout off, 5 steps, bf16):
transformers 4.57.6: BART matches plain PEFT (7.50, 6.00, 4.22, 2.73, 1.59 vs 7.50, 6.03, 4.19, 2.72, 1.59) and T5 is bit-identical to plain PEFT (0.2578, 0.2295, 0.1934, 0.1484, 0.1040); Seq2SeqTrainer with gradient accumulation trains on both versions.
Also checked:
FastLanguageModelentry point, 4-bit QLoRA (T5, BART), LoRAsave_pretrainedandsave_pretrained_mergedfor T5 reloaded in a fresh process without Unsloth (same loss). Causal LM and VLM paths are unchanged:jobs/sft.py --tinyandjobs/vision_sft.py --tinygive bit-identical losses and grad norms on main and this branch.