fa_utils.py 3.96 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4

from vllm.logger import init_logger
5
from vllm.platforms import current_platform
6
7
8

logger = init_logger(__name__)

9
if current_platform.is_cuda():
10
    from vllm._custom_ops import reshape_and_cache_flash
11
12
13
14
15
    from vllm.vllm_flash_attn import (  # type: ignore[attr-defined]
        flash_attn_varlen_func,
        get_scheduler_metadata,
    )

16
elif current_platform.is_xpu():
17
    from vllm._ipex_ops import ipex_ops
18

19
20
21
    reshape_and_cache_flash = ipex_ops.reshape_and_cache_flash
    flash_attn_varlen_func = ipex_ops.flash_attn_varlen_func
    get_scheduler_metadata = ipex_ops.get_scheduler_metadata
22

23
24
25
elif current_platform.is_rocm():
    try:
        from flash_attn import flash_attn_varlen_func  # noqa: F401
26
27
28
29
30
31
32
    except ImportError:

        def flash_attn_varlen_func(*args, **kwargs):
            raise ImportError(
                "ROCm platform requires upstream flash-attn "
                "to be installed. Please install flash-attn first."
            )
33

34

35
def get_flash_attn_version(requires_alibi: bool = False) -> int | None:
36
37
    # import here to avoid circular dependencies
    from vllm.platforms import current_platform
38

39
40
    if current_platform.is_xpu():
        return 2
41
42
43
    if current_platform.is_rocm():
        # ROCm doesn't use vllm_flash_attn; return None to skip fa_version arg
        return None
44
45
    try:
        from vllm.vllm_flash_attn.flash_attn_interface import (
46
47
48
49
            fa_version_unsupported_reason,
            is_fa_version_supported,
        )

50
51
52
53
54
        device_capability = current_platform.get_device_capability()

        assert device_capability is not None

        # 1. default version depending on platform
55
56
57
        fa_version = (
            3 if (device_capability.major == 9 and is_fa_version_supported(3)) else 2
        )
58

59
        # 2. override if passed by environment or config
60
        from vllm.config import get_current_vllm_config_or_none
61

62
63
64
65
66
        vllm_config = get_current_vllm_config_or_none()
        if (
            vllm_config is not None
            and vllm_config.attention_config.flash_attn_version is not None
        ):
67
            fa_version = vllm_config.attention_config.flash_attn_version
68
69
70

        # 3. fallback for unsupported combinations
        if device_capability.major == 10 and fa_version == 3:
71
            logger.warning_once(
72
                "Cannot use FA version 3 on Blackwell platform, "
73
74
                "defaulting to FA version 2."
            )
75
76
77
            fa_version = 2

        if requires_alibi and fa_version == 3:
78
79
80
            logger.warning_once(
                "Cannot use FA version 3 with ALiBi, defaulting to FA version 2."
            )
81
82
83
            fa_version = 2

        if not is_fa_version_supported(fa_version):
84
85
86
87
88
            logger.error(
                "Cannot use FA version %d is not supported due to %s",
                fa_version,
                fa_version_unsupported_reason(fa_version),
            )
89
90
91
92
93

        assert is_fa_version_supported(fa_version)
        return fa_version
    except (ImportError, AssertionError):
        return None
94
95
96


def flash_attn_supports_fp8() -> bool:
97
98
    return (
        get_flash_attn_version() == 3
99
        and current_platform.is_device_capability_family(90)
100
    )
101
102


103
104
105
106
107
108
109
def flash_attn_supports_sinks() -> bool:
    if current_platform.is_xpu():
        return True
    else:
        return get_flash_attn_version() == 3


110
111
def flash_attn_supports_mla():
    from vllm.platforms import current_platform
112

113
114
115
    if current_platform.is_cuda():
        try:
            from vllm.vllm_flash_attn.flash_attn_interface import (
116
117
118
                is_fa_version_supported,
            )

119
120
121
            return is_fa_version_supported(
                3
            ) and current_platform.is_device_capability_family(90)
122
123
124
125
126
        except (ImportError, AssertionError):
            pass
    return False


127
128
def is_flash_attn_varlen_func_available() -> bool:
    return current_platform.is_cuda() or current_platform.is_xpu()