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 MLX inference test stub neutral for every helper zoo enters around a model (#12548)

Daniel Handc3c76131adb
diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.py
index 0687d1438fb..1121b10469f 100644
--- a/studio/backend/tests/test_mlx_inference_backend.py
+++ b/studio/backend/tests/test_mlx_inference_backend.py
@@ -42,10 +42,23 @@ def mlx_inference_patches(monkeypatch, native_vlm_generation_context):
module = types.ModuleType("unsloth_zoo.mlx.inference")
for name in FUSIONS:
setattr(module, f"fused_{name}", contextlib.nullcontext)
+ module.__getattr__ = _neutral_zoo_helper
monkeypatch.setitem(sys.modules, "unsloth_zoo.mlx.inference", module)
return module
+def _neutral_zoo_helper(name):
+ """Every other public helper zoo's generate.py enters around a model is neutral too.
+
+ unsloth_zoo #1541 added nax_quantized_linear to that import list, and a stub holding only the
+ fused_* names failed every real-model test on macOS with an ImportError. fused_* stay explicit,
+ so a test that deletes one still sees it missing.
+ """
+ if name.startswith("_") or name.startswith("fused_"):
+ raise AttributeError(name)
+ return contextlib.nullcontext
+
+
@pytest.fixture
def mlx_moe(mlx_inference_patches):
return mlx_inference_patches
@@ -329,6 +342,50 @@ def test_mlx_audio_input_generation_survives_every_fusion_refusing(
assert Counter(refused) == Counter(dict.fromkeys(FUSIONS, 1))
+def _zoo_inference_imports():
+ """Names zoo's own modules import from unsloth_zoo.mlx.inference, read from source without importing zoo."""
+ import importlib.util
+
+ spec = importlib.util.find_spec("unsloth_zoo")
+ if spec is None or not spec.submodule_search_locations:
+ pytest.skip("unsloth_zoo source is not available")
+ root = Path(next(iter(spec.submodule_search_locations))) / "mlx"
+ names = set()
+ for path in sorted(root.glob("*.py")):
+ for node in ast.walk(ast.parse(path.read_text(encoding = "utf-8"))):
+ if isinstance(node, ast.ImportFrom) and (
+ (node.level == 1 and node.module == "inference")
+ or node.module == "unsloth_zoo.mlx.inference"
+ ):
+ names.update(alias.name for alias in node.names)
+ if not names:
+ pytest.skip("this unsloth_zoo imports nothing from mlx.inference")
+ return names
+
+
+def test_the_stub_answers_every_helper_zoo_imports_from_inference(mlx_inference_patches):
+ import importlib
+
+ stub = importlib.import_module("unsloth_zoo.mlx.inference")
+ assert stub is mlx_inference_patches
+ missing = sorted(name for name in _zoo_inference_imports() if not hasattr(stub, name))
+ assert not missing, f"zoo imports {missing} from mlx.inference and the stub cannot answer them"
+
+
+def test_an_unknown_zoo_helper_is_neutral_but_a_deleted_fusion_stays_missing(
+ monkeypatch, mlx_inference_patches
+):
+ from unsloth_zoo.mlx.inference import nax_quantized_linear # noqa: F401
+
+ model = object()
+ with mlx_inference_patches.some_future_helper(model) as active:
+ assert active is model
+ monkeypatch.delattr(mlx_inference_patches, "fused_moe_router")
+ with pytest.raises(ImportError):
+ from unsloth_zoo.mlx.inference import fused_moe_router # noqa: F401
+ assert not hasattr(mlx_inference_patches, "_private")
+
+
@pytest.mark.parametrize("feature", FUSIONS)
def test_mlx_missing_inference_export_keeps_native(monkeypatch, mlx_inference_patches, feature):
from core.inference import mlx_inference