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.

Studio setup: keep the extension-free mamba-ssm in the stale kernel check (#14687)

Daniel Hand33e77e5404c
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index 019354cc9ec..134d504a2a1 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -8761,9 +8761,14 @@ def _ensure_flash_attn() -> None:
_SSM_KERNEL_EXTENSIONS = {"causal_conv1d": "causal_conv1d_cuda", "mamba_ssm": "selective_scan_cuda"}
# Exit code of the probe below when torch imported and the extension raised ImportError / OSError.
_SSM_EXTENSION_BROKEN_EXIT = 87
+# Missing outright = the extension-free mamba_ssm wheel (Triton route); causal_conv1d has none.
+_SSM_EXTENSION_OPTIONAL = {"selective_scan_cuda"}
_SSM_EXTENSION_PROBE = (
+ "import importlib.util\n"
"import sys\n"
"import torch\n"
+ "if {optional} and importlib.util.find_spec('{extension}') is None:\n"
+ " sys.exit(0)\n"
"try:\n"
" import {extension}\n"
"except (ImportError, OSError) as exc:\n"
@@ -8772,6 +8777,14 @@ _SSM_EXTENSION_PROBE = (
)
+def _ssm_extension_probe(extension: str) -> str:
+ return _SSM_EXTENSION_PROBE.format(
+ extension = extension,
+ code = _SSM_EXTENSION_BROKEN_EXIT,
+ optional = extension in _SSM_EXTENSION_OPTIONAL,
+ )
+
+
def _probe_tail(stderr) -> str:
lines = [line for line in str(stderr or "").strip().splitlines() if line.strip()]
return " | ".join(lines[-3:])[-400:]
@@ -8819,9 +8832,7 @@ def _remove_stale_ssm_kernels() -> None:
for name in installed:
distribution = KERNELS[name][0]
extension = _SSM_KERNEL_EXTENSIONS[name]
- probe = _run_import_probe(
- _SSM_EXTENSION_PROBE.format(extension = extension, code = _SSM_EXTENSION_BROKEN_EXIT)
- )
+ probe = _run_import_probe(_ssm_extension_probe(extension))
if probe is None or probe.returncode == 0:
continue
tail = _probe_tail(probe.stderr)
@@ -8839,9 +8850,7 @@ def _remove_stale_ssm_kernels() -> None:
f"{name} does not load with this torch, but its install lock is busy; kept it"
)
continue
- recheck = _run_import_probe(
- _SSM_EXTENSION_PROBE.format(extension = extension, code = _SSM_EXTENSION_BROKEN_EXIT)
- )
+ recheck = _run_import_probe(_ssm_extension_probe(extension))
if recheck is not None and recheck.returncode == 0:
_note(f"{name} now loads with this torch; kept it")
continue
diff --git a/tests/python/test_ssm_kernels_install_python_stack.py b/tests/python/test_ssm_kernels_install_python_stack.py
index 79cd2133b87..ed1a687c66a 100644
--- a/tests/python/test_ssm_kernels_install_python_stack.py
+++ b/tests/python/test_ssm_kernels_install_python_stack.py
@@ -230,3 +230,54 @@ def test_an_inconclusive_re_probe_under_the_lock_keeps_the_kernel(monkeypatch):
warnings = _run_stale_check(monkeypatch, host, lock = _lock_then_hang)
assert host.removed == []
assert any("kept it" in message for message in warnings)
+
+
+def _real_probe(extension, path):
+ return subprocess.run(
+ [sys.executable, "-c", ips._ssm_extension_probe(extension)],
+ capture_output = True,
+ text = True,
+ timeout = 300,
+ env = {**__import__("os").environ, "PYTHONPATH": str(path)},
+ )
+
+
+# Stand-ins for selective_scan_cuda / causal_conv1d_cuda: a CUDA build on the host cannot interfere.
+_FAKE_SCAN, _FAKE_CONV = "unsloth_test_selective_scan_ext", "unsloth_test_causal_conv1d_ext"
+
+
+def test_the_probe_passes_an_extension_free_mamba_ssm_only(monkeypatch, tmp_path):
+ pytest.importorskip("torch")
+ monkeypatch.setattr(ips, "_SSM_EXTENSION_OPTIONAL", {_FAKE_SCAN})
+ assert _real_probe(_FAKE_SCAN, tmp_path).returncode == 0
+ # causal_conv1d has no extension-free wheel: a missing causal_conv1d_cuda is still broken.
+ missing = _real_probe(_FAKE_CONV, tmp_path)
+ assert missing.returncode == ips._SSM_EXTENSION_BROKEN_EXIT, missing.stderr
+ # A build for another torch: the module is there but does not load.
+ (tmp_path / f"{_FAKE_SCAN}.py").write_text("raise ImportError('undefined symbol: _ZN3c10')\n")
+ broken = _real_probe(_FAKE_SCAN, tmp_path)
+ assert broken.returncode == ips._SSM_EXTENSION_BROKEN_EXIT, broken.stderr
+ assert _FAKE_SCAN in broken.stderr
+
+
+def test_setup_keeps_the_extension_free_mamba_ssm(monkeypatch, tmp_path):
+ # Removing it on every update leaves an offline Mamba-2 load without mamba_ssm.
+ pytest.importorskip("torch")
+ monkeypatch.setattr(ips, "_SSM_KERNEL_EXTENSIONS", {"mamba_ssm": _FAKE_SCAN})
+ monkeypatch.setattr(ips, "_SSM_EXTENSION_OPTIONAL", {_FAKE_SCAN}, raising = False)
+ monkeypatch.setattr(ips, "NO_TORCH", False)
+ monkeypatch.setattr(ips, "IS_MACOS", False)
+ monkeypatch.setattr(
+ ips,
+ "_installed_distribution_version",
+ lambda d: "2.3.2.post1" if d == "mamba-ssm" else None,
+ )
+ removed = []
+ monkeypatch.setattr(ips, "_uninstall_distribution", lambda d: removed.append(d) or True)
+ monkeypatch.setattr(ips, "_count_install_action", lambda: None)
+ monkeypatch.setattr(ips, "kernel_install_lock", _free_lock)
+ messages = []
+ monkeypatch.setattr(ips, "_step", lambda label, value, color_fn = None: messages.append(value))
+ monkeypatch.setattr(ips, "_note", lambda message, color_fn = None: messages.append(message))
+ ips._remove_stale_ssm_kernels()
+ assert removed == [] and messages == []