test_vllm.py 28 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
# SPDX-License-Identifier: Apache-2.0

4
import base64
5
import dataclasses
6
7
import logging
import os
8
import random
9
from dataclasses import dataclass, field
10
from typing import Optional
11
12
13

import pytest

14
15
16
17
18
from tests.serve.common import (
    WORKSPACE_DIR,
    params_with_model_mark,
    run_serve_deployment,
)
19
from tests.serve.conftest import MULTIMODAL_IMG_PATH, MULTIMODAL_IMG_URL
20
from tests.serve.lora_utils import MinioLoraConfig
21
from tests.utils.constants import DefaultPort
22
23
24
25
from tests.utils.engine_process import EngineConfig
from tests.utils.payload_builder import (
    chat_payload,
    chat_payload_default,
26
    chat_payload_with_logprobs,
27
    completion_payload_default,
28
    completion_payload_with_logprobs,
29
    metric_payload_default,
30
)
31
from tests.utils.payloads import LoraTestChatPayload, ToolCallingChatPayload
32
33
34
35
36

logger = logging.getLogger(__name__)


@dataclass
37
class VLLMConfig(EngineConfig):
38
39
    """Configuration for vLLM test scenarios"""

40
    stragglers: list[str] = field(default_factory=lambda: ["VLLM:EngineCore"])
41
42


43
vllm_dir = os.environ.get("VLLM_DIR") or os.path.join(
44
    WORKSPACE_DIR, "examples/backends/vllm"
45
)
46

47

48
# vLLM test configurations
49
# NOTE: pytest.mark.gpu_1 tests take ~5.5 minutes total to run sequentially (with models pre-cached)
50
51
52
53
# TODO: Now that these tests use dynamic ports, optimize the runtime by bin-packing and running
# multiple engine deployments in parallel (while keeping GPU contention under control). This may
# require annotating each config with approximate GPU RAM usage so a future collector/launcher can
# bin-pack safely.
54
55
56
vllm_configs = {
    "aggregated": VLLMConfig(
        name="aggregated",
57
        directory=vllm_dir,
58
        script_name="agg.sh",
59
60
61
        marks=[
            pytest.mark.gpu_1,
            pytest.mark.pre_merge,
62
            pytest.mark.timeout(300),  # 3x measured time (43s) + download time (150s)
63
        ],
64
        model="Qwen/Qwen3-0.6B",
65
66
67
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
68
69
70
71
72
73
74
75
76
77
78
            chat_payload(
                "Can you write me a song?",
                repeat_count=1,
                expected_response=["song"],
                temperature=0.0,
                max_tokens=32,
                extra_body={
                    "stop": ["song"],
                    "include_stop_str_in_output": True,
                },
            ),
79
            metric_payload_default(min_num_requests=6, backend="vllm"),
80
        ],
81
    ),
82
83
84
85
    "aggregated_logprobs": VLLMConfig(
        name="aggregated_logprobs",
        directory=vllm_dir,
        script_name="agg.sh",
86
        marks=[pytest.mark.gpu_1, pytest.mark.post_merge],
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        model="Qwen/Qwen3-0.6B",
        request_payloads=[
            chat_payload_with_logprobs(
                repeat_count=2,
                expected_response=["AI", "knock", "joke"],
                max_tokens=30,
                temperature=0.0,
                top_logprobs=3,
            ),
            completion_payload_with_logprobs(
                repeat_count=2,
                expected_response=["AI", "knock", "joke"],
                max_tokens=30,
                temperature=0.0,
                logprobs=5,
            ),
        ],
    ),
105
106
107
108
    "aggregated_lmcache": VLLMConfig(
        name="aggregated_lmcache",
        directory=vllm_dir,
        script_name="agg_lmcache.sh",
109
110
111
        marks=[
            pytest.mark.gpu_1,
            pytest.mark.pre_merge,
112
            pytest.mark.timeout(360),  # 3x estimated time (70s) + download time (150s)
113
        ],
114
115
116
117
118
119
120
121
        model="Qwen/Qwen3-0.6B",
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
            metric_payload_default(min_num_requests=6, backend="vllm"),
            metric_payload_default(min_num_requests=6, backend="lmcache"),
        ],
    ),
122
123
124
125
    "aggregated_lmcache_multiproc": VLLMConfig(
        name="aggregated_lmcache_multiproc",
        directory=vllm_dir,
        script_name="agg_lmcache_multiproc.sh",
126
127
        marks=[
            pytest.mark.gpu_1,
128
            pytest.mark.pre_merge,
129
            pytest.mark.timeout(360),  # 3x estimated time (70s) + download time (150s)
130
        ],
131
132
133
134
135
136
137
138
139
140
141
        model="Qwen/Qwen3-0.6B",
        env={
            "PROMETHEUS_MULTIPROC_DIR": f"/tmp/prometheus_multiproc_test_{os.getpid()}_{random.randint(0, 10000)}"
        },
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
            metric_payload_default(min_num_requests=6, backend="vllm"),
            metric_payload_default(min_num_requests=6, backend="lmcache"),
        ],
    ),
142
143
144
145
    "agg-request-plane-tcp": VLLMConfig(
        name="agg-request-plane-tcp",
        directory=vllm_dir,
        script_name="agg_request_planes.sh",
146
147
148
        marks=[
            pytest.mark.gpu_1,
            pytest.mark.pre_merge,
149
            pytest.mark.timeout(300),  # 3x measured time (43s) + download time (150s)
150
        ],
151
152
153
154
155
156
157
158
159
160
161
        model="Qwen/Qwen3-0.6B",
        script_args=["--tcp"],
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
        ],
    ),
    "agg-request-plane-http": VLLMConfig(
        name="agg-request-plane-http",
        directory=vllm_dir,
        script_name="agg_request_planes.sh",
162
163
164
        marks=[
            pytest.mark.gpu_1,
            pytest.mark.pre_merge,
165
            pytest.mark.timeout(300),  # 3x measured time (43s) + download time (150s)
166
        ],
167
168
169
170
171
172
173
        model="Qwen/Qwen3-0.6B",
        script_args=["--http"],
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
        ],
    ),
174
175
    "agg-router": VLLMConfig(
        name="agg-router",
176
        directory=vllm_dir,
177
        script_name="agg_router.sh",
178
        marks=[pytest.mark.gpu_2, pytest.mark.post_merge],
179
        model="Qwen/Qwen3-0.6B",
180
181
182
        request_payloads=[
            chat_payload_default(
                expected_log=[
183
                    r"ZMQ listener .* received batch with \d+ events \(seq=\d+(?:, [^)]*)?\)",
184
                    r"Event processor for worker_id \d+ processing event: Stored\(",
Yan Ru Pei's avatar
Yan Ru Pei committed
185
                    r"Selected worker: worker_id=\d+ dp_rank=.*?, logit: ",
186
187
188
189
190
191
                ]
            )
        ],
        env={
            "DYN_LOG": "dynamo_llm::kv_router::publisher=trace,dynamo_llm::kv_router::scheduler=info",
        },
192
    ),
193
194
    "disaggregated": VLLMConfig(
        name="disaggregated",
195
        directory=vllm_dir,
196
        script_name="disagg.sh",
197
        marks=[pytest.mark.gpu_2, pytest.mark.post_merge],
198
        model="Qwen/Qwen3-0.6B",
199
200
201
202
        request_payloads=[
            chat_payload_default(),
            completion_payload_default(),
        ],
203
    ),
204
205
    "deepep": VLLMConfig(
        name="deepep",
206
        directory=vllm_dir,
207
        script_name="dsr1_dep.sh",
208
209
210
211
        marks=[
            pytest.mark.gpu_2,
            pytest.mark.vllm,
            pytest.mark.h100,
212
            pytest.mark.nightly,
213
        ],
214
        model="deepseek-ai/DeepSeek-V2-Lite",
215
        script_args=[
216
217
218
219
220
221
222
223
224
            "--model",
            "deepseek-ai/DeepSeek-V2-Lite",
            "--num-nodes",
            "1",
            "--node-rank",
            "0",
            "--gpus-per-node",
            "2",
        ],
225
        timeout=700,
226
        request_payloads=[
227
228
            chat_payload_default(),
            completion_payload_default(),
229
        ],
230
    ),
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    "multimodal_agg_qwen2vl_2b_epd": VLLMConfig(
        name="multimodal_agg_qwen2vl_2b_epd",
        directory=vllm_dir,
        script_name="agg_multimodal_epd.sh",
        marks=[pytest.mark.gpu_1, pytest.mark.pre_merge],
        model="Qwen/Qwen2-VL-2B-Instruct",
        script_args=["--model", "Qwen/Qwen2-VL-2B-Instruct", "--single-gpu"],
        request_payloads=[
            chat_payload(
                [
                    {
                        "type": "text",
                        "text": "What colors are in the following image? Respond only with the colors.",
                    },
                    {
                        "type": "image_url",
                        "image_url": {"url": MULTIMODAL_IMG_URL},
                    },
                ],
                repeat_count=1,
                expected_response=["purple"],
                temperature=0.0,
                max_tokens=100,
            )
        ],
    ),
257
258
    "multimodal_agg_llava_epd": VLLMConfig(
        name="multimodal_agg_llava_epd",
259
        directory=vllm_dir,
260
        script_name="agg_multimodal_epd.sh",
261
        marks=[pytest.mark.gpu_2, pytest.mark.nightly],
262
        model="llava-hf/llava-1.5-7b-hf",
263
264
265
266
        script_args=["--model", "llava-hf/llava-1.5-7b-hf"],
        request_payloads=[
            chat_payload(
                [
267
268
269
270
                    {
                        "type": "text",
                        "text": "What colors are in the following image? Respond only with the colors.",
                    },
271
272
                    {
                        "type": "image_url",
273
                        "image_url": {"url": MULTIMODAL_IMG_URL},
274
275
276
                    },
                ],
                repeat_count=1,
277
                expected_response=["purple"],
278
                temperature=0.0,
279
                max_tokens=100,
280
281
            )
        ],
282
    ),
283
284
285
286
    "multimodal_agg_qwen_epd": VLLMConfig(
        name="multimodal_agg_qwen_epd",
        directory=vllm_dir,
        script_name="agg_multimodal_epd.sh",
287
        marks=[pytest.mark.gpu_2, pytest.mark.nightly],
288
289
290
291
292
293
294
        model="Qwen/Qwen2.5-VL-7B-Instruct",
        delayed_start=0,
        script_args=["--model", "Qwen/Qwen2.5-VL-7B-Instruct"],
        timeout=360,
        request_payloads=[
            chat_payload(
                [
295
296
297
298
                    {
                        "type": "text",
                        "text": "What colors are in the following image? Respond only with the colors.",
                    },
299
300
                    {
                        "type": "image_url",
301
                        "image_url": {"url": MULTIMODAL_IMG_URL},
302
303
304
                    },
                ],
                repeat_count=1,
305
306
                expected_response=["purple"],
                max_tokens=100,
307
308
309
            )
        ],
    ),
310
311
    "multimodal_agg_qwen": VLLMConfig(
        name="multimodal_agg_qwen",
312
313
        directory=vllm_dir,
        script_name="agg_multimodal.sh",
314
        marks=[pytest.mark.gpu_1, pytest.mark.pre_merge],
315
        model="Qwen/Qwen2.5-VL-7B-Instruct",
316
        script_args=["--model", "Qwen/Qwen2.5-VL-7B-Instruct"],
317
        delayed_start=0,
318
        timeout=360,
319
320
321
322
        request_payloads=[
            chat_payload(
                [
                    {
323
324
                        "type": "text",
                        "text": "What colors are in the following image? Respond only with the colors.",
325
                    },
326
327
                    {
                        "type": "image_url",
328
                        "image_url": {"url": MULTIMODAL_IMG_URL},
329
330
331
                    },
                ],
                repeat_count=1,
332
333
                expected_response=["purple"],
                max_tokens=100,
334
            ),
335
        ],
336
    ),
337
338
339
340
341
    "multimodal_agg_llava": VLLMConfig(
        name="multimodal_agg_llava",
        directory=vllm_dir,
        script_name="agg_multimodal.sh",
        marks=[
342
            pytest.mark.gpu_1,
343
            pytest.mark.nightly,
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
            # https://github.com/ai-dynamo/dynamo/issues/4501
            pytest.mark.xfail(strict=False),
        ],
        model="llava-hf/llava-1.5-7b-hf",
        script_args=["--model", "llava-hf/llava-1.5-7b-hf"],
        delayed_start=0,
        timeout=360,
        request_payloads=[
            # HTTP URL test
            chat_payload(
                [
                    {"type": "text", "text": "What is in this image?"},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "http://images.cocodataset.org/test2017/000000155781.jpg"
                        },
                    },
                ],
                repeat_count=1,
                expected_response=["bus"],
                temperature=0.0,
            ),
            # String content test - verifies string → array conversion for multimodal templates
            chat_payload_default(
                repeat_count=1,
                expected_response=[],  # Just validate no error
            ),
        ],
    ),
374
    # TODO: Update this test case when we have video multimodal support in vllm official components
375
376
    "multimodal_video_agg": VLLMConfig(
        name="multimodal_video_agg",
377
        directory=os.path.join(WORKSPACE_DIR, "examples/multimodal"),
378
        script_name="video_agg.sh",
379
        marks=[pytest.mark.gpu_2, pytest.mark.nightly],
380
381
        model="llava-hf/LLaVA-NeXT-Video-7B-hf",
        delayed_start=0,
382
        script_args=["--model", "llava-hf/LLaVA-NeXT-Video-7B-hf"],
383
        timeout=360,
384
385
386
387
388
389
390
391
392
393
394
395
396
        request_payloads=[
            chat_payload(
                [
                    {"type": "text", "text": "Describe the video in detail"},
                    {
                        "type": "video_url",
                        "video_url": {
                            "url": "https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
                        },
                    },
                ],
                repeat_count=1,
                expected_response=["rabbit"],
397
                temperature=0.7,
398
399
            )
        ],
400
    ),
401
402
403
404
    "multimodal_audio_agg": VLLMConfig(
        name="multimodal_audio_agg",
        directory="/workspace/examples/multimodal",
        script_name="audio_agg.sh",
405
        marks=[pytest.mark.gpu_2, pytest.mark.nightly],
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
        model="Qwen/Qwen2-Audio-7B-Instruct",
        delayed_start=0,
        script_args=["--model", "Qwen/Qwen2-Audio-7B-Instruct"],
        timeout=500,
        request_payloads=[
            chat_payload(
                [
                    {"type": "text", "text": "What is recited in the audio?"},
                    {
                        "type": "audio_url",
                        "audio_url": {
                            "url": "https://raw.githubusercontent.com/yuekaizhang/Triton-ASR-Client/main/datasets/mini_en/wav/1221-135766-0002.wav"
                        },
                    },
                ],
                repeat_count=1,
                expected_response=[
                    "The original content of this audio is:'yet these thoughts affected Hester Pynne less with hope than apprehension.'"
                ],
                temperature=0.8,
            )
        ],
    ),
429
430
431
432
    "aggregated_toolcalling": VLLMConfig(
        name="aggregated_toolcalling",
        directory=vllm_dir,
        script_name="agg_multimodal.sh",
433
        marks=[pytest.mark.gpu_2, pytest.mark.multimodal, pytest.mark.nightly],
434
435
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
        model="Qwen/Qwen3-VL-30B-A3B-Instruct-FP8",
        script_args=[
            "--model",
            "Qwen/Qwen3-VL-30B-A3B-Instruct-FP8",
            "--max-model-len",
            "10000",
            "--dyn-tool-call-parser",
            "hermes",
        ],
        delayed_start=0,
        timeout=600,
        request_payloads=[
            ToolCallingChatPayload(
                body={
                    "messages": [
                        {
                            "role": "user",
                            "content": [
                                {
                                    "type": "text",
                                    "text": "Describe what you see in this image in detail.",
                                },
                                {
                                    "type": "image_url",
                                    "image_url": {"url": MULTIMODAL_IMG_URL},
                                },
                            ],
                        }
                    ],
                    "tools": [
                        {
                            "type": "function",
                            "function": {
                                "name": "describe_image",
                                "description": "Provides detailed description of objects and scenes in an image",
                                "parameters": {
                                    "type": "object",
                                    "properties": {
                                        "objects": {
                                            "type": "array",
                                            "items": {"type": "string"},
                                            "description": "List of objects detected in the image",
                                        },
                                        "scene": {
                                            "type": "string",
                                            "description": "Overall scene description",
                                        },
                                    },
                                    "required": ["objects", "scene"],
                                },
                            },
                        }
                    ],
                    "tool_choice": "auto",
                    "max_tokens": 1024,
                },
                repeat_count=1,
                expected_response=["purple"],  # Validate image understanding
                expected_log=[],
                expected_tool_name="describe_image",  # Validate tool call happened
            )
        ],
    ),
497
498
499
    # TODO: Enable this test case when we have 4 GPUs runners.
    # "multimodal_disagg": VLLMConfig(
    #     name="multimodal_disagg",
500
    #     directory=os.path.join(WORKSPACE_DIR, "examples/multimodal"),
501
502
503
504
    #     script_name="disagg.sh",
    #     marks=[pytest.mark.gpu_4, pytest.mark.vllm],
    #     model="llava-hf/llava-1.5-7b-hf",
    #     delayed_start=45,
505
    #     script_args=["--model", "llava-hf/llava-1.5-7b-hf"],
506
    # ),
507
508
509
510
    "completions_only": VLLMConfig(
        name="completions_only",
        directory=vllm_dir,
        script_name="agg.sh",
511
512
        marks=[
            pytest.mark.gpu_1,
513
            pytest.mark.post_merge,
514
515
516
            pytest.mark.timeout(
                420
            ),  # 3x estimated time (60s) + download time (240s) for 7B model
517
        ],
518
519
520
521
522
523
524
525
526
527
528
        model="deepseek-ai/deepseek-llm-7b-base",
        script_args=[
            "--model",
            "deepseek-ai/deepseek-llm-7b-base",
            "--dyn-endpoint-types",
            "completions",
        ],
        request_payloads=[
            completion_payload_default(),
        ],
    ),
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
    "guided_decoding_json": VLLMConfig(
        name="guided_decoding_json",
        directory=vllm_dir,
        script_name="agg.sh",
        marks=[pytest.mark.gpu_1, pytest.mark.pre_merge],
        model="Qwen/Qwen3-0.6B",
        request_payloads=[
            chat_payload(
                "Generate a person with name and age",
                repeat_count=1,
                expected_response=['"name"', '"age"'],
                temperature=0.0,
                max_tokens=100,
                extra_body={
                    "guided_json": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "age": {"type": "integer"},
                        },
                        "required": ["name", "age"],
                    }
                },
            )
        ],
    ),
    "guided_decoding_regex": VLLMConfig(
        name="guided_decoding_regex",
        directory=vllm_dir,
        script_name="agg.sh",
        marks=[pytest.mark.gpu_1, pytest.mark.pre_merge],
        model="Qwen/Qwen3-0.6B",
        request_payloads=[
            chat_payload(
                "Generate a color name (red, blue, or green)",
                repeat_count=1,
                expected_response=["red", "blue", "green"],
                temperature=0.0,
                max_tokens=20,
                extra_body={"guided_regex": r"(red|blue|green)"},
            )
        ],
    ),
    "guided_decoding_choice": VLLMConfig(
        name="guided_decoding_choice",
        directory=vllm_dir,
        script_name="agg.sh",
        marks=[pytest.mark.gpu_1, pytest.mark.pre_merge],
        model="Qwen/Qwen3-0.6B",
        request_payloads=[
            chat_payload(
                "Generate a color name (red, blue, or green)",
                repeat_count=1,
                expected_response=["red", "blue", "green"],
                temperature=0.0,
                max_tokens=20,
                extra_body={"guided_choice": ["red", "blue", "green"]},
            )
        ],
    ),
589
590
591
}


Alec's avatar
Alec committed
592
@pytest.fixture(params=params_with_model_mark(vllm_configs))
593
594
595
596
597
def vllm_config_test(request):
    """Fixture that provides different vLLM test configurations"""
    return vllm_configs[request.param]


598
@pytest.mark.vllm
599
@pytest.mark.e2e
Alec's avatar
Alec committed
600
def test_serve_deployment(
601
602
603
604
605
606
    vllm_config_test,
    request,
    runtime_services_dynamic_ports,
    dynamo_dynamic_ports,
    predownload_models,
    image_server,
Alec's avatar
Alec committed
607
):
608
609
610
    """
    Test dynamo serve deployments with different graph configurations.
    """
611
612
613
614
    config = dataclasses.replace(
        vllm_config_test, frontend_port=dynamo_dynamic_ports.frontend_port
    )
    run_serve_deployment(config, request, ports=dynamo_dynamic_ports)
615
616
617
618
619


@pytest.mark.vllm
@pytest.mark.e2e
@pytest.mark.gpu_2
620
@pytest.mark.nightly
621
622
@pytest.mark.timeout(360)  # Match VLLMConfig.timeout for this multimodal deployment
def test_multimodal_b64(
623
624
625
626
    request,
    runtime_services_dynamic_ports,
    dynamo_dynamic_ports,
    predownload_models,
627
):
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    """
    Test multimodal inference with base64 url passthrough.

    This test is separate because it loads the required image at runtime
    (not collection time), ensuring it only fails when actually executed.
    """
    # Load B64 image at test execution time
    with open(MULTIMODAL_IMG_PATH, "rb") as f:
        b64_img = base64.b64encode(f.read()).decode()

    # Create payload with B64 image
    b64_payload = chat_payload(
        [
            {
                "type": "text",
                "text": "What colors are in the following image? Respond only with the colors.",
            },
            {
                "type": "image_url",
                "image_url": {"url": f"data:image/png;base64,{b64_img}"},
            },
        ],
        repeat_count=1,
        expected_response=["purple"],
        max_tokens=100,
    )

    # Create test config
    config = VLLMConfig(
        name="test_multimodal_b64",
        directory=vllm_dir,
        script_name="agg_multimodal.sh",
        marks=[],  # markers at function-level
        model="Qwen/Qwen2.5-VL-7B-Instruct",
        script_args=["--model", "Qwen/Qwen2.5-VL-7B-Instruct"],
        delayed_start=0,
        timeout=360,
        request_payloads=[b64_payload],
    )

668
669
670
671
    config = dataclasses.replace(
        config, frontend_port=dynamo_dynamic_ports.frontend_port
    )
    run_serve_deployment(config, request, ports=dynamo_dynamic_ports)
672
673
674
675
676
677
678
679
680


# LoRA Test Directory
lora_dir = os.path.join(vllm_dir, "launch/lora")


def lora_chat_payload(
    lora_name: str,
    s3_uri: str,
681
    system_port: int = DefaultPort.SYSTEM1.value,
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
708
709
710
711
712
713
714
715
716
    repeat_count: int = 2,
    expected_response: Optional[list] = None,
    expected_log: Optional[list] = None,
    max_tokens: int = 100,
    temperature: float = 0.0,
) -> LoraTestChatPayload:
    """Create a LoRA-enabled chat payload for testing"""
    return LoraTestChatPayload(
        body={
            "model": lora_name,
            "messages": [
                {
                    "role": "user",
                    "content": "What is deep learning? Answer in one sentence.",
                }
            ],
            "max_tokens": max_tokens,
            "temperature": temperature,
            "stream": False,
        },
        lora_name=lora_name,
        s3_uri=s3_uri,
        system_port=system_port,
        repeat_count=repeat_count,
        expected_response=expected_response
        or ["learning", "neural", "network", "AI", "model"],
        expected_log=expected_log or [],
    )


@pytest.mark.vllm
@pytest.mark.e2e
@pytest.mark.gpu_1
@pytest.mark.model("Qwen/Qwen3-0.6B")
@pytest.mark.timeout(600)
717
@pytest.mark.post_merge
718
def test_lora_aggregated(
719
720
721
722
723
    request,
    runtime_services_dynamic_ports,
    predownload_models,
    minio_lora_service,
    dynamo_dynamic_ports,
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
):
    """
    Test LoRA inference with aggregated vLLM deployment.

    This test:
    1. Uses MinIO fixture to provide S3-compatible storage with uploaded LoRA
    2. Starts vLLM with LoRA support enabled
    3. Loads the LoRA adapter via system API
    4. Runs inference with the LoRA model
    """
    minio_config: MinioLoraConfig = minio_lora_service

    # Create payload that loads LoRA and tests inference
    lora_payload = lora_chat_payload(
        lora_name=minio_config.lora_name,
        s3_uri=minio_config.get_s3_uri(),
740
        system_port=DefaultPort.SYSTEM1.value,
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
        repeat_count=2,
    )

    # Create test config with MinIO environment variables
    config = VLLMConfig(
        name="test_lora_aggregated",
        directory=vllm_dir,
        script_name="lora/agg_lora.sh",
        marks=[],  # markers at function-level
        model="Qwen/Qwen3-0.6B",
        timeout=600,
        env=minio_config.get_env_vars(),
        request_payloads=[lora_payload],
    )

756
757
758
759
760
761
762
763
764
    config = dataclasses.replace(
        config, frontend_port=dynamo_dynamic_ports.frontend_port
    )
    run_serve_deployment(
        config,
        request,
        ports=dynamo_dynamic_ports,
        extra_env=minio_config.get_env_vars(),
    )
765
766
767
768
769
770
771


@pytest.mark.vllm
@pytest.mark.e2e
@pytest.mark.gpu_2
@pytest.mark.model("Qwen/Qwen3-0.6B")
@pytest.mark.timeout(600)
772
@pytest.mark.post_merge
773
@pytest.mark.parametrize("num_system_ports", [2], indirect=True)
774
def test_lora_aggregated_router(
775
776
777
778
779
    request,
    runtime_services_dynamic_ports,
    predownload_models,
    minio_lora_service,
    dynamo_dynamic_ports,
780
    num_system_ports,
781
782
783
784
785
786
787
788
789
790
):
    """
    Test LoRA inference with aggregated vLLM deployment using KV router.

    This test:
    1. Uses MinIO fixture to provide S3-compatible storage with uploaded LoRA
    2. Starts multiple vLLM workers with LoRA support and KV router
    3. Loads the LoRA adapter on both workers via system API
    4. Runs inference with the LoRA model, verifying KV cache routing
    """
791
792
793
    assert (
        num_system_ports >= 2
    ), "serve tests require at least SYSTEM_PORT1 + SYSTEM_PORT2"
794
795
796
    minio_config: MinioLoraConfig = minio_lora_service

    # Create payloads that load LoRA on both workers and test inference
797
    # Worker 1 (DefaultPort.SYSTEM1)
798
799
800
    lora_payload_worker1 = lora_chat_payload(
        lora_name=minio_config.lora_name,
        s3_uri=minio_config.get_s3_uri(),
801
        system_port=DefaultPort.SYSTEM1.value,
802
803
804
        repeat_count=1,
    )

805
    # Worker 2 (DefaultPort.SYSTEM2)
806
807
808
    lora_payload_worker2 = lora_chat_payload(
        lora_name=minio_config.lora_name,
        s3_uri=minio_config.get_s3_uri(),
809
        system_port=DefaultPort.SYSTEM2.value,
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
        repeat_count=1,
    )

    # Additional inference payload to test routing (LoRA already loaded)
    inference_payload = chat_payload(
        content="Explain machine learning in simple terms.",
        repeat_count=2,
        expected_response=["learn", "data", "algorithm", "model", "pattern"],
        max_tokens=150,
        temperature=0.0,
    ).with_model(minio_config.lora_name)

    # Add env vars including PYTHONHASHSEED for deterministic KV event IDs
    env_vars = minio_config.get_env_vars()
    env_vars["PYTHONHASHSEED"] = "0"

    # Create test config with MinIO environment variables
    config = VLLMConfig(
        name="test_lora_aggregated_router",
        directory=vllm_dir,
        script_name="lora/agg_lora_router.sh",
        marks=[],  # markers at function-level
        model="Qwen/Qwen3-0.6B",
        timeout=600,
        env=env_vars,
        request_payloads=[
            lora_payload_worker1,
            lora_payload_worker2,
            inference_payload,
        ],
    )

842
843
844
845
846
847
    config = dataclasses.replace(
        config, frontend_port=dynamo_dynamic_ports.frontend_port
    )
    run_serve_deployment(
        config, request, ports=dynamo_dynamic_ports, extra_env=env_vars
    )