test_expert_parallel.py 6.17 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4

from dataclasses import dataclass
5
from typing import Literal, NamedTuple, Optional
6
7
8

import pytest

9
from vllm.config import RunnerOption
10
11
from vllm.logger import init_logger

12
from ..utils import compare_two_settings, create_new_process_for_each_test
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

logger = init_logger("test_expert_parallel")


class ParallelSetup(NamedTuple):
    tp_size: int
    eager_mode: bool
    chunked_prefill: bool


class EPTestOptions(NamedTuple):
    trust_remote_code: bool
    tokenizer_mode: Optional[str]
    load_format: Optional[str] = None
    hf_overrides: Optional[str] = None


@dataclass
class EPTestSettings:
32
33
    parallel_setups: list[ParallelSetup]
    distributed_backends: list[str]
34
    runner: RunnerOption
35
36
37
38
39
40
    test_options: EPTestOptions

    @staticmethod
    def detailed(
        *,
        tp_base: int = 2,
41
        runner: RunnerOption = "auto",
42
43
44
45
46
47
48
        trust_remote_code: bool = False,
        tokenizer_mode: Optional[str] = None,
        load_format: Optional[str] = None,
        hf_overrides: Optional[str] = None,
    ):
        return EPTestSettings(
            parallel_setups=[
49
50
51
52
53
54
55
56
57
                ParallelSetup(tp_size=tp_base, eager_mode=False, chunked_prefill=False),
                ParallelSetup(tp_size=tp_base, eager_mode=False, chunked_prefill=True),
                ParallelSetup(tp_size=tp_base, eager_mode=True, chunked_prefill=False),
                ParallelSetup(
                    tp_size=2 * tp_base, eager_mode=False, chunked_prefill=True
                ),
                ParallelSetup(
                    tp_size=2 * tp_base, eager_mode=True, chunked_prefill=False
                ),
58
59
            ],
            distributed_backends=["mp", "ray"],
60
            runner=runner,
61
62
63
64
65
66
            test_options=EPTestOptions(
                trust_remote_code=trust_remote_code,
                tokenizer_mode=tokenizer_mode,
                load_format=load_format,
                hf_overrides=hf_overrides,
            ),
67
68
69
70
71
72
        )

    @staticmethod
    def fast(
        *,
        tp_base: int = 2,
73
        runner: RunnerOption = "auto",
74
75
76
77
78
79
80
        trust_remote_code: bool = False,
        tokenizer_mode: Optional[str] = None,
        load_format: Optional[str] = None,
        hf_overrides: Optional[str] = None,
    ):
        return EPTestSettings(
            parallel_setups=[
81
                ParallelSetup(tp_size=tp_base, eager_mode=True, chunked_prefill=False),
82
83
            ],
            distributed_backends=["mp"],
84
            runner=runner,
85
86
87
88
89
90
            test_options=EPTestOptions(
                trust_remote_code=trust_remote_code,
                tokenizer_mode=tokenizer_mode,
                load_format=load_format,
                hf_overrides=hf_overrides,
            ),
91
92
93
94
95
96
97
        )

    def iter_params(self, model_name: str):
        opts = self.test_options

        for parallel_setup in self.parallel_setups:
            for distributed_backend in self.distributed_backends:
98
99
100
101
102
103
104
                yield (
                    model_name,
                    parallel_setup,
                    distributed_backend,
                    self.runner,
                    opts,
                )
105
106
107
108
109
110


# NOTE: You can adjust tp_base locally to fit the model in GPU
# The values displayed here are only a rough indicator of the size of the model

TEST_MODELS = {
111
    "deepseek-ai/DeepSeek-V2-Lite-Chat": EPTestSettings.fast(trust_remote_code=True),
112
113
114
115
116
117
118
119
    "mistralai/Mixtral-8x7B-Instruct-v0.1": EPTestSettings.fast(tp_base=4),
}


def _compare_tp(
    model_name: str,
    parallel_setup: ParallelSetup,
    distributed_backend: str,
120
    runner: RunnerOption,
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    test_options: EPTestOptions,
    num_gpus_available: int,
    *,
    method: Literal["generate"],
):
    (
        tp_size,
        eager_mode,
        chunked_prefill,
    ) = parallel_setup
    (
        trust_remote_code,
        tokenizer_mode,
        load_format,
        hf_overrides,
    ) = test_options

    if num_gpus_available < tp_size:
        pytest.skip(f"Need at least {tp_size} GPUs")

    common_args = [
        # use half precision for speed and memory savings in CI environment
        "--dtype",
        "float16",
        "--max-model-len",
        "2048",
        "--max-num-seqs",
        "8",
        "--load-format",
        "auto",
    ]
    if chunked_prefill:
        common_args.append("--enable-chunked-prefill")
    if eager_mode:
        common_args.append("--enforce-eager")
156
157
    if runner != "auto":
        common_args.extend(["--runner", runner])
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    if trust_remote_code:
        common_args.append("--trust-remote-code")
    if tokenizer_mode:
        common_args.extend(["--tokenizer-mode", tokenizer_mode])
    if load_format:
        common_args.extend(["--load-format", load_format])
    if hf_overrides:
        common_args.extend(["--hf-overrides", hf_overrides])

    ep_env = {
        "VLLM_TEST_ENABLE_EP": "1",
    }

    ep_args = [
        *common_args,
        "--tensor-parallel-size",
        str(tp_size),
        "--distributed-executor-backend",
        distributed_backend,
    ]

    # compare without expert parallelism
    tp_env = {
        "VLLM_TEST_ENABLE_EP": "0",
    }

    tp_args = [
        *common_args,
        "--tensor-parallel-size",
        str(tp_size),
        "--distributed-executor-backend",
        "mp",
    ]

    try:
193
194
195
196
197
198
199
200
201
        compare_two_settings(
            model_name,
            ep_args,
            tp_args,
            ep_env,
            tp_env,
            method=method,
            max_wait_seconds=360,
        )
202
203
204
205
206
    except Exception:
        raise


@pytest.mark.parametrize(
207
    ("model_name", "parallel_setup", "distributed_backend", "runner", "test_options"),
208
    [
209
210
        params
        for model_name, settings in TEST_MODELS.items()
211
212
213
        for params in settings.iter_params(model_name)
    ],
)
214
@create_new_process_for_each_test()
215
216
217
218
def test_ep(
    model_name: str,
    parallel_setup: ParallelSetup,
    distributed_backend: str,
219
    runner: RunnerOption,
220
221
222
    test_options: EPTestOptions,
    num_gpus_available,
):
223
224
225
226
227
228
229
230
231
    _compare_tp(
        model_name,
        parallel_setup,
        distributed_backend,
        runner,
        test_options,
        num_gpus_available,
        method="generate",
    )