Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/transformers/modeling_flash_attention_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def is_flash_attn_available():
2: {
"flash_attn_version": 2,
"general_availability_check": is_flash_attn_2_available,
"pkg_availability_check": lambda *args, **kwargs: importlib.util.find_spec("flash_attn") is not None
and "flash-attn" in [pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn"]],
"pkg_availability_check": lambda *args, **kwargs: (
importlib.util.find_spec("flash_attn") is not None
and "flash-attn" in [pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn"]]
),
"supported_devices": (
(is_torch_cuda_available, "cuda"),
(is_torch_mlu_available, "mlu"),
Expand All @@ -93,16 +95,17 @@ def is_flash_attn_available():
3: {
"flash_attn_version": 3,
"general_availability_check": is_flash_attn_3_available,
"pkg_availability_check": lambda *args, **kwargs: importlib.util.find_spec("flash_attn_interface") is not None
and "flash-attn-3" in [pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn_interface"]],
"pkg_availability_check": is_flash_attn_3_available, # defers to actual import probing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also use cuda checks, we only want to check for metadata here

"supported_devices": ((is_torch_cuda_available, "cuda"),),
"cuda_min_major_version": 8, # Ampere
"cuda_min_major_version": 9, # Hopper (sm90+) — FA3 does NOT run on Ampere

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plain wrong, and have checked since ages that ampere works

},
4: {
"flash_attn_version": 4,
"general_availability_check": is_flash_attn_4_available,
"pkg_availability_check": lambda *args, **kwargs: importlib.util.find_spec("flash_attn") is not None
and "flash-attn-4" in [pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn"]],
"pkg_availability_check": lambda *args, **kwargs: (
importlib.util.find_spec("flash_attn") is not None
and "flash-attn-4" in [pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn"]]
),
"supported_devices": ((is_torch_cuda_available, "cuda"),),
"cuda_min_major_version": 9, # Hopper
},
Expand Down Expand Up @@ -163,7 +166,14 @@ def _lazy_imports(
from .integrations.npu_flash_attention import npu_flash_attn_with_kvcache as flash_attn_with_kvcache
else:
if implementation == "flash_attention_3" or (implementation is None and is_fa3 and not is_fa4):
from flash_attn_interface import flash_attn_func, flash_attn_varlen_func, flash_attn_with_kvcache
# FA3 Hopper kernels live under different module paths depending on
# install method. Try standalone wheel first, then hopper subpackage.
try:
from flash_attn_interface import flash_attn_func, flash_attn_varlen_func, flash_attn_with_kvcache
except ImportError:
from hopper.flash_attn_interface import flash_attn_func, flash_attn_varlen_func # type: ignore[no-redef]

flash_attn_with_kvcache = None # hopper wheel may not expose this

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, given that this weird install doesn't have the kv cache function, it implies something is seriously wrong because the existing functions are not properly exposed.

elif implementation == "flash_attention_4" or (implementation is None and is_fa4):
from flash_attn.cute import flash_attn_func, flash_attn_varlen_func

Expand Down
24 changes: 17 additions & 7 deletions src/transformers/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,23 @@ def is_flash_attn_2_available() -> bool:

@lru_cache
def is_flash_attn_3_available() -> bool:
# Universally available under `flash_attn_interface`
is_available = _is_package_available("flash_attn_interface")[0]
# Resolving and ensuring the proper name of FA3 being associated
is_available = is_available and "flash-attn-3" in [
pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn_interface"]
]
return is_available and is_torch_cuda_available()
if not is_torch_cuda_available():
return False

# FA3 Hopper kernels can live under different module paths depending on
# how they were installed:
# 1. "flash_attn_interface" — standalone flash-attn-3 wheel
# 2. "hopper.flash_attn_interface" — built from flash-attention/hopper/
Comment on lines +971 to +972

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Dao-AILab/flash-attention?tab=readme-ov-file#flashattention-3-beta-release shows the usage to be

import flash_attn_interface
flash_attn_interface.flash_attn_func()

So I don't see why hopper.flash_attn_interface should be valid - that implies some weird structure inherited from pip which shouldn't be used

# The previous check only looked at package distribution metadata which
# doesn't match most real-world installs. Try the actual imports instead.
for mod_name in ("flash_attn_interface", "hopper.flash_attn_interface"):
try:
mod = __import__(mod_name, fromlist=["flash_attn_func"])
if hasattr(mod, "flash_attn_func"):
return True
except Exception:
continue
return False


@lru_cache
Expand Down
Loading