test_attention_selector.py 3.64 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.platforms import cpu, cuda, openvino, rocm
9
from vllm.utils import STR_FLASH_ATTN_VAL, STR_INVALID_VAL
zhuwenwen's avatar
zhuwenwen committed
10
from vllm.platforms import current_platform
11
12
13


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

    override_backend_env_variable(monkeypatch, name)
22
23

    if device == "cpu":
24
25
        with patch("vllm.attention.selector.current_platform",
                   cpu.CpuPlatform()):
26
27
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
28
29
        assert backend.name == "TORCH_SDPA"
    elif device == "hip":
30
31
        with patch("vllm.attention.selector.current_platform",
                   rocm.RocmPlatform()):
32
33
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
34
        assert backend.name == "ROCM_FLASH"
35
    elif device == "openvino":
36
37
        with patch("vllm.attention.selector.current_platform",
                   openvino.OpenVinoPlatform()):
38
39
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
40
        assert backend.name == "OPENVINO"
41
    else:
42
43
44
45
        with patch("vllm.attention.selector.current_platform",
                   cuda.CudaPlatform()):
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
46
47
48
        assert backend.name == name


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

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
55
56

    # Unsupported CUDA arch
57
    with patch("torch.cuda.get_device_capability", return_value=(7, 5)):
58
        backend = which_attn_to_use(16, torch.float16, None, 16, False)
59
        assert backend.name != STR_FLASH_ATTN_VAL
60
61

    # Unsupported data type
62
    backend = which_attn_to_use(16, torch.float8_e4m3fn, None, 16, False)
63
    assert backend.name != STR_FLASH_ATTN_VAL
64
65

    # Unsupported kv cache data type
66
    backend = which_attn_to_use(16, torch.float16, "fp8", 16, False)
67
    assert backend.name != STR_FLASH_ATTN_VAL
68
69

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

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

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

    # Attention-free models should bypass env and use PlaceholderAttention
83
    backend = which_attn_to_use(16, torch.float16, torch.float16, 16, True)
84
    assert backend.name != STR_FLASH_ATTN_VAL
85
86


87
def test_invalid_env(monkeypatch):
88
    """Throw an exception if the backend name is invalid."""
89
    override_backend_env_variable(monkeypatch, STR_INVALID_VAL)
90
    with pytest.raises(ValueError):
91
        which_attn_to_use(16, torch.float16, None, 16, False)