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 loss_type="dft" on the DFT objective when label smoothing is set (#15396)

Daniel Han2debcf37a5f4
diff --git a/tests/test_sft_dft_loss_routing.py b/tests/test_sft_dft_loss_routing.py
index 6efb2fb3f1a..a6e59d00a13 100644
--- a/tests/test_sft_dft_loss_routing.py
+++ b/tests/test_sft_dft_loss_routing.py
@@ -145,3 +145,22 @@ def test_nll_keeps_the_unsloth_path():
model = _FusedModel()
assert Trainer("nll").compute_loss(model, {}, num_items_in_batch = 7) == ("super", 7)
assert model.calls == []
+
+
+def test_dft_with_label_smoothing_still_trains_dft():
+ Trainer = _trainer_class()
+ trainer = Trainer("dft")
+ trainer.label_smoother = lambda *args, **kwargs: pytest.fail("dft was trained as smoothed NLL")
+ model = _FusedModel()
+ loss = trainer.compute_loss(model, {"input_ids": None}, num_items_in_batch = 3)
+ assert [bool(c.get("fused_lm_head")) for c in model.calls] == [True]
+ assert loss.item() == pytest.approx(_dft(model.log_probs.detach(), 3))
+
+
+def test_nll_with_label_smoothing_keeps_the_unsloth_path():
+ Trainer = _trainer_class()
+ trainer = Trainer("nll")
+ trainer.label_smoother = object()
+ model = _FusedModel()
+ assert trainer.compute_loss(model, {}, num_items_in_batch = 7) == ("super", 7)
+ assert model.calls == []
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index e01bae0be16..11101245a5d 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -1329,11 +1329,8 @@ def sft_trainer_compute_loss(function_name, function):
):
# TRL >= 1.15 computes loss_type="dft" only in the compute_loss this replaces (else it trains as NLL):
# run it on TRL's fused LM head. Older TRL routes DFT through compute_loss_func.
- if (
- getattr(self.args, "loss_type", None) == "dft"
- and self.compute_loss_func is None
- and getattr(self, "label_smoother", None) is None
- ):
+ # TRL label smoothing applies only to "nll", so label_smoother must not opt dft out.
+ if getattr(self.args, "loss_type", None) == "dft" and self.compute_loss_func is None:
import trl.trainer.utils as _trl_utils
if hasattr(_trl_utils, "add_fused_lm_head"):
# TRL's fused head projects with lm_head.weight directly, skipping QAT fake quantization.