"tests/quantization/test_auto_round.py" did not exist on "20d8ce81eb9ab8c9a259278d588b73dfb04b7e50"
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
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
    elif device == "openvino":
33
34
        with patch("vllm.attention.selector.current_platform.is_openvino",
                   return_value=True):
35
36
            backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
                                        False)
37
        assert backend.name == "OPENVINO"
38
    else:
39
        backend = which_attn_to_use(16, torch.float16, torch.float16, 16,
40
                                    False)
41
42
43
        assert backend.name == name


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

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
48
49

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

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

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

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

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

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

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


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