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.

Seq2Seq LoRA task type and GA token count for T5Gemma2 (#7795)

Daru Okta Buanab5fd1442a500
diff --git a/tests/test_seq2seq_auto_class.py b/tests/test_seq2seq_auto_class.py
index 7187cfe19dd..9b711acda9a 100644
--- a/tests/test_seq2seq_auto_class.py
+++ b/tests/test_seq2seq_auto_class.py
@@ -46,3 +46,46 @@ def test_other_configs_keep_their_route(name):
def test_no_config():
assert not _is_text_seq2seq_config(None)
+
+
+ "name, expected",
+ [
+ ("T5Config", True),
+ ("BartConfig", True),
+ ("T5GemmaConfig", True),
+ ("T5Gemma2Config", True),
+ ("VoxtralConfig", False),
+ ("Qwen2AudioConfig", False),
+ ("GraniteSpeechConfig", False),
+ ("WhisperConfig", False),
+ ("LlamaConfig", False),
+ ("Gemma3Config", False),
+ ],
+)
+def test_seq2seq_lm_config_picks_lora_task_and_ga_count(name, expected):
+ from unsloth.models._utils import _is_seq2seq_lm_config
+ assert _is_seq2seq_lm_config(_config(name)) is expected
+
+
+def test_get_batch_samples_dispatch():
+ from types import SimpleNamespace
+ from unsloth.models import _utils
+
+ calls = []
+ dispatch = _utils._make_seq2seq_aware_get_batch_samples(
+ lambda self, *a, **k: calls.append("stock") or "stock"
+ )
+ assert dispatch.__name__ == "_unsloth_get_batch_samples"
+ original = _utils._unsloth_get_batch_samples
+ _utils._unsloth_get_batch_samples = lambda self, *a, **k: calls.append("unsloth") or "unsloth"
+ try:
+ for name, want in (
+ ("T5Gemma2Config", "stock"),
+ ("LlamaConfig", "unsloth"),
+ ("WhisperConfig", "unsloth"),
+ ):
+ trainer = SimpleNamespace(model = SimpleNamespace(config = _config(name)))
+ assert dispatch(trainer, iter([]), 1) == want
+ finally:
+ _utils._unsloth_get_batch_samples = original
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index a48f8a48f98..74a0949fce6 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -4809,6 +4809,28 @@ def patch_fla_autotuner_fast_path():
CachedAutotuner.run = run
+def _is_seq2seq_lm_config(config):
+ # Both halves: Voxtral / Qwen2-Audio are Seq2SeqLM-mapped but decoder-only, Whisper is encoder-decoder but SpeechSeq2Seq.
+ if config is None or not getattr(config, "is_encoder_decoder", False):
+ return False
+ try:
+ from transformers import AutoModelForSeq2SeqLM
+ return type(config) in AutoModelForSeq2SeqLM._model_mapping
+ except Exception:
+ return False
+
+
+def _make_seq2seq_aware_get_batch_samples(original):
+ def _unsloth_get_batch_samples_dispatch(self, *args, **kwargs):
+ # Seq2Seq labels are unshifted, so the causal labels[..., 1:] token count drops one per row and inflates the GA loss.
+ if _is_seq2seq_lm_config(getattr(self.model, "config", None)):
+ return original(self, *args, **kwargs)
+ return _unsloth_get_batch_samples(self, *args, **kwargs)
+
+ _unsloth_get_batch_samples_dispatch.__name__ = "_unsloth_get_batch_samples"
+ return _unsloth_get_batch_samples_dispatch
+
+
def patch_gradient_accumulation_fix(Trainer):
# Fixes "Output 0 of UnslothFusedLossBackward is a view and is being modified inplace" and gradient accumulation.
import inspect
@@ -4830,7 +4852,9 @@ def patch_gradient_accumulation_fix(Trainer):
raise NotImplementedError("Unsloth: Please make a Github issue immediately!!")
else:
if Trainer.get_batch_samples.__name__ != "_unsloth_get_batch_samples":
- Trainer.get_batch_samples = _unsloth_get_batch_samples
+ Trainer.get_batch_samples = _make_seq2seq_aware_get_batch_samples(
+ Trainer.get_batch_samples
+ )
if not hasattr(Trainer, "_old_compute_loss"):
# Fix transformers 4.57.0 raising "Output 0 of UnslothFusedLossBackward is a view and is being modified inplace".
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 1f3e9a772fd..712061be3f3 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -124,6 +124,7 @@ from ._utils import (
arm_gradient_checkpointing,
resolve_training_gradient_checkpointing,
set_module_gradient_checkpointing,
+ _is_seq2seq_lm_config,
)
from ._utils import *
from ._uma_safetensors import is_integrated_unified_memory_gpu
@@ -4173,9 +4174,12 @@ class FastBaseModel:
if isinstance(model, (PeftModelForCausalLM, PeftModelForSeq2SeqLM)):
raise RuntimeError("Unsloth: You already added LoRA adapters to your model!")
+ if task_type == TaskType.CAUSAL_LM and _is_seq2seq_lm_config(
+ getattr(model, "config", None)
+ ):
+ # Also multimodal encoder-decoders (T5Gemma2), which load through the VLM path.
+ task_type = TaskType.SEQ_2_SEQ_LM
if _is_text_seq2seq_config(getattr(model, "config", None)):
- if task_type == TaskType.CAUSAL_LM:
- task_type = TaskType.SEQ_2_SEQ_LM
# No vision tower: FastLanguageModel's finetune_vision_layers=False must not filter the encoder out.
finetune_vision_layers = True
# get_peft_regex misses T5's q/k/v/o/wi/wo and BART's fc1/fc2, so list the Linear leaves (minus the LM head) ourselves.