| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import time |
| 5 | from types import SimpleNamespace |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from vllm import SamplingParams |
| 11 | from vllm.config import VllmConfig |
| 12 | from vllm.exceptions import VLLMValidationError |
| 13 | from vllm.v1.core.sched.utils import check_stop |
| 14 | from vllm.v1.engine.input_processor import InputProcessor |
| 15 | from vllm.v1.request import Request, RequestStatus |
| 16 | |
| 17 | pytestmark = pytest.mark.skip_global_cleanup |
| 18 | |
| 19 | |
| 20 | def _make_request(params: SamplingParams) -> Request: |
| 21 | return Request( |
| 22 | request_id="req-0", |
| 23 | client_index=0, |
| 24 | prompt_token_ids=[1, 2, 3], |
| 25 | sampling_params=params, |
| 26 | pooling_params=None, |
| 27 | arrival_time=time.time(), |
| 28 | ) |
| 29 | |
| 30 | |
| 31 | def _normalize(params: SamplingParams, prompt_len: int, max_model_len: int) -> None: |
| 32 | processor = SimpleNamespace( |
| 33 | model_config=SimpleNamespace(max_model_len=max_model_len) |
| 34 | ) |
| 35 | InputProcessor._normalize_trace_replay_params(processor, params, prompt_len) |
| 36 | |
| 37 | |
| 38 | def test_normalize_trace_replay_params(): |
| 39 | params = SamplingParams( |
| 40 | max_tokens=16, |
| 41 | min_tokens=10, |
| 42 | stop_token_ids=[20], |
| 43 | trace_decode_token_ids=[10, 20, 30], |
| 44 | ) |
| 45 | params.update_from_generation_config({}, eos_token_id=20) |
| 46 | |
| 47 | _normalize(params, prompt_len=3, max_model_len=128) |
| 48 | |
| 49 | assert params.max_tokens == 3 |
| 50 | assert params.min_tokens == 0 |
| 51 | assert params.ignore_eos is True |
| 52 | assert params.eos_token_id is None |
| 53 | assert params.stop_token_ids == [] |
| 54 | assert params.all_stop_token_ids == set() |
| 55 | |
| 56 | request = _make_request(params) |
| 57 | assert request.max_tokens == 3 |
| 58 | request.append_output_token_ids([10, 20, 30]) |
| 59 | assert check_stop(request, max_model_len=128) |
| 60 | assert request.status == RequestStatus.FINISHED_LENGTH_CAPPED |
| 61 | |
| 62 | |
| 63 | def test_trace_longer_than_remaining_context_is_truncated(): |
| 64 | """The trace is staged into a max_model_len-wide row, so it must be cut. |
| 65 | |
| 66 | An explicitly set max_tokens is never clamped to max_model_len, so it cannot |
| 67 | bound the staged write. |
| 68 | """ |
| 69 | params = SamplingParams(max_tokens=100, trace_decode_token_ids=list(range(20))) |
| 70 | |
| 71 | _normalize(params, prompt_len=6, max_model_len=10) |
| 72 | |
| 73 | assert params.trace_decode_token_ids == [0, 1, 2, 3] |
| 74 | assert params.max_tokens == 4 |
| 75 | |
| 76 | |
| 77 | def _validate(enable_trace_replay: bool) -> None: |
| 78 | """Run _validate_params' trace gating with the rest of verify() stubbed.""" |
| 79 | processor = SimpleNamespace( |
| 80 | model_config=SimpleNamespace( |
| 81 | return_sampling_mask=False, |
| 82 | enable_trace_replay=enable_trace_replay, |
| 83 | is_diffusion=False, |
| 84 | ), |
| 85 | vllm_config=SimpleNamespace(reasoning_config=None), |
| 86 | speculative_config=None, |
| 87 | structured_outputs_config=None, |
| 88 | diffusion_config=None, |
| 89 | tokenizer=None, |
| 90 | validate_logits_processors_params=lambda params: None, |
| 91 | ) |
| 92 | params = SamplingParams(trace_decode_token_ids=[1, 2, 3]) |
| 93 | with patch.object(SamplingParams, "verify"): |
| 94 | InputProcessor._validate_params(processor, params, ("generate",)) |
| 95 | |
| 96 | |
| 97 | def test_trace_request_rejected_when_feature_disabled(): |
| 98 | with pytest.raises(VLLMValidationError, match="--enable-trace-replay"): |
| 99 | _validate(enable_trace_replay=False) |
| 100 | |
| 101 | |
| 102 | def test_trace_request_accepted_when_feature_enabled(): |
| 103 | _validate(enable_trace_replay=True) |
| 104 | |
| 105 | |
| 106 | def test_trace_replay_requires_v2_model_runner(): |
| 107 | config = SimpleNamespace( |
| 108 | model_config=SimpleNamespace(enable_trace_replay=True), |
| 109 | use_v2_model_runner=False, |
| 110 | ) |
| 111 | |
| 112 | with pytest.raises(ValueError, match="trace replay requires Model Runner V2"): |
| 113 | VllmConfig._verify_trace_replay_config(config) |