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.

Skip xformers FA3 on H100 when the build has no sm_90a kernels (#13619)

Michael Han11ee56726161
diff --git a/tests/utils/test_xformers_capability_gate.py b/tests/utils/test_xformers_capability_gate.py
index 77ecb328edf..077794e89a4 100644
--- a/tests/utils/test_xformers_capability_gate.py
+++ b/tests/utils/test_xformers_capability_gate.py
@@ -30,11 +30,90 @@ def test_capability_gate(capability, probe_result, expect_disabled):
calls["n"] += 1
return probe_result
- assert ad._xformers_disabled_for_capability(capability, probe = probe) is expect_disabled
+ assert (
+ ad._xformers_disabled_for_capability(capability, probe = probe, fa3_enabled = lambda: True)
+ is expect_disabled
+ )
# Below sm_120 the probe must not run at all (no import-time kernel launch there).
assert calls["n"] == (0 if capability[0] < 12 else 1)
+ "capability, fa3_enabled, probe_result, expect_disabled, expect_probed",
+ [
+ ((9, 0), True, None, False, False), # sm_90a build (PyPI wheels): FA3 kept, never probed
+ ((9, 0), False, True, False, True), # sm_90 build whose FA2 / cutlass run (#3963)
+ ((9, 0), False, False, True, True), # build with no sm_90 kernel at all: SDPA
+ ((8, 0), False, None, False, False), # FA3 off elsewhere does not probe
+ ],
+)
+def test_hopper_gate_probes_only_when_fa3_was_turned_off(
+ capability, fa3_enabled, probe_result, expect_disabled, expect_probed
+):
+ calls = {"n": 0}
+
+ def probe():
+ calls["n"] += 1
+ return probe_result
+
+ assert (
+ ad._xformers_disabled_for_capability(
+ capability, probe = probe, fa3_enabled = lambda: fa3_enabled
+ )
+ is expect_disabled
+ )
+ assert calls["n"] == int(expect_probed)
+
+
+class _Build:
+ def __init__(self, arch_list):
+ self.build_env = {"TORCH_CUDA_ARCH_LIST": arch_list}
+
+
+class _Flash3:
+ def __init__(
+ self,
+ version = "2.8.0",
+ loaded = True,
+ ):
+ self.FLASH_VERSION = version
+ self._C_flashattention3 = object() if loaded else None
+
+
+ "arch_list, capability, expect",
+ [
+ # conda-forge xformers 0.0.32.post2 (#3963) and 0.0.34: no sm_90a anywhere.
+ ("5.0;6.0;7.0;7.5;8.0;8.6;8.9;9.0;10.0;12.0+PTX", (9, 0), True),
+ ("8.0;8.9;10.0;12.0+PTX", (9, 0), True),
+ ("7.5 8.0+PTX 9.0a", (9, 0), False),
+ ("9.0a+PTX", (9, 0), False),
+ (None, (9, 0), False),
+ ("8.0;8.9;10.0;12.0+PTX", (8, 0), False),
+ ("8.0;8.9;10.0;12.0+PTX", (10, 0), False),
+ ],
+)
+def test_fa3_lacks_sm90a(arch_list, capability, expect):
+ from unsloth.models._utils import _xformers_fa3_lacks_sm90a
+ assert _xformers_fa3_lacks_sm90a(_Build(arch_list), capability, _Flash3()) is expect
+
+
+def test_fa3_lacks_sm90a_ignores_external_or_missing_fa3():
+ from unsloth.models._utils import _xformers_fa3_lacks_sm90a
+
+ conda = _Build("8.0;8.9;10.0;12.0+PTX")
+ # xformers >= 0.0.35 loads FA3 from a separate flash_attn_3 package.
+ assert _xformers_fa3_lacks_sm90a(conda, (9, 0), _Flash3(version = "pip_pkg")) is False
+ assert _xformers_fa3_lacks_sm90a(conda, (9, 0), _Flash3(loaded = False)) is False
+ assert _xformers_fa3_lacks_sm90a(conda, (9, 0), None) is False
+
+
+def test_fa3_lacks_sm90a_fails_open_on_unreadable_metadata():
+ from unsloth.models._utils import _xformers_fa3_lacks_sm90a
+ assert _xformers_fa3_lacks_sm90a(None, (9, 0), _Flash3()) is False
+ assert _xformers_fa3_lacks_sm90a(object(), (9, 0), _Flash3()) is False
+
+
@pytest.mark.skipif(
not (has_real_cuda() and ad.HAS_XFORMERS),
reason = "needs a CUDA GPU with a working xformers build",
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index ec0ab6524cf..be4c6a64df9 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -3822,6 +3822,23 @@ elif DEVICE_TYPE == "npu":
# Ask torch_npu: the module-level False silently rewrote an explicit bfloat16 to float16.
SUPPORTS_BFLOAT16 = torch.npu.is_bf16_supported()
+
+def _xformers_fa3_lacks_sm90a(build_info, capability, flash3) -> bool:
+ # Bundled FA3 is compiled for TORCH_CUDA_ARCH_LIST verbatim; without "9.0a" (conda-forge) its wgmma
+ # kernels trap on H100 and poison the CUDA context (#3963). Unset list = xformers' "8.0 9.0a" default.
+ if tuple(capability) != (9, 0):
+ return False
+ if getattr(flash3, "_C_flashattention3", None) is None:
+ return False
+ if getattr(flash3, "FLASH_VERSION", None) == "pip_pkg":
+ return False
+ try:
+ arch_list = str(build_info.build_env.get("TORCH_CUDA_ARCH_LIST") or "")
+ except Exception:
+ return False
+ return bool(arch_list) and "9.0a" not in arch_list
+
+
try:
_xformers_logger = logging.getLogger("xformers")
_xformers_logger.setLevel(logging.ERROR)
@@ -3883,7 +3900,7 @@ try:
from xformers._cpp_lib import _register_extensions
try:
- _register_extensions()
+ _xformers_build = _register_extensions()
except Exception as error:
raise ImportError(
"Unsloth: Xformers was not installed correctly.\n"
@@ -3894,6 +3911,17 @@ try:
) from error
import xformers.ops.fmha as xformers
+ if (
+ DEVICE_TYPE == "cuda"
+ and torch.cuda.is_available()
+ and hasattr(xformers, "_set_use_fa3")
+ and _xformers_fa3_lacks_sm90a(
+ _xformers_build, torch.cuda.get_device_capability(), getattr(xformers, "flash3", None)
+ )
+ ):
+ xformers._set_use_fa3(False)
+ del _xformers_build
+
xformers_attention = xformers.memory_efficient_attention
except ModuleNotFoundError:
xformers = None
diff --git a/unsloth/utils/attention_dispatch.py b/unsloth/utils/attention_dispatch.py
index 5e0c1048cb1..9af708bbc7f 100644
--- a/unsloth/utils/attention_dispatch.py
+++ b/unsloth/utils/attention_dispatch.py
@@ -56,13 +56,22 @@ def _xformers_runs_on_device() -> bool:
return False
-def _xformers_disabled_for_capability(capability, probe = _xformers_runs_on_device) -> bool:
+def _xformers_fa3_enabled() -> bool:
+ return getattr(xformers, "_get_use_fa3", lambda: True)()
+
+
+def _xformers_disabled_for_capability(
+ capability,
+ probe = _xformers_runs_on_device,
+ fa3_enabled = _xformers_fa3_enabled,
+) -> bool:
# At sm_120 (RTX 50-series) xformers' cutlass op is capability-rejected (it caps at sm_90) and its
- # flash-2 op runs only if the build ships an sm_120 kernel, so run one real forward to decide;
- # below sm_120 xformers always works.
- if capability[0] < 12:
- return False
- return not probe()
+ # flash-2 op runs only if the build ships an sm_120 kernel, so run one real forward to decide.
+ # On sm_90 FA3 is off only for builds without sm_90a (_utils), which may also ship no sm_90 kernel
+ # at all (conda-forge xformers >= 0.0.33), so probe those too; elsewhere xformers works.
+ if capability[0] >= 12 or (tuple(capability) == (9, 0) and not fa3_enabled()):
+ return not probe()
+ return False
# FlashAttention always wins in select_attention_backend and nothing downgrades flash to xformers,