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

4
from collections.abc import Callable
5

Mor Zusman's avatar
Mor Zusman committed
6
import pytest
7
import os
Mor Zusman's avatar
Mor Zusman committed
8

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

14
from ...utils import check_logprobs_close, check_outputs_equal
15
from ....utils import models_path_prefix
16

17
18
19
# Mark all tests as hybrid
pytestmark = pytest.mark.hybrid_model

20
21
22
23
# 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

24
25
APC_MULTIPLY_BY = 300

26
SSM_MODELS = [
27
28
    os.path.join(models_path_prefix, "state-spaces/mamba-130m-hf"),
    os.path.join(models_path_prefix, "tiiuae/falcon-mamba-tiny-dev"),
29
30
    # mamba2-codestral in transformers is broken pending:
    # https://github.com/huggingface/transformers/pull/40861
31
    # "yujiepan/mamba2-codestral-v0.1-tiny-random",
32
]
33

34
HYBRID_MODELS = [
35
    os.path.join(models_path_prefix, "ai21labs/Jamba-tiny-dev"),
36
    os.path.join(models_path_prefix, "pfnet/plamo-2-1b"),
37
38
39
40
    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-granite/granite-4.0-tiny-preview"),
    os.path.join(models_path_prefix, "tiiuae/Falcon-H1-0.5B-Base"),
41
    os.path.join(models_path_prefix, "LiquidAI/LFM2-1.2B"),
42
    os.path.join(models_path_prefix, "tiny-random/qwen3-next-moe"),
Chen Zhang's avatar
Chen Zhang committed
43
44
]

45
FULL_CUDA_GRAPH_MODELS = [
46
47
48
    os.path.join(models_path_prefix, "ai21labs/Jamba-tiny-dev"),
    os.path.join(models_path_prefix, "pfnet/plamo-2-1b"),
    os.path.join(models_path_prefix, "Zyphra/Zamba2-1.2B-instruct"),
Shinichi Hemmi's avatar
Shinichi Hemmi committed
49
]
50

51

52
FP32_STATE_MODELS = [
53
54
    os.path.join(models_path_prefix, "state-spaces/mamba-130m-hf"),
    os.path.join(models_path_prefix, "Zyphra/Zamba2-1.2B-instruct"),
55
56
]

57
58
# Avoid OOM
MAX_NUM_SEQS = 4
Mor Zusman's avatar
Mor Zusman committed
59
60


61
62
63
@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
64
65
66
67
def test_models(
    hf_runner,
    vllm_runner,
    example_prompts,
Chen Zhang's avatar
Chen Zhang committed
68
    monkeypatch,
Mor Zusman's avatar
Mor Zusman committed
69
70
    model: str,
    max_tokens: int,
71
    num_logprobs: int,
Mor Zusman's avatar
Mor Zusman committed
72
) -> None:
73
74
75
    try:
        model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
        model_info.check_available_online(on_fail="skip")
76
        model_info.check_transformers_version(on_fail="skip")
77
    except ValueError:
78
        pass
79

80
    with hf_runner(model) as hf_model:
81
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
82
83
            example_prompts, max_tokens, num_logprobs
        )
Mor Zusman's avatar
Mor Zusman committed
84

85
86
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
        vllm_outputs = vllm_model.generate_greedy_logprobs(
87
88
            example_prompts, max_tokens, num_logprobs
        )
89

90
91
92
93
94
95
    check_logprobs_close(
        outputs_0_lst=hf_outputs,
        outputs_1_lst=vllm_outputs,
        name_0="hf",
        name_1="vllm",
    )
Mor Zusman's avatar
Mor Zusman committed
96
97


98
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
99
100
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
101
102
103
104
105
def test_batching(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
106
    num_logprobs: int,
107
) -> None:
108
109
110
111
112
113
114
    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

115
    for_loop_outputs = []
116
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
117
        for prompt in example_prompts:
118
119
120
            (single_output,) = vllm_model.generate_greedy_logprobs(
                [prompt], max_tokens, num_logprobs
            )
121
            for_loop_outputs.append(single_output)
122

123
        batched_outputs = vllm_model.generate_greedy_logprobs(
124
125
            example_prompts, max_tokens, num_logprobs
        )
126

127
    check_logprobs_close(
128
129
130
131
132
133
134
        outputs_0_lst=for_loop_outputs,
        outputs_1_lst=batched_outputs,
        name_0="for_loop_vllm",
        name_1="batched_vllm",
    )


135
136
137
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [10])
def test_chunked_prefill_with_parallel_sampling(
138
139
140
141
142
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
143
    """
144
145
    Tests chunked prefill in conjunction with n > 1.

146
147
148
149
150
151
152
    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)
    """
153
    sampling_params = SamplingParams(n=3, temperature=1, seed=0, max_tokens=max_tokens)
154
    with vllm_runner(
155
156
157
158
159
        model,
        enable_chunked_prefill=True,
        # forces prefill chunks with decoding
        max_num_batched_tokens=MAX_NUM_SEQS * 3,
        max_num_seqs=MAX_NUM_SEQS,
160
161
    ) as vllm_model:
        vllm_model.generate(example_prompts, sampling_params)
162
163


164
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
165
166
167
168
169
170
171
@pytest.mark.parametrize("max_tokens", [20])
def test_mamba_cache_cg_padding(
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
) -> None:
172
173
174
175
176
    """
    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.
    """
177
178
    vllm_config = EngineArgs(model=model, trust_remote_code=True).create_engine_config()
    while len(example_prompts) == vllm_config.pad_for_cudagraph(len(example_prompts)):
179
180
181
        example_prompts.append(example_prompts[0])

    try:
182
        with vllm_runner(model) as vllm_model:
183
184
185
186
187
            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. "
188
189
            "Could be related to mamba cache not padded correctly"
        )
190
191


192
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
193
194
195
def test_fail_upon_inc_requests_and_finished_requests_lt_available_blocks(
    vllm_runner,
    example_prompts,
196
    model: str,
197
) -> None:
198
199
200
201
202
203
    """
    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
204
    statelessness mechanism where it can clean up new incoming requests in
205
206
    a single step.
    """
207
    try:
208
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
209
210
            vllm_model.generate_greedy([example_prompts[0]] * 100, 10)
    except ValueError:
211
212
213
214
        pytest.fail(
            "Hybrid inner state wasn't cleaned up properly between"
            "steps finished requests registered unnecessarily "
        )
215
216


217
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
Mor Zusman's avatar
Mor Zusman committed
218
219
220
def test_state_cleanup(
    vllm_runner,
    example_prompts,
221
    model: str,
Mor Zusman's avatar
Mor Zusman committed
222
) -> None:
223
    """
224
225
    This test is for verifying that the Hybrid state is cleaned up between
    steps.
226

227
    If it's not cleaned, an error would be expected.
228
    """
Mor Zusman's avatar
Mor Zusman committed
229
    try:
230
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
Mor Zusman's avatar
Mor Zusman committed
231
232
233
            for _ in range(10):
                vllm_model.generate_greedy([example_prompts[0]] * 100, 1)
    except ValueError:
234
235
236
237
        pytest.fail(
            "Hybrid inner state wasn't cleaned up between states, "
            "could be related to finished_requests_ids"
        )
Mor Zusman's avatar
Mor Zusman committed
238
239


240
@multi_gpu_test(num_gpus=2)
241
242
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
@pytest.mark.parametrize("max_tokens", [64])
243
244
@pytest.mark.parametrize("num_logprobs", [5])
def test_distributed_correctness(
Mor Zusman's avatar
Mor Zusman committed
245
    vllm_runner,
246
    example_prompts,
247
248
    model: str,
    max_tokens: int,
249
    num_logprobs: int,
Mor Zusman's avatar
Mor Zusman committed
250
) -> None:
251
252
253
    with vllm_runner(
        model, tensor_parallel_size=1, max_num_seqs=MAX_NUM_SEQS
    ) as vllm_model:
254
        vllm_outputs_tp_1 = vllm_model.generate_greedy_logprobs(
255
256
            example_prompts, max_tokens, num_logprobs
        )
257

258
259
260
    with vllm_runner(
        model, tensor_parallel_size=2, max_num_seqs=MAX_NUM_SEQS
    ) as vllm_model:
261
        vllm_outputs_tp_2 = vllm_model.generate_greedy_logprobs(
262
263
            example_prompts, max_tokens, num_logprobs
        )
264

265
    check_logprobs_close(
266
267
268
269
        outputs_0_lst=vllm_outputs_tp_1,
        outputs_1_lst=vllm_outputs_tp_2,
        name_0="vllm_tp_1",
        name_1="vllm_tp_2",
270
271
272
    )


273
@pytest.mark.parametrize("model", FULL_CUDA_GRAPH_MODELS)
274
@pytest.mark.parametrize("max_tokens", [64])
275
@pytest.mark.parametrize("num_logprobs", [5])
276
277
def test_full_cuda_graph(
    hf_runner,
278
279
    vllm_runner,
    example_prompts,
280
    monkeypatch,
281
282
    model: str,
    max_tokens: int,
283
    num_logprobs: int,
284
) -> None:
285
286
287
288
289
290
291
292
    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:
293
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
294
295
            example_prompts, max_tokens, num_logprobs
        )
296
297

    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
298
        vllm_outputs = vllm_model.generate_greedy_logprobs(
299
300
            example_prompts, max_tokens, num_logprobs
        )
301

302
    check_logprobs_close(
303
        outputs_0_lst=hf_outputs,
304
        outputs_1_lst=vllm_outputs,
305
        name_0="hf",
306
        name_1="vllm",
307
    )
308
309


310
@pytest.mark.parametrize("model", FP32_STATE_MODELS)
311
312
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
313
314
315
@pytest.mark.parametrize(
    "cache_dtype_param", ["mamba_ssm_cache_dtype", "mamba_cache_dtype"]
)
316
def test_fp32_cache_state(
317
318
319
320
321
322
323
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    num_logprobs: int,
324
    cache_dtype_param: str,
325
326
327
328
329
330
331
332
333
) -> 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:
334
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
335
336
            example_prompts, max_tokens, num_logprobs
        )
337

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

345
346
    check_logprobs_close(
        outputs_0_lst=hf_outputs,
347
        outputs_1_lst=vllm_outputs,
348
        name_0="hf",
349
        name_1="vllm",
350
    )
351
352
353


# Helper functions for the APC tests
354
355
356
357
358
def _get_vllm_runner_params(
    model: str,
    max_model_len: int,
    tensor_parallel_size: int = 1,
):
359
    return {
360
        "model_name": model,
361
        "enable_chunked_prefill": True,
362
363
364
365
        "enable_prefix_caching": False,
        "max_model_len": max_model_len,
        "tensor_parallel_size": tensor_parallel_size,
        "gpu_memory_utilization": 0.4,
366
367
368
    }


369
370
371
372
373
374
375
376
377
def _get_vLLM_output(
    vllm_runner,
    kwargs,
    prompts,
    max_tokens,
    num_logprobs,
    num_repetitions=1,
    vllm_model=None,
):
378
379
380
381
382
383
384
385
    outs = []
    if vllm_model is None:
        vllm_model = vllm_runner(**kwargs)
    for _ in range(num_repetitions):
        if num_logprobs < 0:
            vllm_output = vllm_model.generate_greedy(prompts, max_tokens)
        else:
            vllm_output = vllm_model.generate_greedy_logprobs(
386
387
                prompts, max_tokens, num_logprobs
            )
388
389
390
391
392
        outs.append(vllm_output)

    return outs, vllm_model


393
@pytest.mark.parametrize("model", [HYBRID_MODELS[0], HYBRID_MODELS[3]])
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("n_repetitions", [2])
# If num_logprobs is set to -1, then the stringent version
# of the test is executed using `check_outputs_equal`
# instead of `check_logprobs_close`
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_apc_single_prompt(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    n_repetitions: int,
    num_logprobs: int,
    tensor_parallel_size: 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

419
420
421
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
422
423

    # Sample prompts.
424
    generated_prompts = [APC_MULTIPLY_BY * example_prompts[0]]
425

426
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
427
    vllm_runner_kwargs = _get_vllm_runner_params(
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )

    vllm_runner_kwargs["enable_prefix_caching"] = True
    vllm_outputs_cache_rep, _ = _get_vLLM_output(
        vllm_runner,
        vllm_runner_kwargs,
        generated_prompts,
        max_tokens,
        num_logprobs,
        n_repetitions,
    )
444
445
446
447
448
449
450
451
452
453
454
455
456

    for r_idx, vllm_outputs_cache_itn in enumerate(vllm_outputs_cache_rep):
        # In the first repetition, the caches are filled
        # In the second repetition, these caches are reused

        compare_operator(
            outputs_0_lst=vllm_outputs_no_cache[0],
            outputs_1_lst=vllm_outputs_cache_itn,
            name_0="vllm_no_cache",
            name_1=f"vllm_cache_it_{r_idx + 1}",
        )


457
@pytest.mark.parametrize("model", [HYBRID_MODELS[0], HYBRID_MODELS[3]])
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("n_repetitions", [2])
# If num_logprobs is set to -1, then the stringent version
# of the test is executed using `check_outputs_equal`
# instead of `check_logprobs_close`
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_apc_single_prompt_block_align_alignment(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    n_repetitions: int,
    num_logprobs: int,
    tensor_parallel_size: 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

483
484
485
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
486
487

    # Sample prompts. This custom prompt is used, as it causes the most issues
488
    generated_prompts = ["The president of the United States is " * APC_MULTIPLY_BY]
489

490
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
491
    vllm_runner_kwargs = _get_vllm_runner_params(
492
493
494
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
495

496
497
498
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
499

500
    vllm_runner_kwargs["enable_prefix_caching"] = True
501
502
    with vllm_runner(**vllm_runner_kwargs) as vllm_model:
        # Retrieve the default mamba state block size
503
        mamba_block_size = vllm_model.llm.llm_engine.cache_config.mamba_block_size
504
505
506
507
508
509
510

    # In case the hybrid model does not have the
    # "mamba_block_size" assume a fixed constant
    if mamba_block_size is None:
        mamba_block_size = 512

    mamba_block_size_multiplier = 10
511
512
513
514
515
516
517
518
519
520
521
522
    for offsets in [-3, 3, mamba_block_size // 4 + 3, mamba_block_size // 2 - 3]:
        vllm_runner_kwargs["max_num_batched_tokens"] = (
            mamba_block_size_multiplier * mamba_block_size - offsets
        )
        vllm_outputs_cache_rep, _ = _get_vLLM_output(
            vllm_runner,
            vllm_runner_kwargs,
            generated_prompts,
            max_tokens,
            num_logprobs,
            n_repetitions,
        )
523
524
525
526
527
528
529
530
531
532
533
534
535
536

        # Check alignment of the output logits when using APC
        for r_idx, vllm_outputs_cache_itn in enumerate(vllm_outputs_cache_rep):
            # In the first repetition, the caches are filled
            # In the second repetition, these caches are reused

            compare_operator(
                outputs_0_lst=vllm_outputs_no_cache[0],
                outputs_1_lst=vllm_outputs_cache_itn,
                name_0="vllm_no_cache",
                name_1=f"vllm_cache_it_{r_idx + 1}",
            )


537
@pytest.mark.parametrize("model", [HYBRID_MODELS[0], HYBRID_MODELS[3]])
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("n_repetitions", [2])
# If num_logprobs is set to -1, then the stringent version
# of the test is executed using `check_outputs_equal`
# instead of `check_logprobs_close`
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_apc_multiple_prompts_all_cached_outputs(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    n_repetitions: int,
    num_logprobs: int,
    tensor_parallel_size: 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

563
564
565
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
566
567

    # Sample prompts.
568
    generated_prompts = [APC_MULTIPLY_BY * prompt for prompt in example_prompts]
569

570
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
571
    vllm_runner_kwargs = _get_vllm_runner_params(
572
573
574
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
575

576
577
578
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
579

580
581
582
583
584
585
586
587
588
    vllm_runner_kwargs["enable_prefix_caching"] = True
    vllm_outputs_cache_rep, _ = _get_vLLM_output(
        vllm_runner,
        vllm_runner_kwargs,
        generated_prompts,
        max_tokens,
        num_logprobs,
        n_repetitions,
    )
589
590
591
592
593
594
595
596
597
598
599
600
601

    for r_idx, vllm_outputs_cache_itn in enumerate(vllm_outputs_cache_rep):
        # In the first repetition, the caches are filled
        # In the second repetition, these caches are reused

        compare_operator(
            outputs_0_lst=vllm_outputs_no_cache[0],
            outputs_1_lst=vllm_outputs_cache_itn,
            name_0="vllm_no_cache",
            name_1=f"vllm_cache_it_{r_idx + 1}",
        )


602
@pytest.mark.parametrize("model", [HYBRID_MODELS[0], HYBRID_MODELS[3]])
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("n_repetitions", [2])
# If num_logprobs is set to -1, then the stringent version
# of the test is executed using `check_outputs_equal`
# instead of `check_logprobs_close`
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_apc_multiple_prompts_block_align_alignment(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    n_repetitions: int,
    num_logprobs: int,
    tensor_parallel_size: 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

628
629
630
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
631
632
633
634

    # Sample prompts. This custom prompt is used, as it causes the most issues
    prompt_text = "The president of the United States is "
    prompt_offsets = [0, 3, 7, 13, 17, 22, 25, 31]
635
636
637
    generated_prompts = [
        prompt_text[offset:] * APC_MULTIPLY_BY for offset in prompt_offsets
    ]
638
639
640
641
642
643
644
645
646
647
648
649

    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
    vllm_runner_kwargs = _get_vllm_runner_params(
        model, max_model_len, tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"

    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )

    vllm_runner_kwargs["enable_prefix_caching"] = True
650
651
    with vllm_runner(**vllm_runner_kwargs) as vllm_model:
        # Retrieve the default mamba state block size
652
        mamba_block_size = vllm_model.llm.llm_engine.cache_config.mamba_block_size
653
654
655
656
657
658
659

    # In case the hybrid model does not have the
    # "mamba_block_size" assume a fixed constant
    if mamba_block_size is None:
        mamba_block_size = 512

    mamba_block_size_multiplier = 10
660
661
662
663
664
665
666
667
668
669
670
671
    for offsets in [-3, 3, mamba_block_size // 4 + 3, mamba_block_size // 2 - 3]:
        vllm_runner_kwargs["max_num_batched_tokens"] = (
            mamba_block_size_multiplier * mamba_block_size - offsets
        )
        vllm_outputs_cache_rep, _ = _get_vLLM_output(
            vllm_runner,
            vllm_runner_kwargs,
            generated_prompts,
            max_tokens,
            num_logprobs,
            n_repetitions,
        )
672
673
674
675
676
677
678
679
680
681
682
683
684
685

        # Check alignment of the output logits when using APC
        for r_idx, vllm_outputs_cache_itn in enumerate(vllm_outputs_cache_rep):
            # In the first repetition, the caches are filled
            # In the second repetition, these caches are reused

            compare_operator(
                outputs_0_lst=vllm_outputs_no_cache[0],
                outputs_1_lst=vllm_outputs_cache_itn,
                name_0="vllm_no_cache",
                name_1=f"vllm_cache_it_{r_idx + 1}",
            )


686
@pytest.mark.parametrize("model", [HYBRID_MODELS[0], HYBRID_MODELS[3]])
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("n_repetitions", [2])
# If num_logprobs is set to -1, then the stringent version
# of the test is executed using `check_outputs_equal`
# instead of `check_logprobs_close`
@pytest.mark.parametrize("num_logprobs", [5])
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_apc_multiple_prompts_partial_cached_outputs(
    hf_runner,
    vllm_runner,
    example_prompts,
    monkeypatch,
    model: str,
    max_tokens: int,
    n_repetitions: int,
    num_logprobs: int,
    tensor_parallel_size: 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

712
713
714
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
715
716

    # Sample prompts.
717
    generated_prompts = [APC_MULTIPLY_BY * prompt for prompt in example_prompts]
718

719
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
720
    vllm_runner_kwargs = _get_vllm_runner_params(
721
722
723
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
724

725
726
727
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
728
729

    # Cache only part of all the prompts
730
    vllm_runner_kwargs["enable_prefix_caching"] = True
731
    vllm_outputs_partial_cache, vllm_model = _get_vLLM_output(
732
733
        vllm_runner, vllm_runner_kwargs, generated_prompts[:3], max_tokens, num_logprobs
    )
734
735
736
737
738
739
740
741

    compare_operator(
        outputs_0_lst=vllm_outputs_no_cache[0][:3],
        outputs_1_lst=vllm_outputs_partial_cache[0],
        name_0="vllm_no_cache",
        name_1="vllm_partial_cache",
    )

742
743
744
745
746
747
748
749
750
    vllm_outputs_cache_rep, _ = _get_vLLM_output(
        vllm_runner,
        vllm_runner_kwargs,
        generated_prompts,
        max_tokens,
        num_logprobs,
        n_repetitions,
        vllm_model=vllm_model,
    )
751
752
753
754
755
756
757
758
759
760
761

    for r_idx, vllm_outputs_cache_itn in enumerate(vllm_outputs_cache_rep):
        # In the first repetition, the caches are filled
        # In the second repetition, these caches are reused

        compare_operator(
            outputs_0_lst=vllm_outputs_no_cache[0],
            outputs_1_lst=vllm_outputs_cache_itn,
            name_0="vllm_no_cache",
            name_1=f"vllm_cache_it_{r_idx + 1}",
        )