test_rocm_attention_selector.py 2.27 KB
Newer Older
1
2
3
4
5
6
7
# SPDX-License-Identifier: Apache-2.0

import pytest
import torch

from vllm.attention.selector import _cached_get_attn_backend, get_attn_backend
from vllm.platforms.rocm import RocmPlatform
8
from vllm.utils import STR_BACKEND_ENV_VAR
9
10
11
12
13
14
15
16
17


@pytest.fixture(autouse=True)
def clear_cache():
    """Clear lru cache to ensure each test case runs without caching.
    """
    _cached_get_attn_backend.cache_clear()


18
19
20
def test_selector(monkeypatch: pytest.MonkeyPatch):
    with monkeypatch.context() as m:
        m.setenv(STR_BACKEND_ENV_VAR, "ROCM_FLASH")
21

22
23
24
25
26
        # Set the current platform to ROCm using monkeypatch
        monkeypatch.setattr("vllm.attention.selector.current_platform",
                            RocmPlatform())

        # Test standard ROCm attention
27
        backend = get_attn_backend(16, torch.float16, torch.float16, 16, False)
28
        assert (backend.get_name() == "ROCM_FLASH"
29
                or backend.get_name() == "TRITON_ATTN_VLLM_V1")
30

31
32
33
34
35
36
37
38
39
40
41
42
        # MLA test for deepseek related

        # change the attention backend to triton MLA
        m.setenv(STR_BACKEND_ENV_VAR, "TRITON_MLA")
        backend = get_attn_backend(576, torch.bfloat16, "auto", 16, False,
                                   False, True)
        assert backend.get_name() == "TRITON_MLA"

        # If attention backend is None
        # If use_mla is true
        # The selected backend is triton MLA
        m.setenv(STR_BACKEND_ENV_VAR, None)
43
44
45
        backend = get_attn_backend(576, torch.bfloat16, "auto", 16, False,
                                   False, True)
        assert backend.get_name() == "TRITON_MLA"
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

        # change the attention backend to AITER MLA
        m.setenv(STR_BACKEND_ENV_VAR, "ROCM_AITER_MLA")
        backend = get_attn_backend(576, torch.bfloat16, "auto", 1, False,
                                   False, True)
        assert backend.get_name() == "ROCM_AITER_MLA"

        # If attention backend is None
        # If use_mla is true
        # If VLLM_ROCM_USE_AITER is enabled
        # The selected backend is ROCM_AITER_MLA
        m.setenv(STR_BACKEND_ENV_VAR, None)
        m.setenv("VLLM_ROCM_USE_AITER", "1")
        backend = get_attn_backend(576, torch.bfloat16, "auto", 1, False,
                                   False, True)
        assert backend.get_name() == "ROCM_AITER_MLA"