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.

Remove xFormers built for another torch after Linux repair (#11783)

xiao0dd7dfc21b6c
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index fbe4cb4dc07..fe39f1a23af 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -7349,19 +7349,46 @@ def _install_wheelhouse_optionals() -> None:
_note(f"windows on arm: installed {name}=={version} from the wheelhouse")
-def _evict_xformers_built_for_another_torch() -> bool:
+def _torch_build_family(label: str) -> str:
+ """cuda<major> / rocm / xpu / cpu from a torch.__version__ local tag, "" when it names none."""
+ tag = label.partition("+")[2].strip().lower()
+ if tag.startswith("cu"):
+ major = _cuda_major_from_torch_version(label)
+ return f"cuda{major}" if major else ""
+ for prefix in ("rocm", "xpu", "cpu"):
+ if tag.startswith(prefix):
+ return prefix
+ return ""
+
+
+def _evict_xformers_built_for_another_torch(
+ scope: str = "windows on arm", family_only: bool = False
+) -> bool:
"""Remove a resident xFormers whose extension was built against another torch. True iff removed.
xFormers links its extension against ONE (torch, CUDA) pair; beside any other it is mute,
- and a package install never uninstalls what an earlier run left behind.
+ and a package install never uninstalls what an earlier run left behind. family_only keeps one
+ of the same family and CUDA major (stable ABI since 0.0.34; _C.so links libcudart.so.<major>).
"""
built_for = _resident_xformers_build_torch()
resident = str(_probe_installed_torch_version() or "")
if not (built_for and resident and built_for != resident):
return False
- _uninstall_distribution("xformers")
+ if family_only:
+ built_family, resident_family = (
+ _torch_build_family(built_for),
+ _torch_build_family(resident),
+ )
+ if not (built_family and resident_family and built_family != resident_family):
+ return False
+ if not _uninstall_distribution("xformers"):
+ _safe_print(
+ f" [WARN] {scope}: xFormers was built for torch {built_for}, not {resident}, "
+ "and could not be removed; its compiled operations stay unavailable."
+ )
+ return False
_note(
- f"windows on arm: the wheelhouse xformers was built for torch "
+ f"{scope}: xFormers was built for torch "
f"{built_for}, not {resident} -- removed; attention uses torch SDPA"
)
return True
@@ -12111,6 +12138,8 @@ def install_python_stack() -> int:
f"{_torch_after_repair} during the repair -- re-selecting torchao"
)
_install_torchao_for_torch(_torch_after_repair)
+ # Unguarded: torch==2.10.0 accepts 2.10.0+rocm7.1, and an earlier run may have moved it.
+ _evict_xformers_built_for_another_torch(scope = "linux torch repair", family_only = True)
_evict_xformers_requiring_another_torch()
# 13w. Windows torch flavor invariant, separate from step 13's Linux-shaped repair set
diff --git a/tests/studio/install/test_cuda_repair.py b/tests/studio/install/test_cuda_repair.py
index e37734593f0..03ac4ace9bb 100644
--- a/tests/studio/install/test_cuda_repair.py
+++ b/tests/studio/install/test_cuda_repair.py
@@ -1959,6 +1959,96 @@ class TestThePackagesTiedToTheTorchReleaseAreResettled:
assert "could not re-check xFormers" in out
+class TestEvictXformersMismatch:
+ def test_a_mismatched_xformers_is_removed_with_its_scope(self, capsys):
+ with (
+ patch.object(
+ stack_mod, "_probe_installed_torch_version", return_value = "2.11.0+rocm7.13.0"
+ ),
+ patch.object(stack_mod, "_resident_xformers_build_torch", return_value = "2.10.0+cu128"),
+ patch.object(stack_mod, "_uninstall_distribution", return_value = True) as uninstall,
+ ):
+ assert (
+ stack_mod._evict_xformers_built_for_another_torch(
+ scope = "linux torch repair", family_only = True
+ )
+ is True
+ )
+ uninstall.assert_called_once_with("xformers")
+ assert "linux torch repair" in capsys.readouterr().out
+
+ def test_a_matching_xformers_is_not_removed(self):
+ with (
+ patch.object(
+ stack_mod, "_probe_installed_torch_version", return_value = "2.11.0+rocm7.13.0"
+ ),
+ patch.object(
+ stack_mod, "_resident_xformers_build_torch", return_value = "2.11.0+rocm7.13.0"
+ ),
+ patch.object(stack_mod, "_uninstall_distribution") as uninstall,
+ ):
+ assert stack_mod._evict_xformers_built_for_another_torch() is False
+ uninstall.assert_not_called()
+
+ @pytest.mark.parametrize(
+ "resident, removed",
+ [("2.11.0+cu128", False), ("2.10.0+cu126", False), ("2.11.0+cu130", True)],
+ )
+ def test_the_cuda_major_decides_on_the_linux_path(self, resident, removed):
+ with (
+ patch.object(stack_mod, "_probe_installed_torch_version", return_value = resident),
+ patch.object(stack_mod, "_resident_xformers_build_torch", return_value = "2.10.0+cu128"),
+ patch.object(stack_mod, "_uninstall_distribution", return_value = True) as uninstall,
+ ):
+ assert (
+ stack_mod._evict_xformers_built_for_another_torch(
+ scope = "linux torch repair", family_only = True
+ )
+ is removed
+ )
+ assert uninstall.called is removed
+
+ @pytest.mark.parametrize(
+ "label, family",
+ [
+ ("2.10.0+cu128", "cuda12"),
+ ("2.11.0+cu130", "cuda13"),
+ ("2.11.0+rocm7.13.0", "rocm"),
+ ("2.10.0+xpu", "xpu"),
+ ("2.10.0+cpu", "cpu"),
+ ("2.10.0", ""),
+ ("2.10.0a0+git1234", ""),
+ ],
+ )
+ def test_the_build_family_is_read_from_the_local_tag(self, label, family):
+ assert stack_mod._torch_build_family(label) == family
+
+ def test_a_blocked_removal_is_reported_not_claimed(self, capsys):
+ with (
+ patch.object(
+ stack_mod, "_probe_installed_torch_version", return_value = "2.10.0+rocm7.1"
+ ),
+ patch.object(stack_mod, "_resident_xformers_build_torch", return_value = "2.10.0+cu128"),
+ patch.object(stack_mod, "_uninstall_distribution", return_value = False),
+ ):
+ assert stack_mod._evict_xformers_built_for_another_torch() is False
+ out = capsys.readouterr().out
+ assert "could not be removed" in out
+ assert "-- removed" not in out
+
+ def test_the_final_repair_checks_the_build_even_when_torch_did_not_move(self):
+ source = inspect.getsource(stack_mod.install_python_stack)
+ step = source.split('_progress(_torch_step_label("final"))', 1)[1]
+ step = step.split("# 13w.", 1)[0]
+ guard = "if _torch_after_repair and _torch_after_repair != _torch_before_repair:"
+ after = step.split(guard, 1)[1]
+ call = (
+ '\n _evict_xformers_built_for_another_torch(scope = "linux torch repair", '
+ "family_only = True)\n"
+ )
+ assert call in after, "the check must sit outside the torch-moved guard"
+
+
class TestTheResidentXformersBuildIsReadFromDisk:
def test_the_recorded_torch_is_returned(self, tmp_path):
pkg = tmp_path / "xformers"
diff --git a/tests/studio/install/test_windows_torch_flavor_invariant.py b/tests/studio/install/test_windows_torch_flavor_invariant.py
index fb1f0dc59bd..45762810653 100644
--- a/tests/studio/install/test_windows_torch_flavor_invariant.py
+++ b/tests/studio/install/test_windows_torch_flavor_invariant.py
@@ -297,7 +297,7 @@ class TestStepThirteenWiring:
["_progress", "_torch_step_label", "_probe_installed_torch_version"]
+ repairs
+ ["_probe_installed_torch_version", "_note", "_install_torchao_for_torch"]
- + ["_evict_xformers_requiring_another_torch"]
+ + ["_evict_xformers_built_for_another_torch", "_evict_xformers_requiring_another_torch"]
), step13
assert "_install_torchao_for_torch" not in _calls_in(guards[0])