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.

Pin the compiled mask wrapper only when it is compiled (#11957)

Daniel Han7d1b02b7be75
diff --git a/tests/test_flex_large_head_dim_fallback.py b/tests/test_flex_large_head_dim_fallback.py
index c204c21cc8c..2793f8c0370 100644
--- a/tests/test_flex_large_head_dim_fallback.py
+++ b/tests/test_flex_large_head_dim_fallback.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""The large-head-dim flex routing: decoder-only scoping, opt-outs, config-driven detection,
-and why the mask is always present under Unsloth."""
+and why the mask is always present under Unsloth's compiled mask wrapper."""
import pytest
@@ -256,7 +256,8 @@ def test_forcing_the_env_var_cannot_override_an_architecture_opt_out(monkeypatch
# Upstream skips the mask for an unpadded batch, but `_ignore_causal_mask_sdpa` returns False
-# while tracing, so Unsloth's compiled create_causal_mask always builds one. Pins both halves.
+# while tracing, so Unsloth's compiled create_causal_mask always builds one. Pins both halves,
+# and that with UNSLOTH_COMPILE_DISABLE=1 the uncompiled wrapper keeps upstream's skip.
def _mask_for(
@@ -316,9 +317,47 @@ def test_upstream_still_materialises_a_mask_when_padded():
assert _mask_for(create, left) is not None
+def _mask_wrapper_is_compiled():
+ """Whether the installed create_causal_mask wrapper calls a compiled function.
+
+ Read off the wrapper rather than UNSLOTH_COMPILE_DISABLE: unsloth_zoo decides once, when it
+ patches at import, and a test module can flip the variable later in the same process. With
+ the switch set, zoo still installs the wrapper (its keyword fixes apply) around the
+ uncompiled original, which is what it stashes.
+ """
+ import inspect
+
+ from transformers import masking_utils
+
+ original = _uncompiled_create_causal_mask()
+ try:
+ inner = inspect.getclosurevars(masking_utils.create_causal_mask).nonlocals.get("f")
+ except TypeError:
+ inner = None
+ if inner is None:
+ pytest.skip("cannot see which function the mask wrapper calls")
+ return inner is not original
+
+
def test_our_compiled_wrapper_is_what_defeats_the_skip():
"""Pins the cause, so this is a deliberate trade and not an accident nobody noticed."""
from transformers import masking_utils
- _uncompiled_create_causal_mask() # skip when the pair does not stash it
+ # Skips when the pair does not stash the original or the wrapper cannot be read.
+ if not _mask_wrapper_is_compiled():
+ pytest.skip("the mask wrapper calls the uncompiled original in this run")
assert _mask_for(masking_utils.create_causal_mask, None) is not None
+
+
+def test_an_uncompiled_wrapper_keeps_the_upstream_skip():
+ """The other half of the cause: without compilation the wrapper changes nothing here.
+
+ unsloth-zoo#1335 made the wrapper install under UNSLOTH_COMPILE_DISABLE=1 too, around the
+ uncompiled original. The mask is then skipped exactly as upstream skips it, so a
+ materialised mask on an unpadded batch comes from compiling, not from wrapping.
+ """
+ from transformers import masking_utils
+
+ if _mask_wrapper_is_compiled():
+ pytest.skip("the mask wrapper is compiled in this run")
+ assert _mask_for(masking_utils.create_causal_mask, None) is None