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.

version_compat: follow TRL 1.15's chunked_nll alias and its GPU-only DPO scorer (#13207)

Daniel Han468e04f45050
diff --git a/tests/version_compat/test_trl_fake_train_cpu.py b/tests/version_compat/test_trl_fake_train_cpu.py
index 244b8d57b04..2ec27cee0a7 100644
--- a/tests/version_compat/test_trl_fake_train_cpu.py
+++ b/tests/version_compat/test_trl_fake_train_cpu.py
@@ -330,6 +330,15 @@ def test_dpo_trains_on_cpu(tmp_path):
from trl import DPOConfig, DPOTrainer
assert DPOTrainer.__name__ == "UnslothDPOTrainer", "DPO patch did not apply"
+ import inspect
+
+ from trl.trainer import dpo_trainer
+
+ # TRL 1.15 scores DPO through its fused LM head, a Triton kernel with no CPU path, so
+ # plain TRL cannot train DPO on a CPU either. Only skip when that is what the installed
+ # TRL does, so an older TRL still has to train here.
+ if "fused_lm_head=True" in inspect.getsource(dpo_trainer):
+ pytest.skip("this TRL computes DPO log-probs with a Triton GPU kernel only")
model, tok = _load_plain()
ds = Dataset.from_list(
[{"prompt": "Hi", "chosen": " hello friend", "rejected": " go away"}] * 8
diff --git a/tests/version_compat/test_trl_loss_normalization_contract.py b/tests/version_compat/test_trl_loss_normalization_contract.py
index 47adfc04ba8..86253fbcbb5 100644
--- a/tests/version_compat/test_trl_loss_normalization_contract.py
+++ b/tests/version_compat/test_trl_loss_normalization_contract.py
@@ -156,8 +156,29 @@ def test_explicit_loss_type_still_wins():
if not hasattr(trl.SFTConfig, "loss_type"):
pytest.skip("this TRL has no SFTConfig.loss_type")
- cfg = trl.SFTConfig(output_dir = "unused", loss_type = "chunked_nll")
- assert cfg.loss_type == "chunked_nll", "explicit loss_type was clobbered"
+ for wanted in ("chunked_nll", "dft"):
+ cfg = trl.SFTConfig(output_dir = "unused", loss_type = wanted)
+ assert cfg.loss_type == _trl_resolves(wanted), "explicit loss_type was clobbered"
+
+
+def _trl_resolves(wanted):
+ """What TRL itself makes of an explicit loss_type, so only Unsloth's own rewrites count.
+
+ TRL 1.15 deprecated "chunked_nll" as an alias of "nll" and rewrites it in its own
+ __post_init__, announcing that with a FutureWarning naming the old value. Read the
+ alias from that warning rather than from a version number.
+ """
+ import warnings
+
+ pristine = _pristine_sft_config_cls()
+ with warnings.catch_warnings(record = True) as caught:
+ warnings.simplefilter("always")
+ got = pristine(output_dir = "unused", loss_type = wanted).loss_type
+ aliased = any(
+ issubclass(w.category, FutureWarning) and repr(wanted) in str(w.message) and "deprecated" in str(w.message)
+ for w in caught
+ )
+ return got if aliased else wanted
def _skip_if_unsloth_refuses_grpo():
@@ -229,7 +250,9 @@ def test_pristine_trl_sft_config_keeps_an_explicit_loss_type():
for wanted in ("chunked_nll", "dft"):
got = pristine(output_dir = "unused", loss_type = wanted).loss_type
- assert got == wanted, f"explicit loss_type {wanted!r} was clobbered to {got!r}"
+ assert got == _trl_resolves(wanted), f"explicit loss_type {wanted!r} was clobbered to {got!r}"
+ # "dft" is not an alias on any TRL, so it must always come back unchanged.
+ assert _trl_resolves("dft") == "dft"
def test_dataclass_field_default_is_nll_for_hfargumentparser():