|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | + |
| 8 | +from tests.v1.worker.test_gpu_model_runner import _schedule_new_request |
| 9 | +from vllm.config import VllmConfig |
| 10 | +from vllm.distributed import (cleanup_dist_env_and_memory, |
| 11 | + init_distributed_environment, |
| 12 | + initialize_model_parallel) |
| 13 | +from vllm.engine.arg_utils import EngineArgs |
| 14 | +from vllm.v1.core.sched.output import CachedRequestData, SchedulerOutput |
| 15 | +from vllm.v1.engine.core import get_kv_cache_config |
| 16 | +from vllm.v1.worker.gpu_model_runner import GPUModelRunner |
| 17 | + |
| 18 | +model_dir = "meta-llama/Llama-3.1-8B-Instruct" |
| 19 | +eagle_dir = "yuhuili/EAGLE-LLaMA3.1-Instruct-8B" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def should_do_global_cleanup_after_test(request) -> bool: |
| 24 | + # So we can share the DraftModelProposer between tests |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="class") |
| 29 | +def monkeyclass(): |
| 30 | + with pytest.MonkeyPatch.context() as mp: |
| 31 | + yield mp |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="class") |
| 35 | +def spec_decode_vllm_config_and_env_setup(monkeyclass: pytest.MonkeyPatch): |
| 36 | + with monkeyclass.context() as m: |
| 37 | + m.setenv("VLLM_USE_V1", "1") |
| 38 | + vllm_config = EngineArgs(model=model_dir, |
| 39 | + max_model_len=256, |
| 40 | + cuda_graph_sizes=[1, 2, 4], |
| 41 | + gpu_memory_utilization=0.8, |
| 42 | + speculative_config={ |
| 43 | + "model": eagle_dir, |
| 44 | + "method": "eagle", |
| 45 | + "num_speculative_tokens": 2, |
| 46 | + }).create_engine_config() |
| 47 | + temp_file = tempfile.mkstemp()[1] |
| 48 | + init_distributed_environment( |
| 49 | + world_size=1, |
| 50 | + rank=0, |
| 51 | + distributed_init_method=f"file://{temp_file}", |
| 52 | + local_rank=0, |
| 53 | + backend="nccl", |
| 54 | + ) |
| 55 | + initialize_model_parallel(1, 1) |
| 56 | + yield vllm_config |
| 57 | + cleanup_dist_env_and_memory() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture(scope="class") |
| 61 | +def mock_spec_decode_model_runner( |
| 62 | + spec_decode_vllm_config_and_env_setup: VllmConfig): |
| 63 | + model_runner = GPUModelRunner(spec_decode_vllm_config_and_env_setup, |
| 64 | + torch.device("cuda")) |
| 65 | + model_runner.load_model() |
| 66 | + kv_cache_spec = model_runner.get_kv_cache_spec() |
| 67 | + |
| 68 | + kv_cache_config = get_kv_cache_config( |
| 69 | + spec_decode_vllm_config_and_env_setup, kv_cache_spec, 1024**3) # 1GB |
| 70 | + model_runner.initialize_kv_cache(kv_cache_config) |
| 71 | + yield model_runner |
| 72 | + |
| 73 | + |
| 74 | +class TestSpecDecodeScheduling: |
| 75 | + |
| 76 | + def test_spec_decode_partial_scheduling( |
| 77 | + self, mock_spec_decode_model_runner: GPUModelRunner): |
| 78 | + """Make sure we don't crash when the scheduler schedules only a subset |
| 79 | + of the requests. |
| 80 | +
|
| 81 | + Four iterations: |
| 82 | + 1. Schedule both req1 (w/ 0 draft) and req2 (w/ 0 draft) |
| 83 | + 2. Schedule only req1 (w/ 1 draft) |
| 84 | + 3. Schedule both req1 (w/ 1 draft) and req2 (w/ 2 draft) |
| 85 | + 4. Terminate req1 and req2 |
| 86 | + """ |
| 87 | + # Schedule both req1 and req2 on the first iteration |
| 88 | + scheduler_output = _schedule_new_request("req1", "req2") |
| 89 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 90 | + |
| 91 | + # Only schedule req1 on the second iteration |
| 92 | + cached_req_data = CachedRequestData( |
| 93 | + req_ids=["req1"], |
| 94 | + resumed_from_preemption=[False], |
| 95 | + new_token_ids=[[3]], |
| 96 | + new_block_ids=[([], )], |
| 97 | + num_computed_tokens=[3], |
| 98 | + ) |
| 99 | + scheduler_output = SchedulerOutput( |
| 100 | + scheduled_new_reqs=[], |
| 101 | + scheduled_cached_reqs=cached_req_data, |
| 102 | + num_scheduled_tokens={"req1": 2}, |
| 103 | + total_num_scheduled_tokens=2, |
| 104 | + scheduled_spec_decode_tokens={"req1": [1001]}, |
| 105 | + scheduled_encoder_inputs={}, |
| 106 | + num_common_prefix_blocks=[0], |
| 107 | + finished_req_ids=set(), |
| 108 | + free_encoder_input_ids=[], |
| 109 | + structured_output_request_ids={}, |
| 110 | + grammar_bitmask=None, |
| 111 | + ) |
| 112 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 113 | + |
| 114 | + # Schedule both req1 and req2 on the third iteration |
| 115 | + cached_req_data = CachedRequestData( |
| 116 | + req_ids=["req1", "req2"], |
| 117 | + resumed_from_preemption=[False, False], |
| 118 | + new_token_ids=[[10], [11]], |
| 119 | + new_block_ids=[([], ), ([], )], |
| 120 | + num_computed_tokens=[4, 3], |
| 121 | + ) |
| 122 | + scheduler_output = SchedulerOutput( |
| 123 | + scheduled_new_reqs=[], |
| 124 | + scheduled_cached_reqs=cached_req_data, |
| 125 | + num_scheduled_tokens={ |
| 126 | + "req1": 2, |
| 127 | + "req2": 3 |
| 128 | + }, |
| 129 | + total_num_scheduled_tokens=5, |
| 130 | + scheduled_spec_decode_tokens={ |
| 131 | + "req1": [1001], |
| 132 | + "req2": [2001, 2002] |
| 133 | + }, |
| 134 | + scheduled_encoder_inputs={}, |
| 135 | + num_common_prefix_blocks=[0], |
| 136 | + finished_req_ids=set(), |
| 137 | + free_encoder_input_ids=[], |
| 138 | + structured_output_request_ids={}, |
| 139 | + grammar_bitmask=None, |
| 140 | + ) |
| 141 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 142 | + |
| 143 | + # Terminate both req1 and req2 |
| 144 | + cached_req_data = CachedRequestData( |
| 145 | + req_ids=[], |
| 146 | + resumed_from_preemption=[], |
| 147 | + new_token_ids=[], |
| 148 | + new_block_ids=[], |
| 149 | + num_computed_tokens=[], |
| 150 | + ) |
| 151 | + scheduler_output = SchedulerOutput( |
| 152 | + scheduled_new_reqs=[], |
| 153 | + scheduled_cached_reqs=cached_req_data, |
| 154 | + num_scheduled_tokens={}, |
| 155 | + total_num_scheduled_tokens=0, |
| 156 | + scheduled_spec_decode_tokens={}, |
| 157 | + scheduled_encoder_inputs={}, |
| 158 | + num_common_prefix_blocks=[0], |
| 159 | + finished_req_ids={"req1", "req2"}, |
| 160 | + free_encoder_input_ids=[], |
| 161 | + structured_output_request_ids={}, |
| 162 | + grammar_bitmask=None, |
| 163 | + ) |
| 164 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 165 | + |
| 166 | + def test_spec_decode_preemption_scheduling( |
| 167 | + self, mock_spec_decode_model_runner: GPUModelRunner): |
| 168 | + """Make sure we don't crash when the scheduler preempts a request. |
| 169 | +
|
| 170 | + Four iterations: |
| 171 | + 1. Schedule req1 (w/ 0 draft) and req2 (w/ 0 draft) |
| 172 | + 2. Schedule req1 (w/ 1 draft) and preempt req2 |
| 173 | + 3. Schedule req1 (w/ 1 draft) and resume req2 (w/ 2 draft) |
| 174 | + 4. Terminate req1 and req2 |
| 175 | + """ |
| 176 | + # Schedule both req1 and req2 on the first iteration |
| 177 | + scheduler_output = _schedule_new_request("req1", "req2") |
| 178 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 179 | + |
| 180 | + # Only schedule req1 on the second iteration |
| 181 | + cached_req_data = CachedRequestData( |
| 182 | + req_ids=["req1"], |
| 183 | + resumed_from_preemption=[False], |
| 184 | + new_token_ids=[[3]], |
| 185 | + new_block_ids=[([], )], |
| 186 | + num_computed_tokens=[3], |
| 187 | + ) |
| 188 | + scheduler_output = SchedulerOutput( |
| 189 | + scheduled_new_reqs=[], |
| 190 | + scheduled_cached_reqs=cached_req_data, |
| 191 | + num_scheduled_tokens={"req1": 2}, |
| 192 | + total_num_scheduled_tokens=2, |
| 193 | + scheduled_spec_decode_tokens={"req1": [1001]}, |
| 194 | + scheduled_encoder_inputs={}, |
| 195 | + num_common_prefix_blocks=[0], |
| 196 | + finished_req_ids=set(), |
| 197 | + free_encoder_input_ids=[], |
| 198 | + structured_output_request_ids={}, |
| 199 | + grammar_bitmask=None, |
| 200 | + ) |
| 201 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 202 | + |
| 203 | + # Schedule both req1 and req2 on the third iteration |
| 204 | + cached_req_data = CachedRequestData( |
| 205 | + req_ids=["req1", "req2"], |
| 206 | + resumed_from_preemption=[False, True], |
| 207 | + new_token_ids=[[10], [11]], |
| 208 | + new_block_ids=[([], ), ([0], )], |
| 209 | + num_computed_tokens=[4, 0], |
| 210 | + ) |
| 211 | + scheduler_output = SchedulerOutput( |
| 212 | + scheduled_new_reqs=[], |
| 213 | + scheduled_cached_reqs=cached_req_data, |
| 214 | + num_scheduled_tokens={ |
| 215 | + "req1": 2, |
| 216 | + "req2": 6 |
| 217 | + }, |
| 218 | + total_num_scheduled_tokens=8, |
| 219 | + scheduled_spec_decode_tokens={ |
| 220 | + "req1": [1001], |
| 221 | + "req2": [2001, 2002] |
| 222 | + }, |
| 223 | + scheduled_encoder_inputs={}, |
| 224 | + num_common_prefix_blocks=[0], |
| 225 | + finished_req_ids=set(), |
| 226 | + free_encoder_input_ids=[], |
| 227 | + structured_output_request_ids={}, |
| 228 | + grammar_bitmask=None, |
| 229 | + ) |
| 230 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
| 231 | + |
| 232 | + # Terminate both req1 and req2 |
| 233 | + cached_req_data = CachedRequestData( |
| 234 | + req_ids=[], |
| 235 | + resumed_from_preemption=[], |
| 236 | + new_token_ids=[], |
| 237 | + new_block_ids=[], |
| 238 | + num_computed_tokens=[], |
| 239 | + ) |
| 240 | + scheduler_output = SchedulerOutput( |
| 241 | + scheduled_new_reqs=[], |
| 242 | + scheduled_cached_reqs=cached_req_data, |
| 243 | + num_scheduled_tokens={}, |
| 244 | + total_num_scheduled_tokens=0, |
| 245 | + scheduled_spec_decode_tokens={}, |
| 246 | + scheduled_encoder_inputs={}, |
| 247 | + num_common_prefix_blocks=[0], |
| 248 | + finished_req_ids={"req1", "req2"}, |
| 249 | + free_encoder_input_ids=[], |
| 250 | + structured_output_request_ids={}, |
| 251 | + grammar_bitmask=None, |
| 252 | + ) |
| 253 | + mock_spec_decode_model_runner.execute_model(scheduler_output) |
0 commit comments