test_attention_selector.py 3.24 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
28
        assert backend.name == "TORCH_SDPA"
    elif device == "hip":
        with patch("vllm.attention.selector.is_hip", return_value=True):
29
30
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
31
        assert backend.name == "ROCM_FLASH"
32
33
    elif device == "openvino":
        with patch("vllm.attention.selector.is_openvino", return_value=True):
34
35
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
36
        assert backend.name == "OPENVINO"
37
    else:
38
        backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
39
                                    False)
40
41
42
        assert backend.name == name


43
def test_flash_attn(monkeypatch):
44
    """Test FlashAttn validation."""
45
46

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
47
48

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

    # Unsupported data type
54
    backend = which_attn_to_use(16, torch.float8_e4m3fn, None, 16, False)
55
    assert backend.name != STR_FLASH_ATTN_VAL
56
57

    # Unsupported kv cache data type
58
    backend = which_attn_to_use(16, torch.float16, "fp8", 16, False)
59
    assert backend.name != STR_FLASH_ATTN_VAL
60
61

    # Unsupported block size
62
    backend = which_attn_to_use(16, torch.float16, None, 8, False)
63
    assert backend.name != STR_FLASH_ATTN_VAL
64
65
66

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

    # Unsupported head size
71
    backend = which_attn_to_use(17, torch.float16, None, 16, False)
72
73
74
    assert backend.name != STR_FLASH_ATTN_VAL

    # Attention-free models should bypass env and use PlaceholderAttention
75
    backend = which_attn_to_use(16, torch.float16, torch.float16, 16, True)
76
    assert backend.name != STR_FLASH_ATTN_VAL
77
78


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