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.

Keep SFT kwargs passed beside a plain TrainingArguments instead of falling back to trl's defaults (#14094)

Leo Borcherdingff9c9583b70e
diff --git a/tests/test_warnings_issued_guard.py b/tests/test_warnings_issued_guard.py
index e26023eef10..a8c8ee024b3 100644
--- a/tests/test_warnings_issued_guard.py
+++ b/tests/test_warnings_issued_guard.py
@@ -343,12 +343,13 @@ def _rl_config_compat():
return module
-def _wrapped_recording(trainer_base = None):
- config_class = _sft_config()
+def _wrapped_recording(trainer_base = None, config_class = None):
+ config_class = config_class or _sft_config()
ns = _load(
"_ensure_warnings_issued",
"_resolve_trainer_params",
"_route_unknown_trainer_kwargs",
+ "_config_family",
"_backwards_compatible_trainer",
)
if "trl" not in ns:
@@ -563,3 +564,49 @@ def test_auto_packing_failure_still_leaves_the_wrapper_installed():
assert guarded, "_patch_sft_trainer_auto_packing must be wrapped in try/except"
return
raise AssertionError("_patch_trl_trainer not found")
+
+
+def test_a_plain_training_arguments_keeps_the_moved_kwargs(tmp_path):
+ """The old notebook shape: transformers.TrainingArguments plus SFT kwargs on the trainer. trl
+ rebuilds a plain TrainingArguments with SFTConfig(**args.to_dict()), which drops anything set
+ as an attribute, so packing, the text field and the length used to fall back to defaults."""
+ from transformers import TrainingArguments
+
+ Trainer, config_class = _wrapped_recording()
+ args = TrainingArguments(output_dir = str(tmp_path), report_to = [], hub_token = "hf_FAKE")
+
+ trainer = Trainer(
+ model = _Bare(),
+ args = args,
+ packing = True,
+ max_length = 4096,
+ dataset_text_field = "body",
+ )
+
+ seen = trainer.args
+ if not isinstance(seen, config_class):
+ # What trl's SFTTrainer does with a plain TrainingArguments.
+ rebuilt = seen.to_dict()
+ rebuilt["hub_token"] = seen.hub_token
+ seen = config_class(**rebuilt)
+ assert (seen.packing, seen.max_length, seen.dataset_text_field) == (True, 4096, "body")
+ assert seen.hub_token == "hf_FAKE"
+ assert seen.output_dir == str(tmp_path)
+
+
+def test_an_instance_of_the_displaced_trl_config_is_not_rebuilt(tmp_path):
+ """Unsloth swaps each trl config for a subclass; a config built from the pristine class before
+ that swap must keep the identity path. Rebuilding a GRPOConfig re-raises its post-init checks."""
+ pristine = _sft_config()
+
+ class Patched(pristine):
+ _unsloth_patched_rl_config = True
+
+ Patched.__name__ = pristine.__name__
+ Trainer, _ = _wrapped_recording(config_class = Patched)
+ config = pristine(output_dir = str(tmp_path), report_to = [], bf16 = False)
+
+ trainer = Trainer(model = _Bare(), args = config, packing = True)
+
+ assert trainer.args is config
+ assert trainer.args.packing is True
diff --git a/unsloth/trainer.py b/unsloth/trainer.py
index 27c4234b21d..6b88ba0682f 100644
--- a/unsloth/trainer.py
+++ b/unsloth/trainer.py
@@ -1712,6 +1712,18 @@ def _route_unknown_trainer_kwargs(
return to_config, to_trainer
+def _config_family(config_class):
+ # Plus the pristine TRL config it displaced: rebuilding one re-raises GRPOConfig's post-init checks.
+ family = [config_class]
+ cls = config_class
+ while (
+ "_unsloth_patched_rl_config" in cls.__dict__ or cls.__name__.startswith("Unsloth")
+ ) and cls.__bases__:
+ cls = cls.__bases__[0]
+ family.append(cls)
+ return tuple(family)
+
+
def _backwards_compatible_trainer(trainer_class, config_class):
original_init = trainer_class.__init__
@@ -1777,6 +1789,12 @@ def _backwards_compatible_trainer(trainer_class, config_class):
# trigger the mutually-exclusive param checks (trl grpo_config.py#L499-L502).
if not isinstance(training_args, TrainingArguments):
config = config_class(**config_dict)
+ elif not isinstance(training_args, _config_family(config_class)):
+ # Plain TrainingArguments: trl rebuilds via to_dict(), dropping setattr'd kwargs, so rebuild here as trl does.
+ dict_args = training_args.to_dict()
+ dict_args["hub_token"] = training_args.hub_token
+ dict_args.pop("push_to_hub_token", None)
+ config = config_class(**{**dict_args, **additional_config_kwargs})
else:
# Every trl config subclasses TrainingArguments, so this is the branch real calls take and
# config_dict was going nowhere: set the moved values on the caller's config rather than rebuild it.