test_phimoe.py 2.93 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
import pytest
import torch

6
from vllm.platforms import current_platform
7

8
from ....utils import large_gpu_test
9
from ...utils import check_logprobs_close
10
11
12
13
14
15
16
17

MODELS = [
    "microsoft/Phi-3.5-MoE-instruct",
]


def test_phimoe_routing_function():
    from vllm.model_executor.models.phimoe import phimoe_routing_function
18

19
20
    test_case = {
        0: {
21
22
23
24
25
26
27
28
            "hidden_states": torch.tensor(
                [1, 2, 3, 4, 5, 6, 7, 8], dtype=torch.float32, requires_grad=False
            ).view(4, 2),
            "gating_output": torch.tensor(
                [0.1, 0.2, 0.3, 0.4], dtype=torch.float32, requires_grad=False
            ),
            "topk": 2,
            "renormalize": False,
29
30
        },
        1: {
31
32
33
34
35
36
37
38
39
            "hidden_states": torch.tensor(
                [1, 2, 3, 4, 5, 6, 7, 8], dtype=torch.float32, requires_grad=False
            ).view(4, 2),
            "gating_output": torch.tensor(
                [0.4, 0.2, 0.3, 0.4], dtype=torch.float32, requires_grad=False
            ),
            "topk": 2,
            "renormalize": False,
        },
40
41
42
43
    }

    ground_truth = {
        0: {
44
45
46
47
            "topk_weights": torch.tensor(
                [1.0, 1.0], dtype=torch.float32, requires_grad=False
            ),
            "topk_ids": torch.tensor([3, 2], dtype=torch.long, requires_grad=False),
48
49
        },
        1: {
50
51
52
53
54
            "topk_weights": torch.tensor(
                [0.5, 1.0], dtype=torch.float32, requires_grad=False
            ),
            "topk_ids": torch.tensor([0, 3], dtype=torch.long, requires_grad=False),
        },
55
56
57
58
    }

    for test_id in test_case:
        topk_weights, topk_ids = phimoe_routing_function(**test_case[test_id])
59
        assert torch.allclose(topk_weights, ground_truth[test_id]["topk_weights"])
60
61
62
        assert torch.equal(topk_ids, ground_truth[test_id]["topk_ids"])


63
64
65
66
67
@pytest.mark.skipif(
    condition=current_platform.is_cpu(),
    reason="This test takes a lot time to run on CPU, "
    "and vllm CI's disk space is not enough for this model.",
)
68
@large_gpu_test(min_gb=80)
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@pytest.mark.parametrize("model", MODELS)
@pytest.mark.parametrize("dtype", ["bfloat16"])
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
def test_models(
    hf_runner,
    vllm_runner,
    example_prompts,
    model: str,
    dtype: str,
    max_tokens: int,
    num_logprobs: int,
) -> None:
    with hf_runner(model, dtype=dtype) as hf_model:
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
84
85
            example_prompts, max_tokens, num_logprobs
        )
86
87
88

    with vllm_runner(model, dtype=dtype) as vllm_model:
        vllm_outputs = vllm_model.generate_greedy_logprobs(
89
90
            example_prompts, max_tokens, num_logprobs
        )
91
92
93
94
95
96
    check_logprobs_close(
        outputs_0_lst=hf_outputs,
        outputs_1_lst=vllm_outputs,
        name_0="hf",
        name_1="vllm",
    )