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: stop the SDPA probe reporting fused attention on MPS (#14488)

Long Yixingcaf7fe793c00
diff --git a/studio/backend/core/inference/diffusion_attention.py b/studio/backend/core/inference/diffusion_attention.py
index 2dae44b7c3a..8d5b15bf001 100644
--- a/studio/backend/core/inference/diffusion_attention.py
+++ b/studio/backend/core/inference/diffusion_attention.py
@@ -248,6 +248,18 @@ def _enabled_sdpa_kernels(kernels: tuple[str, ...]) -> tuple[str, ...]:
return tuple(k for k in kernels if getattr(torch.backends.cuda, flags[k], lambda: True)())
+MPS_PREFILL_ATTENTION_OFF_ENV = "PYTORCH_MPS_DISABLE_PREFILL_ATTENTION"
+
+
+def _mps_sdpa_kernels(torch: Any) -> tuple[str, ...]:
+ """MPS ignores ``sdpa_kernel``, so answer from the build: only torch 2.13+ has a fused (prefill) kernel."""
+ if _version_tuple(torch.__version__)[:2] < (2, 13):
+ return (SDPA_MATH,)
+ if os.environ.get(MPS_PREFILL_ATTENTION_OFF_ENV, "0") != "0":
+ return (SDPA_MATH,)
+ return (SDPA_FLASH, SDPA_MATH)
+
+
def _probe_sdpa_kernels(device: str, dtype: Any) -> tuple[str, ...]:
"""Probe execution; ROCm runs in fresh children so launch errors stay isolated."""
import torch
@@ -261,6 +273,8 @@ def _probe_sdpa_kernels(device: str, dtype: Any) -> tuple[str, ...]:
)
from core._torchao_stub import _module_is_rocm
+ if device.split(":")[0] == "mps":
+ return _mps_sdpa_kernels(torch)
if device.split(":")[0] == "cuda" and _module_is_rocm(torch):
return _probe_rocm_sdpa_kernels(device, dtype)
# Small enough to be free, but shaped like real attention: the fused kernels reject head_dim they cannot serve, and
@@ -286,11 +300,19 @@ def available_sdpa_kernels(target: Any) -> tuple[str, ...]:
Empty when the probe could not run at all (no torch, no device, an allocator failure) -- an
unanswerable probe must never be read as "only math", which is a claim about the hardware."""
- return _enabled_sdpa_kernels(_sdpa_capability(target))
+ kernels = _sdpa_capability(target)
+ if _target_device(target).split(":")[0] == "mps":
+ # MPS dispatch ignores the torch.backends.cuda toggles.
+ return kernels
+ return _enabled_sdpa_kernels(kernels)
+
+
+def _target_device(target: Any) -> str:
+ return str(getattr(target, "torch_device", None) or getattr(target, "device", "") or "")
def _sdpa_capability(target: Any) -> tuple[str, ...]:
- device = str(getattr(target, "torch_device", None) or getattr(target, "device", "") or "")
+ device = _target_device(target)
if device == "cuda":
ordinal = getattr(target, "ordinal", None)
device = f"cuda:{ordinal}" if ordinal is not None else _indexed_cuda_device(device)
diff --git a/studio/backend/tests/test_diffusion_attention.py b/studio/backend/tests/test_diffusion_attention.py
index 4cba33bacb2..944fc4e2c6f 100644
--- a/studio/backend/tests/test_diffusion_attention.py
+++ b/studio/backend/tests/test_diffusion_attention.py
@@ -856,6 +856,36 @@ def test_a_fused_launch_that_fails_late_is_not_reported_available(monkeypatch):
assert att._probe_sdpa_kernels("cpu", torch.float32) == ("math",)
+ "version, env, fused",
+ [
+ ("2.10.0", None, False),
+ ("2.12.1", None, False),
+ ("2.13.0", None, True),
+ ("2.14.1", "0", True),
+ ("2.14.1", "", False),
+ ("2.14.1", "false", False),
+ ("2.14.1", "1", False),
+ ],
+)
+def test_mps_answers_from_the_torch_build_not_an_execution_probe(version, env, fused, monkeypatch):
+ torch = pytest.importorskip("torch")
+ monkeypatch.setattr(torch, "__version__", version)
+ if env is None:
+ monkeypatch.delenv(att.MPS_PREFILL_ATTENTION_OFF_ENV, raising = False)
+ else:
+ monkeypatch.setenv(att.MPS_PREFILL_ATTENTION_OFF_ENV, env)
+ monkeypatch.setattr(
+ torch.nn.functional,
+ "scaled_dot_product_attention",
+ lambda *a, **k: pytest.fail("MPS must not be execution-probed"),
+ )
+ monkeypatch.setattr(torch.backends.cuda, "flash_sdp_enabled", lambda: False)
+ target = _target("mps")
+ assert att.sdpa_subquadratic_confirmed(target) is fused
+ assert att.sdpa_math_only(target) is not fused
+
+
def test_an_unanswerable_probe_is_not_a_math_only_verdict(monkeypatch):
_stub_probe(monkeypatch, ())
assert att.sdpa_math_only(_target()) is False