"vllm/entrypoints/llm.py" did not exist on "0eda2e0953080467eb98f2730341acea0287cdc0"
test_grouped_topk.py 3.27 KB
Newer Older
1
2
3
4
5
6
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the MoE grouped topk kernel

Run `pytest tests/kernels/moe/test_grouped_topk.py`.
"""
7

8
9
10
import pytest
import torch

11
12
13
14
15
16
from vllm.config import (
    CompilationConfig,
    VllmConfig,
    get_cached_compilation_config,
    set_current_vllm_config,
)
17
from vllm.model_executor.layers.fused_moe.fused_moe import (
Xinyu Chen's avatar
Xinyu Chen committed
18
    GroupedTopk,
19
20
    fused_grouped_topk,
)
21
from vllm.platforms import current_platform
22
from vllm.utils.torch_utils import set_random_seed
23
24


25
26
27
@pytest.mark.skipif(
    not current_platform.is_cuda(), reason="This test is skipped on non-CUDA platform."
)
28
29
30
31
32
33
34
35
36
@pytest.mark.parametrize("n_token", [1, 33, 64])
@pytest.mark.parametrize("n_hidden", [1024, 2048])
@pytest.mark.parametrize("n_expert", [16])
@pytest.mark.parametrize("topk", [2])
@pytest.mark.parametrize("renormalize", [True, False])
@pytest.mark.parametrize("num_expert_group", [8])
@pytest.mark.parametrize("topk_group", [2])
@pytest.mark.parametrize("scoring_func", ["softmax", "sigmoid"])
@pytest.mark.parametrize("routed_scaling_factor", [1.0, 2.5])
37
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32])
38
39
40
41
42
43
44
45
46
47
48
49
50
def test_grouped_topk(
    monkeypatch: pytest.MonkeyPatch,
    n_token: int,
    n_hidden: int,
    n_expert: int,
    topk: int,
    renormalize: bool,
    num_expert_group: int,
    topk_group: int,
    scoring_func: str,
    routed_scaling_factor: float,
    dtype: torch.dtype,
):
51
52
53
54
55
    vllm_config = VllmConfig(
        compilation_config=CompilationConfig(custom_ops=["all", "+grouped_topk"])
    )
    get_cached_compilation_config.cache_clear()

56
    set_random_seed(0)
57
58
59
60
61
    hidden_states = torch.randn((n_token, n_hidden), dtype=dtype, device="cuda")
    gating_output = torch.randn((n_token, n_expert), dtype=dtype, device="cuda")
    e_score_correction_bias = torch.randn(
        (n_expert,), dtype=torch.float32, device="cuda"
    )
62

63
    with set_current_vllm_config(vllm_config), monkeypatch.context() as m:
64
        m.setenv("VLLM_USE_FUSED_MOE_GROUPED_TOPK", "0")
Xinyu Chen's avatar
Xinyu Chen committed
65
        grouped_topk = GroupedTopk(
66
67
68
69
70
71
            topk=topk,
            renormalize=renormalize,
            num_expert_group=num_expert_group,
            topk_group=topk_group,
            scoring_func=scoring_func,
            routed_scaling_factor=routed_scaling_factor,
Xinyu Chen's avatar
Xinyu Chen committed
72
        )
73
        assert grouped_topk._forward_method.__name__ == "forward_cuda"
Xinyu Chen's avatar
Xinyu Chen committed
74
75
76
        baseline_topk_weights, baseline_topk_ids = grouped_topk(
            hidden_states=hidden_states,
            gating_output=gating_output,
77
78
            e_score_correction_bias=e_score_correction_bias,
        )
79
80
81
82
83
84
85
86
87
88

        test_topk_weights, test_topk_ids = fused_grouped_topk(
            hidden_states=hidden_states,
            gating_output=gating_output,
            topk=topk,
            renormalize=renormalize,
            num_expert_group=num_expert_group,
            topk_group=topk_group,
            scoring_func=scoring_func,
            routed_scaling_factor=routed_scaling_factor,
89
90
            e_score_correction_bias=e_score_correction_bias,
        )
91
92

        if renormalize:
93
94
95
96
            torch.testing.assert_close(
                baseline_topk_weights, test_topk_weights, atol=2e-2, rtol=0
            )
        torch.testing.assert_close(baseline_topk_ids, test_topk_ids, atol=0, rtol=0)