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.

Block compile: keep the GatedDeltaNet self-check out of a non-reentrant checkpoint (#14195)

oobaboogacefb057a4eeb
diff --git a/tests/test_block_compile.py b/tests/test_block_compile.py
index fbf51a15862..a856e81f639 100644
--- a/tests/test_block_compile.py
+++ b/tests/test_block_compile.py
@@ -362,6 +362,44 @@ def test_gdn_self_check_leaves_default_rng_alone():
assert torch.equal(torch.cuda.get_rng_state(), state)
+def test_gdn_self_check_stays_out_of_a_non_reentrant_checkpoint(monkeypatch):
+ # #14063: the check's autograd.grad must not recompute the checkpointed layer mid-forward.
+ from torch.utils.checkpoint import checkpoint
+
+ G = _load("_gdn_op")
+ chunk = types.ModuleType("fla.ops.gated_delta_rule.chunk")
+ for name in (
+ "chunk_gated_delta_rule_fwd",
+ "chunk_gated_delta_rule_bwd",
+ "l2norm_fwd",
+ "l2norm_bwd",
+ ):
+ setattr(chunk, name, lambda *a, **k: None)
+ for name in ("fla", "fla.ops", "fla.ops.gated_delta_rule"):
+ monkeypatch.setitem(sys.modules, name, types.ModuleType(name))
+ monkeypatch.setitem(sys.modules, "fla.ops.gated_delta_rule.chunk", chunk)
+
+ def self_check(q, k, v, g, beta):
+ ins = [t.requires_grad_() for t in (q, k, v)]
+ out = (ins[0] * ins[1]).sigmoid() * ins[2]
+ return all(gr is not None for gr in torch.autograd.grad(out.sum(), ins))
+
+ monkeypatch.setattr(G, "_self_check", self_check)
+ calls = []
+ w = torch.randn(8, 8, requires_grad = True)
+
+ def layer(x):
+ calls.append(1)
+ h = (x @ w).sigmoid()
+ G._fla_ops(lambda: (h, h, h, h, h))
+ return (h @ w).sigmoid()
+
+ checkpoint(layer, torch.randn(4, 8), use_reentrant = False).sum().backward()
+ assert G._FLA
+ assert len(calls) == 2 # forward, then one recompute in backward
+ assert w.grad is not None
+
+
def test_sm_count_is_read_outside_the_trace(monkeypatch):
# Stands in for Triton's hip_utils.get_device_properties, which Dynamo cannot trace.
M = _load()
diff --git a/unsloth/models/_gdn_op.py b/unsloth/models/_gdn_op.py
index 1d2f125d268..ff4038f1a73 100644
--- a/unsloth/models/_gdn_op.py
+++ b/unsloth/models/_gdn_op.py
@@ -7,6 +7,7 @@ as one graph."""
import torch
import torch.nn.functional as F
+from torch.autograd.graph import saved_tensors_hooks
def _causal_depthwise_conv(x, weight):
@@ -40,11 +41,12 @@ def _fla_ops(make_inputs):
C.l2norm_fwd,
C.l2norm_bwd,
)
- # The first call can come from a no_grad checkpoint forward.
+ # The first call can come from a no_grad checkpoint forward, or a non-reentrant one
+ # whose hooks pack the check's tensors; detached so a failed check leaves no cycle.
with torch.inference_mode(False):
with torch.no_grad():
inputs = [t.detach().clone() for t in make_inputs()]
- with torch.enable_grad():
+ with torch.enable_grad(), saved_tensors_hooks(lambda t: t.detach(), lambda t: t):
_FLA = fns
_FLA = fns if _self_check(*inputs) else False
except Exception: