Skip to content

Support for Seq2Seq Models (T5, T5Gemma, etc.) - #4226

Merged
danielhanchen merged 15 commits into
mainfrom
dh/recover-3153-seq2seq
Sep 29, 2026
Merged

danielhanchen merged 15 commits into
mainfrom
dh/recover-3153-seq2seq

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

Replacement for #3153 due to Studio rebasing. Resolves #719, resolves #643.

Problem

FastModel.from_pretrained picks AutoModelForCausalLM or the image-text auto class. Text encoder-decoders end in ForConditionalGeneration, so they are treated as VLMs:

  • T5, T5Gemma: ValueError: Unrecognized configuration class T5Config for this kind of AutoModel: AutoModelForImageTextToText
  • BART, Marian: load as BartForCausalLM / MarianForCausalLM (encoder dropped), then training fails with Expected input batch_size (13) to match target batch_size (12)

Changes

  • _is_text_seq2seq_config (vision.py): true when transformers maps the config under AutoModelForSeq2SeqLM, 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_pretrained loads those with AutoModelForSeq2SeqLM.

  • 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_model uses TaskType.SEQ_2_SEQ_LM for them, does not let finetune_vision_layers=False (the FastLanguageModel default) filter out the encoder, and with target_modules=None targets every Linear leaf except the LM head, since the default regex misses T5's q/k/v/o/wi/wo and BART's fc1/fc2.

  • FastLanguageModel works too: an omitted target_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_modules alone target only that family (T5 q/k/v/o or wi/wo, BART attention or fc1/fc2).

  • Tokenizers stay right padded (BART / Marian encoder positions are absolute, left padding made batched output batch dependent), generate defaults pad_token_id to the model's pad (T5: 0, not EOS 1), adapter-hosted tokenizers are kept on reload, and fast_inference=True raises a clear error since the vLLM path is causal only.

Results (B200, transformers 5.17, peft 0.21.1)

Model main this PR
google-t5/t5-small ValueError on load PeftModelForSeq2SeqLM, trains, generates "Das Haus ist wunderbar."
facebook/bart-base BartForCausalLM, batch size ValueError PeftModelForSeq2SeqLM, trains, generates
Helsinki-NLP/opus-mt-en-de MarianForCausalLM, batch size ValueError PeftModelForSeq2SeqLM, trains, generates "Das Haus ist wunderbar."
tiny T5Gemma ValueError on load PeftModelForSeq2SeqLM, trains

Loss parity vs plain transformers + PEFT (same LoRA targets, dropout off, 5 steps, bf16):

Model Plain PEFT Unsloth
t5-small 0.2539, 0.2295, 0.1934, 0.1484, 0.1050 0.2559, 0.2305, 0.1934, 0.1484, 0.1035
bart-base 7.5000, 6.0312, 4.1875, 2.7188, 1.5938 7.4688, 5.9688, 4.2500, 2.7500, 1.5859

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: FastLanguageModel entry point, 4-bit QLoRA (T5, BART), LoRA save_pretrained and save_pretrained_merged for T5 reloaded in a fresh process without Unsloth (same loss). Causal LM and VLM paths are unchanged: jobs/sft.py --tiny and jobs/vision_sft.py --tiny give bit-identical losses and grad norms on main and this branch.

@gemini-code-assist

This comment has been minimized.

gemini-code-assist[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen
danielhanchen force-pushed the dh/recover-3153-seq2seq branch from 11fd64c to 1cca720 Compare March 12, 2026 08:17
@danielhanchen
danielhanchen force-pushed the dh/recover-3153-seq2seq branch from a04276e to 6170f18 Compare March 12, 2026 08:19
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@nidhishgajjar

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 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 ✅ Completed 2026-09-29T20:00:58.691105Z 5378a0d 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c3730b65c1

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@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: 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".

Comment thread unsloth/models/loader.py
)
from transformers import AutoModelForSeq2SeqLM

auto_model = AutoModelForSeq2SeqLM

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

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

Comment thread unsloth/models/loader.py
and getattr(model_config, "model_type", None) in ("t5", "mt5", "umt5")
and _is_text_seq2seq_config(model_config)
):
os.environ["UNSLOTH_COMPILE_DISABLE"] = "partial"

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

@danielhanchen danielhanchen Sep 29, 2026 •

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.

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.

@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. What shall we delve into next?

Reviewed commit: 5378a0d222

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature# non-studio# still-needed# Stale but the problem is still present on main; review it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

please give t5 support. Support T5 models

3 participants