trl icon indicating copy to clipboard operation
trl copied to clipboard

optimizer_cls_and_kwargs missing from DPO/GRPO/KTO/RLOO trainers (regression of #2302 for RLOO)

Open shupandee opened this issue 1 month ago • 9 comments

Reproduction

SFTTrainer and RewardTrainer accept optimizer_cls_and_kwargs, but several other trainers do not. On TRL 1.17.1 (and current main), checking the __init__ signatures:

Trainer Accepts optimizer_cls_and_kwargs
SFTTrainer yes
RewardTrainer yes
DPOTrainer no
GRPOTrainer no
KTOTrainer no
RLOOTrainer no
DistillationTrainer no

For RLOOTrainer this looks like a regression of #2302, which specifically added the argument (along with PPO). That keyword no longer appears in rloo_trainer.py / dpo_trainer.py / grpo_trainer.py / kto_trainer.py on main.

import inspect
import torch
from trl import (
    SFTTrainer,
    RewardTrainer,
    DPOTrainer,
    GRPOTrainer,
    KTOTrainer,
    RLOOTrainer,
)

for cls in [SFTTrainer, RewardTrainer, DPOTrainer, GRPOTrainer, KTOTrainer, RLOOTrainer]:
    has = "optimizer_cls_and_kwargs" in inspect.signature(cls.__init__).parameters
    print(cls.__name__, "HAS" if has else "MISSING")

DPOTrainer(model="x", optimizer_cls_and_kwargs=(torch.optim.SGD, {"lr": 0.1}))

outputs:

SFTTrainer HAS
RewardTrainer HAS
DPOTrainer MISSING
GRPOTrainer MISSING
KTOTrainer MISSING
RLOOTrainer MISSING
TypeError: DPOTrainer.__init__() got an unexpected keyword argument 'optimizer_cls_and_kwargs'

(The same TypeError is raised for GRPOTrainer, KTOTrainer, RLOOTrainer, and DistillationTrainer.)

How did you hit this?

Found while comparing trainer __init__ signatures against SFTTrainer / RewardTrainer and against #2302 (which added optimizer_cls_and_kwargs to RLOO/PPO). A user who copies a working SFTTrainer(..., optimizer_cls_and_kwargs=...) setup into DPO/GRPO/KTO/RLOO training hits an unexpected-keyword TypeError and has to subclass or pass a pre-built optimizer instead.

Proposed solution

Plumb optimizer_cls_and_kwargs through the missing trainers the same way SFTTrainer does (accept it in __init__ and forward to the parent), and add a small test per trainer. Happy to open a PR once maintainers confirm they want this restored consistently across trainers (not only RLOO).

System Info

  • Platform: Linux-6.12.94+-x86_64-with-glibc2.41
  • Python version: 3.13.5
  • TRL version: 1.17.1
  • PyTorch version: 2.15.0+cpu
  • accelerator(s): cpu
  • Transformers version: 5.22.1
  • Accelerate version: 1.15.0
  • Accelerate config: not found
  • Datasets version: 5.1.0
  • HF Hub version: 1.33.0
  • bitsandbytes version: not installed
  • DeepSpeed version: not installed
  • Liger-Kernel version: not installed
  • PEFT version: 0.21.2
  • vLLM version: not installed

Checklist

  • [x] I have checked that my issue isn't already filed (see open issues)
  • [x] I have included my system information
  • [x] Any code provided is minimal, complete, and reproducible (more on MREs)
  • [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
  • [x] Any traceback provided is complete

shupandee avatar Nov 05 '26 08:11 shupandee

Hi @qgallouedec I would like to be assigned to this. I can restore optimizer_cls_and_kwargs on the DPO GRPO KTO and RLOO trainers with tests.

its-amann avatar Nov 05 '26 15:11 its-amann

Hi @its-amann, thanks for the interest! I'm planning to open the PR myself as mentioned in the issue. Happy for you to review or test once it's up.

shupandee avatar Nov 05 '26 16:11 shupandee

thanks, can you elaborate on the need, for what exactly you need optimizer_cls_and_kwargs, and in which trainer?

qgallouedec avatar Nov 05 '26 16:11 qgallouedec

Thanks @qgallouedec! A concrete case: when fine-tuning with LoRA I often want a non-default optimizer or hyperparameters that optim choices don't expose cleanly, e.g. torch.optim.RMSprop with specific momentum/alpha, or Adafactor settings, without subclassing the trainer. optimizer_cls_and_kwargs=(RMSprop, {"lr": 1e-3, "momentum": 0.9, "alpha": 0.99}) keeps that config-only.

I hit it first with DPOTrainer, where I was mirroring an SFTTrainer setup that already used this argument and got TypeError: DPOTrainer.__init__() got an unexpected keyword argument 'optimizer_cls_and_kwargs'. The same gap exists for GRPOTrainer, KTOTrainer, RLOOTrainer and DistillationTrainer, while SFTTrainer and RewardTrainer accept it. For RLOOTrainer it looks like a regression of #2302.

Happy to start with just DPO + tests if you'd prefer the smaller change, or restore it consistently across those trainers.

shupandee avatar Nov 05 '26 16:11 shupandee

Opened a fix for this in #8142, would appreciate a look when you get a chance.

its-amann avatar Nov 05 '26 16:11 its-amann

@qgallouedec could you assign this to me when you get a moment? I'd like to open the PR (DPO + tests, or all five trainers if you prefer). I noticed the contributor bot auto-closes PRs unless the linked issue is assigned, so an assignment would let it through. Thanks!

shupandee avatar Nov 05 '26 16:11 shupandee

Thanks. Your case already works from the config, no new argument needed:

DPOConfig(optim="rmsprop", optim_args="momentum=0.9,alpha=0.99")

Same for optim="adafactor".

The argument was dropped when RLOO and DPO were aligned with GRPO (#3801, #3906)

TBH, I'd more keen to align by removing this arg altogether, rather than adding it. Happy to change my mind if a real use case isn't supported.

qgallouedec avatar Nov 05 '26 21:11 qgallouedec

Thanks @qgallouedec, you're right—those examples are already covered by optim and optim_args. To clarify, I found this through source inspection and a minimal constructor repro, not an actual DPO training run. I don't currently have a concrete unsupported use case, so I'll hold off on the proposed PR. Thanks for explaining why the argument was removed.

shupandee avatar Nov 06 '26 05:11 shupandee

Closing since the config covers the use case (optim + optim_args) and there's no case it's needed for. Happy to reopen if a real one comes up.

qgallouedec avatar Dec 01 '26 22:12 qgallouedec