test_hybrid.py 13.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
import pytest
5
import os
Mor Zusman's avatar
Mor Zusman committed
6

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

12
from ....utils import models_path_prefix
13
14
from ...utils import check_logprobs_close, check_outputs_equal

15
16
17
# Mark all tests as hybrid
pytestmark = pytest.mark.hybrid_model

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 = [
zhuwenwen's avatar
zhuwenwen committed
23
24
25
    os.path.join(models_path_prefix,"state-spaces/mamba-130m-hf"),
    os.path.join(models_path_prefix,"tiiuae/falcon-mamba-tiny-dev"),
    os.path.join(models_path_prefix,"mistralai/Mamba-Codestral-7B-v0.1"),
26
]
27

28
HYBRID_MODELS = [
zhuwenwen's avatar
zhuwenwen committed
29
    os.path.join(models_path_prefix,"ai21labs/Jamba-tiny-dev"),
30
31
32
    # NOTE: Running Plamo2 in transformers implementation requires to install
    # causal-conv1d package, which is not listed as a test dependency as it's
    # not compatible with pip-compile.
zhuwenwen's avatar
zhuwenwen committed
33
34
35
36
37
38
39
    os.path.join(models_path_prefix,"pfnet/plamo-2-1b"),
    os.path.join(models_path_prefix,"Zyphra/Zamba2-1.2B-instruct"),
    os.path.join(models_path_prefix,"hmellor/tiny-random-BambaForCausalLM"),
    os.path.join(models_path_prefix,"ibm-ai-platform/Bamba-9B-v1"),
    os.path.join(models_path_prefix,"nvidia/Nemotron-H-8B-Base-8K"),
    os.path.join(models_path_prefix,"ibm-granite/granite-4.0-tiny-preview"),
    os.path.join(models_path_prefix,"tiiuae/Falcon-H1-0.5B-Base"),
40
41
42
43
44
45
46
]

HF_UNSUPPORTED_MODELS = [
    # The HF transformers implementation of
    # Mamba2 is buggy for Codestral as it doesn't handle n_groups, so the test
    # doesn't compare vLLM output with HF output.
    # See https://github.com/huggingface/transformers/pull/35943
zhuwenwen's avatar
zhuwenwen committed
47
    os.path.join(models_path_prefix,"mistralai/Mamba-Codestral-7B-v0.1"),
48
49
    # Note: I'm not seeing the same output from vLLM V0 vs. HF transformers
    # for Nemotron-H-8B; currently only compare vLLM V0 vs. vLLM V1
zhuwenwen's avatar
zhuwenwen committed
50
    os.path.join(models_path_prefix,"nvidia/Nemotron-H-8B-Base-8K"),
51
52
53
    # NOTE: Currently the test fails due to HF transformers issue fixed in:
    # https://github.com/huggingface/transformers/pull/39033
    # We will enable vLLM test for Granite after next HF transformers release.
zhuwenwen's avatar
zhuwenwen committed
54
    os.path.join(models_path_prefix,"ibm-granite/granite-4.0-tiny-preview"),
Shinichi Hemmi's avatar
Shinichi Hemmi committed
55
]
56

Chen Zhang's avatar
Chen Zhang committed
57
V1_SUPPORTED_MODELS = [
zhuwenwen's avatar
zhuwenwen committed
58
59
60
61
62
63
    os.path.join(models_path_prefix,"mistralai/Mamba-Codestral-7B-v0.1"),
    os.path.join(models_path_prefix,"ibm-ai-platform/Bamba-9B-v1"),
    os.path.join(models_path_prefix,"Zyphra/Zamba2-1.2B-instruct"),
    os.path.join(models_path_prefix,"nvidia/Nemotron-H-8B-Base-8K"),
    os.path.join(models_path_prefix,"ibm-granite/granite-4.0-tiny-preview"),
    os.path.join(models_path_prefix,"tiiuae/Falcon-H1-0.5B-Base"),
Shinichi Hemmi's avatar
Shinichi Hemmi committed
64
]
65

66

67
68
# Avoid OOM
MAX_NUM_SEQS = 4
Mor Zusman's avatar
Mor Zusman committed
69
70


71
72
73
@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
74
75
76
77
def test_models(
    hf_runner,
    vllm_runner,
    example_prompts,
Chen Zhang's avatar
Chen Zhang committed
78
    monkeypatch,
Mor Zusman's avatar
Mor Zusman committed
79
80
    model: str,
    max_tokens: int,
81
    num_logprobs: int,
Mor Zusman's avatar
Mor Zusman committed
82
) -> None:
83
84
85
86
87
88
89
90

    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

91
    with hf_runner(model) as hf_model:
92
        if model not in HF_UNSUPPORTED_MODELS:
Chen Zhang's avatar
Chen Zhang committed
93
94
95
96
            hf_outputs = hf_model.generate_greedy_logprobs_limit(
                example_prompts, max_tokens, num_logprobs)
        else:
            hf_outputs = None
Mor Zusman's avatar
Mor Zusman committed
97

98
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
Chen Zhang's avatar
Chen Zhang committed
99
        vllm_v0_outputs = vllm_model.generate_greedy_logprobs(
100
            example_prompts, max_tokens, num_logprobs)
101

Chen Zhang's avatar
Chen Zhang committed
102
103
104
    if model in V1_SUPPORTED_MODELS:
        with monkeypatch.context() as m:
            m.setenv("VLLM_USE_V1", "1")
105
106
107
            if model in HYBRID_MODELS:
                # required due to reorder_batch behaviour
                m.setenv("VLLM_ATTENTION_BACKEND", "FLASHINFER")
Chen Zhang's avatar
Chen Zhang committed
108
109
            with vllm_runner(model,
                             max_num_seqs=MAX_NUM_SEQS,
110
                             enable_prefix_caching=False) as vllm_model:
Chen Zhang's avatar
Chen Zhang committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                vllm_v1_outputs = vllm_model.generate_greedy_logprobs(
                    example_prompts, max_tokens, num_logprobs)
    else:
        vllm_v1_outputs = None

    if hf_outputs is not None:
        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:
        ref_outputs = hf_outputs if hf_outputs is not None else vllm_v0_outputs
        check_logprobs_close(
            outputs_0_lst=ref_outputs,
            outputs_1_lst=vllm_v1_outputs,
            name_0="hf" if hf_outputs is not None else "vllm-v0",
            name_1="vllm-v1",
        )
Mor Zusman's avatar
Mor Zusman committed
132
133


134
135
136
@pytest.mark.parametrize("model", SSM_MODELS + HYBRID_MODELS)
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
137
138
139
140
141
def test_batching(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
142
    num_logprobs: int,
143
) -> None:
144
145
146
147
148
149
150
151

    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

152
    for_loop_outputs = []
153
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
154
        for prompt in example_prompts:
155
156
157
158
            single_output, = vllm_model.generate_greedy_logprobs([prompt],
                                                                 max_tokens,
                                                                 num_logprobs)
            for_loop_outputs.append(single_output)
159

160
161
        batched_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
162

163
    check_logprobs_close(
164
165
166
167
168
169
170
        outputs_0_lst=for_loop_outputs,
        outputs_1_lst=batched_outputs,
        name_0="for_loop_vllm",
        name_1="batched_vllm",
    )


171
172
173
174
175
176
177
178
179
180
181
182
183
184
@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,
) -> None:
    max_num_seqs = chunked_prefill_token_size
    max_num_batched_tokens = chunked_prefill_token_size
185
186
187

    with vllm_runner(model,
                     enable_chunked_prefill=True,
188
189
190
191
                     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)
192

193
194
195
196
197
198
199
    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)

    check_logprobs_close(
200
201
202
203
204
205
206
        outputs_0_lst=chunked,
        outputs_1_lst=non_chunked,
        name_0="chunked",
        name_1="non_chunked",
    )


207
208
209
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [10])
def test_chunked_prefill_with_parallel_sampling(
210
211
212
213
214
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    """
    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)
237
238


239
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
240
241
242
243
244
245
246
@pytest.mark.parametrize("max_tokens", [20])
def test_mamba_cache_cg_padding(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
247
248
249
250
251
    """
    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
252
253
    vllm_config = EngineArgs(model=model,
                             trust_remote_code=True).create_engine_config()
254
    while len(example_prompts) == vllm_config.pad_for_cudagraph(
255
            len(example_prompts)):
256
257
258
        example_prompts.append(example_prompts[0])

    try:
259
        with vllm_runner(model) as vllm_model:
260
261
262
263
264
265
266
267
            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")


268
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
269
270
271
272
273
274
275
@pytest.mark.parametrize("max_tokens", [20])
def test_models_preemption_recompute(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
276
277
278
279
    """
    Tests that outputs are identical with and w/o preemptions (recompute).
    """
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
280
        scheduler = vllm_model.llm.llm_engine.scheduler[0]
281
        scheduler.ENABLE_ARTIFICIAL_PREEMPT = True
282
283
284
        preempt_vllm_outputs = vllm_model.generate_greedy(
            example_prompts, max_tokens)

285
        scheduler.ENABLE_ARTIFICIAL_PREEMPT = False
286
287
288
289
290
291
292
293
294
295
        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",
    )


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


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


340
341
342
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [64])
def test_multistep_correctness(
Mor Zusman's avatar
Mor Zusman committed
343
    vllm_runner,
344
    example_prompts,
345
346
    model: str,
    max_tokens: int,
Mor Zusman's avatar
Mor Zusman committed
347
) -> None:
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    with vllm_runner(model, num_scheduler_steps=8,
                     max_num_seqs=2) as vllm_model:
        vllm_outputs_multistep = vllm_model.generate_greedy(
            example_prompts, max_tokens)

    with vllm_runner(model, num_scheduler_steps=1,
                     max_num_seqs=2) as vllm_model:
        vllm_outputs_single_step = vllm_model.generate_greedy(
            example_prompts, max_tokens)

    check_outputs_equal(
        outputs_0_lst=vllm_outputs_multistep,
        outputs_1_lst=vllm_outputs_single_step,
        name_0="vllm_outputs_multistep",
        name_1="vllm_outputs_single_step",
    )


366
@multi_gpu_test(num_gpus=2)
367
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
368
@pytest.mark.parametrize("max_tokens", [64])
369
370
@pytest.mark.parametrize("num_logprobs", [5])
def test_distributed_correctness(
371
372
373
374
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
375
    num_logprobs: int,
376
) -> None:
377
    with vllm_runner(model, tensor_parallel_size=1,
378
                     max_num_seqs=2) as vllm_model:
379
380
        vllm_outputs_tp_1 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
381

382
    with vllm_runner(model, tensor_parallel_size=2,
383
                     max_num_seqs=2) as vllm_model:
384
385
        vllm_outputs_tp_2 = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
386

387
    check_logprobs_close(
388
389
390
391
392
        outputs_0_lst=vllm_outputs_tp_1,
        outputs_1_lst=vllm_outputs_tp_2,
        name_0="vllm_tp_1",
        name_1="vllm_tp_2",
    )