|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import os |
| 4 | +from itertools import repeat |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch._dynamo.config as dynamo_config |
| 9 | +from vllm import SamplingParams |
| 10 | +from vllm.v1.metrics.reader import Metric |
| 11 | + |
| 12 | +from tests.e2e.conftest import VllmRunner |
| 13 | +from tests.e2e.model_utils import check_outputs_equal |
| 14 | + |
| 15 | +MODEL = "Qwen/Qwen3-0.6B" |
| 16 | + |
| 17 | +first_prompt = ("The following numbers of the sequence " + |
| 18 | + ", ".join(str(i) for i in range(10)) + " are:") |
| 19 | +example_prompts = [first_prompt, "In one word, the capital of France is " |
| 20 | + ] + [f"Tell me about the number {i}: " for i in range(32)] |
| 21 | + |
| 22 | +default_params = dict( |
| 23 | + temperature=0.0, # greedy |
| 24 | + max_tokens=23, |
| 25 | + min_tokens=18, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def test_without_spec_decoding(monkeypatch: pytest.MonkeyPatch, ): |
| 30 | + """Test consistency of combos of async scheduling, preemption, |
| 31 | + uni/multiproc executor, prefill chunking.""" |
| 32 | + test_sampling_params: list[dict[str, Any]] = [ |
| 33 | + dict(), |
| 34 | + ] |
| 35 | + |
| 36 | + # test_preemption, executor, async_scheduling, |
| 37 | + # spec_config, test_prefill_chunking |
| 38 | + test_configs = [ |
| 39 | + (False, "mp", False, None, False), |
| 40 | + (False, "mp", True, None, False), |
| 41 | + (False, "uni", True, None, False), |
| 42 | + ] |
| 43 | + |
| 44 | + run_tests(monkeypatch, MODEL, test_configs, test_sampling_params) |
| 45 | + |
| 46 | + |
| 47 | +@dynamo_config.patch(cache_size_limit=16) |
| 48 | +def run_tests( |
| 49 | + monkeypatch: pytest.MonkeyPatch, |
| 50 | + model: str, |
| 51 | + test_configs: list[tuple], |
| 52 | + test_sampling_params: list[dict[str, Any]], |
| 53 | +): |
| 54 | + """Test consistency of combos of async scheduling, preemption, |
| 55 | + uni/multiproc executor with spec decoding.""" |
| 56 | + |
| 57 | + with monkeypatch.context(): |
| 58 | + # avoid precision errors |
| 59 | + outputs: list[tuple[str, list, list]] = [] |
| 60 | + for n, ( |
| 61 | + test_preemption, |
| 62 | + executor, |
| 63 | + async_scheduling, |
| 64 | + spec_config, |
| 65 | + test_prefill_chunking, |
| 66 | + ) in enumerate(test_configs, 1): |
| 67 | + test_str = f"{n}/{len(test_configs)}" |
| 68 | + test_results = run_test( |
| 69 | + model, |
| 70 | + test_str, |
| 71 | + test_sampling_params, |
| 72 | + test_preemption, |
| 73 | + executor, |
| 74 | + async_scheduling, |
| 75 | + spec_config, |
| 76 | + test_prefill_chunking=test_prefill_chunking, |
| 77 | + ) |
| 78 | + outputs.append(test_results) |
| 79 | + |
| 80 | + baseline_config, baseline_tests, _ = outputs[0] |
| 81 | + _, _, baseline_acceptances = next((o for o in outputs if o[2] is not None), |
| 82 | + (None, None, None)) |
| 83 | + |
| 84 | + print( |
| 85 | + f"BASELINE: config=[{baseline_config}], accept_rates={baseline_acceptances}" |
| 86 | + ) |
| 87 | + |
| 88 | + failure = None |
| 89 | + for test_config, test_outputs, test_acceptance_rates in outputs[1:]: |
| 90 | + for base_outs, base_acceptance_rate, test_outs, test_acceptance_rate, params in zip( |
| 91 | + baseline_tests, |
| 92 | + baseline_acceptances or repeat(None), |
| 93 | + test_outputs, |
| 94 | + test_acceptance_rates or repeat(None), |
| 95 | + test_sampling_params, |
| 96 | + ): |
| 97 | + try: |
| 98 | + check_outputs_equal( |
| 99 | + outputs_0_lst=base_outs, |
| 100 | + outputs_1_lst=test_outs, |
| 101 | + name_0=f"baseline=[{baseline_config}], params={params}", |
| 102 | + name_1=f"config=[{test_config}], params={params}", |
| 103 | + ) |
| 104 | + |
| 105 | + if (base_acceptance_rate is not None |
| 106 | + and test_acceptance_rate is not None): |
| 107 | + if "spec_mml=None" in test_config: |
| 108 | + assert (test_acceptance_rate > base_acceptance_rate |
| 109 | + or test_acceptance_rate == pytest.approx( |
| 110 | + base_acceptance_rate, rel=5e-2)) |
| 111 | + else: |
| 112 | + # Currently the reported acceptance rate is expected to be |
| 113 | + # lower when we sometimes skip drafting altogether. |
| 114 | + assert test_acceptance_rate > 0.1 |
| 115 | + print(f"PASSED: config=[{test_config}], params={params}" |
| 116 | + f" accept_rate={test_acceptance_rate}") |
| 117 | + except AssertionError as e: |
| 118 | + print(f"FAILED: config=[{test_config}], params={params}" |
| 119 | + f" accept_rate={test_acceptance_rate}") |
| 120 | + if failure is None: |
| 121 | + failure = e |
| 122 | + |
| 123 | + if failure is not None: |
| 124 | + raise failure |
| 125 | + |
| 126 | + |
| 127 | +def run_test( |
| 128 | + model: str, |
| 129 | + test_str: str, |
| 130 | + sampling_param_tests: list[dict[str, Any]], |
| 131 | + test_preemption: bool, |
| 132 | + executor: str, |
| 133 | + async_scheduling: bool, |
| 134 | + spec_config: dict[str, Any] | None, |
| 135 | + test_prefill_chunking: bool, |
| 136 | +): |
| 137 | + os.environ['VLLM_WORKER_MULTIPROC_METHOD'] = 'spawn' |
| 138 | + spec_decoding = spec_config is not None |
| 139 | + cache_arg: dict[str, Any] = ( |
| 140 | + # Force preemptions |
| 141 | + dict(num_gpu_blocks_override=2) if test_preemption else dict( |
| 142 | + gpu_memory_utilization=0.9)) |
| 143 | + spec_mml = (spec_config or {}).get("max_model_len") |
| 144 | + test_config = (f"executor={executor}, preemption={test_preemption}, " |
| 145 | + f"async_sched={async_scheduling}, " |
| 146 | + f"chunk_prefill={test_prefill_chunking}, " |
| 147 | + f"spec_decoding={spec_decoding}, spec_mml={spec_mml}") |
| 148 | + print("-" * 80) |
| 149 | + print(f"---- TESTING {test_str}: {test_config}") |
| 150 | + print("-" * 80) |
| 151 | + with VllmRunner( |
| 152 | + model, |
| 153 | + max_model_len=512, |
| 154 | + enable_chunked_prefill=test_prefill_chunking, |
| 155 | + # Force prefill chunking |
| 156 | + max_num_batched_tokens=48 if test_prefill_chunking else None, |
| 157 | + enforce_eager=True, |
| 158 | + async_scheduling=async_scheduling, |
| 159 | + distributed_executor_backend=executor, |
| 160 | + dtype="float16", # avoid precision errors |
| 161 | + speculative_config=spec_config, |
| 162 | + disable_log_stats=False, |
| 163 | + **cache_arg, |
| 164 | + ) as vllm_model: |
| 165 | + results = [] |
| 166 | + acceptance_rates: list[float] | None = [] if spec_decoding else None |
| 167 | + for override_params in sampling_param_tests: |
| 168 | + metrics_before = vllm_model.model.get_metrics() |
| 169 | + print(f"----------- RUNNING PARAMS: {override_params}") |
| 170 | + results.append( |
| 171 | + vllm_model.generate( |
| 172 | + example_prompts, |
| 173 | + sampling_params=SamplingParams(**default_params, |
| 174 | + **override_params), |
| 175 | + )) |
| 176 | + metrics_after = vllm_model.model.get_metrics() |
| 177 | + if acceptance_rates is not None: |
| 178 | + acceptance_rate = _get_acceptance_rate(metrics_before, |
| 179 | + metrics_after) |
| 180 | + acceptance_rates.append(acceptance_rate) |
| 181 | + print(f"ACCEPTANCE RATE {acceptance_rate}") |
| 182 | + |
| 183 | + if test_preemption: |
| 184 | + preemptions = _get_count(metrics_before, metrics_after, |
| 185 | + "vllm:num_preemptions") |
| 186 | + assert preemptions > 0, "preemption test had no preemptions" |
| 187 | + |
| 188 | + if len(results) > 1: |
| 189 | + # First check that the different parameter configs |
| 190 | + # actually result in different output. |
| 191 | + for other_test_outs, params in zip(results[1:], |
| 192 | + sampling_param_tests[1:]): |
| 193 | + with pytest.raises(AssertionError): |
| 194 | + check_outputs_equal( |
| 195 | + outputs_0_lst=results[0][0], |
| 196 | + outputs_1_lst=other_test_outs, |
| 197 | + name_0=f"baseline params={params}", |
| 198 | + name_1=f"other params={params}", |
| 199 | + ) |
| 200 | + |
| 201 | + return test_config, results, acceptance_rates |
| 202 | + |
| 203 | + |
| 204 | +def _get_acceptance_rate(before: list[Metric], after: list[Metric]) -> float: |
| 205 | + draft = _get_count(before, after, "vllm:spec_decode_num_draft_tokens") |
| 206 | + accept = _get_count(before, after, "vllm:spec_decode_num_accepted_tokens") |
| 207 | + return accept / draft if draft > 0 else 0.0 |
| 208 | + |
| 209 | + |
| 210 | +def _get_count(before: list[Metric], after: list[Metric], name: str) -> int: |
| 211 | + before_val = next(m.value for m in before if m.name == name) |
| 212 | + after_val = next(m.value for m in after if m.name == name) |
| 213 | + return after_val - before_val |
0 commit comments