"vscode:/vscode.git/clone" did not exist on "33156f56e0b1a4e872048669ae625fe3067f9c24"
test_attention_selector.py 3.19 KB
Newer Older
1
2
3
4
5
from unittest.mock import patch

import pytest
import torch

6
7
from tests.kernels.utils import (STR_FLASH_ATTN_VAL, STR_INVALID_VAL,
                                 override_backend_env_variable)
8
9
10
11
from vllm.attention.selector import which_attn_to_use


@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
22
23
24
25
26
27
28
29
30

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


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

    override_backend_env_variable(monkeypatch, STR_FLASH_ATTN_VAL)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    # Unsupported CUDA arch
    with patch("torch.cuda.get_device_capability", return_value=[7, 5]):
        backend = which_attn_to_use(8, 16, 8, None, torch.float16, None, 16)
        assert backend.name != "FLASH_ATTN"

    # Unsupported data type
    backend = which_attn_to_use(8, 16, 8, None, torch.float8_e4m3fn, None, 16)
    assert backend.name != "FLASH_ATTN"

    # Unsupported kv cache data type
    backend = which_attn_to_use(8, 16, 8, None, torch.float16, "fp8", 16)
    assert backend.name != "FLASH_ATTN"

    # Unsupported block size
    backend = which_attn_to_use(8, 16, 8, None, torch.float16, None, 8)
    assert backend.name != "FLASH_ATTN"

    # Unsupported sliding window
    backend = which_attn_to_use(8, 16, 8, 1, torch.float16, None, 16)
    assert backend.name != "FLASH_ATTN"

    # 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)
        assert backend.name != "FLASH_ATTN"

    # Unsupported head size
    backend = which_attn_to_use(8, 17, 8, None, torch.float16, None, 16)
    assert backend.name != "FLASH_ATTN"


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