transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Seq2SeqTrainer does not accept optimizer_cls_and_kwargs (supported by Trainer and listed in its docstring)

Open shupandee opened this issue 1 month ago • 3 comments

System Info

  • transformers version: 5.22.1
  • Platform: Linux-6.12.94+-x86_64-with-glibc2.41
  • Python version: 3.13.5
  • Huggingface_hub version: 1.33.0
  • Safetensors version: 0.8.0
  • Accelerate version: 1.15.0
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.15.0+cpu (NA)
  • Using distributed or parallel set-up in script?: no

Who can help?

@SunMarc (Trainer)

Reproduction

Trainer accepts optimizer_cls_and_kwargs, and Seq2SeqTrainer inherits the docstring that documents it. But Seq2SeqTrainer.__init__ does not take the argument or forward it to Trainer.__init__, so seq2seq users get a TypeError.

import torch
from transformers import (
    T5Config,
    T5ForConditionalGeneration,
    Trainer,
    Seq2SeqTrainer,
    Seq2SeqTrainingArguments,
)

model = T5ForConditionalGeneration(
    T5Config(
        vocab_size=32,
        d_model=8,
        d_ff=16,
        num_layers=1,
        num_heads=1,
        d_kv=8,
    )
)
args = Seq2SeqTrainingArguments(output_dir="/tmp/s2s", report_to=[])
opt = (torch.optim.SGD, {"lr": 0.1})

Trainer(model=model, args=args, optimizer_cls_and_kwargs=opt)  # OK
Seq2SeqTrainer(model=model, args=args, optimizer_cls_and_kwargs=opt)

Output:

TypeError: Seq2SeqTrainer.__init__() got an unexpected keyword argument 'optimizer_cls_and_kwargs'

Confirmed on transformers 5.22.1 and on current main (src/transformers/trainer_seq2seq.py).

Expected behavior

Seq2SeqTrainer should accept optimizer_cls_and_kwargs and pass it through to Trainer.__init__, matching both Trainer and the inherited docstring.

Proposed solution

Add optimizer_cls_and_kwargs: tuple[type[Optimizer], dict[str, Any]] | None = None to Seq2SeqTrainer.__init__ and forward it in super().__init__(..., optimizer_cls_and_kwargs=optimizer_cls_and_kwargs). Add a small unit test that constructs a tiny Seq2SeqTrainer with a custom optimizer class the same way the Trainer tests do.

Happy to open a PR with that change if this looks right.

shupandee avatar Nov 05 '26 07:11 shupandee

Thanks @shupandee. optimizer_cls_and_kwargs is indeed the only Trainer.__init__ argument that Seq2SeqTrainer doesn't forward. Feel free to open a PR.

Until it lands, you can set trainer.optimizer_cls_and_kwargs = (torch.optim.SGD, {"lr": 0.1}) right after constructing the trainer, since create_optimizer reads that attribute.

qgallouedec avatar Nov 25 '26 09:11 qgallouedec

Thanks @qgallouedec. I see #50398 was opened for this on Nov 5 and is still waiting on review. I have a fix ready locally (forwards the argument plus a test for the constructor path). Happy to open a PR if you'd like one, or to leave it to #50398. Your call.

shupandee avatar Nov 25 '26 09:11 shupandee

We usually give the OP (here: you) the priority for the PR, so please feel free to open a PR

qgallouedec avatar Nov 25 '26 10:11 qgallouedec