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.

Don't warn about loss_type = "nll" on a TRL whose SFTConfig predates it (#16042)

Debojyoti Banerjee3cf4d45ed4e6
diff --git a/tests/test_rl_config_compat.py b/tests/test_rl_config_compat.py
index 1fadda1a161..951ca5b354f 100644
--- a/tests/test_rl_config_compat.py
+++ b/tests/test_rl_config_compat.py
@@ -401,6 +401,65 @@ def test_a_default_factory_field_is_compared_not_crashed_on():
assert kept["include_for_metrics"] == []
+class SFTConfig:
+ """TRL 0.22.x `SFTConfig`: no `loss_type` yet, training is always plain NLL."""
+
+ output_dir: str = "out"
+ completion_only_loss: bool = None
+
+
+class ModernSFTConfigWithLossType:
+ """TRL >= 0.23.0 `SFTConfig`, which declares `loss_type`."""
+
+ output_dir: str = "out"
+ loss_type: str = "nll"
+
+
+def test_loss_type_nll_on_an_sft_config_without_it_is_dropped_quietly():
+ """Issue #16034: the Kaggle notebooks pin trl==0.22.2 and set `loss_type = "nll"`."""
+ kept, messages = _collect(SFTConfig, {"output_dir": "out", "loss_type": "nll"})
+ assert kept == {"output_dir": "out"}
+ assert SFTConfig(**kept).output_dir == "out"
+ assert messages == []
+
+
+def test_another_loss_type_on_an_older_sft_config_names_the_trl_it_needs():
+ """`dft` changes training, so dropping it must stay loud and say how to get it."""
+ kept, messages = _collect(SFTConfig, {"loss_type": "dft"})
+ assert kept == {}
+ assert len(messages) == 1
+ assert "IGNORED" in messages[0]
+ assert "TRL >= 0.23.0" in messages[0]
+ assert "'dft'" in messages[0]
+
+
+def test_loss_type_is_forwarded_once_sft_config_declares_it():
+ """A TRL that has the field gets every value, `nll` included."""
+ for value in ("nll", "dft"):
+ kept, messages = _collect(ModernSFTConfigWithLossType, {"loss_type": value})
+ assert kept == {"loss_type": value}
+ assert messages == []
+
+
+def test_an_added_field_is_only_excused_on_the_config_that_gained_it():
+ """Other configs rejecting `loss_type` still get the generic warning."""
+ kept, messages = _collect(ModernGRPOConfig, {"loss_type": "nll"})
+ assert kept == {}
+ assert len(messages) == 1
+ assert "not a valid ModernGRPOConfig argument" in messages[0]
+
+
+def test_added_field_entries_carry_a_version_and_noop_values():
+ """A malformed entry would raise while unpacking inside the filter."""
+ for (config_name, field), (version, noop_values) in _MODULE.TRL_ADDED_FIELDS.items():
+ assert config_name.endswith("Config"), config_name
+ assert field.isidentifier(), field
+ assert all(part.isdigit() for part in version.split(".")), version
+ assert isinstance(noop_values, tuple), noop_values
+
+
# Guards the rl.py template wiring; read as text since importing pulls in torch/trl/zoo.
RL_SOURCE = (REPO_ROOT / "unsloth" / "models" / "rl.py").read_text(encoding = "utf-8")
diff --git a/unsloth/models/rl_config_compat.py b/unsloth/models/rl_config_compat.py
index bb0945199d6..3db6c210638 100644
--- a/unsloth/models/rl_config_compat.py
+++ b/unsloth/models/rl_config_compat.py
@@ -20,7 +20,8 @@ cache file can import it behind a try/except without pulling in torch.
Three outcomes per unrecognised argument: TRL documented a rename and the new
name exists, so the value is moved across; TRL removed the field, so it is
dropped and reported by name with TRL's advice; anything else is dropped with
-a warning.
+a warning, except a newer-TRL field whose value is the installed TRL's behaviour
+(`SFTConfig(loss_type = "nll")` on TRL < 0.23.0), which is dropped quietly.
That last case means `GRPOConfig(learnign_rate = 3e-4)` now warns instead of
raising `TypeError`. The trade is accepted: a typo is still surfaced loudly,
@@ -36,6 +37,7 @@ __all__ = [
"filter_config_init_kwargs",
"TRANSFORMERS_CONFIG_RENAMES",
"TRANSFORMERS_REMOVED_FIELD_ADVICE",
+ "TRL_ADDED_FIELDS",
"TRL_CONFIG_RENAMES",
"TRL_REMOVED_FIELD_ADVICE",
]
@@ -73,6 +75,17 @@ TRL_REMOVED_FIELD_ADVICE = {
}
+# (config, field) -> (first TRL with the field, values an older TRL already behaves as).
+TRL_ADDED_FIELDS = {
+ ("SFTConfig", "loss_type"): ("0.23.0", ("nll",)),
+}
+
+
+def _added_field(config_class, key):
+ config_name = getattr(config_class, "__name__", None)
+ return TRL_ADDED_FIELDS.get((config_name, key))
+
+
# transformers 5 removals (MIGRATION_GUIDE_V5.md; its `warmup_step` is a typo for warmup_steps).
TRANSFORMERS_CONFIG_RENAMES = {
"warmup_ratio": "warmup_steps",
@@ -350,11 +363,21 @@ def filter_config_init_kwargs(
)
continue
+ added = _added_field(config_class, key) if verdict == "unknown" else None
if verdict == "retired":
notify(
f"Unsloth: `{key}` is not supported by the installed "
f"{removal_source(key)}'s {config_name} and will be IGNORED - {detail}."
)
+ elif added is not None:
+ version, noop_values = added
+ if isinstance(value, str) and value in noop_values:
+ continue
+ notify(
+ f"Unsloth: `{key}` needs TRL >= {version} for {config_name}, and "
+ f"the installed TRL is older, so `{key} = {value!r}` will be IGNORED. "
+ f"Upgrade TRL to use it."
+ )
else:
notify(
f"Unsloth: `{key}` is not a valid {config_name} argument for the "