test_attention_selector.py 3.28 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
from vllm.utils import is_hip
10
11
12


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

    override_backend_env_variable(monkeypatch, name)
21
22
23
24
25
26
27
28
29
30
31

    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"
32
33
34
35
36
    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"
37
38
39
40
41
42
    else:
        backend = which_attn_to_use(8, 16, 8, None, torch.float16,
                                    torch.float16, 16)
        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(8, 16, 8, None, torch.float16, None, 16)
51
        assert backend.name != STR_FLASH_ATTN_VAL
52
53
54

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

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

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

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

    # 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)
72
        assert backend.name != STR_FLASH_ATTN_VAL
73
74
75

    # Unsupported head size
    backend = which_attn_to_use(8, 17, 8, None, torch.float16, None, 16)
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
83
    with pytest.raises(ValueError):
        which_attn_to_use(8, 16, 8, None, torch.float16, None, 16)