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

Mor Zusman's avatar
Mor Zusman committed
4
5
import pytest

6
from tests.models.registry import HF_EXAMPLE_MODELS
7
from tests.utils import multi_gpu_test
8
from vllm.engine.arg_utils import EngineArgs
9
from vllm.sampling_params import SamplingParams
10

11
from ...utils import check_logprobs_close
12

13
14
15
# Mark all tests as hybrid
pytestmark = pytest.mark.hybrid_model

16
17
18
19
20
21
22
# NOTE: The first model in each list is taken as the primary model,
# meaning that it will be used in all tests in this file
# The rest of the models will only be tested by test_models

SSM_MODELS = [
    "state-spaces/mamba-130m-hf",
    "tiiuae/falcon-mamba-tiny-dev",
23
    "yujiepan/mamba2-codestral-v0.1-tiny-random",
24
]
25

26
27
HYBRID_MODELS = [
    "ai21labs/Jamba-tiny-dev",
28
    "pfnet/plamo-2-1b",
29
    "Zyphra/Zamba2-1.2B-instruct",
30
    "hmellor/tiny-random-BambaForCausalLM",
31
32
    "ibm-granite/granite-4.0-tiny-preview",
    "tiiuae/Falcon-H1-0.5B-Base",
33
    "LiquidAI/LFM2-1.2B",
34
35
]

Chen Zhang's avatar
Chen Zhang committed
36
V1_SUPPORTED_MODELS = [
37
38
    "state-spaces/mamba-130m-hf",
    "ai21labs/Jamba-tiny-dev",
39
    "pfnet/plamo-2-1b",
40
    "yujiepan/mamba2-codestral-v0.1-tiny-random",
41
    "Zyphra/Zamba2-1.2B-instruct",
42
    "hmellor/tiny-random-BambaForCausalLM",
43
44
    "ibm-granite/granite-4.0-tiny-preview",
    "tiiuae/Falcon-H1-0.5B-Base",
45
    "LiquidAI/LFM2-1.2B",
Chen Zhang's avatar
Chen Zhang committed
46
47
]

48
FULL_CUDA_GRAPH_MODELS = [
49
    "ai21labs/Jamba-tiny-dev",
50
    "pfnet/plamo-2-1b",
51
    "Zyphra/Zamba2-1.2B-instruct",
52
53
]

54
55
56
57
V0_UNSUPPORTED_MODELS = [
    "LiquidAI/LFM2-1.2B",
]

58
59
60
61
62
FP32_STATE_MODELS = [
    "state-spaces/mamba-130m-hf",
    "Zyphra/Zamba2-1.2B-instruct",
]

63
64
65
# Avoid OOM
MAX_NUM_SEQS = 4

Mor Zusman's avatar
Mor Zusman committed
66

67
68
69
@pytest.mark.parametrize("model", SSM_MODELS + HYBRID_MODELS)
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
Mor Zusman's avatar
Mor Zusman committed
70
71
72
73
def test_models(
    hf_runner,
    vllm_runner,
    example_prompts,
Chen Zhang's avatar
Chen Zhang committed
74
    monkeypatch,
Mor Zusman's avatar
Mor Zusman committed
75
76
    model: str,
    max_tokens: int,
77
    num_logprobs: int,
Mor Zusman's avatar
Mor Zusman committed
78
) -> None:
79
80
81
82

    try:
        model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
        model_info.check_available_online(on_fail="skip")
83
        model_info.check_transformers_version(on_fail="skip")
84
    except ValueError:
85
        pass
86

87
    with hf_runner(model) as hf_model:
88
89
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
90

Chen Zhang's avatar
Chen Zhang committed
91
    if model in V1_SUPPORTED_MODELS:
92
93
94
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
            vllm_v1_outputs = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, num_logprobs)
Chen Zhang's avatar
Chen Zhang committed
95
96
97
98
99
    else:
        vllm_v1_outputs = None

    if model in V1_SUPPORTED_MODELS:
        check_logprobs_close(
100
            outputs_0_lst=hf_outputs,
Chen Zhang's avatar
Chen Zhang committed
101
            outputs_1_lst=vllm_v1_outputs,
102
            name_0="hf",
Chen Zhang's avatar
Chen Zhang committed
103
104
            name_1="vllm-v1",
        )
Mor Zusman's avatar
Mor Zusman committed
105
106


107
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
108
109
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
110
111
112
113
114
def test_batching(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
115
    num_logprobs: int,
116
) -> None:
117
118
119
120
121
122
123
    try:
        model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
        model_info.check_available_online(on_fail="skip")
        model_info.check_transformers_version(on_fail="skip")
    except ValueError:
        pass

124
    for_loop_outputs = []
125
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
126
        for prompt in example_prompts:
127
128
129
130
            single_output, = vllm_model.generate_greedy_logprobs([prompt],
                                                                 max_tokens,
                                                                 num_logprobs)
            for_loop_outputs.append(single_output)
131

132
133
        batched_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
134

135
    check_logprobs_close(
136
137
138
139
140
141
142
        outputs_0_lst=for_loop_outputs,
        outputs_1_lst=batched_outputs,
        name_0="for_loop_vllm",
        name_1="batched_vllm",
    )


143
144
145
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [10])
def test_chunked_prefill_with_parallel_sampling(
146
147
148
149
150
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    """
    Tests chunked prefill in conjunction with n > 1. 
    
    In this case, prefill is populated with decoding tokens and
    we test that it doesn't fail.

    This test might fail if cache is not allocated correctly for n > 1
    decoding steps inside a chunked prefill forward pass
    (where we have both prefill and decode together)
    """
    sampling_params = SamplingParams(n=3,
                                     temperature=1,
                                     seed=0,
                                     max_tokens=max_tokens)
    with vllm_runner(
            model,
            enable_chunked_prefill=True,
            # forces prefill chunks with decoding
            max_num_batched_tokens=MAX_NUM_SEQS * 3,
            max_num_seqs=MAX_NUM_SEQS,
    ) as vllm_model:
        vllm_model.generate(example_prompts, sampling_params)
173
174


175
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
176
177
178
179
180
181
182
@pytest.mark.parametrize("max_tokens", [20])
def test_mamba_cache_cg_padding(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
183
184
185
186
187
    """
    This test is for verifying that mamba cache is padded to CG captured
    batch size. If it's not, a torch RuntimeError will be raised because
    tensor dimensions aren't compatible.
    """
Shinichi Hemmi's avatar
Shinichi Hemmi committed
188
189
    vllm_config = EngineArgs(model=model,
                             trust_remote_code=True).create_engine_config()
190
    while len(example_prompts) == vllm_config.pad_for_cudagraph(
191
            len(example_prompts)):
192
193
194
        example_prompts.append(example_prompts[0])

    try:
195
        with vllm_runner(model) as vllm_model:
196
197
198
199
200
201
202
203
            vllm_model.generate_greedy(example_prompts, max_tokens)
    except RuntimeError:
        pytest.fail(
            "Couldn't run batch size which is not equal to a Cuda Graph "
            "captured batch size. "
            "Could be related to mamba cache not padded correctly")


204
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
205
206
207
def test_fail_upon_inc_requests_and_finished_requests_lt_available_blocks(
    vllm_runner,
    example_prompts,
208
    model: str,
209
) -> None:
210
211
212
213
214
215
    """
    This test is for verifying that the hybrid inner state management doesn't
    collapse in case where the number of incoming requests and
    finished_requests_ids is larger than the maximum mamba block capacity.

    This could generally happen due to the fact that hybrid does support
216
    statelessness mechanism where it can clean up new incoming requests in
217
218
    a single step.
    """
219
    try:
220
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
221
222
            vllm_model.generate_greedy([example_prompts[0]] * 100, 10)
    except ValueError:
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
223
        pytest.fail("Hybrid inner state wasn't cleaned up properly between"
224
225
226
                    "steps finished requests registered unnecessarily ")


227
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
Mor Zusman's avatar
Mor Zusman committed
228
229
230
def test_state_cleanup(
    vllm_runner,
    example_prompts,
231
    model: str,
Mor Zusman's avatar
Mor Zusman committed
232
) -> None:
233
234
235
236
    """ 
    This test is for verifying that the Hybrid state is cleaned up between
    steps.
    
237
    If it's not cleaned, an error would be expected.
238
    """
Mor Zusman's avatar
Mor Zusman committed
239
    try:
240
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
Mor Zusman's avatar
Mor Zusman committed
241
242
243
            for _ in range(10):
                vllm_model.generate_greedy([example_prompts[0]] * 100, 1)
    except ValueError:
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
244
        pytest.fail("Hybrid inner state wasn't cleaned up between states, "
Mor Zusman's avatar
Mor Zusman committed
245
246
247
                    "could be related to finished_requests_ids")


248
@multi_gpu_test(num_gpus=2)
249
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
250
@pytest.mark.parametrize("max_tokens", [64])
251
252
@pytest.mark.parametrize("num_logprobs", [5])
def test_distributed_correctness(
253
254
255
256
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
257
    num_logprobs: int,
258
) -> None:
259
    with vllm_runner(model, tensor_parallel_size=1,
260
                     max_num_seqs=2) as vllm_model:
261
262
        vllm_outputs_tp_1 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
263

264
    with vllm_runner(model, tensor_parallel_size=2,
265
                     max_num_seqs=2) as vllm_model:
266
267
        vllm_outputs_tp_2 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
268

269
    check_logprobs_close(
270
271
272
273
274
        outputs_0_lst=vllm_outputs_tp_1,
        outputs_1_lst=vllm_outputs_tp_2,
        name_0="vllm_tp_1",
        name_1="vllm_tp_2",
    )
275
276


277
@pytest.mark.parametrize("model", FULL_CUDA_GRAPH_MODELS)
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
def test_full_cuda_graph(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    num_logprobs: int,
) -> None:

    try:
        model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
        model_info.check_available_online(on_fail="skip")
        model_info.check_transformers_version(on_fail="skip")
    except ValueError:
        pass

    with hf_runner(model) as hf_model:
298
299
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
300

301
302
303
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
        vllm_v1_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
304
305

    check_logprobs_close(
306
        outputs_0_lst=hf_outputs,
307
        outputs_1_lst=vllm_v1_outputs,
308
        name_0="hf",
309
310
        name_1="vllm-v1",
    )
311
312


313
@pytest.mark.parametrize("model", FP32_STATE_MODELS)
314
315
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
316
317
318
@pytest.mark.parametrize("cache_dtype_param",
                         ["mamba_ssm_cache_dtype", "mamba_cache_dtype"])
def test_fp32_cache_state(
319
320
321
322
323
324
325
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    num_logprobs: int,
326
    cache_dtype_param: str,
327
328
329
330
331
332
333
334
335
336
) -> None:

    try:
        model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
        model_info.check_available_online(on_fail="skip")
        model_info.check_transformers_version(on_fail="skip")
    except ValueError:
        pass

    with hf_runner(model) as hf_model:
337
338
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
339

340
341
    with vllm_runner(model,
                     max_num_seqs=MAX_NUM_SEQS,
342
                     **{cache_dtype_param: "float32"}) as vllm_model:
343
344
345
        vllm_v1_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)

346
    check_logprobs_close(
347
        outputs_0_lst=hf_outputs,
348
        outputs_1_lst=vllm_v1_outputs,
349
        name_0="hf",
350
351
        name_1="vllm-v1",
    )