Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Right-pad encoder-decoder tokenizers and load SeamlessM4T without auto_model (#13066)

Daniel Hanc2a24a1def38
diff --git a/tests/test_seq2seq_auto_class.py b/tests/test_seq2seq_auto_class.py
index 20ef5d27fc6..9fe5886a8d2 100644
--- a/tests/test_seq2seq_auto_class.py
+++ b/tests/test_seq2seq_auto_class.py
@@ -91,6 +91,39 @@ def test_get_batch_samples_dispatch():
_utils._unsloth_get_batch_samples = original
+ "name, expected",
+ [
+ ("T5Config", "right"),
+ ("BartConfig", "right"),
+ ("WhisperConfig", "right"),
+ ("SeamlessM4Tv2Config", "right"),
+ ("T5Gemma2Config", "right"),
+ ("LlamaConfig", "left"),
+ ("Qwen3Config", "left"),
+ ("Gemma3Config", "left"),
+ ("VoxtralConfig", "left"),
+ ("Qwen2AudioConfig", "left"),
+ ],
+)
+def test_encoder_decoders_pad_right(name, expected):
+ from unsloth.models.vision import _generation_padding_side
+ assert _generation_padding_side(_config(name)) == expected
+
+
[email protected]("name", ["SeamlessM4TConfig", "SeamlessM4Tv2Config"])
+def test_seamless_has_a_seq2seq_class_and_no_causal_class(name):
+ from unsloth.models._utils import _is_seq2seq_lm_config, resolve_model_class
+
+ config = _config(name)
+ assert not _is_text_seq2seq_config(config)
+ assert _is_seq2seq_lm_config(config)
+ assert resolve_model_class(transformers.AutoModelForCausalLM, config) is None
+ assert resolve_model_class(transformers.AutoModelForSeq2SeqLM, config).__name__.endswith(
+ "ForTextToText"
+ )
+
+
@pytest.mark.parametrize("name", ["WhisperConfig", "MoonshineConfig", "Speech2TextConfig"])
def test_speech_seq2seq_configs_resolve_without_auto_model(name):
# FastModel.from_pretrained(whisper_lora) without auto_model used to ask AutoModelForImageTextToText (#2726).
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 7c84fddb687..29e75e194d7 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -122,6 +122,7 @@ from ._utils import (
_mark_full_finetuning,
_get_text_only_config,
resolve_model_class,
+ _is_seq2seq_lm_config,
_is_family_text_decoder,
_apply_text_only_key_mapping,
_get_remote_composite_text_only,
@@ -2643,7 +2644,13 @@ class FastModel(FastBaseModel):
if _num_labels is not None:
from transformers import AutoModelForSequenceClassification
auto_model = AutoModelForSequenceClassification
- elif _is_text_seq2seq_config(model_config):
+ elif _is_text_seq2seq_config(model_config) or (
+ # SeamlessM4T: Seq2SeqLM-mapped (text-to-text) beside its speech classes, no causal-LM class.
+ not is_vlm
+ and _is_seq2seq_lm_config(model_config)
+ and resolve_model_class(AutoModelForCausalLM, model_config, **_probe_hub_kwargs)
+ is None
+ ):
if fast_inference:
raise NotImplementedError(
"Unsloth: fast_inference (vLLM) does not support encoder-decoder models "
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 7f921857e62..49f6b481deb 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -90,6 +90,9 @@ def _is_text_seq2seq_config(config):
def _generation_padding_side(config):
# BART / Marian encoder positions are absolute, so left padding would shift every real token.
+ # Any encoder-decoder (Whisper, SeamlessM4T): left-padded labels put pads before the decoder's target.
+ if getattr(config, "is_encoder_decoder", False):
+ return "right"
return "right" if _is_text_seq2seq_config(config) else "left"
@@ -4741,15 +4744,16 @@ class FastBaseModel:
raise RuntimeError("Unsloth: Unsuccessfully patched inner_training_loop")
patch_saving_functions(model, vision = True)
+ _padding_side = _generation_padding_side(getattr(model, "config", None))
m = model
while hasattr(m, "model"):
if hasattr(m, "_saved_temp_tokenizer"):
if hasattr(m._saved_temp_tokenizer, "tokenizer"):
- m._saved_temp_tokenizer.tokenizer.padding_side = "left"
+ m._saved_temp_tokenizer.tokenizer.padding_side = _padding_side
m = m.model
if hasattr(m, "_saved_temp_tokenizer"):
if hasattr(m._saved_temp_tokenizer, "tokenizer"):
- m._saved_temp_tokenizer.tokenizer.padding_side = "left"
+ m._saved_temp_tokenizer.tokenizer.padding_side = _padding_side
# Prevent Transformers Trainer from auto-wrapping Unsloth LoRA models in DP.
_mark_unsloth_disable_data_parallel(model, disable = not full_finetuning)