Repository navigation
Fix flash_attention_3 detection and import for hopper wheel installs #45387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"), | ||
|
|
@@ -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 | ||
| "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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }, | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 So I don't see why |
||
| # 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 | ||
|
|
||
There was a problem hiding this comment.
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