test_attention_selector.py 3.37 KB
Newer Older
1
2
3
4
5
from unittest.mock import patch

import pytest
import torch

6
from tests.kernels.utils import override_backend_env_variable
7
from vllm.attention.selector import which_attn_to_use
8
from vllm.utils import STR_FLASH_ATTN_VAL, STR_INVALID_VAL
9
10


11
12
13
# @pytest.mark.parametrize(
#     "name", ["TORCH_SDPA", "ROCM_FLASH", "XFORMERS", "FLASHINFER", "OPENVINO"])
# @pytest.mark.parametrize("device", ["cpu", "openvino", "hip", "cuda"])
14
@pytest.mark.parametrize(
15
16
    "name", ["TORCH_SDPA", "ROCM_FLASH", "XFORMERS", "FLASHINFER"])
@pytest.mark.parametrize("device", ["cpu", "hip", "cuda"])
17
def test_env(name: str, device: str, monkeypatch):
18
19
20
    """Test that the attention selector can be set via environment variable.
    Note that we do not test FlashAttn because it is the default backend.
    """
21
22

    override_backend_env_variable(monkeypatch, name)
23
24
25
26
27
28
29
30
31
32
33

    if device == "cpu":
        with patch("vllm.attention.selector.is_cpu", return_value=True):
            backend = which_attn_to_use(8, 16, 8, None, torch.float16,
                                        torch.float16, 16)
        assert backend.name == "TORCH_SDPA"
    elif device == "hip":
        with patch("vllm.attention.selector.is_hip", return_value=True):
            backend = which_attn_to_use(8, 16, 8, None, torch.float16,
                                        torch.float16, 16)
        assert backend.name == "ROCM_FLASH"
34
35
36
37
38
    elif device == "openvino":
        with patch("vllm.attention.selector.is_openvino", return_value=True):
            backend = which_attn_to_use(8, 16, 8, None, torch.float16,
                                        torch.float16, 16)
        assert backend.name == "OPENVINO"
39
40
41
42
43
44
    else:
        backend = which_attn_to_use(8, 16, 8, None, torch.float16,
                                    torch.float16, 16)
        assert backend.name == name


45
def test_flash_attn(monkeypatch):
46
    """Test FlashAttn validation."""
47
48

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
49
50

    # Unsupported CUDA arch
51
    with patch("torch.cuda.get_device_capability", return_value=(7, 5)):
52
        backend = which_attn_to_use(8, 16, 8, None, torch.float16, None, 16)
53
        assert backend.name != STR_FLASH_ATTN_VAL
54
55
56

    # Unsupported data type
    backend = which_attn_to_use(8, 16, 8, None, torch.float8_e4m3fn, None, 16)
57
    assert backend.name != STR_FLASH_ATTN_VAL
58
59
60

    # Unsupported kv cache data type
    backend = which_attn_to_use(8, 16, 8, None, torch.float16, "fp8", 16)
61
    assert backend.name != STR_FLASH_ATTN_VAL
62
63
64

    # Unsupported block size
    backend = which_attn_to_use(8, 16, 8, None, torch.float16, None, 8)
65
    assert backend.name != STR_FLASH_ATTN_VAL
66
67
68

    # Unsupported sliding window
    backend = which_attn_to_use(8, 16, 8, 1, torch.float16, None, 16)
69
    assert backend.name != STR_FLASH_ATTN_VAL
70
71
72
73

    # flash-attn is not installed
    with patch.dict('sys.modules', {'vllm_flash_attn': None}):
        backend = which_attn_to_use(8, 16, 8, None, torch.float16, None, 16)
74
        assert backend.name != STR_FLASH_ATTN_VAL
75
76
77

    # Unsupported head size
    backend = which_attn_to_use(8, 17, 8, None, torch.float16, None, 16)
78
    assert backend.name != STR_FLASH_ATTN_VAL
79
80


81
def test_invalid_env(monkeypatch):
82
    """Throw an exception if the backend name is invalid."""
83
    override_backend_env_variable(monkeypatch, STR_INVALID_VAL)
84
85
    with pytest.raises(ValueError):
        which_attn_to_use(8, 16, 8, None, torch.float16, None, 16)