test_grouped_topk.py 3.5 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
import vllm.model_executor.layers.batch_invariant as batch_invariant
12
13
14
15
16
17
from vllm.config import (
    CompilationConfig,
    VllmConfig,
    get_cached_compilation_config,
    set_current_vllm_config,
)
18
from vllm.model_executor.layers.fused_moe.router.grouped_topk_router import (
Xinyu Chen's avatar
Xinyu Chen committed
19
    GroupedTopk,
20
21
    fused_grouped_topk,
)
22
from vllm.platforms import current_platform
23
from vllm.utils.torch_utils import set_random_seed
24
25


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

65
    set_random_seed(0)
66
67
68
    hidden_states = torch.randn((n_token, n_hidden), dtype=input_dtype, device="cuda")
    gating_output = torch.randn((n_token, n_expert), dtype=input_dtype, device="cuda")
    e_score_correction_bias = torch.randn((n_expert,), dtype=bias_dtype, device="cuda")
69

70
    with set_current_vllm_config(vllm_config), monkeypatch.context() as m:
71
        m.setenv("VLLM_USE_FUSED_MOE_GROUPED_TOPK", "0")
72
        m.setattr(batch_invariant, "VLLM_BATCH_INVARIANT", True)
Xinyu Chen's avatar
Xinyu Chen committed
73
        grouped_topk = GroupedTopk(
74
75
76
77
78
79
            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
80
        )
81
        assert grouped_topk._forward_method.__name__ == "forward_cuda"
Xinyu Chen's avatar
Xinyu Chen committed
82
83
84
        baseline_topk_weights, baseline_topk_ids = grouped_topk(
            hidden_states=hidden_states,
            gating_output=gating_output,
85
86
            e_score_correction_bias=e_score_correction_bias,
        )
87
88
89
90
91
92
93
94
95
96

        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,
97
98
            e_score_correction_bias=e_score_correction_bias,
        )
99

100
101
102
        torch.testing.assert_close(
            baseline_topk_weights, test_topk_weights, atol=2e-2, rtol=0
        )
103
        torch.testing.assert_close(baseline_topk_ids, test_topk_ids, atol=0, rtol=0)