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: mark Unsloth's own compiled region apart from a user's enclosing compile (#15526)

Daniel Han73560d36077c
diff --git a/tests/test_block_compile.py b/tests/test_block_compile.py
index ce2a27c54b7..dbafb428e1b 100644
--- a/tests/test_block_compile.py
+++ b/tests/test_block_compile.py
@@ -253,6 +253,46 @@ def test_outer_compile_owns_the_block(monkeypatch, fullgraph, suppress):
assert inner
[email protected]("fullgraph", [True, False], ids = ["fullgraph", "default"])
+def test_compiled_region_flag_only_in_own_block(monkeypatch, fullgraph):
+ """_IN_COMPILED_REGION marks Unsloth's own block compile (a graph break is legal there), not the block
+ traced inside a user's compile, which also sets _IN_BLOCK (unsloth_zoo's sparse flex reads it)."""
+ M = _load()
+ real_compile = torch.compile
+
+ class ToyDecoderLayer(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.proj = torch.nn.Linear(8, 8)
+
+ def forward(self, hidden_states):
+ # Flags read where the zoo reads them: inside the traced layer.
+ in_block = float(M._IN_BLOCK[0] != M._OFF)
+ region = float(M._IN_COMPILED_REGION[0])
+ return self.proj(hidden_states) + in_block + 10 * region
+
+ monkeypatch.setattr(
+ torch,
+ "compile",
+ lambda fn, **kwargs: real_compile(fn, backend = "eager", fullgraph = False, dynamic = None),
+ )
+ original = ToyDecoderLayer.forward
+ M._wrap(ToyDecoderLayer, {})
+ monkeypatch.setattr(torch, "compile", real_compile)
+ torch.manual_seed(0)
+ layer = ToyDecoderLayer().train()
+ x = torch.randn(2, 4, 8)
+ stock = original(layer, x).detach()
+
+ torch._dynamo.reset()
+ torch.testing.assert_close(layer(x).detach(), stock + 11) # Unsloth's block: both flags
+ torch._dynamo.reset()
+ outer = torch.compile(layer, backend = "eager", fullgraph = fullgraph)
+ torch.testing.assert_close(outer(x).detach(), stock + 1) # user's compile: _IN_BLOCK only
+ torch._dynamo.reset()
+ assert M._IN_BLOCK[0] == M._OFF and M._IN_COMPILED_REGION[0] is False
+
+
def _install_blocks(M, model, monkeypatch):
"""patch_block_compile on CPU with an eager inner compile, restored at teardown."""
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
diff --git a/unsloth/models/_block_compile.py b/unsloth/models/_block_compile.py
index 72790c5d8f3..e76e202171d 100644
--- a/unsloth/models/_block_compile.py
+++ b/unsloth/models/_block_compile.py
@@ -692,6 +692,9 @@ def _precheck_band(layer, seq_len, args, kwargs):
# Mode of the block being traced (_SPLIT: flex_attention outside); dynamo guards on it.
_IN_BLOCK = [0]
+# True only inside Unsloth's own compiled block (fullgraph=False, a graph break is legal), not when the
+# block is inlined into a user's torch.compile, which may be fullgraph=True. Read by unsloth_zoo.
+_IN_COMPILED_REGION = [False]
_PACKED_MODULE = "unsloth.utils.hf_packed_attention"
_NO_PACKED = [False]
_OFF, _FULL, _SPLIT = 0, 1, 2
@@ -868,6 +871,7 @@ def _wrap(
previous = inline_flag[0]
inline_flag[0] = True
_IN_BLOCK[0] = mode
+ _IN_COMPILED_REGION[0] = True
packed_module = sys.modules.get(_PACKED_MODULE)
packed = getattr(packed_module, "IN_COMPILED_BLOCK", _NO_PACKED)
packed_previous = packed[0]
@@ -899,6 +903,7 @@ def _wrap(
finally:
inline_flag[0] = previous
_IN_BLOCK[0] = _OFF
+ _IN_COMPILED_REGION[0] = False
packed[0] = packed_previous
def _inline(self, hidden_states, args, kwargs, mode):