test_hybrid.py 14.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
12
from ...utils import check_logprobs_close, check_outputs_equal

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

91
92
93
94
95
96
97
98
    with monkeypatch.context() as m:
        m.setenv("VLLM_USE_V1", "0")
        if model not in V0_UNSUPPORTED_MODELS:
            with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
                vllm_v0_outputs = vllm_model.generate_greedy_logprobs(
                    example_prompts, max_tokens, num_logprobs)
        else:
            vllm_v0_outputs = None
99

Chen Zhang's avatar
Chen Zhang committed
100
    if model in V1_SUPPORTED_MODELS:
101
102
103
        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
104
105
106
    else:
        vllm_v1_outputs = None

107
    if vllm_v0_outputs is not None:
Chen Zhang's avatar
Chen Zhang committed
108
109
110
111
112
113
114
115
116
        check_logprobs_close(
            outputs_0_lst=hf_outputs,
            outputs_1_lst=vllm_v0_outputs,
            name_0="hf",
            name_1="vllm-v0",
        )

    if model in V1_SUPPORTED_MODELS:
        check_logprobs_close(
117
            outputs_0_lst=hf_outputs,
Chen Zhang's avatar
Chen Zhang committed
118
            outputs_1_lst=vllm_v1_outputs,
119
            name_0="hf",
Chen Zhang's avatar
Chen Zhang committed
120
121
            name_1="vllm-v1",
        )
Mor Zusman's avatar
Mor Zusman committed
122
123


124
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
125
126
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
127
128
129
130
131
def test_batching(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
132
    num_logprobs: int,
133
) -> None:
134
135
136
137
138
139
140
    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

141
    for_loop_outputs = []
142
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
143
        for prompt in example_prompts:
144
145
146
147
            single_output, = vllm_model.generate_greedy_logprobs([prompt],
                                                                 max_tokens,
                                                                 num_logprobs)
            for_loop_outputs.append(single_output)
148

149
150
        batched_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
151

152
    check_logprobs_close(
153
154
155
156
157
158
159
        outputs_0_lst=for_loop_outputs,
        outputs_1_lst=batched_outputs,
        name_0="for_loop_vllm",
        name_1="batched_vllm",
    )


160
161
162
163
164
165
166
167
168
169
170
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [32])
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("chunked_prefill_token_size", [1, 4, 16])
def test_chunked_prefill(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
    num_logprobs: int,
    chunked_prefill_token_size: int,
171
    monkeypatch,
172
173
174
) -> None:
    max_num_seqs = chunked_prefill_token_size
    max_num_batched_tokens = chunked_prefill_token_size
175

176
177
178
179
180
181
182
183
    with monkeypatch.context() as m:
        m.setenv("VLLM_USE_V1", "0")
        with vllm_runner(model,
                         enable_chunked_prefill=True,
                         max_num_batched_tokens=max_num_batched_tokens,
                         max_num_seqs=max_num_seqs) as vllm_model:
            chunked = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, num_logprobs)
184

185
186
187
188
189
        with vllm_runner(model,
                         enable_chunked_prefill=False,
                         max_num_seqs=max_num_seqs) as vllm_model:
            non_chunked = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, num_logprobs)
190

191
192
193
194
195
196
        check_logprobs_close(
            outputs_0_lst=chunked,
            outputs_1_lst=non_chunked,
            name_0="chunked",
            name_1="non_chunked",
        )
197
198


199
200
201
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [10])
def test_chunked_prefill_with_parallel_sampling(
202
203
204
205
206
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    """
    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)
229
230


231
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
232
233
234
235
236
237
238
@pytest.mark.parametrize("max_tokens", [20])
def test_mamba_cache_cg_padding(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
239
240
241
242
243
    """
    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
244
245
    vllm_config = EngineArgs(model=model,
                             trust_remote_code=True).create_engine_config()
246
    while len(example_prompts) == vllm_config.pad_for_cudagraph(
247
            len(example_prompts)):
248
249
250
        example_prompts.append(example_prompts[0])

    try:
251
        with vllm_runner(model) as vllm_model:
252
253
254
255
256
257
258
259
            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")


260
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
261
262
263
264
265
266
@pytest.mark.parametrize("max_tokens", [20])
def test_models_preemption_recompute(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
267
    monkeypatch,
268
) -> None:
269
270
271
    """
    Tests that outputs are identical with and w/o preemptions (recompute).
    """
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    with monkeypatch.context() as m:
        m.setenv("VLLM_USE_V1", "0")
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
            scheduler = vllm_model.llm.llm_engine.scheduler[0]
            scheduler.ENABLE_ARTIFICIAL_PREEMPT = True
            preempt_vllm_outputs = vllm_model.generate_greedy(
                example_prompts, max_tokens)

            scheduler.ENABLE_ARTIFICIAL_PREEMPT = False
            vllm_outputs = vllm_model.generate_greedy(example_prompts,
                                                      max_tokens)

        check_outputs_equal(
            outputs_0_lst=preempt_vllm_outputs,
            outputs_1_lst=vllm_outputs,
            name_0="vllm_preepmtions",
            name_1="vllm",
        )
290
291


292
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
293
294
295
def test_fail_upon_inc_requests_and_finished_requests_lt_available_blocks(
    vllm_runner,
    example_prompts,
296
    model: str,
297
) -> None:
298
299
300
301
302
303
    """
    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
304
    statelessness mechanism where it can clean up new incoming requests in
305
306
    a single step.
    """
307
    try:
308
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
309
310
            vllm_model.generate_greedy([example_prompts[0]] * 100, 10)
    except ValueError:
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
311
        pytest.fail("Hybrid inner state wasn't cleaned up properly between"
312
313
314
                    "steps finished requests registered unnecessarily ")


315
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
Mor Zusman's avatar
Mor Zusman committed
316
317
318
def test_state_cleanup(
    vllm_runner,
    example_prompts,
319
    model: str,
Mor Zusman's avatar
Mor Zusman committed
320
) -> None:
321
322
323
324
    """ 
    This test is for verifying that the Hybrid state is cleaned up between
    steps.
    
325
    If it's not cleaned, an error would be expected.
326
    """
Mor Zusman's avatar
Mor Zusman committed
327
    try:
328
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
Mor Zusman's avatar
Mor Zusman committed
329
330
331
            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
332
        pytest.fail("Hybrid inner state wasn't cleaned up between states, "
Mor Zusman's avatar
Mor Zusman committed
333
334
335
                    "could be related to finished_requests_ids")


336
@multi_gpu_test(num_gpus=2)
337
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
338
@pytest.mark.parametrize("max_tokens", [64])
339
340
@pytest.mark.parametrize("num_logprobs", [5])
def test_distributed_correctness(
341
342
343
344
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
345
    num_logprobs: int,
346
) -> None:
347
    with vllm_runner(model, tensor_parallel_size=1,
348
                     max_num_seqs=2) as vllm_model:
349
350
        vllm_outputs_tp_1 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
351

352
    with vllm_runner(model, tensor_parallel_size=2,
353
                     max_num_seqs=2) as vllm_model:
354
355
        vllm_outputs_tp_2 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
356

357
    check_logprobs_close(
358
359
360
361
362
        outputs_0_lst=vllm_outputs_tp_1,
        outputs_1_lst=vllm_outputs_tp_2,
        name_0="vllm_tp_1",
        name_1="vllm_tp_2",
    )
363
364


365
@pytest.mark.parametrize("model", FULL_CUDA_GRAPH_MODELS)
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
@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:
386
387
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
388
389

    with monkeypatch.context() as m:
390
391
392
393
394
395
396
397
398
399
400
        m.setenv("VLLM_USE_V1", "0")
        if model not in V0_UNSUPPORTED_MODELS:
            with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
                vllm_v0_outputs = vllm_model.generate_greedy_logprobs(
                    example_prompts, max_tokens, num_logprobs)
        else:
            vllm_v0_outputs = None

    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)
401

402
    if vllm_v0_outputs is not None:
403
404
405
406
407
408
409
410
        check_logprobs_close(
            outputs_0_lst=hf_outputs,
            outputs_1_lst=vllm_v0_outputs,
            name_0="hf",
            name_1="vllm-v0",
        )

    check_logprobs_close(
411
        outputs_0_lst=hf_outputs,
412
        outputs_1_lst=vllm_v1_outputs,
413
        name_0="hf",
414
415
        name_1="vllm-v1",
    )
416
417


418
@pytest.mark.parametrize("model", FP32_STATE_MODELS)
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
def test_fp32_state(
    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:
439
440
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
441
442

    with monkeypatch.context() as m:
443
        m.setenv("VLLM_USE_V1", "0")
444
445
        with vllm_runner(model,
                         max_num_seqs=MAX_NUM_SEQS,
446
447
                         mamba_ssm_cache_dtype="float32") as vllm_model:
            vllm_v0_outputs = vllm_model.generate_greedy_logprobs(
448
449
                example_prompts, max_tokens, num_logprobs)

450
451
452
453
454
455
    with vllm_runner(model,
                     max_num_seqs=MAX_NUM_SEQS,
                     mamba_ssm_cache_dtype="float32") as vllm_model:
        vllm_v1_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)

456
457
458
459
460
461
    check_logprobs_close(
        outputs_0_lst=hf_outputs,
        outputs_1_lst=vllm_v0_outputs,
        name_0="hf",
        name_1="vllm-v0",
    )
462
463

    check_logprobs_close(
464
        outputs_0_lst=hf_outputs,
465
        outputs_1_lst=vllm_v1_outputs,
466
        name_0="hf",
467
468
        name_1="vllm-v1",
    )