test_attention_selector.py 3.4 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


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

    override_backend_env_variable(monkeypatch, name)
20
21

    if device == "cpu":
22
23
        with patch("vllm.attention.selector.current_platform.is_cpu",
                   return_value=True):
24
25
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
26
27
        assert backend.name == "TORCH_SDPA"
    elif device == "hip":
28
29
        with patch("vllm.attention.selector.current_platform.is_rocm",
                   return_value=True):
30
31
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
32
        assert backend.name == "ROCM_FLASH"
33
    elif device == "openvino":
34
35
        with patch("vllm.attention.selector.current_platform.is_openvino",
                   return_value=True):
36
37
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
38
        assert backend.name == "OPENVINO"
39
    else:
40
        backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
41
                                    False)
42
43
44
        assert backend.name == name


45
def test_flash_attn(monkeypatch):
46
    """Test FlashAttn validation."""
Joe Runde's avatar
Joe Runde committed
47
48
    # TODO: When testing for v1, pipe in `use_v1` as an argument to
    # which_attn_to_use
49
50

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
51
52

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

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

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

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

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

    # Unsupported head size
75
    backend = which_attn_to_use(17, torch.float16, None, 16, False)
76
77
78
    assert backend.name != STR_FLASH_ATTN_VAL

    # Attention-free models should bypass env and use PlaceholderAttention
79
    backend = which_attn_to_use(16, torch.float16, torch.float16, 16, True)
80
    assert backend.name != STR_FLASH_ATTN_VAL
81
82


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