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 the checkpoint_skip_layers="auto" planner out of torch.compile regions (#14928)

Daniel Han969bbfc81b78
diff --git a/tests/test_gc_skip_auto.py b/tests/test_gc_skip_auto.py
index 8d2fae3ba76..be1a889d9e8 100644
--- a/tests/test_gc_skip_auto.py
+++ b/tests/test_gc_skip_auto.py
@@ -604,3 +604,48 @@ def test_counts_apply_only_to_checkpointable_layers():
for i in (1, 3, 5):
model.model.layers[i] = torch.nn.Linear(4, 4)
assert ns["skip_checkpointing"](model, 3) == [0, 2, 4]
+
+
[email protected](not has_real_cuda(), reason = "the auto planner sizes CUDA memory")
[email protected]("mode", ["fullgraph", "dynamic"])
+def test_auto_planner_stays_out_of_compiled_regions(mode):
+ # TrainingArguments(torch_compile = True) compiles the whole model; the planner must stay out of it.
+ from torch._dynamo.utils import counters
+
+ class Wrapped(torch.nn.Module):
+ def __init__(self, base):
+ super().__init__()
+ self.base = base
+ self.config = base.config
+ self.max_seq_length = base.max_seq_length
+
+ def forward(self, input_ids):
+ hidden = self.base.model(input_ids = input_ids, use_cache = False).last_hidden_state
+ return self.base.lm_head(hidden).float().logsumexp(-1).mean()
+
+ ns, _ = _load()
+ base = _model(layers = 4, device = "cuda")
+ model = Wrapped(base)
+ ns["skip_checkpointing"](model, "auto")
+ planner = model._unsloth_auto_skip
+ torch._dynamo.reset()
+ counters.clear()
+ compiled = torch.compile(
+ model,
+ backend = "aot_eager",
+ fullgraph = mode == "fullgraph",
+ dynamic = True if mode == "dynamic" else None,
+ )
+ try:
+ for seq in (8, 12, 16) if mode == "dynamic" else (8, 8, 8):
+ ids = torch.randint(0, 128, (2, seq), device = "cuda")
+ if mode == "dynamic":
+ torch._dynamo.mark_dynamic(ids, 1)
+ compiled(ids).backward()
+ model.zero_grad(set_to_none = True)
+ assert sum(counters["graph_break"].values()) == 0, dict(counters["graph_break"])
+ assert planner.started is None and planner.kept is None
+ assert _checkpointed(base) == [True] * 4
+ finally:
+ planner.remove()
+ torch._dynamo.reset()
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 08a6688e302..ba860305cff 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -9296,6 +9296,9 @@ class _AutoCheckpointSkip:
return None
def _model_pre_hook(self, module, args):
+ # Planning under torch.compile breaks fullgraph/dynamic tracing; compiled models stay fully checkpointed.
+ if torch.compiler.is_compiling():
+ return
if not module.training or not torch.is_grad_enabled():
return
device = self._plan_device()
@@ -9314,6 +9317,8 @@ class _AutoCheckpointSkip:
self.plan(device)
def _probe_pre_hook(self, index, module, args):
+ if torch.compiler.is_compiling():
+ return
if self.started is None or index in self.measured or not module.training:
return
if not torch.is_grad_enabled():
@@ -9333,6 +9338,8 @@ class _AutoCheckpointSkip:
self.before = (index, torch.cuda.memory_allocated(hidden.device), hidden.device)
def _probe_hook(self, index, module, args, output):
+ if torch.compiler.is_compiling():
+ return
if self.before is None or self.before[0] != index or index in self.measured:
return
device = self.before[2]