test_determinism_agg.py 21.1 KB
Newer Older
1
#!/usr/bin/env python3
2
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
# SPDX-License-Identifier: Apache-2.0

"""
Determinism test for KVBM in aggregated mode.

To make sure KVBM's accuracy, this test suite checks if the model produces
deterministic outputs when same requests are served 1) without KVBM onboarded KV
blocks and 2) with KVBM onboarded KV blocks, when given the same inputs with
fixed seed and temperature=0.

The expected results should be 100% match between the two cases. Compared to
disaggregated mode, aggregated mode has less randomness chances.
15
16
17
18
19
20

These tests are slow by default (~368s and ~601s). For faster runs with
fewer iterations, run the following command (expected to finish in ~58s + ~152s):

    KVBM_MAX_ITERATIONS=2 KVBM_NUM_ITERATIONS=2 KVBM_REQUEST_DELAY=2 \
        pytest tests/kvbm_integration/test_determinism_agg.py -v --tb=short
21
22
23
24
25
26
"""

import logging
import os
import signal
import subprocess
27
28
import sys
import threading
29
import time
30
from dataclasses import dataclass
31
32
from datetime import datetime
from pathlib import Path
33
from typing import Any, Dict, List, Optional, TextIO
34
35
36
37

import pytest
import requests

38
from tests.utils.port_utils import allocate_port, deallocate_port
39
from tests.utils.test_output import resolve_test_output_path
40

41
42
from .common import DeterminismTester, ServerType
from .common import TestDeterminism as BaseTestDeterminism
43
44
45
from .common import check_module_available

HAS_VLLM_BENCH = check_module_available("vllm")
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

@dataclass
class KvbmModelConfig:
    """Describes a model and the vLLM serving flags needed for KVBM testing."""

    model_id: str
    block_size: Optional[int] = None  # None = let vllm decide
    attention_backend: Optional[str] = None  # None = let vllm decide
    max_model_len: int = 8000
    # Set False for MLA models: VLLM_BATCH_INVARIANT=1 disables prefix caching
    # for TRITON_MLA in vLLM 0.17.1, defeating KV offload testing.
    batch_invariant: bool = True

    @property
    def short_name(self) -> str:
        return self.model_id.split("/")[-1]

    @property
    def use_mla(self) -> bool:
        """True when the model uses Multi-head Latent Attention (e.g. TRITON_MLA)."""
        return self.attention_backend is not None and "MLA" in self.attention_backend


# Models exercised by this test suite.
# CI iterates over all entries; add a new entry to test an additional model.
_MODEL_CONFIGS: List[KvbmModelConfig] = [
    KvbmModelConfig(
        model_id=os.environ.get(
            "KVBM_MODEL_ID", "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
        ),
        block_size=16,
        attention_backend="FLASH_ATTN",
    ),
    KvbmModelConfig(
        model_id="deepseek-ai/DeepSeek-V2-Lite",
        # TRITON_MLA works on all devices; on H100 set KVBM_MLA_BACKEND=FLASH_ATTN_MLA
        attention_backend=os.environ.get("KVBM_MLA_BACKEND", "TRITON_MLA"),
        # VLLM_BATCH_INVARIANT=1 disables prefix caching for TRITON_MLA in vLLM 0.17.1
        batch_invariant=False,
    ),
]

89
90
91
92
93
# KVBM env vars that drive test duration (used to compute timeouts below).
_KVBM_MAX_ITERATIONS = int(os.environ.get("KVBM_MAX_ITERATIONS", "100"))
_KVBM_NUM_ITERATIONS = int(os.environ.get("KVBM_NUM_ITERATIONS", "15"))
_KVBM_REQUEST_DELAY = int(os.environ.get("KVBM_REQUEST_DELAY", "30"))

94
95
96
97
# Server startup timeout (env-configurable; larger models like DeepSeek-V2-Lite
# may need 600s+).
_SERVER_START_TIMEOUT = int(os.environ.get("KVBM_SERVER_START_TIMEOUT", "600"))

98
# Compute timeouts from the same env vars that control test duration.
99
100
101
102
103
104
# Each formula adds _SERVER_START_TIMEOUT so the pytest timeout covers both
# the server startup and the actual test body.
#
# test_determinism_agg_with_cache_reset: warmup + 2 phases of KVBM_MAX_ITERATIONS,
# each iteration ~4s (request + overhead), plus ~50s teardown.
_CACHE_RESET_TIMEOUT = _SERVER_START_TIMEOUT + 2 * (_KVBM_MAX_ITERATIONS * 4 + 50)
105
106
# test_concurrent_determinism_under_load: dominated by
# (KVBM_NUM_ITERATIONS - 1) * KVBM_REQUEST_DELAY seconds of sleep,
107
108
109
110
# plus ~150s overhead (benchmark ramp, teardown).
_CONCURRENT_TIMEOUT = _SERVER_START_TIMEOUT + 2 * (
    (_KVBM_NUM_ITERATIONS - 1) * _KVBM_REQUEST_DELAY + 150
)
111

112
113
114
115
116
117
# Test markers to align with repository conventions
# Todo: enable the rest when kvbm is built in the ci
pytestmark = [
    pytest.mark.e2e,
    pytest.mark.slow,
    pytest.mark.gpu_1,
118
    pytest.mark.nightly,
119
120
121
122
123
124
125
126
127
128
129
130
131
132
]


class LLMServerManager:
    """Manages LLM server lifecycle for determinism testing."""

    def __init__(
        self,
        base_url: Optional[str] = None,
        port: Optional[int] = None,
        cpu_cache_blocks: Optional[int] = None,
        gpu_cache_blocks: Optional[int] = None,
        log_dir: Optional[Path] = None,
        server_type: Optional[str] = ServerType.vllm,
133
        model_config: Optional[KvbmModelConfig] = None,
134
135
    ):
        self.server_type = server_type
136
        self.model_config = model_config or _MODEL_CONFIGS[0]
137
        # Use provided port, env var, or allocate a dynamic port to avoid conflicts
138
139
        if port is not None:
            self.port = port
140
            self.port_allocated = False  # Port provided by caller, don't deallocate
141
142
        elif os.environ.get("KVBM_SERVER_PORT"):
            self.port = int(os.environ["KVBM_SERVER_PORT"])
143
            self.port_allocated = False  # Port from env var, don't deallocate
144
        else:
145
146
            self.port = allocate_port(start_port=8000)
            self.port_allocated = True  # Port allocated by us, must deallocate
147
        self.base_url = base_url or f"http://localhost:{self.port}"
148
149
        self.metrics_port = allocate_port(start_port=6880)
        self.metrics_port_allocated = True
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        self.process: Optional[subprocess.Popen] = None
        self.cpu_cache_blocks = cpu_cache_blocks
        self.gpu_cache_blocks = gpu_cache_blocks

        # Prepare logging
        self.log_dir = log_dir or Path(".")
        self.log_dir.mkdir(parents=True, exist_ok=True)
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        config_str = (
            f"cpu{cpu_cache_blocks or 'default'}_gpu{gpu_cache_blocks or 'default'}"
        )
        self.server_log_file = (
            self.log_dir / f"{self.server_type}_server_{config_str}_{timestamp}.log"
        )
        self.server_stdout_file: Optional[TextIO] = None
165
        self._tee_threads: List[threading.Thread] = []
166
167
168
169
170
171
172
173
174

        # Environment for the process
        self.env = os.environ.copy()
        self.env.update(
            {
                "RUST_BACKTRACE": "1",
                # DynamoConnector connection settings
                "NATS_SERVER": "nats://localhost:4222",
                "ETCD_ENDPOINTS": "http://localhost:2379",
175
176
                # Enable KVBM metrics for monitoring offload/onboard
                "DYN_KVBM_METRICS": "true",
177
                "DYN_KVBM_METRICS_PORT": str(self.metrics_port),
178
179
180
181
182
183
184
185
            }
        )

        # CPU cache blocks override via env
        if cpu_cache_blocks is not None:
            self.env["DYN_KVBM_CPU_CACHE_OVERRIDE_NUM_BLOCKS"] = str(cpu_cache_blocks)

        if self.server_type == ServerType.vllm:
186
            self._set_up_vllm_config(gpu_cache_blocks, self.model_config)
187
188
189
190
191
192
193
        elif self.server_type == ServerType.trtllm:
            self._set_up_trtllm_config(gpu_cache_blocks)
        else:
            raise ValueError(
                f"{self.server_type} is not supported yet in the KVBM test suite"
            )

194
    def _set_up_vllm_config(self, gpu_cache_blocks, model_config: KvbmModelConfig):
195
        self.env["VLLM_SERVER_DEV_MODE"] = "1"
196
197
198
199
        if model_config.batch_invariant:
            self.env["VLLM_BATCH_INVARIANT"] = "1"
        else:
            self.env.pop("VLLM_BATCH_INVARIANT", None)
200
201
202
203
204
205
206

        self.server_cmd = [
            "vllm",
            "serve",
            "--port",
            str(self.port),
            "--kv-transfer-config",
Richard Huo's avatar
Richard Huo committed
207
            '{"kv_connector":"DynamoConnector","kv_role":"kv_both", "kv_connector_module_path": "kvbm.vllm_integration.connector"}',
208
            model_config.model_id,
Alec's avatar
Alec committed
209
            "--max-model-len",
210
            str(model_config.max_model_len),
211
212
        ]

213
214
215
216
217
218
219
220
        if model_config.block_size is not None:
            self.server_cmd.extend(["--block-size", str(model_config.block_size)])

        if model_config.attention_backend is not None:
            self.server_cmd.extend(
                ["--attention-config.backend", model_config.attention_backend]
            )

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        if gpu_cache_blocks is not None:
            self.server_cmd.extend(["--num-gpu-blocks-override", str(gpu_cache_blocks)])

    def _set_up_trtllm_config(self, gpu_cache_blocks):
        config_path = os.environ.get(
            "KVBM_TRTLLM_LLMAPI_CONFIG_PATH", "/tmp/kvbm_llm_api_config.yaml"
        )
        llm_api_config: Dict[str, Any] = {}
        llm_api_config[
            "cuda_graph_config"
        ] = None  # explicitly disable CUDA graph since Connector API doesn't support CUDA graph yet in TRTLLM
        llm_api_config["kv_cache_config"] = {
            "enable_partial_reuse": False,
            "free_gpu_memory_fraction": 0.10,  # Set a small GPU fraction so that we can evict/reset the on-device kv cache faster
        }
        llm_api_config["kv_connector_config"] = {
Richard Huo's avatar
Richard Huo committed
237
            "connector_module": "kvbm.trtllm_integration.connector",
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
            "connector_scheduler_class": "DynamoKVBMConnectorLeader",
            "connector_worker_class": "DynamoKVBMConnectorWorker",
        }

        # GPU blocks override
        if gpu_cache_blocks is not None:
            del llm_api_config["kv_cache_config"]["free_gpu_memory_fraction"]
            llm_api_config["kv_cache_config"]["max_tokens"] = (
                int(gpu_cache_blocks) * 32
            )  # TRTLLM defaults 32 tokens per block

        # Construct serve command
        self.server_cmd = [
            "trtllm-serve",
            os.environ.get("KVBM_MODEL_ID", "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"),
            "--host",
            "localhost",
            "--port",
            str(self.port),
            "--backend",
            "pytorch",
            "--extra_llm_api_options",
            config_path,
        ]

        import yaml

        with open(config_path, "w") as f:
            yaml.dump(llm_api_config, f, default_flow_style=False, sort_keys=False)

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    def _tee_output(self, pipe: Any, log_file: TextIO, prefix: str) -> None:
        """Read from pipe and write to both log file and stdout (tee)."""
        try:
            for line in iter(pipe.readline, ""):
                if not line:
                    break
                # Write to log file
                log_file.write(line)
                log_file.flush()
                # Write to stdout with prefix
                sys.stdout.write(f"[{prefix}] {line}")
                sys.stdout.flush()
        except (ValueError, OSError):
            pass  # Pipe closed
        finally:
            pipe.close()

285
286
287
288
289
290
    def start_server(self, timeout: int = 300) -> bool:
        """Start LLM server and wait for readiness."""
        if self.is_server_running():
            self.stop_server()
            time.sleep(2)

291
292
293
294
295
296
297
298
        # Open log file (combined stdout+stderr)
        self.server_stdout_file = open(self.server_log_file.with_suffix(".log"), "w")

        # Write header
        header = f"=== {self.server_type} Server Started at {datetime.now()} ===\nCommand: {' '.join(self.server_cmd)}\n"
        self.server_stdout_file.write(header)
        self.server_stdout_file.flush()
        print(f"[{self.server_type}] {header}", end="")
299

300
        # Launch with pipe, redirect stderr to stdout
301
302
        self.process = subprocess.Popen(
            self.server_cmd,
303
304
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,  # Redirect stderr to stdout
305
306
            env=self.env,
            preexec_fn=os.setsid,
307
308
            text=True,
            bufsize=1,  # Line buffered
309
310
        )

311
312
313
314
315
316
317
318
319
320
321
        # Start tee thread for combined output
        self._tee_threads = [
            threading.Thread(
                target=self._tee_output,
                args=(self.process.stdout, self.server_stdout_file, self.server_type),
                daemon=True,
            ),
        ]
        for t in self._tee_threads:
            t.start()

322
323
324
325
        # Wait for health
        start_time = time.time()
        while time.time() - start_time < timeout:
            if self.is_server_running():
326
327
328
329
330
331
332
333
334
335
                # Verify metrics endpoint is reachable (fail fast on wrong port)
                try:
                    requests.get(
                        f"http://localhost:{self.metrics_port}/metrics", timeout=5
                    )
                    return True
                except requests.exceptions.RequestException:
                    print(
                        f"Warning: server healthy but metrics port {self.metrics_port} not reachable yet"
                    )
336
            if self.process.poll() is not None:
337
338
339
                # Process exited, wait for tee thread to finish
                for t in self._tee_threads:
                    t.join(timeout=2)
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
                self._close_log_files()
                return False
            time.sleep(5)

        # Timeout
        self.stop_server()
        return False

    def stop_server(self):
        """Stop LLM server and close logs."""
        if self.process:
            try:
                os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
                try:
                    self.process.wait(timeout=30)
                except subprocess.TimeoutExpired:
                    os.killpg(os.getpgid(self.process.pid), signal.SIGKILL)
                    self.process.wait()
            except (ProcessLookupError, OSError):
                pass
            finally:
                self.process = None
362
363
364
365
        # Wait for tee threads to finish
        for t in self._tee_threads:
            t.join(timeout=2)
        self._tee_threads = []
366
367
        self._close_log_files()

368
        # Deallocate ports if we allocated them
369
370
371
        if self.port_allocated:
            deallocate_port(self.port)
            self.port_allocated = False
372
373
374
        if self.metrics_port_allocated:
            deallocate_port(self.metrics_port)
            self.metrics_port_allocated = False
375

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    def _close_log_files(self):
        if self.server_stdout_file:
            self.server_stdout_file.write(
                f"\n=== Server Stopped at {datetime.now()} ===\n"
            )
            self.server_stdout_file.close()
            self.server_stdout_file = None

    def is_server_running(self) -> bool:
        try:
            # First check basic health
            response = requests.get(f"{self.base_url}/health", timeout=5)
            if response.status_code != 200:
                return False

            # Then check if the model endpoint is ready with a simple test request
            test_payload = {
393
                "model": self.model_config.model_id,
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
                "messages": [{"role": "user", "content": "test"}],
                "max_completion_tokens": 1,
                "temperature": 0,
            }

            response = requests.post(
                f"{self.base_url}/v1/chat/completions",
                headers={"Content-Type": "application/json"},
                json=test_payload,
                timeout=10,
            )
            return response.status_code == 200

        except requests.exceptions.RequestException:
            return False


class AggDeterminismTester(DeterminismTester):
    """Aggregated architecture specific determinism tester."""

    def __init__(
        self,
        base_url: Optional[str] = None,
        model_id: Optional[str] = None,
        server_type: Optional[str] = ServerType.vllm,
    ):
        super().__init__(base_url, model_id, server_type)

    def reset_prefix_cache(self):
        """Reset the prefix cache."""
        print("Resetting prefix cache...")
        if self.server_type == ServerType.trtllm:
            # TRTLLM doesn't support reset_prefix_cache endpoint API
            # 300 shakespeare content could evict the 0.1 x 80G (~1700 blocks) on-device cache
            shakespeare_count = 300
            for seq_idx in range(1, shakespeare_count + 1):
                start_word = (seq_idx - 1) * self.word_count
                content = self.get_shakespeare_content(start_word)

                if content:
                    print(
                        f"Resetting Shakespeare sequence {seq_idx} (words {start_word}-{start_word + self.word_count - 1})..."
                    )
                    try:
                        self.make_request(content)
                    except Exception as e:
                        print(f"Resetting request failed: {e}")
        else:
            response = requests.post(
                f"{self.base_url}/reset_prefix_cache",
                timeout=int(os.environ.get("KVBM_HTTP_TIMEOUT", "30")),
            )
            response.raise_for_status()
        print("Cache reset done")


@pytest.fixture(scope="function")
def llm_server(request, runtime_services):
    """Start and stop a LLM server for each test with optional cache block overrides.

    To parametrize, use:
      @pytest.mark.parametrize("llm_server", [{"cpu_blocks": 10000, "gpu_blocks": 2048}], indirect=True)
    """
    logger = logging.getLogger("pytest")
    logger.setLevel(logging.INFO)

    cpu_blocks = getattr(request, "param", {}).get("cpu_blocks", None)
    gpu_blocks = getattr(request, "param", {}).get("gpu_blocks", None)
    port = getattr(request, "param", {}).get("port", None)
463
    model_config = getattr(request, "param", {}).get("model_config", None)
464
465

    # Put logs in the per-test directory set up by tests/conftest.py
466
    log_dir = Path(resolve_test_output_path(request.node.name))
467

468
    if check_module_available("vllm"):
469
        server_type = ServerType.vllm
470
    elif check_module_available("tensorrt_llm"):
471
472
473
474
475
476
477
478
479
480
481
482
        server_type = ServerType.trtllm
    else:
        raise Exception(
            "Neither the vllm nor the tensorrt_llm module is available in the current environment."
        )

    server_manager = LLMServerManager(
        port=port,
        cpu_cache_blocks=cpu_blocks,
        gpu_cache_blocks=gpu_blocks,
        log_dir=log_dir,
        server_type=server_type,
483
        model_config=model_config,
484
485
    )

486
    start_timeout = _SERVER_START_TIMEOUT
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    if not server_manager.start_server(timeout=start_timeout):
        pytest.fail(
            f"Failed to start {server_type} server (cpu_blocks={cpu_blocks}, gpu_blocks={gpu_blocks}, port={server_manager.port})"
        )

    yield server_manager

    server_manager.stop_server()


@pytest.fixture(scope="function")
def tester(llm_server):
    """Create determinism tester bound to the running server's base URL."""
    t = AggDeterminismTester(
501
        base_url=llm_server.base_url,
502
        model_id=llm_server.model_config.model_id,
503
        server_type=llm_server.server_type,
504
505
506
507
508
509
510
511
512
513
514
    )
    t.download_shakespeare_text()
    return t


class TestDeterminismAgg(BaseTestDeterminism):
    """Test class for determinism validation."""

    @pytest.mark.parametrize(
        "llm_server",
        [
515
516
517
            {
                "cpu_blocks": int(os.environ.get("KVBM_CPU_BLOCKS", "10000")),
                "gpu_blocks": int(os.environ.get("KVBM_GPU_BLOCKS", "2048")),
518
519
520
                "model_config": cfg,
            }
            for cfg in _MODEL_CONFIGS
521
522
        ],
        indirect=True,
523
        ids=[cfg.short_name for cfg in _MODEL_CONFIGS],
524
    )
525
    @pytest.mark.kvbm
526
527
528
    @pytest.mark.timeout(
        _CACHE_RESET_TIMEOUT
    )  # ~368s actual measured on 32-core machine
529
530
531
532
533
534
535
536
537
538
539
540
    def test_determinism_agg_with_cache_reset(
        self, tester, llm_server, runtime_services
    ):
        """Test determinism across cache reset: run test with warmup, reset cache, run again without warmup."""
        # Call the base class implementation
        super().base_test_determinism_with_cache_reset(
            tester, llm_server, runtime_services
        )

    @pytest.mark.parametrize(
        "llm_server",
        [
541
542
543
            {
                "cpu_blocks": int(os.environ.get("KVBM_CPU_BLOCKS", "30000")),
                "gpu_blocks": int(os.environ.get("KVBM_GPU_BLOCKS", "2048")),
544
545
546
                "model_config": cfg,
            }
            for cfg in _MODEL_CONFIGS
547
548
        ],
        indirect=True,
549
        ids=[cfg.short_name for cfg in _MODEL_CONFIGS],
550
    )
551
552
553
    @pytest.mark.kvbm_concurrency
    @pytest.mark.skipif(
        not HAS_VLLM_BENCH, reason="requires vllm bench (vllm module not found)"
554
    )
555
556
557
    @pytest.mark.timeout(
        _CONCURRENT_TIMEOUT
    )  # ~601s actual measured on 32-core machine
558
559
    def test_concurrent_determinism_under_load(
        self, tester, llm_server, runtime_services
560
    ):
561
        """Test Spanish prompt determinism under high concurrency load.
562

563
564
565
566
567
568
        Reproduces the bug where Spanish responses become English or corrupted.
        """
        # Get the Spanish prompt path relative to this test file
        spanish_prompt_path = Path(
            os.path.join(os.path.dirname(__file__), "es_prompt.txt")
        ).absolute()
569

570
571
572
        # Call the base class implementation
        super().base_test_spanish_prompt_determinism_under_load(
            tester, llm_server, runtime_services, spanish_prompt_path
573
574
575
576
577
578
        )


if __name__ == "__main__":
    # Allow running as script
    pytest.main([__file__, "-v", "-s"])