Skip to content

Commit 54c6003

Browse files
committed
flash attention protection for < CUDA sm_80
1 parent eaa397b commit 54c6003

4 files changed

Lines changed: 446 additions & 2 deletions

File tree

library/anima_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,8 @@ def forward(
14721472
can_use_flash = (
14731473
attention.flash_attn_varlen_func is not None
14741474
and query_states.dtype in (torch.float16, torch.bfloat16)
1475+
and torch.cuda.is_available()
1476+
and torch.cuda.get_device_capability(query_states.device) >= (8, 0)
14751477
)
14761478

14771479
if can_use_flash and q_mask is None and kv_mask is None:

library/attention.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Unified attention function supporting various implementations
22

3+
import logging
34
from dataclasses import dataclass
45
import torch
56
from typing import Optional, Union
67

8+
_logger = logging.getLogger(__name__)
9+
710
try:
811
import flash_attn
912
from flash_attn.flash_attn_interface import _flash_attn_forward
@@ -15,6 +18,39 @@
1518
_flash_attn_forward = None
1619
flash_attn_func = None
1720

21+
# Verify flash_attn GPU compatibility: requires Ampere (sm_80, compute capability 8.0) or newer.
22+
# T4 (Turing, sm_75) and older GPUs will hit "FlashAttention only supports Ampere GPUs or newer"
23+
# at runtime. Detect this early and disable flash_attn so callers fall back gracefully.
24+
if flash_attn is not None:
25+
try:
26+
if torch.cuda.is_available():
27+
_capability = torch.cuda.get_device_capability()
28+
if _capability < (8, 0):
29+
_logger.warning(
30+
"flash_attn is installed but requires Ampere GPU (sm_80) or newer. "
31+
f"Current GPU compute capability: {_capability[0]}.{_capability[1]}. "
32+
"Disabling flash_attn and falling back to other attention implementations."
33+
)
34+
flash_attn = None
35+
flash_attn_varlen_func = None
36+
_flash_attn_forward = None
37+
flash_attn_func = None
38+
except Exception:
39+
_logger.debug("Could not determine GPU capability for flash_attn compatibility check")
40+
41+
42+
def is_flash_attn_supported() -> bool:
43+
"""Return True if flash_attn is importable AND the current GPU supports it (Ampere+)."""
44+
if flash_attn_varlen_func is None:
45+
return False
46+
try:
47+
if torch.cuda.is_available():
48+
return torch.cuda.get_device_capability() >= (8, 0)
49+
except Exception:
50+
pass
51+
return False
52+
53+
1854
try:
1955
from sageattention import sageattn_varlen, sageattn
2056
except ImportError:

library/lumina_models.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# MAE: https://github.com/facebookresearch/mae/blob/main/models_mae.py
2020
# --------------------------------------------------------
2121

22+
import logging
2223
import math
2324
from typing import List, Optional, Tuple
2425
from dataclasses import dataclass
@@ -31,13 +32,30 @@
3132

3233
from library import custom_offloading_utils
3334

35+
_logger = logging.getLogger(__name__)
36+
3437
try:
3538
from flash_attn import flash_attn_varlen_func
3639
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
3740
except ImportError:
3841
# flash_attn may not be available but it is not required
3942
pass
4043

44+
# Verify flash_attn GPU compatibility: requires Ampere (sm_80) or newer.
45+
if "flash_attn_varlen_func" in dir():
46+
try:
47+
if torch.cuda.is_available():
48+
_capability = torch.cuda.get_device_capability()
49+
if _capability < (8, 0):
50+
_logger.warning(
51+
"flash_attn is installed but requires Ampere GPU (sm_80) or newer. "
52+
f"Current GPU compute capability: {_capability[0]}.{_capability[1]}. "
53+
"Disabling flash_attn for lumina models."
54+
)
55+
flash_attn_varlen_func = None
56+
except Exception:
57+
_logger.debug("Could not determine GPU capability for flash_attn compatibility check")
58+
4159
try:
4260
from sageattention import sageattn
4361
except ImportError:
@@ -317,6 +335,21 @@ def __init__(
317335
else:
318336
self.q_norm = self.k_norm = nn.Identity()
319337

338+
# Disable flash_attn if not importable or GPU doesn't support it (requires Ampere+)
339+
# flash_attn_varlen_func is a module-level name; access it via the global scope.
340+
# Three states: (1) import failed -> NameError, (2) GPU unsupported -> None, (3) available -> function
341+
try:
342+
_flash_available = flash_attn_varlen_func is not None
343+
except NameError:
344+
_flash_available = False
345+
346+
if use_flash_attn and not _flash_available:
347+
_logger.warning(
348+
"Flash attention requested but not available (not installed or GPU does not support it). "
349+
"Falling back to standard attention."
350+
)
351+
use_flash_attn = False
352+
320353
self.use_flash_attn = use_flash_attn
321354
self.use_sage_attn = use_sage_attn
322355

@@ -544,9 +577,10 @@ def flash_attn(
544577
# end var_len_flash_attn
545578

546579
return output
547-
except NameError as e:
580+
except (NameError, TypeError) as e:
548581
raise RuntimeError(
549-
f"Could not load flash attention. Please install flash_attn. / フラッシュアテンションを読み込めませんでした。flash_attn をインストールしてください。 / {e}"
582+
f"Could not load flash attention. Please install flash_attn (requires Ampere GPU or newer). / "
583+
f"フラッシュアテンションを読み込めませんでした。flash_attn をインストールしてください(Ampere GPU以降が必要です)。 / {e}"
550584
)
551585

552586

0 commit comments

Comments
 (0)