"tests/models/multimodal/generation/test_florence2.py" did not exist on "92ec5d8e100e0718039a8f647065ff556168562f"
test_trtllm.py 23.2 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
"""
5
Test Execution Times (Last Run: 2025-12-13):
6
- test_request_cancellation_trtllm_aggregated: ~45s (gpu_1)
7
8
9
10
- test_request_cancellation_trtllm_decode_cancel: ~65s (gpu_1)
- test_request_cancellation_trtllm_prefill_cancel: ~65s (gpu_1)
- test_request_cancellation_trtllm_kv_transfer_cancel: ~65s (gpu_1)
- Total: ~240s x2 request planes = ~480s (0:08:00)
11
12
"""

13
14
15
16
17
18
19
20
21
import logging
import os
import shutil
import time

import pytest

from tests.fault_tolerance.cancellation.utils import (
    DynamoFrontendProcess,
22
23
24
    poll_for_pattern,
    read_streaming_responses,
    send_cancellable_request,
25
26
    verify_frontend_cancellation_metrics,
    verify_runtime_cancellation_metrics,
27
28
29
30
)
from tests.utils.constants import FAULT_TOLERANCE_MODEL_NAME
from tests.utils.managed_process import ManagedProcess
from tests.utils.payloads import check_health_generate, check_models_api
31
from tests.utils.port_utils import allocate_port, deallocate_port
32
33
34

logger = logging.getLogger(__name__)

35
pytestmark = [
36
    pytest.mark.fault_tolerance,
37
38
39
40
    pytest.mark.trtllm,
    pytest.mark.gpu_1,
    pytest.mark.e2e,
    pytest.mark.model(FAULT_TOLERANCE_MODEL_NAME),
41
    pytest.mark.nightly,
42
    pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True),
43
44
]

45
46
47
48

class DynamoWorkerProcess(ManagedProcess):
    """Process manager for Dynamo worker with TensorRT-LLM backend"""

49
50
51
52
53
54
    def __init__(
        self,
        request,
        frontend_port: int,
        mode: str = "prefill_and_decode",
    ):
55
56
57
58
59
        """
        Initialize TensorRT-LLM worker process.

        Args:
            request: pytest request object
60
            frontend_port: Port for the frontend server
61
62
            mode: One of "prefill_and_decode", "prefill", "decode"
        """
63
64
65
66
        # Allocate system port for this worker
        system_port = allocate_port(9100)
        self.system_port = system_port
        self.frontend_port = frontend_port
67

68
69
70
71
72
73
74
75
76
        command = [
            "python3",
            "-m",
            "dynamo.trtllm",
            "--model",
            FAULT_TOLERANCE_MODEL_NAME,
            "--disaggregation-mode",
            mode,
            "--max-seq-len",
77
78
79
            "16384",
            "--max-num-tokens",
            "16384",
80
81
82
        ]
        if mode != "prefill_and_decode":
            with open("test_request_cancellation_trtllm_config.yaml", "w") as f:
83
84
85
                f.write(
                    "cache_transceiver_config:\n  backend: DEFAULT\n  max_tokens_in_buffer: 16384\n"
                )
86
                f.write("disable_overlap_scheduler: true\n")
87
                f.write("kv_cache_config:\n  max_tokens: 16384\n")
88
89
90
91
92
93
            command += [
                "--extra-engine-args",
                "test_request_cancellation_trtllm_config.yaml",
            ]

        health_check_urls = [
94
95
            (f"http://localhost:{frontend_port}/v1/models", check_models_api),
            (f"http://localhost:{frontend_port}/health", check_health_generate),
96
97
        ]

98
99
100
101
102
        # Set health check based on worker type
        if mode in ["prefill", "decode"]:
            health_check_urls = [
                (f"http://localhost:{system_port}/health", self.is_ready)
            ]
103

104
        # Set environment variables
105
        env = os.environ.copy()
106
107
        env["DYN_REQUEST_PLANE"] = request.getfixturevalue("request_plane")

108
        env["DYN_LOG"] = "debug"
109
110
111
112
113
        # 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"
114
        env["DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS"] = '["generate"]'
115
        env["DYN_SYSTEM_PORT"] = str(system_port)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

        # Set log directory based on worker type
        log_dir = f"{request.node.name}_{mode}_worker"

        # 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,
            health_check_urls=health_check_urls,
            timeout=300,
            display_output=True,
134
            terminate_all_matching_process_names=False,
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            log_dir=log_dir,
        )

        self.mode = mode

    def is_ready(self, response) -> bool:
        """Check the health of the worker process"""
        try:
            data = response.json()
            if data.get("status") == "ready":
                logger.info(f"{self.mode.capitalize()} worker status is ready")
                return True
            logger.warning(
                f"{self.mode.capitalize()} worker status is not ready: {data.get('status')}"
            )
        except ValueError:
            logger.warning(
                f"{self.mode.capitalize()} worker health response is not valid JSON"
            )
        return False

156
157
158
159
160
161
162
163
164
165
    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 TRT-LLM worker port: {e}")

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

166

167
@pytest.mark.timeout(135)  # 3x average
168
def test_request_cancellation_trtllm_aggregated(
169
    request, runtime_services_dynamic_ports, predownload_models
170
):
171
172
173
174
175
    """
    End-to-end test for request cancellation functionality in aggregated mode.

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

    Timing (Last Run: 2025-12-09): ~45s total
    - Engine initialization: ~27s (frontend + worker)
    - Testing 3 scenarios: ~15s (~5s each)
    - Teardown: ~3s
185
186
    """

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

191
        # Step 2: Start an aggregated worker (allocates its own system_port)
192
193
194
        with DynamoWorkerProcess(
            request, frontend.frontend_port, mode="prefill_and_decode"
        ) as worker:
195
196
197
198
199
            logger.info(f"Aggregated Worker PID: {worker.get_pid()}")

            # TODO: Why wait after worker ready fixes frontend 404 / 500 flakiness?
            time.sleep(2)

200
            # Step 3: Test request cancellation with polling approach
201
202
203
204
205
206
207
208
209
210
211
            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",
                ),
            ]

212
            for idx, (request_type, description) in enumerate(test_scenarios):
213
214
                logger.info(f"Testing {description.lower()}...")

215
                # Send the request (non-blocking)
216
217
218
                cancellable_req = send_cancellable_request(
                    frontend.frontend_port, request_type
                )
219

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

                # 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,
248
249
250
251
                )

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

252
253
254
255
256
257
258
259
260
261
262
263
                # Verify cancellation metrics after each scenario
                verify_frontend_cancellation_metrics(
                    frontend_port=frontend.frontend_port,
                    request_type=request_type,
                    expected_count=1,
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=worker.system_port,
                    expected_count=idx + 1,
                    component="tensorrt_llm",
                )

264

265
@pytest.mark.timeout(195)  # 3x average
266
def test_request_cancellation_trtllm_decode_cancel(
267
    request, runtime_services_dynamic_ports, predownload_models
268
):
269
    """
270
    End-to-end test for request cancellation during decode phase with unified frontend.
271
272
273

    This test verifies that when a request is cancelled by the client during the decode phase,
    the system properly handles the cancellation and cleans up resources
274
    on the decode worker side in a disaggregated setup.
275
276
277
278
279

    Timing (Last Run: 2025-12-09): ~115s total (2 workers at 45% GPU each)
    - Engine initialization: ~92s (frontend: 2s, prefill worker: 45s, decode worker: 45s sequential)
    - Testing stream cancellation during decode: ~20s
    - Teardown: ~3s
280
281
    """

282
    # Step 1: Start the frontend (allocates its own frontend_port)
283
284
285
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

286
287
288
289
        # Step 2: Start the prefill worker (allocates its own system_port)
        with DynamoWorkerProcess(
            request, frontend.frontend_port, mode="prefill"
        ) as prefill_worker:
290
291
            logger.info(f"Prefill Worker PID: {prefill_worker.get_pid()}")

292
293
294
295
            # Step 3: Start the decode worker (allocates its own system_port)
            with DynamoWorkerProcess(
                request, frontend.frontend_port, mode="decode"
            ) as decode_worker:
296
297
298
299
300
                logger.info(f"Decode Worker PID: {decode_worker.get_pid()}")

                # TODO: Why wait after worker ready fixes frontend 404 / 500 flakiness?
                time.sleep(2)

301
                # Step 4: Test request cancellation for streaming scenario
302
                logger.info(
303
                    "Testing chat completion stream request cancellation in decode worker (decode phase)..."
304
305
                )

306
                # Send streaming request (non-blocking)
307
308
309
                cancellable_req = send_cancellable_request(
                    frontend.frontend_port, "chat_completion_stream"
                )
310

311
312
313
314
                # Poll for "Prefill Request ID" pattern in prefill worker (frontend routes here first)
                request_id, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
                    pattern="Prefill Request ID: ",
315
316
317
                    match_type="contains",
                )

318
319
320
321
                # Verify same request ID reached decode worker (after prefill completes)
                _, decode_log_offset = poll_for_pattern(
                    process=decode_worker,
                    pattern=f"Decode Request ID: {request_id}",
322
                )
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

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

                logger.info(
                    "Chat completion stream cancellation in decode phase detected successfully"
346
347
                )

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
                # Verify cancellation metrics
                verify_frontend_cancellation_metrics(
                    frontend_port=frontend.frontend_port,
                    request_type="chat_completion_stream",
                    expected_count=1,
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=decode_worker.system_port,
                    expected_count=1,
                    component="tensorrt_llm",
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=prefill_worker.system_port,
                    expected_count=0,
                    component="prefill",
                )

365

366
@pytest.mark.skip(reason="TRT-LLM prefill cancellation is disabled due to reliability")
367
@pytest.mark.timeout(195)  # 3x average
368
def test_request_cancellation_trtllm_prefill_cancel(
369
    request, runtime_services_dynamic_ports, predownload_models
370
):
371
    """
372
    End-to-end test for request cancellation during prefill phase with unified frontend.
373

374
375
376
    This test verifies that when a request is cancelled by the client during the prefill phase,
    the system properly handles the cancellation and cleans up resources on the prefill worker.
    Since the request is cancelled before prefill completes, the decode worker never receives it.
377
378
379
380
381

    Timing (Last Run: 2025-12-09): ~115s total (2 workers at 45% GPU each)
    - Engine initialization: ~92s (frontend: 2s, prefill worker: 45s, decode worker: 45s sequential)
    - Testing cancellation during prefill: ~20s
    - Teardown: ~3s
382
383
    """

384
    # Step 1: Start the frontend (allocates its own frontend_port)
385
386
387
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

388
389
390
391
        # Step 2: Start the prefill worker (allocates its own system_port)
        with DynamoWorkerProcess(
            request, frontend.frontend_port, mode="prefill"
        ) as prefill_worker:
392
393
            logger.info(f"Prefill Worker PID: {prefill_worker.get_pid()}")

394
395
396
397
            # Step 3: Start the decode worker (allocates its own system_port)
            with DynamoWorkerProcess(
                request, frontend.frontend_port, mode="decode"
            ) as decode_worker:
398
399
400
401
402
403
404
405
406
407
                logger.info(f"Decode Worker PID: {decode_worker.get_pid()}")

                # TODO: Why wait after worker ready fixes frontend 404 / 500 flakiness?
                time.sleep(2)

                # Step 4: Test request cancellation during prefill phase
                logger.info(
                    "Testing completion request cancellation during prefill phase..."
                )

408
409
                # Send request with long prompt (non-blocking)
                cancellable_req = send_cancellable_request(
410
                    frontend.frontend_port, "completion", use_long_prompt=True
411
412
                )

413
                # Poll for "Prefill Request ID" pattern in prefill worker (frontend routes here first)
414
415
                request_id, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
416
                    pattern="Prefill Request ID: ",
417
418
419
                    match_type="contains",
                )

420
                # Cancel during prefill phase
421
                cancellable_req.cancel()
422
                logger.info(f"Cancelled request ID: {request_id} during prefill")
423

424
                # Poll for "Aborted Request ID" in prefill worker (where cancellation happens)
425
426
427
428
429
430
431
432
433
434
                _, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
                    pattern=f"Aborted 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",
435
                )
436

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
                # 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}"

454
455
                logger.info(
                    "Completion request cancellation during prefill phase detected successfully"
456
                )
457

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                # Verify cancellation metrics
                verify_frontend_cancellation_metrics(
                    frontend_port=frontend.frontend_port,
                    request_type="completion",
                    expected_count=1,
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=decode_worker.system_port,
                    expected_count=0,
                    component="tensorrt_llm",
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=prefill_worker.system_port,
                    expected_count=1,
                    component="prefill",
                )

475

476
@pytest.mark.skip(reason="Test fails only on CI")
477
@pytest.mark.timeout(195)  # 3x average
478
def test_request_cancellation_trtllm_kv_transfer_cancel(
479
    request, runtime_services_dynamic_ports, predownload_models
480
):
481
482
483
484
485
    """
    End-to-end test for request cancellation during prefill to decode KV transfer phase.

    This test verifies that when a request is cancelled by the client during the KV transfer phase,
    the system properly handles the cancellation and cleans up resources on the workers.
486
487
488
489
490

    Timing (Last Run: 2025-12-09): ~115s total (2 workers at 45% GPU each)
    - Engine initialization: ~92s (frontend: 2s, prefill worker: 45s, decode worker: 45s sequential)
    - Testing KV transfer cancellation: ~20s
    - Teardown: ~3s
491
492
    """

493
    # Step 1: Start the frontend (allocates its own frontend_port)
494
495
496
    with DynamoFrontendProcess(request) as frontend:
        logger.info("Frontend started successfully")

497
498
499
500
        # Step 2: Start the prefill worker (allocates its own system_port)
        with DynamoWorkerProcess(
            request, frontend.frontend_port, mode="prefill"
        ) as prefill_worker:
501
502
            logger.info(f"Prefill Worker PID: {prefill_worker.get_pid()}")

503
504
505
506
            # Step 3: Start the decode worker (allocates its own system_port)
            with DynamoWorkerProcess(
                request, frontend.frontend_port, mode="decode"
            ) as decode_worker:
507
508
509
510
511
512
513
514
515
516
517
518
                logger.info(f"Decode Worker PID: {decode_worker.get_pid()}")

                # TODO: Why wait after worker ready fixes frontend 404 / 500 flakiness?
                time.sleep(2)

                # Step 4: Test request cancellation during KV transfer phase
                logger.info(
                    "Testing completion request cancellation during KV transfer phase..."
                )

                # Send request with long prompt
                cancellable_req = send_cancellable_request(
519
                    frontend.frontend_port, "completion", use_long_prompt=True
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
557
558
559
                )

                # Poll for "Prefill Request ID" pattern in prefill worker
                request_id, prefill_log_offset = poll_for_pattern(
                    process=prefill_worker,
                    pattern="Prefill Request ID: ",
                    match_type="contains",
                )

                # Poll for decode worker entry signaling start of KV transfer phase
                _, decode_log_offset = poll_for_pattern(
                    process=decode_worker,
                    pattern=f"Decode Request ID: {request_id}",
                    poll_interval_ms=2,
                )

                # Cancel during KV transfer phase in decode worker
                cancellable_req.cancel()
                logger.info(
                    f"Cancelled request ID: {request_id} at beginning of decode"
                )

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

                logger.info(
                    "Completion request cancellation at beginning of decode detected successfully"
                )

                # Verify the workers are still functional
560
561
562
                cancellable_req = send_cancellable_request(
                    frontend.frontend_port, "chat_completion_stream"
                )
563
564
565
566
567
568
569
570
571
572
573
                _, decode_log_offset = poll_for_pattern(
                    process=decode_worker,
                    pattern="Decode Request ID: ",
                    log_offset=decode_log_offset,
                    match_type="contains",
                )
                read_streaming_responses(cancellable_req, expected_count=5)

                logger.info(
                    "Workers are functional after cancellation during KV transfer"
                )
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590

                # Verify cancellation metrics
                verify_frontend_cancellation_metrics(
                    frontend_port=frontend.frontend_port,
                    request_type="completion",
                    expected_count=1,
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=decode_worker.system_port,
                    expected_count=1,
                    component="tensorrt_llm",
                )
                verify_runtime_cancellation_metrics(
                    worker_system_port=prefill_worker.system_port,
                    expected_count=0,
                    component="prefill",
                )