fa_utils.py 3.89 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
10
if current_platform.is_cuda():
    from vllm import _custom_ops as ops
11
    
12
    reshape_and_cache_flash = ops.reshape_and_cache_flash
13
    from vllm.vllm_flash_attn import flash_attn_varlen_func, get_scheduler_metadata
14
15
elif current_platform.is_xpu():
    from vllm._ipex_ops import ipex_ops as ops
16

17
18
19
    reshape_and_cache_flash = ops.reshape_and_cache_flash
    flash_attn_varlen_func = ops.flash_attn_varlen_func
    get_scheduler_metadata = ops.get_scheduler_metadata
20
21
elif current_platform.is_rocm():
    try:
22
23
24
25
        # from flash_attn import flash_attn_varlen_func  # noqa: F401
        from vllm import _custom_ops as ops
        from flash_attn import vllm_flash_attn_varlen_func
        reshape_and_cache_cuda = ops.reshape_and_cache_cuda
26
27
28
29
30
    except ImportError as e:
        raise ImportError(
            "Rocm platform requires upstream flash-attn "
            "to be installed. Please install flash-attn first."
        ) from e
31
    
32

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

37
38
    if current_platform.is_xpu():
        return 2
39
40
    try:
        from vllm.vllm_flash_attn.flash_attn_interface import (
41
42
43
44
            fa_version_unsupported_reason,
            is_fa_version_supported,
        )

45
46
47
48
49
        device_capability = current_platform.get_device_capability()

        assert device_capability is not None

        # 1. default version depending on platform
50
51
52
        fa_version = (
            3 if (device_capability.major == 9 and is_fa_version_supported(3)) else 2
        )
53

54
55
56
57
58
59
        # 2. override if passed by environment or config
        from vllm.config import get_current_vllm_config

        vllm_config = get_current_vllm_config()
        if vllm_config.attention_config.flash_attn_version is not None:
            fa_version = vllm_config.attention_config.flash_attn_version
60
61
62

        # 3. fallback for unsupported combinations
        if device_capability.major == 10 and fa_version == 3:
63
64
            logger.warning_once(
                "Cannot use FA version 3 on Blackwell platform "
65
66
                "defaulting to FA version 2."
            )
67
68
69
            fa_version = 2

        if requires_alibi and fa_version == 3:
70
71
72
            logger.warning_once(
                "Cannot use FA version 3 with ALiBi, defaulting to FA version 2."
            )
73
74
75
            fa_version = 2

        if not is_fa_version_supported(fa_version):
76
77
78
79
80
            logger.error(
                "Cannot use FA version %d is not supported due to %s",
                fa_version,
                fa_version_unsupported_reason(fa_version),
            )
81
82
83
84
85

        assert is_fa_version_supported(fa_version)
        return fa_version
    except (ImportError, AssertionError):
        return None
86
87
88


def flash_attn_supports_fp8() -> bool:
89
90
91
92
    return (
        get_flash_attn_version() == 3
        and current_platform.get_device_capability().major == 9
    )
93
94


95
96
97
98
99
def flash_attn_supports_sinks() -> bool:
    if current_platform.is_xpu():
        return True
    else:
        return get_flash_attn_version() == 3
100
101


102
103
def flash_attn_supports_mla():
    from vllm.platforms import current_platform
104

105
106
107
    if current_platform.is_cuda():
        try:
            from vllm.vllm_flash_attn.flash_attn_interface import (
108
109
110
111
112
                is_fa_version_supported,
            )

            return (
                is_fa_version_supported(3)
113
                and current_platform.get_device_capability()[0] == 9
114
            )
115
116
117
118
119
        except (ImportError, AssertionError):
            pass
    return False


120
def is_flash_attn_varlen_func_available() -> bool:
zhuwenwen's avatar
zhuwenwen committed
121
    return current_platform.is_cuda() or current_platform.is_rocm() or current_platform.is_xpu()