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.

Flash impl: warn when an explicit fa3 / fa4 request falls back on the device or torch (#15554)

Daniel Hand8cd1e7e753d
diff --git a/tests/utils/test_flash_impl.py b/tests/utils/test_flash_impl.py
index 47e50a9050c..eb28ce06f01 100644
--- a/tests/utils/test_flash_impl.py
+++ b/tests/utils/test_flash_impl.py
@@ -798,3 +798,34 @@ def test_hidden_size_follows_get_text_config():
return types.SimpleNamespace(hidden_size = 3072)
assert fi._hidden_size(types.SimpleNamespace(config = Config())) == 3072
+
+
[email protected]("value", ["fa3", "fa4"])
[email protected]("case", ["device", "registry"])
+def test_explicit_request_that_falls_back_warns(fake, monkeypatch, caplog, value, case):
+ import logging
+
+ monkeypatch.setitem(fi.FLASH_IMPL_STATE, "logged", set())
+ monkeypatch.setenv(fi._ENV, value)
+ if case == "device":
+ monkeypatch.setattr(fi, "_device_majors", lambda: {8})
+ reason = "no supported impl for devices sm[8]"
+ else:
+ monkeypatch.setattr(fi, "_registry", lambda: None)
+ reason = "torch has no flash attention impl registry (< 2.11)"
+ with caplog.at_level(logging.WARNING, logger = fi.__name__):
+ assert fi.maybe_activate_flash_attention_impl(_model()) is None
+ assert fi.maybe_activate_flash_attention_impl(_model()) is None
+ warnings = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
+ assert warnings == [f"Unsloth: {fi._ENV}={value} not used: {reason}"]
+ assert fake.probes == [] and fake.current_impl is None
+
+
+def test_auto_fallback_stays_quiet(fake, monkeypatch, caplog):
+ import logging
+
+ monkeypatch.setitem(fi.FLASH_IMPL_STATE, "logged", set())
+ monkeypatch.setattr(fi, "_device_majors", lambda: {8})
+ with caplog.at_level(logging.WARNING, logger = fi.__name__):
+ assert fi.maybe_activate_flash_attention_impl(_model()) is None
+ assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
diff --git a/unsloth/utils/flash_impl.py b/unsloth/utils/flash_impl.py
index 7755473ab83..375396e07cd 100644
--- a/unsloth/utils/flash_impl.py
+++ b/unsloth/utils/flash_impl.py
@@ -74,6 +74,14 @@ def _log_once(message, level = logging.INFO):
_logger.log(level, message)
+def _warn_explicit(requested):
+ if requested not in (None, "auto"):
+ _log_once(
+ f"Unsloth: {_ENV}={requested.lower()} not used: {FLASH_IMPL_STATE['reason']}",
+ logging.WARNING,
+ )
+
+
def _registry():
try:
from torch.nn.attention import (
@@ -409,6 +417,7 @@ def maybe_activate_flash_attention_impl(model = None, mean_document_length = Non
FLASH_IMPL_STATE["reason"] = (
"off" if requested is None else "torch has no flash attention impl registry (< 2.11)"
)
+ _warn_explicit(requested)
return None
if torch.compiler.is_compiling():
return FLASH_IMPL_STATE["active"]
@@ -422,6 +431,7 @@ def maybe_activate_flash_attention_impl(model = None, mean_document_length = Non
if not candidates:
FLASH_IMPL_STATE["reason"] = f"no supported impl for devices sm{sorted(majors or [])}"
deactivate_flash_attention_impl()
+ _warn_explicit(requested)
return None
major = next(iter(majors))
if requested == "auto" and major == 9:
@@ -472,8 +482,7 @@ def maybe_activate_flash_attention_impl(model = None, mean_document_length = Non
if error is not None:
FLASH_IMPL_STATE["reason"] = error
deactivate_flash_attention_impl()
- if requested != "auto":
- _log_once(f"Unsloth: {_ENV}={requested.lower()} not used: {error}", logging.WARNING)
+ _warn_explicit(requested)
return None
FLASH_IMPL_STATE["active"] = impl
FLASH_IMPL_STATE["reason"] = f"{impl} active"