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

4
5
from typing import Callable

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

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

13
from ...utils import check_logprobs_close, check_outputs_equal
14

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

18
19
20
21
22
23
24
# 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",
25
26
    # mamba2-codestral in transformers is broken pending:
    # https://github.com/huggingface/transformers/pull/40861
27
    # "yujiepan/mamba2-codestral-v0.1-tiny-random",
28
]
29

30
31
HYBRID_MODELS = [
    "ai21labs/Jamba-tiny-dev",
32
    "pfnet/plamo-2-1b",
33
    "Zyphra/Zamba2-1.2B-instruct",
34
    "hmellor/tiny-random-BambaForCausalLM",
35
36
    "ibm-granite/granite-4.0-tiny-preview",
    "tiiuae/Falcon-H1-0.5B-Base",
37
    "LiquidAI/LFM2-1.2B",
38
    "tiny-random/qwen3-next-moe",
Chen Zhang's avatar
Chen Zhang committed
39
40
]

41
FULL_CUDA_GRAPH_MODELS = [
42
    "ai21labs/Jamba-tiny-dev",
43
    "pfnet/plamo-2-1b",
44
    "Zyphra/Zamba2-1.2B-instruct",
45
46
]

47
48
49
50
51
FP32_STATE_MODELS = [
    "state-spaces/mamba-130m-hf",
    "Zyphra/Zamba2-1.2B-instruct",
]

52
53
54
# Avoid OOM
MAX_NUM_SEQS = 4

Mor Zusman's avatar
Mor Zusman committed
55

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

75
    with hf_runner(model) as hf_model:
76
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
77
78
            example_prompts, max_tokens, num_logprobs
        )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
79

80
81
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
        vllm_outputs = vllm_model.generate_greedy_logprobs(
82
83
            example_prompts, max_tokens, num_logprobs
        )
Chen Zhang's avatar
Chen Zhang committed
84

85
86
87
88
89
90
    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
91
92


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

110
    for_loop_outputs = []
111
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
112
        for prompt in example_prompts:
113
114
115
            (single_output,) = vllm_model.generate_greedy_logprobs(
                [prompt], max_tokens, num_logprobs
            )
116
            for_loop_outputs.append(single_output)
117

118
        batched_outputs = vllm_model.generate_greedy_logprobs(
119
120
            example_prompts, max_tokens, num_logprobs
        )
121

122
    check_logprobs_close(
123
124
125
126
127
128
129
        outputs_0_lst=for_loop_outputs,
        outputs_1_lst=batched_outputs,
        name_0="for_loop_vllm",
        name_1="batched_vllm",
    )


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

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


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

    try:
177
        with vllm_runner(model) as vllm_model:
178
179
180
181
182
            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. "
183
184
            "Could be related to mamba cache not padded correctly"
        )
185
186


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


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

222
    If it's not cleaned, an error would be expected.
223
    """
Mor Zusman's avatar
Mor Zusman committed
224
    try:
225
        with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
Mor Zusman's avatar
Mor Zusman committed
226
227
228
            for _ in range(10):
                vllm_model.generate_greedy([example_prompts[0]] * 100, 1)
    except ValueError:
229
230
231
232
        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
233
234


235
@multi_gpu_test(num_gpus=2)
236
@pytest.mark.parametrize("model", [SSM_MODELS[0], HYBRID_MODELS[0]])
237
@pytest.mark.parametrize("max_tokens", [64])
238
239
@pytest.mark.parametrize("num_logprobs", [5])
def test_distributed_correctness(
240
241
242
243
    vllm_runner,
    example_prompts,
    model: str,
    max_tokens: int,
244
    num_logprobs: int,
245
) -> None:
246
247
248
    with vllm_runner(
        model, tensor_parallel_size=1, max_num_seqs=MAX_NUM_SEQS
    ) as vllm_model:
249
        vllm_outputs_tp_1 = vllm_model.generate_greedy_logprobs(
250
251
            example_prompts, max_tokens, num_logprobs
        )
252

253
254
255
    with vllm_runner(
        model, tensor_parallel_size=2, max_num_seqs=MAX_NUM_SEQS
    ) as vllm_model:
256
        vllm_outputs_tp_2 = vllm_model.generate_greedy_logprobs(
257
258
            example_prompts, max_tokens, num_logprobs
        )
259

260
    check_logprobs_close(
261
262
263
264
265
        outputs_0_lst=vllm_outputs_tp_1,
        outputs_1_lst=vllm_outputs_tp_2,
        name_0="vllm_tp_1",
        name_1="vllm_tp_2",
    )
266
267


268
@pytest.mark.parametrize("model", FULL_CUDA_GRAPH_MODELS)
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
@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:
288
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
289
290
            example_prompts, max_tokens, num_logprobs
        )
291

292
    with vllm_runner(model, max_num_seqs=MAX_NUM_SEQS) as vllm_model:
293
        vllm_outputs = vllm_model.generate_greedy_logprobs(
294
295
            example_prompts, max_tokens, num_logprobs
        )
296
297

    check_logprobs_close(
298
        outputs_0_lst=hf_outputs,
299
        outputs_1_lst=vllm_outputs,
300
        name_0="hf",
301
        name_1="vllm",
302
    )
303
304


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

333
334
335
    with vllm_runner(
        model, max_num_seqs=MAX_NUM_SEQS, **{cache_dtype_param: "float32"}
    ) as vllm_model:
336
        vllm_outputs = vllm_model.generate_greedy_logprobs(
337
338
            example_prompts, max_tokens, num_logprobs
        )
339

340
    check_logprobs_close(
341
        outputs_0_lst=hf_outputs,
342
        outputs_1_lst=vllm_outputs,
343
        name_0="hf",
344
        name_1="vllm",
345
    )
346
347
348
349
350


# Helper functions for the APC tests
def _get_vllm_runner_params(model, max_model_len, tensor_parallel_size=1):
    return {
351
352
353
354
355
        "model_name": model,
        "enable_prefix_caching": False,
        "max_model_len": max_model_len,
        "tensor_parallel_size": tensor_parallel_size,
        "gpu_memory_utilization": 0.4,
356
357
358
    }


359
360
361
362
363
364
365
366
367
def _get_vLLM_output(
    vllm_runner,
    kwargs,
    prompts,
    max_tokens,
    num_logprobs,
    num_repetitions=1,
    vllm_model=None,
):
368
369
370
371
372
373
374
375
    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(
376
377
                prompts, max_tokens, num_logprobs
            )
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        outs.append(vllm_output)

    return outs, vllm_model


@pytest.mark.parametrize("model", [HYBRID_MODELS[3]])
@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

409
410
411
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
412
413
414
415
416
417

    MULTIPLE = 300

    # Sample prompts.
    generated_prompts = [MULTIPLE * example_prompts[0]]

418
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
419
    vllm_runner_kwargs = _get_vllm_runner_params(
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        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,
    )
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

    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}",
        )


@pytest.mark.parametrize("model", [HYBRID_MODELS[3]])
@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

475
476
477
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
478
479
480
481
482
483

    MULTIPLE = 300

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

484
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
485
    vllm_runner_kwargs = _get_vllm_runner_params(
486
487
488
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
489

490
491
492
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
493

494
    vllm_runner_kwargs["enable_prefix_caching"] = True
495
496
    with vllm_runner(**vllm_runner_kwargs) as vllm_model:
        # Retrieve the default mamba state block size
497
        mamba_block_size = vllm_model.llm.llm_engine.cache_config.mamba_block_size
498
499
500
501
502
503
504

    # 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
505
506
507
508
509
510
511
512
513
514
515
516
    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,
        )
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

        # 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}",
            )


@pytest.mark.parametrize("model", [HYBRID_MODELS[3]])
@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

557
558
559
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
560
561
562
563
564
565

    MULTIPLE = 300

    # Sample prompts.
    generated_prompts = [MULTIPLE * prompt for prompt in example_prompts]

566
    max_model_len = max(len(prompt) + max_tokens for prompt in generated_prompts)
567
    vllm_runner_kwargs = _get_vllm_runner_params(
568
569
570
        model, max_model_len, tensor_parallel_size=tensor_parallel_size
    )
    vllm_runner_kwargs["mamba_ssm_cache_dtype"] = "float32"
571

572
573
574
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
575

576
577
578
579
580
581
582
583
584
    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,
    )
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

    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}",
        )


@pytest.mark.parametrize("model", [HYBRID_MODELS[3]])
@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

624
625
626
    compare_operator: Callable = (
        check_logprobs_close if num_logprobs > 0 else check_outputs_equal  # type: ignore
    )
627
628
629
630
631
632

    MULTIPLE = 300

    # 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]
633
634
635
636
637
638
639
640
641
642
643
644
645
    generated_prompts = [prompt_text[offset:] * MULTIPLE for offset in prompt_offsets]

    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
646
647
    with vllm_runner(**vllm_runner_kwargs) as vllm_model:
        # Retrieve the default mamba state block size
648
        mamba_block_size = vllm_model.llm.llm_engine.cache_config.mamba_block_size
649
650
651
652
653
654
655

    # 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
656
657
658
659
660
661
662
663
664
665
666
667
    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,
        )
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707

        # 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}",
            )


@pytest.mark.parametrize("model", [HYBRID_MODELS[3]])
@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

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

    MULTIPLE = 300

    # Sample prompts.
    generated_prompts = [MULTIPLE * prompt for prompt in example_prompts]

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

723
724
725
    vllm_outputs_no_cache, _ = _get_vLLM_output(
        vllm_runner, vllm_runner_kwargs, generated_prompts, max_tokens, num_logprobs
    )
726
727

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

    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",
    )

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

    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}",
        )