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

4
5
6
7
8
9
10
11
"""
Test Execution Times (Last Run: 2025-12-09):
- test_request_cancellation_vllm_aggregated: ~55s (gpu_1)
- test_request_cancellation_vllm_decode_cancel: ~53s (gpu_2)
- test_request_cancellation_vllm_prefill_cancel: ~53s (gpu_2)
- Total: 161.65s (0:02:41)
"""

12
13
14
15
16
17
import logging
import os
import shutil

import pytest

18
19
from tests.fault_tolerance.cancellation.utils import (
    DynamoFrontendProcess,
20
21
22
    poll_for_pattern,
    read_streaming_responses,
    send_cancellable_request,
23
)
Alec's avatar
Alec committed
24
from tests.utils.constants import FAULT_TOLERANCE_MODEL_NAME
25
from tests.utils.managed_process import ManagedProcess
26
from tests.utils.payloads import check_health_generate, check_models_api
27
from tests.utils.port_utils import allocate_port, deallocate_port
28
29
30

logger = logging.getLogger(__name__)

31
32
33
34
35
pytestmark = [
    pytest.mark.vllm,
    pytest.mark.gpu_1,
    pytest.mark.e2e,
    pytest.mark.model(FAULT_TOLERANCE_MODEL_NAME),
36
    pytest.mark.post_merge,  # post_merge to pinpoint failure commit
37
38
]

39
40
41
42

class DynamoWorkerProcess(ManagedProcess):
    """Process manager for Dynamo worker with vLLM backend"""

43
44
45
46
47
48
49
50
51
52
53
    def __init__(
        self,
        request,
        frontend_port: int,
        is_prefill: bool = False,
    ):
        # Allocate system port for this worker
        system_port = allocate_port(9100)
        self.system_port = system_port
        self.frontend_port = frontend_port

54
55
56
57
58
        command = [
            "python3",
            "-m",
            "dynamo.vllm",
            "--model",
Alec's avatar
Alec committed
59
            FAULT_TOLERANCE_MODEL_NAME,
60
61
62
63
            "--enforce-eager",
            "--gpu-memory-utilization",
            "0.45",
            "--max-model-len",
64
            "16384",
65
66
67
68
            "--migration-limit",
            "3",
        ]

69
        # Configure health check based on worker type
70
        if is_prefill:
71
            # Prefill workers check their own status endpoint
72
            command.append("--is-prefill-worker")
73
74
75
            health_check_urls = [
                (f"http://localhost:{system_port}/health", self.is_ready)
            ]
76
77
78
79
        else:
            # Decode workers should also check their own status endpoint first,
            # then verify the frontend sees the model
            health_check_urls = [
80
81
82
                (f"http://localhost:{system_port}/health", self.is_ready),
                (f"http://localhost:{frontend_port}/v1/models", check_models_api),
                (f"http://localhost:{frontend_port}/health", check_health_generate),
83
            ]
84

85
        # Set environment variables
86
        env = os.environ.copy()
87
88
        env["DYN_REQUEST_PLANE"] = request.getfixturevalue("request_plane")

89
        env["DYN_LOG"] = "debug"
90
91
92
93
94
        # Disable canary health check - these tests expect full control over requests
        # sent to the workers where canary health check intermittently sends dummy
        # requests to workers interfering with the test process which may cause
        # intermittent failures
        env["DYN_HEALTH_CHECK_ENABLED"] = "false"
95
        env["DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS"] = '["generate"]'
96
97
        env["DYN_SYSTEM_PORT"] = str(system_port)
        env["DYN_HTTP_PORT"] = str(frontend_port)
98

99
100
101
        # Set KV event port and NIXL side channel port only for prefill worker
        # to avoid conflicts with decode worker
        if is_prefill:
102
103
104
105
            env["DYN_VLLM_KV_EVENT_PORT"] = "20082"  # TODO: use dynamic port allocation
            env[
                "VLLM_NIXL_SIDE_CHANNEL_PORT"
            ] = "5601"  # TODO: use dynamic port allocation
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        # Set log directory based on worker type
        worker_type = "prefill_worker" if is_prefill else "worker"
        log_dir = f"{request.node.name}_{worker_type}"

        # Clean up any existing log directory from previous runs
        try:
            shutil.rmtree(log_dir)
            logger.info(f"Cleaned up existing log directory: {log_dir}")
        except FileNotFoundError:
            # Directory doesn't exist, which is fine
            pass

        super().__init__(
            command=command,
            env=env,
122
            health_check_urls=health_check_urls,
123
124
125
            timeout=300,
            display_output=True,
            terminate_existing=False,
126
127
128
129
130
131
132
            # Ensure any orphaned vLLM engine cores or child helpers are cleaned up
            stragglers=[
                "VLLM::EngineCore",
            ],
            straggler_commands=[
                "-m dynamo.vllm",
            ],
133
134
135
136
137
138
139
140
141
            log_dir=log_dir,
        )

        self.is_prefill = is_prefill

    def get_pid(self):
        """Get the PID of the worker process"""
        return self.proc.pid if self.proc else None

142
143
144
145
146
147
148
149
150
151
    def __exit__(self, exc_type, exc_val, exc_tb):
        """Release allocated port when worker exits."""
        try:
            # system_port is always allocated in __init__
            deallocate_port(self.system_port)
        except Exception as e:
            logging.warning(f"Failed to release vLLM worker port: {e}")

        return super().__exit__(exc_type, exc_val, exc_tb)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    def is_ready(self, response) -> bool:
        """Check the health of the worker process"""
        try:
            data = response.json()
            if data.get("status") == "ready":
                worker_type = "Prefill worker" if self.is_prefill else "Worker"
                logger.info(f"{worker_type} status is ready")
                return True
            worker_type = "Prefill worker" if self.is_prefill else "Worker"
            logger.warning(f"{worker_type} status is not ready: {data.get('status')}")
        except ValueError:
            worker_type = "Prefill worker" if self.is_prefill else "Worker"
            logger.warning(f"{worker_type} health response is not valid JSON")
        return False


168
@pytest.mark.timeout(110)  # 3x average
169
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
170
171
172
def test_request_cancellation_vllm_aggregated(
    request, runtime_services_dynamic_ports, predownload_models
):
173
    """
174
    End-to-end test for request cancellation functionality in aggregated mode.
175
176
177

    This test verifies that when a request is cancelled by the client,
    the system properly handles the cancellation and cleans up resources
178
    on the worker side in aggregated (single worker) mode. Tests three scenarios:
179
180
181
    1. Completion request
    2. Chat completion request (non-streaming)
    3. Chat completion request (streaming)
182
183
184
185
186

    Timing (Last Run: 2025-12-09): ~55s total
    - Engine initialization: ~15s
    - Testing 3 scenarios: ~38s (~12s each)
    - Teardown: ~2s
187
188
    """

189
    # Step 1: Start the frontend (allocates its own frontend_port)
190
191
192
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

193
194
        # Step 2: Start a single worker (allocates its own system_port)
        with DynamoWorkerProcess(request, frontend.frontend_port) as worker:
195
196
            logger.info(f"Worker PID: {worker.get_pid()}")

197
            # Step 3: Test request cancellation with polling approach
198
199
200
201
202
203
204
205
206
207
208
            frontend_log_offset, worker_log_offset = 0, 0

            test_scenarios = [
                ("completion", "Completion request cancellation"),
                ("chat_completion", "Chat completion request cancellation"),
                (
                    "chat_completion_stream",
                    "Chat completion stream request cancellation",
                ),
            ]

209
            for request_type, description in test_scenarios:
210
211
                logger.info(f"Testing {description.lower()}...")

212
                # Send the request (non-blocking)
213
214
215
                cancellable_req = send_cancellable_request(
                    frontend.frontend_port, request_type
                )
216

217
                # Poll for "Decode Request ID" pattern (vLLM v2 pattern)
218
219
                request_id, worker_log_offset = poll_for_pattern(
                    process=worker,
220
                    pattern="Decode Request ID: ",
221
222
                    log_offset=worker_log_offset,
                    match_type="contains",
223
                )
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

                # For streaming, read 5 responses before cancelling
                if request_type == "chat_completion_stream":
                    read_streaming_responses(cancellable_req, expected_count=5)

                # Now cancel the request
                cancellable_req.cancel()
                logger.info(f"Cancelled request ID: {request_id}")

                # Poll for "Aborted Request ID" with matching ID
                _, worker_log_offset = poll_for_pattern(
                    process=worker,
                    pattern=f"Aborted Request ID: {request_id}",
                    log_offset=worker_log_offset,
                )

                # Verify frontend log has kill message
                _, frontend_log_offset = poll_for_pattern(
                    process=frontend,
                    pattern="issued control message Kill to sender",
                    log_offset=frontend_log_offset,
245
246
247
248
249
                )

                logger.info(f"{description} detected successfully")


250
@pytest.mark.timeout(150)  # 3x average
251
252
253
254
255
256
257
258
259
260
261
@pytest.mark.parametrize(
    "request_plane",
    [
        "nats",
        pytest.param(
            "tcp",
            marks=pytest.mark.xfail(reason="Multi-worker TCP unstable", strict=False),
        ),
    ],
    indirect=True,
)
262
def test_request_cancellation_vllm_decode_cancel(
263
    request, runtime_services_dynamic_ports, set_ucx_tls_no_mm, predownload_models
Alec's avatar
Alec committed
264
):
265
    """
266
    End-to-end test for request cancellation during decode phase.
267

268
    This test verifies that when a request is cancelled by the client during the decode phase,
269
270
    the system properly handles the cancellation and cleans up resources
    on the decode worker side in a disaggregated setup.
271
272
273
274
275

    Timing (Last Run: 2025-12-09): ~53s total (requires 2 GPUs)
    - Engine initialization: ~23s (decode + prefill workers)
    - Testing stream cancellation during decode: ~28s
    - Teardown: ~2s
276
277
    """

278
    # Step 1: Start the frontend (allocates its own frontend_port)
279
280
281
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

282
283
284
285
        # Step 2: Start the prefill worker (allocates its own system_port)
        with DynamoWorkerProcess(
            request, frontend.frontend_port, is_prefill=True
        ) as prefill_worker:
286
287
            logger.info(f"Prefill Worker PID: {prefill_worker.get_pid()}")

288
289
290
291
            # Step 3: Start the decode worker (allocates its own system_port)
            with DynamoWorkerProcess(
                request, frontend.frontend_port, is_prefill=False
            ) as decode_worker:
292
293
                logger.info(f"Decode Worker PID: {decode_worker.get_pid()}")

294
                # Step 4: Test request cancellation for streaming scenario
295
                logger.info(
296
297
298
299
                    "Testing chat completion stream request cancellation in decode worker (decode phase)..."
                )

                # Send streaming request (non-blocking)
300
301
302
                cancellable_req = send_cancellable_request(
                    frontend.frontend_port, "chat_completion_stream"
                )
303

304
                # Poll for "Decode Request ID" pattern in decode worker (vLLM v2 pattern)
305
306
                request_id, decode_log_offset = poll_for_pattern(
                    process=decode_worker,
307
                    pattern="Decode Request ID: ",
308
309
310
                    match_type="contains",
                )

311
                # Verify same request ID reached prefill worker (as "Prefill Request ID")
312
313
                _, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
314
                    pattern=f"Prefill Request ID: {request_id}",
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
                )

                # Read 5 streaming responses (decode phase)
                read_streaming_responses(cancellable_req, expected_count=5)

                # Now cancel the request
                cancellable_req.cancel()
                logger.info(f"Cancelled request ID: {request_id}")

                # Poll for "Aborted Request ID" in decode worker
                _, decode_log_offset = poll_for_pattern(
                    process=decode_worker,
                    pattern=f"Aborted Request ID: {request_id}",
                    log_offset=decode_log_offset,
                )

                # Verify frontend log has kill message
                _, frontend_log_offset = poll_for_pattern(
                    process=frontend,
                    pattern="issued control message Kill to sender",
335
336
337
                )

                logger.info(
338
                    "Chat completion stream cancellation in decode phase detected successfully"
339
340
341
                )


342
@pytest.mark.timeout(150)  # 3x average
343
344
345
346
347
348
349
350
351
352
353
@pytest.mark.parametrize(
    "request_plane",
    [
        "nats",
        pytest.param(
            "tcp",
            marks=pytest.mark.xfail(reason="Multi-worker TCP unstable", strict=False),
        ),
    ],
    indirect=True,
)
354
def test_request_cancellation_vllm_prefill_cancel(
355
    request, runtime_services_dynamic_ports, set_ucx_tls_no_mm, predownload_models
356
):
357
    """
358
    End-to-end test for request cancellation during prefill phase.
359

360
    This test verifies that when a request is cancelled by the client during the prefill phase,
361
362
    the system properly handles the cancellation and cleans up resources
    on both the decode and prefill workers in a disaggregated setup.
363
364
365
366
367

    Timing (Last Run: 2025-12-09): ~53s total (requires 2 GPUs)
    - Engine initialization: ~23s (decode + prefill workers)
    - Testing cancellation during prefill: ~28s
    - Teardown: ~2s
368
    """
369

370
    # Step 1: Start the frontend (allocates its own frontend_port)
371
372
373
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

374
375
376
377
        # Step 2: Start the prefill worker (allocates its own system_port)
        with DynamoWorkerProcess(
            request, frontend.frontend_port, is_prefill=True
        ) as prefill_worker:
378
379
            logger.info(f"Prefill Worker PID: {prefill_worker.get_pid()}")

380
381
382
383
            # Step 3: Start the decode worker (allocates its own system_port)
            with DynamoWorkerProcess(
                request, frontend.frontend_port, is_prefill=False
            ) as decode_worker:
384
385
                logger.info(f"Decode Worker PID: {decode_worker.get_pid()}")

386
387
388
                # Step 4: Test request cancellation during prefill phase
                # Note: With the new architecture, prefill routing happens in the frontend,
                # so the request goes directly to the prefill worker first
389
                logger.info(
390
                    "Testing completion request cancellation during prefill phase..."
391
392
                )

393
394
                # Send request with long prompt (non-blocking)
                cancellable_req = send_cancellable_request(
395
                    frontend.frontend_port, "completion", use_long_prompt=True
396
                )
397

398
399
400
                # Poll for "Prefill Request ID" pattern in prefill worker (vLLM v2 pattern)
                # With new architecture, prefill is routed by frontend's internal router
                request_id, prefill_log_offset = poll_for_pattern(
401
                    process=prefill_worker,
402
403
                    pattern="Prefill Request ID: ",
                    match_type="contains",
404
405
406
407
                )

                # Cancel during prefill phase
                cancellable_req.cancel()
408
                logger.info(f"Cancelled request ID: {request_id} during prefill")
409

410
                # Poll for "Aborted Prefill Request ID" in prefill worker (where cancellation happens)
411
412
413
414
415
416
417
418
419
420
421
422
                _, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
                    pattern=f"Aborted Prefill Request ID: {request_id}",
                    log_offset=prefill_log_offset,
                )

                # Verify frontend log has kill message
                _, frontend_log_offset = poll_for_pattern(
                    process=frontend,
                    pattern="issued control message Kill to sender",
                )

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
                # Verify decode worker never received the request
                pattern = "Request ID: "
                try:
                    _, decode_log_offset = poll_for_pattern(
                        process=decode_worker,
                        pattern=pattern,
                        max_wait_ms=10,
                        match_type="contains",
                    )
                    pytest.fail(
                        "Decode worker received request cancelled during prefill phase"
                    )
                except AssertionError as e:
                    assert str(e).startswith(
                        f"Failed to find '{pattern}' pattern after 2 iterations "
                    ), f"Unexpected error: {e}"

440
                logger.info(
441
                    "Completion request cancellation during prefill phase detected successfully"
442
                )