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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fused preference loss: fall back when lm_head has module backward hooks (#14788)
Daniel Han2350ba88b12a
diff --git a/tests/test_fused_preference_loss.py b/tests/test_fused_preference_loss.pyindex 8a06e716b09..75b1110e027 100644--- a/tests/test_fused_preference_loss.py+++ b/tests/test_fused_preference_loss.py@@ -708,3 +708,83 @@ def test_hooked_or_custom_head_falls_back(dpo_module, monkeypatch, kind):loss, metrics, _ = _run(trainer, model, batch)assert HiddenStatesLlama.hidden_calls == 0torch.testing.assert_close(loss, reference[0])+++def _zero_head_input_grad(module, grad_input, grad_output):+ return (torch.zeros_like(grad_input[0]),)+++ "kind", ["full_backward_hook", "backward_pre_hook", "legacy_backward_hook", "global_backward_hook"]+)+def test_backward_hooked_head_falls_back(dpo_module, monkeypatch, kind):+ """The kernel never calls the head module, so its module backward hooks would never fire."""+ monkeypatch.setattr(dpo_module, "_logit_transforms", lambda unwrapped: (0.0, 0.0, 0.0))+ batch = _orpo_batch(seed = 4)+ model = _model(0)+ head = model.get_output_embeddings()+ calls = []++ def count(fn):+ def hook(*args):+ calls.append(1)+ return fn(*args)++ return hook++ with contextlib.ExitStack() as stack:+ if kind == "full_backward_hook":+ handle = head.register_full_backward_hook(count(_zero_head_input_grad))+ elif kind == "backward_pre_hook":+ handle = head.register_full_backward_pre_hook(+ count(lambda m, grad_output: (grad_output[0] * 3,))+ )+ elif kind == "legacy_backward_hook":+ handle = head.register_backward_hook(+ count(lambda m, gi, go: tuple(None if g is None else g * 3 for g in gi))+ )+ else:+ from torch.nn.modules.module import register_module_full_backward_hook++ def global_hook(module, grad_input, grad_output):+ if module is head:+ return _zero_head_input_grad(module, grad_input, grad_output)++ handle = register_module_full_backward_hook(count(global_hook))+ stack.callback(handle.remove)+ trainer = _orpo_trainer(dpo_module, model, "orpo", "auto")+ assert dpo_module._output_head(trainer, model) == (None, None)+ reference = _run(_orpo_trainer(dpo_module, model, "orpo", False), model, batch)+ ref_calls = len(calls)+ assert ref_calls >= 1+ HiddenStatesLlama.hidden_calls = 0+ result = _run(trainer, model, batch)+ assert HiddenStatesLlama.hidden_calls == 0+ assert len(calls) == 2 * ref_calls+ _assert_same(reference, result)+++def test_weight_tensor_hooks_still_take_fused_path(dpo_module, monkeypatch):+ """Tensor and AccumulateGrad hooks on the head weight see the kernel's returned dW, so the+ fused path stays eligible and still gives the stock gradients."""+ monkeypatch.setattr(dpo_module, "_logit_transforms", lambda unwrapped: (0.0, 0.0, 0.0))+ batch = _orpo_batch(seed = 4)+ model = _model(0)+ weight = model.get_output_embeddings().weight+ calls = []+ handles = [+ weight.register_hook(lambda g: (calls.append("tensor"), g * 2)[1]),+ weight.register_post_accumulate_grad_hook(lambda p: calls.append("post")),+ ]+ try:+ reference = _run(_orpo_trainer(dpo_module, model, "orpo", False), model, batch)+ assert calls == ["tensor", "post"]+ calls.clear()+ HiddenStatesLlama.hidden_calls = 0+ result = _run(_orpo_trainer(dpo_module, model, "orpo", True), model, batch)+ assert HiddenStatesLlama.hidden_calls >= 1+ assert calls == ["tensor", "post"]+ finally:+ for handle in handles:+ handle.remove()+ _assert_same(reference, result)diff --git a/unsloth/models/dpo.py b/unsloth/models/dpo.pyindex 37c8e825fc5..93a9ee35e62 100644--- a/unsloth/models/dpo.py+++ b/unsloth/models/dpo.py@@ -81,6 +81,16 @@ def _uses_fsdp1(trainer, model):return cache[model]+def _global_module_backward_hooks():+ # torch.nn.modules.module.register_module_full_backward_(pre_)hook applies to every module.+ from torch.nn.modules import module as _module++ return bool(+ getattr(_module, "_global_backward_hooks", None)+ or getattr(_module, "_global_backward_pre_hooks", None)+ )++def _output_head(trainer, model):# FSDP1 frees or reshards the unwrapped head's parameter view outside its own forward.if isinstance(model, torch.nn.Module) and _uses_fsdp1(trainer, model):@@ -100,12 +110,17 @@ def _output_head(trainer, model):if head is None or hasattr(head, "base_layer") or hasattr(head, "modules_to_save"):return None, None# The kernel reads weight / bias directly: only a plain, unhooked nn.Linear is exactly F.linear(h, W, b).+ # The head module is never called, so its module backward hooks (which may rewrite grad_input)+ # would never fire either. Weight tensor / AccumulateGrad hooks still see the kernel's dW.if (type(head) is not torch.nn.Linearor hasattr(head, "_hf_hook")or "forward" in vars(head)or head._forward_hooksor head._forward_pre_hooks+ or head._backward_hooks+ or getattr(head, "_backward_pre_hooks", None)+ or _global_module_backward_hooks()):return None, Noneweight = getattr(head, "weight", None)