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.

Fix DecisionTrainer crashing on step 1 without a prior inference pass (#13176)

Daniel Han3b0e721bdd1f
diff --git a/tests/test_decision_clef_head.py b/tests/test_decision_clef_head.py
index 292ce92a76b..8c949507d56 100644
--- a/tests/test_decision_clef_head.py
+++ b/tests/test_decision_clef_head.py
@@ -277,3 +277,31 @@ def test_autocast_matches_the_per_record_head(monkeypatch):
scale = theirs[0][theirs[3]].abs().max().item()
assert logits <= 2e-2 * scale and grads < 5e-2
assert torch.equal(ours[0].argmax(-1), theirs[0].argmax(-1))
+
+
[email protected](not has_real_cuda(), reason = "Inductor pads saved strides on CUDA")
+def test_checkpointed_head_trains_under_a_compiled_layer_norm(monkeypatch):
+ # #13160: over 1024 tokens the first (static) graph pads mean / rstd strides; the queries'
+ # dynamic recompile then served the checkpoint recompute and that backward crashed.
+ original = getattr(clef.functional, "_uncompiled_layer_norm", clef.functional.layer_norm)
+ monkeypatch.setattr(clef.functional, "_uncompiled_layer_norm", original, raising = False)
+ monkeypatch.setattr(clef.functional, "layer_norm", torch.compile(original))
+ torch._dynamo.reset()
+ torch.manual_seed(0)
+ length, width = 1222, 512
+ head = clef.JointSchemaHead(HIDDEN, width, 2, 2, 4, 64).cuda().train()
+ question = reference.EncodedQuestion("q", 0, (5, 8), ((10, 12), (14, 17)), ("0", "1"))
+ records = [reference.EncodedRecord(tuple(range(length)), (question,), "r")] * 2
+ hidden = torch.randn(len(records), length, HIDDEN, device = "cuda")
+ ids = torch.zeros(len(records), length, dtype = torch.long, device = "cuda")
+ mask = torch.ones_like(ids)
+ embedding = torch.randn(length, HIDDEN, device = "cuda")
+ grad_out = torch.randn(len(records), 2, device = "cuda")
+ results = {}
+ for checkpoint in ("1", "0"):
+ monkeypatch.setenv("UNSLOTH_CLEF_CHECKPOINT", checkpoint)
+ results[checkpoint] = _run(
+ head, head.forward, hidden, ids, mask, records, embedding, grad_out
+ )
+ for a, b in zip(_max_error(results["1"], results["0"]), (1e-5, 1e-5, 1e-4)):
+ assert a < b
diff --git a/unsloth/models/clef.py b/unsloth/models/clef.py
index b2ec9048802..552e92b0db1 100644
--- a/unsloth/models/clef.py
+++ b/unsloth/models/clef.py
@@ -340,13 +340,24 @@ def build_layout(records, device) -> dict:
return layout
+def _layer_norm(norm, x):
+ # Not Unsloth's compiled F.layer_norm: memory and queries recompile it mid-checkpoint, and the
+ # recompute's newer graph hands the first graph's backward unpadded mean / rstd strides (#13160).
+ layer_norm = getattr(functional, "_uncompiled_layer_norm", functional.layer_norm)
+ return layer_norm(x, norm.normalized_shape, norm.weight, norm.bias, norm.eps)
+
+
def _route(layer, queries, memory, padding):
- memory = layer.memory_norm(memory)
+ memory = _layer_norm(layer.memory_norm, memory)
routed, _ = layer.attention(
- layer.query_norm(queries), memory, memory, key_padding_mask = padding, need_weights = False
+ _layer_norm(layer.query_norm, queries),
+ memory,
+ memory,
+ key_padding_mask = padding,
+ need_weights = False,
)
queries = queries + layer.attention_dropout(routed)
- return queries + layer.feedforward(layer.feedforward_norm(queries))
+ return queries + layer.feedforward(_layer_norm(layer.feedforward_norm, queries))
def _decode(layer, fields, memory, field_padding, padding):