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.

Use side stream offload hooks for non-reentrant gradient checkpointing (#13872)

Daniel Hanc6f69b9f2430
diff --git a/tests/test_nonreentrant_gc_side_stream_offload.py b/tests/test_nonreentrant_gc_side_stream_offload.py
new file mode 100644
index 00000000000..ded5bd5e68e
--- /dev/null
+++ b/tests/test_nonreentrant_gc_side_stream_offload.py
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team.
+"""Non-reentrant "unsloth" checkpointing offloads through unsloth_zoo's side stream hooks.
+
+The old hooks queued the D2H copy on the compute stream, so it could not overlap compute.
+Gradients must stay identical to checkpointing without offload.
+"""
+
+import json
+import os
+import subprocess
+import sys
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+pytestmark = pytest.mark.skipif(
+ not torch.cuda.is_available(), reason = "importing unsloth needs a GPU"
+)
+
+_CHILD = r"""
+import json
+import unsloth # noqa: F401
+import torch
+from transformers import LlamaConfig, LlamaForCausalLM
+from unsloth.models.vision import FastBaseModel
+import unsloth_zoo.gradient_checkpointing as zoo_gc
+
+def run(mode):
+ torch.manual_seed(0)
+ config = LlamaConfig(
+ vocab_size = 128, hidden_size = 256, intermediate_size = 512, num_hidden_layers = 4,
+ num_attention_heads = 4, num_key_value_heads = 2, max_position_embeddings = 256,
+ )
+ model = LlamaForCausalLM(config).cuda()
+ # deepseek_v41 is the model type post_patch_model always runs non-reentrant.
+ model.config.model_type = "deepseek_v41"
+ model = FastBaseModel.post_patch_model(model, use_gradient_checkpointing = mode, trust_remote_code = True)
+ model.gradient_checkpointing_enable(gradient_checkpointing_kwargs = {"use_reentrant": True})
+ model.train()
+ params = [p for n, p in model.named_parameters() if "layers." in n and "proj" in n]
+ for p in params: p.requires_grad_(True)
+ ids = torch.randint(0, 128, (2, 128), device = "cuda", generator = torch.Generator("cuda").manual_seed(1))
+ loss = model(input_ids = ids, labels = ids).loss
+ loss.backward()
+ torch.cuda.synchronize()
+ return loss.item(), [p.grad.detach().float().cpu() for p in params]
+
+ref_loss, ref_grads = run(True)
+loss, grads = run("unsloth")
+offloader = getattr(zoo_gc, "NONREENTRANT_OFFLOADER", None)
+print("@@@" + json.dumps({
+ "same_loss": loss == ref_loss,
+ "same_grads": all(torch.equal(a, b) for a, b in zip(grads, ref_grads)),
+ "pinned_side_stream_copies": None if offloader is None else offloader.stats["pinned"],
+}))
+"""
+
+
+def test_unsloth_non_reentrant_offload_uses_side_stream_hooks(tmp_path):
+ zoo_gc = pytest.importorskip("unsloth_zoo.gradient_checkpointing")
+ if not hasattr(zoo_gc, "NonReentrantActivationOffloader"):
+ pytest.skip("unsloth_zoo predates the side stream offload hooks")
+ env = dict(os.environ)
+ repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ env["PYTHONPATH"] = os.pathsep.join(
+ [repo_root] + ([env["PYTHONPATH"]] if env.get("PYTHONPATH") else [])
+ )
+ env.setdefault("UNSLOTH_COMPILE_LOCATION", str(tmp_path / "unsloth_compiled_cache"))
+ result = subprocess.run(
+ [sys.executable, "-c", _CHILD],
+ cwd = str(tmp_path),
+ env = env,
+ capture_output = True,
+ text = True,
+ timeout = 900,
+ )
+ assert result.returncode == 0, result.stdout[-4000:] + result.stderr[-4000:]
+ line = next(l for l in result.stdout.splitlines() if l.startswith("@@@"))
+ out = json.loads(line[3:])
+ assert out["same_loss"] and out["same_grads"], out
+ assert out["pinned_side_stream_copies"], out
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index d5574bba6e5..b48c2f2785f 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -191,6 +191,11 @@ from unsloth_zoo.gradient_checkpointing import (
unpatch_unsloth_gradient_checkpointing,
unpatch_unsloth_smart_gradient_checkpointing,
)
+
+try:
+ from unsloth_zoo.gradient_checkpointing import non_reentrant_offload_hooks
+except ImportError: # older unsloth_zoo
+ non_reentrant_offload_hooks = None
import torch.utils.checkpoint as torch_checkpoint
import transformers.modeling_utils as hf_modeling_utils
from peft import LoraConfig, TaskType, get_peft_model as _get_peft_model
@@ -2998,6 +3003,15 @@ def _offload_activation_unpack(packed):
return x if device is None else x.to(device, non_blocking = True)
+def _offload_activation_hooks():
+ # The zoo hooks copy on a side stream; the fallback queues the copy on the compute stream.
+ if non_reentrant_offload_hooks is not None:
+ return non_reentrant_offload_hooks()
+ return torch.autograd.graph.saved_tensors_hooks(
+ _offload_activation_pack, _offload_activation_unpack
+ )
+
+
_NON_REENTRANT_GC_MODEL_TYPES = ("deepseek_v41",)
@@ -4996,9 +5010,7 @@ class FastBaseModel:
kwargs["use_reentrant"] = False
if not _offload:
return _orig_checkpoint(function, *args, **kwargs)
- with torch.autograd.graph.saved_tensors_hooks(
- _offload_activation_pack, _offload_activation_unpack
- ):
+ with _offload_activation_hooks():
return _orig_checkpoint(function, *args, **kwargs)
_nonre_checkpoint._unsloth_original = _orig_checkpoint