repo
stringlengths
7
90
file_url
stringlengths
81
315
file_path
stringlengths
4
228
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:38:15
2026-01-05 02:33:18
truncated
bool
2 classes
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/test_multimodal.py
tests/v1/tpu/test_multimodal.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import openai import pytest from vllm.multimodal.utils import encode_image_url from vllm.platforms import current_platform from ...entrypoints.openai.test_vision import TEST_IMAGE_ASSETS from ...utils import RemoteOpenAIServer @pytest.fixture(scope="session") def url_encoded_image(local_asset_server) -> dict[str, str]: return { image_asset: encode_image_url(local_asset_server.get_image_asset(image_asset)) for image_asset in TEST_IMAGE_ASSETS } @pytest.mark.asyncio @pytest.mark.skipif(not current_platform.is_tpu(), reason="This test needs a TPU") @pytest.mark.parametrize("model_name", ["llava-hf/llava-1.5-7b-hf"]) async def test_basic_vision(model_name: str, url_encoded_image: dict[str, str]): pytest.skip("Skip this test until it's fixed.") def whats_in_this_image_msg(url): return [ { "role": "user", "content": [ {"type": "text", "text": "What's in this image?"}, {"type": "image_url", "image_url": {"url": url}}, ], } ] server_args = [ "--max-model-len", "1024", "--max-num-seqs", "16", "--gpu-memory-utilization", "0.95", "--trust-remote-code", "--max-num-batched-tokens", "576", # NOTE: max-num-batched-tokens>=mm_item_size "--disable_chunked_mm_input", ] # Server will pre-compile on first startup (takes a long time). with RemoteOpenAIServer( model_name, server_args, max_wait_seconds=600 ) as remote_server: client: openai.AsyncOpenAI = remote_server.get_async_client() # Other requests now should be much faster for image_url in TEST_IMAGE_ASSETS: image_url = url_encoded_image[image_url] chat_completion_from_url = await client.chat.completions.create( model=model_name, messages=whats_in_this_image_msg(image_url), max_completion_tokens=24, temperature=0.0, ) result = chat_completion_from_url assert result choice = result.choices[0] assert choice.finish_reason == "length" message = choice.message message = result.choices[0].message assert message.content is not None and len(message.content) >= 10 assert message.role == "assistant"
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/test_topk_topp_sampler.py
tests/v1/tpu/test_topk_topp_sampler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math import pytest import torch import torch_xla from vllm.platforms import current_platform from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p from vllm.v1.sample.tpu.sampler import apply_top_k_top_p as apply_top_k_top_p_tpu if not current_platform.is_tpu(): pytest.skip("This test needs a TPU.", allow_module_level=True) import torch_xla.core.xla_model as xm BATCH_SIZE = 1024 VOCAB_SIZE = 128 * 1024 TOLERANCE = 1e-6 def test_topk_equivalence_to_native_impl(): with torch.device(xm.xla_device()): xm.set_rng_state(seed=33) logits = torch.rand((BATCH_SIZE, VOCAB_SIZE)) # Random top-k values between 1 and 10. k = torch.randint(1, 10, (BATCH_SIZE,)) # Set k=vocab_size for ~50% of requests in the batch (top-k disabled). k.masked_fill_(torch.randint(0, 2, (BATCH_SIZE,), dtype=bool), VOCAB_SIZE) result_tpu = apply_top_k_top_p_tpu(logits=logits.clone(), k=k, p=None) result_native = apply_top_k_top_p(logits=logits.clone(), k=k, p=None) assert torch.allclose(result_native, result_tpu) def test_topp_result_sums_past_p(): with torch.device(xm.xla_device()): xm.set_rng_state(seed=33) logits = torch.rand((BATCH_SIZE, VOCAB_SIZE)) probs = logits.softmax(dim=-1) # Random top-p values between 0 and 1. p = torch.rand((BATCH_SIZE,)) # Set p=1 for ~50% of requests in the batch (top-p disabled). p.masked_fill_(torch.randint(0, 2, (BATCH_SIZE,), dtype=bool), 1) no_op_k = torch.tensor([VOCAB_SIZE]) logits_masked = apply_top_k_top_p_tpu(logits=logits.clone(), k=no_op_k, p=p) # Verify that the masked logit's probability sums to at least p. probs.masked_fill_(logits_masked.isinf(), 0) masked_prob_sum = probs.sum(dim=-1) torch_xla.sync() # Perform assertion on CPU. assert torch.all(torch.ge(masked_prob_sum.cpu() + TOLERANCE, p.cpu())) def test_topp_basic(): with torch.device(xm.xla_device()): logits = torch.tensor( [ [math.log(0.2), math.log(0.3), math.log(0.5)], [math.log(0.5), math.log(0.1), math.log(0.4)], ] ) result = apply_top_k_top_p_tpu( logits=logits.clone(), k=torch.tensor([3, 3]), p=torch.tensor([0.79, 0.79]) ) torch_xla.sync() # Expect the smallest elements to be dropped. expected_result = logits.clone().cpu() expected_result[0, 0] = float("-inf") expected_result[1, 1] = float("-inf") assert torch.allclose(expected_result, result.cpu()) def test_topp_select_all(): with torch.device(xm.xla_device()): logits = torch.tensor( [ [math.log(0.2), math.log(0.3), math.log(0.5)], [math.log(0.5), math.log(0.1), math.log(0.4)], ] ) result = apply_top_k_top_p_tpu( logits=logits.clone(), k=torch.tensor([3, 3]), p=torch.tensor([1.0, 1.0]) ) torch_xla.sync() assert torch.allclose(logits.cpu(), result.cpu()) def test_topp_with_ties(): with torch.device(xm.xla_device()): # Input has multiple math.log(0.3). logits = torch.tensor( [[math.log(0.3), math.log(0.3), math.log(0.3), math.log(0.1)]] ) result = apply_top_k_top_p_tpu( logits=logits.clone(), k=torch.tensor([4]), p=torch.tensor([0.2]) ) torch_xla.sync() # All tie values are included in the top-p set. Tie breaking is left # to be done during final sampling (all tie tokens have equal # probability of being chosen). expected_result = logits.clone().cpu() expected_result[0, 3] = float("-inf") assert torch.allclose(expected_result, result.cpu()) def test_both_topk_topp(): with torch.device(xm.xla_device()): logits = torch.tensor( [ [math.log(0.2), math.log(0.3), math.log(0.5)], [math.log(0.5), math.log(0.1), math.log(0.4)], ] ) # Set k=1 for the first batch. result = apply_top_k_top_p_tpu( logits=logits.clone(), k=torch.tensor([1, 3]), p=torch.tensor([0.79, 0.79]) ) torch_xla.sync() # Since for the first batch k=1, expect only the largest element gets # selected. expected_result = logits.clone().cpu() expected_result[0, 0] = float("-inf") expected_result[0, 1] = float("-inf") expected_result[1, 1] = float("-inf") assert torch.allclose(expected_result, result.cpu())
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/test_sampler.py
tests/v1/tpu/test_sampler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import pytest from vllm import LLM from vllm.platforms import current_platform from vllm.sampling_params import SamplingParams @pytest.mark.parametrize("model_name", ["Qwen/Qwen2.5-1.5B-Instruct"]) @pytest.mark.skipif(not current_platform.is_tpu(), reason="This test needs a TPU") def test_sampler_different(model_name: str): """ Test significantly different sampling params to assert the model produces different results. """ llm = LLM( model_name, enforce_eager=False, max_num_seqs=1, max_model_len=512, max_num_batched_tokens=256, ) prompts = ["Write a short story about a robot that dreams for the first time."] sampling_params = SamplingParams(temperature=0.9, min_p=0.2, max_tokens=64) output = llm.generate(prompts, sampling_params) sampling_params = SamplingParams(temperature=0.1, min_p=0.8, max_tokens=64) output2 = llm.generate(prompts, sampling_params) assert output[0].outputs[0].text != output2[0].outputs[0].text with pytest.raises(ValueError): # Unsupported `seed` param. sampling_params = SamplingParams(temperature=0.3, seed=42) output2 = llm.generate(prompts, sampling_params) # Batch-case with TopK/P for B in [4, 16]: p = prompts * B sampling_params = [ SamplingParams( temperature=0.1, min_p=0.8, max_tokens=64, # Vary number of ks top_k=random.randint(4, 12), top_p=random.random(), ) for _ in range(B) ] # Make sure first two reqs have the same K/P sampling_params[0] = sampling_params[1] output = llm.generate(p, sampling_params) # There are natural numerical instabilities that make it difficult # to have deterministic results over many tokens, tests the first ~20 # tokens match. assert output[0].outputs[0].text[:20] == output[1].outputs[0].text[:20] @pytest.mark.parametrize("model_name", ["Qwen/Qwen2.5-1.5B-Instruct"]) # TODO TPU will appear busy if we fan-out test params here @pytest.mark.parametrize("n_prompts", [1]) @pytest.mark.skipif(not current_platform.is_tpu(), reason="This test needs a TPU") def test_logprobs(model_name: str, n_prompts: int): """ Request top logprobs with different sampling settings and check that results contains the requested number, ordered ascendingly. """ def check_num_logprobs(logprobs, expected_num: int): for step in logprobs: prev_logp = 1.0 # order by rank sorted_step = dict(sorted(step.items(), key=lambda item: item[1].rank)) # Can contain the sampled token assert len(step) == expected_num or len(step) == expected_num + 1 # Check results are ordered by prob value for rankno, (tid, logp) in enumerate(sorted_step.items()): assert logp.logprob <= prev_logp prev_logp = logp.logprob assert logp.rank == rankno + 1 llm = LLM( model_name, enforce_eager=False, max_num_seqs=1, max_model_len=128, max_num_batched_tokens=128, ) prompts = [ "Write a short story about a robot that dreams for the first time." ] * n_prompts greedy_sampling_params = SamplingParams(temperature=0.0, max_tokens=64, logprobs=4) regular_sampling_params = SamplingParams(temperature=0.4, max_tokens=64, logprobs=4) topkp_sampling_params = SamplingParams( temperature=0.4, max_tokens=64, logprobs=4, top_k=12, top_p=0.5 ) for sp in [greedy_sampling_params, regular_sampling_params, topkp_sampling_params]: output = llm.generate(prompts, sp) for o in output: check_num_logprobs(o.outputs[0].logprobs, 4)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/test_basic.py
tests/v1/tpu/test_basic.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """A basic correctness check for TPUs Run `pytest tests/v1/tpu/test_basic.py`. """ from typing import TYPE_CHECKING import pytest from torch_xla._internal import tpu from vllm.platforms import current_platform if TYPE_CHECKING: from tests.conftest import VllmRunner else: VllmRunner = object MODELS = [ "Qwen/Qwen2.5-1.5B-Instruct", # TODO: Enable this model when fixed. # "Qwen/Qwen1.5-MoE-A2.7B", # TODO: Enable this models with v6e # "Qwen/Qwen2-7B-Instruct", # "meta-llama/Llama-3.1-8B", ] TENSOR_PARALLEL_SIZES = [1] MAX_NUM_REQS = [16, 1024] # TODO: Enable when CI/CD will have a multi-tpu instance # TENSOR_PARALLEL_SIZES = [1, 4] @pytest.mark.skipif( not current_platform.is_tpu(), reason="This is a basic test for TPU only" ) @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("max_tokens", [5]) @pytest.mark.parametrize("tensor_parallel_size", TENSOR_PARALLEL_SIZES) @pytest.mark.parametrize("max_num_seqs", MAX_NUM_REQS) def test_basic( vllm_runner: type[VllmRunner], model: str, max_tokens: int, tensor_parallel_size: int, max_num_seqs: int, ) -> None: prompt = ( "The next numbers of the sequence " + ", ".join(str(i) for i in range(1024)) + " are:" ) example_prompts = [prompt] with vllm_runner( model, # Note: max_num_batched_tokens == 1024 is needed here to # actually test chunked prompt max_num_batched_tokens=1024, max_model_len=8192, gpu_memory_utilization=0.7, max_num_seqs=max_num_seqs, tensor_parallel_size=tensor_parallel_size, ) as vllm_model: vllm_outputs = vllm_model.generate_greedy(example_prompts, max_tokens) output = vllm_outputs[0][1] assert "1024" in output or "0, 1" in output @pytest.mark.skip(reason="Temporarily disabled due to timeout") @pytest.mark.skipif( not current_platform.is_tpu(), reason="This is a basic test for TPU only" ) @pytest.mark.parametrize("max_tokens", [8]) @pytest.mark.parametrize("max_num_seqs", [16]) def test_phi3( vllm_runner: type[VllmRunner], max_tokens: int, max_num_seqs: int, ) -> None: prompts = [ "A robot may not injure a human being", "It is only with the heart that one can see rightly;", "The greatest glory in living lies not in never falling,", ] answers = [ " or, by violating privacy", " what is essential is love.", " but in rising every time we fall.", ] # test head dim = 96 model = "microsoft/Phi-3-mini-128k-instruct" with vllm_runner( model, max_num_batched_tokens=256, max_num_seqs=max_num_seqs ) as vllm_model: vllm_outputs = vllm_model.generate_greedy(prompts, max_tokens) # vllm_outputs is a list of tuples whose first element is the token id # and the second element is the output (including the prompt). for output, answer in zip(vllm_outputs, answers): generated_text = output[1] assert answer in generated_text TP_SIZE_8 = 8 @pytest.mark.skipif(not current_platform.is_tpu(), reason="This is a test for TPU only") @pytest.mark.skipif( tpu.num_available_chips() < TP_SIZE_8, reason=f"This test requires {TP_SIZE_8} TPU chips.", ) def test_gemma3_27b_with_text_input_and_tp( vllm_runner: type[VllmRunner], ) -> None: model = "google/gemma-3-27b-it" max_tokens = 16 tensor_parallel_size = TP_SIZE_8 max_num_seqs = 4 prompts = [ "A robot may not injure a human being", "It is only with the heart that one can see rightly;", "The greatest glory in living lies not in never falling,", ] answers = [ " or, through inaction, allow a human being to come to harm.", " what is essential is invisible to the eye.", " but in rising every time we fall.", ] with vllm_runner( model, max_num_batched_tokens=256, max_num_seqs=max_num_seqs, tensor_parallel_size=tensor_parallel_size, ) as vllm_model: vllm_outputs = vllm_model.generate_greedy(prompts, max_tokens) # vllm_outputs is a list of tuples whose first element is the token id # and the second element is the output (including the prompt). for output, answer in zip(vllm_outputs, answers): generated_text = output[1] assert answer in generated_text @pytest.mark.skipif( not current_platform.is_tpu(), reason="This is a basic test for TPU only" ) def test_w8a8_quantization( vllm_runner: type[VllmRunner], ) -> None: model = "neuralmagic/Meta-Llama-3.1-8B-Instruct-quantized.w8a8" max_tokens = 5 tensor_parallel_size = 1 max_num_seqs = 4 prompt = ( "The next numbers of the sequence " + ", ".join(str(i) for i in range(1024)) + " are:" ) example_prompts = [prompt] with vllm_runner( model, max_num_batched_tokens=64, max_model_len=4096, gpu_memory_utilization=0.7, max_num_seqs=max_num_seqs, tensor_parallel_size=tensor_parallel_size, ) as vllm_model: vllm_outputs = vllm_model.generate_greedy(example_prompts, max_tokens) output = vllm_outputs[0][1] assert "1024" in output or "0, 1" in output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/worker/test_tpu_model_runner.py
tests/v1/tpu/worker/test_tpu_model_runner.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from vllm.attention.layer import Attention from vllm.config import ( CacheConfig, ModelConfig, SchedulerConfig, VllmConfig, set_current_vllm_config, ) from vllm.pooling_params import PoolingParams from vllm.sampling_params import SamplingParams from vllm.utils.mem_constants import GiB_bytes from vllm.v1.core.kv_cache_utils import estimate_max_model_len, get_kv_cache_configs from vllm.v1.core.sched.output import CachedRequestData, NewRequestData, SchedulerOutput from vllm.v1.worker.tpu_model_runner import ( TPUModelRunner, _get_padded_num_reqs_with_upper_limit, _get_padded_token_len, _get_req_paddings, _get_token_paddings, ) def get_vllm_config(): model_config = ModelConfig( model="facebook/opt-125m", dtype="bfloat16", # TPUs typically use bfloat16 seed=42, ) scheduler_config = SchedulerConfig( max_num_seqs=10, max_num_batched_tokens=512, max_model_len=512, is_encoder_decoder=model_config.is_encoder_decoder, ) cache_config = CacheConfig( block_size=16, gpu_memory_utilization=0.9, swap_space=0, cache_dtype="auto", ) vllm_config = VllmConfig( model_config=model_config, cache_config=cache_config, scheduler_config=scheduler_config, ) return vllm_config def get_model_runner(vllm_config): device = "xla:0" # Mocking TPU device return TPUModelRunner(vllm_config, device) @pytest.fixture def model_runner(): # Patchers have already been started at module level. vllm_config = get_vllm_config() return get_model_runner(vllm_config) def _schedule_new_request(*req_ids: str) -> SchedulerOutput: new_reqs = [] num_scheduled_tokens = {} total_num_scheduled_tokens = 0 for req_id in req_ids: new_reqs.append( NewRequestData( req_id=req_id, prompt_token_ids=[1, 2, 3], mm_features=[], sampling_params=SamplingParams(), pooling_params=PoolingParams(), block_ids=([0],), # block_ids should be tuple[list[int]] num_computed_tokens=0, lora_request=None, ) ) num_scheduled_tokens[req_id] = 3 total_num_scheduled_tokens += num_scheduled_tokens[req_id] return SchedulerOutput( scheduled_new_reqs=new_reqs, scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens=num_scheduled_tokens, total_num_scheduled_tokens=total_num_scheduled_tokens, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) def _is_req_scheduled(model_runner, req_id: str) -> bool: return req_id in model_runner.input_batch.req_id_to_index def _is_req_added(model_runner, req_id: str) -> bool: return req_id in model_runner.requests def _is_req_state_block_table_match(model_runner, req_id: str) -> bool: """Check if the request state block IDs match the block table. This function handles both legacy BlockTable and new MultiGroupBlockTable structures for backward compatibility. """ req_index = model_runner.input_batch.req_id_to_index[req_id] multi_group_block_table = model_runner.input_batch.block_table req_state = model_runner.requests[req_id] # Access the first block table from MultiGroupBlockTable # This is safe since we currently only use single KV cache groups block_table = multi_group_block_table[0] # req_state.block_ids is now tuple[list[int], ...] for MultiGroupBlockTable # Extract the first group's block IDs if isinstance(req_state.block_ids[0], list): # New format: tuple[list[int], ...] - extract first group req_block_ids = req_state.block_ids[0] else: # Legacy format: list[int] - use directly req_block_ids = req_state.block_ids if block_table.num_blocks_per_row[req_index] != len(req_block_ids): return False num_blocks = block_table.num_blocks_per_row[req_index] block_table_values = block_table.block_table.np[req_index, :num_blocks] return (block_table_values == req_block_ids).all() def test_update_states_new_request(model_runner): req_id = "req_0" # new req scheduler_output = _schedule_new_request(req_id) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) assert _is_req_state_block_table_match(model_runner, req_id) def test_update_states_request_finished(model_runner): req_id = "req_0" # new req scheduler_output = _schedule_new_request(req_id) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) # finish req scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={}, total_num_scheduled_tokens=0, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids={req_id}, free_encoder_mm_hashes=[], ) model_runner._update_states(scheduler_output) assert not _is_req_added(model_runner, req_id) assert not _is_req_scheduled(model_runner, req_id) def test_update_states_request_resumed(model_runner): req_id = "req_0" # new req scheduler_output = _schedule_new_request(req_id) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) # unschedule req scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={}, total_num_scheduled_tokens=0, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert not _is_req_scheduled(model_runner, req_id) # resume req cached_req_data = CachedRequestData( req_ids=[req_id], resumed_req_ids={req_id}, new_token_ids=[[]], all_token_ids={req_id: scheduler_output.scheduled_new_reqs[0].prompt_token_ids}, new_block_ids=[([],)], num_computed_tokens=[0], num_output_tokens=[0], ) scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=cached_req_data, num_scheduled_tokens={req_id: 1}, total_num_scheduled_tokens=1, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) assert _is_req_state_block_table_match(model_runner, req_id) def test_update_states_no_changes(model_runner): req_id = "req_0" # new req scheduler_output = _schedule_new_request(req_id) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) # schedule req scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={req_id: 1}, total_num_scheduled_tokens=1, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) assert _is_req_state_block_table_match(model_runner, req_id) def test_update_states_request_unscheduled(model_runner): req_ids = ("req_0", "req_1") # new reqs scheduler_output = _schedule_new_request(*req_ids) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_ids[0]) assert _is_req_scheduled(model_runner, req_ids[0]) assert _is_req_added(model_runner, req_ids[1]) assert _is_req_scheduled(model_runner, req_ids[1]) # unschedule req_1 scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={req_ids[0]: 1}, total_num_scheduled_tokens=1, scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_runner._update_states(scheduler_output) assert _is_req_added(model_runner, req_ids[0]) assert _is_req_scheduled(model_runner, req_ids[0]) assert _is_req_added(model_runner, req_ids[1]) assert not _is_req_scheduled(model_runner, req_ids[1]) def test_get_paddings(): # Bucketed padding min_token_size, max_token_size, padding_gap = 16, 512, 64 expected_paddings = [16, 32, 64, 128, 192, 256, 320, 384, 448, 512] actual_paddings = _get_token_paddings(min_token_size, max_token_size, padding_gap) # Bucketed padding with max_token_size not a power of two. max_token_size = 317 expected_paddings = [16, 32, 64, 128, 192, 256, 320] actual_paddings = _get_token_paddings(min_token_size, max_token_size, padding_gap) assert actual_paddings == expected_paddings # Exponential padding. max_token_size, padding_gap = 1024, 0 expected_paddings = [16, 32, 64, 128, 256, 512, 1024] actual_paddings = _get_token_paddings(min_token_size, max_token_size, padding_gap) assert actual_paddings == expected_paddings # Exponential padding with max_token_size not a power of two. max_token_size = 317 expected_paddings = [16, 32, 64, 128, 256, 512] actual_paddings = _get_token_paddings(min_token_size, max_token_size, padding_gap) assert actual_paddings == expected_paddings def test_get_padded_token_len(): min_token_size, max_token_size, padding_gap = 16, 512, 64 paddings = _get_token_paddings(min_token_size, max_token_size, padding_gap) assert _get_padded_token_len(paddings, 1) == 16 assert _get_padded_token_len(paddings, 16) == 16 assert _get_padded_token_len(paddings, 20) == 32 assert _get_padded_token_len(paddings, 300) == 320 assert _get_padded_token_len(paddings, 512) == 512 def test_get_padded_num_reqs_with_upper_limit(): assert _get_padded_num_reqs_with_upper_limit(3, 32) == 8 assert _get_padded_num_reqs_with_upper_limit(9, 32) == 16 assert _get_padded_num_reqs_with_upper_limit(19, 32) == 32 assert _get_padded_num_reqs_with_upper_limit(17, 28) == 28 def test_get_req_paddings(): assert _get_req_paddings(1, 32) == [8, 16, 32] assert _get_req_paddings(8, 32) == [8, 16, 32] assert _get_req_paddings(8, 36) == [8, 16, 32, 36] def test_init_kv_cache_with_kv_sharing_invalid_target_layer_order(model_runner): layer_0 = "model.layers.0.self_attn.attn" layer_1 = "model.layers.1.self_attn.attn" error_msg = f"{layer_1} must come before the current layer" vllm_config = model_runner.vllm_config with ( pytest.raises(ValueError, match=error_msg), set_current_vllm_config(vllm_config), ): fwd_context = { # initialization below will fail because target layer is invalid; # the target layer needs to come before layer 1 layer_0: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_0, kv_sharing_target_layer_name=layer_1, ), layer_1: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_1, ), } # suppress var not used error assert fwd_context is not None def test_init_kv_cache_with_kv_sharing_target_layer_not_exist(model_runner): layer_0 = "model.layers.0.self_attn.attn" layer_1 = "model.layers.1.self_attn.attn" invalid_layer = "model.layers.0.cross_attn.attn" error_msg = f"{invalid_layer} is not a valid Attention layer in the model" vllm_config = model_runner.vllm_config with ( pytest.raises(ValueError, match=error_msg), set_current_vllm_config(vllm_config), ): fwd_context = { layer_0: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_0, ), layer_1: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_1, # invalid layer: cross_attn.atn doesn't exist! kv_sharing_target_layer_name=invalid_layer, ), } # suppress var not used error assert fwd_context is not None def test_init_kv_cache_with_kv_sharing_target_same_as_current(model_runner): layer_0 = "model.layers.0.self_attn.attn" layer_1 = "model.layers.1.self_attn.attn" error_msg = f"{layer_1} cannot be the same as the current layer" vllm_config = model_runner.vllm_config with ( pytest.raises(ValueError, match=error_msg), set_current_vllm_config(vllm_config), ): fwd_context = { # initialization below will fail because target layer is invalid; # the target layer needs to come before layer 1 layer_0: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_0, ), layer_1: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_1, kv_sharing_target_layer_name=layer_1, ), } # suppress var not used error assert fwd_context is not None def test_init_kv_cache_without_kv_sharing(): layer_0 = "model.layers.0.self_attn.attn" layer_1 = "model.layers.1.self_attn.attn" vllm_config = get_vllm_config() with set_current_vllm_config(vllm_config): fwd_context = { layer_0: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_0, ), layer_1: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_1, ), } # suppress var not used error assert fwd_context is not None # Set high context length to test max context length estimation vllm_config.model_config.max_model_len = 1_000_000 vllm_ctx = vllm_config.compilation_config.static_forward_context model_runner = get_model_runner(vllm_config) kv_cache_spec = model_runner.get_kv_cache_spec() assert len(kv_cache_spec) == 2 assert len(model_runner.shared_kv_cache_layers) == 0 available_memory = 20 * GiB_bytes # page size for each layer KV can be calculated as # 2 (non-MLA) * 8 (num_heads) * 128 (head_dim) # * 2 (bfloat16, kv_cache dtype) * 128 (block_size) = 512KB num_expected_blocks = 20480 # 20GB / 512KB / 2 (num layers) kv_cache_config = get_kv_cache_configs( vllm_config, [kv_cache_spec], [available_memory] )[0] assert kv_cache_config.num_blocks == num_expected_blocks assert len(kv_cache_config.kv_cache_tensors) == 2 assert kv_cache_config.kv_cache_tensors[0].size == available_memory // 2 assert kv_cache_config.kv_cache_tensors[1].size == available_memory // 2 max_context_len = estimate_max_model_len(vllm_config, kv_cache_spec, 5 * GiB_bytes) # max context len with KV sharing should be 2x as large as without # max_context_len = available_memory / (page_size / block_size) / num_caches # max_context_len = 5GB / (512KB / 128) / 2 = 655360 assert max_context_len == 655360 # important: override tensor size to prevent large mem alloc during test # this will only allocate 2 block worth of memory (2 * 512kb) kv_cache_config.num_blocks = 1 for kv_cache_tensor in kv_cache_config.kv_cache_tensors: kv_cache_tensor.size = kv_cache_spec[ kv_cache_tensor.shared_by[0] ].page_size_bytes model_runner.initialize_kv_cache(kv_cache_config) layer_0_kv = vllm_ctx[layer_0].kv_cache[0] layer_1_kv = vllm_ctx[layer_1].kv_cache[0] # check layer 1 kv cache does NOT share memory with layer 0 assert id(layer_1_kv) != id(layer_0_kv) # check layer 1 added to kv cache group's layer names assert len(kv_cache_config.kv_cache_groups) == 1 assert len(kv_cache_config.kv_cache_groups[0].layer_names) == 2 assert kv_cache_config.kv_cache_groups[0].layer_names[0] == layer_0 assert kv_cache_config.kv_cache_groups[0].layer_names[1] == layer_1 def test_init_kv_cache_with_kv_sharing_valid(): layer_0 = "model.layers.0.self_attn.attn" layer_1 = "model.layers.1.self_attn.attn" vllm_config = get_vllm_config() with set_current_vllm_config(vllm_config): fwd_context = { layer_0: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_0, ), layer_1: Attention( num_heads=8, head_size=128, scale=1.0, prefix=layer_1, kv_sharing_target_layer_name="model.layers.0.self_attn.attn", ), } # suppress var not used error assert fwd_context is not None # Set high context length to test max context length estimation vllm_config.model_config.max_model_len = 3_000_000 vllm_ctx = vllm_config.compilation_config.static_forward_context model_runner = get_model_runner(vllm_config) kv_cache_spec = model_runner.get_kv_cache_spec() assert len(kv_cache_spec) == 1 assert layer_0 in kv_cache_spec assert model_runner.shared_kv_cache_layers[layer_1] == layer_0 available_memory = 20 * GiB_bytes # page size for layer 0's kv_cache_spec is 512KB # with KV sharing, we can allocate (available_mem//page_size//1) blocks # which is twice as many as without KV sharing num_expected_blocks = 2 * 20480 # 20GB / 512KB kv_cache_config = get_kv_cache_configs( vllm_config, [kv_cache_spec], [available_memory] )[0] assert kv_cache_config.num_blocks == num_expected_blocks assert len(kv_cache_config.kv_cache_tensors) == 1 # Each layer now has twice the available memory for KV cache # compared to no KV sharing assert kv_cache_config.kv_cache_tensors[0].size == available_memory max_context_len = estimate_max_model_len(vllm_config, kv_cache_spec, 5 * GiB_bytes) # max context len with KV sharing should be 2x as large as without assert max_context_len == (2 * 655360) # important: override tensor size to prevent large mem alloc during test # this will only allocate 1 block worth of memory (512kb) kv_cache_config.num_blocks = 1 kv_cache_config.kv_cache_tensors[0].size = kv_cache_spec[layer_0].page_size_bytes model_runner.initialize_kv_cache(kv_cache_config) layer_0_kv = vllm_ctx[layer_0].kv_cache[0] layer_1_kv = vllm_ctx[layer_1].kv_cache[0] # check layer 1 kv cache shares memory with layer 0 assert id(layer_1_kv) == id(layer_0_kv) # check layer 1 added to kv cache group's layer names assert len(kv_cache_config.kv_cache_groups) == 1 assert len(kv_cache_config.kv_cache_groups[0].layer_names) == 2 assert kv_cache_config.kv_cache_groups[0].layer_names[0] == layer_0 assert kv_cache_config.kv_cache_groups[0].layer_names[1] == layer_1 def test_most_model_len(monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("VLLM_TPU_MOST_MODEL_LEN", "2048") vllm_config = get_vllm_config() vllm_config.model_config.max_model_len = 32000 vllm_config.scheduler_config.max_num_seqs = 1200 model_runner = get_model_runner(vllm_config) # verify model runner will adjust num_reqs to avoid SMEM OOM. assert model_runner.num_reqs_most_model_len == 1200 # num_page_per_req = 32k // 128 # num_reqs = 1024 ** 2 // 2 // num_page_per_req // 4 = 524 assert model_runner.num_reqs_max_model_len == 524
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tpu/worker/__init__.py
tests/v1/tpu/worker/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/conftest.py
tests/v1/entrypoints/conftest.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest @pytest.fixture def sample_prompts(): return [ "Hello, my name is", "The president of the United States is", "The capital of France is", "The future of AI is", ] @pytest.fixture def sample_token_ids(): return [ [0], [0, 1], [0, 2, 1], [0, 3, 1, 2], ] @pytest.fixture def sample_regex(): return ( r"((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.){3}" r"(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)" ) # Note: Ensure this only uses attributes compatible with xgrammar @pytest.fixture def sample_json_schema(): return { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"}, "skills": { "type": "array", "items": { "type": "string", }, }, "grade": { "type": "string", "pattern": "^[A-D]$", # Regex pattern }, "email": { "type": "string", "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", }, "work_history": { "type": "array", "items": { "type": "object", "properties": { "company": {"type": "string"}, "duration": { "type": "number", "minimum": 0.0, "maximum": 100.0, # Numeric range }, "position": {"type": "string"}, }, "required": ["company", "duration", "position"], "additionalProperties": False, }, "minItems": 0, "maxItems": 3, }, }, "required": ["name", "age", "skills", "grade", "email", "work_history"], "additionalProperties": False, "minProperties": 1, "maxProperties": 10, } # A schema unsupported by xgrammar @pytest.fixture def unsupported_json_schema(): return { "type": "object", "properties": { "score": { "type": "integer", "multipleOf": 5, # Numeric multiple }, "tags": { "type": "array", "items": {"type": "string", "minLength": 10, "maxLength": 20}, }, }, "required": ["score", "tags"], "additionalProperties": False, "patternProperties": { "^score$": {"type": "integer"}, }, } @pytest.fixture def sample_definition_json_schema(): return { "$defs": { "Step": { "properties": { "explanation": {"title": "Explanation", "type": "string"}, "output": {"title": "Output", "type": "string"}, }, "required": ["explanation", "output"], "title": "Step", "type": "object", } }, "properties": { "steps": { "items": {"$ref": "#/$defs/Step"}, "title": "Steps", "type": "array", }, "final_answer": {"title": "Final Answer", "type": "string"}, }, "required": ["steps", "final_answer"], "title": "MathReasoning", "type": "object", "additionalProperties": False, } @pytest.fixture def sample_structured_outputs_choices(): return [ "Python", "Java", "JavaScript", "C++", "C#", "PHP", "TypeScript", "Ruby", "Swift", "Kotlin", ] @pytest.fixture def sample_sql_ebnf(): return """ root ::= select_statement select_statement ::= "SELECT" column "from" table "where" condition column ::= "col_1" | "col_2" table ::= "table_1" | "table_2" condition ::= column "=" number number ::= "1" | "2" """ @pytest.fixture def sample_sql_lark(): return """ start: select_statement select_statement: "SELECT" column "from" table "where" condition column: "col_1" | "col_2" table: "table_1" | "table_2" condition: column "=" number number: "1" | "2" """
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/__init__.py
tests/v1/entrypoints/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/test_chat_completion.py
tests/v1/entrypoints/openai/test_chat_completion.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import openai # use the official client for correctness check import pytest import pytest_asyncio from tests.utils import RemoteOpenAIServer # any model with a chat template defined in tokenizer_config should work here MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct" @pytest.fixture(scope="module") def default_server_args(): return [ # use half precision for speed and memory savings in CI environment "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", ] @pytest.fixture(scope="module") def server(default_server_args): with RemoteOpenAIServer(MODEL_NAME, default_server_args) as remote_server: yield remote_server @pytest_asyncio.fixture async def client(server): async with server.get_async_client() as async_client: yield async_client @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_json_schema(client: openai.AsyncOpenAI, model_name: str) -> None: invalid_json_schema = { "$defs": { "CarType": { "enum": ["sedan", "SUV", "Truck", "Coupe"], "title": "CarType", "type": "string", } }, "properties": { "brand": {"title": "Brand", "type": "string"}, "model": {"title": "Model", "type": "string"}, "car_type": {"$ref": "#/$defs/CarType"}, "foo": "bar", }, "required": ["brand", "model", "car_type"], "title": "CarDescription", "type": "object", } prompt = ( "Generate a JSON with the brand, model and car_type of" "the most iconic car from the 90's" ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.chat.completions.create( model=model_name, messages=[ { "role": "user", "content": prompt, } ], extra_body={"structured_outputs": {"json": invalid_json_schema}}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_regex(client: openai.AsyncOpenAI, model_name: str): prompt = ( "Generate an email address for Alan Turing, who works in Enigma." "End in .com and new line. Example result:" "alan.turing@enigma.com\n" ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.chat.completions.create( model=model_name, messages=[ { "role": "user", "content": prompt, } ], extra_body={"structured_outputs": {"regex": r"[.*"}, "stop": ["\n"]}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_grammar(client: openai.AsyncOpenAI, model_name: str): invalid_simplified_sql_grammar = """ root ::= select_statementinvalidsyntax select_statement ::= "SELECT " column " from " table " where " condition column ::= "col_1 " | "col_2 " table ::= "table_1 " | "table_2 " condition ::= column "= " number number ::= "1 " | "2 " """ prompt = ( "Generate an SQL query to show the 'username' and 'email'" "from the 'users' table." ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.chat.completions.create( model=model_name, messages=[ { "role": "user", "content": prompt, } ], extra_body={ "structured_outputs": {"grammar": invalid_simplified_sql_grammar} }, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_empty_grammar(client: openai.AsyncOpenAI, model_name: str) -> None: prompt = "Say hello" with pytest.raises((openai.BadRequestError, openai.APIError)): await client.chat.completions.create( model=model_name, messages=[ { "role": "user", "content": prompt, } ], extra_body={"structured_outputs": {"grammar": ""}}, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/test_multi_api_servers.py
tests/v1/entrypoints/openai/test_multi_api_servers.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os import openai # use the official client for correctness check import pytest import pytest_asyncio from tests.utils import RemoteOpenAIServer from tests.v1.utils import check_request_balancing MODEL_NAME = "hmellor/tiny-random-LlamaForCausalLM" DP_SIZE = os.getenv("DP_SIZE", "1") @pytest.fixture(scope="module") def default_server_args(): return [ # use half precision for speed and memory savings in CI environment "--dtype", "bfloat16", "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", "--api-server-count", "4", "--data_parallel_size", DP_SIZE, ] @pytest.fixture(scope="module") def server(default_server_args): with RemoteOpenAIServer(MODEL_NAME, default_server_args) as remote_server: yield remote_server @pytest_asyncio.fixture async def client(server): async with server.get_async_client() as async_client: yield async_client @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_single_completion( client: openai.AsyncOpenAI, server: RemoteOpenAIServer, model_name: str ) -> None: async def make_request(): completion = await client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=10, temperature=1.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] # The exact number of tokens can vary slightly with temperature=1.0, # so we check for a reasonable minimum length. assert len(choice.text) >= 1 # Finish reason might not always be 'length' if the model finishes early # or due to other reasons, especially with high temperature. # So, we'll accept 'length' or 'stop'. assert choice.finish_reason in ("length", "stop") # Token counts can also vary, so we check they are positive. assert completion.usage.completion_tokens > 0 assert completion.usage.prompt_tokens > 0 assert completion.usage.total_tokens > 0 return completion # Test single request result = await make_request() assert result is not None await asyncio.sleep(0.5) # Send two bursts of requests num_requests = 100 tasks = [make_request() for _ in range(num_requests)] results = await asyncio.gather(*tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) await asyncio.sleep(0.5) tasks = [make_request() for _ in range(num_requests)] results = await asyncio.gather(*tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) # Check request balancing via Prometheus metrics if DP_SIZE > 1 check_request_balancing(server, int(DP_SIZE)) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_completion_streaming( client: openai.AsyncOpenAI, server: RemoteOpenAIServer, model_name: str ) -> None: prompt = "What is an LLM?" async def make_streaming_request(): # Perform a non-streaming request to get the expected full output single_completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text # Perform the streaming request stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 last_chunk = None async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 last_chunk = chunk # Keep track of the last chunk # finish reason should only return in the last block for OpenAI API assert finish_reason_count == 1, "Finish reason should appear exactly once." assert last_chunk is not None, "Stream should have yielded at least one chunk." assert last_chunk.choices[0].finish_reason == "length", ( "Finish reason should be 'length'." ) # Check that the combined text matches the non-streamed version. assert "".join(chunks) == single_output, ( "Streamed output should match non-streamed output." ) return True # Indicate success for this request # Test single request result = await make_streaming_request() assert result is not None await asyncio.sleep(0.5) # Send two bursts of requests num_requests = 100 tasks = [make_streaming_request() for _ in range(num_requests)] results = await asyncio.gather(*tasks) assert len(results) == num_requests, ( f"Expected {num_requests} results, got {len(results)}" ) assert all(results), "Not all streaming requests completed successfully." await asyncio.sleep(0.5) tasks = [make_streaming_request() for _ in range(num_requests)] results = await asyncio.gather(*tasks) assert len(results) == num_requests, ( f"Expected {num_requests} results, got {len(results)}" ) assert all(results), "Not all streaming requests completed successfully." # Check request balancing via Prometheus metrics if DP_SIZE > 1 check_request_balancing(server, int(DP_SIZE))
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/test_completion.py
tests/v1/entrypoints/openai/test_completion.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import openai # use the official client for correctness check import pytest import pytest_asyncio import regex as re from openai import BadRequestError from tests.utils import RemoteOpenAIServer from vllm.tokenizers import get_tokenizer # any model with a chat template should work here MODEL_NAME = "facebook/opt-125m" @pytest.fixture(scope="module") def default_server_args(): return [ "--dtype", "float32", "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", "--enable-prompt-tokens-details", ] @pytest.fixture( scope="module", params=[ ["--no-enable-prefix-caching"], ["--no-enable-prefix-caching", "--disable-frontend-multiprocessing"], ], ) def server(default_server_args, request): if request.param: default_server_args = default_server_args + request.param with RemoteOpenAIServer(MODEL_NAME, default_server_args) as remote_server: yield remote_server @pytest_asyncio.fixture async def client(server): async with server.get_async_client() as async_client: yield async_client @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_single_completion(client: openai.AsyncOpenAI, model_name: str) -> None: completion = await client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=5, temperature=0.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] assert len(choice.text) >= 5 assert choice.finish_reason == "length" assert completion.usage == openai.types.CompletionUsage( completion_tokens=5, prompt_tokens=6, total_tokens=11 ) # test using token IDs completion = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, ) assert len(completion.choices[0].text) >= 1 assert completion.choices[0].prompt_logprobs is None @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_no_logprobs(client: openai.AsyncOpenAI, model_name: str): # test using token IDs completion = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, logprobs=None, ) choice = completion.choices[0] assert choice.logprobs is None @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_zero_logprobs(client: openai.AsyncOpenAI, model_name: str): # test using token IDs completion = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, logprobs=0, ) choice = completion.choices[0] assert choice.logprobs is not None assert choice.logprobs.token_logprobs is not None assert choice.logprobs.top_logprobs is not None assert len(choice.logprobs.top_logprobs[0]) == 1 @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_some_logprobs(client: openai.AsyncOpenAI, model_name: str): # test using token IDs completion = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, logprobs=5, ) choice = completion.choices[0] assert choice.logprobs is not None assert choice.logprobs.token_logprobs is not None assert choice.logprobs.top_logprobs is not None assert 5 <= len(choice.logprobs.top_logprobs[0]) <= 6 @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_too_many_completion_logprobs( client: openai.AsyncOpenAI, model_name: str ) -> None: with pytest.raises( (openai.BadRequestError, openai.APIError) ): # test using token IDs await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, # vLLM has higher default max_logprobs (20 instead of 5) to support # both Completion API and Chat Completion API logprobs=21, ) ... with pytest.raises( (openai.BadRequestError, openai.APIError) ): # test using token IDs stream = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, # vLLM has higher default max_logprobs (20 instead of 5) to support # both Completion API and Chat Completion API logprobs=30, stream=True, ) async for chunk in stream: ... # the server should still work afterwards completion = await client.completions.create( model=model_name, prompt=[0, 0, 0, 0, 0], max_tokens=5, temperature=0.0, ) assert len(completion.choices[0].text) >= 0 @pytest.mark.asyncio @pytest.mark.parametrize( "model_name, prompt_logprobs", [(MODEL_NAME, -1), (MODEL_NAME, 0), (MODEL_NAME, 1), (MODEL_NAME, None)], ) async def test_prompt_logprobs_completion( client: openai.AsyncOpenAI, model_name: str, prompt_logprobs: int | None ): params: dict = { "prompt": ["A robot may not injure another robot", "My name is"], "model": model_name, } if prompt_logprobs is not None: params["extra_body"] = {"prompt_logprobs": prompt_logprobs} if prompt_logprobs is not None and prompt_logprobs < 0: with pytest.raises(BadRequestError): await client.completions.create(**params) else: completion = await client.completions.create(**params) if prompt_logprobs is not None: assert completion.choices[0].prompt_logprobs is not None assert len(completion.choices[0].prompt_logprobs) > 0 assert completion.choices[1].prompt_logprobs is not None assert len(completion.choices[1].prompt_logprobs) > 0 else: assert completion.choices[0].prompt_logprobs is None @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_completion_streaming( client: openai.AsyncOpenAI, model_name: str ) -> None: prompt = "What is an LLM?" single_completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 # finish reason should only return in last block assert finish_reason_count == 1 assert chunk.choices[0].finish_reason == "length" assert chunk.choices[0].text assert "".join(chunks) == single_output @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_parallel_no_streaming(client: openai.AsyncOpenAI, model_name: str): """Parallel sampling without streaming. A single request output contains a list of completions. """ prompt = "What is an LLM?" n = 3 max_tokens = 50 # we want some to finish earlier than others # High temperature to maximize chance of unique completions. completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=max_tokens, n=n, temperature=1.0, stream=False, logprobs=0, seed=42, ) # Assert `n` completions num_completions = len(completion.choices) assert num_completions == n, f"Num completions {num_completions} but expected {n}." completion_repeats: dict[str, int] = {} output_token_lengths = set() for idx, choice in enumerate(completion.choices): # Assert correct completion index & some finish reason. assert choice.index == idx, f"Index {choice.index} but expected {idx}." assert choice.finish_reason is not None, "None finish_reason is invalid." text = choice.text completion_repeats[text] = completion_repeats.get(text, 0) + 1 output_token_lengths.add(len(choice.logprobs.tokens)) # Assert subrequests finished at different times assert len(output_token_lengths) > 1 # Assert `n` unique completions num_unique = len(completion_repeats) if num_unique != n: repeats = {txt: num for (txt, num) in completion_repeats.items() if num > 1} raise AssertionError( f"Expected {n} unique completions, got {num_unique}; repeats: {repeats}." ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_parallel_streaming(client: openai.AsyncOpenAI, model_name: str): """Streaming for parallel sampling. The tokens from multiple samples, are flattened into a single stream, with an index to indicate which sample the token belongs to. """ prompt = "What is an LLM?" n = 3 max_tokens = 50 # we want some to finish earlier than others stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=max_tokens, n=n, temperature=1.0, stream=True, seed=42, ) chunks: list[list[str]] = [[] for _ in range(n)] finish_reason_count = 0 async for chunk in stream: index = chunk.choices[0].index text = chunk.choices[0].text chunks[index].append(text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 # Assert `n` completions with correct finish reasons assert finish_reason_count == n, ( f"Expected {n} completions with valid indices and finish_reason." ) completion_repeats: dict[str, int] = {} chunk_lengths = set() for chunk in chunks: chunk_len = len(chunk) # Assert correct number of completion tokens chunk_lengths.add(chunk_len) assert chunk_len <= max_tokens, ( f"max_tokens={max_tokens} but chunk len is {chunk_len}." ) text = "".join(chunk) completion_repeats[text] = completion_repeats.get(text, 0) + 1 print(text) # Assert subrequests finished at different times assert len(chunk_lengths) > 1 # Assert `n` unique completions num_unique = len(completion_repeats) if num_unique != n: repeats = {txt: num for (txt, num) in completion_repeats.items() if num > 1} raise AssertionError( f"{num_unique} unique completions, expected {n}; repeats: {repeats}" ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_completion_stream_options(client: openai.AsyncOpenAI, model_name: str): prompt = "What is the capital of France?" # Test stream=True, stream_options= # {"include_usage": False, "continuous_usage_stats": False} stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True, stream_options={ "include_usage": False, "continuous_usage_stats": False, }, ) async for chunk in stream: assert chunk.usage is None # Test stream=True, stream_options= # {"include_usage": False, "continuous_usage_stats": True} stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True, stream_options={ "include_usage": False, "continuous_usage_stats": True, }, ) async for chunk in stream: assert chunk.usage is None # Test stream=True, stream_options= # {"include_usage": True, "continuous_usage_stats": False} stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True, stream_options={ "include_usage": True, "continuous_usage_stats": False, }, ) async for chunk in stream: if chunk.choices[0].finish_reason is None: assert chunk.usage is None else: assert chunk.usage is None final_chunk = await anext(stream) assert final_chunk.usage is not None assert final_chunk.usage.prompt_tokens > 0 assert final_chunk.usage.completion_tokens > 0 assert final_chunk.usage.total_tokens == ( final_chunk.usage.prompt_tokens + final_chunk.usage.completion_tokens ) assert final_chunk.choices == [] # Test stream=True, stream_options= # {"include_usage": True, "continuous_usage_stats": True} stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True, stream_options={ "include_usage": True, "continuous_usage_stats": True, }, ) async for chunk in stream: assert chunk.usage is not None assert chunk.usage.prompt_tokens > 0 assert chunk.usage.completion_tokens > 0 assert chunk.usage.total_tokens == ( chunk.usage.prompt_tokens + chunk.usage.completion_tokens ) if chunk.choices[0].finish_reason is not None: final_chunk = await anext(stream) assert final_chunk.usage is not None assert final_chunk.usage.prompt_tokens > 0 assert final_chunk.usage.completion_tokens > 0 assert final_chunk.usage.total_tokens == ( final_chunk.usage.prompt_tokens + final_chunk.usage.completion_tokens ) assert final_chunk.choices == [] # Test stream=False, stream_options= # {"include_usage": None} with pytest.raises(BadRequestError): await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=False, stream_options={"include_usage": None}, ) # Test stream=False, stream_options= # {"include_usage": True} with pytest.raises(BadRequestError): await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=False, stream_options={"include_usage": True}, ) # Test stream=False, stream_options= # {"continuous_usage_stats": None} with pytest.raises(BadRequestError): await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=False, stream_options={"continuous_usage_stats": None}, ) # Test stream=False, stream_options= # {"continuous_usage_stats": True} with pytest.raises(BadRequestError): await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=False, stream_options={"continuous_usage_stats": True}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_batch_completions(client: openai.AsyncOpenAI, model_name: str): # test both text and token IDs for prompts in (["Hello, my name is"] * 2, [[0, 0, 0, 0, 0]] * 2): # test simple list batch = await client.completions.create( model=model_name, prompt=prompts, max_tokens=5, temperature=0.0, ) assert len(batch.choices) == 2 assert batch.choices[0].text == batch.choices[1].text # test n = 2 batch = await client.completions.create( model=model_name, prompt=prompts, n=2, max_tokens=5, temperature=0.0, extra_body=dict( # NOTE: this has to be true for n > 1 in vLLM, but # not necessary for official client. use_beam_search=True ), ) assert len(batch.choices) == 4 assert batch.choices[0].text != batch.choices[1].text, ( "beam search should be different" ) assert batch.choices[0].text == batch.choices[2].text, ( "two copies of the same prompt should be the same" ) assert batch.choices[1].text == batch.choices[3].text, ( "two copies of the same prompt should be the same" ) # test streaming batch = await client.completions.create( model=model_name, prompt=prompts, max_tokens=5, temperature=0.0, stream=True, ) texts = [""] * 2 async for chunk in batch: assert len(chunk.choices) == 1 choice = chunk.choices[0] texts[choice.index] += choice.text assert texts[0] == texts[1] @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) @pytest.mark.parametrize("logprobs_arg", [1, 0]) async def test_echo_logprob_completion( client: openai.AsyncOpenAI, model_name: str, logprobs_arg: int ): tokenizer = get_tokenizer(tokenizer_name=MODEL_NAME) # test using text and token IDs for prompt in ("Hello, my name is", [0, 0, 0, 0, 0]): completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, echo=True, logprobs=logprobs_arg, ) prompt_text = tokenizer.decode(prompt) if isinstance(prompt, list) else prompt assert re.search(r"^" + prompt_text, completion.choices[0].text) logprobs = completion.choices[0].logprobs assert logprobs is not None assert len(logprobs.text_offset) > 5 assert len(logprobs.token_logprobs) > 5 and logprobs.token_logprobs[0] is None assert len(logprobs.top_logprobs) > 5 and logprobs.top_logprobs[0] is None for top_logprobs in logprobs.top_logprobs[1:]: assert max(logprobs_arg, 1) <= len(top_logprobs) <= logprobs_arg + 1 assert len(logprobs.tokens) > 5 @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_json_schema(client: openai.AsyncOpenAI, model_name: str) -> None: invalid_json_schema = { "$defs": { "CarType": { "enum": ["sedan", "SUV", "Truck", "Coupe"], "title": "CarType", "type": "string", } }, "properties": { "brand": {"title": "Brand", "type": "string"}, "model": {"title": "Model", "type": "string"}, "car_type": {"$ref": "#/$defs/CarType"}, "foo": "bar", }, "required": ["brand", "model", "car_type"], "title": "CarDescription", "type": "object", } prompt = ( "Generate a JSON with the brand, model and car_type of" "the most iconic car from the 90's" ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.completions.create( model=model_name, prompt=prompt, extra_body={"structured_outputs": {"json": invalid_json_schema}}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_regex(client: openai.AsyncOpenAI, model_name: str): prompt = ( "Generate an email address for Alan Turing, who works in Enigma." "End in .com and new line. Example result:" "alan.turing@enigma.com\n" ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.completions.create( model=model_name, prompt=prompt, extra_body={"structured_outputs": {"regex": r"[.*"}, "stop": ["\n"]}, ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_invalid_grammar(client: openai.AsyncOpenAI, model_name: str): invalid_simplified_sql_grammar = """ root ::= select_statementinvalidsyntax select_statement ::= "SELECT " column " from " table " where " condition column ::= "col_1 " | "col_2 " table ::= "table_1 " | "table_2 " condition ::= column "= " number number ::= "1 " | "2 " """ prompt = ( "Generate an SQL query to show the 'username' and 'email'" "from the 'users' table." ) with pytest.raises((openai.BadRequestError, openai.APIError)): await client.completions.create( model=model_name, prompt=prompt, extra_body={ "structured_outputs": {"grammar": invalid_simplified_sql_grammar} }, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/test_completion_with_image_embeds.py
tests/v1/entrypoints/openai/test_completion_with_image_embeds.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import openai # use the official client for correctness check import pytest import pytest_asyncio import torch from transformers import AutoConfig from tests.conftest import ImageTestAssets from tests.utils import RemoteOpenAIServer from vllm.utils.serial_utils import tensor2base64 # any model with a chat template should work here MODEL_NAME = "llava-hf/llava-1.5-7b-hf" CONFIG = AutoConfig.from_pretrained(MODEL_NAME) MAXIMUM_IMAGES = 2 @pytest.fixture(scope="module") def default_image_embeds_server_args() -> list[str]: return [ "--dtype", "bfloat16", "--max-model-len", "2048", "--max-num-seqs", "4", "--enforce-eager", "--limit-mm-per-prompt", json.dumps({"image": MAXIMUM_IMAGES}), "--enable-mm-embeds", ] @pytest.fixture(scope="module") def server_with_image_embeds(default_image_embeds_server_args): with RemoteOpenAIServer( MODEL_NAME, default_image_embeds_server_args, max_wait_seconds=600 ) as remote_server: yield remote_server @pytest_asyncio.fixture async def client_with_image_embeds(server_with_image_embeds): async with server_with_image_embeds.get_async_client() as async_client: yield async_client @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) @pytest.mark.parametrize("dtype", [torch.half, torch.float16, torch.float32]) async def test_completions_with_image_embeds( client_with_image_embeds: openai.AsyncOpenAI, model_name: str, image_assets: ImageTestAssets, dtype: torch.dtype, ): # Test case: Single image embeds input image_embeds = image_assets[0].image_embeds.to(dtype=dtype) base64_image_embedding = tensor2base64(image_embeds) chat_completion = await client_with_image_embeds.chat.completions.create( messages=[ {"role": "system", "content": "You are a helpful assistant."}, { "role": "user", "content": [ { "type": "text", "text": "Describe these images separately. For each image," "reply with a short sentence (no more than 10 words).", }, { "type": "image_embeds", "image_embeds": base64_image_embedding, }, ], }, ], model=model_name, ) assert chat_completion.choices[0].message.content is not None assert isinstance(chat_completion.choices[0].message.content, str) assert len(chat_completion.choices[0].message.content) > 0
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/test_function_call.py
tests/v1/entrypoints/openai/serving_responses/test_function_call.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import openai # use the official client for correctness check import pytest MODEL_NAME = "Qwen/Qwen3-1.7B" tools = [ { "type": "function", "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "The city to find the weather for, e.g. 'Vienna'", "default": "Vienna", }, "country": { "type": "string", "description": "The country that the city is in, e.g. 'Austria'", }, "unit": { "type": "string", "description": "The unit to fetch the temperature in", "enum": ["celsius", "fahrenheit"], }, "options": { "$ref": "#/$defs/WeatherOptions", "description": "Optional parameters for weather query", }, }, "required": ["country", "unit"], "$defs": { "WeatherOptions": { "title": "WeatherOptions", "type": "object", "additionalProperties": False, "properties": { "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius", "description": "Temperature unit", "title": "Temperature Unit", }, "include_forecast": { "type": "boolean", "default": False, "description": "Whether to include a 24-hour forecast", "title": "Include Forecast", }, "language": { "type": "string", "default": "zh-CN", "description": "Language of the response", "title": "Language", "enum": ["zh-CN", "en-US", "ja-JP"], }, }, }, }, }, }, { "type": "function", "name": "get_forecast", "description": "Get the weather forecast for a given location", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "The city to get the forecast for, e.g. 'Vienna'", "default": "Vienna", }, "country": { "type": "string", "description": "The country that the city is in, e.g. 'Austria'", }, "days": { "type": "integer", "description": "Number of days to get the forecast for (1-7)", }, "unit": { "type": "string", "description": "The unit to fetch the temperature in", "enum": ["celsius", "fahrenheit"], }, }, "required": ["country", "days", "unit"], }, }, ] @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) @pytest.mark.parametrize("tool_choice", ["auto", "required"]) async def test_function_tool_use( client: openai.AsyncOpenAI, model_name: str, tool_choice: str ): prompt = [ { "role": "user", "content": "Can you tell me what the current weather is in Berlin and the " "forecast for the next 5 days, in fahrenheit?", }, ] response = await client.responses.create( model=model_name, input=prompt, tools=tools, tool_choice=tool_choice, temperature=0.0, ) assert len(response.output) >= 1 tool_call = None reasoning = None for out in response.output: if out.type == "function_call": tool_call = out if out.type == "reasoning": reasoning = out assert tool_call is not None assert tool_call.type == "function_call" assert json.loads(tool_call.arguments) is not None assert reasoning is not None assert reasoning.type == "reasoning" @pytest.mark.asyncio async def test_named_tool_use(client: openai.AsyncOpenAI): def get_weather(latitude: float, longitude: float) -> str: """ Mock function to simulate getting weather data. In a real application, this would call an external weather API. """ return f"Current temperature at ({latitude}, {longitude}) is 20°C." tools = [ { "type": "function", "name": "get_weather", "description": ( "Get current temperature for provided coordinates in celsius." ), "parameters": { "type": "object", "properties": { "latitude": {"type": "number"}, "longitude": {"type": "number"}, }, "required": ["latitude", "longitude"], "additionalProperties": False, }, "strict": True, } ] input_messages = [ {"role": "user", "content": "What's the weather like in Paris today?"} ] response = await client.responses.create( model=MODEL_NAME, input=input_messages, tools=tools, tool_choice={"type": "function", "name": "get_weather"}, ) assert len(response.output) >= 1 for out in response.output: if out.type == "function_call": tool_call = out assert tool_call is not None assert tool_call.type == "function_call" assert tool_call.name == "get_weather" args = json.loads(tool_call.arguments) assert args["latitude"] is not None assert args["longitude"] is not None # call the tool result = get_weather(args["latitude"], args["longitude"]) input_messages.append(tool_call) # append model's function call message input_messages.append( { # append result message "type": "function_call_output", "call_id": tool_call.call_id, "output": str(result), } ) # create a new response with the tool call result response_2 = await client.responses.create(model=MODEL_NAME, input=input_messages) # check the output assert len(response_2.output_text) > 0
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/test_structured_output.py
tests/v1/entrypoints/openai/serving_responses/test_structured_output.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import openai import pytest from pydantic import BaseModel @pytest.mark.asyncio async def test_structured_output(client: openai.AsyncOpenAI): response = await client.responses.create( input=[ {"role": "system", "content": "Extract the event information."}, { "role": "user", "content": "Alice and Bob are going to a science fair on Friday.", }, ], text={ "format": { "type": "json_schema", "name": "calendar_event", "schema": { "type": "object", "properties": { "event_name": {"type": "string"}, "date": {"type": "string"}, "participants": {"type": "array", "items": {"type": "string"}}, }, "required": ["event_name", "date", "participants"], "additionalProperties": False, }, "description": "A calendar event.", "strict": True, } }, ) print(response) # NOTE: The JSON schema is applied to the output text, not reasoning. output_text = response.output[-1].content[0].text event = json.loads(output_text) assert event["event_name"].lower() == "science fair" assert event["date"] == "Friday" participants = event["participants"] assert len(participants) == 2 assert participants[0] == "Alice" assert participants[1] == "Bob" @pytest.mark.asyncio async def test_structured_output_with_parse(client: openai.AsyncOpenAI): class CalendarEvent(BaseModel): event_name: str date: str participants: list[str] response = await client.responses.parse( model=None, instructions="Extract the event information.", input="Alice and Bob are going to a science fair on Friday.", text_format=CalendarEvent, ) print(response) # The output is successfully parsed. event = response.output_parsed assert event is not None # The output is correct. assert event.event_name.lower() == "science fair" assert event.date == "Friday" participants = event.participants assert len(participants) == 2 assert participants[0] == "Alice" assert participants[1] == "Bob"
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/conftest.py
tests/v1/entrypoints/openai/serving_responses/conftest.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import pytest_asyncio from tests.utils import RemoteOpenAIServer # Use a small reasoning model to test the responses API. MODEL_NAME = "Qwen/Qwen3-1.7B" @pytest.fixture(scope="module") def default_server_args(): return [ "--max-model-len", "8192", "--enforce-eager", # For faster startup. "--enable-auto-tool-choice", "--structured-outputs-config.backend", "xgrammar", "--tool-call-parser", "hermes", "--reasoning-parser", "qwen3", ] @pytest.fixture(scope="module") def server_with_store(default_server_args): with RemoteOpenAIServer( MODEL_NAME, default_server_args, env_dict={ "VLLM_ENABLE_RESPONSES_API_STORE": "1", "VLLM_SERVER_DEV_MODE": "1", }, ) as remote_server: yield remote_server @pytest_asyncio.fixture async def client(server_with_store): async with server_with_store.get_async_client() as async_client: yield async_client
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/test_image.py
tests/v1/entrypoints/openai/serving_responses/test_image.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import openai import pytest import pytest_asyncio from tests.utils import RemoteOpenAIServer from vllm.multimodal.utils import encode_image_url # Use a small vision model for testing MODEL_NAME = "Qwen/Qwen2.5-VL-3B-Instruct" MAXIMUM_IMAGES = 2 # Test different image extensions (JPG/PNG) and formats (gray/RGB/RGBA) TEST_IMAGE_ASSETS = [ "2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", # "https://vllm-public-assets.s3.us-west-2.amazonaws.com/vision_model_images/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" "Grayscale_8bits_palette_sample_image.png", # "https://vllm-public-assets.s3.us-west-2.amazonaws.com/vision_model_images/Grayscale_8bits_palette_sample_image.png", "1280px-Venn_diagram_rgb.svg.png", # "https://vllm-public-assets.s3.us-west-2.amazonaws.com/vision_model_images/1280px-Venn_diagram_rgb.svg.png", "RGBA_comp.png", # "https://vllm-public-assets.s3.us-west-2.amazonaws.com/vision_model_images/RGBA_comp.png", ] @pytest.fixture(scope="module") def default_image_server_args(): return [ "--enforce-eager", "--max-model-len", "6000", "--max-num-seqs", "128", "--limit-mm-per-prompt", json.dumps({"image": MAXIMUM_IMAGES}), ] @pytest.fixture(scope="module") def image_server(default_image_server_args): with RemoteOpenAIServer( MODEL_NAME, default_image_server_args, env_dict={"VLLM_ENABLE_RESPONSES_API_STORE": "1"}, ) as remote_server: yield remote_server @pytest_asyncio.fixture async def client(image_server): async with image_server.get_async_client() as async_client: yield async_client @pytest.fixture(scope="session") def url_encoded_image(local_asset_server) -> dict[str, str]: return { image_url: encode_image_url(local_asset_server.get_image_asset(image_url)) for image_url in TEST_IMAGE_ASSETS } @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) @pytest.mark.parametrize("image_url", TEST_IMAGE_ASSETS, indirect=True) async def test_single_chat_session_image( client: openai.AsyncOpenAI, model_name: str, image_url: str ): content_text = "What's in this image?" messages = [ { "role": "user", "content": [ { "type": "input_image", "image_url": image_url, "detail": "auto", }, {"type": "input_text", "text": content_text}, ], } ] # test image url response = await client.responses.create( model=model_name, input=messages, ) assert len(response.output_text) > 0 @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) @pytest.mark.parametrize("raw_image_url", TEST_IMAGE_ASSETS) async def test_single_chat_session_image_base64encoded( client: openai.AsyncOpenAI, model_name: str, raw_image_url: str, url_encoded_image: dict[str, str], ): content_text = "What's in this image?" messages = [ { "role": "user", "content": [ { "type": "input_image", "image_url": url_encoded_image[raw_image_url], "detail": "auto", }, {"type": "input_text", "text": content_text}, ], } ] # test image base64 response = await client.responses.create( model=model_name, input=messages, ) assert len(response.output_text) > 0 @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) @pytest.mark.parametrize( "image_urls", [TEST_IMAGE_ASSETS[:i] for i in range(2, len(TEST_IMAGE_ASSETS))], indirect=True, ) async def test_multi_image_input( client: openai.AsyncOpenAI, model_name: str, image_urls: list[str] ): messages = [ { "role": "user", "content": [ *( { "type": "input_image", "image_url": image_url, "detail": "auto", } for image_url in image_urls ), {"type": "input_text", "text": "What's in this image?"}, ], } ] if len(image_urls) > MAXIMUM_IMAGES: with pytest.raises(openai.BadRequestError): # test multi-image input await client.responses.create( model=model_name, input=messages, ) # the server should still work afterwards response = await client.responses.create( model=model_name, input=[ { "role": "user", "content": "What's the weather like in Paris today?", } ], ) assert len(response.output_text) > 0 else: response = await client.responses.create( model=model_name, input=messages, ) assert len(response.output_text) > 0
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/__init__.py
tests/v1/entrypoints/openai/serving_responses/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/test_stateful.py
tests/v1/entrypoints/openai/serving_responses/test_stateful.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import openai import pytest @pytest.mark.asyncio async def test_store(client: openai.AsyncOpenAI): # By default, store is True. response = await client.responses.create(input="Hello!") assert response.status == "completed" # Retrieve the response. response = await client.responses.retrieve(response.id) assert response.status == "completed" # Test store=False. response = await client.responses.create( input="Hello!", store=False, ) assert response.status == "completed" # The response should not be found. with pytest.raises(openai.NotFoundError, match="Response with id .* not found."): await client.responses.retrieve(response.id) @pytest.mark.asyncio async def test_background(client: openai.AsyncOpenAI): # NOTE: This query should be easy enough for the model to answer # within the 10 seconds. response = await client.responses.create( input="Hello!", background=True, ) assert response.status == "queued" max_retries = 10 for _ in range(max_retries): await asyncio.sleep(1) response = await client.responses.retrieve(response.id) if response.status != "queued": break print(response) assert response.status == "completed" @pytest.mark.asyncio async def test_background_error(client: openai.AsyncOpenAI): with pytest.raises( openai.BadRequestError, match="background can only be used when `store` is true" ): _ = await client.responses.create( input="What is 13 * 24?", background=True, store=False, ) @pytest.mark.asyncio async def test_background_cancel(client: openai.AsyncOpenAI): response = await client.responses.create( input="Write a long story about a cat.", background=True, ) assert response.status == "queued" # Cancel the response before it is completed. # FIXME: This test can be flaky. await asyncio.sleep(0.5) response = await client.responses.cancel(response.id) assert response.status == "cancelled" # Make sure the response status remains unchanged. await asyncio.sleep(5) response = await client.responses.retrieve(response.id) assert response.status == "cancelled" @pytest.mark.asyncio async def test_cancel_completed(client: openai.AsyncOpenAI): response = await client.responses.create(input="Hello") assert response.status == "completed" with pytest.raises( openai.BadRequestError, match="Cannot cancel a synchronous response." ): await client.responses.cancel(response.id) @pytest.mark.asyncio async def test_previous_response_id(client: openai.AsyncOpenAI): response1 = await client.responses.create( instructions="You are tested on your ability to retrieve the correct " "information from the previous response.", input="Hello, my name is John.", ) response2 = await client.responses.create( input="Actually, my name is not John. My real name is Mark.", previous_response_id=response1.id, ) response3 = await client.responses.create( input="What is my real name again? Answer in one word.", previous_response_id=response2.id, ) print(response3) assert "Mark" in response3.output[-1].content[0].text assert "John" not in response3.output[-1].content[0].text @pytest.mark.asyncio async def test_two_responses_with_same_prev_id(client: openai.AsyncOpenAI): response1 = await client.responses.create( instructions="You are tested on your ability to retrieve the correct " "information from the previous response.", input="Hello, my name is John.", ) # Both response 2 and 3 use response 1 as the previous response. response2 = client.responses.create( input="Actually, my name is not John. My name is Mark.", previous_response_id=response1.id, ) response3 = client.responses.create( input="What is my name again? Answer in one word.", previous_response_id=response1.id, ) _ = await response2 response3_result = await response3 print(response3_result) assert "John" in response3_result.output[-1].content[0].text assert "Mark" not in response3_result.output[-1].content[0].text
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/openai/serving_responses/test_basic.py
tests/v1/entrypoints/openai/serving_responses/test_basic.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import openai # use the official client for correctness check import openai.types.responses as openai_responses_types import pytest @pytest.mark.asyncio async def test_simple_input(client: openai.AsyncOpenAI): response = await client.responses.create(input="What is 13 * 24?") print(response) outputs = response.output # Whether the output contains the answer. assert outputs[-1].type == "message" assert "312" in outputs[-1].content[0].text # Whether the output contains the reasoning. assert outputs[0].type == "reasoning" assert outputs[0].content[0].text != "" @pytest.mark.asyncio async def test_instructions(client: openai.AsyncOpenAI): response = await client.responses.create( instructions="Finish the answer with QED.", input="What is 13 * 24?", ) print(response) output_text = response.output[-1].content[0].text assert "312" in output_text assert "QED" in output_text @pytest.mark.asyncio async def test_chat(client: openai.AsyncOpenAI): response = await client.responses.create( input=[ {"role": "system", "content": "Finish the answer with QED."}, {"role": "user", "content": "What is 5 * 3?"}, {"role": "assistant", "content": "15. QED."}, {"role": "user", "content": "Multiply the result by 2."}, ], ) print(response) output_text = response.output[-1].content[0].text assert "30" in output_text assert "QED" in output_text @pytest.mark.asyncio async def test_chat_with_input_type(client: openai.AsyncOpenAI): response = await client.responses.create( input=[ { "role": "user", "content": [{"type": "input_text", "text": "Hello!"}], }, ], ) print(response) assert response.status == "completed" @pytest.mark.asyncio async def test_logprobs(client: openai.AsyncOpenAI): response = await client.responses.create( include=["message.output_text.logprobs"], input="What is 13 * 24?", top_logprobs=5, ) print(response) outputs = response.output assert outputs[-1].content[-1].logprobs assert len(outputs[-1].content[-1].logprobs[0].top_logprobs) == 5 @pytest.mark.asyncio async def test_streaming(client: openai.AsyncOpenAI): stream = await client.responses.create( input="What is 13 * 24?", stream=True, ) events = [event async for event in stream] assert isinstance(events[0], openai_responses_types.ResponseCreatedEvent) assert any( isinstance(event, openai_responses_types.ResponseTextDeltaEvent) for event in events ) assert isinstance(events[-1], openai_responses_types.ResponseCompletedEvent)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/llm/__init__.py
tests/v1/entrypoints/llm/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/entrypoints/llm/test_struct_output_generate.py
tests/v1/entrypoints/llm/test_struct_output_generate.py
# ruff: noqa: E501 # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json from enum import Enum from typing import Any import jsonschema import pytest import regex as re import torch from pydantic import BaseModel from tests.reasoning.utils import run_reasoning_extraction from vllm.config import StructuredOutputsConfig from vllm.distributed import cleanup_dist_env_and_memory from vllm.entrypoints.llm import LLM from vllm.outputs import RequestOutput from vllm.platforms import current_platform from vllm.reasoning.abs_reasoning_parsers import ReasoningParserManager from vllm.sampling_params import ( SamplingParams, StructuredOutputsParams, ) NGRAM_SPEC_CONFIG = { "model": "[ngram]", "num_speculative_tokens": 5, "prompt_lookup_max": 5, "prompt_lookup_min": 1, } EAGLE_SPEC_CONFIG = { "method": "eagle", "model": "yuhuili/EAGLE-LLaMA3.1-Instruct-8B", "num_speculative_tokens": 5, } PARAMS_MODELS_BACKENDS_TOKENIZER_MODE = [ ("mistralai/Ministral-8B-Instruct-2410", "xgrammar", "auto", None), # FIXME: Since "auto" will use Mistral tokenizer and these backends do not support # it, we skip these tests for now. # ("mistralai/Ministral-8B-Instruct-2410", "guidance", "auto", None), # ("mistralai/Ministral-8B-Instruct-2410", "lm-format-enforcer", "auto", None), ("mistralai/Ministral-8B-Instruct-2410", "guidance", "hf", None), pytest.param( "mistralai/Ministral-8B-Instruct-2410", "lm-format-enforcer", "hf", None, marks=pytest.mark.skip( reason=( "Flaky: lm-format-enforcer intermittently returns" "incomplete JSON." "See https://github.com/noamgat/lm-format-enforcer/issues/169" ) ), ), ("mistralai/Ministral-8B-Instruct-2410", "xgrammar", "mistral", None), ("Qwen/Qwen2.5-1.5B-Instruct", "xgrammar", "auto", None), pytest.param( "Qwen/Qwen2.5-1.5B-Instruct", "lm-format-enforcer", "auto", None, marks=pytest.mark.skip( reason=( "Flaky: lm-format-enforcer intermittently returns" "incomplete JSON." "See https://github.com/noamgat/lm-format-enforcer/issues/169" ) ), ), # FIXME: This tests are flaky on CI thus disabled. Tracking in Issue #24402 # ("mistralai/Ministral-8B-Instruct-2410", "outlines", "auto", None), # ("mistralai/Ministral-8B-Instruct-2410", "outlines", "mistral", None), # ("Qwen/Qwen2.5-1.5B-Instruct", "guidance", "auto"), ("mistralai/Ministral-8B-Instruct-2410", "outlines", "auto", NGRAM_SPEC_CONFIG), ("mistralai/Ministral-8B-Instruct-2410", "guidance", "hf", NGRAM_SPEC_CONFIG), ("Qwen/Qwen2.5-1.5B-Instruct", "xgrammar", "auto", NGRAM_SPEC_CONFIG), ("meta-llama/Meta-Llama-3.1-8B-Instruct", "xgrammar", "auto", EAGLE_SPEC_CONFIG), ] PARAMS_MODELS_TOKENIZER_MODE = [ ("mistralai/Ministral-8B-Instruct-2410", "auto"), ("Qwen/Qwen2.5-1.5B-Instruct", "auto"), ] class CarType(str, Enum): sedan = "sedan" suv = "SUV" truck = "Truck" coupe = "Coupe" class CarDescription(BaseModel): brand: str model: str car_type: CarType @pytest.mark.parametrize( "model_name, backend, tokenizer_mode, speculative_config", PARAMS_MODELS_BACKENDS_TOKENIZER_MODE, ) def test_structured_output( sample_json_schema: dict[str, Any], unsupported_json_schema: dict[str, Any], sample_sql_ebnf: str, sample_sql_lark: str, sample_regex: str, sample_structured_outputs_choices: str, backend: str, tokenizer_mode: str, model_name: str, speculative_config: dict[str, Any], ): if current_platform.is_tpu() and speculative_config: pytest.skip("TPU does not support speculative decoding") # Use a single LLM instance for several scenarios to # speed up the test suite. llm = LLM( model=model_name, enforce_eager=True, max_model_len=1024, structured_outputs_config=dict( backend=backend, disable_any_whitespace=backend in {"xgrammar", "guidance"} ), seed=120, tokenizer_mode=tokenizer_mode, load_format="auto" if not model_name.startswith("mistralai/") else "hf", config_format="auto" if not model_name.startswith("mistralai/") else "hf", speculative_config=speculative_config, ) # # Test 1: Generate JSON output based on a provided schema # sampling_params = SamplingParams( temperature=1.0, max_tokens=4096, structured_outputs=StructuredOutputsParams(json=sample_json_schema), ) prompt = ( "Give an example JSON for an employee profile that fits this " "schema. Make the response as short as possible. Schema: " f"{sample_json_schema}" ) outputs = llm.generate( [prompt] * 2, sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None if backend != "lm-format-enforcer": assert "\n" not in generated_text print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") try: output_json = json.loads(generated_text) except json.JSONDecodeError as e: pytest.fail( f"Invalid JSON from backend={backend}: {generated_text!r}\n" f"Schema: {sample_json_schema}\nError: {e}" ) jsonschema.validate(instance=output_json, schema=sample_json_schema) # # Test 2: Generate JSON object without a schema # if backend != "outlines": sampling_params = SamplingParams( temperature=1.0, max_tokens=4096, n=2, structured_outputs=StructuredOutputsParams(json_object=True), ) outputs = llm.generate( prompts=( "Generate a JSON object with curly braces for a person with " "name and age fields for John Smith who is 31 years old. " "Make the response as short as possible." ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) for i in range(2): generated_text = output.outputs[i].text print(generated_text) assert generated_text is not None # Parse to verify it is a valid JSON object parsed_json = json.loads(generated_text) assert isinstance(parsed_json, dict) # # Test 3: test a jsonschema incompatible with xgrammar # sampling_params = SamplingParams( temperature=1.0, max_tokens=4096, structured_outputs=StructuredOutputsParams(json=unsupported_json_schema), ) if backend.startswith("xgrammar"): with pytest.raises( ValueError, match="The provided JSON schema contains features " "not supported by xgrammar.", ): prompt = ( f"Give an example JSON for an employee profile that " f"fits this schema: {unsupported_json_schema}. " f"Make the response as short as possible." ) llm.generate( [prompt] * 2, sampling_params=sampling_params, use_tqdm=True, ) else: prompt = ( f"Give an example JSON object for a grade that " f"fits this schema: {unsupported_json_schema}. " f"Make the response as short as possible." ) outputs = llm.generate( prompt, sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) generated_text = output.outputs[0].text assert generated_text is not None print(generated_text) # Parse to verify it is valid JSON parsed_json = json.loads(generated_text) assert isinstance(parsed_json, dict) if backend not in ["outlines", "lm-format-enforcer"]: # # Test 4: Generate SQL statement using EBNF grammar # sampling_params = SamplingParams( temperature=0.8, top_p=0.95, max_tokens=1000, structured_outputs=StructuredOutputsParams(grammar=sample_sql_ebnf), ) outputs = llm.generate( ( "Generate a sql statement that selects col_1 from " "table_1 where it is equal to 1. Make the response as short as " "possible." ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None # remove spaces for comparison b/c we removed them in the grammar ground_truth = "SELECT col_1 from table_1 where col_1 = 1".replace(" ", "") assert generated_text.strip() == ground_truth print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") # # Test 5: Generate SQL statement using Lark grammar # sampling_params = SamplingParams( temperature=0.8, top_p=0.95, max_tokens=1000, structured_outputs=StructuredOutputsParams(grammar=sample_sql_lark), ) outputs = llm.generate( ( "Generate a sql statement that selects col_1 from " "table_1 where it is equal to 1. Make the response as short as " "possible." ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None # use Lark to parse the output, and make sure it's a valid parse tree from lark import Lark parser = Lark(sample_sql_lark) parser.parse(generated_text) # remove spaces for comparison b/c we removed them in the grammar ground_truth = "SELECT col_1 from table_1 where col_1 = 1".replace(" ", "") assert generated_text.strip() == ground_truth print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") # # Test 6: Test invalid grammar input # sampling_params = SamplingParams( temperature=0.8, top_p=0.95, max_tokens=1000, structured_outputs=StructuredOutputsParams(grammar="not a grammar"), ) with pytest.raises(ValueError, match="Failed to convert the grammar "): llm.generate( ( "Generate a sql statement that selects col_1 from " "table_1 where it is equal to 1. Make the response as short " "as possible." ), sampling_params=sampling_params, use_tqdm=True, ) # # Test 7: Generate text based on a regex pattern # sampling_params = SamplingParams( temperature=0.8, top_p=0.95, structured_outputs=StructuredOutputsParams(regex=sample_regex), ) prompt = ( f"Give an example IPv4 address with this regex: {sample_regex}. " f"Make the response as short as possible." ) outputs = llm.generate( [prompt] * 2, sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text print(generated_text) assert generated_text is not None assert re.fullmatch(sample_regex, generated_text) is not None print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") # # Test 8: Generate text based on a choices # sampling_params = SamplingParams( temperature=0.8, top_p=0.95, structured_outputs=StructuredOutputsParams( choice=sample_structured_outputs_choices ), ) outputs = llm.generate( ( "The best language for type-safe systems programming is " "(Make the response as short as possible.) " ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text print(generated_text) assert generated_text is not None assert generated_text in sample_structured_outputs_choices print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") # # Test 9: Generate structured output using a Pydantic model with an enum # json_schema = CarDescription.model_json_schema() sampling_params = SamplingParams( temperature=1.0, max_tokens=1000, structured_outputs=StructuredOutputsParams(json=json_schema), ) outputs = llm.generate( ( "Generate a JSON with the brand, model and car_type of the most " "iconic car from the 90's. Make the response as short as " "possible." ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") try: output_json = json.loads(generated_text) except json.JSONDecodeError as e: pytest.fail( f"Invalid JSON from backend={backend}: {generated_text!r}\n" f"Schema: {json_schema}\nError: {e}" ) jsonschema.validate(instance=output_json, schema=json_schema) # # Test 10: Generate structured with minLength and maxLength # min_length = 50 max_length = 50 json_schema = { "type": "object", "properties": { "description": { "type": "string", "maxLength": max_length, "minLength": min_length, } }, "required": ["description"], "additionalProperties": False, } sampling_params = SamplingParams( temperature=1.0, max_tokens=4096, structured_outputs=StructuredOutputsParams(json=json_schema), ) outputs = llm.generate( ( "Generate a description of a frog using 50 characters. " "Make the response as short as possible." ), sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") try: output_json = json.loads(generated_text) except json.JSONDecodeError as e: pytest.fail( f"Invalid JSON from backend={backend}: {generated_text!r}\n" f"Schema: {json_schema}\nError: {e}" ) jsonschema.validate(instance=output_json, schema=json_schema) if backend not in ["outlines", "lm-format-enforcer"]: # # Test 11: Generate structured output using structural_tag format # structural_tag_config = { "type": "structural_tag", "structures": [ { "begin": "<function=get_weather>", "schema": { "type": "object", "properties": {"city": {"type": "string"}}, "additionalProperties": False, }, "end": "</function>", } ], "triggers": ["<function="], } sampling_params = SamplingParams( temperature=0.0, max_tokens=4096, structured_outputs=StructuredOutputsParams( structural_tag=json.dumps(structural_tag_config) ), ) prompt = """ You have access to the following function to retrieve the weather in a city: { "name": "get_weather", "parameters": { "city": { "param_type": "string", "description": "The city to get the weather for", "required": True } } } If a you choose to call a function ONLY reply in the following format: <{start_tag}={function_name}>{parameters}{end_tag} where start_tag => `<function` parameters => a JSON dict with the function argument name as key and function argument value as value. end_tag => `</function>` Here is an example, <function=example_function_name>{"example_name": "example_value"}</function> Reminder: - Function calls MUST follow the specified format - Required parameters MUST be specified - Only call one function at a time - Put the entire function call reply on one line - Always add your sources when using search results to answer the user query You are a helpful assistant. Given the previous instructions, what is the weather in New York City? \ Make the response as short as possible. """ # Change this once other backends support structural_tag outputs = llm.generate(prompt, sampling_params=sampling_params, use_tqdm=True) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) generated_text = output.outputs[0].text assert generated_text is not None # Search for function call pattern in the response function_call_pattern = r"<function=get_weather>(.*?)</function>" matches = re.findall(function_call_pattern, generated_text) if not matches: print( f"Warning: No function calls found in response: {generated_text!r}" ) continue # Take the first function call if multiple are found json_str = matches[0] try: json_content = json.loads(json_str) assert "city" in json_content assert isinstance(json_content["city"], str) print(f"Found valid function call: {generated_text!r}") except (json.JSONDecodeError, AssertionError) as e: pytest.fail( f"Invalid function call format: {generated_text!r}\nError: {str(e)}" ) @pytest.mark.parametrize( "model_name, backend, tokenizer_mode, reasoning_parser, speculative_config, async_scheduling", # noqa: E501 [ ( "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", "xgrammar", "auto", "deepseek_r1", NGRAM_SPEC_CONFIG, False, ), ("Qwen/Qwen3-1.7B", "xgrammar", "auto", "deepseek_r1", None, False), ("Qwen/Qwen3-1.7B", "xgrammar", "auto", "deepseek_r1", None, True), ], ) def test_structured_output_with_reasoning_matrices( backend: str, tokenizer_mode: str, reasoning_parser: str, model_name: str, speculative_config: dict[str, Any] | None, async_scheduling: bool, ): if current_platform.is_tpu() and speculative_config: pytest.skip("TPU does not support speculative decoding") # Use a single LLM instance for several scenarios to # speed up the test suite. llm = LLM( model=model_name, # Don't use eager execution on TPUs because we want to test for no # recompilation at runtime enforce_eager=bool(not current_platform.is_tpu()), max_model_len=1024, max_num_seqs=16, structured_outputs_config=dict( backend=backend, disable_any_whitespace=backend in {"xgrammar", "guidance"}, reasoning_parser=reasoning_parser, ), tokenizer_mode=tokenizer_mode, speculative_config=speculative_config, async_scheduling=async_scheduling, ) tokenizer = llm.get_tokenizer() reasoner = ReasoningParserManager.get_reasoning_parser(reasoning_parser)( tokenizer=tokenizer ) reasoning_prompt = "Solve the following math problem step-by-step, then provide the final answer as JSON object with a single key 'result'. Make sure to correct your reasoning if there are any issue should it arise.\nProblem: What is 5 * 8 + 2?" # noqa: E501 reasoning_schema = { "type": "object", "properties": {"result": {"type": "integer"}}, "required": ["result"], "additionalProperties": False, } if "Qwen3" in model_name: reasoning_prompt += "<think>\n" sampling_params = SamplingParams( temperature=0.1, max_tokens=8192, structured_outputs=StructuredOutputsParams(json=reasoning_schema), ) outputs = llm.generate( [reasoning_prompt], sampling_params=sampling_params, use_tqdm=True, ) assert outputs is not None output = outputs[0] assert output is not None and isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text reasoning, content = run_reasoning_extraction(reasoner, [generated_text]) print(f"Prompt: {prompt!r}\nReasoning: {reasoning!r}\nContent: {content!r}") if "Qwen3" in model_name: assert content is not None assert reasoning is not None if content is not None: output_json = json.loads(content) jsonschema.validate(instance=output_json, schema=reasoning_schema) @pytest.mark.parametrize("model_name, tokenizer_mode", PARAMS_MODELS_TOKENIZER_MODE) def test_structured_output_auto_mode( unsupported_json_schema: dict[str, Any], model_name: str, tokenizer_mode: str, ): llm = LLM( model=model_name, max_model_len=1024, structured_outputs_config=dict(backend="auto"), tokenizer_mode=tokenizer_mode, load_format="auto", config_format="auto", ) sampling_params = SamplingParams( temperature=1.0, max_tokens=1000, structured_outputs=StructuredOutputsParams(json=unsupported_json_schema), ) prompts = ( "Give an example JSON object for a grade " "that fits this schema: " f"{unsupported_json_schema}. Make the response as short as possible." ) # This would fail with the default of "xgrammar", but in "auto" # we will handle fallback automatically. outputs = llm.generate(prompts, sampling_params=sampling_params, use_tqdm=True) # Make sure `auto` backend handling doesn't mess up sampling_params # and that we can reuse it without error. outputs.extend( llm.generate(prompts, sampling_params=sampling_params, use_tqdm=True) ) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) generated_text = output.outputs[0].text assert generated_text is not None print(generated_text) # Parse to verify it is valid JSON parsed_json = json.loads(generated_text) assert isinstance(parsed_json, dict) def test_guidance_no_additional_properties(): llm = LLM( model="Qwen/Qwen2.5-1.5B-Instruct", max_model_len=1024, structured_outputs_config=dict( backend="guidance", disable_any_whitespace=True, disable_additional_properties=True, ), ) schema = { "type": "object", "properties": { "a1": {"type": "string"}, "a2": {"type": "string"}, "a3": {"type": "string"}, }, "required": ["a1", "a2", "a3"], } prompt = ( "<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a " "helpful assistant.<|im_end|>\n<|im_start|>user\nPlease generate a " "large JSON object with key-value pairs a1=b1, a2=b2, ..., a20=b20. " "Make the response as short as possible." "<|im_end|>\n<|im_start|>assistant\n" ) def generate_with_backend(backend): structured_outputs_params = StructuredOutputsParams( json=schema, backend=backend, disable_any_whitespace=True, disable_additional_properties=True, ) sampling_params = SamplingParams( temperature=0, max_tokens=256, structured_outputs=structured_outputs_params ) outputs = llm.generate(prompt, sampling_params=sampling_params) assert outputs is not None generated_text = outputs[0].outputs[0].text assert generated_text is not None parsed_json = json.loads(generated_text) assert isinstance(parsed_json, dict) jsonschema.validate(instance=parsed_json, schema=schema) return parsed_json generated = generate_with_backend("guidance") assert "a1" in generated assert "a2" in generated assert "a3" in generated assert "a4" not in generated assert "a5" not in generated assert "a6" not in generated @pytest.mark.parametrize("backend", ["guidance", "xgrammar", "outlines"]) def test_structured_output_batched_with_non_structured_outputs_requests( sample_json_schema: dict[str, Any], backend: str, ): # Don't use eager execution on TPUs because we want to test for no # recompilation at runtime enforce_eager = bool(not current_platform.is_tpu()) llm = LLM( model="meta-llama/Meta-Llama-3.1-8B-Instruct", enforce_eager=enforce_eager, max_model_len=1024, structured_outputs_config=StructuredOutputsConfig( backend=backend, disable_any_whitespace=backend in {"xgrammar", "guidance"}, ), ) structured_outputs_prompt = ( "Give an example JSON for an employee profile that fits this " "schema. Make the response as short as possible. Schema: " f"{sample_json_schema}" ) non_structured_outputs_prompt = "The diameter of the Earth in kilometers is " prompts = [structured_outputs_prompt, non_structured_outputs_prompt] sampling_params = [ SamplingParams( temperature=1.0, max_tokens=400, structured_outputs=StructuredOutputsParams(json=sample_json_schema), ), # No max tokens, temp=0 to assert on contents SamplingParams( seed=42, temperature=0, top_p=1.0, ), ] outputs = llm.generate( prompts=prompts, sampling_params=sampling_params, use_tqdm=True ) assert outputs is not None # Free memory as soon as possible as failed assertions # will short circuit and not free up memory del llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() for index, output in enumerate(outputs): assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None print(f"Prompt:\n{prompt!r}\nGenerated text:\n{generated_text!r}") if index == 0: # First prompt is structured outputs, expect valid JSON assert "\n" not in generated_text output_json = json.loads(generated_text) jsonschema.validate(instance=output_json, schema=sample_json_schema) else: # Second prompt is not structured outputs, expect valid output # Cannot assert on exact output, but we can expect it to be factual assert "12,742" in generated_text # non-structured outputs requests should not return a valid JSON here with pytest.raises(ValueError): output_json = json.loads(generated_text) @pytest.mark.parametrize("backend", ["xgrammar"]) def test_structured_output_with_structural_tag(backend: str): llm = LLM( model="Qwen/Qwen2.5-1.5B-Instruct", structured_outputs_config=StructuredOutputsConfig(backend=backend), ) structural_tag_config = { "type": "structural_tag", "format": { "type": "triggered_tags", "tags": [ {"begin": "hello_flag", "content": {"type": "any_text"}, "end": "hello"} ], "triggers": ["hello"], "stop_after_first": False, }, } sampling_params = SamplingParams( temperature=0.0, max_tokens=500, structured_outputs=StructuredOutputsParams( structural_tag=json.dumps(structural_tag_config) ), ) prompt = "Hello and repete hello 10 times, do not say anything else. Only say hello hello hello, now start" outputs = llm.generate(prompt, sampling_params=sampling_params, use_tqdm=True) assert outputs is not None for output in outputs: assert output is not None assert isinstance(output, RequestOutput) prompt = output.prompt generated_text = output.outputs[0].text assert generated_text is not None assert "hello_flag" in generated_text, ( f"Expected 'hello_flag' to be in generated text, but got: {generated_text}" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tracing/__init__.py
tests/v1/tracing/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/tracing/test_tracing.py
tests/v1/tracing/test_tracing.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # ruff: noqa # type: ignore import threading from collections.abc import Iterable from concurrent import futures from typing import Callable, Generator, Literal import grpc import pytest from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ( ExportTraceServiceResponse, ) from opentelemetry.proto.collector.trace.v1.trace_service_pb2_grpc import ( TraceServiceServicer, add_TraceServiceServicer_to_server, ) from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_TRACES_INSECURE from vllm import LLM, SamplingParams from vllm.tracing import SpanAttributes FAKE_TRACE_SERVER_ADDRESS = "localhost:4317" FieldName = Literal[ "bool_value", "string_value", "int_value", "double_value", "array_value" ] def decode_value(value: AnyValue): field_decoders: dict[FieldName, Callable] = { "bool_value": (lambda v: v.bool_value), "string_value": (lambda v: v.string_value), "int_value": (lambda v: v.int_value), "double_value": (lambda v: v.double_value), "array_value": ( lambda v: [decode_value(item) for item in v.array_value.values] ), } for field, decoder in field_decoders.items(): if value.HasField(field): return decoder(value) raise ValueError(f"Couldn't decode value: {value}") def decode_attributes(attributes: Iterable[KeyValue]): return {kv.key: decode_value(kv.value) for kv in attributes} class FakeTraceService(TraceServiceServicer): def __init__(self): self.request = None self.evt = threading.Event() def Export(self, request, context): self.request = request self.evt.set() return ExportTraceServiceResponse() @pytest.fixture def trace_service() -> Generator[FakeTraceService, None, None]: """Fixture to set up a fake gRPC trace service""" server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) service = FakeTraceService() add_TraceServiceServicer_to_server(service, server) server.add_insecure_port(FAKE_TRACE_SERVER_ADDRESS) server.start() yield service server.stop(None) def test_traces( monkeypatch: pytest.MonkeyPatch, trace_service: FakeTraceService, ): with monkeypatch.context() as m: m.setenv(OTEL_EXPORTER_OTLP_TRACES_INSECURE, "true") sampling_params = SamplingParams( temperature=0.01, top_p=0.1, max_tokens=256, ) model = "facebook/opt-125m" llm = LLM( model=model, otlp_traces_endpoint=FAKE_TRACE_SERVER_ADDRESS, gpu_memory_utilization=0.3, disable_log_stats=False, ) prompts = ["This is a short prompt"] outputs = llm.generate(prompts, sampling_params=sampling_params) print(f"test_traces outputs is : {outputs}") timeout = 10 if not trace_service.evt.wait(timeout): raise TimeoutError( f"The fake trace service didn't receive a trace within " f"the {timeout} seconds timeout" ) request = trace_service.request assert len(request.resource_spans) == 1, ( f"Expected 1 resource span, but got {len(request.resource_spans)}" ) assert len(request.resource_spans[0].scope_spans) == 1, ( f"Expected 1 scope span, " f"but got {len(request.resource_spans[0].scope_spans)}" ) assert len(request.resource_spans[0].scope_spans[0].spans) == 1, ( f"Expected 1 span, " f"but got {len(request.resource_spans[0].scope_spans[0].spans)}" ) attributes = decode_attributes( request.resource_spans[0].scope_spans[0].spans[0].attributes ) # assert attributes.get(SpanAttributes.GEN_AI_RESPONSE_MODEL) == model assert attributes.get(SpanAttributes.GEN_AI_REQUEST_ID) == outputs[0].request_id assert ( attributes.get(SpanAttributes.GEN_AI_REQUEST_TEMPERATURE) == sampling_params.temperature ) assert ( attributes.get(SpanAttributes.GEN_AI_REQUEST_TOP_P) == sampling_params.top_p ) assert ( attributes.get(SpanAttributes.GEN_AI_REQUEST_MAX_TOKENS) == sampling_params.max_tokens ) assert attributes.get(SpanAttributes.GEN_AI_REQUEST_N) == sampling_params.n assert attributes.get(SpanAttributes.GEN_AI_USAGE_PROMPT_TOKENS) == len( outputs[0].prompt_token_ids ) completion_tokens = sum(len(o.token_ids) for o in outputs[0].outputs) assert ( attributes.get(SpanAttributes.GEN_AI_USAGE_COMPLETION_TOKENS) == completion_tokens ) assert attributes.get(SpanAttributes.GEN_AI_LATENCY_TIME_IN_QUEUE) > 0 assert attributes.get(SpanAttributes.GEN_AI_LATENCY_TIME_TO_FIRST_TOKEN) > 0 assert attributes.get(SpanAttributes.GEN_AI_LATENCY_E2E) > 0
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/shutdown/test_processor_error.py
tests/v1/shutdown/test_processor_error.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Test error handling in Processor. Should not impact other reqs.""" import asyncio import pytest from tests.v1.shutdown.utils import SHUTDOWN_TEST_TIMEOUT_SEC from vllm import SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.inputs.data import TokensPrompt from vllm.sampling_params import RequestOutputKind from vllm.v1.engine.async_llm import AsyncLLM from vllm.v1.engine.exceptions import EngineGenerateError MODELS = ["meta-llama/Llama-3.2-1B"] @pytest.mark.asyncio @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("model", MODELS) async def test_async_llm_processor_error(model: str) -> None: """Test that AsyncLLM propagates a processor error. Test empty tokens prompt (failure) and non-empty prompt (no failure.) AsyncLLM always uses an MP client. """ engine_args = AsyncEngineArgs(model=model, enforce_eager=True) async_llm = AsyncLLM.from_engine_args(engine_args) async def generate(request_id: str): # [] is not allowed and will raise a ValueError in Processor. generator = async_llm.generate( TokensPrompt([]), request_id=request_id, sampling_params=SamplingParams() ) try: async for _ in generator: pass except Exception as e: return e NUM_REQS = 3 tasks = [generate(f"request-{idx}") for idx in range(NUM_REQS)] outputs = await asyncio.gather(*tasks) # Every request should have get an EngineGenerateError. for output in outputs: with pytest.raises(EngineGenerateError): raise output # AsyncLLM should be errored. assert not async_llm.errored # This should be no problem. EXPECTED_TOKENS = 5 outputs = [] async for out in async_llm.generate( "Hello my name is", request_id="abc", sampling_params=SamplingParams( max_tokens=EXPECTED_TOKENS, output_kind=RequestOutputKind.DELTA ), ): outputs.append(out) generated_tokens = [] for out in outputs: generated_tokens.extend(out.outputs[0].token_ids) assert len(generated_tokens) == EXPECTED_TOKENS async_llm.shutdown()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/shutdown/test_forward_error.py
tests/v1/shutdown/test_forward_error.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Test that we handle an Error in model forward and shutdown.""" import asyncio import pytest from tests.utils import wait_for_gpu_memory_to_clear from tests.v1.shutdown.utils import ( SHUTDOWN_TEST_THRESHOLD_BYTES, SHUTDOWN_TEST_TIMEOUT_SEC, ) from vllm import LLM, AsyncEngineArgs, SamplingParams from vllm.distributed import get_tensor_model_parallel_rank from vllm.model_executor.models.llama import LlamaForCausalLM from vllm.utils.torch_utils import cuda_device_count_stateless from vllm.v1.engine.async_llm import AsyncLLM from vllm.v1.engine.exceptions import EngineDeadError MODELS = ["hmellor/tiny-random-LlamaForCausalLM"] def evil_forward(self, *args, **kwargs): """Evil forward method that raise an exception after 10 calls.""" NUMBER_OF_GOOD_PASSES = 10 if not hasattr(self, "num_calls"): self.num_calls = 0 if ( self.num_calls == NUMBER_OF_GOOD_PASSES and get_tensor_model_parallel_rank() == 0 ): raise Exception("Simulated illegal memory access on Rank 0!") self.num_calls += 1 return self.model(*args, **kwargs) @pytest.mark.asyncio @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("model", MODELS) async def test_async_llm_model_error( monkeypatch, tensor_parallel_size: int, model: str ) -> None: """Test that AsyncLLM propagates a forward pass error and frees memory. AsyncLLM always uses an MP client. """ if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") # Monkeypatch an error in the model. monkeypatch.setattr(LlamaForCausalLM, "forward", evil_forward) engine_args = AsyncEngineArgs( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size ) async_llm = AsyncLLM.from_engine_args(engine_args) async def generate(request_id: str): generator = async_llm.generate( "Hello my name is", request_id=request_id, sampling_params=SamplingParams() ) try: async for _ in generator: pass except Exception as e: return e NUM_REQS = 3 tasks = [generate(f"request-{idx}") for idx in range(NUM_REQS)] outputs = await asyncio.gather(*tasks) # Every request should get an EngineDeadError. for output in outputs: assert isinstance(output, EngineDeadError) # AsyncLLM should be errored. assert async_llm.errored # We should not be able to make another request. with pytest.raises(EngineDeadError): async for _ in async_llm.generate( "Hello my name is", request_id="abc", sampling_params=SamplingParams() ): raise Exception("We should not get here.") # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=2 * 2**30, timeout_s=60, ) # NOTE: shutdown is handled by the API Server if an exception # occurs, so it is expected that we would need to call this. async_llm.shutdown() @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("enable_multiprocessing", [True]) @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("model", MODELS) def test_llm_model_error( monkeypatch, tensor_parallel_size: int, enable_multiprocessing: bool, model: str ) -> None: """Test that LLM propagates a forward pass error and frees memory. TODO(andy) - LLM without multiprocessing; LLM with multiprocessing and >1 rank """ if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") with monkeypatch.context() as m: MP_VALUE = "1" if enable_multiprocessing else "0" m.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", MP_VALUE) # Monkeypatch an error in the model. m.setattr(LlamaForCausalLM, "forward", evil_forward) llm = LLM( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size ) with pytest.raises(EngineDeadError if enable_multiprocessing else Exception): llm.generate("Hello my name is Robert and I") # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=SHUTDOWN_TEST_THRESHOLD_BYTES, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/shutdown/test_delete.py
tests/v1/shutdown/test_delete.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Test that we handle a startup Error and shutdown.""" import pytest from tests.utils import wait_for_gpu_memory_to_clear from tests.v1.shutdown.utils import ( SHUTDOWN_TEST_THRESHOLD_BYTES, SHUTDOWN_TEST_TIMEOUT_SEC, ) from vllm import LLM, SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.sampling_params import RequestOutputKind from vllm.utils.torch_utils import cuda_device_count_stateless from vllm.v1.engine.async_llm import AsyncLLM MODELS = ["hmellor/tiny-random-LlamaForCausalLM"] @pytest.mark.asyncio @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("send_one_request", [False, True]) async def test_async_llm_delete( model: str, tensor_parallel_size: int, send_one_request: bool ) -> None: """Test that AsyncLLM frees GPU memory upon deletion. AsyncLLM always uses an MP client. Args: model: model under test tensor_parallel_size: degree of tensor parallelism send_one_request: send one request to engine before deleting """ if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") engine_args = AsyncEngineArgs( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size ) # Instantiate AsyncLLM; make request to complete any deferred # initialization; then delete instance async_llm = AsyncLLM.from_engine_args(engine_args) if send_one_request: async for _ in async_llm.generate( "Hello my name is", request_id="abc", sampling_params=SamplingParams( max_tokens=1, output_kind=RequestOutputKind.DELTA ), ): pass del async_llm # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=SHUTDOWN_TEST_THRESHOLD_BYTES, ) @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("enable_multiprocessing", [True]) @pytest.mark.parametrize("send_one_request", [False, True]) def test_llm_delete( monkeypatch, model: str, tensor_parallel_size: int, enable_multiprocessing: bool, send_one_request: bool, ) -> None: """Test that LLM frees GPU memory upon deletion. TODO(andy) - LLM without multiprocessing. Args: model: model under test tensor_parallel_size: degree of tensor parallelism enable_multiprocessing: enable workers in separate process(es) send_one_request: send one request to engine before deleting """ if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") with monkeypatch.context() as m: MP_VALUE = "1" if enable_multiprocessing else "0" m.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", MP_VALUE) # Instantiate LLM; make request to complete any deferred # initialization; then delete instance llm = LLM( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size ) if send_one_request: llm.generate( "Hello my name is", sampling_params=SamplingParams(max_tokens=1) ) del llm # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=SHUTDOWN_TEST_THRESHOLD_BYTES, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/shutdown/test_startup_error.py
tests/v1/shutdown/test_startup_error.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Test that we handle a startup Error and shutdown.""" import pytest from tests.utils import wait_for_gpu_memory_to_clear from tests.v1.shutdown.utils import ( SHUTDOWN_TEST_THRESHOLD_BYTES, SHUTDOWN_TEST_TIMEOUT_SEC, ) from vllm import LLM from vllm.distributed import get_tensor_model_parallel_rank from vllm.engine.arg_utils import AsyncEngineArgs from vllm.model_executor.models.llama import LlamaForCausalLM from vllm.utils.torch_utils import cuda_device_count_stateless from vllm.v1.engine.async_llm import AsyncLLM MODELS = ["hmellor/tiny-random-LlamaForCausalLM"] def evil_method(self, *args, **kwargs): """Evil method that raises an exception.""" if get_tensor_model_parallel_rank() == 0: raise Exception("Simulated Error in startup!") return self.model(*args, **kwargs, intermediate_tensors=None) @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("failing_method", ["forward", "load_weights"]) def test_async_llm_startup_error( monkeypatch, model: str, tensor_parallel_size: int, failing_method: str ) -> None: """Test that AsyncLLM propagates an __init__ error & frees memory. Test profiling (forward()) and load weights failures. AsyncLLM always uses an MP client. """ if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") # Monkeypatch an error in the model. monkeypatch.setattr(LlamaForCausalLM, failing_method, evil_method) engine_args = AsyncEngineArgs( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size ) # Confirm we get an exception. with pytest.raises(Exception, match="initialization failed"): _ = AsyncLLM.from_engine_args(engine_args) # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=SHUTDOWN_TEST_THRESHOLD_BYTES, ) @pytest.mark.timeout(SHUTDOWN_TEST_TIMEOUT_SEC) @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("tensor_parallel_size", [2, 1]) @pytest.mark.parametrize("enable_multiprocessing", [True]) @pytest.mark.parametrize("failing_method", ["forward", "load_weights"]) def test_llm_startup_error( monkeypatch, model: str, tensor_parallel_size: int, enable_multiprocessing: bool, failing_method: str, ) -> None: """Test that LLM propagates an __init__ error and frees memory. Test profiling (forward()) and load weights failures. TODO(andy) - LLM without multiprocessing. """ # Skip non-Llama models since we monkeypatch LlamaForCausalLM specifically. # If MODELS list grows, each architecture needs its own test variant. if model != "JackFram/llama-68m": pytest.skip(reason="Only test JackFram/llama-68m") if cuda_device_count_stateless() < tensor_parallel_size: pytest.skip(reason="Not enough CUDA devices") with monkeypatch.context() as m: MP_VALUE = "1" if enable_multiprocessing else "0" m.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", MP_VALUE) # Monkeypatch an error in the model. monkeypatch.setattr(LlamaForCausalLM, failing_method, evil_method) with pytest.raises( Exception, match="initialization failed" if enable_multiprocessing else "Simulated Error in startup!", ): _ = LLM( model=model, enforce_eager=True, tensor_parallel_size=tensor_parallel_size, ) # Confirm all the processes are cleaned up. wait_for_gpu_memory_to_clear( devices=list(range(tensor_parallel_size)), threshold_bytes=SHUTDOWN_TEST_THRESHOLD_BYTES, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/shutdown/utils.py
tests/v1/shutdown/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Shutdown test utils""" SHUTDOWN_TEST_TIMEOUT_SEC = 120 SHUTDOWN_TEST_THRESHOLD_BYTES = 2 * 2**30
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/cudagraph/test_cudagraph_mode.py
tests/v1/cudagraph/test_cudagraph_mode.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import weakref from contextlib import ExitStack import pytest from tests.utils import wait_for_gpu_memory_to_clear from tests.v1.attention.utils import full_cg_backend_configs as backend_configs from vllm import LLM from vllm.config import CompilationConfig, CompilationMode from vllm.platforms import current_platform # test attention backend and cudagraph_mode combo # (backend_name, cudagraph_mode, supported) if current_platform.is_rocm(): combo_cases_1 = [ ("RocmAttn", "FULL", True), ("RocmAttn", "FULL_AND_PIECEWISE", True), ("TritonAttn", "FULL", True), ("TritonAttn", "FULL_AND_PIECEWISE", True), ] else: combo_cases_1 = [ ("FA3", "FULL", True), ("FA3", "FULL_AND_PIECEWISE", True), ("FA2", "FULL", True), # Should fallback to FULL_AND_PIECEWISE ("FA2", "FULL_AND_PIECEWISE", True), ("FlashInfer", "FULL", True), # Should fallback to FULL_AND_PIECEWISE ("FlashInfer", "FULL_AND_PIECEWISE", True), ] @pytest.mark.parametrize("backend_name, cudagraph_mode, supported", combo_cases_1) def test_backend_and_cudagraph_mode_combo(backend_name, cudagraph_mode, supported): if backend_name == "FlashInfer": try: import flashinfer # noqa: F401 except ImportError: pytest.skip("FlashInfer is not installed") backend_config = backend_configs[backend_name] # Dynamically skip test if GPU capability is not met if ( backend_config.specific_gpu_arch and backend_config.specific_gpu_arch != current_platform.get_device_capability() ): pytest.skip("Only Hopper GPUs support FA3 and FlashMLA") attention_config = backend_config.attention_config with ExitStack() as stack: if not supported: stack.enter_context(pytest.raises(Exception)) llm = LLM( model="Qwen/Qwen2-1.5B-Instruct", max_num_seqs=256, trust_remote_code=True, gpu_memory_utilization=0.45, max_model_len=1024, attention_config=attention_config, compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, cudagraph_mode=cudagraph_mode ), ) llm.generate(["Hello, my name is"] * 10) # when above code raises, `llm` may be undefined, so we need to catch that try: llm = weakref.proxy(llm) del llm except UnboundLocalError: pass wait_for_gpu_memory_to_clear( devices=[0], threshold_ratio=0.1, ) # test cudagraph_mode with different compilation mode. # (backend_name, cudagraph_mode, compilation_mode, supported) attn_backend = "RocmAttn" if current_platform.is_rocm() else "FA2" combo_cases_2 = [ (attn_backend, "FULL", CompilationMode.NONE, True), (attn_backend, "FULL", CompilationMode.VLLM_COMPILE, True), (attn_backend, "PIECEWISE", CompilationMode.NONE, True), (attn_backend, "PIECEWISE", CompilationMode.VLLM_COMPILE, True), (attn_backend, "FULL_AND_PIECEWISE", CompilationMode.NONE, True), (attn_backend, "FULL_AND_PIECEWISE", CompilationMode.VLLM_COMPILE, True), (attn_backend, "FULL_DECODE_ONLY", CompilationMode.NONE, True), (attn_backend, "FULL_DECODE_ONLY", CompilationMode.VLLM_COMPILE, True), (attn_backend, "NONE", CompilationMode.NONE, True), (attn_backend, "NONE", CompilationMode.VLLM_COMPILE, True), ] @pytest.mark.parametrize( "backend_name,cudagraph_mode,compilation_mode,supported", combo_cases_2 ) def test_cudagraph_compilation_combo( backend_name, cudagraph_mode, compilation_mode, supported ): backend_config = backend_configs[backend_name] attention_config = backend_config.attention_config with ExitStack() as stack: if not supported: stack.enter_context(pytest.raises(Exception)) llm = LLM( model="Qwen/Qwen2-1.5B-Instruct", max_num_seqs=256, trust_remote_code=True, gpu_memory_utilization=0.45, max_model_len=1024, attention_config=attention_config, compilation_config=CompilationConfig( mode=compilation_mode, cudagraph_mode=cudagraph_mode ), ) llm.generate(["Hello, my name is"] * 10) # when above code raises, `llm` may be undefined, so we need to catch that try: llm = weakref.proxy(llm) del llm except UnboundLocalError: pass finally: wait_for_gpu_memory_to_clear( devices=[0], threshold_ratio=0.1, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/cudagraph/test_cudagraph_dispatch.py
tests/v1/cudagraph/test_cudagraph_dispatch.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from unittest.mock import MagicMock, patch import pytest import torch import torch.nn as nn from tests.utils import create_new_process_for_each_test from vllm.compilation.cuda_graph import CUDAGraphWrapper from vllm.compilation.monitor import set_cudagraph_capturing_enabled from vllm.config import ( CompilationConfig, CompilationMode, CUDAGraphMode, ParallelConfig, SchedulerConfig, VllmConfig, ) from vllm.forward_context import BatchDescriptor, set_forward_context from vllm.platforms import current_platform from vllm.v1.cudagraph_dispatcher import CudagraphDispatcher # Helper MLP for testing class SimpleMLP(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(10, 10) self.fc2 = nn.Linear(10, 10) def forward(self, x): return self.fc2(self.fc1(x)) def _create_vllm_config( compilation_config: CompilationConfig, max_num_seqs: int = 8, lora_config: bool = False, ) -> MagicMock: mock_config = MagicMock(spec=VllmConfig) mock_config.compilation_config = compilation_config mock_config.scheduler_config = SchedulerConfig.default_factory( max_num_seqs=max_num_seqs, ) mock_config.parallel_config = ParallelConfig() mock_config.speculative_config = None # No speculative decoding if not lora_config: mock_config.lora_config = None # Mimic the behavior of VllmConfig.__post_init__() if compilation_config.mode == CompilationMode.VLLM_COMPILE: compilation_config.set_splitting_ops_for_v1( all2all_backend=mock_config.parallel_config.all2all_backend, data_parallel_size=mock_config.parallel_config.data_parallel_size, ) # mimic VllmConfig.__post_init__ if compilation_config.cudagraph_capture_sizes: compilation_config.max_cudagraph_capture_size = ( compilation_config.cudagraph_capture_sizes[-1] ) compilation_config.post_init_cudagraph_sizes() mock_config.pad_for_cudagraph = ( lambda batch_size: compilation_config.bs_to_padded_graph_size[batch_size] ) return mock_config class TestCudagraphDispatcher: @pytest.mark.parametrize( "cudagraph_mode_str,compilation_mode,lora_config", [ # Test case 0: Full CG for mixed batches, no separate routine ("FULL", CompilationMode.NONE, False), # Test case 1: Full CG for uniform batches, piecewise for mixed ("FULL_AND_PIECEWISE", CompilationMode.NONE, False), # Test case 2: Full CG for uniform batches, no CG for mixed ("FULL_DECODE_ONLY", CompilationMode.NONE, False), # Test case 3: PIECEWISE for all ("PIECEWISE", CompilationMode.VLLM_COMPILE, False), # Test case 4: PIECEWISE for all, specialize LoRA cases ("PIECEWISE", CompilationMode.VLLM_COMPILE, True), ], ) def test_dispatcher(self, cudagraph_mode_str, compilation_mode, lora_config): # Setup dispatcher comp_config = CompilationConfig( cudagraph_mode=cudagraph_mode_str, mode=compilation_mode, cudagraph_capture_sizes=[1, 8], ) config = _create_vllm_config( comp_config, max_num_seqs=8, lora_config=lora_config ) if ( cudagraph_mode_str == "FULL_AND_PIECEWISE" and compilation_mode == CompilationMode.NONE ): with pytest.raises(AssertionError): dispatcher = CudagraphDispatcher(config) return dispatcher = CudagraphDispatcher(config) dispatcher.initialize_cudagraph_keys( cudagraph_mode=comp_config.cudagraph_mode, uniform_decode_query_len=1 ) # Verify the key is initialized correctly if cudagraph_mode_str in ["FULL_AND_PIECEWISE", "PIECEWISE"]: assert len(dispatcher.cudagraph_keys[CUDAGraphMode.PIECEWISE]) == ( 4 if lora_config else 2 ) else: assert len(dispatcher.cudagraph_keys[CUDAGraphMode.PIECEWISE]) == 0 if cudagraph_mode_str not in ["NONE", "PIECEWISE"]: assert len(dispatcher.cudagraph_keys[CUDAGraphMode.FULL]) == ( 4 if lora_config else 2 ) else: assert len(dispatcher.cudagraph_keys[CUDAGraphMode.FULL]) == 0 # Test dispatch logic # 1. non-uniform batch, size in cudagraph size list desc_full_exact = BatchDescriptor( num_tokens=8, uniform=False, ) rt_mode, key = dispatcher.dispatch( num_tokens=8, uniform_decode=False, has_lora=False ) if cudagraph_mode_str == "FULL": assert rt_mode == CUDAGraphMode.FULL assert key == desc_full_exact elif cudagraph_mode_str in ["FULL_AND_PIECEWISE", "PIECEWISE"]: assert rt_mode == CUDAGraphMode.PIECEWISE assert key == desc_full_exact else: assert rt_mode == CUDAGraphMode.NONE # 2. uniform decode batch, size in cudagraph size list desc_uniform_exact = BatchDescriptor(num_tokens=8, num_reqs=8, uniform=True) rt_mode, key = dispatcher.dispatch( num_tokens=8, uniform_decode=True, has_lora=False ) if cudagraph_mode_str == "FULL": assert rt_mode == CUDAGraphMode.FULL assert key == desc_uniform_exact.relax_for_mixed_batch_cudagraphs() elif cudagraph_mode_str in ["FULL_DECODE_ONLY", "FULL_AND_PIECEWISE"]: assert rt_mode == CUDAGraphMode.FULL assert key == desc_uniform_exact elif cudagraph_mode_str == "PIECEWISE": assert rt_mode == CUDAGraphMode.PIECEWISE assert key == desc_uniform_exact.relax_for_mixed_batch_cudagraphs() else: assert rt_mode == CUDAGraphMode.NONE # 3. No key match rt_mode, key = dispatcher.dispatch( num_tokens=15, uniform_decode=False, has_lora=False ) assert rt_mode == CUDAGraphMode.NONE assert key == BatchDescriptor(num_tokens=15) # 4. disable_full should have a fall back mode (e.g., cascade attention) desc_full_exact = BatchDescriptor(num_tokens=8, uniform=False) rt_mode, key = dispatcher.dispatch( num_tokens=8, uniform_decode=False, has_lora=False, disable_full=True ) if "PIECEWISE" in cudagraph_mode_str: # string contains check assert rt_mode == CUDAGraphMode.PIECEWISE assert key == desc_full_exact.relax_for_mixed_batch_cudagraphs() else: assert rt_mode == CUDAGraphMode.NONE @pytest.mark.skipif(not current_platform.is_cuda(), reason="Skip if not cuda") class TestCUDAGraphWrapper: def setup_method(self): self.vllm_config = _create_vllm_config(CompilationConfig()) self.model = SimpleMLP().to("cuda") self.persistent_input_buffer = torch.zeros(1, 10, device="cuda") self.input_tensor = torch.randn(1, 10, device="cuda") def test_capture_and_replay(self): wrapper = CUDAGraphWrapper( self.model, self.vllm_config, runtime_mode=CUDAGraphMode.FULL ) batch_descriptor = BatchDescriptor(num_tokens=10) # 0. global warmup with set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.NONE, batch_descriptor=None, ): wrapper(self.input_tensor) # 1. Capture with ( set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.FULL, batch_descriptor=batch_descriptor, ), patch("torch.cuda.graph", wraps=torch.cuda.graph) as mock_cuda_graph, ): output1 = wrapper(self.input_tensor) # capturing phase should generate a zero output assert torch.allclose(output1, torch.zeros_like(output1)) mock_cuda_graph.assert_called_once() assert batch_descriptor in wrapper.concrete_cudagraph_entries entry = wrapper.concrete_cudagraph_entries[batch_descriptor] assert entry.cudagraph is not None # 2. Replay with ( set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.FULL, batch_descriptor=batch_descriptor, ), patch.object( entry.cudagraph, "replay", wraps=entry.cudagraph.replay ) as mock_replay, ): output2 = wrapper(self.input_tensor) mock_replay.assert_called_once() # Compare with eager output eager_output = self.model(self.input_tensor) torch.testing.assert_close(eager_output, output2) def test_bypass_on_mode_mismatch(self): wrapper = CUDAGraphWrapper( self.model, self.vllm_config, runtime_mode=CUDAGraphMode.FULL ) batch_descriptor = BatchDescriptor(num_tokens=10) with ( set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.PIECEWISE, batch_descriptor=batch_descriptor, ), patch("torch.cuda.graph", wraps=torch.cuda.graph) as mock_cuda_graph, patch.object( self.model, "forward", wraps=self.model.forward ) as mock_forward, ): wrapper(self.input_tensor) mock_cuda_graph.assert_not_called() mock_forward.assert_called_once() assert not wrapper.concrete_cudagraph_entries def test_bypass_on_mode_none(self): wrapper = CUDAGraphWrapper( self.model, self.vllm_config, runtime_mode=CUDAGraphMode.FULL ) batch_descriptor = BatchDescriptor(num_tokens=10) with ( set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.NONE, batch_descriptor=batch_descriptor, ), patch("torch.cuda.graph", wraps=torch.cuda.graph) as mock_cuda_graph, ): wrapper(self.input_tensor) mock_cuda_graph.assert_not_called() assert not wrapper.concrete_cudagraph_entries @pytest.mark.skipif(not current_platform.is_cuda(), reason="Skip if not cuda") class TestCudagraphIntegration: def setup_method(self): # only FULL mode for non-uniform batches self.comp_config = CompilationConfig( mode=CompilationMode.VLLM_COMPILE, cudagraph_mode="FULL", cudagraph_capture_sizes=[10, 20], ) self.vllm_config = _create_vllm_config(self.comp_config) self.dispatcher = CudagraphDispatcher(self.vllm_config) self.dispatcher.initialize_cudagraph_keys( self.comp_config.cudagraph_mode, uniform_decode_query_len=1 ) def _run_and_monitor_call( self, wrapper, input_tensor, runtime_mode, batch_descriptor ): """Helper to run a single call and monitor the action.""" with ( patch("torch.cuda.graph", wraps=torch.cuda.graph) as mock_graph_context, patch.object(wrapper, "runnable", wraps=wrapper.runnable) as mock_runnable, ): entry = wrapper.concrete_cudagraph_entries.get(batch_descriptor, None) context = set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=runtime_mode, batch_descriptor=batch_descriptor, ) mock_replay = MagicMock() if entry and entry.cudagraph: with ( context, patch.object( entry.cudagraph, "replay", new_callable=MagicMock ) as mock_replay, ): wrapper(input_tensor) else: with context: wrapper(input_tensor) if mock_graph_context.called: # note that this is globally mocked, so it will be detected # even whether called by the inner or outer wrapper return "capture_global" if mock_replay.called: # only for outer wrapper return "replay" if mock_runnable.call_count > 0: # only for outer wrapper return "bypass" return "unknown" @create_new_process_for_each_test("spawn") def test_capture_replay_bypass_logic(self): model = SimpleMLP().to("cuda") full_wrapper = CUDAGraphWrapper(model, self.vllm_config, CUDAGraphMode.FULL) max_bs = 16 persistent_input_buffer = torch.zeros(max_bs, 10, device="cuda") input_1 = persistent_input_buffer[:1] input_2 = persistent_input_buffer[:2] input_3 = persistent_input_buffer[:3] desc_1 = BatchDescriptor(num_tokens=1) desc_2 = BatchDescriptor(num_tokens=2) desc_3_unseen = BatchDescriptor(num_tokens=3) # 0. global warmup with set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.NONE, batch_descriptor=None, ): full_wrapper(input_1) rt_mode, key = self.dispatcher.dispatch(desc_1) # 1. Capture first shape action = self._run_and_monitor_call(full_wrapper, input_1, rt_mode, key) assert action == "capture_global" # 2. Replay first shape action = self._run_and_monitor_call(full_wrapper, input_1, rt_mode, key) assert action == "replay" rt_mode, key = self.dispatcher.dispatch(desc_2) # 3. Capture second shape action = self._run_and_monitor_call(full_wrapper, input_2, rt_mode, key) assert action == "capture_global" # 4. Replay second shape action = self._run_and_monitor_call( full_wrapper, input_2, CUDAGraphMode.FULL, desc_2 ) assert action == "replay" # 5. Bypass if no key match rt_mode, key = self.dispatcher.dispatch(desc_3_unseen) assert rt_mode == CUDAGraphMode.NONE action = self._run_and_monitor_call(full_wrapper, input_3, rt_mode, key) assert action == "bypass" # capture unseen shape is not allowed after disable set_cudagraph_capturing_enabled(False) with pytest.raises(RuntimeError): self._run_and_monitor_call( full_wrapper, input_3, CUDAGraphMode.FULL, desc_3_unseen ) set_cudagraph_capturing_enabled(True) @create_new_process_for_each_test("spawn") def test_nested_wrappers(self): """Tests a scenario with a PIECEWISE wrapper inside a FULL one.""" model = SimpleMLP().to("cuda") full_wrapper = CUDAGraphWrapper(model, self.vllm_config, CUDAGraphMode.FULL) input_1 = torch.randn(1, 10, device="cuda") # Setup: Inner model is wrapped with PIECEWISE, outer with FULL inner_model = SimpleMLP().to("cuda") piecewise_wrapper = CUDAGraphWrapper( inner_model, self.vllm_config, CUDAGraphMode.PIECEWISE ) inner_model.forward = MagicMock(wraps=inner_model.forward) outer_model = SimpleMLP().to("cuda") # When outer model is called, it calls the piecewise_wrapper outer_model.forward = MagicMock( wraps=outer_model.forward, side_effect=piecewise_wrapper ) full_wrapper = CUDAGraphWrapper( outer_model, self.vllm_config, CUDAGraphMode.FULL ) desc_1 = BatchDescriptor(num_tokens=1) # 0. global warmup with set_forward_context( attn_metadata=None, vllm_config=self.vllm_config, cudagraph_runtime_mode=CUDAGraphMode.NONE, batch_descriptor=None, ): full_wrapper(input_1) # --- Test runtime mode FULL--- # Run with FULL mode context. Expect outer wrapper to capture. # The inner mock should be called once inside the graph capture. outer_model.forward.reset_mock() inner_model.forward.reset_mock() action = self._run_and_monitor_call( full_wrapper, input_1, CUDAGraphMode.FULL, desc_1 ) assert action == "capture_global" assert outer_model.forward.call_count == 1 assert inner_model.forward.call_count == 1 # Run again. Expect outer wrapper to replay. # The outer model should NOT be called because the whole graph # is replayed. action = self._run_and_monitor_call( full_wrapper, input_1, CUDAGraphMode.FULL, desc_1 ) assert action == "replay" assert outer_model.forward.call_count == 1 # No new call assert inner_model.forward.call_count == 1 # --- Test runtime mode PIECEWISE --- outer_model.forward.reset_mock() inner_model.forward.reset_mock() # Run with PIECEWISE mode context. # Expect outer wrapper to bypass and call inner wrapper. # Inner wrapper should capture. action = self._run_and_monitor_call( full_wrapper, input_1, CUDAGraphMode.PIECEWISE, desc_1 ) assert action == "capture_global" assert outer_model.forward.call_count == 1 assert inner_model.forward.call_count == 1 # Run again with PIECEWISE. # Outer bypasses, inner replays. action = self._run_and_monitor_call( full_wrapper, input_1, CUDAGraphMode.PIECEWISE, desc_1 ) assert action == "bypass" assert outer_model.forward.call_count == 2 assert inner_model.forward.call_count == 1
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/cudagraph/__init__.py
tests/v1/cudagraph/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/structured_output/test_utils.py
tests/v1/structured_output/test_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from vllm.v1.structured_output.backend_xgrammar import ( has_xgrammar_unsupported_json_features, ) pytestmark = pytest.mark.cpu_test @pytest.fixture def unsupported_string_schemas(): return [ {"type": "string", "format": "non_existing_format"}, ] @pytest.fixture def unsupported_integer_schemas(): return [ {"type": "integer", "multipleOf": 120}, ] @pytest.fixture def unsupported_number_schemas(): return [ {"type": "number", "multipleOf": 120}, ] @pytest.fixture def unsupported_array_schemas(): return [ {"type": "array", "uniqueItems": True}, {"type": "array", "contains": {"type": "string"}}, {"type": "array", "minContains": 1}, {"type": "array", "maxContains": 5}, ] @pytest.fixture def unsupported_object_schemas(): return [ {"type": "object", "propertyNames": {"pattern": "^[a-z]+$"}}, {"type": "object", "patternProperties": {"^S": {"type": "string"}}}, ] @pytest.fixture def supported_schema(): return { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"}, "email": {"type": "string", "format": "email"}, "status": {"type": "string"}, "scores": {"type": "array", "items": {"type": "number"}}, "car_type": {"type": "string", "enum": ["sedan", "suv", "truck"]}, "car_brand": {"type": "string", "pattern": "^[a-zA-Z]+$"}, "short_description": {"type": "string", "maxLength": 50}, "mileage": {"type": "number", "minimum": 0, "maximum": 1000000}, "model_year": { "type": "integer", "exclusiveMinimum": 1900, "exclusiveMaximum": 2100, }, "long_description": {"type": "string", "minLength": 50, "maxLength": 2000}, "address": { "type": "object", "properties": { "street": {"type": "string"}, "city": {"type": "string"}, }, }, }, "minProperties": 1, "maxProperties": 100, } @pytest.mark.parametrize( "schema_type", [ "unsupported_string_schemas", "unsupported_integer_schemas", "unsupported_number_schemas", "unsupported_array_schemas", "unsupported_object_schemas", ], ) def test_unsupported_json_features_by_type(schema_type, request): schemas = request.getfixturevalue(schema_type) for schema in schemas: assert has_xgrammar_unsupported_json_features(schema), ( f"Schema should be unsupported: {schema}" ) def test_supported_json_features(supported_schema): assert not has_xgrammar_unsupported_json_features(supported_schema), ( "Schema should be supported" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/structured_output/test_backend_guidance.py
tests/v1/structured_output/test_backend_guidance.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import time from concurrent.futures import Future import pytest from transformers import AutoTokenizer from vllm.config import StructuredOutputsConfig, VllmConfig from vllm.config.model import ModelConfig from vllm.config.parallel import ParallelConfig from vllm.config.speculative import SpeculativeConfig from vllm.sampling_params import SamplingParams, StructuredOutputsParams from vllm.v1.request import Request from vllm.v1.structured_output import StructuredOutputManager from vllm.v1.structured_output.backend_guidance import GuidanceBackend from vllm.v1.structured_output.backend_types import StructuredOutputOptions TOKENIZER = "gpt2" def test_backend_guidance_rollback_terminated(): # Test that the backend guidance successfully rollbacks from a # terminated state. This can happen with speculative decoding, # where the draft model proposes EOS and it is verified by the # guidance backend. In that case we are in a stopped state, but # it should be reverted in case EOS is not accepted by the target # model. vllm_config = VllmConfig( decoding_config=StructuredOutputsConfig( backend="guidance", ) ) tokenizer = AutoTokenizer.from_pretrained(TOKENIZER) backend = GuidanceBackend( vllm_config, tokenizer=tokenizer, vocab_size=50257, ) grammar = backend.compile_grammar( StructuredOutputOptions.JSON, '{"type": "object"}' ) prompt = tokenizer.encode('{"a": "b"}') assert len(prompt) > 1 dummy_wrong = tokenizer.encode('{"a"}') for token in prompt: assert grammar.accept_tokens("", [token]) assert not grammar.is_terminated() assert grammar.accept_tokens("", [tokenizer.eos_token_id]) assert grammar.is_terminated() # Giving any other token should also be accepted assert grammar.accept_tokens("", dummy_wrong) # Rollback is done from where state was terminated, so from '}' not EOS grammar.rollback(len(prompt) - 1) assert not grammar.is_terminated() assert grammar.validate_tokens([tokenizer.eos_token_id]) == [] assert grammar.validate_tokens(dummy_wrong) != dummy_wrong assert grammar.accept_tokens("", prompt[1:]) assert not grammar.is_terminated() assert grammar.accept_tokens("", [tokenizer.eos_token_id]) assert grammar.is_terminated() # Rollback of <= 0 should not change the terminated state grammar.rollback(0) assert grammar.is_terminated() grammar.rollback(-1) assert grammar.is_terminated() def test_grammar_bitmask_with_specdec(): tokenizer = AutoTokenizer.from_pretrained(TOKENIZER) prompt = tokenizer.encode('{"a": "b"}') vllm_config = VllmConfig( model_config=ModelConfig(tokenizer=TOKENIZER), structured_outputs_config=StructuredOutputsConfig(backend="guidance"), speculative_config=SpeculativeConfig(model="[ngram]", num_speculative_tokens=3), ) structured_output_manager = StructuredOutputManager(vllm_config) for i in range(1, 2): sampling_params = SamplingParams( structured_outputs=StructuredOutputsParams( json='{"type": "object"}', ), ) sampling_params.structured_outputs._backend = "guidance" my_req_id = f"my_req_id_{i}" request = Request( my_req_id, prompt_token_ids=prompt[:i], sampling_params=sampling_params, pooling_params=None, eos_token_id=tokenizer.eos_token_id, ) structured_output_manager.grammar_init(request) def grammar_bitmask(req: Request, tokens: list[int]) -> None: structured_output_manager.grammar_bitmask( requests={req.request_id: req}, structured_output_request_ids={req.request_id: 0}, scheduled_spec_decode_tokens={req.request_id: tokens}, ) # At this point, we rolled-back, so should not be terminated assert not req.structured_output_request.grammar.is_terminated() # The grammar might not yet be compiled, so we wait for it while not request.structured_output_request._check_grammar_completion(): continue assert request.structured_output_request.grammar.accept_tokens( request.request_id, prompt[:i] ) grammar_bitmask(request, prompt[i:] + [tokenizer.eos_token_id]) grammar_bitmask( request, prompt[i:] + [tokenizer.eos_token_id] + prompt ) # EOS not the final token grammar_bitmask(request, prompt[i:]) # EOS not present grammar_bitmask(request, prompt[i:] + [tokenizer.eos_token_id]) @pytest.mark.parametrize("async_grammar", [True, False]) def test_grammar_init_async_and_sync(async_grammar): """Test grammar initialization works correctly in both async and sync modes. This test validates that the distributed_executor_backend config option correctly controls whether grammar compilation happens asynchronously (via executor.submit) or synchronously. When set to "external_launcher", grammar compilation is synchronous to avoid deadlocks. """ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER) prompt = tokenizer.encode('{"a": "b"}') # Use "external_launcher" for sync mode, None for async mode executor_backend = None if async_grammar else "external_launcher" vllm_config = VllmConfig( model_config=ModelConfig(tokenizer=TOKENIZER), structured_outputs_config=StructuredOutputsConfig(backend="guidance"), parallel_config=ParallelConfig(distributed_executor_backend=executor_backend), ) structured_output_manager = StructuredOutputManager(vllm_config) sampling_params = SamplingParams( structured_outputs=StructuredOutputsParams( json='{"type": "object"}', ), ) sampling_params.structured_outputs._backend = "guidance" request = Request( "test_request", prompt_token_ids=prompt, sampling_params=sampling_params, pooling_params=None, eos_token_id=tokenizer.eos_token_id, ) structured_output_manager.grammar_init(request) # Check the internal _grammar type immediately after init # Before _check_grammar_completion is called, async mode should have a Future raw_grammar = request.structured_output_request._grammar if async_grammar: assert isinstance(raw_grammar, Future), ( "Async mode should store a Future before completion" ) else: assert not isinstance(raw_grammar, Future), ( "Sync mode should store the grammar directly, not a Future" ) # Wait for grammar to be ready (handles both async and sync cases) start_time = time.time() while not request.structured_output_request._check_grammar_completion(): if time.time() - start_time > 5: # 5-second timeout pytest.fail("Grammar compilation timed out") time.sleep(0.01) # After completion, _grammar should no longer be a Future assert not isinstance(request.structured_output_request._grammar, Future) # Verify grammar is properly initialized and functional grammar = request.structured_output_request.grammar assert grammar is not None assert not grammar.is_terminated() # Verify the grammar can accept valid tokens assert grammar.accept_tokens(request.request_id, prompt)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/structured_output/test_reasoning_structured_output.py
tests/v1/structured_output/test_reasoning_structured_output.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Unit tests for reasoning-aware structured output functionality (PR #25515).""" from unittest.mock import Mock import pytest from vllm.config import ModelConfig, SchedulerConfig, VllmConfig from vllm.reasoning import ReasoningParser from vllm.v1.request import Request from vllm.v1.structured_output import StructuredOutputManager class TestReasoningStructuredOutput: """Test reasoning-aware structured output functionality.""" @pytest.fixture def mock_model_config(self): """Create a mock ModelConfig.""" config = Mock(spec=ModelConfig) config.skip_tokenizer_init = True # Skip tokenizer init to avoid network calls config.get_vocab_size = Mock(return_value=50000) # Add missing runner_type attribute that tokenizer initialization expects config.runner_type = "generate" # Add other attributes that tokenizer initialization might need config.tokenizer = "test-tokenizer" config.tokenizer_mode = "auto" config.trust_remote_code = False config.tokenizer_revision = None return config @pytest.fixture def mock_scheduler_config(self): """Create a mock SchedulerConfig.""" config = Mock(spec=SchedulerConfig) config.max_num_seqs = 128 return config @pytest.fixture def mock_vllm_config(self, mock_model_config, mock_scheduler_config): """Create a mock VllmConfig.""" config = Mock(spec=VllmConfig) config.model_config = mock_model_config config.scheduler_config = mock_scheduler_config config.structured_outputs_config = Mock() config.structured_outputs_config.reasoning_parser = None config.structured_outputs_config.enable_in_reasoning = False config.speculative_config = None return config @pytest.fixture def mock_reasoning_parser(self): """Create a mock ReasoningParser.""" parser = Mock(spec=ReasoningParser) parser.is_reasoning_end = Mock(return_value=False) return parser @pytest.fixture def mock_request_with_structured_output(self): """Create a mock request with structured output.""" request = Mock(spec=Request) request.structured_output_request = Mock() request.structured_output_request.reasoning_ended = None request.structured_output_request.grammar = Mock() request.structured_output_request.grammar.is_terminated = Mock( return_value=False ) request.use_structured_output = True request.prompt_token_ids = [1, 2, 3, 4, 5] request.all_token_ids = [1, 2, 3, 4, 5, 6, 7, 8] request.num_computed_tokens = 5 request.num_output_placeholders = 0 return request def test_should_fill_bitmask_with_enable_in_reasoning( self, mock_vllm_config, mock_request_with_structured_output ): """Test should_fill_bitmask when enable_in_reasoning is True.""" # Enable enable_in_reasoning mock_vllm_config.structured_outputs_config.enable_in_reasoning = True manager = StructuredOutputManager(mock_vllm_config) # Should always return True when enable_in_reasoning is enabled result = manager.should_fill_bitmask(mock_request_with_structured_output) assert result is True def test_should_fill_bitmask_without_enable_in_reasoning( self, mock_vllm_config, mock_request_with_structured_output, mock_reasoning_parser, ): """Test should_fill_bitmask when enable_in_reasoning is False.""" # Keep enable_in_reasoning as False (default) config = mock_vllm_config.structured_outputs_config assert config.enable_in_reasoning is False manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = mock_reasoning_parser # Mock reasoning not ended mock_reasoning_parser.is_reasoning_end.return_value = False result = manager.should_fill_bitmask(mock_request_with_structured_output) # Should set reasoning_ended and return its value assert ( mock_request_with_structured_output.structured_output_request.reasoning_ended is False ) assert result is False def test_should_fill_bitmask_no_reasoner( self, mock_vllm_config, mock_request_with_structured_output ): """Test should_fill_bitmask when no reasoner is configured.""" manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = None result = manager.should_fill_bitmask(mock_request_with_structured_output) # Should default to True when no reasoner assert result is True def test_should_advance_with_enable_in_reasoning( self, mock_vllm_config, mock_request_with_structured_output, mock_reasoning_parser, ): """Test should_advance when enable_in_reasoning is True.""" # Enable enable_in_reasoning mock_vllm_config.structured_outputs_config.enable_in_reasoning = True manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = mock_reasoning_parser # Should always return True when enable_in_reasoning is enabled result = manager.should_advance(mock_request_with_structured_output) assert result is True def test_should_advance_reasoning_not_ended( self, mock_vllm_config, mock_request_with_structured_output, mock_reasoning_parser, ): """Test should_advance when reasoning has not ended.""" manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = mock_reasoning_parser # Set reasoning as not ended ( mock_request_with_structured_output.structured_output_request ).reasoning_ended = False mock_reasoning_parser.is_reasoning_end.return_value = False result = manager.should_advance(mock_request_with_structured_output) # Should return False since reasoning hasn't ended assert result is False def test_should_advance_reasoning_just_ended( self, mock_vllm_config, mock_request_with_structured_output, mock_reasoning_parser, ): """Test should_advance when reasoning ends in current step.""" manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = mock_reasoning_parser # Set reasoning as not ended initially, but ends in this step ( mock_request_with_structured_output.structured_output_request ).reasoning_ended = False mock_reasoning_parser.is_reasoning_end.return_value = True result = manager.should_advance(mock_request_with_structured_output) # Should set reasoning_ended to True but return False for this step assert ( mock_request_with_structured_output.structured_output_request.reasoning_ended is True ) assert result is False def test_should_advance_reasoning_already_ended( self, mock_vllm_config, mock_request_with_structured_output, mock_reasoning_parser, ): """Test should_advance when reasoning has already ended.""" manager = StructuredOutputManager(mock_vllm_config) manager.reasoner = mock_reasoning_parser # Set reasoning as already ended ( mock_request_with_structured_output.structured_output_request ).reasoning_ended = True result = manager.should_advance(mock_request_with_structured_output) # Should return True since reasoning has ended assert result is True
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/structured_output/__init__.py
tests/v1/structured_output/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/structured_output/test_gptoss_structural_tags.py
tests/v1/structured_output/test_gptoss_structural_tags.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Unit tests for GPT-OSS structural tag support in reasoning (PR #25515).""" import json from unittest.mock import Mock import pytest from vllm.entrypoints.tool_server import ToolServer from vllm.reasoning.gptoss_reasoning_parser import ( GptOssReasoningParser, from_builtin_tool_to_tag, no_func_reaonsing_tag, tag_with_builtin_funcs, ) class TestGptOssReasoningParser: """Test cases for GptOssReasoningParser structural tag functionality.""" @pytest.fixture def mock_tokenizer(self): """Create a mock tokenizer for testing.""" tokenizer = Mock() tokenizer.encode = Mock(return_value=[1, 2, 3, 4, 5]) return tokenizer @pytest.fixture def reasoning_parser(self, mock_tokenizer): """Create a GptOssReasoningParser instance.""" return GptOssReasoningParser(mock_tokenizer) @pytest.fixture def mock_tool_server_empty(self): """Create a mock ToolServer with no tools.""" tool_server = Mock(spec=ToolServer) tool_server.has_tool = Mock(return_value=False) return tool_server @pytest.fixture def mock_tool_server_with_browser(self): """Create a mock ToolServer with browser tool.""" tool_server = Mock(spec=ToolServer) tool_server.has_tool = Mock(side_effect=lambda tool: tool == "browser") return tool_server @pytest.fixture def mock_tool_server_with_all_tools(self): """Create a mock ToolServer with all builtin tools.""" tool_server = Mock(spec=ToolServer) tool_server.has_tool = Mock( side_effect=lambda tool: tool in ["browser", "python", "container"] ) return tool_server def test_prepare_structured_tag_no_tool_server(self, reasoning_parser): """Test prepare_structured_tag with no tool server.""" result = reasoning_parser.prepare_structured_tag(None, None) expected = json.dumps(no_func_reaonsing_tag) assert result == expected # Verify the structure is correct parsed = json.loads(result) assert parsed["type"] == "structural_tag" assert parsed["format"]["type"] == "triggered_tags" assert len(parsed["format"]["tags"]) == 1 assert parsed["format"]["tags"][0]["begin"] == "<|channel|>analysis<|message|>" assert parsed["format"]["triggers"] == ["<|channel|>analysis"] def test_prepare_structured_tag_with_all_tools( self, reasoning_parser, mock_tool_server_with_all_tools ): """Test prepare_structured_tag with all builtin tools.""" result = reasoning_parser.prepare_structured_tag( None, mock_tool_server_with_all_tools ) parsed = json.loads(result) # Should have analysis tag + tags for all 3 tools (2 tags each) assert len(parsed["format"]["tags"]) == 7 # 1 analysis + 6 tool tags # Check all tool tags are present tag_begins = [tag["begin"] for tag in parsed["format"]["tags"]] for tool in ["browser", "python", "container"]: assert f"<|channel|>commentary to={tool}" in tag_begins assert f"<|channel|>analysis to={tool}" in tag_begins def test_prepare_structured_tag_with_original_tag(self, reasoning_parser): """Test prepare_structured_tag when original_tag is provided.""" original_tag = '{"custom": "tag"}' result = reasoning_parser.prepare_structured_tag(original_tag, None) # Should return the original tag unchanged assert result == original_tag def test_from_builtin_tool_to_tag(self): """Test from_builtin_tool_to_tag function.""" tags = from_builtin_tool_to_tag("python") assert len(tags) == 2 assert tags[0]["begin"] == "<|channel|>commentary to=python" assert tags[0]["content"]["type"] == "any_text" assert tags[0]["end"] == "<|end|>" assert tags[1]["begin"] == "<|channel|>analysis to=python" assert tags[1]["content"]["type"] == "any_text" assert tags[1]["end"] == "<|end|>" def test_tag_with_builtin_funcs(self): """Test tag_with_builtin_funcs function.""" builtin_tools = ["browser", "python"] result = tag_with_builtin_funcs(no_func_reaonsing_tag, builtin_tools) assert result["type"] == "structural_tag" # Should have original analysis tag + 2 tags per tool assert len(result["format"]["tags"]) == 5 # 1 + 2*2 # Should have added commentary trigger assert "<|channel|>commentary to=" in result["format"]["triggers"] assert "<|channel|>analysis" in result["format"]["triggers"] def test_tag_structure_invariants(self): """Test that the basic tag structure follows expected format.""" # Test the base no_func_reaonsing_tag structure assert no_func_reaonsing_tag["type"] == "structural_tag" assert no_func_reaonsing_tag["format"]["type"] == "triggered_tags" assert no_func_reaonsing_tag["format"]["stop_after_first"] is False # Verify analysis tag structure analysis_tag = no_func_reaonsing_tag["format"]["tags"][0] assert analysis_tag["begin"] == "<|channel|>analysis<|message|>" assert analysis_tag["content"]["type"] == "any_text" assert analysis_tag["end"] == "<|end|>" def test_json_serialization_valid( self, reasoning_parser, mock_tool_server_with_all_tools ): """Test that all generated tags produce valid JSON.""" # Test with no tool server result1 = reasoning_parser.prepare_structured_tag(None, None) json.loads(result1) # Should not raise # Test with empty tool server empty_server = Mock(spec=ToolServer) empty_server.has_tool = Mock(return_value=False) result2 = reasoning_parser.prepare_structured_tag(None, empty_server) json.loads(result2) # Should not raise # Test with tools result3 = reasoning_parser.prepare_structured_tag( None, mock_tool_server_with_all_tools ) json.loads(result3) # Should not raise @pytest.mark.parametrize("tool_name", ["browser", "python", "container"]) def test_single_tool_integration(self, reasoning_parser, tool_name): """Test integration with individual tools.""" tool_server = Mock(spec=ToolServer) tool_server.has_tool = Mock(side_effect=lambda tool: tool == tool_name) result = reasoning_parser.prepare_structured_tag(None, tool_server) parsed = json.loads(result) # Should have 1 analysis + 2 tool-specific tags assert len(parsed["format"]["tags"]) == 3 tag_begins = [tag["begin"] for tag in parsed["format"]["tags"]] assert f"<|channel|>commentary to={tool_name}" in tag_begins assert f"<|channel|>analysis to={tool_name}" in tag_begins
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/metrics/test_perf_metrics.py
tests/v1/metrics/test_perf_metrics.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Tests for the analytic estimators in metrics/flops.py. """ import types from types import SimpleNamespace from transformers.models.deepseek_v3.configuration_deepseek_v3 import DeepseekV3Config from transformers.models.llama4.configuration_llama4 import ( Llama4Config, Llama4TextConfig, ) from transformers.models.qwen3.configuration_qwen3 import Qwen3Config from transformers.models.qwen3_moe.configuration_qwen3_moe import Qwen3MoeConfig from vllm.config.model import ModelConfig, get_hf_text_config from vllm.transformers_utils.model_arch_config_convertor import ( MODEL_ARCH_CONFIG_CONVERTORS, ModelArchConfigConvertorBase, ) from vllm.v1.metrics.perf import ( AttentionMetrics, BaseConfigParser, ExecutionContext, FfnMetrics, ModelMetrics, ParsedArgs, UnembedMetrics, ) class MockModelConfig: """Mock ModelConfig that implements the getter methods used by parsers.""" def __init__(self, hf_config, dtype): self.hf_config = hf_config self.hf_text_config = get_hf_text_config(hf_config) convertor_cls = MODEL_ARCH_CONFIG_CONVERTORS.get( self.hf_config.model_type, ModelArchConfigConvertorBase ) self.model_arch_config = convertor_cls( self.hf_config, self.hf_text_config ).convert() self.dtype = dtype self.is_attention_free = False def __getattr__(self, name): # 1. Check if ModelConfig actually has this attribute if not hasattr(ModelConfig, name): raise AttributeError( f"'{type(self).__name__}' object has no attribute '{name}' " f"and neither does 'ModelConfig'." ) # 2. Fetch the attribute from the ModelConfig CLASS attr = getattr(ModelConfig, name) # 3. Case A: It is a @property if isinstance(attr, property): # Manually invoke the property's getter, passing 'self' (this mock instance) return attr.__get__(self, self.__class__) # 4. Case B: It is a standard method (function) if isinstance(attr, types.FunctionType): # Bind the function to 'self' so it acts like a method of # this instance. This creates a bound method where 'self' is # automatically passed as the first arg. return types.MethodType(attr, self) # 5. Case C: It is a class attribute / static variable return attr def create_mock_vllm_config( hf_config, model_dtype="bfloat16", cache_dtype="auto", quant_config=None, data_parallel_size=1, tensor_parallel_size=1, pipeline_parallel_size=1, enable_expert_parallel=False, ) -> SimpleNamespace: vllm_config = SimpleNamespace() vllm_config.model_config = MockModelConfig(hf_config, model_dtype) vllm_config.cache_config = SimpleNamespace() vllm_config.cache_config.cache_dtype = cache_dtype vllm_config.quant_config = quant_config vllm_config.parallel_config = SimpleNamespace() vllm_config.parallel_config.data_parallel_size = data_parallel_size vllm_config.parallel_config.tensor_parallel_size = tensor_parallel_size vllm_config.parallel_config.pipeline_parallel_size = pipeline_parallel_size vllm_config.parallel_config.enable_expert_parallel = enable_expert_parallel return vllm_config #### Parser Tests #### def test_base_config_parser(): """Test BaseConfigParser extracts base model attributes correctly.""" hf_config = Qwen3Config( vocab_size=50000, hidden_size=2048, num_attention_heads=16, num_hidden_layers=24, ) vllm_config = create_mock_vllm_config(hf_config, model_dtype="float16") parser = BaseConfigParser() args = ParsedArgs() result = parser.parse(args, vllm_config) assert result.vocab_size == 50000 assert result.hidden_size == 2048 assert result.num_attention_heads == 16 assert result.num_hidden_layers == 24 assert result.weight_byte_size == 2 # float16 is 2 bytes assert result.activation_byte_size == 2 # default activation size def test_base_attention_config_parser_with_gqa(): """Test BaseAttentionConfigParser with grouped query attention.""" hf_config = Qwen3Config( hidden_size=4096, num_attention_heads=32, num_key_value_heads=8, # GQA with 4:1 ratio head_dim=128, ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = AttentionMetrics.get_parser() result = parser_chain.parse(vllm_config) assert result.num_key_value_heads == 8 assert result.head_dim == 128 def test_base_attention_config_parser_without_gqa(): """ Test BaseAttentionConfigParser defaults to MHA when num_key_value_heads not specified. """ hf_config = Qwen3Config( hidden_size=4096, num_attention_heads=32, # No num_key_value_heads specified ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = AttentionMetrics.get_parser() result = parser_chain.parse(vllm_config) # Should default to MHA (num_key_value_heads = num_attention_heads) assert result.num_key_value_heads == 32 def test_base_ffn_config_parser_dense(): """Test BaseFfnConfigParser for dense FFN.""" hf_config = Qwen3Config( hidden_size=4096, intermediate_size=11008, num_hidden_layers=32, ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = FfnMetrics.get_parser() result = parser_chain.parse(vllm_config) assert result.intermediate_size == 11008 assert result.num_experts == 0 assert result.num_experts_per_tok == 0 assert result.num_moe_layers == 0 # No MoE def test_base_ffn_config_parser_moe(): """Test BaseFfnConfigParser for MoE FFN.""" hf_config = Qwen3MoeConfig( hidden_size=4096, intermediate_size=11008, num_hidden_layers=32, num_experts=64, num_experts_per_tok=8, moe_intermediate_size=14336, n_shared_experts=2, ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = FfnMetrics.get_parser() result = parser_chain.parse(vllm_config) assert result.num_experts == 64 assert result.num_experts_per_tok == 8 assert result.moe_intermediate_size == 14336 assert result.num_shared_experts == 2 assert result.num_moe_layers == 32 # All layers are MoE by default def test_interleave_moe_layer_step_parser(): """Test InterleaveMoeLayerStepParser correctly computes MoE layer count.""" hf_config = Llama4Config( text_config=Llama4TextConfig( num_hidden_layers=32, num_local_experts=64, interleave_moe_layer_step=4, # Every 4th layer is MoE ), ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = FfnMetrics.get_parser() result = parser_chain.parse(vllm_config) assert result.num_moe_layers == 8 def test_moe_layer_freq_parser(): """Test MoeLayerFreqParser correctly computes MoE layer count.""" hf_config = DeepseekV3Config( num_hidden_layers=30, n_routed_experts=64, moe_layer_freq=3, # Every 3rd layer after first_k_dense_replace first_k_dense_replace=6, # First 6 layers are dense ) vllm_config = create_mock_vllm_config(hf_config) parser_chain = FfnMetrics.get_parser() result = parser_chain.parse(vllm_config) # Layers >= 6 and divisible by 3: 6, 9, 12, 15, 18, 21, 24, 27 expected_moe_layers = len( [layer for layer in range(30) if layer >= 6 and layer % 3 == 0] ) assert expected_moe_layers == 8 assert result.num_moe_layers == expected_moe_layers #### ComponentMetrics Tests #### def test_attention_metrics_scaling(): """Test that attention metrics scale proportionally with model dimensions.""" base_hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_key_value_heads=16, num_hidden_layers=12, head_dim=128, ) base_vllm_config = create_mock_vllm_config(base_hf_config) base_metrics = AttentionMetrics.from_vllm_config(base_vllm_config) # Test scaling with number of layers double_layers_hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_key_value_heads=16, num_hidden_layers=24, # Double the layers head_dim=128, ) double_layers_vllm_config = create_mock_vllm_config(double_layers_hf_config) double_layers_metrics = AttentionMetrics.from_vllm_config(double_layers_vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # FLOPS should double when layers double base_flops = base_metrics.get_num_flops(ctx) double_flops = double_layers_metrics.get_num_flops(ctx) assert double_flops == 2 * base_flops # Read/write bytes should also scale proportionally base_read = base_metrics.get_read_bytes(ctx) double_read = double_layers_metrics.get_read_bytes(ctx) assert double_read == 2 * base_read base_write = base_metrics.get_write_bytes(ctx) double_write = double_layers_metrics.get_write_bytes(ctx) assert double_write == 2 * base_write def test_attention_metrics_grouped_query(): """Test attention metrics handle grouped query attention correctly.""" mha_hf_config = Qwen3Config( hidden_size=4096, num_attention_heads=32, num_key_value_heads=32, # MHA num_hidden_layers=1, ) mha_config = create_mock_vllm_config(mha_hf_config) gqa_hf_config = Qwen3Config( hidden_size=4096, num_attention_heads=32, num_key_value_heads=8, # GQA with 4:1 ratio num_hidden_layers=1, ) gqa_config = create_mock_vllm_config(gqa_hf_config) mha_metrics = AttentionMetrics.from_vllm_config(mha_config) gqa_metrics = AttentionMetrics.from_vllm_config(gqa_config) ctx = ExecutionContext.from_single_request( num_tokens=1, context_len=1024, is_prefill=False ) # GQA should have less KV cache reads since fewer KV heads mha_read = mha_metrics.get_read_bytes(ctx) gqa_read = gqa_metrics.get_read_bytes(ctx) assert gqa_read < mha_read def test_ffn_metrics_scaling(): """Test FFN metrics scale proportionally with model dimensions.""" base_hf_config = Qwen3Config( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, ) base_vllm_config = create_mock_vllm_config(base_hf_config) base_metrics = FfnMetrics.from_vllm_config(base_vllm_config) # Test scaling with intermediate size larger_ffn_hf_config = Qwen3Config( hidden_size=2048, intermediate_size=16384, # Double intermediate size num_hidden_layers=12, ) larger_ffn_vllm_config = create_mock_vllm_config(larger_ffn_hf_config) larger_ffn_metrics = FfnMetrics.from_vllm_config(larger_ffn_vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # FLOPS should double when intermediate size doubles base_flops = base_metrics.get_num_flops(ctx) larger_flops = larger_ffn_metrics.get_num_flops(ctx) assert larger_flops == base_flops * 2 def test_moe_metrics_vs_dense(): """Test MoE metrics versus dense metrics.""" dense_hf_config = Qwen3Config( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, ) dense_config = create_mock_vllm_config(dense_hf_config) moe_hf_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, num_experts=64, num_experts_per_tok=2, # 2 routed expert moe_intermediate_size=8192, n_shared_experts=0, ) moe_config = create_mock_vllm_config(moe_hf_config) dense_metrics = FfnMetrics.from_vllm_config(dense_config) moe_metrics = FfnMetrics.from_vllm_config(moe_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # MoE should have different compute/memory characteristics dense_flops = dense_metrics.get_num_flops(ctx) moe_flops = moe_metrics.get_num_flops(ctx) # 2 routed experts vs 1 dense. assert moe_flops == dense_flops * 2 def test_unembed_metrics_scaling(): """Test unembedding metrics scale with vocab size.""" small_vocab_hf_config = Qwen3Config( hidden_size=2048, vocab_size=32000, ) small_vocab_config = create_mock_vllm_config(small_vocab_hf_config) large_vocab_hf_config = Qwen3Config( hidden_size=2048, vocab_size=64000, # Double vocab size ) large_vocab_config = create_mock_vllm_config(large_vocab_hf_config) small_vocab_metrics = UnembedMetrics.from_vllm_config(small_vocab_config) large_vocab_metrics = UnembedMetrics.from_vllm_config(large_vocab_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # FLOPS should double when vocab size doubles small_flops = small_vocab_metrics.get_num_flops(ctx) large_flops = large_vocab_metrics.get_num_flops(ctx) assert large_flops == 2 * small_flops def test_prefill_vs_decode_differences(): """Test that prefill and decode have different memory access patterns.""" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_key_value_heads=16, num_hidden_layers=1, ) config = create_mock_vllm_config(hf_config) metrics = AttentionMetrics.from_vllm_config(config) prefill_ctx = ExecutionContext.from_single_request( num_tokens=512, context_len=512, is_prefill=True ) decode_ctx = ExecutionContext.from_single_request( num_tokens=1, context_len=512, is_prefill=False ) prefill_read = metrics.get_read_bytes(prefill_ctx) decode_read = metrics.get_read_bytes(decode_ctx) assert prefill_read != decode_read def test_model_metrics_aggregation(): """Test ModelMetrics correctly aggregates across components.""" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_hidden_layers=12, vocab_size=32000, intermediate_size=8192, ) config = create_mock_vllm_config(hf_config) model_metrics = ModelMetrics(config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Should have metrics for attention, ffn, and unembed total_flops = model_metrics.get_num_flops(ctx) breakdown = model_metrics.get_num_flops_breakdown(ctx) # Breakdown should sum to total assert total_flops == sum(breakdown.values()) def test_moe_expert_activation_proportional_scaling(): """Test that routed expert metrics scale proportionally with num_experts_per_tok.""" base_moe_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, num_experts=64, num_experts_per_tok=1, # 1 expert per token moe_intermediate_size=8192, n_shared_experts=2, ) double_experts_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, num_experts=64, num_experts_per_tok=2, # 2 experts per token (double) moe_intermediate_size=8192, n_shared_experts=2, # Same shared experts ) triple_experts_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, num_experts=64, num_experts_per_tok=3, # 3 experts per token (triple) moe_intermediate_size=8192, n_shared_experts=2, # Same shared experts ) base_vllm_config = create_mock_vllm_config(base_moe_config) double_vllm_config = create_mock_vllm_config(double_experts_config) triple_vllm_config = create_mock_vllm_config(triple_experts_config) base_metrics = FfnMetrics.from_vllm_config(base_vllm_config) double_metrics = FfnMetrics.from_vllm_config(double_vllm_config) triple_metrics = FfnMetrics.from_vllm_config(triple_vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Get total metrics - the key insight is that differences should be proportional base_flops = base_metrics.get_num_flops(ctx) double_flops = double_metrics.get_num_flops(ctx) triple_flops = triple_metrics.get_num_flops(ctx) # The difference between double and base should equal one additional expert one_expert_diff = double_flops - base_flops # The difference between triple and base should equal two additional experts two_expert_diff = triple_flops - base_flops # Proportional scaling: 2 * (1 expert diff) should equal (2 expert diff) assert two_expert_diff == 2 * one_expert_diff # Same logic applies to memory operations base_read = base_metrics.get_read_bytes(ctx) double_read = double_metrics.get_read_bytes(ctx) triple_read = triple_metrics.get_read_bytes(ctx) one_expert_read_diff = double_read - base_read two_expert_read_diff = triple_read - base_read assert two_expert_read_diff == 2 * one_expert_read_diff # Same for write bytes base_write = base_metrics.get_write_bytes(ctx) double_write = double_metrics.get_write_bytes(ctx) triple_write = triple_metrics.get_write_bytes(ctx) one_expert_write_diff = double_write - base_write two_expert_write_diff = triple_write - base_write assert two_expert_write_diff == 2 * one_expert_write_diff def test_quantization_config_parser_fp8(): """Test quantization parsers with fp8.""" class MockQuantConfig: def get_name(self): return "fp8" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_hidden_layers=1 ) vllm_config = create_mock_vllm_config(hf_config, quant_config=MockQuantConfig()) attn_result = AttentionMetrics.get_parser().parse(vllm_config) assert attn_result.weight_byte_size == 1 # fp8 ffn_result = FfnMetrics.get_parser().parse(vllm_config) assert ffn_result.weight_byte_size == 1 # fp8 def test_quantization_config_parser_mxfp4(): """Test quantization parsers with mxfp4.""" class MockQuantConfig: def get_name(self): return "mxfp4" hf_config = Qwen3Config( hidden_size=2048, intermediate_size=8192, num_hidden_layers=1 ) vllm_config = create_mock_vllm_config(hf_config, quant_config=MockQuantConfig()) ffn_result = FfnMetrics.get_parser().parse(vllm_config) assert ffn_result.weight_byte_size == 0.5 # mxfp4 #### Per-GPU Tests #### def test_attention_per_gpu_with_tensor_parallelism(): """Test attention metrics with tensor parallelism - per_gpu vs global.""" hf_config = Qwen3Config( hidden_size=4096, num_attention_heads=32, num_key_value_heads=8, num_hidden_layers=24, ) # Test with TP=4 vllm_config = create_mock_vllm_config(hf_config, tensor_parallel_size=4) metrics = AttentionMetrics.from_vllm_config(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=128, context_len=1024, is_prefill=True ) # Get global and per-gpu metrics global_flops = metrics.get_num_flops(ctx, per_gpu=False) per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) # With TP=4, global flops should be 4x per-gpu flops (heads divided by 4) assert global_flops == 4 * per_gpu_flops # Same for read/write bytes global_read = metrics.get_read_bytes(ctx, per_gpu=False) per_gpu_read = metrics.get_read_bytes(ctx, per_gpu=True) # Reads should scale similarly (weight reads are divided by TP) assert global_read > per_gpu_read global_write = metrics.get_write_bytes(ctx, per_gpu=False) per_gpu_write = metrics.get_write_bytes(ctx, per_gpu=True) assert global_write > per_gpu_write def test_attention_per_gpu_with_pipeline_parallelism(): """Test attention metrics with pipeline parallelism - per_gpu vs global.""" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_hidden_layers=32, ) # Test with PP=4 vllm_config = create_mock_vllm_config(hf_config, pipeline_parallel_size=4) metrics = AttentionMetrics.from_vllm_config(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=False ) # Get global and per-gpu metrics global_flops = metrics.get_num_flops(ctx, per_gpu=False) per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) # With PP=4, global flops should be 4x per-gpu flops (layers divided by 4) assert global_flops == 4 * per_gpu_flops global_read = metrics.get_read_bytes(ctx, per_gpu=False) per_gpu_read = metrics.get_read_bytes(ctx, per_gpu=True) assert global_read == 4 * per_gpu_read def test_ffn_per_gpu_with_tensor_parallelism(): """Test FFN metrics with tensor parallelism - per_gpu vs global.""" hf_config = Qwen3Config( hidden_size=4096, intermediate_size=14336, num_hidden_layers=32, ) # Test with DP=2, TP=4 (ffn_tp_size will be 8) vllm_config = create_mock_vllm_config( hf_config, data_parallel_size=2, tensor_parallel_size=4, ) metrics = FfnMetrics.from_vllm_config(vllm_config) # ffn_tp_size should be dp_size * tp_size = 8 (when EP not enabled) assert metrics.ffn_tp_size == 8 ctx = ExecutionContext.from_single_request( num_tokens=128, context_len=2048, is_prefill=True ) # Get global and per-gpu metrics global_flops = metrics.get_num_flops(ctx, per_gpu=False) per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) # With ffn_tp_size=8, global should be 8x per-gpu assert global_flops == 8 * per_gpu_flops def test_ffn_per_gpu_with_pipeline_parallelism(): """Test FFN metrics with pipeline parallelism - per_gpu vs global.""" hf_config = Qwen3Config( hidden_size=2048, intermediate_size=8192, num_hidden_layers=24, ) # Test with PP=6 vllm_config = create_mock_vllm_config(hf_config, pipeline_parallel_size=6) metrics = FfnMetrics.from_vllm_config(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Get global and per-gpu metrics global_flops = metrics.get_num_flops(ctx, per_gpu=False) per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) # With PP=6, global should be 6x per-gpu (layers divided by 6) assert global_flops == 6 * per_gpu_flops def test_moe_per_gpu_with_expert_parallelism(): """ Test MoE metrics with expert parallelism - verifies num_activated_experts bug fix. """ hf_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=24, num_experts=64, num_experts_per_tok=8, moe_intermediate_size=14336, n_shared_experts=2, ) # Test with DP=2, TP=4, EP enabled (ffn_ep_size will be 8) vllm_config = create_mock_vllm_config( hf_config, data_parallel_size=2, tensor_parallel_size=4, enable_expert_parallel=True, ) metrics = FfnMetrics.from_vllm_config(vllm_config) # When EP enabled, ffn_ep_size = dp_size * tp_size = 8 assert metrics.ffn_ep_size == 8 assert metrics.ffn_tp_size == 1 ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Get per-gpu metrics per_gpu_read_breakdown = metrics.get_read_bytes_breakdown(ctx, per_gpu=True) global_read_breakdown = metrics.get_read_bytes_breakdown(ctx, per_gpu=False) # Verify that routed expert weight reads are reasonable # With per_gpu=True, each GPU has 64/8 = 8 experts # T=100, E_per_gpu=8/8=1, so T*E=100 expert activations # num_activated_experts should be min(100, 8) = 8 # Check that weight reads scale appropriately # Global has all 64 experts, per-gpu has 8 experts # So weight reads should reflect this difference if "routed_up_gate_weights" in per_gpu_read_breakdown: per_gpu_weight_reads = per_gpu_read_breakdown["routed_up_gate_weights"] global_weight_reads = global_read_breakdown["routed_up_gate_weights"] # The ratio should reflect the expert count difference # This verifies the bug fix works correctly assert per_gpu_weight_reads < global_weight_reads # Global should read more experts than per-gpu # Exact ratio depends on num_activated_experts calculation ratio = global_weight_reads / per_gpu_weight_reads # Should be > 1 since global has more experts to read assert ratio > 1 def test_moe_per_gpu_expert_activation_accounting(): """ Test that MoE correctly accounts for expert activations with small batch sizes. """ hf_config = Qwen3MoeConfig( hidden_size=2048, intermediate_size=8192, num_hidden_layers=12, num_experts=64, num_experts_per_tok=8, moe_intermediate_size=14336, n_shared_experts=0, # No shared experts for this test ) # Test with EP=8 vllm_config = create_mock_vllm_config( hf_config, data_parallel_size=8, enable_expert_parallel=True, ) metrics = FfnMetrics.from_vllm_config(vllm_config) # Small batch: T=10, E_per_gpu=8/8=1 # Each GPU: T*E = 10*1 = 10 activations # Experts per GPU: 64/8 = 8 # So num_activated_experts should be min(10, 8) = 8 small_ctx = ExecutionContext.from_single_request( num_tokens=10, context_len=512, is_prefill=True ) small_read = metrics.get_read_bytes_breakdown(small_ctx, per_gpu=True) # Large batch: T=1000, E_per_gpu=1 # Each GPU: T*E = 1000*1 = 1000 activations # Experts per GPU: 8 # So num_activated_experts should be min(1000, 8) = 8 (all experts activated) large_ctx = ExecutionContext.from_single_request( num_tokens=1000, context_len=512, is_prefill=True ) large_read = metrics.get_read_bytes_breakdown(large_ctx, per_gpu=True) # Weight reads should be similar (both activate all 8 experts per GPU) # But activation reads should differ (proportional to T*E) if "routed_up_gate_weights" in small_read: small_weight = small_read["routed_up_gate_weights"] large_weight = large_read["routed_up_gate_weights"] # Weight reads should be the same (both read all 8 experts) assert small_weight == large_weight # But input activation reads should scale with T*E small_input = small_read["routed_up_gate_input"] large_input = large_read["routed_up_gate_input"] assert large_input == 100 * small_input # 1000/10 = 100x def test_unembed_per_gpu_with_tensor_parallelism(): """Test unembed metrics with tensor parallelism - per_gpu vs global.""" hf_config = Qwen3Config( hidden_size=4096, vocab_size=128000, ) # Test with TP=8 vllm_config = create_mock_vllm_config(hf_config, tensor_parallel_size=8) metrics = UnembedMetrics.from_vllm_config(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Get global and per-gpu metrics global_flops = metrics.get_num_flops(ctx, per_gpu=False) per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) # With TP=8, vocab is divided by 8, so global should be 8x per-gpu assert global_flops == 8 * per_gpu_flops # For read bytes, weight reads scale with TP but input reads don't (replicated) global_read_breakdown = metrics.get_read_bytes_breakdown(ctx, per_gpu=False) per_gpu_read_breakdown = metrics.get_read_bytes_breakdown(ctx, per_gpu=True) # Input reads should be the same (replicated across TP ranks) assert global_read_breakdown["input"] == per_gpu_read_breakdown["input"] # Weight reads should scale 8x (divided by TP) assert global_read_breakdown["weight"] == 8 * per_gpu_read_breakdown["weight"] def test_model_metrics_per_gpu_aggregation(): """Test ModelMetrics correctly aggregates per_gpu metrics across components.""" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=16, num_hidden_layers=12, vocab_size=32000, intermediate_size=8192, ) # Test with mixed parallelism: TP=2, PP=2 vllm_config = create_mock_vllm_config( hf_config, tensor_parallel_size=2, pipeline_parallel_size=2, ) model_metrics = ModelMetrics(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=100, context_len=512, is_prefill=True ) # Get breakdowns for both modes per_gpu_breakdown = model_metrics.get_num_flops_breakdown(ctx, per_gpu=True) global_breakdown = model_metrics.get_num_flops_breakdown(ctx, per_gpu=False) # Verify breakdown sums match totals per_gpu_total = model_metrics.get_num_flops(ctx, per_gpu=True) global_total = model_metrics.get_num_flops(ctx, per_gpu=False) assert per_gpu_total == sum(per_gpu_breakdown.values()) assert global_total == sum(global_breakdown.values()) # Global should be larger than per-gpu due to parallelism assert global_total > per_gpu_total # With TP=2 and PP=2, the ratio depends on which parallelism applies to # which component but we can verify that global is reasonably larger ratio = global_total / per_gpu_total assert ratio > 1 # Should be between PP and TP*PP depending on component mix def test_attention_per_gpu_heads_not_evenly_divisible(): """Test attention with heads not evenly divisible by TP.""" hf_config = Qwen3Config( hidden_size=2048, num_attention_heads=17, # Not divisible by 4 num_key_value_heads=5, # Not divisible by 4 num_hidden_layers=8, ) vllm_config = create_mock_vllm_config(hf_config, tensor_parallel_size=4) metrics = AttentionMetrics.from_vllm_config(vllm_config) ctx = ExecutionContext.from_single_request( num_tokens=64, context_len=256, is_prefill=True ) # Should not crash and should handle max(1, ...) correctly per_gpu_flops = metrics.get_num_flops(ctx, per_gpu=True) global_flops = metrics.get_num_flops(ctx, per_gpu=False) # Both should be positive assert per_gpu_flops > 0 assert global_flops > 0 assert global_flops > per_gpu_flops
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/metrics/test_stats.py
tests/v1/metrics/test_stats.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm.v1.engine import FinishReason from vllm.v1.metrics.stats import IterationStats, RequestStateStats def test_iteration_stats_repr(): iteration_stats = IterationStats() assert repr(iteration_stats).startswith("IterationStats(") def test_prefill_kv_computed_with_cache(): """Test that prefill KV compute correctly excludes cached tokens.""" iteration_stats = IterationStats() req_stats = RequestStateStats(arrival_time=0.0) req_stats.scheduled_ts = 0.1 req_stats.first_token_ts = 0.5 req_stats.last_token_ts = 5.0 req_stats.num_generation_tokens = 50 # Case 1: With prefix cache (1200 tokens cached) iteration_stats.update_from_finished_request( finish_reason=FinishReason.STOP, num_prompt_tokens=10000, max_tokens_param=100, req_stats=req_stats, num_cached_tokens=1200, ) finished_req = iteration_stats.finished_requests[0] assert finished_req.num_prompt_tokens == 10000 assert finished_req.num_cached_tokens == 1200 # Verify calculation: prefill KV = prompt tokens - cached tokens prefill_kv_computed = finished_req.num_prompt_tokens - max( finished_req.num_cached_tokens, 0 ) assert prefill_kv_computed == 8800 # 10000 - 1200 def test_prefill_kv_computed_no_cache(): """Test prefill KV compute without prefix caching.""" iteration_stats = IterationStats() req_stats = RequestStateStats(arrival_time=0.0) req_stats.scheduled_ts = 0.1 req_stats.first_token_ts = 0.5 req_stats.last_token_ts = 2.0 req_stats.num_generation_tokens = 10 # Case 2: No prefix cache iteration_stats.update_from_finished_request( finish_reason=FinishReason.STOP, num_prompt_tokens=2000, max_tokens_param=100, req_stats=req_stats, num_cached_tokens=0, ) finished_req = iteration_stats.finished_requests[0] assert finished_req.num_prompt_tokens == 2000 assert finished_req.num_cached_tokens == 0 # Verify calculation: prefill KV = full prompt when no cache prefill_kv_computed = finished_req.num_prompt_tokens - max( finished_req.num_cached_tokens, 0 ) assert prefill_kv_computed == 2000 def test_prefill_kv_computed_edge_cases(): """Test edge cases for prefill KV compute calculation.""" iteration_stats = IterationStats() req_stats = RequestStateStats(arrival_time=0.0) req_stats.scheduled_ts = 0.1 req_stats.first_token_ts = 0.5 req_stats.last_token_ts = 1.0 req_stats.num_generation_tokens = 1 # Case 3: Negative num_cached_tokens (shouldn't happen, but handle gracefully) iteration_stats.update_from_finished_request( finish_reason=FinishReason.STOP, num_prompt_tokens=100, max_tokens_param=10, req_stats=req_stats, num_cached_tokens=-1, ) finished_req = iteration_stats.finished_requests[0] # max() should handle negative values prefill_kv_computed = finished_req.num_prompt_tokens - max( finished_req.num_cached_tokens, 0 ) assert prefill_kv_computed == 100 # Should treat negative as 0 # Case 4: All tokens cached (shouldn't happen in practice) iteration_stats2 = IterationStats() iteration_stats2.update_from_finished_request( finish_reason=FinishReason.STOP, num_prompt_tokens=100, max_tokens_param=10, req_stats=req_stats, num_cached_tokens=100, ) finished_req2 = iteration_stats2.finished_requests[0] prefill_kv_computed2 = finished_req2.num_prompt_tokens - max( finished_req2.num_cached_tokens, 0 ) assert prefill_kv_computed2 == 0 # All cached, nothing computed
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/metrics/test_ray_metrics.py
tests/v1/metrics/test_ray_metrics.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import ray from vllm.config.model import ModelDType from vllm.sampling_params import SamplingParams from vllm.v1.engine.async_llm import AsyncEngineArgs, AsyncLLM from vllm.v1.metrics.ray_wrappers import RayPrometheusMetric, RayPrometheusStatLogger MODELS = [ "distilbert/distilgpt2", ] @pytest.mark.parametrize("model", MODELS) @pytest.mark.parametrize("dtype", ["half"]) @pytest.mark.parametrize("max_tokens", [16]) def test_engine_log_metrics_ray( example_prompts, model: str, dtype: ModelDType, max_tokens: int, ) -> None: """Simple smoke test, verifying this can be used without exceptions. Need to start a Ray cluster in order to verify outputs.""" @ray.remote(num_gpus=1) class EngineTestActor: async def run(self): engine_args = AsyncEngineArgs( model=model, dtype=dtype, disable_log_stats=False, enforce_eager=True ) engine = AsyncLLM.from_engine_args( engine_args, stat_loggers=[RayPrometheusStatLogger] ) for i, prompt in enumerate(example_prompts): results = engine.generate( request_id=f"request-id-{i}", prompt=prompt, sampling_params=SamplingParams(max_tokens=max_tokens), ) async for _ in results: pass # Create the actor and call the async method actor = EngineTestActor.remote() # type: ignore[attr-defined] ray.get(actor.run.remote()) def test_sanitized_opentelemetry_name(): """Test the metric name sanitization logic for Ray.""" # Only a-z, A-Z, 0-9, _, test valid characters are preserved valid_name = "valid_metric_123_abcDEF" assert ( RayPrometheusMetric._get_sanitized_opentelemetry_name(valid_name) == valid_name ) # Test dash, dot, are replaced name_with_dash_dot = "metric-name.test" expected = "metric_name_test" assert ( RayPrometheusMetric._get_sanitized_opentelemetry_name(name_with_dash_dot) == expected ) # Test colon is replaced with underscore name_with_colon = "metric:name" expected = "metric_name" assert ( RayPrometheusMetric._get_sanitized_opentelemetry_name(name_with_colon) == expected ) # Test multiple invalid characters are replaced name_with_invalid = "metric:name@with#special%chars" expected = "metric_name_with_special_chars" assert ( RayPrometheusMetric._get_sanitized_opentelemetry_name(name_with_invalid) == expected ) # Test mixed valid and invalid characters complex_name = "vllm:engine_stats/time.latency_ms-99p" expected = "vllm_engine_stats_time_latency_ms_99p" assert ( RayPrometheusMetric._get_sanitized_opentelemetry_name(complex_name) == expected ) # Test empty string assert RayPrometheusMetric._get_sanitized_opentelemetry_name("") == ""
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/metrics/test_metrics_reader.py
tests/v1/metrics/test_metrics_reader.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import prometheus_client import pytest from vllm.v1.metrics.reader import ( Counter, Gauge, Histogram, Vector, get_metrics_snapshot, ) pytestmark = pytest.mark.cpu_test @pytest.fixture(autouse=True) def test_registry(monkeypatch): # Use a custom registry for tests test_registry = prometheus_client.CollectorRegistry(auto_describe=True) monkeypatch.setattr("vllm.v1.metrics.reader.REGISTRY", test_registry) return test_registry @pytest.mark.parametrize("num_engines", [1, 4]) def test_gauge_metric(test_registry, num_engines): g = prometheus_client.Gauge( "vllm:test_gauge", "Test gauge metric", labelnames=["model", "engine_index"], registry=test_registry, ) for i in range(num_engines): g.labels(model="foo", engine_index=str(i)).set(98.5) metrics = get_metrics_snapshot() assert len(metrics) == num_engines engine_labels = [str(i) for i in range(num_engines)] for m in metrics: assert isinstance(m, Gauge) assert m.name == "vllm:test_gauge" assert m.value == 98.5 assert m.labels["model"] == "foo" assert m.labels["engine_index"] in engine_labels engine_labels.remove(m.labels["engine_index"]) @pytest.mark.parametrize("num_engines", [1, 4]) def test_counter_metric(test_registry, num_engines): c = prometheus_client.Counter( "vllm:test_counter", "Test counter metric", labelnames=["model", "engine_index"], registry=test_registry, ) for i in range(num_engines): c.labels(model="bar", engine_index=str(i)).inc(19) metrics = get_metrics_snapshot() assert len(metrics) == num_engines engine_labels = [str(i) for i in range(num_engines)] for m in metrics: assert isinstance(m, Counter) assert m.name == "vllm:test_counter" assert m.value == 19 assert m.labels["model"] == "bar" assert m.labels["engine_index"] in engine_labels engine_labels.remove(m.labels["engine_index"]) @pytest.mark.parametrize("num_engines", [1, 4]) def test_histogram_metric(test_registry, num_engines): h = prometheus_client.Histogram( "vllm:test_histogram", "Test histogram metric", labelnames=["model", "engine_index"], buckets=[10, 20, 30, 40, 50], registry=test_registry, ) for i in range(num_engines): hist = h.labels(model="blaa", engine_index=str(i)) hist.observe(42) hist.observe(21) hist.observe(7) metrics = get_metrics_snapshot() assert len(metrics) == num_engines engine_labels = [str(i) for i in range(num_engines)] for m in metrics: assert isinstance(m, Histogram) assert m.name == "vllm:test_histogram" assert m.count == 3 assert m.sum == 70 assert m.buckets["10.0"] == 1 assert m.buckets["20.0"] == 1 assert m.buckets["30.0"] == 2 assert m.buckets["40.0"] == 2 assert m.buckets["50.0"] == 3 assert m.labels["model"] == "blaa" assert m.labels["engine_index"] in engine_labels engine_labels.remove(m.labels["engine_index"]) @pytest.mark.parametrize("num_engines", [1, 4]) def test_vector_metric(test_registry, num_engines): c = prometheus_client.Counter( "vllm:spec_decode_num_accepted_tokens_per_pos", "Vector-like counter metric", labelnames=["position", "model", "engine_index"], registry=test_registry, ) for i in range(num_engines): c.labels(position="0", model="llama", engine_index=str(i)).inc(10) c.labels(position="1", model="llama", engine_index=str(i)).inc(5) c.labels(position="2", model="llama", engine_index=str(i)).inc(1) metrics = get_metrics_snapshot() assert len(metrics) == num_engines engine_labels = [str(i) for i in range(num_engines)] for m in metrics: assert isinstance(m, Vector) assert m.name == "vllm:spec_decode_num_accepted_tokens_per_pos" assert m.values == [10, 5, 1] assert m.labels["model"] == "llama" assert m.labels["engine_index"] in engine_labels engine_labels.remove(m.labels["engine_index"])
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/metrics/test_engine_logger_apis.py
tests/v1/metrics/test_engine_logger_apis.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy import pytest from tests.plugins.vllm_add_dummy_stat_logger.dummy_stat_logger.dummy_stat_logger import ( # noqa E501 DummyStatLogger, ) from vllm.v1.engine.async_llm import AsyncEngineArgs, AsyncLLM from vllm.v1.metrics.ray_wrappers import RayPrometheusStatLogger @pytest.fixture def log_stats_enabled_engine_args(): """ Shared fixture providing common AsyncEngineArgs configuration used across multiple tests. """ return AsyncEngineArgs( model="distilbert/distilgpt2", dtype="half", disable_log_stats=False, enforce_eager=True, ) @pytest.mark.asyncio async def test_async_llm_replace_default_loggers(log_stats_enabled_engine_args): """ RayPrometheusStatLogger should replace the default PrometheusStatLogger """ engine = AsyncLLM.from_engine_args( log_stats_enabled_engine_args, stat_loggers=[RayPrometheusStatLogger] ) assert isinstance(engine.logger_manager.stat_loggers[0], RayPrometheusStatLogger) engine.shutdown() @pytest.mark.asyncio async def test_async_llm_add_to_default_loggers(log_stats_enabled_engine_args): """ It's still possible to use custom stat loggers exclusively by passing disable_log_stats=True in addition to a list of custom stat loggers. """ # Create engine_args with disable_log_stats=True for this test disabled_log_engine_args = copy.deepcopy(log_stats_enabled_engine_args) disabled_log_engine_args.disable_log_stats = True # Disable default loggers; pass custom stat logger to the constructor engine = AsyncLLM.from_engine_args( disabled_log_engine_args, stat_loggers=[DummyStatLogger] ) assert len(engine.logger_manager.stat_loggers) == 2 assert len(engine.logger_manager.stat_loggers[0].per_engine_stat_loggers) == 1 assert isinstance( engine.logger_manager.stat_loggers[0].per_engine_stat_loggers[0], DummyStatLogger, ) # log_stats is still True, since custom stat loggers are used assert engine.log_stats engine.shutdown()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/determinism/conftest.py
tests/v1/determinism/conftest.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import vllm.model_executor.layers.batch_invariant as batch_invariant @pytest.fixture(autouse=True) def enable_batch_invariant_mode(monkeypatch: pytest.MonkeyPatch): """Automatically enable batch invariant kernel overrides for all tests.""" monkeypatch.setattr(batch_invariant, "VLLM_BATCH_INVARIANT", True) monkeypatch.setenv("VLLM_BATCH_INVARIANT", "1")
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/determinism/utils.py
tests/v1/determinism/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os import random import pytest import torch from vllm.attention.utils.fa_utils import flash_attn_supports_mla from vllm.platforms import current_platform from vllm.utils.flashinfer import has_flashinfer skip_unsupported = pytest.mark.skipif( not (current_platform.is_cuda() and current_platform.has_device_capability(80)), # Supports testing on Ampere and Ada Lovelace devices. # Note: For devices with SM < 90, batch invariance does not support CUDA Graphs. reason="Requires CUDA and >= Ampere (SM80)", ) BACKENDS: list[str] = [ "FLASH_ATTN", "TRITON_MLA", ] if has_flashinfer(): BACKENDS.append("FLASHINFER") if flash_attn_supports_mla(): BACKENDS.append("FLASH_ATTN_MLA") DEFAULT_MODEL = "Qwen/Qwen3-1.7B" MLA_MODEL = "deepseek-ai/DeepSeek-V2-Lite-Chat" def resolve_model_name(backend: str) -> str: """Resolve the model name for the given backend.""" model = os.getenv("VLLM_TEST_MODEL", DEFAULT_MODEL) if backend.endswith("MLA") and model == DEFAULT_MODEL: return MLA_MODEL return model def _random_prompt(min_words: int = 1024, max_words: int = 1024 * 2) -> str: # Generate more realistic prompts that will actually produce varied tokens # Use a mix of common English text patterns prompt_templates = [ # Question-answer style "Question: What is the capital of France?\nAnswer: The capital of France is", "Q: How does photosynthesis work?\nA: Photosynthesis is the process by which", "User: Can you explain quantum mechanics?\nAssistant: Quantum mechanics is", # Story/narrative style "Once upon a time in a distant galaxy, there lived", "The old man walked slowly down the street, remembering", "In the year 2157, humanity finally discovered", # Technical/code style "To implement a binary search tree in Python, first we need to", "The algorithm works by iterating through the array and", "Here's how to optimize database queries using indexing:", # Factual/informative style "The Renaissance was a period in European history that", "Climate change is caused by several factors including", "The human brain contains approximately 86 billion neurons which", # Conversational style "I've been thinking about getting a new laptop because", "Yesterday I went to the store and bought", "My favorite thing about summer is definitely", ] # Pick a random template base_prompt = random.choice(prompt_templates) if max_words < min_words: max_words = min_words target_words = random.randint(min_words, max_words) if target_words > 50: # For longer prompts, repeat context padding_text = ( " This is an interesting topic that deserves more explanation. " * (target_words // 50) ) base_prompt = base_prompt + padding_text return base_prompt def _extract_step_logprobs(request_output): if getattr(request_output, "outputs", None): inner = request_output.outputs[0] if hasattr(inner, "logprobs") and inner.logprobs is not None: t = torch.tensor( [ inner.logprobs[i][tid].logprob for i, tid in enumerate(inner.token_ids) ], dtype=torch.float32, ) return t, inner.token_ids return None, None def is_device_capability_below_90() -> bool: return not current_platform.has_device_capability(90)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/determinism/test_batch_invariance.py
tests/v1/determinism/test_batch_invariance.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import contextlib import os import random import pytest import torch from utils import ( BACKENDS, _extract_step_logprobs, _random_prompt, is_device_capability_below_90, resolve_model_name, skip_unsupported, ) import vllm.model_executor.layers.batch_invariant as batch_invariant from vllm import LLM, SamplingParams IS_DEVICE_CAPABILITY_BELOW_90 = is_device_capability_below_90() @skip_unsupported @pytest.mark.timeout(1000) @pytest.mark.parametrize( "backend", BACKENDS, ) def test_v1_generation_is_deterministic_across_batch_sizes_with_needle( backend, ): """ Ensures that the same request (the 'needle' prompt) yields identical output whether run alone (bs=1) or mixed into a larger batch (e.g., bs=64), using the high-level v1 LLM() API only (no manual batching). Strategy: - Create two LLM engines with identical config except max_num_seqs: 1 vs N. - Compute a baseline output for the needle prompt with the bs=1 engine. - For many trials, generate a batch (size N) where the needle appears at a random position among random filler prompts using the bs=N engine. - Track how many trials match vs mismatch, and report totals at the end. The test fails if any mismatches occur, but we still dump pass/fail counts. Notes: - Use seeded stochastic sampling with a fixed seed to test determinism. - Outputs are intentionally longer and sampled at higher temperature/top_p to produce a more random-sounding phrase, yet remain deterministic by seed. - Keep max_tokens and max_model_len bounded for speed and memory use. """ seed = int(os.getenv("VLLM_TEST_SEED", "12345")) random.seed(seed) attention_config = {"backend": backend} # Allow overrides from environment (useful for CI tuning) # "facebook/opt-125m" is too small, doesn't reliably test determinism model = resolve_model_name(backend) num_trials = int(os.getenv("VLLM_NEEDLE_TRIALS", "5")) max_batch_size = int(os.getenv("VLLM_NEEDLE_BATCH_SIZE", "128")) min_random_prompt = int(os.getenv("VLLM_MIN_PROMPT", "1024")) max_random_prompt = int(os.getenv("VLLM_MAX_PROMPT", "2048")) assert max_batch_size >= 2, "Batch size should be >= 2 to mix needle." # Keep GPU memory usage low to avoid startup allocation failures. gpu_mem_util = float(os.getenv("VLLM_GPU_MEMORY_UTILIZATION", "0.4")) max_model_len = int(os.getenv("VLLM_MAX_MODEL_LEN", "5120")) # Sampling parameters: longer outputs with a more random-sounding # continuation,but still deterministic due to fixed seed. temperature = float(os.getenv("VLLM_NEEDLE_TEMPERATURE", "0.0")) top_p = float(os.getenv("VLLM_NEEDLE_TOP_P", "0.95")) max_tokens = int(os.getenv("VLLM_NEEDLE_MAX_TOKENS", "128")) sampling = SamplingParams( temperature=temperature, top_p=top_p, max_tokens=max_tokens, seed=20240919, ) needle_prompt = "There once was a " llm_bs1 = None llm_bsN = None try: # Engine with bs=1 behavior llm_bs1 = LLM_with_max_seqs( model=model, max_num_seqs=max_batch_size, gpu_memory_utilization=gpu_mem_util, max_model_len=max_model_len, attention_config=attention_config, ) # Baseline generation for the needle prompt alone. baseline_out = llm_bs1.generate([needle_prompt], sampling) assert len(baseline_out) == 1 assert len(baseline_out[0].outputs) >= 1 baseline_text = baseline_out[0].outputs[0].text # Engine with larger batch limit (e.g., 64) llm_bsN = LLM_with_max_seqs( model=model, max_num_seqs=max_batch_size, gpu_memory_utilization=gpu_mem_util, max_model_len=max_model_len, attention_config=attention_config, ) mismatches = 0 for trial in range(num_trials): # Create a batch of size `max_batch_size` and insert the needle at # a random index prompts: list[str] = [] batch_size = random.randint(max_batch_size // 2, max_batch_size) needle_pos = random.randint(0, batch_size - 1) for i in range(batch_size): if i == needle_pos: prompts.append(needle_prompt) else: prompts.append(_random_prompt(min_random_prompt, max_random_prompt)) # Generate with the larger-batch engine outputs = llm_bsN.generate(prompts, sampling) # Find the needle output by position needle_output = outputs[needle_pos] assert needle_output.prompt == needle_prompt assert len(needle_output.outputs) >= 1 text = needle_output.outputs[0].text if text != baseline_text: print(f"{text}\n\n== Not the same as ==\n\n{baseline_text}\n\n") mismatches += 1 passes = num_trials - mismatches # Dump how many passed vs failed print( f"[determinism] total={num_trials}, passed={passes}, " f"failed={mismatches}, max_batch_size={max_batch_size}" ) if mismatches > 0: pytest.fail( f"Nondeterministic outputs detected: {mismatches} failed out " f"of {num_trials} trials (max_batch_size={max_batch_size})." ) finally: # Ensure engines are shutdown to free GPU/VRAM across test sessions if llm_bs1 is not None: with contextlib.suppress(Exception): llm_bs1.shutdown() if llm_bsN is not None: with contextlib.suppress(Exception): llm_bsN.shutdown() @skip_unsupported @pytest.mark.parametrize( "backend", BACKENDS, ) def test_logprobs_bitwise_batch_invariance_bs1_vs_bsN( backend, ): seed = int(os.getenv("VLLM_TEST_SEED", "12345")) random.seed(seed) model_name = resolve_model_name(backend) tp_size = int(os.getenv("VLLM_TEST_TP_SIZE", "1")) # For batch invariance, disable custom all-reduce to ensure deterministic # all-reduce operations (custom all-reduce may not be deterministic) from vllm.model_executor.layers.batch_invariant import ( vllm_is_batch_invariant, ) disable_custom_ar = vllm_is_batch_invariant() if disable_custom_ar: print(f"\n{'=' * 80}") print(f"BATCH INVARIANCE MODE: Disabling custom all-reduce (TP={tp_size})") print(f"{'=' * 80}\n") llm = LLM( model=model_name, tensor_parallel_size=tp_size, max_num_seqs=32, max_model_len=8192, dtype="bfloat16", # not everything is supported gpu_memory_utilization=0.9, enforce_eager=IS_DEVICE_CAPABILITY_BELOW_90, attention_config={"backend": backend}, ) # Use more realistic prompts for better token generation prompts = [_random_prompt(10, 50) for i in range(32)] sp = SamplingParams( temperature=0.6, top_p=1.0, max_tokens=8, seed=1234, logprobs=5, ) # BS=1: run prompts individually and collect logprobs per step. print("\n" + "=" * 80) print("STARTING BS=1 RUNS (each prompt individually)") print("=" * 80 + "\n") bs1_logprobs_per_prompt = [] bs1_tokens_per_prompt = [] for idx, p in enumerate(prompts): print(f"\n[BS=1] Running prompt {idx}/{len(prompts)} - Preview: {p[:80]}...") outs = llm.generate([p], sp, use_tqdm=False) assert len(outs) == 1 step_logprobs, token_ids = _extract_step_logprobs(outs[0]) if step_logprobs is None: pytest.skip( "Logits are not available on RequestOutput; " "enable logprobs return to run this test." ) bs1_logprobs_per_prompt.append(step_logprobs) bs1_tokens_per_prompt.append(token_ids) print(f"[BS=1] Prompt {idx} generated tokens: {token_ids}") # BS=N: run prompts in a batch and collect logprobs per step for each # prompt. print("\n" + "=" * 80) print(f"STARTING BS={len(prompts)} RUN (all prompts batched)") print("=" * 80 + "\n") outs_batched = llm.generate(prompts, sp, use_tqdm=False) assert len(outs_batched) == len(prompts) bsN_logprobs_per_prompt = [] bsN_tokens_per_prompt = [] print(f"\n[BS={len(prompts)}] Processing batched outputs...") for idx, o in enumerate(outs_batched): tokens = o.outputs[0].token_ids if o.outputs else "N/A" print(f"[BS={len(prompts)}] Prompt {idx} generated tokens: {tokens}") step_logprobs, token_ids = _extract_step_logprobs(o) if step_logprobs is None: pytest.skip( "Logits are not available on RequestOutput; " "enable logprobs return to run this test." ) bsN_logprobs_per_prompt.append(step_logprobs) bsN_tokens_per_prompt.append(token_ids) # Compare step-by-step logprobs for each prompt between BS=1 and BS=N runs. failed_prompts = [] for i, (logprobs_bs1, logprobs_bsN, tokens_bs1, tokens_bsN) in enumerate( zip( bs1_logprobs_per_prompt, bsN_logprobs_per_prompt, bs1_tokens_per_prompt, bsN_tokens_per_prompt, ) ): if len(logprobs_bs1) != len(logprobs_bsN): reason = ( f"Different number of steps: {len(logprobs_bs1)} (BS=1) " f"vs {len(logprobs_bsN)} (BS=N)" ) failed_prompts.append( { "prompt_idx": i, "step": "all", "reason": reason, "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) continue # Check if tokens match first if tokens_bs1 != tokens_bsN: failed_prompts.append( { "prompt_idx": i, "step": "sampling", "reason": "Different tokens sampled", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, "bs1_all_logprobs": [ logprobs_bs1[s].tolist() for s in range(len(logprobs_bs1)) ], "bsN_all_logprobs": [ logprobs_bsN[s].tolist() for s in range(len(logprobs_bsN)) ], } ) continue for t, (a, b) in enumerate(zip(logprobs_bs1, logprobs_bsN)): if a.shape != b.shape: failed_prompts.append( { "prompt_idx": i, "step": t, "reason": f"Shape mismatch: {a.shape} vs {b.shape}", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) break if not torch.equal(a, b): max_diff = torch.abs(a - b).max().item() # Print which token failed print(f"\n[DIVERGENCE] Prompt {i}, Token {t}: max_diff={max_diff:.6e}") bs1_tok = tokens_bs1[t] if t < len(tokens_bs1) else "N/A" bsN_tok = tokens_bsN[t] if t < len(tokens_bsN) else "N/A" print(f" Token IDs: bs1={bs1_tok}, bsN={bsN_tok}") print(f" BS=1 logprob: {a.tolist()}") print(f" BS=N logprob: {b.tolist()}") failed_prompts.append( { "prompt_idx": i, "step": t, "reason": f"Bitwise mismatch (max_diff={max_diff:.6e})", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, "bs1_all_logprobs": [ logprobs_bs1[s].tolist() for s in range(len(logprobs_bs1)) ], "bsN_all_logprobs": [ logprobs_bsN[s].tolist() for s in range(len(logprobs_bsN)) ], } ) break # Print summary of all failures if failed_prompts: print(f"\n{'=' * 80}") fail_msg = ( f"BATCH INVARIANCE FAILURES: {len(failed_prompts)}/" f"{len(prompts)} prompts failed" ) print(fail_msg) print(f"{'=' * 80}") for fail in failed_prompts: print(f"\nPrompt {fail['prompt_idx']} (step {fail['step']}):") print(f" Reason: {fail['reason']}") print(f" Preview: {fail['prompt_preview']}...") # Always show the tokens if "bs1_tokens" in fail: print(f" BS=1 tokens: {fail['bs1_tokens']}") if "bsN_tokens" in fail: print(f" BS=N tokens: {fail['bsN_tokens']}") if "bs1_all_logprobs" in fail: print(f" BS=1 logprobs for all {len(fail['bs1_all_logprobs'])} steps:") for step_idx, logprobs in enumerate(fail["bs1_all_logprobs"]): print(f" Step {step_idx}: {logprobs}") print(f" BS=N logprobs for all {len(fail['bsN_all_logprobs'])} steps:") for step_idx, logprobs in enumerate(fail["bsN_all_logprobs"]): print(f" Step {step_idx}: {logprobs}") print(f"{'=' * 80}\n") # Fail the test with summary msg = ( f"Batch invariance violated in {len(failed_prompts)}/" f"{len(prompts)} prompts. See output above for details." ) pytest.fail(msg) @skip_unsupported @pytest.mark.parametrize( "backend", BACKENDS, ) def test_simple_generation(backend): """ Simple test that runs the model with a basic prompt and prints the output. Useful for quick smoke testing and debugging. """ model = resolve_model_name(backend) llm = LLM( model=model, max_num_seqs=1, tensor_parallel_size=int(os.getenv("VLLM_TP_SIZE", "1")), gpu_memory_utilization=0.9, max_model_len=2048, dtype="bfloat16", enable_prefix_caching=False, enforce_eager=IS_DEVICE_CAPABILITY_BELOW_90, attention_config={"backend": backend}, ) prompt = "the capital of france is" sampling_params = SamplingParams( temperature=0.0, max_tokens=20, ) print(f"\n{'=' * 80}") print("Running simple generation test") print(f"Prompt: '{prompt}'") print(f"{'=' * 80}\n") try: outputs = llm.generate([prompt], sampling_params) assert len(outputs) == 1 output_text = outputs[0].outputs[0].text print(f"Output: '{output_text}'") print(f"\n{'=' * 80}") print(f"Full completion: '{prompt}{output_text}'") print(f"{'=' * 80}\n") finally: with contextlib.suppress(Exception): llm.shutdown() @skip_unsupported @pytest.mark.parametrize( "backend", BACKENDS, ) def test_logprobs_without_batch_invariance_should_fail( backend, monkeypatch: pytest.MonkeyPatch ): """ This test is the inverse of test_logprobs_bitwise_batch_invariance_bs1_vs_bsN. It DISABLES batch invariance mode and expects to see non-deterministic behavior between BS=1 and BS=N runs. This demonstrates that batch invariance is actually doing something useful. The test will PASS if we detect differences (proving batch invariance matters). The test will FAIL if everything matches (suggesting batch invariance isn't needed). """ # CRITICAL: Disable batch invariance for this test monkeypatch.setenv("VLLM_BATCH_INVARIANT", "0") monkeypatch.setattr(batch_invariant, "VLLM_BATCH_INVARIANT", False) seed = int(os.getenv("VLLM_TEST_SEED", "12345")) random.seed(seed) model_name = resolve_model_name(backend) tp_size = int(os.getenv("VLLM_TEST_TP_SIZE", "1")) print(f"\n{'=' * 80}") print("BATCH INVARIANCE DISABLED: Expecting non-deterministic behavior") print(f"{'=' * 80}\n") llm = LLM( model=model_name, tensor_parallel_size=tp_size, max_num_seqs=32, max_model_len=8192, dtype="bfloat16", enforce_eager=IS_DEVICE_CAPABILITY_BELOW_90, attention_config={"backend": backend}, ) # build ragged prompts to change shapes significantly across BS=1 vs BS=N long_min = int(os.getenv("VLLM_MIN_PROMPT", "768")) long_max = int(os.getenv("VLLM_MAX_PROMPT", "2048")) prompts: list[str] = [] options = [ (max(long_min, 1536), max(long_max, 3072)), # very long (max(1024, long_min), max(2048, long_max)), # long (256, 512), # mid (10, 20), # short ] for _ in range(32): lo, hi = random.choice(options) prompts.append(_random_prompt(lo, hi)) sp = SamplingParams( temperature=0.6, top_p=1.0, max_tokens=8, seed=1234, logprobs=5, ) # BS=1: run prompts individually and collect logprobs per step. print("\n" + "=" * 80) print("STARTING BS=1 RUNS (each prompt individually)") print("=" * 80 + "\n") bs1_logprobs_per_prompt = [] bs1_tokens_per_prompt = [] for idx, p in enumerate(prompts): print(f"\n[BS=1] Running prompt {idx}/{len(prompts)} - Preview: {p[:80]}...") outs = llm.generate([p], sp, use_tqdm=False) assert len(outs) == 1 step_logprobs, token_ids = _extract_step_logprobs(outs[0]) if step_logprobs is None: pytest.skip( "Logits are not available on RequestOutput; " "enable logprobs return to run this test." ) bs1_logprobs_per_prompt.append(step_logprobs) bs1_tokens_per_prompt.append(token_ids) print(f"[BS=1] Prompt {idx} generated tokens: {token_ids}") # BS=N: run prompts in a batch and collect logprobs per step for each prompt. print("\n" + "=" * 80) print(f"STARTING BS={len(prompts)} RUN (all prompts batched)") print("=" * 80 + "\n") outs_batched = llm.generate(prompts, sp, use_tqdm=False) assert len(outs_batched) == len(prompts) bsN_logprobs_per_prompt = [] bsN_tokens_per_prompt = [] print(f"\n[BS={len(prompts)}] Processing batched outputs...") for idx, o in enumerate(outs_batched): tokens = o.outputs[0].token_ids if o.outputs else "N/A" print(f"[BS={len(prompts)}] Prompt {idx} generated tokens: {tokens}") step_logprobs, token_ids = _extract_step_logprobs(o) if step_logprobs is None: pytest.skip( "Logits are not available on RequestOutput; " "enable logprobs return to run this test." ) bsN_logprobs_per_prompt.append(step_logprobs) bsN_tokens_per_prompt.append(token_ids) # Compare step-by-step logprobs for each prompt between BS=1 and BS=N runs. differences_found = [] for i, (logprobs_bs1, logprobs_bsN, tokens_bs1, tokens_bsN) in enumerate( zip( bs1_logprobs_per_prompt, bsN_logprobs_per_prompt, bs1_tokens_per_prompt, bsN_tokens_per_prompt, ) ): if len(logprobs_bs1) != len(logprobs_bsN): reason = ( f"Different number of steps: {len(logprobs_bs1)} (BS=1) " f"vs {len(logprobs_bsN)} (BS=N)" ) differences_found.append( { "prompt_idx": i, "step": "all", "reason": reason, "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) continue # Check if tokens match first if tokens_bs1 != tokens_bsN: differences_found.append( { "prompt_idx": i, "step": "sampling", "reason": "Different tokens sampled", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) continue for t, (a, b) in enumerate(zip(logprobs_bs1, logprobs_bsN)): if a.shape != b.shape: differences_found.append( { "prompt_idx": i, "step": t, "reason": f"Shape mismatch: {a.shape} vs {b.shape}", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) break if not torch.equal(a, b): max_diff = torch.abs(a - b).max().item() print( f"\n[EXPECTED DIVERGENCE FOUND] Prompt {i}, " f"Token {t}: max_diff={max_diff:.6e}" ) bs1_tok = tokens_bs1[t] if t < len(tokens_bs1) else "N/A" bsN_tok = tokens_bsN[t] if t < len(tokens_bsN) else "N/A" print(f" Token IDs: bs1={bs1_tok}, bsN={bsN_tok}") print(f" BS=1 logprob: {a.tolist()}") print(f" BS=N logprob: {b.tolist()}") differences_found.append( { "prompt_idx": i, "step": t, "reason": f"Bitwise mismatch (max_diff={max_diff:.6e})", "prompt_preview": prompts[i][:100], "bs1_tokens": tokens_bs1, "bsN_tokens": tokens_bsN, } ) break # Print summary print(f"\n{'=' * 80}") if differences_found: success_msg = ( f"✓ SUCCESS: Batch invariance is doing something! " f"Found {len(differences_found)}/{len(prompts)} prompts " f"with differences when batch invariance was DISABLED." ) print(success_msg) print(f"{'=' * 80}") for diff in differences_found: print(f"\nPrompt {diff['prompt_idx']} (step {diff['step']}):") print(f" Reason: {diff['reason']}") print(f" Preview: {diff['prompt_preview']}...") if "bs1_tokens" in diff: print(f" BS=1 tokens: {diff['bs1_tokens']}") if "bsN_tokens" in diff: print(f" BS=N tokens: {diff['bsN_tokens']}") print(f"{'=' * 80}\n") # Test PASSES because we found differences (batch invariance matters!) return else: # Test FAILS because everything matched even without batch invariance fail_msg = ( f"✗ UNEXPECTED: All {len(prompts)} prompts matched " f"between BS=1 and BS=N even with batch invariance DISABLED. " f"This suggests batch invariance might not be necessary, " f"or the test needs more sensitive prompts." ) print(fail_msg) print(f"{'=' * 80}\n") pytest.fail(fail_msg) @skip_unsupported @pytest.mark.parametrize("backend", ["FLASH_ATTN"]) def test_decode_logprobs_match_prefill_logprobs( backend, ): """ Test that verifies decode logprobs match prefill logprobs. For each decoded token at position i: 1. Run decode to generate N tokens and collect their logprobs 2. For each position i in [0, N): - Take prefix = prompt + tokens[0:i] - Run prefill(prefix + tokens[i]) to get logprob of tokens[i] - Verify prefill logprob matches decode logprob bitwise This ensures that the logprobs from decode are consistent with what we would get if we ran prefill on each prefix. """ seed = int(os.getenv("VLLM_TEST_SEED", "12345")) random.seed(seed) model_name = resolve_model_name(backend) tp_size = int(os.getenv("VLLM_TEST_TP_SIZE", "1")) from vllm.model_executor.layers.batch_invariant import ( vllm_is_batch_invariant, ) disable_custom_ar = vllm_is_batch_invariant() if disable_custom_ar: print(f"\n{'=' * 80}") print(f"BATCH INVARIANCE MODE: Disabling custom all-reduce (TP={tp_size})") print(f"{'=' * 80}\n") llm = LLM( model=model_name, tensor_parallel_size=tp_size, max_num_seqs=32, max_model_len=8192, dtype="bfloat16", enforce_eager=IS_DEVICE_CAPABILITY_BELOW_90, attention_config={"backend": backend}, ) # Use a few test prompts num_test_prompts = int(os.getenv("VLLM_DECODE_PREFILL_NUM_PROMPTS", "4")) prompts = [_random_prompt(10, 50) for _ in range(num_test_prompts)] # Generate longer sequences to test multiple decode steps max_tokens = int(os.getenv("VLLM_DECODE_PREFILL_MAX_TOKENS", "16")) sp = SamplingParams( temperature=0.0, # Greedy for determinism max_tokens=max_tokens, logprobs=5, ) print("\n" + "=" * 80) print("STEP 1: Running decode to generate tokens and collect logprobs") print("=" * 80 + "\n") # Step 1: Run decode and collect logprobs decode_outputs = llm.generate(prompts, sp, use_tqdm=False) failed_comparisons = [] for prompt_idx, (prompt, decode_output) in enumerate(zip(prompts, decode_outputs)): print(f"\n[Prompt {prompt_idx}] Testing: {prompt[:80]}...") # Extract decode logprobs and tokens decode_logprobs, token_ids = _extract_step_logprobs(decode_output) if decode_logprobs is None: pytest.skip( "Logprobs are not available on RequestOutput; " "enable logprobs return to run this test." ) print(f"[Prompt {prompt_idx}] Generated {len(token_ids)} tokens: {token_ids}") print(f"[Prompt {prompt_idx}] Decode logprobs: {decode_logprobs.tolist()}") # Step 2: For each token position, run prefill and compare print(f"\n[Prompt {prompt_idx}] Verifying each token via prefill...") for token_idx in range(len(token_ids)): # Construct the prefix up to (but not including) this token current_token = token_ids[token_idx] # We need to detokenize to get the text prefix # For this, we'll use the tokenizer from the LLM # However, the LLM API doesn't expose tokenizer easily, so we'll # construct the prefix by decoding from the original prompt # Get text up to this point by using the output text # This is approximate but should work for verification if token_idx == 0: prefix_prompt = prompt else: # Use the partial output text up to this token # We'll need to construct this from the full output prefix_output = decode_output.outputs[0] # Get the text for tokens 0 to token_idx-1 # Unfortunately, we don't have per-token text, so we'll use # a different approach: run prefill with prompt + tokens[0:token_idx] # Actually, we need to get the actual text. Let's use a workaround: # Run a generation with max_tokens = token_idx to get that prefix prefix_sp = SamplingParams( temperature=0.0, max_tokens=token_idx, logprobs=1, ) prefix_output = llm.generate([prompt], prefix_sp, use_tqdm=False)[0] prefix_prompt = prompt + prefix_output.outputs[0].text # Now run prefill with max_tokens=1 to get the logprob of the next token prefill_sp = SamplingParams( temperature=0.0, max_tokens=1, logprobs=5, ) print( f" [Token {token_idx}] Running prefill for prefix " f"(len={len(prefix_prompt)})..." ) prefill_output = llm.generate([prefix_prompt], prefill_sp, use_tqdm=False)[ 0 ] prefill_logprobs, prefill_token_ids = _extract_step_logprobs(prefill_output) if prefill_logprobs is None: print(f" [Token {token_idx}] Warning: No prefill logprobs available") continue # The first token from prefill should match the current token prefill_token = prefill_token_ids[0] prefill_logprob = prefill_logprobs[0].item() decode_logprob = decode_logprobs[token_idx].item() print( f" [Token {token_idx}] Decode token: {current_token}, " f"logprob: {decode_logprob:.8f}" ) print( f" [Token {token_idx}] Prefill token: {prefill_token}, " f"logprob: {prefill_logprob:.8f}" ) # Check if tokens match if current_token != prefill_token: failed_comparisons.append( { "prompt_idx": prompt_idx, "token_idx": token_idx, "reason": "Token mismatch", "decode_token": current_token, "prefill_token": prefill_token, "decode_logprob": decode_logprob, "prefill_logprob": prefill_logprob, "prompt_text": prompt[:100], "prefix_text": prefix_prompt[:100], } ) print(f" [Token {token_idx}] ✗ TOKEN MISMATCH!") continue # Check if logprobs match bitwise if decode_logprob != prefill_logprob: diff = abs(decode_logprob - prefill_logprob) failed_comparisons.append( { "prompt_idx": prompt_idx, "token_idx": token_idx, "reason": "Logprob mismatch", "decode_token": current_token, "prefill_token": prefill_token, "decode_logprob": decode_logprob, "prefill_logprob": prefill_logprob, "diff": diff, "prompt_text": prompt[:100], "prefix_text": prefix_prompt[:100], "decode_all_tokens": token_ids, "decode_all_logprobs": decode_logprobs.tolist(), } ) print(f" [Token {token_idx}] ✗ LOGPROB MISMATCH! diff={diff:.8e}") else: print(f" [Token {token_idx}] ✓ Match (bitwise equal)") # Print summary print(f"\n{'=' * 80}") if failed_comparisons: print(f"DECODE-PREFILL MISMATCH: {len(failed_comparisons)} failures detected") print(f"{'=' * 80}") # Group failures by prompt for better readability failures_by_prompt: dict[int, list[dict]] = {} for fail in failed_comparisons: pid = fail["prompt_idx"] if pid not in failures_by_prompt: failures_by_prompt[pid] = [] failures_by_prompt[pid].append(fail) for prompt_idx, failures in failures_by_prompt.items(): print(f"\n{'=' * 80}") print(f"PROMPT {prompt_idx}: {failures[0]['prompt_text']}...") print(f"{'=' * 80}") print(f"Total failures for this prompt: {len(failures)}") # Show where mismatches occur (which token positions) mismatch_positions = [f["token_idx"] for f in failures] print(f"Mismatch at token positions: {mismatch_positions}") # Show first few failures in detail for i, fail in enumerate(failures[:5]): # Show first 5 failures per prompt print(f"\n [Failure {i + 1}] Token position {fail['token_idx']}:")
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/determinism/test_online_batch_invariance.py
tests/v1/determinism/test_online_batch_invariance.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ HTTP-based batch invariance test: send requests to a running vLLM server and compare BS=1 vs BS=N results (tokens and per-step logprobs). Environment variables: - VLLM_TEST_MODEL: served model name (e.g., Qwen/Qwen3-1.7B / DeepSeek-R1) - VLLM_TP_SIZE: tensor parallelism size (e.g., 4) """ import os import random import sys from typing import Any import openai import pytest from utils import BACKENDS, _random_prompt, resolve_model_name, skip_unsupported from tests.utils import RemoteOpenAIServer def _request_completion( client: openai.OpenAI, model: str, prompt: Any, sp: dict[str, Any], max_retries: int = 3, retry_backoff: float = 0.5, ) -> dict[str, Any] | None: payload: dict[str, Any] = {"model": model, "prompt": prompt} payload.update(sp) for attempt in range(max_retries + 1): try: completion = client.completions.create(**payload) # Convert to plain dict so downstream logic can keep using # dict-style access just like with raw HTTP JSON. return completion.model_dump() except Exception as e: # pragma: no cover if attempt < max_retries: import time as _t _t.sleep(retry_backoff * (2**attempt)) continue sys.stderr.write(f"Error: {e}\n") return None return None def _extract_tokens_and_logprobs( choice: dict[str, Any], ) -> tuple[list[Any], list[float] | None]: tokens: list[Any] = [] token_logprobs: list[float] | None = None lp = choice.get("logprobs") if lp and isinstance(lp, dict): tokens = lp.get("token_ids") or lp.get("tokens") or [] token_logprobs = lp.get("token_logprobs", None) return tokens, token_logprobs def _compare_bs1_vs_bsn_single_process( prompts: list[str], sp_kwargs: dict[str, Any], client: openai.OpenAI, model_name: str, ) -> None: # BS=1 bs1_tokens_per_prompt: list[list[Any]] = [] bs1_logprobs_per_prompt: list[list[float] | None] = [] for p in prompts: resp = _request_completion(client, model_name, p, sp_kwargs) if resp is None or not resp.get("choices"): raise AssertionError("BS=1 empty/failed response") choice = resp["choices"][0] toks, lps = _extract_tokens_and_logprobs(choice) if lps is None: raise AssertionError( "logprobs not returned; ensure server supports 'logprobs'" ) bs1_tokens_per_prompt.append(list(toks)) bs1_logprobs_per_prompt.append(list(lps)) # BS=N bsN_tokens_per_prompt: list[list[Any]] = [None] * len(prompts) # type: ignore[list-item] bsN_logprobs_per_prompt: list[list[float] | None] = [None] * len(prompts) resp = _request_completion(client, model_name, prompts, sp_kwargs) if resp is None or not resp.get("choices"): raise AssertionError("BS=N empty/failed batched response") choices = resp.get("choices", []) if len(choices) != len(prompts): raise AssertionError( f"BS=N choices length {len(choices)} != num prompts {len(prompts)}" ) for idx, choice in enumerate(choices): toks, lps = _extract_tokens_and_logprobs(choice) if lps is None: raise AssertionError(f"BS=N missing logprobs for prompt {idx}") bsN_tokens_per_prompt[idx] = list(toks) bsN_logprobs_per_prompt[idx] = list(lps) # compare for i, (tokens_bs1, tokens_bsN, logprobs_bs1, logprobs_bsN) in enumerate( zip( bs1_tokens_per_prompt, bsN_tokens_per_prompt, bs1_logprobs_per_prompt, bsN_logprobs_per_prompt, ) ): if tokens_bs1 != tokens_bsN: raise AssertionError( f"Prompt {i} (sampling): Different tokens sampled. " f"BS=1 tokens: {tokens_bs1} BS=N tokens: {tokens_bsN}" ) if logprobs_bs1 is None or logprobs_bsN is None: raise AssertionError(f"Prompt {i}: Missing logprobs in one of the runs") if len(logprobs_bs1) != len(logprobs_bsN): raise AssertionError( f"Prompt {i}: Different number of steps: " f"{len(logprobs_bs1)} (BS=1) vs {len(logprobs_bsN)} (BS=N)." ) for t, (a, b) in enumerate(zip(logprobs_bs1, logprobs_bsN)): if a != b: diff = abs(a - b) raise AssertionError( f"Prompt {i} Step {t}: Bitwise mismatch " f"(abs diff={diff:.6e}). " f"BS=1 tokens: {tokens_bs1} BS=N tokens: {tokens_bsN}" ) @skip_unsupported @pytest.mark.parametrize("backend", BACKENDS) def test_logprobs_bitwise_batch_invariance_bs1_vs_bsN( backend: str, ) -> None: random.seed(int(os.getenv("VLLM_TEST_SEED", "12345"))) model_name = resolve_model_name(backend) prompts_all = [_random_prompt(10, 50) for _ in range(32)] sp_kwargs: dict[str, Any] = { "temperature": 0.6, "top_p": 1.0, "max_tokens": 8, "seed": 42, "logprobs": 5, } tp_size = os.getenv("VLLM_TP_SIZE", "1") server_args: list[str] = [ "--max-model-len=8192", "--max-num-seqs=32", f"--attention-backend={backend}", ] if tp_size: server_args += ["-tp", tp_size] with RemoteOpenAIServer(model_name, server_args) as server: client = server.get_client() _compare_bs1_vs_bsn_single_process( prompts=prompts_all, sp_kwargs=sp_kwargs, client=client, model_name=model_name, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/determinism/test_rms_norm_batch_invariant.py
tests/v1/determinism/test_rms_norm_batch_invariant.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Test batch-invariant RMS normalization against standard implementations. This test compares the Triton-based batch-invariant RMS norm implementation with the standard CUDA-based implementation to ensure numerical accuracy. """ import pytest import torch from utils import skip_unsupported from vllm.model_executor.layers.batch_invariant import rms_norm as triton_rms_norm from vllm.model_executor.layers.layernorm import RMSNorm @skip_unsupported @pytest.mark.parametrize("batch_size", [1, 4, 16, 64]) @pytest.mark.parametrize("hidden_size", [512, 2048, 4096, 8192]) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16]) @pytest.mark.parametrize("eps", [1e-6, 1e-5]) def test_rms_norm_batch_invariant_vs_standard( batch_size: int, hidden_size: int, dtype: torch.dtype, eps: float ): """ Compare batch-invariant Triton RMS norm against standard CUDA implementation. Tests that the Triton-based batch-invariant RMS norm produces numerically equivalent results to the standard CUDA implementation across various configurations. """ device = torch.device("cuda") # Create test input and weight torch.manual_seed(42) input_tensor = torch.randn(batch_size, hidden_size, dtype=dtype, device=device) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Standard implementation (CUDA ops) rms_norm_layer = RMSNorm(hidden_size, eps=eps, dtype=dtype).to(device) rms_norm_layer.weight.data = weight.clone() standard_output = rms_norm_layer.forward_cuda(input_tensor) # Batch-invariant implementation (Triton) triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Compare outputs # Use looser tolerance for bfloat16 due to its lower precision if dtype == torch.bfloat16: rtol, atol = 1e-1, 1e-1 # 10% relative tolerance for bfloat16 else: rtol, atol = 1e-2, 1e-2 # 1% for float16/float32 torch.testing.assert_close( triton_output, standard_output, rtol=rtol, atol=atol, msg=f"RMS norm mismatch for batch_size={batch_size}, " f"hidden_size={hidden_size}, " f"dtype={dtype}, eps={eps}", ) @skip_unsupported @pytest.mark.parametrize("batch_size", [1, 16, 128]) @pytest.mark.parametrize("seq_len", [1, 32, 512]) @pytest.mark.parametrize("hidden_size", [2048, 4096]) def test_rms_norm_3d_input(batch_size: int, seq_len: int, hidden_size: int): """ Test RMS norm with 3D input tensors (batch, seq_len, hidden_size). Ensures that the batch-invariant RMS norm correctly handles multi-dimensional inputs that are common in transformer models. """ device = torch.device("cuda") dtype = torch.bfloat16 eps = 1e-6 torch.manual_seed(42) input_tensor = torch.randn( batch_size, seq_len, hidden_size, dtype=dtype, device=device ) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Standard implementation rms_norm_layer = RMSNorm(hidden_size, eps=eps, dtype=dtype).to(device) rms_norm_layer.weight.data = weight.clone() standard_output = rms_norm_layer.forward_cuda(input_tensor) # Batch-invariant implementation triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Use looser tolerance for bfloat16 rtol, atol = 1e-1, 1e-1 # 10% tolerance for bfloat16 torch.testing.assert_close( triton_output, standard_output, rtol=rtol, atol=atol, msg=f"RMS norm mismatch for 3D input with batch_size={batch_size}, " f"seq_len={seq_len}, hidden_size={hidden_size}", ) @skip_unsupported def test_rms_norm_numerical_stability(): """ Test RMS norm numerical stability with extreme values. Ensures that both implementations handle edge cases like very small or large values without producing NaN or Inf. """ device = torch.device("cuda") dtype = torch.float16 eps = 1e-6 hidden_size = 2048 # Test cases with extreme values test_cases = [ # Very small values torch.ones(4, hidden_size, dtype=dtype, device=device) * 1e-5, # Very large values torch.ones(4, hidden_size, dtype=dtype, device=device) * 1e4, # Mixed small and large torch.randn(4, hidden_size, dtype=dtype, device=device) * 100, # Values near zero torch.randn(4, hidden_size, dtype=dtype, device=device) * 1e-6, ] weight = torch.ones(hidden_size, dtype=dtype, device=device) for idx, input_tensor in enumerate(test_cases): # Standard implementation rms_norm_layer = RMSNorm(hidden_size, eps=eps, dtype=dtype).to(device) rms_norm_layer.weight.data = weight.clone() standard_output = rms_norm_layer.forward_cuda(input_tensor) # Batch-invariant implementation triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Check for NaN or Inf assert not torch.isnan(standard_output).any(), ( f"Standard RMS norm produced NaN for test case {idx}" ) assert not torch.isinf(standard_output).any(), ( f"Standard RMS norm produced Inf for test case {idx}" ) assert not torch.isnan(triton_output).any(), ( f"Triton RMS norm produced NaN for test case {idx}" ) assert not torch.isinf(triton_output).any(), ( f"Triton RMS norm produced Inf for test case {idx}" ) # Compare outputs - very lenient for extreme values with float16 torch.testing.assert_close( triton_output, standard_output, rtol=2e-1, # 20% tolerance for extreme values atol=2e-1, msg=f"RMS norm mismatch for extreme value test case {idx}", ) @skip_unsupported def test_rms_norm_formula(): """ Test that RMS norm follows the correct mathematical formula. Verifies: output = input / sqrt(mean(input^2) + eps) * weight """ device = torch.device("cuda") dtype = torch.float32 # Use float32 for higher precision in formula check eps = 1e-6 hidden_size = 1024 torch.manual_seed(42) input_tensor = torch.randn(8, hidden_size, dtype=dtype, device=device) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Compute expected output using the formula variance = (input_tensor.pow(2).mean(dim=-1, keepdim=True)).to(dtype) expected_output = input_tensor * torch.rsqrt(variance + eps) * weight # Batch-invariant implementation triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Compare against formula torch.testing.assert_close( triton_output, expected_output, rtol=1e-4, atol=1e-4, msg="Triton RMS norm doesn't match expected formula", ) @skip_unsupported @pytest.mark.parametrize("hidden_size", [128, 1024, 4096, 16384]) def test_rms_norm_different_hidden_sizes(hidden_size: int): """ Test RMS norm with various hidden sizes to ensure block size handling. The Triton kernel uses a fixed BLOCK_SIZE=1024, so this tests that it correctly handles hidden sizes both smaller and larger than the block size. """ device = torch.device("cuda") dtype = torch.bfloat16 eps = 1e-6 batch_size = 16 torch.manual_seed(42) input_tensor = torch.randn(batch_size, hidden_size, dtype=dtype, device=device) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Standard implementation rms_norm_layer = RMSNorm(hidden_size, eps=eps, dtype=dtype).to(device) rms_norm_layer.weight.data = weight.clone() standard_output = rms_norm_layer.forward_cuda(input_tensor) # Batch-invariant implementation triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Use looser tolerance for bfloat16 rtol, atol = 1e-1, 1e-1 # 10% tolerance for bfloat16 torch.testing.assert_close( triton_output, standard_output, rtol=rtol, atol=atol, msg=f"RMS norm mismatch for hidden_size={hidden_size}", ) @skip_unsupported def test_rms_norm_determinism(): """ Test that batch-invariant RMS norm produces deterministic results. Runs the same input through the kernel multiple times and verifies identical outputs. """ device = torch.device("cuda") dtype = torch.bfloat16 eps = 1e-6 hidden_size = 4096 batch_size = 32 torch.manual_seed(42) input_tensor = torch.randn(batch_size, hidden_size, dtype=dtype, device=device) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Run multiple times outputs = [] for _ in range(5): output = triton_rms_norm(input_tensor.clone(), weight, eps=eps) outputs.append(output) # All outputs should be identical reference = outputs[0] for idx, output in enumerate(outputs[1:], start=1): torch.testing.assert_close( output, reference, rtol=0.0, atol=0.0, msg=f"RMS norm not deterministic: run {idx} differs from reference", ) if __name__ == "__main__": # Run a quick smoke test print("Running quick smoke test of RMS norm implementations...") device = torch.device("cuda") batch_size = 8 hidden_size = 4096 dtype = torch.bfloat16 eps = 1e-6 torch.manual_seed(42) input_tensor = torch.randn(batch_size, hidden_size, dtype=dtype, device=device) weight = torch.randn(hidden_size, dtype=dtype, device=device) # Standard implementation rms_norm_layer = RMSNorm(hidden_size, eps=eps, dtype=dtype).to(device) rms_norm_layer.weight.data = weight.clone() standard_output = rms_norm_layer.forward_cuda(input_tensor) # Batch-invariant implementation triton_output = triton_rms_norm(input_tensor, weight, eps=eps) # Compare max_diff = (triton_output - standard_output).abs().max().item() mean_diff = (triton_output - standard_output).abs().mean().item() print(f"Max difference: {max_diff:.6e}") print(f"Mean difference: {mean_diff:.6e}") print(f"Standard output sample: {standard_output[0, :5].tolist()}") print(f"Triton output sample: {triton_output[0, :5].tolist()}") if max_diff < 1e-3: print("✓ Smoke test passed!") else: print("✗ Smoke test failed - differences too large")
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_async_llm_dp.py
tests/v1/distributed/test_async_llm_dp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os from contextlib import ExitStack from dataclasses import dataclass import pytest from vllm import SamplingParams from vllm.config import VllmConfig from vllm.engine.arg_utils import AsyncEngineArgs from vllm.inputs import PromptType from vllm.platforms import current_platform from vllm.sampling_params import RequestOutputKind from vllm.v1.engine.async_llm import AsyncLLM from vllm.v1.engine.core_client import DPAsyncMPClient from vllm.v1.metrics.loggers import StatLoggerBase from vllm.v1.metrics.stats import IterationStats, MultiModalCacheStats, SchedulerStats DP_SIZE = int(os.getenv("DP_SIZE", 2)) async def generate( engine: AsyncLLM, request_id: str, prompt: PromptType, output_kind: RequestOutputKind, max_tokens: int, prompt_logprobs: int | None = None, data_parallel_rank: int | None = None, ) -> tuple[int, str]: # Ensure generate doesn't complete too fast for cancellation test. await asyncio.sleep(0.2) count = 0 sampling_params = SamplingParams( max_tokens=max_tokens, ignore_eos=True, output_kind=output_kind, temperature=0, prompt_logprobs=prompt_logprobs, ) async for out in engine.generate( request_id=request_id, prompt=prompt, sampling_params=sampling_params, data_parallel_rank=data_parallel_rank, ): num_tokens = len(out.outputs[0].token_ids) if output_kind == RequestOutputKind.DELTA: count += num_tokens else: count = num_tokens await asyncio.sleep(0.0) return count, request_id @pytest.mark.parametrize( "model", [ "ibm-research/PowerMoE-3b", "hmellor/tiny-random-LlamaForCausalLM", ], ) @pytest.mark.parametrize( "output_kind", [ RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY, ], ) @pytest.mark.parametrize("data_parallel_backend", ["mp", "ray"]) @pytest.mark.parametrize("async_scheduling", [True, False]) @pytest.mark.asyncio async def test_load( model: str, output_kind: RequestOutputKind, data_parallel_backend: str, async_scheduling: bool, ): if async_scheduling and data_parallel_backend == "ray": # TODO(NickLucche) Re-enable when async scheduling is supported pytest.skip("Async scheduling is not supported with ray") elif data_parallel_backend == "ray" and current_platform.is_rocm(): pytest.skip( "Ray as the distributed executor backend is not supported with ROCm." ) stats_loggers = {} @dataclass class SimpleStatsLogger(StatLoggerBase): init_count: int = 0 finished_req_count: int = 0 def __init__(self, vllm_config: VllmConfig, engine_index: int = 0): stats_loggers[engine_index] = self def record( self, scheduler_stats: SchedulerStats | None, iteration_stats: IterationStats | None, mm_cache_stats: MultiModalCacheStats | None = None, engine_idx: int = 0, ): if iteration_stats: self.finished_req_count += len(iteration_stats.finished_requests) def log_engine_initialized(self): self.init_count += 1 with ExitStack() as after: prompt = "This is a test of data parallel" engine_args = AsyncEngineArgs( model=model, enforce_eager=True, tensor_parallel_size=int(os.getenv("TP_SIZE", 1)), data_parallel_size=DP_SIZE, data_parallel_backend=data_parallel_backend, async_scheduling=async_scheduling, ) engine = AsyncLLM.from_engine_args( engine_args, stat_loggers=[SimpleStatsLogger] ) after.callback(engine.shutdown) NUM_REQUESTS = 100 NUM_EXPECTED_TOKENS = 10 request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] # Create concurrent requests. tasks = [] for request_id in request_ids: tasks.append( asyncio.create_task( generate( engine, request_id, prompt, output_kind, NUM_EXPECTED_TOKENS ) ) ) # Short sleep to ensure that requests are distributed. await asyncio.sleep(0.01) # Confirm that we got all the EXPECTED tokens from the requests. done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION) for task in pending: task.cancel() for task in done: num_generated_tokens, request_id = await task assert num_generated_tokens == NUM_EXPECTED_TOKENS, ( f"{request_id} generated {num_generated_tokens} but " f"expected {NUM_EXPECTED_TOKENS}" ) assert not engine.output_processor.has_unfinished_requests() # testing internals here which may break core_client: DPAsyncMPClient = engine.engine_core # the engines only synchronize stopping every N steps so # allow a small amount of time here. for _ in range(10): if not core_client.engines_running: break await asyncio.sleep(0.5) assert not core_client.engines_running assert not core_client.reqs_in_flight # Check that requests were distributed between the engines print(f"Stats loggers after test: {stats_loggers}") assert len(stats_loggers) == DP_SIZE assert stats_loggers[0].init_count == 1 for sl in stats_loggers.values(): slogger: SimpleStatsLogger = sl assert slogger.finished_req_count > NUM_REQUESTS // (DP_SIZE + 1), ( f"requests are imbalanced: {stats_loggers}" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_hybrid_lb_dp.py
tests/v1/distributed/test_hybrid_lb_dp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os import threading import time from contextlib import AsyncExitStack import openai # use the official client for correctness check import pytest import pytest_asyncio import requests from tests.utils import RemoteOpenAIServer from tests.v1.utils import check_request_balancing from vllm.platforms import current_platform MODEL_NAME = "ibm-research/PowerMoE-3b" # Number of data parallel ranks for hybrid LB testing (4 total) DP_SIZE = int(os.getenv("DP_SIZE", "4")) # Default tensor parallel size to use TP_SIZE = int(os.getenv("TP_SIZE", "1")) # Number of nodes (2 nodes, each with 2 DP ranks) NUM_NODES = 2 DP_SIZE_LOCAL = DP_SIZE // NUM_NODES # 2 ranks per node class HybridLBServerManager: """Manages hybrid data parallel vLLM server instances where each node runs a single logical API server that balances requests only to the DP engines running on that same node.""" def __init__( self, model_name: str, dp_size: int, api_server_count: int, base_server_args: list, dp_size_local: int = DP_SIZE_LOCAL, tp_size: int = TP_SIZE, ): self.model_name = model_name self.dp_size = dp_size self.dp_size_local = dp_size_local self.tp_size = tp_size self.api_server_count = api_server_count self.base_server_args = base_server_args self.servers: list[tuple[RemoteOpenAIServer, list[str]]] = [] self.server_threads: list[threading.Thread] = [] self.num_nodes = dp_size // dp_size_local def __enter__(self) -> list[tuple[RemoteOpenAIServer, list[str]]]: """Start all server instances for hybrid LB mode.""" for node_id in range(self.num_nodes): # Create server args for this specific node server_args = self.base_server_args.copy() # Calculate start rank for this node start_rank = node_id * self.dp_size_local # Add hybrid LB specific arguments server_args.extend( [ "--data-parallel-size", str(self.dp_size), "--data-parallel-size-local", str(self.dp_size_local), "--data-parallel-start-rank", str(start_rank), "--data-parallel-hybrid-lb", # Enable hybrid LB mode "--tensor-parallel-size", str(self.tp_size), "--port", str(8000 + node_id), # Different port for each node "--api-server-count", str(self.api_server_count), "--data-parallel-address", "127.0.0.1", "--data-parallel-rpc-port", "13345", ] ) # Use a thread to start each server to allow parallel initialization def start_server(node: int, sargs: list[str]): try: # Calculate GPU devices for this node gpus_per_node = self.dp_size_local * self.tp_size gpu_start = node * gpus_per_node gpu_end = gpu_start + gpus_per_node # Start the server server = RemoteOpenAIServer( self.model_name, sargs, auto_port=False, env_dict={ "VLLM_SERVER_DEV_MODE": "1", current_platform.device_control_env_var: ",".join( str(current_platform.device_id_to_physical_device_id(i)) for i in range(gpu_start, gpu_end) ), }, ) server.__enter__() print( f"Hybrid LB node {node} started successfully with " f"{self.dp_size_local} local DP ranks and " f"{self.api_server_count} API servers" ) self.servers.append((server, sargs)) except Exception as e: print(f"Failed to start hybrid LB node {node}: {e}") raise thread = threading.Thread(target=start_server, args=(node_id, server_args)) thread.start() self.server_threads.append(thread) # Wait for all servers to start for thread in self.server_threads: thread.join() # Give servers additional time to fully initialize and coordinate time.sleep(3) if len(self.servers) != self.num_nodes: raise Exception("Servers failed to start") return self.servers def __exit__(self, exc_type, exc_val, exc_tb): """Stop all server instances.""" while self.servers: try: self.servers.pop()[0].__exit__(exc_type, exc_val, exc_tb) except Exception as e: print(f"Error stopping server: {e}") @pytest.fixture(scope="module") def default_server_args(): return [ # use half precision for speed and memory savings in CI environment "--dtype", "bfloat16", "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", ] @pytest.fixture(scope="module", params=[1, 4]) def server_manager(request, default_server_args): api_server_count = request.param server_manager = HybridLBServerManager( MODEL_NAME, DP_SIZE, api_server_count, default_server_args, DP_SIZE_LOCAL, TP_SIZE, ) with server_manager: yield server_manager @pytest.fixture def servers(server_manager): return server_manager.servers @pytest_asyncio.fixture async def clients(servers: list[tuple[RemoteOpenAIServer, list[str]]]): # Create a client for each node (each node has its own API endpoint) async with AsyncExitStack() as stack: yield [ await stack.enter_async_context(server.get_async_client()) for server, _ in servers ] def _get_parallel_config(server: RemoteOpenAIServer): response = requests.get(server.url_for("server_info?config_format=json")) response.raise_for_status() vllm_config = response.json()["vllm_config"] return vllm_config["parallel_config"] def test_hybrid_dp_server_info(server_manager): servers = server_manager.servers api_server_count = server_manager.api_server_count for i, (server, _) in enumerate(servers): print(f"Testing {i=}") # Each request will hit one of the API servers # `n_reqs` is set so that there is a good chance each server # receives at least one request n_reqs = 2 * api_server_count * api_server_count parallel_configs = [_get_parallel_config(server) for _ in range(n_reqs)] api_process_counts = [c["_api_process_count"] for c in parallel_configs] api_process_ranks = [c["_api_process_rank"] for c in parallel_configs] assert all(c == api_server_count for c in api_process_counts), ( api_process_counts ) assert all(0 <= r < api_server_count for r in api_process_ranks), ( api_process_ranks ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_hybrid_lb_completion( clients: list[openai.AsyncOpenAI], servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: async def make_request(client: openai.AsyncOpenAI): completion = await client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=5, temperature=1.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] # The exact number of tokens can vary slightly with temperature=1.0, # so we check for a reasonable minimum length. assert len(choice.text) >= 1 # Finish reason might not always be 'length' if the model finishes early # or due to other reasons, especially with high temperature. # So, we'll accept 'length' or 'stop'. assert choice.finish_reason in ("length", "stop") # Token counts can also vary, so we check they are positive. assert completion.usage.completion_tokens > 0 assert completion.usage.prompt_tokens > 0 assert completion.usage.total_tokens > 0 return completion # Test single request to each node for i, client in enumerate(clients): result = await make_request(client) assert result is not None print(f"Hybrid LB node {i} handled single completion request successfully") await asyncio.sleep(0.5) # Send requests to all nodes - each should balance within its local DP ranks num_requests = 200 # Total 200 requests across 2 nodes all_tasks = [] for i in range(num_requests): client = clients[i % len(clients)] all_tasks.append(asyncio.create_task(make_request(client))) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) await asyncio.sleep(0.5) # Second burst of requests all_tasks = [] for i in range(num_requests): client = clients[i % len(clients)] all_tasks.append(asyncio.create_task(make_request(client))) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed hybrid LB test with {len(clients)} nodes " f"({DP_SIZE_LOCAL} DP ranks each, API server count: {api_server_count})" ) # Check request balancing within each node for i, (server, _) in enumerate(servers): print(f"Checking request balancing for node {i}") check_request_balancing(server, DP_SIZE_LOCAL) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_hybrid_lb_completion_streaming( clients: list[openai.AsyncOpenAI], servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: prompt = "What is an LLM?" async def make_streaming_request(client: openai.AsyncOpenAI): # Perform a non-streaming request to get the expected full output single_completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text # Perform the streaming request stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 last_chunk = None async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 last_chunk = chunk # Keep track of the last chunk # finish reason should only return in the last block for OpenAI API assert finish_reason_count == 1, "Finish reason should appear exactly once." assert last_chunk is not None, "Stream should have yielded at least one chunk." assert last_chunk.choices[0].finish_reason == "length", ( "Finish reason should be 'length'." ) # Check that the combined text matches the non-streamed version. assert "".join(chunks) == single_output, ( "Streamed output should match non-streamed output." ) return True # Indicate success for this request # Test single request to each node for i, client in enumerate(clients): result = await make_streaming_request(client) assert result is not None print(f"Hybrid LB node {i} handled single streaming request successfully") await asyncio.sleep(0.5) # Send streaming requests to all nodes num_requests = 200 # Total 200 requests across 2 nodes all_tasks = [] for i in range(num_requests): client = clients[i % len(clients)] all_tasks.append(asyncio.create_task(make_streaming_request(client))) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." await asyncio.sleep(0.5) # Second burst of streaming requests all_tasks = [] for i in range(num_requests): client = clients[i % len(clients)] all_tasks.append(asyncio.create_task(make_streaming_request(client))) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed hybrid LB streaming test with " f"{len(clients)} nodes ({DP_SIZE_LOCAL} DP ranks each, " f"API server count: {api_server_count})" ) # Check request balancing within each node for i, (server, _) in enumerate(servers): print(f"Checking streaming request balancing for node {i}") check_request_balancing(server, DP_SIZE_LOCAL)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_external_lb_dp.py
tests/v1/distributed/test_external_lb_dp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os import threading import time from contextlib import AsyncExitStack import openai # use the official client for correctness check import pytest import pytest_asyncio import requests from tests.utils import RemoteOpenAIServer from vllm.platforms import current_platform MODEL_NAME = "ibm-research/PowerMoE-3b" # Number of data parallel ranks for external LB testing DP_SIZE = int(os.getenv("DP_SIZE", "2")) # Default tensor parallel size to use TP_SIZE = int(os.getenv("TP_SIZE", "1")) class ExternalLBServerManager: """Manages data parallel vLLM server instances for external load balancer testing.""" def __init__( self, model_name: str, dp_size: int, api_server_count: int, base_server_args: list, tp_size: int = TP_SIZE, ): self.model_name = model_name self.dp_size = dp_size self.tp_size = tp_size self.api_server_count = api_server_count self.base_server_args = base_server_args self.servers: list[tuple[RemoteOpenAIServer, list[str]]] = [] self.server_threads: list[threading.Thread] = [] def __enter__(self) -> list[tuple[RemoteOpenAIServer, list[str]]]: """Start all server instances for external LB mode.""" for rank in range(self.dp_size): # Create server args for this specific rank server_args = self.base_server_args.copy() # Add external LB specific arguments server_args.extend( [ "--data-parallel-size", str(self.dp_size), "--data-parallel-rank", str(rank), "--data-parallel-size-local", "1", "--tensor-parallel-size", str(self.tp_size), "--port", str(8000 + rank), # Different port for each rank "--api-server-count", str(self.api_server_count), ] ) # Use a thread to start each server to allow parallel initialization def start_server(r: int, sargs: list[str]): try: # Start the server server = RemoteOpenAIServer( self.model_name, sargs, auto_port=False, env_dict={ "VLLM_SERVER_DEV_MODE": "1", current_platform.device_control_env_var: ",".join( str(current_platform.device_id_to_physical_device_id(i)) for i in range(r * TP_SIZE, (r + 1) * TP_SIZE) ), }, ) server.__enter__() print( f"Server rank {r} started successfully with " f"{self.api_server_count} API servers" ) self.servers.append((server, sargs)) except Exception as e: print(f"Failed to start server rank {r}: {e}") raise thread = threading.Thread(target=start_server, args=(rank, server_args)) thread.start() self.server_threads.append(thread) # Wait for all servers to start for thread in self.server_threads: thread.join() # Give servers additional time to fully initialize and coordinate time.sleep(2) if len(self.servers) != self.dp_size: raise Exception("Servers failed to start") return self.servers def __exit__(self, exc_type, exc_val, exc_tb): """Stop all server instances.""" while self.servers: try: self.servers.pop()[0].__exit__(exc_type, exc_val, exc_tb) except Exception as e: print(f"Error stopping server: {e}") @pytest.fixture(scope="module") def default_server_args(): return [ # use half precision for speed and memory savings in CI environment "--dtype", "bfloat16", "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", ] @pytest.fixture(scope="module", params=[1, 4]) def server_manager(request, default_server_args): api_server_count = request.param server_manager = ExternalLBServerManager( MODEL_NAME, DP_SIZE, api_server_count, default_server_args ) with server_manager: yield server_manager @pytest.fixture def servers(server_manager): return server_manager.servers @pytest_asyncio.fixture async def clients(servers: list[tuple[RemoteOpenAIServer, list[str]]]): # Create a client for each server async with AsyncExitStack() as stack: yield [ await stack.enter_async_context(server.get_async_client()) for server, _ in servers ] def _get_parallel_config(server: RemoteOpenAIServer): response = requests.get(server.url_for("server_info?config_format=json")) response.raise_for_status() vllm_config = response.json()["vllm_config"] return vllm_config["parallel_config"] def test_external_lb_server_info(server_manager): servers = server_manager.servers api_server_count = server_manager.api_server_count for i, (server, _) in enumerate(servers): print(f"Testing {i=}") # Each request will hit one of the API servers # `n_reqs` is set so that there is a good chance each server # receives at least one request n_reqs = 2 * api_server_count * api_server_count parallel_configs = [_get_parallel_config(server) for _ in range(n_reqs)] api_process_counts = [c["_api_process_count"] for c in parallel_configs] api_process_ranks = [c["_api_process_rank"] for c in parallel_configs] assert all(c == api_server_count for c in api_process_counts), ( api_process_counts ) assert all(0 <= r < api_server_count for r in api_process_ranks), ( api_process_ranks ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_external_lb_single_completion( clients: list[openai.AsyncOpenAI], servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: async def make_request(client: openai.AsyncOpenAI): completion = await client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=10, temperature=1.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] # The exact number of tokens can vary slightly with temperature=1.0, # so we check for a reasonable minimum length. assert len(choice.text) >= 1 # Finish reason might not always be 'length' if the model finishes early # or due to other reasons, especially with high temperature. # So, we'll accept 'length' or 'stop'. assert choice.finish_reason in ("length", "stop") # Token counts can also vary, so we check they are positive. assert completion.usage.completion_tokens > 0 assert completion.usage.prompt_tokens > 0 assert completion.usage.total_tokens > 0 return completion # Test single request to each server for i, client in enumerate(clients): result = await make_request(client) assert result is not None print(f"Server {i} handled single completion request successfully") await asyncio.sleep(0.5) # Send requests to all servers in round-robin fashion num_requests_per_server = 25 # Total 50 requests across 2 servers all_tasks = [] for i, client in enumerate(clients): tasks = [make_request(client) for _ in range(num_requests_per_server)] all_tasks.extend(tasks) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests_per_server * len(clients) assert all(completion is not None for completion in results) await asyncio.sleep(0.5) # Second burst of requests all_tasks = [] for i, client in enumerate(clients): tasks = [make_request(client) for _ in range(num_requests_per_server)] all_tasks.extend(tasks) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests_per_server * len(clients) assert all(completion is not None for completion in results) _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed external LB test with {len(clients)} servers " f"(API server count: {api_server_count})" ) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_external_lb_completion_streaming( clients: list[openai.AsyncOpenAI], servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: prompt = "What is an LLM?" async def make_streaming_request(client: openai.AsyncOpenAI): # Perform a non-streaming request to get the expected full output single_completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text # Perform the streaming request stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 last_chunk = None async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 last_chunk = chunk # Keep track of the last chunk # finish reason should only return in the last block for OpenAI API assert finish_reason_count == 1, "Finish reason should appear exactly once." assert last_chunk is not None, "Stream should have yielded at least one chunk." assert last_chunk.choices[0].finish_reason == "length", ( "Finish reason should be 'length'." ) # Check that the combined text matches the non-streamed version. assert "".join(chunks) == single_output, ( "Streamed output should match non-streamed output." ) return True # Indicate success for this request # Test single request to each server for i, client in enumerate(clients): result = await make_streaming_request(client) assert result is not None print(f"Server {i} handled single streaming request successfully") await asyncio.sleep(0.5) # Send streaming requests to all servers in round-robin fashion num_requests_per_server = 25 # Total 50 requests across 2 servers all_tasks = [] for i, client in enumerate(clients): tasks = [make_streaming_request(client) for _ in range(num_requests_per_server)] all_tasks.extend(tasks) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests_per_server * len(clients) assert all(results), "Not all streaming requests completed successfully." await asyncio.sleep(0.5) # Second burst of streaming requests all_tasks = [] for i, client in enumerate(clients): tasks = [make_streaming_request(client) for _ in range(num_requests_per_server)] all_tasks.extend(tasks) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests_per_server * len(clients) assert all(results), "Not all streaming requests completed successfully." _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed external LB streaming test with " f"{len(clients)} servers (API server count: {api_server_count})" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_internal_lb_dp.py
tests/v1/distributed/test_internal_lb_dp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os import threading import time import traceback from typing import cast import openai # use the official client for correctness check import pytest import pytest_asyncio import requests from tests.utils import RemoteOpenAIServer from tests.v1.utils import check_request_balancing from vllm.platforms import current_platform MODEL_NAME = "ibm-research/PowerMoE-3b" # Number of data parallel ranks for multi-node internal LB testing DP_SIZE = int(os.getenv("DP_SIZE", "2")) # Default tensor parallel size to use TP_SIZE = int(os.getenv("TP_SIZE", "1")) # Number of nodes to simulate NUM_NODES = 2 class MultinodeInternalLBServerManager: """Manages multi-node data parallel vLLM server instances for internal load balancer testing using --headless mode.""" def __init__( self, model_name: str, dp_size: int, api_server_count: int, base_server_args: list, dp_per_node: int = 1, tp_size: int = TP_SIZE, ): self.model_name = model_name self.dp_size = dp_size self.dp_per_node = dp_per_node self.tp_size = tp_size self.api_server_count = api_server_count self.base_server_args = base_server_args self.servers: list[tuple[RemoteOpenAIServer, list[str]] | None] = [None] * ( dp_size // dp_per_node ) self.server_threads: list[threading.Thread] = [] def __enter__(self) -> list[tuple[RemoteOpenAIServer, list[str]]]: """Start all server instances for multi-node internal LB mode.""" for server_idx, rank in enumerate(range(0, self.dp_size, self.dp_per_node)): # Create server args for this specific rank server_args = self.base_server_args.copy() if rank == 0: # Head node - runs API server and first DP rank server_args.extend( [ "--data-parallel-size", str(self.dp_size), "--data-parallel-size-local", str(self.dp_per_node), "--tensor-parallel-size", str(self.tp_size), "--port", "8000", # Single endpoint for all requests "--api-server-count", str(self.api_server_count), "--data-parallel-address", "127.0.0.1", "--data-parallel-rpc-port", "13345", ] ) else: # Secondary nodes - run in headless mode server_args.extend( [ "--headless", "--data-parallel-size", str(self.dp_size), "--data-parallel-size-local", str(self.dp_per_node), "--data-parallel-start-rank", str(rank), "--tensor-parallel-size", str(self.tp_size), "--data-parallel-address", "127.0.0.1", "--data-parallel-rpc-port", "13345", ] ) # Use a thread to start each server to allow parallel initialization def start_server(sidx: int, r: int, sargs: list[str]): gpus_per_node = self.tp_size * self.dp_per_node try: # Start the server server = RemoteOpenAIServer( self.model_name, sargs, auto_port=False, env_dict={ "VLLM_SERVER_DEV_MODE": "1", current_platform.device_control_env_var: ",".join( str(current_platform.device_id_to_physical_device_id(i)) for i in range(r, r + gpus_per_node) ), }, ) server.__enter__() if r == 0: print( f"Head node (rank {r}) started successfully with " f"{self.api_server_count} API servers" ) else: print(f"Headless node (rank {r}) started successfully") self.servers[sidx] = (server, sargs) except Exception as e: print(f"Failed to start server rank {r}: {e}") traceback.print_exc() raise thread = threading.Thread( target=start_server, args=(server_idx, rank, server_args) ) thread.start() self.server_threads.append(thread) # Wait for all servers to start for thread in self.server_threads: thread.join() # Give servers additional time to fully initialize and coordinate time.sleep(3) if not all(self.servers): raise Exception("Servers failed to start") return cast(list[tuple[RemoteOpenAIServer, list[str]]], self.servers) def __exit__(self, exc_type, exc_val, exc_tb): """Stop all server instances.""" while self.servers: if server := self.servers.pop(): try: server[0].__exit__(exc_type, exc_val, exc_tb) except Exception as e: print(f"Error stopping server: {e}") traceback.print_exc() class APIOnlyServerManager: """Manages API-only server (Node 0) and headless engines server (Node 1) for testing separated API server and engine configuration.""" def __init__( self, model_name: str, dp_size: int, api_server_count: int, base_server_args: list, tp_size: int = TP_SIZE, ): self.model_name = model_name self.dp_size = dp_size self.tp_size = tp_size self.api_server_count = api_server_count self.base_server_args = base_server_args self.servers: list[tuple[RemoteOpenAIServer, list[str]] | None] = [None] * 2 self.server_threads: list[threading.Thread] = [] def __enter__(self) -> list[tuple[RemoteOpenAIServer, list[str]]]: """Start API-only server and headless engines server.""" # Start API-only server (Node 0) - no engines, only API server api_server_args = self.base_server_args.copy() api_server_args.extend( [ "--data-parallel-size", str(self.dp_size), "--data-parallel-size-local", "0", # No engines on this node "--tensor-parallel-size", str(self.tp_size), "--port", "8000", "--api-server-count", str(self.api_server_count), "--data-parallel-address", "127.0.0.1", "--data-parallel-rpc-port", "13345", ] ) # Start headless engines server (Node 1) - all engines, no API server engines_server_args = self.base_server_args.copy() engines_server_args.extend( [ "--headless", "--data-parallel-size", str(self.dp_size), "--data-parallel-size-local", str(self.dp_size), # All engines on this node "--tensor-parallel-size", str(self.tp_size), "--data-parallel-address", "127.0.0.1", "--data-parallel-rpc-port", "13345", ] ) # Use threads to start both servers in parallel def start_api_server(): try: server = RemoteOpenAIServer( self.model_name, api_server_args, auto_port=False, env_dict={ "VLLM_SERVER_DEV_MODE": "1", # No GPUs needed for API-only server }, ) server.__enter__() print( f"API-only server started successfully with " f"{self.api_server_count} API servers" ) self.servers[0] = (server, api_server_args) except Exception as e: print(f"Failed to start API-only server: {e}") raise def start_engines_server(): try: server = RemoteOpenAIServer( self.model_name, engines_server_args, auto_port=False, env_dict={ current_platform.device_control_env_var: ",".join( str(current_platform.device_id_to_physical_device_id(i)) for i in range(self.dp_size * self.tp_size) ) }, ) server.__enter__() print( f"Headless engines server started successfully with " f"{self.dp_size} engines" ) self.servers[1] = (server, engines_server_args) except Exception as e: print(f"Failed to start headless engines server: {e}") raise # Start API server first api_thread = threading.Thread(target=start_api_server) api_thread.start() self.server_threads.append(api_thread) # Start engines server second engines_thread = threading.Thread(target=start_engines_server) engines_thread.start() self.server_threads.append(engines_thread) # Wait for both servers to start for thread in self.server_threads: thread.join() # Give servers additional time to fully initialize and coordinate time.sleep(3) if not all(self.servers): raise Exception("Both servers failed to start") return cast(list[tuple[RemoteOpenAIServer, list[str]]], self.servers) def __exit__(self, exc_type, exc_val, exc_tb): """Stop both server instances.""" while self.servers: if server := self.servers.pop(): try: server[0].__exit__(exc_type, exc_val, exc_tb) except Exception as e: print(f"Error stopping server: {e}") traceback.print_exc() @pytest.fixture(scope="module") def default_server_args(): return [ # use half precision for speed and memory savings in CI environment "--dtype", "bfloat16", "--max-model-len", "2048", "--max-num-seqs", "128", "--enforce-eager", ] @pytest.fixture(scope="module", params=[1, 4]) def server_manager(request, default_server_args): api_server_count = request.param server_manager = MultinodeInternalLBServerManager( MODEL_NAME, DP_SIZE, api_server_count, default_server_args, DP_SIZE // NUM_NODES, TP_SIZE, ) with server_manager: yield server_manager @pytest.fixture def servers(server_manager): return server_manager.servers @pytest.fixture(scope="module", params=[1, 4]) def api_only_servers(request, default_server_args): """Fixture for API-only server + headless engines configuration.""" api_server_count = request.param with APIOnlyServerManager( MODEL_NAME, DP_SIZE, api_server_count, default_server_args, TP_SIZE ) as server_list: yield server_list @pytest_asyncio.fixture async def client(servers: list[tuple[RemoteOpenAIServer, list[str]]]): # For internal LB, we only connect to the head node (rank 0) # which provides the single API endpoint head_server = servers[0][0] async with head_server.get_async_client() as client: yield client @pytest_asyncio.fixture async def api_only_client(api_only_servers: list[tuple[RemoteOpenAIServer, list[str]]]): """Client fixture for API-only server configuration.""" # Connect to the API-only server (first server in the list) api_server = api_only_servers[0][0] async with api_server.get_async_client() as client: yield client def _get_parallel_config(server: RemoteOpenAIServer): response = requests.get(server.url_for("server_info?config_format=json")) response.raise_for_status() vllm_config = response.json()["vllm_config"] return vllm_config["parallel_config"] def test_multinode_dp_server_info(server_manager): head_server = server_manager.servers[0][0] api_server_count = server_manager.api_server_count # Each request will hit one of the API servers # `n_reqs` is set so that there is a good chance each server # receives at least one request n_reqs = 2 * api_server_count * api_server_count parallel_configs = [_get_parallel_config(head_server) for _ in range(n_reqs)] api_process_counts = [c["_api_process_count"] for c in parallel_configs] api_process_ranks = [c["_api_process_rank"] for c in parallel_configs] assert all(c == api_server_count for c in api_process_counts), api_process_counts assert all(0 <= r < api_server_count for r in api_process_ranks), api_process_ranks @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_multinode_dp_completion( client: openai.AsyncOpenAI, servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: async def make_request(): completion = await client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=5, temperature=1.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] # The exact number of tokens can vary slightly with temperature=1.0, # so we check for a reasonable minimum length. assert len(choice.text) >= 1 # Finish reason might not always be 'length' if the model finishes early # or due to other reasons, especially with high temperature. # So, we'll accept 'length' or 'stop'. assert choice.finish_reason in ("length", "stop") # Token counts can also vary, so we check they are positive. assert completion.usage.completion_tokens > 0 assert completion.usage.prompt_tokens > 0 assert completion.usage.total_tokens > 0 return completion # Test single request result = await make_request() assert result is not None print("Multi-node internal LB handled single completion request successfully") await asyncio.sleep(0.5) # Send multiple requests - internal LB should distribute across DP ranks num_requests = 200 all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) await asyncio.sleep(0.5) # Second burst of requests all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed multi-node internal LB test with " f"{len(servers)} DP ranks (API server count: {api_server_count})" ) # Check request balancing via Prometheus metrics head_server = servers[0][0] check_request_balancing(head_server, DP_SIZE) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_multinode_dp_completion_streaming( client: openai.AsyncOpenAI, servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: prompt = "What is an LLM?" async def make_streaming_request(): # Perform a non-streaming request to get the expected full output single_completion = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text # Perform the streaming request stream = await client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 last_chunk = None async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 last_chunk = chunk # Keep track of the last chunk # finish reason should only return in the last block for OpenAI API assert finish_reason_count == 1, "Finish reason should appear exactly once." assert last_chunk is not None, "Stream should have yielded at least one chunk." assert last_chunk.choices[0].finish_reason == "length", ( "Finish reason should be 'length'." ) # Check that the combined text matches the non-streamed version. assert "".join(chunks) == single_output, ( "Streamed output should match non-streamed output." ) return True # Indicate success for this request # Test single streaming request result = await make_streaming_request() assert result is not None print("Multi-node internal LB handled single streaming request successfully") await asyncio.sleep(0.5) # Send multiple streaming requests - internal LB should distribute across # DP ranks num_requests = 200 all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_streaming_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." await asyncio.sleep(0.5) # Second burst of streaming requests all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_streaming_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." _, server_args = servers[0] api_server_count = ( server_args.count("--api-server-count") and server_args[server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed multi-node internal LB streaming test with " f"{len(servers)} DP ranks (API server count: {api_server_count})" ) # Check request balancing via Prometheus metrics head_server = servers[0][0] check_request_balancing(head_server, DP_SIZE) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_api_only_multinode_dp_completion( api_only_client: openai.AsyncOpenAI, api_only_servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: """Test API-only server with all engines on separate headless server.""" async def make_request(): completion = await api_only_client.completions.create( model=model_name, prompt="Hello, my name is", max_tokens=5, temperature=1.0 ) assert completion.id is not None assert completion.choices is not None and len(completion.choices) == 1 choice = completion.choices[0] # The exact number of tokens can vary slightly with temperature=1.0, # so we check for a reasonable minimum length. assert len(choice.text) >= 1 # Finish reason might not always be 'length' if the model finishes # early or due to other reasons, especially with high temperature. # So, we'll accept 'length' or 'stop'. assert choice.finish_reason in ("length", "stop") # Token counts can also vary, so we check they are positive. assert completion.usage.completion_tokens > 0 assert completion.usage.prompt_tokens > 0 assert completion.usage.total_tokens > 0 return completion # Test single request result = await make_request() assert result is not None print("API-only server handled single completion request successfully") await asyncio.sleep(0.5) # Send multiple requests - should be distributed across engines on # headless server num_requests = 200 all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) await asyncio.sleep(0.5) # Second burst of requests all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(completion is not None for completion in results) api_server, api_server_args = api_only_servers[0] api_server_count = ( api_server_args.count("--api-server-count") and api_server_args[api_server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed API-only multi-node test with {DP_SIZE} " f"engines on headless server (API server count: {api_server_count})" ) # Check request balancing via Prometheus metrics check_request_balancing(api_server, DP_SIZE) @pytest.mark.asyncio @pytest.mark.parametrize( "model_name", [MODEL_NAME], ) async def test_api_only_multinode_dp_completion_streaming( api_only_client: openai.AsyncOpenAI, api_only_servers: list[tuple[RemoteOpenAIServer, list[str]]], model_name: str, ) -> None: """Test API-only server streaming with all engines on separate headless server.""" prompt = "What is an LLM?" async def make_streaming_request(): # Perform a non-streaming request to get the expected full output single_completion = await api_only_client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, ) single_output = single_completion.choices[0].text # Perform the streaming request stream = await api_only_client.completions.create( model=model_name, prompt=prompt, max_tokens=5, temperature=0.0, stream=True ) chunks: list[str] = [] finish_reason_count = 0 last_chunk = None async for chunk in stream: chunks.append(chunk.choices[0].text) if chunk.choices[0].finish_reason is not None: finish_reason_count += 1 last_chunk = chunk # Keep track of the last chunk # finish reason should only return in the last block for OpenAI API assert finish_reason_count == 1, "Finish reason should appear exactly once." assert last_chunk is not None, "Stream should have yielded at least one chunk." assert last_chunk.choices[0].finish_reason == "length", ( "Finish reason should be 'length'." ) # Check that the combined text matches the non-streamed version. assert "".join(chunks) == single_output, ( "Streamed output should match non-streamed output." ) return True # Indicate success for this request # Test single streaming request result = await make_streaming_request() assert result is not None print("API-only server handled single streaming request successfully") await asyncio.sleep(0.5) # Send multiple streaming requests - should be distributed across engines num_requests = 200 all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_streaming_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." await asyncio.sleep(0.5) # Second burst of streaming requests all_tasks = [] for _ in range(num_requests): all_tasks.append(asyncio.create_task(make_streaming_request())) await asyncio.sleep(0.01) results = await asyncio.gather(*all_tasks) assert len(results) == num_requests assert all(results), "Not all streaming requests completed successfully." _, api_server_args = api_only_servers[0] api_server_count = ( api_server_args.count("--api-server-count") and api_server_args[api_server_args.index("--api-server-count") + 1] or 1 ) print( f"Successfully completed API-only streaming test with {DP_SIZE} " f"engines on headless server (API server count: {api_server_count})" ) # Check request balancing via Prometheus metrics api_server = api_only_servers[0][0] check_request_balancing(api_server, DP_SIZE)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_dbo.py
tests/v1/distributed/test_dbo.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Test Dual Batch Overlap (DBO) with Data Parallelism + Expert Parallelism. DBO is specifically designed for DP+EP scenarios to hide communication latency by overlapping computation of two batches. This test validates that DBO works correctly with the DeepSeek-V2-Lite model using GSM8K evaluation. """ import pytest import torch from tests.evals.gsm8k.gsm8k_eval import evaluate_gsm8k from tests.utils import RemoteOpenAIServer from vllm.utils.import_utils import has_deep_ep # Detect Blackwell / B200 (compute capability 10.x) try: if torch.cuda.is_available(): cap = torch.cuda.get_device_capability(0) IS_BLACKWELL = cap[0] >= 10 else: IS_BLACKWELL = False except Exception: # Be conservative: if we can't detect, don't xfail by default IS_BLACKWELL = False MODEL_NAME = "deepseek-ai/DeepSeek-V2-Lite-Chat" DP_SIZE = 2 # GSM8K eval configuration NUM_QUESTIONS = 256 # Fast eval for CI; but must be large enough to hit dbo thresholds NUM_SHOTS = 5 # Few-shot examples MIN_ACCURACY = 0.62 # Expected 0.64 with 2% buffer (based on vLLM test data) # Increase max_num_seqs to trigger DBO for decode batches # With 64 seqs, decode batches should exceed the 32 token threshold MAX_NUM_SEQS = 64 # Increased from 16 to trigger decode DBO # DeepEP backends to test DEEPEP_BACKENDS = [ "deepep_low_latency", "deepep_high_throughput", ] @pytest.mark.skipif(not has_deep_ep(), reason="These tests require deep_ep to run") @pytest.mark.parametrize("all2all_backend", DEEPEP_BACKENDS) @pytest.mark.xfail( IS_BLACKWELL, reason=( "Temporary: DBO accuracy unstable on Blackwell " "(doesn't meet expectation of MIN_ACCURACY = 0.62)" ), ) def test_dbo_dp_ep_gsm8k(all2all_backend: str, num_gpus_available): """ Test DBO with DP+EP using GSM8K evaluation. """ required_gpus = DP_SIZE if num_gpus_available < required_gpus: pytest.skip(f"Need at least {required_gpus} GPUs (DP={DP_SIZE})") # Server arguments for DBO + DP + EP server_args = [ "--max-model-len", "4096", "--max-num-seqs", str(MAX_NUM_SEQS), # Use larger batch to trigger decode DBO "--trust-remote-code", # Note: Not using --enforce-eager to test DBO's alternate CUDA graph dispatching "--data-parallel-size", str(DP_SIZE), "--enable-expert-parallel", "--enable-dbo", # Fix threshold so we know we trigger DBO "--dbo-decode-token-threshold", "16", "--dbo-prefill-token-threshold", "256", "--all2all-backend", all2all_backend, ] with RemoteOpenAIServer( MODEL_NAME, server_args, max_wait_seconds=600, # Allow time for model loading with DP+EP ) as remote_server: # Use host and port directly from RemoteOpenAIServer host = f"http://{remote_server.host}" port = remote_server.port # Run GSM8K evaluation results = evaluate_gsm8k( num_questions=NUM_QUESTIONS, num_shots=NUM_SHOTS, host=host, port=port, ) # Validate accuracy is reasonable accuracy = results["accuracy"] assert accuracy >= MIN_ACCURACY, ( f"DBO+DP+EP accuracy too low ({all2all_backend}): " f"{accuracy:.3f} < {MIN_ACCURACY:.3f} " )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/__init__.py
tests/v1/distributed/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/distributed/test_eagle_dp.py
tests/v1/distributed/test_eagle_dp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os from contextlib import AsyncExitStack from dataclasses import replace import pytest from vllm import SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.sampling_params import RequestOutputKind from vllm.v1.engine.async_llm import AsyncLLM DP_SIZE = int(os.getenv("DP_SIZE", 2)) @pytest.mark.asyncio async def test_run_eagle_dp(monkeypatch: pytest.MonkeyPatch): # This test checks that running a model with and without eagle # leads to identical tokens. This is only true in batch invariant mode # (because the target model verifies all draft tokens in one big forward pass) monkeypatch.setenv("VLLM_BATCH_INVARIANT", "1") target_model = "meta-llama/Llama-3.1-8B-Instruct" draft_model = "yuhuili/EAGLE-LLaMA3.1-Instruct-8B" engine_args = AsyncEngineArgs( model=target_model, tokenizer_mode="auto", enforce_eager=False, tensor_parallel_size=int(os.getenv("TP_SIZE", 1)), data_parallel_size=DP_SIZE, data_parallel_backend="mp", # ray takes more time trust_remote_code=True, max_model_len=16384, attention_config={"backend": "FLASH_ATTN"}, ) eagle_engine_args = replace( engine_args, speculative_config={ "model": draft_model, "method": "eagle", "num_speculative_tokens": 3, }, ) prompt = "This is a test of data parallel with eagle" num_expected_tokens = 100 sampling_params = SamplingParams( min_tokens=num_expected_tokens, max_tokens=num_expected_tokens, ignore_eos=True, output_kind=RequestOutputKind.FINAL_ONLY, temperature=0, ) async def generate_with_timeout(given_engine: AsyncLLM): async for out in given_engine.generate( request_id="test-eagle-dp", prompt=prompt, sampling_params=sampling_params ): token_ids = out.outputs[0].token_ids assert len(token_ids) == num_expected_tokens return token_ids async def engine_create_and_generate(engine_args: AsyncEngineArgs): async with AsyncExitStack() as after: engine = AsyncLLM.from_engine_args(engine_args) after.callback(engine.shutdown) token_ids = await asyncio.wait_for( generate_with_timeout(engine), timeout=30 ) assert not engine.output_processor.has_unfinished_requests() return token_ids token_ids_with_eagle = await engine_create_and_generate(eagle_engine_args) token_ids_no_eagle = await engine_create_and_generate(engine_args) # Test for correctness assert token_ids_with_eagle == token_ids_no_eagle
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_mtp.py
tests/v1/spec_decode/test_mtp.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from unittest import mock import pytest import torch from tests.v1.attention.utils import ( BatchSpec, create_common_attn_metadata, create_standard_kv_cache_spec, try_get_attention_backend, ) from vllm.attention.backends.registry import AttentionBackendEnum from vllm.config import ( CacheConfig, DeviceConfig, ModelConfig, ParallelConfig, SchedulerConfig, SpeculativeConfig, VllmConfig, ) from vllm.config.load import LoadConfig from vllm.model_executor.models.llama import LlamaForCausalLM from vllm.platforms import current_platform from vllm.v1.spec_decode.eagle import EagleProposer mimo_7b_dir = "XiaomiMiMo/MiMo-7B-Base" def _create_mtp_proposer(num_speculative_tokens: int) -> EagleProposer: """Create an MTP proposer with unified model configuration.""" model_config = ModelConfig( model=mimo_7b_dir, runner="generate", max_model_len=100, trust_remote_code=True ) speculative_config = SpeculativeConfig( target_model_config=model_config, target_parallel_config=ParallelConfig(), model=mimo_7b_dir, method="mtp", num_speculative_tokens=num_speculative_tokens, ) vllm_config = VllmConfig( model_config=model_config, cache_config=CacheConfig(), speculative_config=speculative_config, device_config=DeviceConfig(device=current_platform.device_type), parallel_config=ParallelConfig(), load_config=LoadConfig(), scheduler_config=SchedulerConfig( max_model_len=model_config.max_model_len, is_encoder_decoder=model_config.is_encoder_decoder, ), ) return EagleProposer(vllm_config=vllm_config, device=current_platform.device_type) @mock.patch("vllm.v1.spec_decode.eagle.get_pp_group") @mock.patch("vllm.v1.spec_decode.eagle.get_layers_from_vllm_config") @mock.patch("vllm.v1.spec_decode.eagle.get_model") def test_mtp_load_model_unified(mock_get_model, mock_get_layers, mock_get_pp_group): """Test MTP-specific model loading with unified model approach.""" # Setup mocks mock_model = mock.MagicMock() mock_model.model.embed_tokens.weight.shape = (131072, 4096) mock_get_model.return_value = mock_model # MTP does not have its own embed_tokens or lm_head # so it should share them with the target model mock_model.has_own_embed_tokens = False mock_model.has_own_lm_head = False target_attn_layers = {"target_attn_1": mock.MagicMock()} all_attn_layers = {**target_attn_layers, "draft_attn_1": mock.MagicMock()} target_indexer_layers: dict = {} all_indexer_layers: dict = {} mock_get_layers.side_effect = [ target_attn_layers, target_indexer_layers, all_attn_layers, all_indexer_layers, ] mock_pp_group = mock.MagicMock() mock_pp_group.world_size = 1 mock_get_pp_group.return_value = mock_pp_group # Create target model class _TargetModelStub(LlamaForCausalLM): model: mock.MagicMock lm_head: mock.MagicMock target_model = mock.create_autospec(_TargetModelStub, instance=True) target_model.model = mock.MagicMock() target_model.model.embed_tokens.weight.shape = (131072, 4096) target_model.lm_head = mock.MagicMock() # Create MTP proposer proposer = _create_mtp_proposer(num_speculative_tokens=4) proposer.load_model(target_model) # Verify MTP-specific behavior: # Model is loaded mock_get_model.assert_called_once() # MTP shares lm_head with target model assert proposer.model.lm_head == target_model.lm_head # MTP shares embed_tokens with target model assert proposer.model.model.embed_tokens == target_model.model.embed_tokens @pytest.mark.parametrize("num_speculative_tokens", [1]) def test_mtp_propose(num_speculative_tokens, monkeypatch): """Test that MTP's forward method returns hidden states directly""" device = torch.device(current_platform.device_type) batch_size = 2 seq_lens = [5, 3] total_tokens = sum(seq_lens) vocab_size = 100 proposer = _create_mtp_proposer(num_speculative_tokens) hidden_size = proposer.hidden_size # Mock the MTP model to verify it returns hidden states directly model_mock = mock.MagicMock() # MTP returns hidden states directly if num_speculative_tokens == 1: model_mock.return_value = torch.zeros(total_tokens, hidden_size, device=device) else: # Multiple forward passes for multi-token speculation forward_returns = [] for i in range(num_speculative_tokens): if i == 0: h_states = torch.zeros(total_tokens, hidden_size, device=device) else: h_states = torch.zeros(batch_size, hidden_size, device=device) forward_returns.append(h_states) model_mock.side_effect = forward_returns # Mock compute_logits def create_deterministic_logits(batch_size, vocab_size, token_offset): logits = torch.full((batch_size, vocab_size), -100.0, device=device) logits[:, token_offset] = 100.0 return logits if num_speculative_tokens == 1: model_mock.compute_logits.return_value = create_deterministic_logits( batch_size, vocab_size, 42 ) else: logits_returns = [ create_deterministic_logits(batch_size, vocab_size, 42 + i) for i in range(num_speculative_tokens) ] model_mock.compute_logits.side_effect = logits_returns proposer.model = model_mock proposer.attn_layer_names = ["layer.0"] # Prepare inputs batch_spec = BatchSpec(seq_lens=seq_lens, query_lens=seq_lens) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device ) target_token_ids = torch.randint(0, vocab_size, (total_tokens,), device=device) target_positions = torch.cat( [ torch.arange(seq_lens[0], device=device), torch.arange(seq_lens[1], device=device), ] ) target_hidden_states = torch.randn(total_tokens, hidden_size, device=device) next_token_ids = torch.randint( 0, vocab_size, (batch_size,), dtype=torch.int32, device=device ) sampling_metadata = mock.MagicMock() # Setup attention metadata attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.FLASH_ATTN ) attn_metadata_builder = attn_metadata_builder_cls( kv_cache_spec=create_standard_kv_cache_spec(proposer.vllm_config), layer_names=proposer.attn_layer_names, vllm_config=proposer.vllm_config, device=device, ) proposer.runner = mock.MagicMock() proposer.attn_metadata_builder = attn_metadata_builder # Run propose result = proposer.propose( target_token_ids=target_token_ids, target_positions=target_positions, target_hidden_states=target_hidden_states, next_token_ids=next_token_ids, last_token_indices=None, common_attn_metadata=common_attn_metadata, sampling_metadata=sampling_metadata, ) # Verify the model was called correctly assert model_mock.called # Verify output shape assert result.shape == (batch_size, num_speculative_tokens)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_ngram.py
tests/v1/spec_decode/test_ngram.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import numpy as np from vllm.config import ( ModelConfig, SpeculativeConfig, VllmConfig, ) from vllm.v1.spec_decode.ngram_proposer import ( NgramProposer, _find_longest_matched_ngram_and_propose_tokens, ) def test_find_longest_matched_ngram_and_propose_tokens(): tokens = np.array([1, 2, 3, 4, 1, 2, 3, 5, 6]) result = _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=2, max_ngram=2, max_model_len=1024, k=2 ) assert len(result) == 0 tokens = np.array([1, 2, 3, 4, 1, 2, 3]) np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=2, max_ngram=2, max_model_len=1024, k=3 ), np.array([4, 1, 2]), ) np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=2, max_ngram=2, max_model_len=1024, k=2 ), np.array([4, 1]), ) np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=1, max_ngram=1, max_model_len=1024, k=3 ), np.array([4, 1, 2]), ) np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=1, max_ngram=1, max_model_len=1024, k=2 ), np.array([4, 1]), ) tokens = np.array([1, 3, 6, 2, 3, 4, 1, 2, 3]) np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=2, max_ngram=2, max_model_len=1024, k=3 ), np.array([4, 1, 2]), ) # Return on the first match np.testing.assert_array_equal( _find_longest_matched_ngram_and_propose_tokens( origin_tokens=tokens, min_ngram=1, max_ngram=1, max_model_len=1024, k=2 ), np.array([6, 2]), ) def test_ngram_proposer(): def get_ngram_proposer(min_n: int, max_n: int, k: int) -> NgramProposer: # Dummy model config. Just to set max_model_len. model_config = ModelConfig(model="facebook/opt-125m") return NgramProposer( vllm_config=VllmConfig( model_config=model_config, speculative_config=SpeculativeConfig( prompt_lookup_min=min_n, prompt_lookup_max=max_n, num_speculative_tokens=k, method="ngram", ), ) ) # No match. token_ids_cpu = np.array([[1, 2, 3, 4, 5]]) result = get_ngram_proposer(min_n=2, max_n=2, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result[0]) == 0 # No match for 4-gram. token_ids_cpu = np.array([[1, 2, 3, 4, 1, 2, 3]]) result = get_ngram_proposer(min_n=4, max_n=4, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result[0]) == 0 # No match for 4-gram but match for 3-gram. token_ids_cpu = np.array([[1, 2, 3, 4, 1, 2, 3]]) result = get_ngram_proposer(min_n=3, max_n=4, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert np.array_equal(result, np.array([[4, 1]])) # Match for both 4-gram and 3-gram. # In this case, the proposer should return the 4-gram match. token_ids_cpu = np.array([[2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]]) result = get_ngram_proposer(min_n=3, max_n=4, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert np.array_equal(result, np.array([[1, 2]])) # Not [5, 1]] # Match for 2-gram and 3-gram, but not 4-gram. token_ids_cpu = np.array([[3, 4, 5, 2, 3, 4, 1, 2, 3, 4]]) result = get_ngram_proposer(min_n=2, max_n=4, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert np.array_equal(result, np.array([[1, 2]])) # Not [5, 2]] # Multiple 3-gram matched, but always pick the first one. token_ids_cpu = np.array([[1, 2, 3, 100, 1, 2, 3, 200, 1, 2, 3, 300, 1, 2, 3]]) result = get_ngram_proposer(min_n=3, max_n=3, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert np.array_equal(result, np.array([[100, 1]])) # check empty input token_ids_cpu = np.array([[]]) result = get_ngram_proposer(min_n=2, max_n=2, k=2).propose( sampled_token_ids=[[0]], req_ids=["0"], num_tokens_no_spec=np.array([len(c) for c in token_ids_cpu]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result[0]) == 0 # check multibatch input # first request has 5 tokens and a match # second request has 3 tokens and no match. Padded with -1 for max len 5 token_ids_cpu = np.array([[1, 2, 3, 1, 2], [4, 5, 6, -1, -1]]) result = get_ngram_proposer(min_n=2, max_n=2, k=2).propose( sampled_token_ids=[[0], [1]], req_ids=["0", "1"], num_tokens_no_spec=np.array([5, 3]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result[0]) == 2 assert np.array_equal(result[0], np.array([3, 1])) assert np.array_equal(result[1], np.array([])) # Test non-contiguous indices: requests 0 and 2 need proposals, # request 1 is in prefill proposer = get_ngram_proposer(min_n=2, max_n=2, k=2) max_model_len = 20 token_ids_cpu = np.zeros((3, max_model_len), dtype=np.int32) token_ids_cpu[0, :5] = [1, 2, 3, 1, 2] token_ids_cpu[1, :3] = [4, 5, 6] token_ids_cpu[2, :5] = [7, 8, 9, 7, 8] num_tokens_no_spec = np.array([5, 3, 5], dtype=np.int32) sampled_token_ids = [[2], [], [8]] # Empty list for request 1 simulates prefill result = proposer.propose( sampled_token_ids=sampled_token_ids, req_ids=["0", "1", "2"], num_tokens_no_spec=num_tokens_no_spec, token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result) == 3 assert np.array_equal(result[0], [3, 1]) assert len(result[1]) == 0 assert np.array_equal(result[2], [9, 7]) # Verify internal arrays written to correct indices assert proposer.valid_ngram_num_drafts[0] == 2 assert proposer.valid_ngram_num_drafts[1] == 0 assert proposer.valid_ngram_num_drafts[2] == 2 assert np.array_equal(proposer.valid_ngram_draft[0, :2], [3, 1]) assert np.array_equal(proposer.valid_ngram_draft[2, :2], [9, 7]) # test if 0 threads available: can happen if TP size > CPU count ngram_proposer = get_ngram_proposer(min_n=2, max_n=2, k=2) ngram_proposer.num_numba_thread_available = 0 # set max_model_len to 2 * threshold to ensure multithread is used num_tokens_threshold = ngram_proposer.num_tokens_threshold ngram_proposer.max_model_len = 2 * num_tokens_threshold # using multibatch test middle_integer = num_tokens_threshold // 2 input_1 = [_ for _ in range(num_tokens_threshold)] input_1 += [middle_integer, middle_integer + 1] input_2 = [-1] * len(input_1) input_2[:3] = [4, 5, 6] token_ids_cpu = np.array([input_1, input_2]) result = ngram_proposer.propose( sampled_token_ids=[[0], [1]], req_ids=["0", "1"], num_tokens_no_spec=np.array([len(input_1), 3]), token_ids_cpu=token_ids_cpu, spec_decode_unsupported_reqs=(), ) assert len(result[0]) == 2 assert np.array_equal(result[0], np.array([middle_integer + 2, middle_integer + 3])) assert np.array_equal(result[1], np.array([]))
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_speculators_eagle3.py
tests/v1/spec_decode/test_speculators_eagle3.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from vllm.config import SpeculativeConfig from vllm.model_executor.models.interfaces import supports_eagle3 from vllm.platforms import current_platform @pytest.mark.parametrize( "model_path", [ pytest.param( "nm-testing/SpeculatorLlama3-1-8B-Eagle3-converted-0717-quantized", id="llama3-eagle3-speculator", ), pytest.param( "nm-testing/Speculator-Qwen3-8B-Eagle3-converted-071-quantized", id="qwen3-eagle3-speculator", ), pytest.param( "nm-testing/Speculator-Qwen3-8B-Eagle3-converted-071-quantized-w4a16", id="qwen3-eagle3-speculator-w4a16-verifier", marks=pytest.mark.skipif( current_platform.is_rocm(), reason="The tests are skipped on rocm platform.", ), ), ], ) def test_eagle3_speculators_model( vllm_runner, example_prompts, model_path, monkeypatch ): """ Test Eagle3 speculators models properly initialize speculative decoding. This test verifies: 1. Eagle3 support is detected for the model 2. Speculative config is automatically initialized from embedded config 3. The draft model path is correctly set to the speculators model 4. Speculative tokens count is valid 5. Text generation works with speculative decoding enabled """ # Set environment variable for V1 engine serialization monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") with vllm_runner(model_path, dtype=torch.bfloat16) as vllm_model: # Verify Eagle3 support is detected eagle3_supported = vllm_model.apply_model(supports_eagle3) assert eagle3_supported, f"Eagle3 should be supported for {model_path}" vllm_config = vllm_model.llm.llm_engine.vllm_config assert isinstance(vllm_config.speculative_config, SpeculativeConfig), ( "Speculative config should be initialized for speculators model" ) spec_config = vllm_config.speculative_config assert spec_config.num_speculative_tokens > 0, ( f"Expected positive speculative tokens, " f"got {spec_config.num_speculative_tokens}" ) assert spec_config.model == model_path, ( f"Draft model should be {model_path}, got {spec_config.model}" ) vllm_outputs = vllm_model.generate_greedy(example_prompts, max_tokens=20) assert vllm_outputs, f"No outputs generated for speculators model {model_path}"
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_tree_attention.py
tests/v1/spec_decode/test_tree_attention.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math import pytest import torch from tests.v1.attention.utils import ( create_standard_kv_cache_spec, create_vllm_config, try_get_attention_backend, ) from vllm.attention.backends.registry import AttentionBackendEnum from vllm.attention.utils.fa_utils import is_flash_attn_varlen_func_available from vllm.config import ParallelConfig, SpeculativeConfig from vllm.v1.attention.backends.utils import CommonAttentionMetadata if not is_flash_attn_varlen_func_available(): pytest.skip( "This test requires flash_attn_varlen_func, but it's not available.", allow_module_level=True, ) class MockAttentionLayer(torch.nn.Module): _q_scale = torch.tensor(1.0, dtype=torch.float32, device="cuda") _k_scale = torch.tensor(1.0, dtype=torch.float32, device="cuda") _v_scale = torch.tensor(1.0, dtype=torch.float32, device="cuda") def __init__(self): super().__init__() def forward(self, x): return x def forward_attention( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, kv_cache: torch.Tensor, block_table: torch.Tensor, slot_mapping: torch.Tensor, seqlen_k: int, backend: AttentionBackendEnum, spec_token_tree: str | None = None, num_spec_tokens: int = 0, ) -> torch.Tensor: batch_size, q_len, num_heads, dim_per_head = q.shape num_kv_heads = k.shape[-2] # Initialize the query and KV sequence lengths. query_start_loc = q_len * torch.arange( batch_size + 1, device=q.device, dtype=torch.int32 ) query_lens = torch.diff(query_start_loc) seq_lens = torch.full( (batch_size,), seqlen_k, device=q.device, dtype=torch.int32, ) context_lens = seq_lens - query_lens max_seq_len = int(seq_lens.max()) max_query_len = q_len num_actual_tokens = query_start_loc[-1] softmax_scale = q.shape[-1] ** (-0.5) layer = MockAttentionLayer() # Build common metadata. model_name = "meta-llama/Meta-Llama-3-8B" builder_cls, impl_cls = try_get_attention_backend(backend) vllm_config = create_vllm_config(model_name=model_name, max_model_len=max(seq_lens)) if spec_token_tree is not None: # Create speculative config if token tree is specified. vllm_config.speculative_config = SpeculativeConfig( target_model_config=vllm_config.model_config, target_parallel_config=ParallelConfig(), model=model_name, method="eagle", num_speculative_tokens=num_spec_tokens, speculative_token_tree=spec_token_tree, ) kv_cache_spec = create_standard_kv_cache_spec(vllm_config) builder = builder_cls(kv_cache_spec, [], vllm_config, q.device) common_attn_metadata = CommonAttentionMetadata( query_start_loc=query_start_loc, query_start_loc_cpu=query_start_loc.cpu(), seq_lens=seq_lens, _seq_lens_cpu=seq_lens.cpu(), _num_computed_tokens_cpu=context_lens.cpu(), num_reqs=batch_size, num_actual_tokens=num_actual_tokens, max_query_len=max_query_len, max_seq_len=max_seq_len, block_table_tensor=block_table, slot_mapping=slot_mapping, ) # Build attention metadata. attn_metadata = builder.build( common_prefix_len=0, common_attn_metadata=common_attn_metadata, ) # Initialize the backend implementation. instance = impl_cls( num_heads=num_heads, head_size=dim_per_head, scale=softmax_scale, num_kv_heads=num_kv_heads, alibi_slopes=None, sliding_window=None, kv_cache_dtype="auto", ) # Run forward pass and return output. query = q.view(-1, num_heads, dim_per_head) key = k.view(-1, num_kv_heads, dim_per_head) value = v.view(-1, num_kv_heads, dim_per_head) output = torch.empty_like(query) return instance.forward( layer=layer, query=query, key=key, value=value, kv_cache=kv_cache.clone(), attn_metadata=attn_metadata, output=output, ) def test_tree_attn_correctness() -> None: torch.manual_seed(42) torch.cuda.manual_seed_all(42) device = "cuda" tree_attn_masks = { # Chain. "[(0,), (0, 0), (0, 0, 0)]": torch.tensor( [ [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1], ], device=device, dtype=torch.int32, ), # Tree. "[(0,), (1,), (0, 0), (0, 1), (1, 0), (1, 1)]": torch.tensor( [ [1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [1, 1, 0, 1, 0, 0, 0], [1, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1], ], device=device, dtype=torch.int32, ), } dim_per_head = 128 num_kv_heads = 2 block_size = 32 max_sequence_length = 8192 randomize_blocks = True for batch_size in [1, 16, 32]: for num_heads in [2, 4]: for sequence_position in [16, 1024, 2048]: for spec_token_tree, tree_attn_mask in tree_attn_masks.items(): # Assert that the number of heads is divisible # by the number of KV heads. assert num_heads % num_kv_heads == 0 # Initialize q, k, and v. tree_size_q = tree_attn_mask.shape[0] seqlen_k = sequence_position + tree_size_q q = torch.randn( (batch_size, tree_size_q, num_heads, dim_per_head), device=device, dtype=torch.bfloat16, ) k = torch.randn( (batch_size, tree_size_q, num_kv_heads, dim_per_head), device=device, dtype=torch.bfloat16, ) v = torch.randn( (batch_size, tree_size_q, num_kv_heads, dim_per_head), device=device, dtype=torch.bfloat16, ) # Set up the block table and KV cache for paged KV. assert max_sequence_length % block_size == 0 max_blocks_per_batch = max_sequence_length // block_size kv_cache = torch.randn( ( 2, batch_size * max_blocks_per_batch, block_size, num_kv_heads, dim_per_head, ), device=q.device, dtype=torch.bfloat16, ) num_alloc_blocks_per_batch = math.ceil(seqlen_k / block_size) block_table = torch.zeros( (batch_size, max_blocks_per_batch), device=q.device, dtype=torch.int32, ) block_ids = torch.arange( 0, batch_size * num_alloc_blocks_per_batch, device=q.device, dtype=torch.int32, ) if randomize_blocks: # Randomize the block ids. block_ids = block_ids[torch.randperm(block_ids.numel())] block_table[:, :num_alloc_blocks_per_batch] = block_ids.view( -1, num_alloc_blocks_per_batch ) # Set up the slot mapping for the input KVs. tree_positions = sequence_position + torch.arange( 0, tree_size_q, device=q.device, dtype=torch.int64, ).repeat(batch_size, 1) tree_slot_mapping = _gen_slot_mapping( tree_positions, block_table, block_size ) # Compute attention for the tree. tree_attn_output = forward_attention( q=q, k=k, v=v, kv_cache=kv_cache, block_table=block_table, slot_mapping=tree_slot_mapping, seqlen_k=seqlen_k, backend=AttentionBackendEnum.TREE_ATTN, spec_token_tree=spec_token_tree, num_spec_tokens=tree_size_q - 1, ).view(batch_size, -1, num_heads, dim_per_head) # Verify that the chain attention output for each # branch of the tree (computed using FA3) matches # the tree attention output. for q_index in range(tree_size_q): # Get the q, k, and v for the branch. branch_mask = tree_attn_mask[q_index, :] branch_indices = torch.nonzero(branch_mask, as_tuple=True)[0] q_len = branch_indices.shape[0] q_branch = q[:, branch_indices] k_branch = k[:, branch_indices] v_branch = v[:, branch_indices] # Setup slot mapping for the branch. branch_positions = sequence_position + torch.arange( 0, q_len, device=q.device, dtype=torch.int64, ).repeat(batch_size, 1) branch_slot_mapping = _gen_slot_mapping( branch_positions, block_table, block_size ) # Compute flash attention for the branch. flash_attn_output = forward_attention( q=q_branch, k=k_branch, v=v_branch, kv_cache=kv_cache, block_table=block_table, slot_mapping=branch_slot_mapping, seqlen_k=sequence_position + q_len, backend=AttentionBackendEnum.FLASH_ATTN, ).view(batch_size, -1, num_heads, dim_per_head) # Compare the outputs. assert torch.allclose( tree_attn_output[:, branch_indices], flash_attn_output, atol=7.81e-3, ), ( f"outputs are not close for " f"batch_size: {batch_size}, " f"num_heads: {num_heads}, " f"sequence_position: {sequence_position}, " f"tree_attn_mask: {tree_attn_mask}, " f"q_index: {q_index}." ) def _gen_slot_mapping( positions: torch.Tensor, block_table: torch.Tensor, block_size: int ): block_indices = positions // block_size blocks = block_table.gather(dim=1, index=block_indices) return (blocks * block_size + positions % block_size).view(-1)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_eagle.py
tests/v1/spec_decode/test_eagle.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from unittest import mock import pytest import torch from tests.utils import get_attn_backend_list_based_on_platform from tests.v1.attention.utils import ( BatchSpec, create_common_attn_metadata, create_standard_kv_cache_spec, try_get_attention_backend, ) from vllm.attention.backends.registry import AttentionBackendEnum from vllm.config import ( AttentionConfig, CacheConfig, DeviceConfig, ModelConfig, ParallelConfig, SchedulerConfig, SpeculativeConfig, VllmConfig, ) from vllm.config.load import LoadConfig from vllm.model_executor.models.llama import LlamaForCausalLM from vllm.platforms import current_platform from vllm.v1.spec_decode.eagle import EagleProposer from vllm.v1.spec_decode.metadata import SpecDecodeMetadata from vllm.v1.worker.gpu_input_batch import CachedRequestState, InputBatch model_dir = "meta-llama/Llama-3.1-8B-Instruct" eagle_dir = "yuhuili/EAGLE-LLaMA3.1-Instruct-8B" eagle3_dir = "yuhuili/EAGLE3-LLaMA3.1-Instruct-8B" def _create_proposer( method: str, num_speculative_tokens: int, attention_backend: str | None = None, speculative_token_tree: list[tuple[int, ...]] | None = None, ) -> EagleProposer: model_config = ModelConfig(model=model_dir, runner="generate", max_model_len=100) # Choose model directory based on method draft_model_dir = eagle_dir if method == "eagle" else eagle3_dir spec_token_tree_str = None if speculative_token_tree is not None: assert num_speculative_tokens == len(speculative_token_tree) spec_token_tree_str = str(speculative_token_tree) speculative_config = SpeculativeConfig( target_model_config=model_config, target_parallel_config=ParallelConfig(), model=draft_model_dir, method=method, num_speculative_tokens=num_speculative_tokens, speculative_token_tree=spec_token_tree_str, ) vllm_config = VllmConfig( model_config=model_config, cache_config=CacheConfig(), speculative_config=speculative_config, device_config=DeviceConfig(device=current_platform.device_type), parallel_config=ParallelConfig(), load_config=LoadConfig(), scheduler_config=SchedulerConfig( max_model_len=model_config.max_model_len, is_encoder_decoder=model_config.is_encoder_decoder, ), attention_config=AttentionConfig(backend=attention_backend), ) return EagleProposer(vllm_config=vllm_config, device=current_platform.device_type) def test_prepare_next_token_ids(): """ Test for prepare_next_token_ids_cpu and prepare_next_token_ids_padded. Each will produce a device tensor of next_token_ids, taking as input either the GPU tensor of sampled_token_ids with -1 for rejected tokens, or the CPU python list[list[int]] with the rejected tokens removed. """ device = torch.device(current_platform.device_type) num_requests = 4 num_speculative_tokens = 4 batch_spec = BatchSpec( seq_lens=[num_speculative_tokens + 1] * num_requests, query_lens=[num_speculative_tokens + 1] * num_requests, ) req_ids = [f"req_{i + 1}" for i in range(num_requests)] mock_input_batch = mock.MagicMock(spec=InputBatch) mock_input_batch.req_ids = req_ids mock_input_batch.num_reqs = num_requests mock_input_batch.vocab_size = 100 mock_num_scheduled_tokens = {req_id: 0 for req_id in req_ids} mock_requests = {} for req_id in req_ids: mock_request = mock.MagicMock(spec=CachedRequestState) # Each request will have a backup next token id of 10, 20, 30, 40 mock_request.get_token_id.return_value = int(req_id.split("_")[1]) * 10 mock_request.num_computed_tokens = 0 mock_requests[req_id] = mock_request # explicitly discard the last request discarded_req_mask = torch.tensor( [False, False, False, True], dtype=torch.bool, device=device ) sampled_token_ids = [ [0, 1, -1, -1, -1], # 1 accepted, 3 rejected, "1" sampled [0, 1, 2, 3, 4], # all accepted, "4" sampled [-1, -1, -1, -1, -1], # sampling skipped, use backup token "30" [0, 1, 2, -1, -1], # explicitly discarded, sampling should be ignored ] sampled_token_ids_tensor = torch.tensor( sampled_token_ids, dtype=torch.int32, device=device ) sampled_token_ids_cpu = [[i for i in seq if i != -1] for seq in sampled_token_ids] for i in range(len(sampled_token_ids_cpu)): if discarded_req_mask[i]: sampled_token_ids_cpu[i] = [] expected_next_token_ids_cpu = [1, 4, 30, 40] expected_next_token_ids_tensor = torch.tensor( expected_next_token_ids_cpu, dtype=torch.int32, device=device ) proposer = _create_proposer("eagle", num_speculative_tokens) next_token_ids_from_cpu = proposer.prepare_next_token_ids_cpu( sampled_token_ids_cpu, mock_requests, mock_input_batch, mock_num_scheduled_tokens, ) assert torch.equal(next_token_ids_from_cpu, expected_next_token_ids_tensor) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device, ) expected_valid_sampled_tokens_count = torch.tensor( [2, 5, 0, 0], dtype=torch.int32, device=device ) next_token_ids_from_padded, valid_sampled_tokens_count = ( proposer.prepare_next_token_ids_padded( common_attn_metadata, sampled_token_ids_tensor, mock_requests, mock_input_batch, discarded_req_mask, ) ) assert torch.equal(next_token_ids_from_padded, expected_next_token_ids_tensor) assert torch.equal(valid_sampled_tokens_count, expected_valid_sampled_tokens_count) def test_prepare_inputs(): """ cu_target_query_lens: [0, a, a + b, a + b + c] num_rejected_tokens: [n1, n2, n3] num_tokens_per_req: [a - n1, b - n2, c - n3] cu_num_tokens: [0, a - n1, a + b - n1 - n2, a + b + c - n1 - n2 - n3] token_indices: [0, 1, ..., a - n1 - 1, a, a + 1, ..., a + b - n2 - 1, a + b, a + b + 1, ..., a + b + c - n3 - 1] """ device = torch.device(current_platform.device_type) # q1 = 4, q2 = 7, q3 = 5 # n1 = 1, n2 = 3, n3 = 2 batch_spec = BatchSpec( seq_lens=[4, 7, 5], query_lens=[4, 7, 5], ) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device, ) # If there are `k` sampled tokens, then `k-1` tokens are draft tokens # from the previous iteration, and the last token is the bonus token sampled # from the base model. num_draft_tokens = [3, 6, 4] # one less than query_lens # num rejected tokens is [1, 3, 2] ACCEPT_TOKEN = 0 BONUS_TOKEN = 1 REJECT_TOKEN = -1 sampled_token_ids = [ [ACCEPT_TOKEN, ACCEPT_TOKEN, REJECT_TOKEN, BONUS_TOKEN], [ ACCEPT_TOKEN, ACCEPT_TOKEN, ACCEPT_TOKEN, REJECT_TOKEN, REJECT_TOKEN, REJECT_TOKEN, BONUS_TOKEN, ], [ACCEPT_TOKEN, ACCEPT_TOKEN, REJECT_TOKEN, REJECT_TOKEN, BONUS_TOKEN], ] sampled_token_ids = [ [i for i in seq if i != REJECT_TOKEN] for seq in sampled_token_ids ] # Expected calculations: # query_len_per_req = [4, 7, 5] # num_tokens_per_req = [3, 4, 3] (after subtracting rejected tokens) # Expected cumulative counts: [0, 3, 7, 10] expected_cu_num_tokens = torch.tensor( [0, 3, 7, 10], dtype=torch.int32, device=device ) # Expected token indices (mapped from original positions): # First request: indices 0, 1, 2 (keeping first 3 from positions 0-3) # Second request: indices 4, 5, 6, 7 (keeping first 4 from positions 4-10) # Third request: indices 11, 12, 13 (keeping first 3 from positions 11-15) expected_token_indices = torch.tensor( [ 0, 1, 2, # First request: 3 tokens (4-1) 4, 5, 6, 7, # Second request: 4 tokens (7-3) 11, 12, 13, # Third request: 3 tokens (5-2) ], dtype=torch.int32, device=device, ) proposer = _create_proposer("eagle", 1) updated_metadata, token_indices = proposer.prepare_inputs( common_attn_metadata, sampled_token_ids, num_draft_tokens ) assert torch.equal(updated_metadata.query_start_loc, expected_cu_num_tokens) assert token_indices.shape[0] == expected_cu_num_tokens[-1].item() assert torch.equal(token_indices, expected_token_indices) def test_prepare_inputs_padded(): """ Input scenario is 3 requests with num_speculative_tokens == 2 and: - Request 1: query_len = 3, rejected = 1 - Request 2: query_len = 3, rejected = 0 - Request 3: query_len = 3, rejected = 2 Expected outputs: token_indices_to_sample: [1, 5, 6] Reason: After accounting for rejections, these are the valid token positions from the original indices to sample from. """ device = torch.device(current_platform.device_type) expected_token_indices_to_sample = torch.tensor( [1, 5, 6], dtype=torch.int32, device=device ) num_speculative_tokens = 2 batch_spec = BatchSpec( seq_lens=[3, 3, 3], query_lens=[3, 3, 3], ) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device, ) # Needed for cu_num_draft_tokens, which is expected to be [3, 6, 9] expected_query_start_loc = torch.tensor( [0, 3, 6, 9], dtype=torch.int32, device=device ) spec_decode_metadata = SpecDecodeMetadata.make_dummy( draft_token_ids=[[0] * num_speculative_tokens] * 3, device=device, ) # num_rejected_tokens = [1, 0, 2] # num_draft_tokens = [2, 2, 2] # valid_sampled_tokens_count = num_draft_tokens + 1 - num_rejected_tokens valid_sampled_tokens_count = torch.tensor( [2, 3, 1], dtype=torch.int32, device=device ) proposer = _create_proposer("eagle", num_speculative_tokens) output_metadata, token_indices_to_sample, num_rejected_tokens_gpu = ( proposer.prepare_inputs_padded( common_attn_metadata, spec_decode_metadata, valid_sampled_tokens_count ) ) # Verify num_rejected_tokens_gpu is calculated correctly expected_num_rejected = torch.tensor([1, 0, 2], dtype=torch.int32, device=device) assert torch.equal(num_rejected_tokens_gpu, expected_num_rejected) assert output_metadata.max_query_len == 3 assert torch.equal(output_metadata.query_start_loc, expected_query_start_loc) assert torch.equal(token_indices_to_sample, expected_token_indices_to_sample) @pytest.mark.parametrize("method", ["eagle", "eagle3"]) @pytest.mark.parametrize("attn_backend", get_attn_backend_list_based_on_platform()) @pytest.mark.parametrize("pp_size", [1, 2]) @pytest.mark.parametrize("use_distinct_embed_tokens", [True, False]) @pytest.mark.parametrize("use_distinct_lm_head", [True, False]) @mock.patch("vllm.v1.spec_decode.eagle.get_pp_group") @mock.patch("vllm.v1.spec_decode.eagle.get_layers_from_vllm_config") @mock.patch("vllm.v1.spec_decode.eagle.get_model") def test_load_model( mock_get_model, mock_get_layers, mock_get_pp_group, method, attn_backend, pp_size, use_distinct_embed_tokens, use_distinct_lm_head, monkeypatch, ): if attn_backend == "TRITON_ATTN" and not current_platform.is_rocm(): pytest.skip( "TRITON_ATTN does not support " "multi-token eagle spec decode on current platform" ) if attn_backend == "ROCM_AITER_FA" and current_platform.is_rocm(): monkeypatch.setenv("VLLM_ROCM_USE_AITER", "1") # Setup draft model mock mock_model = mock.MagicMock() mock_model.model = mock.MagicMock() mock_model.has_own_embed_tokens = use_distinct_embed_tokens if use_distinct_embed_tokens: mock_model.model.embed_tokens = mock.MagicMock() mock_model.has_own_lm_head = use_distinct_lm_head if use_distinct_lm_head: mock_model.lm_head = mock.MagicMock() mock_get_model.return_value = mock_model # Setup mocks for attention layers target_attn_layers = { "target_attn_1": mock.MagicMock(), "target_attn_2": mock.MagicMock(), } target_indx_layers: dict[str, mock.MagicMock] = {} # Draft model has one extra attention layer compared to target model all_attn_layers = {**target_attn_layers, "draft_extra_attn": mock.MagicMock()} all_indx_layers: dict[str, mock.MagicMock] = {} # Make mock_get_layers return different values for each call mock_get_layers.side_effect = [ target_attn_layers, target_indx_layers, all_attn_layers, all_indx_layers, ] # Setup mock for pp group to return the appropriate value for world size mock_pp_group = mock.MagicMock() mock_pp_group.world_size = pp_size mock_get_pp_group.return_value = mock_pp_group # Set up the target model mock with a custom class so that # isinstance() checks match the expected type. class _TargetModelStub(LlamaForCausalLM): model: mock.MagicMock lm_head: mock.MagicMock target_model = mock.create_autospec(_TargetModelStub, instance=True) target_model.model = mock.MagicMock() target_model.lm_head = mock.MagicMock() target_model.model.embed_tokens = mock.MagicMock() from vllm.model_executor.models import SupportsMultiModal assert not isinstance(target_model, SupportsMultiModal) # Create proposer using the helper function proposer = _create_proposer( method, num_speculative_tokens=8, attention_backend=attn_backend ) # Call the method under test proposer.load_model(target_model) # Verify common interactions mock_get_model.assert_called_once() # Verify that the lm head is set correctly if use_distinct_lm_head: assert proposer.model.lm_head is not target_model.lm_head else: assert proposer.model.lm_head is target_model.lm_head # Verify that the embed tokens are set correctly # If pp_size is > 1, the embed tokens should be distinct if pp_size > 1 or use_distinct_embed_tokens: assert proposer.model.model.embed_tokens is not target_model.model.embed_tokens else: assert proposer.model.model.embed_tokens is target_model.model.embed_tokens @pytest.mark.parametrize("method", ["eagle", "eagle3"]) @pytest.mark.parametrize("attn_backend", get_attn_backend_list_based_on_platform()) @pytest.mark.parametrize("num_speculative_tokens", [1, 3, 8]) def test_propose(method, attn_backend, num_speculative_tokens, monkeypatch): if attn_backend == "TRITON_ATTN" and not current_platform.is_rocm(): pytest.skip( "TRITON_ATTN does not support " "multi-token eagle spec decode on current platform" ) if attn_backend == "TREE_ATTN": pytest.skip( "TREE_ATTN is tested separately in test_propose_tree" "because it requires special input mocking." ) if attn_backend == "ROCM_AITER_FA" and current_platform.is_rocm(): monkeypatch.setenv("VLLM_ROCM_USE_AITER", "1") # Use GPU device device = torch.device(current_platform.device_type) # Setup test parameters batch_size = 2 seq_len_1 = 5 seq_len_2 = 3 total_tokens = seq_len_1 + seq_len_2 vocab_size = 100 seq_lens = [seq_len_1, seq_len_2] # Create proposer first so we can use its actual hidden_size proposer = _create_proposer( "eagle", num_speculative_tokens, attention_backend=attn_backend ) # Get the hidden_size from the proposer to ensure consistency hidden_size = proposer.hidden_size # Helper to create deterministic logits that will produce specific tokens def create_deterministic_logits(token_ids): logits = torch.full((batch_size, vocab_size), -100.0, device=device) for i, token_id in enumerate(token_ids): logits[i, token_id] = 100.0 return logits # We mock a model that returns deterministic logits # Sequence 1: 42, 43, 44, ... # Sequence 2: 60, 61, 62, ... base_token_ids = [42, 60] # Skip loading the model and replace it with a mock directly # Create the mock model with deterministic outputs model_mock = mock.MagicMock() # Setup for model forward calls forward_returns = [] for i in range(num_speculative_tokens): if i == 0: # First call uses all tokens h_logits = torch.zeros(total_tokens, hidden_size, device=device) h_states = torch.zeros(total_tokens, hidden_size, device=device) else: # Subsequent calls use batch_size tokens h_logits = torch.zeros(batch_size, hidden_size, device=device) h_states = torch.zeros(batch_size, hidden_size, device=device) forward_returns.append((h_logits, h_states)) # For single token case, we only need the first item; # for multi-token, we need the sequence if num_speculative_tokens == 1: model_mock.return_value = forward_returns[0] else: model_mock.side_effect = forward_returns # Setup for compute_logits calls logits_returns = [] for i in range(num_speculative_tokens): # For each call, increment the base token IDs current_tokens = [base_id + i for base_id in base_token_ids] logits_returns.append(create_deterministic_logits(current_tokens)) if num_speculative_tokens == 1: model_mock.compute_logits.return_value = logits_returns[0] else: model_mock.compute_logits.side_effect = logits_returns # Assign the mock to the proposer proposer.model = model_mock # Assign draft attn_layer_names since load_model is not invoked proposer.attn_layer_names = ["layer.0"] # Create input tensors batch_spec = BatchSpec( seq_lens=seq_lens, query_lens=seq_lens, ) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device, ) target_token_ids = torch.randint(0, vocab_size, (total_tokens,), device=device) target_positions = torch.cat( [torch.arange(seq_len_1, device=device), torch.arange(seq_len_2, device=device)] ) target_hidden_states = torch.randn(total_tokens, hidden_size, device=device) next_token_ids = torch.randint( 0, vocab_size, (batch_size,), dtype=torch.int32, device=device ) sampling_metadata = mock.MagicMock() if attn_backend == "FLASH_ATTN": attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.FLASH_ATTN ) elif attn_backend == "TRITON_ATTN": attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.TRITON_ATTN ) elif attn_backend == "TREE_ATTN": attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.TREE_ATTN ) elif attn_backend == "ROCM_AITER_FA": attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.ROCM_AITER_FA ) else: raise ValueError(f"Unsupported attention backend: {attn_backend}") attn_metadata_builder = attn_metadata_builder_cls( kv_cache_spec=create_standard_kv_cache_spec(proposer.vllm_config), layer_names=proposer.attn_layer_names, vllm_config=proposer.vllm_config, device=device, ) # Mock runner for attention metadata building proposer.runner = mock.MagicMock() proposer.runner.attn_groups.append([mock.MagicMock()]) proposer.runner.attn_groups[0][ 0 ].get_metadata_builder.return_value = attn_metadata_builder proposer._get_attention_metadata_builder = mock.MagicMock( return_value=attn_metadata_builder ) result = proposer.propose( target_token_ids=target_token_ids, target_positions=target_positions, target_hidden_states=target_hidden_states, next_token_ids=next_token_ids, last_token_indices=None, common_attn_metadata=common_attn_metadata, sampling_metadata=sampling_metadata, ) assert result.shape == (batch_size, num_speculative_tokens) # Create expected tokens based on our token pattern if num_speculative_tokens == 1: # Example for num_speculative_tokens=1: # [[42], [60]] expected_tokens = torch.tensor( [[base_token_ids[0]], [base_token_ids[1]]], device=device ) else: # Example for num_speculative_tokens=3: # [[42, 43, 44], [60, 61, 62]] expected_tokens = torch.zeros( (batch_size, num_speculative_tokens), dtype=torch.int64, device=device ) for i in range(batch_size): for j in range(num_speculative_tokens): expected_tokens[i, j] = base_token_ids[i] + j # Verify all tokens match our expectations assert torch.equal(result, expected_tokens) @pytest.mark.parametrize( "spec_token_tree", [ [(0,)], # A single token [(0,), (0, 0), (0, 0, 0)], # Chain [(0,), (1,), (2,)], # Parallel [(0,), (1,), (2,), (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)], # Tree ], ) def test_propose_tree(spec_token_tree): # Get GPU device. device = torch.device(current_platform.device_type) # Setup test parameters. batch_size = 2 seq_len_1 = 5 seq_len_2 = 3 total_tokens = seq_len_1 + seq_len_2 vocab_size = 100 seq_lens = [seq_len_1, seq_len_2] num_speculative_tokens = len(spec_token_tree) # Create proposer first so we can use its actual hidden_size. proposer = _create_proposer( "eagle", num_speculative_tokens, speculative_token_tree=spec_token_tree, ) # Get the hidden_size from the proposer to ensure consistency. hidden_size = proposer.hidden_size # Helper to create deterministic logits that will produce specific tokens def create_deterministic_logits(token_ids, k: int): logits = torch.full((batch_size, vocab_size), -100.0, device=device) for i, token_id in enumerate(token_ids): # Assign decreasing values to the k, consecutive, tokens. for j in range(k): logits[i, token_id + j] = 100.0 - j return logits # Mock a model that returns deterministic logits. base_token_ids = torch.tensor([42, 60], dtype=torch.int64, device=device) # Skip loading the model and replace it with a mock that returns # deterministic outputs. model_mock = mock.MagicMock() # Mock the model forward calls. forward_returns = [ ( torch.zeros(total_tokens, hidden_size, device=device), torch.zeros(total_tokens, hidden_size, device=device), ) ] for cu_num_drafts in proposer.cu_drafts_per_level: h_logits = torch.zeros(batch_size * cu_num_drafts, hidden_size, device=device) h_states = torch.zeros(batch_size * cu_num_drafts, hidden_size, device=device) forward_returns.append((h_logits, h_states)) model_mock.side_effect = forward_returns # Mock the compute_logits calls. cu_num_drafts_tensor = torch.tensor( [0] + proposer.cu_drafts_per_level, dtype=torch.int32, device=device ) logits_returns = [] for level, num_children in enumerate(proposer.child_drafts_per_level): token_ids = base_token_ids + cu_num_drafts_tensor[level] level_num_drafts = cu_num_drafts_tensor[level + 1] - cu_num_drafts_tensor[level] level_logits = [] for i in range(level_num_drafts // num_children): level_logits.append( create_deterministic_logits(token_ids + i * num_children, num_children) ) logits_returns.append(torch.stack(level_logits, dim=1)) model_mock.compute_logits.side_effect = logits_returns # Assign the mock to the proposer proposer.model = model_mock # Assign draft attn_layer_names since load_model is not invoked proposer.attn_layer_names = ["layer.0"] # Get the tree attention metadata builder. attn_metadata_builder_cls, _ = try_get_attention_backend( AttentionBackendEnum.TREE_ATTN ) attn_metadata_builder = attn_metadata_builder_cls( kv_cache_spec=create_standard_kv_cache_spec(proposer.vllm_config), layer_names=proposer.attn_layer_names, vllm_config=proposer.vllm_config, device=device, ) # Mock runner for attention metadata building. proposer.runner = mock.MagicMock() proposer.runner.attn_groups.append([mock.MagicMock()]) proposer.runner.attn_groups[0][0].metadata_builders = [attn_metadata_builder] proposer.runner.attn_groups[0][ 0 ].get_metadata_builder.return_value = attn_metadata_builder proposer._get_attention_metadata_builder = mock.MagicMock( return_value=attn_metadata_builder ) # Setup inputs for the proposer. target_token_ids = torch.randint(0, vocab_size, (total_tokens,), device=device) target_positions = torch.cat( [torch.arange(seq_len_1, device=device), torch.arange(seq_len_2, device=device)] ) target_hidden_states = torch.randn(total_tokens, hidden_size, device=device) next_token_ids = torch.randint( 0, vocab_size, (batch_size,), dtype=torch.int32, device=device ) batch_spec = BatchSpec( seq_lens=seq_lens, query_lens=seq_lens, ) common_attn_metadata = create_common_attn_metadata( batch_spec, block_size=16, device=device, ) sampling_metadata = mock.MagicMock() # Propose draft tokens. result = proposer.propose( target_token_ids=target_token_ids, target_positions=target_positions, target_hidden_states=target_hidden_states, next_token_ids=next_token_ids, last_token_indices=None, common_attn_metadata=common_attn_metadata, sampling_metadata=sampling_metadata, ) assert result.shape == (batch_size, num_speculative_tokens) # The tokens are expected to be consecutive integers starting # from the base token IDs. expected_tokens = base_token_ids[:, None] + torch.arange( num_speculative_tokens, dtype=torch.int64, device=device ) # Verify that the draft tokens match our expectations. assert torch.equal(result, expected_tokens)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/spec_decode/test_max_len.py
tests/v1/spec_decode/test_max_len.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Test whether spec decoding handles the max model length properly.""" import pytest from tests.utils import get_attn_backend_list_based_on_platform from vllm import LLM, SamplingParams from vllm.platforms import current_platform from vllm.sampling_params import StructuredOutputsParams _PROMPTS = [ "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "Repeat the following sentence 10 times: Consistency is key to mastering any skill.", # noqa: E501 "Who won the Turing Award in 2018, and for what contribution? Describe in detail.", # noqa: E501 ] @pytest.mark.parametrize("num_speculative_tokens", [1, 3, 10]) def test_ngram_max_len(num_speculative_tokens: int): llm = LLM( model="facebook/opt-125m", max_model_len=100, enforce_eager=True, # For faster initialization. speculative_config={ "method": "ngram", "prompt_lookup_max": 5, "prompt_lookup_min": 3, "num_speculative_tokens": num_speculative_tokens, }, ) sampling_params = SamplingParams(max_tokens=100, ignore_eos=True) llm.generate(_PROMPTS, sampling_params) @pytest.mark.parametrize("num_speculative_tokens", [1, 3, 10]) @pytest.mark.parametrize("attn_backend", get_attn_backend_list_based_on_platform()) def test_eagle_max_len( monkeypatch: pytest.MonkeyPatch, num_speculative_tokens: int, attn_backend: str ): if attn_backend == "TRITON_ATTN" and not current_platform.is_rocm(): pytest.skip( "TRITON_ATTN does not support " "multi-token eagle spec decode on current platform" ) if attn_backend == "ROCM_AITER_FA" and current_platform.is_rocm(): monkeypatch.setenv("VLLM_ROCM_USE_AITER", "1") llm = LLM( model="meta-llama/Meta-Llama-3-8B-Instruct", enforce_eager=True, # For faster initialization. speculative_config={ "method": "eagle", "model": "yuhuili/EAGLE-LLaMA3-Instruct-8B", "num_speculative_tokens": num_speculative_tokens, "max_model_len": 80, }, max_model_len=200, attention_config={"backend": attn_backend}, ) sampling_params = SamplingParams(max_tokens=200, ignore_eos=True) outputs = llm.generate(_PROMPTS, sampling_params) for o in outputs: assert o.outputs[0].finish_reason == "length", ( "This test is only meaningful if the output is truncated due to max length" ) sampling_params = SamplingParams( max_tokens=200, structured_outputs=StructuredOutputsParams(regex="^" + "a b c d e " * 15 + "$"), ) output = llm.generate(_PROMPTS, sampling_params) for o in output: assert o.prompt_token_ids is not None assert ( len(o.prompt_token_ids) < 80 < len(o.prompt_token_ids) + len(o.outputs[0].token_ids) <= 200 ), ( "This test is only meaningful if the output " "is longer than the eagle max length" ) assert o.outputs[0].text == "a b c d e " * 15
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_async_scheduling.py
tests/v1/e2e/test_async_scheduling.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from itertools import repeat from typing import Any import pytest import torch._dynamo.config as dynamo_config from vllm import SamplingParams from vllm.logprobs import Logprob from vllm.platforms import current_platform from vllm.sampling_params import StructuredOutputsParams from vllm.v1.metrics.reader import Metric from ...conftest import VllmRunner from ...models.utils import check_outputs_equal MODEL = "Qwen/Qwen3-0.6B" MTP_MODEL = "meta-llama/Llama-3.2-1B-Instruct" first_prompt = ( "The following numbers of the sequence " + ", ".join(str(i) for i in range(10)) + " are:" ) example_prompts = [first_prompt, "In one word, the capital of France is "] + [ f"Tell me about the number {i}: " for i in range(32) ] default_params = dict( temperature=0.0, # greedy max_tokens=23, min_tokens=18, ) def test_without_spec_decoding( sample_json_schema, monkeypatch: pytest.MonkeyPatch, ): """Test consistency of combos of async scheduling, preemption, uni/multiproc executor, prefill chunking.""" struct_outputs = StructuredOutputsParams(json=sample_json_schema) test_sampling_params: list[dict[str, Any]] = [ dict(), # dict(min_tokens=20), dict(presence_penalty=-1.0), dict(bad_words=["the", " the"]), dict(logprobs=2), dict(logprobs=2, presence_penalty=-1.0), dict(structured_outputs=struct_outputs), dict( structured_outputs=struct_outputs, logprobs=2, presence_penalty=-1.0, ), ] # test_preemption, executor, async_scheduling, # spec_config, test_prefill_chunking test_configs = [ (False, "mp", False, None, False), (True, "mp", False, None, True), (False, "mp", True, None, False), (False, "uni", True, None, False), (True, "mp", True, None, False), (True, "uni", True, None, False), (False, "mp", True, None, True), (True, "mp", True, None, True), (True, "uni", True, None, True), ] if current_platform.is_rocm(): # On ROCm, Only test with structured_outputs (deterministic) # and skip chunk_prefill (more variable). test_configs = [ cfg for cfg in test_configs if not cfg[4] # skip chunk_prefill=True ] test_sampling_params = [ p for p in test_sampling_params if p.get("structured_outputs") is not None ] run_tests(monkeypatch, MODEL, test_configs, test_sampling_params) def test_with_spec_decoding(monkeypatch: pytest.MonkeyPatch): """Test consistency and acceptance rates with some different combos of preemption, executor, async scheduling, prefill chunking, spec decoding model length. """ spec_config = { "method": "eagle3", "num_speculative_tokens": 2, "model": "nm-testing/Llama3_2_1B_speculator.eagle3", } # Set small draft model len to force doesn't-fit-in-drafter case. spec_config_short = spec_config | {"max_model_len": 50} test_sampling_params = [ dict(), dict(logprobs=2), ] # test_preemption, executor, async_scheduling, # spec_config, test_prefill_chunking test_configs = [ (False, "mp", False, None, False), (False, "mp", False, spec_config, False), (True, "mp", False, spec_config, True), (True, "uni", False, spec_config_short, True), (False, "mp", True, spec_config, False), (True, "mp", True, spec_config, False), (False, "mp", True, spec_config_short, True), (True, "uni", True, spec_config, False), (True, "uni", True, spec_config_short, False), (True, "mp", True, spec_config, True), (True, "uni", True, spec_config_short, True), ] # On ROCm, use TRITON_ATTN + float32 for better numerical consistency run_tests( monkeypatch, MTP_MODEL, test_configs, test_sampling_params, is_testing_with_spec_decoding=True, ) @dynamo_config.patch(cache_size_limit=16) def run_tests( monkeypatch: pytest.MonkeyPatch, model: str, test_configs: list[tuple], test_sampling_params: list[dict[str, Any]], is_testing_with_spec_decoding: bool = False, ): """Test consistency of combos of async scheduling, preemption, uni/multiproc executor with spec decoding.""" # Determine attention config based on platform if current_platform.is_rocm(): if is_testing_with_spec_decoding: # Use TRITON_ATTN for spec decoding test for consistency attention_config = {"backend": "TRITON_ATTN"} else: attention_config = {"backend": "ROCM_ATTN"} else: attention_config = {"backend": "FLEX_ATTENTION"} with monkeypatch.context() as m: # lock matmul precision to full FP32 (IEEE) m.setenv("VLLM_FLOAT32_MATMUL_PRECISION", "ieee") # m.setenv("VLLM_BATCH_INVARIANT", "1") outputs: list[tuple[str, list, list]] = [] for n, ( test_preemption, executor, async_scheduling, spec_config, test_prefill_chunking, ) in enumerate(test_configs, 1): test_str = f"{n}/{len(test_configs)}" test_results = run_test( model, test_str, test_sampling_params, test_preemption, executor, async_scheduling, spec_config, test_prefill_chunking=test_prefill_chunking, is_testing_with_spec_decoding=is_testing_with_spec_decoding, attention_config=attention_config, ) outputs.append(test_results) baseline_config, baseline_tests, _ = outputs[0] _, _, baseline_acceptances = next( (o for o in outputs if o[2] is not None), (None, None, None) ) print(f"BASELINE: config=[{baseline_config}], accept_rates={baseline_acceptances}") failure = None for test_config, test_outputs, test_acceptance_rates in outputs[1:]: for (base_outs, base_logprobs), base_acceptance_rate, ( test_outs, test_logprobs, ), test_acceptance_rate, params in zip( baseline_tests, baseline_acceptances or repeat(None), test_outputs, test_acceptance_rates or repeat(None), test_sampling_params, ): try: check_outputs_equal( outputs_0_lst=base_outs, outputs_1_lst=test_outs, name_0=f"baseline=[{baseline_config}], params={params}", name_1=f"config=[{test_config}], params={params}", ) # On ROCm with TRITON_ATTN (spec decoding test), skip strict # logprobs comparison when logprobs are requested skip_logprobs_check = ( current_platform.is_rocm() and params.get("logprobs") and is_testing_with_spec_decoding ) if not skip_logprobs_check: assert _all_logprobs_match(base_logprobs, test_logprobs) if ( base_acceptance_rate is not None and test_acceptance_rate is not None ): if "spec_mml=None" in test_config: # Preemption causes more variance in acceptance rates if ( current_platform.is_rocm() and "preemption=True" in test_config ): tolerance = 0.10 else: tolerance = 0.05 assert ( test_acceptance_rate > base_acceptance_rate or test_acceptance_rate == pytest.approx(base_acceptance_rate, rel=tolerance) ) else: # Currently the reported acceptance rate is expected to be # lower when we sometimes skip drafting altogether. assert test_acceptance_rate > 0.1 print( f"PASSED: config=[{test_config}], params={params}" f" accept_rate={test_acceptance_rate}" ) except AssertionError as e: print( f"FAILED: config=[{test_config}], params={params}" f" accept_rate={test_acceptance_rate}" ) if failure is None: failure = e if failure is not None: raise failure def run_test( model: str, test_str: str, sampling_param_tests: list[dict[str, Any]], test_preemption: bool, executor: str, async_scheduling: bool, spec_config: dict[str, Any] | None, test_prefill_chunking: bool, is_testing_with_spec_decoding: bool = False, attention_config: dict[str, Any] | None = None, ): spec_decoding = spec_config is not None cache_arg: dict[str, Any] = ( # Force preemptions dict(num_gpu_blocks_override=32) if test_preemption else dict(gpu_memory_utilization=0.9) ) spec_mml = (spec_config or {}).get("max_model_len") test_config = ( f"executor={executor}, preemption={test_preemption}, " f"async_sched={async_scheduling}, " f"chunk_prefill={test_prefill_chunking}, " f"spec_decoding={spec_decoding}, spec_mml={spec_mml}" ) print("-" * 80) print(f"---- TESTING {test_str}: {test_config}") print("-" * 80) with VllmRunner( model, max_model_len=512, enable_chunked_prefill=test_prefill_chunking, # Force prefill chunking max_num_batched_tokens=48 if test_prefill_chunking else None, # enforce_eager=True, async_scheduling=async_scheduling, distributed_executor_backend=executor, dtype="float32", speculative_config=spec_config, disable_log_stats=False, attention_config=attention_config, **cache_arg, ) as vllm_model: results = [] acceptance_rates: list[float] | None = [] if spec_decoding else None for override_params in sampling_param_tests: metrics_before = vllm_model.llm.get_metrics() print(f"----------- RUNNING PARAMS: {override_params}") results.append( vllm_model.generate( example_prompts, sampling_params=SamplingParams(**default_params, **override_params), return_logprobs=True, ) ) metrics_after = vllm_model.llm.get_metrics() if acceptance_rates is not None: acceptance_rate = _get_acceptance_rate(metrics_before, metrics_after) acceptance_rates.append(acceptance_rate) print(f"ACCEPTANCE RATE {acceptance_rate}") if test_preemption: preemptions = _get_count( metrics_before, metrics_after, "vllm:num_preemptions" ) assert preemptions > 0, "preemption test had no preemptions" if len(results) > 1: # First check that the different parameter configs # actually result in different output. for (other_test_outs, other_test_logprobs), params in zip( results[1:], sampling_param_tests[1:] ): with pytest.raises(AssertionError): check_outputs_equal( outputs_0_lst=results[0][0], outputs_1_lst=other_test_outs, name_0=f"baseline params={params}", name_1=f"other params={params}", ) assert _all_logprobs_match(results[0][1], other_test_logprobs) return test_config, results, acceptance_rates def _all_logprobs_match(req_a, req_b) -> bool: return ( req_a == req_b or len(req_a) == len(req_b) and all( len(seq_a) == len(seq_b) and all(_logprobs_match(a, b) for a, b in zip(seq_a, seq_b)) for seq_a, seq_b in zip(req_a, req_b) ) ) def _logprobs_match(lps_a: dict[int, Logprob], lps_b: dict[int, Logprob]) -> bool: if current_platform.is_rocm(): # ROCm has higher numerical variance # due to use of float16. rel_tol, abs_tol = 5e-2, 1e-5 else: rel_tol, abs_tol = 1e-3, 1e-6 return ( len(lps_a) == len(lps_b) and lps_a.keys() == lps_b.keys() and all( a.decoded_token == b.decoded_token and a.rank == b.rank and a.logprob == pytest.approx(b.logprob, rel=rel_tol, abs=abs_tol) for a, b in ((lps_a[x], lps_b[x]) for x in lps_a) ) ) def _get_acceptance_rate(before: list[Metric], after: list[Metric]) -> float: draft = _get_count(before, after, "vllm:spec_decode_num_draft_tokens") accept = _get_count(before, after, "vllm:spec_decode_num_accepted_tokens") return accept / draft if draft > 0 else 0.0 def _get_count(before: list[Metric], after: list[Metric], name: str) -> int: before_val = next(m.value for m in before if m.name == name) after_val = next(m.value for m in after if m.name == name) return after_val - before_val
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_correctness_sliding_window.py
tests/v1/e2e/test_correctness_sliding_window.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass import pytest from vllm import LLM, SamplingParams from vllm.platforms import current_platform from ...utils import check_answers, prep_prompts @dataclass class TestConfig: sliding_window: int ln_range: tuple[int, int] model_config = { "bigcode/starcoder2-3b": TestConfig(4096, (800, 1100)), "google/gemma-3-1b-it": TestConfig(4096, (400, 800)), } @pytest.mark.parametrize( "model", [ "bigcode/starcoder2-3b", # sliding window only "google/gemma-3-1b-it", # sliding window + full attention ], ) @pytest.mark.parametrize("batch_size", [5]) @pytest.mark.parametrize("seed", [1]) @pytest.mark.parametrize("disable_hybrid_kv_cache_manager", [True, False]) def test_sliding_window_retrieval( model, batch_size, seed, disable_hybrid_kv_cache_manager ): """ The test does a bunch of assignments "x1 = 10\nx2 = 33\n..." and then asks for value of one of them (which is outside the sliding window). If we tell it upfront which we are going to be looking for, then it answers correctly (mostly). """ # NOTE: For ROCm, we have to enforce eager mode to use custom kernel # implementation of GELU with tanh approximation, as PyTorch's native # implementation is currently unstable with torch.compile and produces garbage. enforce_eager = current_platform.is_rocm() test_config = model_config[model] llm = LLM( model=model, disable_hybrid_kv_cache_manager=disable_hybrid_kv_cache_manager, enforce_eager=enforce_eager, ) sampling_params = SamplingParams(temperature=0.0, max_tokens=100) prompts, answer, indices = prep_prompts(batch_size, ln_range=test_config.ln_range) check_length(prompts, llm, test_config.sliding_window) # Fresh generation responses = llm.generate(prompts, sampling_params) check_answers( indices, answer, [response.outputs[0].text for response in responses], accept_rate=1.0, ) # Re-generate with the same prompts to test prefix caching responses = llm.generate(prompts, sampling_params) check_answers( indices, answer, [response.outputs[0].text for response in responses], accept_rate=1.0, ) def check_length(prompts: list[str], llm: LLM, sliding_window: int): """ Check if the prompt length is valid, i.e., longer than the sliding window size and shorter than the model's max length. Args: prompts: list of prompts llm: LLM object sliding_window: Sliding window size """ tokenizer = llm.get_tokenizer() max_model_len = llm.llm_engine.model_config.max_model_len assert any(len(tokenizer.encode(prompt)) > sliding_window for prompt in prompts), ( "Prompt is too short for test" ) assert all(len(tokenizer.encode(prompt)) <= max_model_len for prompt in prompts), ( "Prompt is too long for test" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_min_tokens.py
tests/v1/e2e/test_min_tokens.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Comprehensive end-to-end tests for `min_tokens` in the V1 engine. Addresses #21950: verify and add CI coverage. Covers: 1) Basic functionality 2) Stop strings with `min_tokens` (bug #21987; fix in PR #22014) 3) EOS behavior with `min_tokens` (potential logits-processor bug) 4) Edge cases (min_tokens == max_tokens, min_tokens == 0) 5) Multiple stop conditions """ import pytest from vllm import LLM, SamplingParams from vllm.outputs import RequestOutput # Test configuration TEST_MODEL = "facebook/opt-125m" # Small model for fast CI execution GREEDY = 0.0 # Deterministic generation for consistent testing class MinTokensTestCase: """Data class for min_tokens test scenarios""" def __init__( self, name: str, min_tokens: int, max_tokens: int, stop: str | list[str] | None = None, expected_min_len: int | None = None, expected_exact_len: int | None = None, ): self.name = name self.min_tokens = min_tokens self.max_tokens = max_tokens self.stop = stop self.expected_min_len = expected_min_len or min_tokens self.expected_exact_len = expected_exact_len def __str__(self): return ( f"{self.name}: min={self.min_tokens}, " f"max={self.max_tokens}, stop={self.stop}" ) # Test scenarios covering all critical cases MIN_TOKENS_TEST_CASES = [ # === BASIC FUNCTIONALITY (should work) === MinTokensTestCase( name="basic_min_tokens_no_stop", min_tokens=8, max_tokens=20, stop=None, expected_min_len=8, ), MinTokensTestCase( name="min_tokens_zero", min_tokens=0, max_tokens=10, stop=None, expected_min_len=0, ), MinTokensTestCase( name="min_equals_max_no_stop", min_tokens=15, max_tokens=15, stop=None, expected_exact_len=15, ), # === STOP STRINGS WITH MIN_TOKENS === # These tests expose the detokenizer bug where stop strings # bypass min_tokens # Using mathematically guaranteed approach with wide stop nets pytest.param( MinTokensTestCase( name="min_tokens_with_comprehensive_stops", min_tokens=5, max_tokens=20, stop=[ "a", "e", "i", "o", "u", "t", "n", "s", "r", "l", " ", ], expected_min_len=5, ), marks=pytest.mark.xfail( reason=( "Known bug #21987: stop strings bypass min_tokens (fixed by PR #22014)" ), strict=False, ), id="min_tokens_with_comprehensive_stops", ), pytest.param( MinTokensTestCase( name="min_tokens_with_simple_char_stop", min_tokens=3, max_tokens=15, stop=["e", "a", " "], expected_min_len=3, ), marks=pytest.mark.xfail( reason=( "Known bug #21987: stop strings bypass min_tokens (fixed by PR #22014)" ), strict=False, ), id="min_tokens_with_simple_char_stop", ), # === EOS TOKEN WITH MIN_TOKENS (potential LogitsProcessor bug) === # These test the MinTokensLogitsProcessor handling of EOS tokens pytest.param( MinTokensTestCase( name="min_equals_max_eos_only", min_tokens=20, max_tokens=20, stop=None, # Relies on default EOS token behavior expected_exact_len=20, ), marks=pytest.mark.xfail( reason=("Potential logits-processor bug: EOS tokens may bypass min_tokens"), strict=False, ), id="min_equals_max_eos_only", ), # === EDGE CASES === MinTokensTestCase( name="large_min_tokens", min_tokens=50, max_tokens=60, stop=None, expected_min_len=50, ), MinTokensTestCase( name="min_tokens_with_empty_stop_list", min_tokens=5, max_tokens=15, stop=[], # Empty stop list expected_min_len=5, ), ] @pytest.fixture(scope="module") def llm_v1(): """Create V1 LLM instance for testing""" llm = LLM( model=TEST_MODEL, tensor_parallel_size=1, max_model_len=1024, # Small context for fast testing enforce_eager=True, # Avoid graph compilation overhead ) return llm def get_token_count(output: RequestOutput) -> int: """Extract token count from LLM output""" if not output.outputs: return 0 return len(output.outputs[0].token_ids) def assert_min_tokens_satisfied( output: RequestOutput, test_case: MinTokensTestCase ) -> None: """Assert that min_tokens requirement is satisfied""" token_count = get_token_count(output) stop_reason = output.outputs[0].stop_reason if output.outputs else "no output" if test_case.expected_exact_len is not None: # Exact length requirement assert token_count == test_case.expected_exact_len, ( f"Expected exactly {test_case.expected_exact_len} tokens, " f"got {token_count} tokens. " f"Stop reason: {stop_reason}" ) else: # Minimum length requirement assert token_count >= (test_case.expected_min_len or 0), ( f"Expected at least {test_case.expected_min_len} tokens, " f"got {token_count} tokens. " f"Stop reason: {stop_reason}" ) @pytest.mark.parametrize( "test_case", MIN_TOKENS_TEST_CASES, ids=lambda tc: tc.name, ) def test_min_tokens_comprehensive(llm_v1: LLM, test_case: MinTokensTestCase): """ Comprehensive test for min_tokens functionality in V1 engine. This test covers all critical scenarios for min_tokens: - Basic functionality (should work) - Stop strings with min_tokens (known bug) - EOS tokens with min_tokens (potential bug) - Edge cases Args: llm_v1: V1 LLM instance test_case: Test scenario parameters """ # Known failing cases are handled via param-level xfail marks above. # Create sampling parameters sampling_params = SamplingParams( min_tokens=test_case.min_tokens, max_tokens=test_case.max_tokens, stop=test_case.stop, temperature=GREEDY, include_stop_str_in_output=True, # Include stop strings for debugging ) # Use simple prompt. Comprehensive stop lists should catch any generation prompt = "Hello" # Generate output outputs = llm_v1.generate([prompt], sampling_params) assert len(outputs) == 1, "Expected exactly one output" output = outputs[0] # Debug information token_count = get_token_count(output) generated_text = output.outputs[0].text if output.outputs else "" stop_reason = output.outputs[0].stop_reason if output.outputs else "unknown" print(f"\nTest: {test_case.name}") print(f"Generated {token_count} tokens") print(f"Stop reason: {stop_reason}") print(f"Generated text: {repr(generated_text)}") print(f"Expected min: {test_case.expected_min_len}") if test_case.expected_exact_len: print(f"Expected exact: {test_case.expected_exact_len}") # Validate min_tokens requirement assert_min_tokens_satisfied(output, test_case) def test_min_tokens_basic_functionality(llm_v1: LLM): """ Test basic min_tokens functionality without stop conditions. This is a baseline test that should always pass and validates that min_tokens works correctly in the simple case. """ sampling_params = SamplingParams(min_tokens=10, max_tokens=20, temperature=GREEDY) prompt = "Once upon a time" outputs = llm_v1.generate([prompt], sampling_params) assert len(outputs) == 1 token_count = get_token_count(outputs[0]) assert token_count >= 10, f"Expected at least 10 tokens, got {token_count}" assert token_count <= 20, f"Expected at most 20 tokens, got {token_count}" @pytest.mark.xfail( reason=("Known bug #21987: stop strings bypass min_tokens (fixed by PR #22014)"), strict=False, ) def test_min_tokens_stop_strings_bug(llm_v1: LLM): """ Test the specific bug where stop strings bypass min_tokens. This test specifically reproduces the bug Calvin is fixing in PR #22014. It should fail until that fix is merged. Strategy: Use guaranteed stop characters that will appear in any generated text. """ # If the bug is fixed upstream, this test will XPASS sampling_params = SamplingParams( min_tokens=15, max_tokens=50, # Common letter; likely appears early stop=["e"], temperature=GREEDY, include_stop_str_in_output=True, ) # Simple prompt that will generate text containing "e" prompt = "The quick brown fox" outputs = llm_v1.generate([prompt], sampling_params) assert len(outputs) == 1 token_count = get_token_count(outputs[0]) generated_text = outputs[0].outputs[0].text if outputs[0].outputs else "" # Debug info to understand what happened print(f"Generated text: {repr(generated_text)}") print(f"Token count: {token_count}") print(f"Contains 'e': {'e' in generated_text}") # This assertion should fail due to the bug - if stop string is found early, # the model should still continue generating until min_tokens is reached stop_reason = ( outputs[0].outputs[0].stop_reason if outputs[0].outputs else "no output" ) assert token_count >= 15, ( "Bug confirmed: " f"{token_count} tokens < min_tokens=15. " f"Reason: {stop_reason}. " f"Text: {repr(generated_text)}" ) @pytest.mark.xfail( reason=("Known bug #21987: stop strings bypass min_tokens (fixed by PR #22014)"), strict=False, ) def test_min_tokens_stop_strings_guaranteed_early_trigger(llm_v1: LLM): """ Guaranteed test for stop strings bypassing min_tokens bug. Strategy: Use very low temperature and multiple common stop strings to virtually guarantee early detection, combined with long min_tokens to ensure the bug is exposed regardless of model behavior. """ # If the bug is fixed upstream, this test will XPASS sampling_params = SamplingParams( min_tokens=50, # Set high min_tokens to ensure bug detection max_tokens=200, # Use multiple very common patterns - at least one will appear stop=["e", "a", "i", "o", "u", " ", "t", "n", "s", "r"], temperature=GREEDY, include_stop_str_in_output=True, ) # Simple prompt that will generate some text prompt = "The cat" outputs = llm_v1.generate([prompt], sampling_params) assert len(outputs) == 1 token_count = get_token_count(outputs[0]) generated_text = outputs[0].outputs[0].text if outputs[0].outputs else "" stop_reason = outputs[0].outputs[0].stop_reason if outputs[0].outputs else "unknown" print(f"Generated text: {repr(generated_text)}") print(f"Token count: {token_count}") print(f"Stop reason: {stop_reason}") # With the bug, this will fail because ANY of the common characters # will trigger early termination before min_tokens=50 is reached # It's virtually impossible to generate 50 tokens without hitting # at least one of: e, a, i, o, u, space, t, n, s, r finish_reason = ( outputs[0].outputs[0].finish_reason if outputs[0].outputs else "unknown" ) print(f"Finish reason: {finish_reason}") if finish_reason == "stop": assert token_count >= 50, ( "Bug confirmed: " f"{token_count} tokens < min_tokens=50. " f"Reason: {finish_reason}. " f"Text: {repr(generated_text)}" ) @pytest.mark.xfail( reason=("Potential logits-processor bug: EOS tokens may bypass min_tokens"), strict=False, ) def test_min_tokens_eos_behavior(llm_v1: LLM): """ Verify EOS handling with and without min_tokens. - Without min_tokens: expect early EOS -> finish_reason == "stop", stop_reason is None, and generated tokens < max_tokens (25). - With min_tokens: EOS should be blocked until min_tokens is reached (finish_reason == "length"); verify that eos_token_id does not appear in generated token_ids. """ # tokenizer + eos id tokenizer = llm_v1.get_tokenizer() eos_token_id = tokenizer.eos_token_id prompt = "Give a file extension." max_toks = 32 # Case 1: WITHOUT min_tokens sp_no_min = SamplingParams( max_tokens=max_toks, temperature=GREEDY, ) out_no_min = llm_v1.generate([prompt], sp_no_min) assert len(out_no_min) == 1 choice_no_min = out_no_min[0].outputs[0] ids_no_min = choice_no_min.token_ids or [] finish_no_min = choice_no_min.finish_reason stop_no_min = choice_no_min.stop_reason print( "[no-min] tokens=", len(ids_no_min), " finish=", finish_no_min, " stop_reason=", stop_no_min, ) assert finish_no_min == "stop", ( f"Expected finish_reason 'stop' without min_tokens, got {finish_no_min}" ) assert stop_no_min is None, ( "For EOS-based stop (no user stop strings), stop_reason should be None." ) assert len(ids_no_min) < max_toks, ( f"Expected early EOS with < {max_toks} tokens, got {len(ids_no_min)}" ) # Case 2: WITH min_tokens sp_with_min = SamplingParams( min_tokens=max_toks, max_tokens=max_toks, temperature=GREEDY, ) out_with_min = llm_v1.generate([prompt], sp_with_min) assert len(out_with_min) == 1 choice_with_min = out_with_min[0].outputs[0] ids_with_min = choice_with_min.token_ids or [] finish_with_min = choice_with_min.finish_reason stop_with_min = choice_with_min.stop_reason print( "[with-min] tokens=", len(ids_with_min), " finish=", finish_with_min, " stop_reason=", stop_with_min, ) # Exact length reached; EOS should have been blocked assert len(ids_with_min) == max_toks, ( f"Expected exactly {max_toks} tokens with min_tokens; got {len(ids_with_min)}" ) assert finish_with_min == "length", ( f"Expected finish_reason 'length'; got {finish_with_min}" ) assert eos_token_id not in ids_with_min, ( "EOS token id should not appear when min_tokens prevents early EOS." ) def test_min_tokens_validation(): """ Test that SamplingParams correctly validates min_tokens parameters. This tests the parameter validation logic in SamplingParams. """ # Valid cases SamplingParams(min_tokens=0, max_tokens=10) SamplingParams(min_tokens=5, max_tokens=10) SamplingParams(min_tokens=10, max_tokens=10) # Invalid cases with pytest.raises( ValueError, match="min_tokens must be greater than or equal to 0", ): SamplingParams(min_tokens=-1, max_tokens=10) with pytest.raises( ValueError, match="min_tokens must be less than or equal to max_tokens", ): SamplingParams(min_tokens=15, max_tokens=10) if __name__ == "__main__": """ Run tests locally for development. Usage: cd vllm/ python -m pytest tests/v1/e2e/test_min_tokens.py -v """ pytest.main([__file__, "-v"])
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_async_spec_decode.py
tests/v1/e2e/test_async_spec_decode.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Test that verifies no implicit GPU-CPU synchronization occurs during speculative decoding generation under expected conditions. """ import multiprocessing import sys import traceback import pytest import torch @pytest.fixture def sync_tracker(): """ Fixture that patches CommonAttentionMetadata.seq_lens_cpu to detect lazy init syncs. Prints stack traces immediately when syncs occur. """ from vllm.v1.attention.backends.utils import CommonAttentionMetadata # Shared counter for cross-process communication (inherited by fork) sync_count = multiprocessing.Value("i", 0) # Save original property original_prop = CommonAttentionMetadata.seq_lens_cpu original_fget = original_prop.fget # Create tracking wrapper def tracking_seq_lens_cpu(self): if self._seq_lens_cpu is None: # Increment counter with sync_count.get_lock(): sync_count.value += 1 count = sync_count.value # Print stack trace immediately (shows in subprocess output) print(f"\n{'=' * 60}", file=sys.stderr) print(f"SYNC #{count}: seq_lens_cpu lazy init triggered!", file=sys.stderr) print(f"{'=' * 60}", file=sys.stderr) traceback.print_stack(file=sys.stderr) print(f"{'=' * 60}\n", file=sys.stderr) sys.stderr.flush() return original_fget(self) # Apply patch CommonAttentionMetadata.seq_lens_cpu = property(tracking_seq_lens_cpu) class SyncTracker: @property def count(self) -> int: return sync_count.value def assert_no_sync(self, msg: str = ""): count = sync_count.value assert count == 0, ( f"Unexpected GPU-CPU sync: seq_lens_cpu lazy init triggered " f"{count} times. See stack traces above. {msg}" ) yield SyncTracker() # Restore original property CommonAttentionMetadata.seq_lens_cpu = original_prop torch._dynamo.reset() # Test configurations: (model, spec_model, method, num_spec_tokens, backend_env) SPEC_DECODE_CONFIGS = [ pytest.param( "meta-llama/Llama-3.2-1B-Instruct", "nm-testing/Llama3_2_1B_speculator.eagle3", "eagle3", 2, id="eagle3-llama", ), pytest.param( "eagle618/deepseek-v3-random", "eagle618/eagle-deepseek-v3-random", "eagle", 2, id="eagle-mla-deepseek", ), ] @pytest.mark.parametrize( "model,spec_model,method,num_spec_tokens", SPEC_DECODE_CONFIGS, ) def test_no_sync_with_spec_decode( sync_tracker, model: str, spec_model: str, method: str, num_spec_tokens: int, ): """ Test that no implicit GPU-CPU sync occurs during speculative decoding generation. """ # Import vLLM AFTER sync_tracker fixture has applied the patch from vllm import LLM, SamplingParams from vllm.distributed import cleanup_dist_env_and_memory llm = LLM( model=model, max_model_len=256, speculative_config={ "method": method, "num_speculative_tokens": num_spec_tokens, "model": spec_model, }, enforce_eager=True, async_scheduling=True, ) outputs = llm.generate( ["Hello, my name is"], SamplingParams(temperature=0, max_tokens=10), ) assert len(outputs) == 1 assert len(outputs[0].outputs[0].text) > 0 del llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() sync_tracker.assert_no_sync()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_pooling_chunked_prefill.py
tests/v1/e2e/test_pooling_chunked_prefill.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch.nn as nn from vllm.platforms import current_platform prompt = """ Generals gathered in their masses Just like witches at black masses Evil minds that plot destruction Sorcerer of death's construction In the fields, the bodies burning As the war machine keeps turning Death and hatred to mankind Poisoning their brainwashed minds Oh, Lord, yeah Politicians hide themselves away They only started the war Why should they go out to fight? They leave that all to the poor, yeah Time will tell on their power minds Making war just for fun Treating people just like pawns in chess Wait till their judgment day comes, yeah Now, in darkness, world stops turning Ashes where their bodies burning No more war pigs have the power Hand of God has struck the hour Day of Judgment, God is calling On their knees, the war pigs crawling Begging mercies for their sins Satan, laughing, spreads his wings Oh, Lord, yeah """ class WrapperPooler(nn.Module): def __init__(self, pooler): super().__init__() self.pooler = pooler self.chunks = [] def get_pooling_updates(self, task): return self.pooler.get_pooling_updates(task) def forward( self, hidden_states, pooling_metadata, ): self.chunks.append(hidden_states.shape[0]) return self.pooler(hidden_states, pooling_metadata) def inject_pooler(self): model = self.get_model() wrapper = WrapperPooler(model.pooler) model.pooler = wrapper def retrieve_chunks(self): model = self.get_model() chunks = model.pooler.chunks model.pooler.chunks = [] return chunks @pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") def test_pooling_chunked_prefill(vllm_runner, monkeypatch): """Test chunked prefill for pooling models with LastPool.""" with monkeypatch.context() as m: m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") model_id = "Qwen/Qwen3-Embedding-0.6B" chunk_size = 10 # Set chunking parameters to force chunked prefill # Note: Chunked prefill is automatically handled by vLLM # internally based on the model size and prompt with vllm_runner( model_id, runner="pooling", long_prefill_token_threshold=chunk_size, tensor_parallel_size=1, enforce_eager=True, enable_chunked_prefill=True, ) as llm: llm.get_llm().llm_engine.collective_rpc(inject_pooler) tokenizer = llm.get_llm().get_tokenizer() tokens = tokenizer(prompt)["input_ids"] prompt_len = len(tokens) full_chunks, last_chunk = divmod(prompt_len, chunk_size) expected_chunks = [chunk_size] * full_chunks if last_chunk: expected_chunks.append(last_chunk) llm.embed([prompt]) chunks = llm.get_llm().llm_engine.collective_rpc(retrieve_chunks)[0] # Check that PoolerWrapper was called and chunks were received assert len(chunks) > 1 assert chunks == expected_chunks # Disable chunked prefill with vllm_runner( model_id, runner="pooling", tensor_parallel_size=1, enforce_eager=True, ) as llm: llm.get_llm().llm_engine.collective_rpc(inject_pooler) llm.embed([prompt]) chunks = llm.get_llm().llm_engine.collective_rpc(retrieve_chunks)[0] # Check that PoolerWrapper was called and no chunks were received assert len(chunks) == 1 assert chunks[0] == prompt_len @pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") def test_pooling_prefix_cache(vllm_runner, monkeypatch): """Test chunked prefill for pooling models with LastPool.""" verses = prompt.split("\n\n") with monkeypatch.context() as m: m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") model_id = "Qwen/Qwen3-Embedding-0.6B" with vllm_runner( model_id, runner="pooling", enable_prefix_caching=True, tensor_parallel_size=1, enforce_eager=True, ) as llm: llm.get_llm().llm_engine.collective_rpc(inject_pooler) tokenizer = llm.get_llm().get_tokenizer() prompt1 = "\n\n".join([verses[0], verses[1]]) prompt2 = "\n\n".join([verses[0], verses[2]]) tokens1 = tokenizer(prompt1)["input_ids"] tokens2 = tokenizer(prompt2)["input_ids"] prompt1_len = len(tokens1) prompt2_len = len(tokens2) llm.embed([prompt1]) chunks = llm.get_llm().llm_engine.collective_rpc(retrieve_chunks)[0] assert len(chunks) == 1 assert chunks[0] == prompt1_len llm.embed([prompt2]) chunks = llm.get_llm().llm_engine.collective_rpc(retrieve_chunks)[0] assert len(chunks) == 1 assert chunks[0] <= prompt1_len assert chunks[0] < prompt2_len cache_config = llm.get_llm().llm_engine.cache_config print(f"{cache_config=}") # Prefixes are cached in blocks assert (prompt2_len - chunks[0]) % cache_config.block_size == 0
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_kv_sharing_fast_prefill.py
tests/v1/e2e/test_kv_sharing_fast_prefill.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import pytest from vllm import LLM, SamplingParams from vllm.config import CompilationConfig, CompilationMode from vllm.platforms import current_platform from ...utils import check_answers, fork_new_process_for_each_test, prep_prompts # global seed SEED = 42 @pytest.fixture def test_prompts(): """ Adapted from tests/v1/e2e/test_spec_decode.py """ prompt_types = ["repeat", "sentence"] # Setting higher num prompts increases the chance of numerics mismatch # due to matrix multiplication numerics depending on batch dimension num_prompts = 10 prompts = [] random.seed(0) random_prompt_type_choices = random.choices(prompt_types, k=num_prompts) for kind in random_prompt_type_choices: word_choices = ["test", "temp", "hello", "where"] word = random.choice(word_choices) if kind == "repeat": prompt = f"""please repeat the word '{word}' 10 times.""" elif kind == "sentence": prompt = f"""please give a ten-word sentence that uses the word {word} at least once.""" else: raise ValueError(f"Unknown prompt type: {kind}") prompts.append(prompt) return prompts use_fork_for_test = ( fork_new_process_for_each_test if not current_platform.is_rocm() else lambda x: x ) @use_fork_for_test @pytest.mark.parametrize("kv_sharing_fast_prefill", [False, True]) @pytest.mark.parametrize("enforce_eager", [True, False]) def test_kv_sharing_fast_prefill( monkeypatch: pytest.MonkeyPatch, kv_sharing_fast_prefill: bool, enforce_eager: bool, ): if not enforce_eager and current_platform.is_rocm(): # Relevant context: https://github.com/vllm-project/vllm/pull/29244 pytest.skip( "ROCm: torch.compile produces incorrect output for gemma-3n's GELU " "with tanh approximation. Use enforce_eager=True instead." ) sampling_params = SamplingParams(temperature=0.0, max_tokens=100) compilation_config = CompilationConfig( # This allows vLLM compilation backend to handle allocating and # managing buffers for cudagraph cudagraph_copy_inputs=True, mode=CompilationMode.VLLM_COMPILE if not enforce_eager else CompilationMode.NONE, ) batch_size = 10 with monkeypatch.context() as m: # Make scheduling deterministic for reproducibility if current_platform.is_rocm(): # Use spawn to prevent cuda re-initialization error m.setenv("VLLM_WORKER_MULTIPROC_METHOD", "spawn") else: m.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") prompts, answer, indices = prep_prompts(batch_size) llm = LLM( model="google/gemma-3n-E2B-it", enforce_eager=enforce_eager, compilation_config=compilation_config, seed=SEED, kv_sharing_fast_prefill=kv_sharing_fast_prefill, ) responses = llm.generate(prompts, sampling_params) check_answers( indices, answer, [response.outputs[0].text for response in responses], accept_rate=1.0, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_lora_with_spec_decode.py
tests/v1/e2e/test_lora_with_spec_decode.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ This script contains: 1. test lora with speculative decoding for batch inference """ import random import numpy as np import pytest import torch from vllm import LLM, SamplingParams from vllm.distributed import cleanup_dist_env_and_memory from vllm.lora.request import LoRARequest from vllm.platforms import current_platform LORA_TEST_PROMPT_MAP: dict[str, str] = {} LORA_TEST_PROMPT_MAP["premjatin/qwen-linear-algebra-coder"] = """ ### INSTRUCTION: You are an AI assistant that generates Python code to solve linear algebra problems. ### PROBLEM: Find the eigenvalues and eigenvectors of the following 3x3 matrix: [[3, 2, 0], [2, 3, 0], [0, 0, 2]] ### OUTPUT FORMAT (STRICT): Numbers should be represented as integers only. ### PYTHON SOLUTION: """ SEED = 42 @pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") @pytest.mark.parametrize( "model_setup", [ ( "eagle3", "Qwen/Qwen3-1.7B", "AngelSlim/Qwen3-1.7B_eagle3", "premjatin/qwen-linear-algebra-coder", 1, ) ], ) def test_batch_inference_correctness( monkeypatch: pytest.MonkeyPatch, model_setup: tuple[str, str, str, str, int], ): """ Compare the outputs of a LLM with only Lora and a LLM with both SD and Lora. Should be the same and no failure when doing batch inference. model_setup: (method, model_name, spec_model_name, lora_path, tp_size) """ with monkeypatch.context() as m: # Disable randomness m.setenv("CUBLAS_WORKSPACE_CONFIG", ":4096:8") torch.manual_seed(SEED) np.random.seed(SEED) random.seed(SEED) torch.cuda.manual_seed_all(SEED) torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True method, model_name, spec_model_name, lora_path, tp_size = model_setup # without speculative decoding ref_llm = LLM( model=model_name, trust_remote_code=True, tensor_parallel_size=tp_size, max_model_len=2048, max_num_seqs=4, enable_lora=True, max_loras=1, max_cpu_loras=1, max_lora_rank=16, ) prompts = [LORA_TEST_PROMPT_MAP[lora_path]] * 100 lora_request = LoRARequest("adapter", 1, lora_path) sampling_params = SamplingParams( temperature=0.0, top_p=1.0, top_k=-1, seed=SEED, max_tokens=128 ) ref_outputs = ref_llm.generate( prompts, sampling_params, lora_request=lora_request ) del ref_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() lora_spec_llm = LLM( model=model_name, trust_remote_code=True, tensor_parallel_size=tp_size, speculative_config={ "method": method, "model": spec_model_name, "num_speculative_tokens": 3, "max_model_len": 2048, }, max_model_len=2048, max_num_seqs=4, enable_lora=True, max_loras=1, max_cpu_loras=1, max_lora_rank=16, ) lora_spec_outputs = lora_spec_llm.generate( prompts, sampling_params, lora_request=lora_request ) matches = 0 misses = 0 for ref_output, spec_output in zip(ref_outputs, lora_spec_outputs): if ref_output.outputs[0].text == spec_output.outputs[0].text: matches += 1 else: misses += 1 print(f"ref_output: {ref_output.outputs[0].text}") print(f"spec_output: {spec_output.outputs[0].text}") # Heuristic: expect at least 90% of the prompts to match exactly # Upon failure, inspect the outputs to check for inaccuracy. print(f"match ratio: {matches}/{len(ref_outputs)}") assert matches > int(0.90 * len(ref_outputs)) del lora_spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_cascade_attention.py
tests/v1/e2e/test_cascade_attention.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from vllm import LLM, SamplingParams from ...utils import create_new_process_for_each_test @create_new_process_for_each_test() @pytest.mark.parametrize("attn_backend", ["FLASH_ATTN", "FLASHINFER"]) def test_cascade_attention(example_system_message, attn_backend): prompt = "\n<User>: Implement fibonacci sequence in Python.\n<Claude>:" if attn_backend == "FLASHINFER": pytest.skip( "This test is failing with FlashInfer backend and " "needs investigation. See issue #25679." ) llm = LLM( model="Qwen/Qwen2-1.5B-Instruct", attention_config={"backend": attn_backend} ) sampling_params = SamplingParams(temperature=0.0, max_tokens=100) # No cascade attention. single_prompt = [example_system_message + prompt] responses = llm.generate(single_prompt, sampling_params) ref_output = responses[0].outputs[0].text # (Probably) Use cascade attention. prompts = [example_system_message + prompt] * 64 responses = llm.generate(prompts, sampling_params) for response in responses: assert response.outputs[0].text == ref_output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/__init__.py
tests/v1/e2e/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_context_length.py
tests/v1/e2e/test_context_length.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Tests for vLLM `vllm/v1/engine/processor.Processor._validate_model_input()` handling of maximum context length for decoder models. This test ensures: - A prompt that is one token shorter than the model's maximum context length can be processed successfully when requesting one additional token. - A prompt that reaches the model's maximum context length throws a `ValueError` when requesting at least one additional token. """ import pytest from tests.conftest import VllmRunner from tests.utils import create_new_process_for_each_test @create_new_process_for_each_test() @pytest.mark.parametrize("model, max_model_len", [("JackFram/llama-160m", 2048)]) @pytest.mark.parametrize( "prompt_len, max_tokens", [ (2047, 1), # prompt_len = max_model_len - 1 -> allowed (2048, 1), # prompt_len = max_model_len -> not allowed ], ) def test_decoder_max_context_length_validation( model: str, max_model_len: int, vllm_runner: type[VllmRunner], prompt_len: int, max_tokens: int, ) -> None: """Check vLLM decoder model input validation for edge cases where the prompt length is (almost) equal to the max model length.""" prompt_ids = [[43] * prompt_len] with vllm_runner( model_name=model, tokenizer_name=model, max_model_len=max_model_len, max_num_seqs=1, tensor_parallel_size=1, ) as vllm_model: if prompt_len + max_tokens <= max_model_len: # Should succeed as constraints are met vllm_model.generate_greedy(prompt_ids, max_tokens) else: # Should raise the ValueError defined in # vllm/v1/engine/processor.Processor_validate_model_input() expected_msg = ( f"The decoder prompt (length {prompt_len}) plus the number of " f"requested output tokens (at least 1) is longer than " f"the maximum model length of {max_model_len}. " "Make sure that `max_model_len` is no smaller than the number of " "text tokens (prompt + requested output tokens)." ) with pytest.raises(ValueError) as excinfo: vllm_model.generate_greedy(prompt_ids, max_tokens) assert expected_msg in str(excinfo.value)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/e2e/test_spec_decode.py
tests/v1/e2e/test_spec_decode.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random from typing import Any import pytest import torch from tests.utils import get_attn_backend_list_based_on_platform, large_gpu_mark from vllm import LLM, SamplingParams from vllm.assets.base import VLLM_S3_BUCKET_URL from vllm.assets.image import VLM_IMAGES_DIR from vllm.distributed import cleanup_dist_env_and_memory from vllm.platforms import current_platform MTP_SIMILARITY_RATE = 0.8 def _skip_if_insufficient_gpus_for_tp(tp_size: int): """Skip test if available GPUs < tp_size on ROCm.""" if current_platform.is_rocm(): available_gpus = torch.cuda.device_count() if available_gpus < tp_size: pytest.skip( f"Test requires {tp_size} GPUs, but only {available_gpus} available" ) def get_test_prompts(mm_enabled: bool): prompt_types = ["repeat", "sentence"] if mm_enabled: prompt_types.append("mm") num_prompts = 100 prompts = [] random.seed(0) random_prompt_type_choices = random.choices(prompt_types, k=num_prompts) print(f"Prompt types: {random_prompt_type_choices}") # Generate a mixed batch of prompts, some of which can be easily # predicted by n-gram matching and some which likely cannot. for kind in random_prompt_type_choices: word_choices = ["test", "temp", "hello", "where"] word = random.choice(word_choices) prompt: str | list[dict[str, Any]] = "" if kind == "repeat": prompt = f""" please repeat the word '{word}' 10 times. give no other output than the word at least ten times in a row, in lowercase with spaces between each word and without quotes. """ elif kind == "sentence": prompt = f""" please give a ten-word sentence that uses the word {word} at least once. give no other output than that simple sentence without quotes. """ elif kind == "mm": placeholders = [ { "type": "image_url", "image_url": { "url": f"{VLLM_S3_BUCKET_URL}/{VLM_IMAGES_DIR}/stop_sign.jpg" }, } ] prompt = [ *placeholders, {"type": "text", "text": "The meaning of the image is"}, ] else: raise ValueError(f"Unknown prompt type: {kind}") prompts.append([{"role": "user", "content": prompt}]) return prompts @pytest.fixture def sampling_config(): return SamplingParams(temperature=0, max_tokens=10, ignore_eos=False) @pytest.fixture def model_name(): return "meta-llama/Llama-3.1-8B-Instruct" @pytest.fixture(autouse=True) def reset_torch_dynamo(): """Reset torch dynamo cache before each test""" yield # Cleanup after test torch._dynamo.reset() @pytest.mark.parametrize( "speculative_config", [ { "method": "ngram", "prompt_lookup_max": 5, "prompt_lookup_min": 3, "num_speculative_tokens": 3, }, { "method": "suffix", "suffix_decoding_max_spec_factor": 2.0, }, ], ) def test_ngram_and_suffix_correctness( speculative_config: dict, monkeypatch: pytest.MonkeyPatch, sampling_config: SamplingParams, model_name: str, ): """ Compare the outputs of an original LLM and a speculative LLM should be the same when using ngram speculative decoding. """ test_prompts = get_test_prompts(mm_enabled=False) ref_llm = LLM(model=model_name, max_model_len=1024) ref_outputs = ref_llm.chat(test_prompts, sampling_config) del ref_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() spec_llm = LLM( model=model_name, speculative_config=speculative_config, max_model_len=1024, ) spec_outputs = spec_llm.chat(test_prompts, sampling_config) matches = 0 misses = 0 for ref_output, spec_output in zip(ref_outputs, spec_outputs): if ref_output.outputs[0].text == spec_output.outputs[0].text: matches += 1 else: misses += 1 print(f"ref_output: {ref_output.outputs[0].text}") print(f"spec_output: {spec_output.outputs[0].text}") # Heuristic: expect at least 66% of the prompts to match exactly # Upon failure, inspect the outputs to check for inaccuracy. assert matches >= int(0.66 * len(ref_outputs)) del spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() def test_suffix_decoding_acceptance( monkeypatch: pytest.MonkeyPatch, sampling_config: SamplingParams, model_name: str, ): """ Check that suffix decoding caching takes effect and improves acceptance lengths and acceptance rates over multiple runs of the same prompts. """ test_prompts = get_test_prompts(mm_enabled=False) spec_llm = LLM( model=model_name, speculative_config={ "method": "suffix", "suffix_decoding_max_spec_factor": 2.0, "suffix_decoding_max_cached_requests": 1000, }, max_model_len=1024, disable_log_stats=False, ) # Run several times and check that the accepted tokens increase. num_draft = [] num_accept = [] for i in range(10): # Run multiple times to warm up the cache. spec_llm.chat(test_prompts, sampling_config) # Collect draft and acceptance stats. metrics = spec_llm.get_metrics() for metric in metrics: if metric.name == "vllm:spec_decode_num_draft_tokens": num_draft.append(metric.value) if metric.name == "vllm:spec_decode_num_accepted_tokens": num_accept.append(metric.value) # Calculate the acceptance rates for the first and last runs. first_accept_tokens = num_accept[0] first_draft_tokens = num_draft[0] first_accept_rate = first_accept_tokens / first_draft_tokens # Take the diff since the stats are cumulative. last_accept_tokens = num_accept[-1] - num_accept[-2] last_draft_tokens = num_draft[-1] - num_draft[-2] last_accept_rate = last_accept_tokens / last_draft_tokens # Expect the acceptance length to improve. assert first_accept_tokens < last_accept_tokens # Expect the acceptance rate to improve. assert first_accept_rate < last_accept_rate # Heuristic: expect at least 80.0% acceptance rate at the end. assert last_accept_rate > 0.80 del spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() @pytest.mark.parametrize( "model_path", [ "RedHatAI/Llama-3.1-8B-Instruct-speculator.eagle3", "RedHatAI/Qwen3-8B-speculator.eagle3", ], ids=["llama3_eagle3_speculator", "qwen3_eagle3_speculator"], ) def test_speculators_model_integration( monkeypatch: pytest.MonkeyPatch, sampling_config: SamplingParams, model_path: str, ): """ Test that speculators models work with the simplified integration. This verifies the `vllm serve <speculator-model>` use case where speculative config is automatically detected from the model config without requiring explicit --speculative-config argument. Tests: 1. Speculator model is correctly detected 2. Verifier model is extracted from speculator config 3. Speculative decoding is automatically enabled 4. Text generation works correctly 5. Output matches reference (non-speculative) generation """ monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") # Generate test prompts test_prompts = get_test_prompts(mm_enabled=False) # First run: Direct speculator model (simplified integration) spec_llm = LLM(model=model_path, max_model_len=1024) spec_outputs = spec_llm.chat(test_prompts, sampling_config) # Verify speculative config was auto-detected assert spec_llm.llm_engine.vllm_config.speculative_config is not None, ( f"Speculative config should be auto-detected for {model_path}" ) spec_config = spec_llm.llm_engine.vllm_config.speculative_config assert spec_config.num_speculative_tokens > 0, ( f"Expected positive speculative tokens, " f"got {spec_config.num_speculative_tokens}" ) # Verify draft model is set to the speculator model assert spec_config.model == model_path, ( f"Draft model should be {model_path}, got {spec_config.model}" ) # Extract verifier model for reference run verifier_model = spec_llm.llm_engine.vllm_config.model_config.model del spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() # Second run: Reference without speculative decoding ref_llm = LLM(model=verifier_model, max_model_len=1024) ref_outputs = ref_llm.chat(test_prompts, sampling_config) del ref_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() # Compare outputs matches = sum( 1 for ref, spec in zip(ref_outputs, spec_outputs) if ref.outputs[0].text == spec.outputs[0].text ) # Heuristic: expect at least 66% of prompts to match exactly assert matches >= int(0.66 * len(ref_outputs)), ( f"Only {matches}/{len(ref_outputs)} outputs matched. " f"Expected at least {int(0.66 * len(ref_outputs))} matches." ) @pytest.mark.parametrize( ["model_setup", "mm_enabled", "enable_chunked_prefill", "model_impl"], [ ( ("eagle3", "Qwen/Qwen3-8B", "AngelSlim/Qwen3-8B_eagle3", 1), False, False, "auto", ), ( ("eagle3", "Qwen/Qwen3-8B", "AngelSlim/Qwen3-8B_eagle3", 1), False, False, "transformers", ), pytest.param( ( "eagle3", "Qwen/Qwen3-VL-8B-Instruct", "taobao-mnn/Qwen3-VL-8B-Instruct-Eagle3", 1, ), False, False, "auto", marks=pytest.mark.skip( reason="architecture of its eagle3 is LlamaForCausalLMEagle3" ), ), pytest.param( ( "eagle3", "Qwen/Qwen2.5-VL-7B-Instruct", "Rayzl/qwen2.5-vl-7b-eagle3-sgl", 1, ), False, False, "auto", marks=pytest.mark.skip( reason="Skipping due to its head_dim not being a a multiple of 32" ), ), pytest.param( ( "eagle", "meta-llama/Llama-3.1-8B-Instruct", "yuhuili/EAGLE-LLaMA3.1-Instruct-8B", 1, ), False, True, "auto", marks=large_gpu_mark(min_gb=40), ), # works on 4x H100 ( ( "eagle3", "meta-llama/Llama-3.1-8B-Instruct", "yuhuili/EAGLE3-LLaMA3.1-Instruct-8B", 1, ), False, False, "auto", ), pytest.param( ( "eagle", "meta-llama/Llama-4-Scout-17B-16E-Instruct", "morgendave/EAGLE-Llama-4-Scout-17B-16E-Instruct", 4, ), False, False, "auto", marks=large_gpu_mark(min_gb=80), ), # works on 4x H100 pytest.param( ( "eagle", "meta-llama/Llama-4-Scout-17B-16E-Instruct", "morgendave/EAGLE-Llama-4-Scout-17B-16E-Instruct", 4, ), True, True, "auto", marks=large_gpu_mark(min_gb=80), ), # works on 4x H100 ( ( "eagle", "eagle618/deepseek-v3-random", "eagle618/eagle-deepseek-v3-random", 1, ), False, False, "auto", ), ], ids=[ "qwen3_eagle3", "qwen3_eagle3-transformers", "qwen3_vl_eagle3", "qwen2_5_vl_eagle3", "llama3_eagle", "llama3_eagle3", "llama4_eagle", "llama4_eagle_mm", "deepseek_eagle", ], ) @pytest.mark.parametrize("attn_backend", get_attn_backend_list_based_on_platform()) def test_eagle_correctness( monkeypatch: pytest.MonkeyPatch, sampling_config: SamplingParams, model_setup: tuple[str, str, str, int], mm_enabled: bool, enable_chunked_prefill: bool, model_impl: str, attn_backend: str, ): if attn_backend == "TREE_ATTN": # TODO: Fix this flaky test pytest.skip( "TREE_ATTN is flaky in the test disable for now until it can be " "resolved (see https://github.com/vllm-project/vllm/issues/22922)" ) if model_impl == "transformers": import transformers from packaging.version import Version installed = Version(transformers.__version__) required = Version("5.0.0.dev") if installed < required: pytest.skip( "Eagle3 with the Transformers modeling backend requires " f"transformers>={required}, but got {installed}" ) # Generate test prompts inside the function instead of using fixture test_prompts = get_test_prompts(mm_enabled) """ Compare the outputs of a original LLM and a speculative LLM should be the same when using eagle speculative decoding. model_setup: (method, model_name, eagle_model_name, tp_size) """ # Determine attention config # Scout requires default backend selection because vision encoder has # head_dim 88 being incompatible with FLASH_ATTN and needs to fall back # to Flex Attn if "Llama-4-Scout" in model_setup[1] and attn_backend == "FLASH_ATTN": if current_platform.is_rocm(): # TODO: Enable Flex Attn for spec_decode on ROCm pytest.skip("Flex Attn for spec_decode not supported on ROCm currently") attention_config = None # Let it fall back to default else: attention_config = {"backend": attn_backend} if attn_backend == "TRITON_ATTN" and not current_platform.is_rocm(): pytest.skip( "TRITON_ATTN does not support " "multi-token eagle spec decode on current platform" ) with monkeypatch.context() as m: m.setenv("VLLM_MLA_DISABLE", "1") if attn_backend == "ROCM_AITER_FA" and current_platform.is_rocm(): if "deepseek" in model_setup[1].lower(): pytest.skip("ROCM_AITER_FA for deepseek not supported on ROCm platform") else: m.setenv("VLLM_ROCM_USE_AITER", "1") method, model_name, spec_model_name, tp_size = model_setup _skip_if_insufficient_gpus_for_tp(tp_size) max_model_len = 2048 max_num_batched_tokens = 128 if enable_chunked_prefill else max_model_len ref_llm = LLM( model=model_name, max_model_len=max_model_len, tensor_parallel_size=tp_size, attention_config=attention_config, ) ref_outputs = ref_llm.chat(test_prompts, sampling_config) del ref_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() spec_llm = LLM( model=model_name, trust_remote_code=True, tensor_parallel_size=tp_size, speculative_config={ "method": method, "model": spec_model_name, "num_speculative_tokens": 3, "max_model_len": max_model_len, }, max_model_len=max_model_len, max_num_batched_tokens=max_num_batched_tokens, enable_chunked_prefill=enable_chunked_prefill, model_impl=model_impl, attention_config=attention_config, ) spec_outputs = spec_llm.chat(test_prompts, sampling_config) matches = 0 misses = 0 for ref_output, spec_output in zip(ref_outputs, spec_outputs): if ref_output.outputs[0].text == spec_output.outputs[0].text: matches += 1 else: misses += 1 print(f"ref_output: {ref_output.outputs[0].text}") print(f"spec_output: {spec_output.outputs[0].text}") # Heuristic: expect at least 60% of the prompts to match exactly # Upon failure, inspect the outputs to check for inaccuracy. assert matches > int(0.6 * len(ref_outputs)) del spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() @pytest.mark.parametrize( ["model_setup", "mm_enabled"], [ (("mtp", "XiaomiMiMo/MiMo-7B-Base", 1), False), (("mtp", "ZixiQi/DeepSeek-V3-4layers-MTP-FP8", 1), False), ], ids=["mimo", "deepseek"], ) def test_mtp_correctness( monkeypatch: pytest.MonkeyPatch, sampling_config: SamplingParams, model_setup: tuple[str, str, int], mm_enabled: bool, ): # Generate test prompts inside the function instead of using fixture test_prompts = get_test_prompts(mm_enabled) """ Compare the outputs of a original LLM and a speculative LLM should be the same when using MTP speculative decoding. model_setup: (method, model_name, tp_size) """ with monkeypatch.context() as m: m.setenv("VLLM_MLA_DISABLE", "1") method, model_name, tp_size = model_setup _skip_if_insufficient_gpus_for_tp(tp_size) ref_llm = LLM( model=model_name, max_model_len=2048, tensor_parallel_size=tp_size, trust_remote_code=True, ) ref_outputs = ref_llm.chat(test_prompts, sampling_config) del ref_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory() spec_llm = LLM( model=model_name, trust_remote_code=True, tensor_parallel_size=tp_size, speculative_config={ "method": method, "num_speculative_tokens": 1, "max_model_len": 2048, }, max_model_len=2048, ) spec_outputs = spec_llm.chat(test_prompts, sampling_config) matches = 0 misses = 0 for ref_output, spec_output in zip(ref_outputs, spec_outputs): if ref_output.outputs[0].text == spec_output.outputs[0].text: matches += 1 else: misses += 1 print(f"ref_output: {ref_output.outputs[0].text}") print(f"spec_output: {spec_output.outputs[0].text}") # Heuristic: expect at least 80% of the prompts to match exactly # Upon failure, inspect the outputs to check for inaccuracy. assert matches > int(MTP_SIMILARITY_RATE * len(ref_outputs)) del spec_llm torch.cuda.empty_cache() cleanup_dist_env_and_memory()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/ec_connector/integration/test_epd_correctness.py
tests/v1/ec_connector/integration/test_epd_correctness.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ EPD Correctness Test Tests that EPD (Encoder-Prefill-Decode) disaggregation produces the same outputs as a baseline single instance. Usage: # Baseline mode (saves outputs): python test_epd_correctness.py \ --service_url http://localhost:8000 \ --model_name Qwen/Qwen2.5-VL-3B-Instruct \ --mode baseline \ --baseline_file .vllm_epd_baseline.txt # Disagg mode (compares outputs): python test_epd_correctness.py \ --service_url http://localhost:8000 \ --model_name Qwen/Qwen2.5-VL-3B-Instruct \ --mode disagg \ --baseline_file .vllm_epd_baseline.txt """ import argparse import json import os import time import openai import requests from vllm.assets.image import ImageAsset from vllm.multimodal.utils import encode_image_url MAX_OUTPUT_LEN = 256 # Sample prompts with multimodal content image_1 = ImageAsset("stop_sign").pil_image.resize((1280, 720)) image_2 = ImageAsset("cherry_blossom").pil_image.resize((1280, 720)) image_local_path = f"{os.path.dirname(os.path.abspath(__file__))}/hato.jpg" SAMPLE_PROMPTS_MM: list[dict] = [ { "messages": [ { "role": "user", "content": [ { "type": "image_url", "image_url": {"url": encode_image_url(image_1)}, }, {"type": "text", "text": "What's in this image?"}, ], } ], "description": "Single image query", }, { "messages": [ { "role": "user", "content": [ { "type": "image_url", "image_url": {"url": encode_image_url(image_2)}, }, { "type": "image_url", "image_url": {"url": f"file://{image_local_path}"}, }, {"type": "text", "text": "Describe these 2 images in detail."}, ], } ], "description": "2 images with detailed query", }, ] # Text-only prompts for mixed testing SAMPLE_PROMPTS_TEXT: list[dict] = [ { "messages": [{"role": "user", "content": "What is the capital of France?"}], "description": "Simple text-only query", }, { "messages": [ {"role": "user", "content": "Explain quantum computing in simple terms."} ], "description": "Text-only explanation request", }, ] def check_vllm_server(url: str, timeout=5, retries=10) -> bool: """Check if the vLLM server is ready. Args: url: The URL to check (usually /health or /healthcheck endpoint) timeout: Timeout in seconds for each request retries: Number of retries if the server is not ready Returns: True if the server is ready, False otherwise """ for attempt in range(retries): try: response = requests.get(url, timeout=timeout) if response.status_code == 200: print(f"Server is ready at {url}") return True else: print( f"Attempt {attempt + 1}/{retries}: Server returned " f"status code {response.status_code}" ) except requests.exceptions.RequestException as e: print(f"Attempt {attempt + 1}/{retries}: Error connecting: {e}") time.sleep(2) # Wait before retrying return False def run_chat_completion( base_url: str, model_name: str, messages: list, max_tokens: int = MAX_OUTPUT_LEN, ) -> str: """Run a chat completion request. Args: base_url: Base URL of the vLLM server model_name: Name of the model messages: Messages for chat completion max_tokens: Maximum tokens to generate Returns: Generated text content """ client = openai.OpenAI(api_key="EMPTY", base_url=base_url) completion = client.chat.completions.create( model=model_name, messages=messages, max_tokens=max_tokens, temperature=0.0, seed=42, ) return completion.choices[0].message.content def main(): """Main test function.""" parser = argparse.ArgumentParser( description="EPD correctness test - compare disagg vs baseline" ) parser.add_argument( "--service_url", type=str, required=True, help="The vLLM service URL (e.g., http://localhost:8000)", ) parser.add_argument( "--model_name", type=str, required=True, help="Model name", ) parser.add_argument( "--mode", type=str, default="baseline", choices=["baseline", "baseline_pd", "disagg"], help="Mode: baseline/baseline_pd (saves outputs) or disagg (compares outputs)", ) parser.add_argument( "--baseline_file", type=str, default=".vllm_epd_baseline.txt", help="File to save/load baseline outputs", ) parser.add_argument( "--use_mm_prompts", action="store_true", help="Use multimodal prompts (default: use text-only for quick testing)", ) args = parser.parse_args() print(f"Service URL: {args.service_url}") print(f"Model: {args.model_name}") print(f"Mode: {args.mode}") print(f"Output file: {args.baseline_file}") print(f"Use MM prompts: {args.use_mm_prompts}") # Determine health check endpoint if args.mode == "baseline": health_check_url = f"{args.service_url}/health" elif args.mode == "baseline_pd": # Nixl toy proxy use /healthcheck health_check_url = f"{args.service_url}/healthcheck" else: # Disagg EPD proxy uses /health health_check_url = f"{args.service_url}/health" if not os.path.exists(args.baseline_file): raise ValueError( f"In disagg mode, the output file {args.baseline_file} from " "baseline does not exist. Run baseline mode first." ) # Check if server is ready if not check_vllm_server(health_check_url): raise RuntimeError(f"vLLM server at {args.service_url} is not ready!") # Select prompts to use if args.use_mm_prompts: test_prompts = SAMPLE_PROMPTS_MM print("Using multimodal prompts") else: test_prompts = SAMPLE_PROMPTS_TEXT print("Using text-only prompts for quick testing") # Run completions service_url = f"{args.service_url}/v1" output_strs = {} for i, prompt_data in enumerate(test_prompts): print( f"\nRunning prompt {i + 1}/{len(test_prompts)}: " f"{prompt_data['description']}" ) output_str = run_chat_completion( base_url=service_url, model_name=args.model_name, messages=prompt_data["messages"], max_tokens=MAX_OUTPUT_LEN, ) # Use description as key for comparison key = prompt_data["description"] output_strs[key] = output_str print(f"Output: {output_str}") if args.mode in ("baseline", "baseline_pd"): # Baseline mode: Save outputs print(f"\nSaving baseline outputs to {args.baseline_file}") try: with open(args.baseline_file, "w") as json_file: json.dump(output_strs, json_file, indent=4) print("✅ Baseline outputs saved successfully") except OSError as e: print(f"Error writing to file: {e}") raise else: # Disagg mode: Load and compare outputs print(f"\nLoading baseline outputs from {args.baseline_file}") baseline_outputs = None try: with open(args.baseline_file) as json_file: baseline_outputs = json.load(json_file) except OSError as e: print(f"Error reading from file: {e}") raise # Verify outputs match print("\nComparing disagg outputs with baseline...") assert isinstance(baseline_outputs, dict), "Baseline outputs should be a dict" assert len(baseline_outputs) == len(output_strs), ( f"Length mismatch: baseline has {len(baseline_outputs)}, " f"disagg has {len(output_strs)}" ) all_match = True for key, baseline_output in baseline_outputs.items(): assert key in output_strs, f"{key} not in disagg outputs" disagg_output = output_strs[key] if baseline_output == disagg_output: print(f"✅ {key}: MATCH") else: print(f"❌ {key}: MISMATCH") print(f" Baseline: {baseline_output}") print(f" Disagg: {disagg_output}") all_match = False assert all_match, "❌❌Disagg outputs do not match baseline!❌❌" if all_match: print("\n✅ All outputs match! Test PASSED") if __name__ == "__main__": main()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/ec_connector/unit/test_ec_example_connector.py
tests/v1/ec_connector/unit/test_ec_example_connector.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Unit tests for ECExampleConnector. """ import os from unittest.mock import Mock, patch import pytest import safetensors import torch from vllm.config import VllmConfig from vllm.distributed.ec_transfer.ec_connector.base import ECConnectorRole from vllm.distributed.ec_transfer.ec_connector.example_connector import ( ECExampleConnector, ECExampleConnectorMetadata, MMMeta, ) from vllm.multimodal.inputs import MultiModalFeatureSpec, PlaceholderRange from vllm.v1.core.sched.output import SchedulerOutput # ------------------ Mock Classes ------------------ # class MockRequest: def __init__(self, request_id, mm_hashes: list[str], token_counts: list[int]): assert len(mm_hashes) == len(token_counts) self.request_id = request_id self._token_counts = token_counts self.mm_features = [] for i, mm_hash in enumerate(mm_hashes): feature = MultiModalFeatureSpec( data=None, modality="image", identifier=mm_hash, mm_position=PlaceholderRange(offset=0, length=self._token_counts[i]), ) self.mm_features.append(feature) def get_num_encoder_embeds(self, input_id: int) -> int: assert input_id < len(self._token_counts) return self._token_counts[input_id] @pytest.fixture def temp_storage(tmp_path): """Fixture providing temporary storage path.""" return str(tmp_path) @pytest.fixture def mock_vllm_config_producer(temp_storage): """Fixture providing mock VllmConfig for producer role.""" config = Mock(spec=VllmConfig) config.ec_transfer_config = Mock() config.ec_transfer_config.get_from_extra_config = Mock(return_value=temp_storage) config.ec_transfer_config.is_ec_producer = True return config @pytest.fixture def mock_vllm_config_consumer(temp_storage): """Fixture providing mock VllmConfig for consumer role.""" config = Mock(spec=VllmConfig) config.ec_transfer_config = Mock() config.ec_transfer_config.get_from_extra_config = Mock(return_value=temp_storage) config.ec_transfer_config.is_ec_producer = False return config @pytest.fixture def mock_request_with_3_mm(): """Fixture providing mock Request with 3 multimodal items.""" request_id = "test_req_123" mm_hashes = ["img_hash_1", "img_hash_2", "img_hash_3"] token_counts = [100, 150, 200] request = MockRequest(request_id, mm_hashes, token_counts) return request # ------------------ Unit Tests ------------------ # class TestECExampleConnectorBasics: """Test basic EC connector functionality.""" def test_initialization_producer(self, mock_vllm_config_producer, temp_storage): """Test connector initializes correctly as producer.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) assert connector.role == ECConnectorRole.SCHEDULER assert connector.is_producer assert connector._storage_path == temp_storage assert connector._mm_datas_need_loads == {} def test_initialization_consumer(self, mock_vllm_config_consumer, temp_storage): """Test connector initializes correctly as consumer.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) assert connector.role == ECConnectorRole.WORKER assert not connector.is_producer assert connector._storage_path == temp_storage def test_role_assignment(self, mock_vllm_config_producer): """Test role is correctly assigned.""" scheduler_connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) worker_connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) assert scheduler_connector.role == ECConnectorRole.SCHEDULER assert worker_connector.role == ECConnectorRole.WORKER class TestCacheExistence: """Test cache existence checking using has_caches() API.""" def test_has_caches_all_exist_3_items( self, mock_vllm_config_producer, mock_vllm_config_consumer, mock_request_with_3_mm, ): """Test has_caches returns True when all 3 caches exist.""" # Test for producer first producer = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Create cache files using save_caches (proper way) encoder_cache: dict[str, torch.Tensor] = {} for mm_feature in mock_request_with_3_mm.mm_features: mm_hash = mm_feature.identifier encoder_cache[mm_hash] = torch.randn(10, 768) producer.save_caches(encoder_cache, mm_hash) # Test using has_caches API producer_result = producer.has_caches(mock_request_with_3_mm) # Assert assert len(producer_result) == 3 assert all(producer_result), f"Expected all True, got {producer_result}" # Also test consumer can check if cache exists consumer = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.SCHEDULER, ) # Test using has_caches API consumer_result = consumer.has_caches(mock_request_with_3_mm) # Assert assert len(consumer_result) == 3 assert all(consumer_result), f"Expected all True, got {consumer_result}" def test_has_caches_none_exist( self, mock_vllm_config_producer, mock_request_with_3_mm ): """Test has_caches returns False when no caches exist.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Test without creating any files result = connector.has_caches(mock_request_with_3_mm) # Assert assert len(result) == 3 assert not any(result), f"Expected all False, got {result}" def test_has_caches_partial_exist( self, mock_vllm_config_producer, mock_request_with_3_mm ): """Test has_caches with some caches existing (1 of 3).""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Create only the second cache file mm_hash_second = mock_request_with_3_mm.mm_features[1].identifier encoder_cache = {mm_hash_second: torch.randn(10, 768)} connector.save_caches(encoder_cache, mm_hash_second) # Test result = connector.has_caches(mock_request_with_3_mm) # Assert assert len(result) == 3 assert not result[0] # First doesn't exist assert result[1] # Second exists assert not result[2] # Third doesn't exist class TestStateManagement: """Test connector state management.""" def test_update_state_after_alloc_3_items( self, mock_vllm_config_producer, mock_request_with_3_mm ): """Test state update after allocation for 3 MM items.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Initial state should be empty assert len(connector._mm_datas_need_loads) == 0 # Update state for all 3 items for i in range(3): connector.update_state_after_alloc(mock_request_with_3_mm, index=i) # Check state updated for all 3 assert len(connector._mm_datas_need_loads) == 3 assert "img_hash_1" in connector._mm_datas_need_loads assert "img_hash_2" in connector._mm_datas_need_loads assert "img_hash_3" in connector._mm_datas_need_loads assert connector._mm_datas_need_loads["img_hash_1"] == 100 assert connector._mm_datas_need_loads["img_hash_2"] == 150 assert connector._mm_datas_need_loads["img_hash_3"] == 200 def test_build_connector_meta_3_items( self, mock_vllm_config_producer, mock_request_with_3_mm ): """Test metadata building for 3 MM items.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Setup state for all 3 items for i in range(3): connector.update_state_after_alloc(mock_request_with_3_mm, index=i) # Build metadata scheduler_output = Mock(spec=SchedulerOutput) metadata = connector.build_connector_meta(scheduler_output) # Assert assert isinstance(metadata, ECExampleConnectorMetadata) assert len(metadata.mm_datas) == 3 assert metadata.mm_datas[0].mm_hash == "img_hash_1" assert metadata.mm_datas[0].num_token == 100 assert metadata.mm_datas[1].mm_hash == "img_hash_2" assert metadata.mm_datas[1].num_token == 150 assert metadata.mm_datas[2].mm_hash == "img_hash_3" assert metadata.mm_datas[2].num_token == 200 # State should be cleared after building assert len(connector._mm_datas_need_loads) == 0 def test_build_connector_meta_empty(self, mock_vllm_config_producer): """Test metadata building with empty state.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) scheduler_output = Mock(spec=SchedulerOutput) metadata = connector.build_connector_meta(scheduler_output) assert isinstance(metadata, ECExampleConnectorMetadata) assert len(metadata.mm_datas) == 0 def test_state_cleared_after_metadata_build( self, mock_vllm_config_producer, mock_request_with_3_mm ): """Test that state is properly cleared after building metadata.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) # Add state for i in range(3): connector.update_state_after_alloc(mock_request_with_3_mm, index=i) assert len(connector._mm_datas_need_loads) == 3 # Build metadata (should clear state) scheduler_output = Mock(spec=SchedulerOutput) connector.build_connector_meta(scheduler_output) # State should be empty assert len(connector._mm_datas_need_loads) == 0 # Build again should return empty metadata metadata2 = connector.build_connector_meta(scheduler_output) assert len(metadata2.mm_datas) == 0 class TestCacheSaving: """Test encoder cache saving (producer only).""" def test_save_caches_producer_3_items( self, mock_vllm_config_producer, mock_request_with_3_mm, temp_storage ): """Test cache saving as producer for 3 different MM items.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) # Create and save 3 different caches mm_hashes = [f.identifier for f in mock_request_with_3_mm.mm_features] encoder_cache: dict[str, torch.Tensor] = {} for mm_hash in mm_hashes: encoder_cache[mm_hash] = torch.randn(10, 768) connector.save_caches(encoder_cache, mm_hash) # Verify all files exist using has_caches result = connector.has_caches(mock_request_with_3_mm) assert all(result), f"Not all caches were saved: {result}" # Verify each file's content for mm_hash in mm_hashes: filename = connector._generate_filename_debug(mm_hash) loaded = safetensors.torch.load_file(filename) assert "ec_cache" in loaded assert torch.allclose(loaded["ec_cache"], encoder_cache[mm_hash].cpu()) def test_save_caches_consumer_skips(self, mock_vllm_config_consumer): """Test cache saving is skipped for consumer.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) mm_hash = "test_hash_consumer" encoder_cache = {mm_hash: torch.randn(10, 768)} # Save should not raise but also not create file connector.save_caches(encoder_cache, mm_hash) # Verify file doesn't exist using has_caches mock_request = MockRequest("req_consumer", [mm_hash], [10]) result = connector.has_caches(mock_request) assert not result[0], "Consumer should not save caches" class TestCacheLoading: """Test encoder cache loading (consumer).""" @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available") def test_start_load_caches_consumer_3_items( self, mock_vllm_config_producer, mock_vllm_config_consumer, mock_request_with_3_mm, temp_storage, ): """Test consumer loads 3 caches from storage.""" # First, create producer to save caches producer = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) # Producer saves 3 caches mm_hashes = [f.identifier for f in mock_request_with_3_mm.mm_features] saved_caches = {} for mm_hash in mm_hashes: saved_caches[mm_hash] = torch.randn(10, 768) producer.save_caches(saved_caches, mm_hash) # Now consumer loads consumer = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) # Setup metadata for all 3 metadata = ECExampleConnectorMetadata() for mm_hash in mm_hashes: metadata.add_mm_data(MMMeta.make_meta(mm_hash, 100)) consumer.bind_connector_metadata(metadata) # Load encoder_cache: dict[str, torch.Tensor] = {} consumer.start_load_caches(encoder_cache=encoder_cache) # Verify all 3 loaded assert len(encoder_cache) == 3 for mm_hash in mm_hashes: assert mm_hash in encoder_cache, f"{mm_hash} missing in encoder_cache" assert encoder_cache[mm_hash].is_cuda, ( f"{mm_hash} cache is in {encoder_cache[mm_hash].device}" ) assert torch.allclose( encoder_cache[mm_hash].cpu(), saved_caches[mm_hash] ), f"{mm_hash} cache saved and loaded tesnor are not the same" def test_start_load_caches_skip_existing( self, mock_vllm_config_producer, mock_vllm_config_consumer, temp_storage ): """Test cache loading skips already cached items.""" # Setup: producer saves cache producer = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) mm_hash = "existing_hash" saved_cache = torch.randn(10, 768) producer.save_caches({mm_hash: saved_cache}, mm_hash) # Consumer setup consumer = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) metadata = ECExampleConnectorMetadata() metadata.add_mm_data(MMMeta.make_meta(mm_hash, 100)) consumer.bind_connector_metadata(metadata) # Pre-populate encoder_cache with different value existing_cache = torch.randn(5, 512) encoder_cache = {mm_hash: existing_cache} # Load (should skip since already exists) with patch("safetensors.torch.load_file") as mock_load: consumer.start_load_caches(encoder_cache=encoder_cache) # Should not call load_file since cache exists mock_load.assert_not_called() # Verify original cache unchanged assert torch.equal(encoder_cache[mm_hash], existing_cache) def test_start_load_caches_empty_metadata(self, mock_vllm_config_consumer): """Test loading with empty metadata does nothing.""" consumer = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) # Setup empty metadata metadata = ECExampleConnectorMetadata() consumer.bind_connector_metadata(metadata) # Load (should not raise) encoder_cache: dict[str, torch.Tensor] = {} consumer.start_load_caches(encoder_cache=encoder_cache) # Cache should remain empty assert len(encoder_cache) == 0 class TestFilenameGeneration: """Test filename and path generation.""" def test_generate_foldername(self, mock_vllm_config_producer, temp_storage): """Test folder name generation.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) mm_hash = "test_folder_hash" folder = connector._generate_foldername_debug(mm_hash) assert folder == os.path.join(temp_storage, mm_hash) assert os.path.isdir(folder) # Should be created def test_generate_filename(self, mock_vllm_config_producer, temp_storage): """Test filename generation.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) mm_hash = "test_file_hash" filename = connector._generate_filename_debug(mm_hash) expected = os.path.join(temp_storage, mm_hash, "encoder_cache.safetensors") assert filename == expected assert os.path.isdir(os.path.dirname(filename)) # Folder created def test_generate_filename_consistency(self, mock_vllm_config_producer): """Test filename generation is consistent.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) mm_hash = "consistency_hash" filename1 = connector._generate_filename_debug(mm_hash) filename2 = connector._generate_filename_debug(mm_hash) assert filename1 == filename2 class TestMetadataBindingLifecycle: """Test metadata binding and clearing lifecycle.""" def test_bind_connector_metadata(self, mock_vllm_config_consumer): """Test binding connector metadata.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) metadata = ECExampleConnectorMetadata() metadata.add_mm_data(MMMeta.make_meta("hash_1", 100)) connector.bind_connector_metadata(metadata) assert connector._connector_metadata is metadata def test_clear_connector_metadata(self, mock_vllm_config_consumer): """Test clearing connector metadata.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) metadata = ECExampleConnectorMetadata() connector.bind_connector_metadata(metadata) connector.clear_connector_metadata() assert connector._connector_metadata is None def test_get_connector_metadata(self, mock_vllm_config_consumer): """Test getting connector metadata.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) metadata = ECExampleConnectorMetadata() connector.bind_connector_metadata(metadata) retrieved = connector._get_connector_metadata() assert retrieved is metadata def test_get_connector_metadata_not_set(self, mock_vllm_config_consumer): """Test getting metadata when not set raises.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) with pytest.raises(AssertionError): connector._get_connector_metadata() class TestEdgeCases: """Test edge cases and error handling.""" def test_save_empty_cache(self, mock_vllm_config_producer): """Test saving empty tensor.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.WORKER, ) mm_hash = "empty_hash" encoder_cache = {mm_hash: torch.empty(0)} # Should not raise connector.save_caches(encoder_cache, mm_hash) def test_load_nonexistent_cache(self, mock_vllm_config_consumer): """Test loading cache that doesn't exist raises error.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_consumer, role=ECConnectorRole.WORKER, ) metadata = ECExampleConnectorMetadata() metadata.add_mm_data(MMMeta.make_meta("nonexistent_hash", 100)) connector.bind_connector_metadata(metadata) encoder_cache: dict[str, torch.Tensor] = {} # Should raise FileNotFoundError with pytest.raises(FileNotFoundError): connector.start_load_caches(encoder_cache=encoder_cache) def test_has_caches_empty_request(self, mock_vllm_config_producer): """Test has_caches with request that has no MM data.""" connector = ECExampleConnector( vllm_config=mock_vllm_config_producer, role=ECConnectorRole.SCHEDULER, ) mock_request = MockRequest("req_empty", [], []) result = connector.has_caches(mock_request) assert len(result) == 0 assert result == []
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/executor/test_executor.py
tests/v1/executor/test_executor.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import os from collections.abc import Callable from concurrent.futures import Future from typing import Any import pytest from vllm.distributed.kv_transfer.kv_connector.utils import KVOutputAggregator from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs from vllm.sampling_params import SamplingParams from vllm.v1.engine.async_llm import AsyncLLM from vllm.v1.engine.llm_engine import LLMEngine from vllm.v1.executor.multiproc_executor import MultiprocExecutor class Mock: ... class CustomMultiprocExecutor(MultiprocExecutor): def collective_rpc( self, method: str | Callable, timeout: float | None = None, args: tuple = (), kwargs: dict | None = None, non_block: bool = False, unique_reply_rank: int | None = None, kv_output_aggregator: KVOutputAggregator = None, ) -> Any | list[Any] | Future[Any | list[Any]]: # Drop marker to show that this was run with open(".marker", "w"): ... return super().collective_rpc( method, timeout, args, kwargs, non_block, unique_reply_rank, kv_output_aggregator, ) CustomMultiprocExecutorAsync = CustomMultiprocExecutor MODEL = "Qwen/Qwen3-0.6B" def test_custom_executor_type_checking(): with pytest.raises(ValueError): engine_args = EngineArgs( model=MODEL, gpu_memory_utilization=0.2, max_model_len=8192, distributed_executor_backend=Mock, ) LLMEngine.from_engine_args(engine_args) with pytest.raises(ValueError): engine_args = AsyncEngineArgs( model=MODEL, gpu_memory_utilization=0.2, max_model_len=8192, distributed_executor_backend=Mock, ) AsyncLLM.from_engine_args(engine_args) @pytest.mark.parametrize( "distributed_executor_backend", [ CustomMultiprocExecutor, "tests.v1.executor.test_executor.CustomMultiprocExecutor", ], ) def test_custom_executor(distributed_executor_backend, tmp_path): cwd = os.path.abspath(".") os.chdir(tmp_path) try: assert not os.path.exists(".marker") engine_args = EngineArgs( model=MODEL, gpu_memory_utilization=0.2, max_model_len=8192, distributed_executor_backend=distributed_executor_backend, enforce_eager=True, # reduce test time ) engine = LLMEngine.from_engine_args(engine_args) sampling_params = SamplingParams(max_tokens=1) engine.add_request("0", "foo", sampling_params) engine.step() assert os.path.exists(".marker") finally: os.chdir(cwd) @pytest.mark.parametrize( "distributed_executor_backend", [ CustomMultiprocExecutorAsync, "tests.v1.executor.test_executor.CustomMultiprocExecutorAsync", ], ) def test_custom_executor_async(distributed_executor_backend, tmp_path): cwd = os.path.abspath(".") os.chdir(tmp_path) try: assert not os.path.exists(".marker") engine_args = AsyncEngineArgs( model=MODEL, gpu_memory_utilization=0.2, max_model_len=8192, distributed_executor_backend=distributed_executor_backend, enforce_eager=True, # reduce test time ) engine = AsyncLLM.from_engine_args(engine_args) sampling_params = SamplingParams(max_tokens=1) async def t(): stream = engine.generate( request_id="0", prompt="foo", sampling_params=sampling_params ) async for x in stream: ... asyncio.run(t()) assert os.path.exists(".marker") finally: os.chdir(cwd)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/executor/__init__.py
tests/v1/executor/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/kv_offload/test_cpu_manager.py
tests/v1/kv_offload/test_cpu_manager.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Iterable from dataclasses import dataclass import numpy as np from vllm.v1.core.kv_cache_utils import BlockHash from vllm.v1.kv_offload.abstract import ( LoadStoreSpec, OffloadingEvent, PrepareStoreOutput, ) from vllm.v1.kv_offload.arc_manager import ARCOffloadingManager from vllm.v1.kv_offload.backends.cpu import CPUBackend from vllm.v1.kv_offload.lru_manager import LRUOffloadingManager from vllm.v1.kv_offload.mediums import CPULoadStoreSpec @dataclass class ExpectedPrepareStoreOutput: block_hashes_to_store: list[int] store_block_ids: list[int] block_hashes_evicted: list[int] def to_hashes(int_hashes: list[int]) -> list[BlockHash]: return [BlockHash(str(i).encode()) for i in int_hashes] def verify_store_output( prepare_store_output: PrepareStoreOutput | None, expected_prepare_store_output: ExpectedPrepareStoreOutput, ): assert prepare_store_output is not None assert prepare_store_output.block_hashes_to_store == to_hashes( expected_prepare_store_output.block_hashes_to_store ) assert prepare_store_output.block_hashes_evicted == to_hashes( expected_prepare_store_output.block_hashes_evicted ) store_spec = prepare_store_output.store_spec assert isinstance(store_spec, CPULoadStoreSpec) expected_array = np.array( expected_prepare_store_output.store_block_ids, dtype=np.int64 ) assert np.array_equal(expected_array, store_spec.block_ids) def verify_load_output( prepare_load_output: LoadStoreSpec, expected_prepare_load_output: list[int] ): assert isinstance(prepare_load_output, CPULoadStoreSpec) expected_array = np.array(expected_prepare_load_output, dtype=np.int64) assert np.array_equal(expected_array, prepare_load_output.block_ids) def verify_events( events: Iterable[OffloadingEvent], block_size: int, expected_stores: tuple[set[int], ...] = (), expected_evictions: tuple[set[int], ...] = (), ): stores: list[set[BlockHash]] = [] evictions: list[set[BlockHash]] = [] for event in events: assert event.medium == CPULoadStoreSpec.medium() assert event.block_size == block_size if event.removed: evictions.append(set(event.block_hashes)) else: stores.append(set(event.block_hashes)) def to_hash_sets(int_sets: tuple[set[int], ...]) -> tuple[set[BlockHash], ...]: return tuple([set(to_hashes(list(int_set))) for int_set in int_sets]) assert tuple(evictions) == to_hash_sets(expected_evictions) assert tuple(stores) == to_hash_sets(expected_stores) def test_cpu_manager(): """ Tests LRUOffloadingManager with a CPUBackend. """ # initialize a CPU backend with a capacity of 4 blocks block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) cpu_manager = LRUOffloadingManager(cpu_backend, enable_events=True) # prepare store [1, 2] prepare_store_output = cpu_manager.prepare_store(to_hashes([1, 2])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[1, 2], store_block_ids=[0, 1], block_hashes_evicted=[], ), ) # lookup [1, 2] -> not ready assert cpu_manager.lookup(to_hashes([1, 2])) == 0 # no events so far assert list(cpu_manager.take_events()) == [] # complete store [1, 2] cpu_manager.complete_store(to_hashes([1, 2])) verify_events( cpu_manager.take_events(), block_size=block_size, expected_stores=({1, 2},) ) # lookup [1, 2] assert cpu_manager.lookup(to_hashes([1])) == 1 assert cpu_manager.lookup(to_hashes([1, 2])) == 2 assert cpu_manager.lookup(to_hashes([1, 2, 3])) == 2 # prepare store [2, 3, 4, 5] -> evicts [1] prepare_store_output = cpu_manager.prepare_store(to_hashes([2, 3, 4, 5])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[3, 4, 5], store_block_ids=[2, 3, 0], block_hashes_evicted=[1], ), ) # verify eviction event verify_events( cpu_manager.take_events(), block_size=block_size, expected_evictions=({1},) ) # prepare store with no space assert cpu_manager.prepare_store(to_hashes([1, 6])) is None # complete store [2, 3, 4, 5] cpu_manager.complete_store(to_hashes([2, 3, 4, 5])) # prepare load [2, 3] prepare_load_output = cpu_manager.prepare_load(to_hashes([2, 3])) verify_load_output(prepare_load_output, [1, 2]) # prepare store with no space ([2, 3] is being loaded) assert cpu_manager.prepare_store(to_hashes([6, 7, 8])) is None # complete load [2, 3] cpu_manager.complete_load(to_hashes([2, 3])) # prepare store [6, 7, 8] -> evicts [2, 3, 4] (oldest) prepare_store_output = cpu_manager.prepare_store(to_hashes([6, 7, 8])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[6, 7, 8], store_block_ids=[3, 2, 1], block_hashes_evicted=[2, 3, 4], ), ) # complete store [6, 7, 8] cpu_manager.complete_store(to_hashes([6, 7, 8])) # touch [5, 6, 7] (move to end of LRU order) cpu_manager.touch(to_hashes([5, 6, 7])) # prepare store [7, 9] -> evicts [8] (oldest following previous touch) prepare_store_output = cpu_manager.prepare_store(to_hashes([9])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[9], store_block_ids=[1], block_hashes_evicted=[8], ), ) # complete store [7, 9] with failure cpu_manager.complete_store(to_hashes([7, 9]), success=False) # assert [7] is still stored, but [9] is not assert cpu_manager.lookup(to_hashes([7])) == 1 assert cpu_manager.lookup(to_hashes([9])) == 0 verify_events( cpu_manager.take_events(), block_size=block_size, expected_stores=({3, 4, 5}, {6, 7, 8}), expected_evictions=({2, 3, 4}, {8}), ) def test_arc_manager_basic(): """ Tests ARCOffloadingManager basic operations with a CPUBackend. Verifies that ARC handles store, load, and lookup operations correctly. """ # initialize a CPU backend with a capacity of 4 blocks block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=True) # prepare store [1, 2] prepare_store_output = arc_manager.prepare_store(to_hashes([1, 2])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[1, 2], store_block_ids=[0, 1], block_hashes_evicted=[], ), ) # lookup [1, 2] -> not ready assert arc_manager.lookup(to_hashes([1, 2])) == 0 # no events so far assert list(arc_manager.take_events()) == [] # complete store [1, 2] arc_manager.complete_store(to_hashes([1, 2])) verify_events( arc_manager.take_events(), block_size=block_size, expected_stores=({1, 2},) ) # lookup [1, 2] assert arc_manager.lookup(to_hashes([1])) == 1 assert arc_manager.lookup(to_hashes([1, 2])) == 2 assert arc_manager.lookup(to_hashes([1, 2, 3])) == 2 # blocks should be in T1 (recent) assert len(arc_manager.t1) == 2 assert len(arc_manager.t2) == 0 def test_arc_manager_t1_to_t2_promotion(): """ Tests that accessing a block in T1 promotes it to T2 (frequent). This is a key feature of ARC's adaptive behavior. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=False) # store and complete block 1 arc_manager.prepare_store(to_hashes([1])) arc_manager.complete_store(to_hashes([1])) # block 1 starts in T1 (recent) assert to_hashes([1])[0] in arc_manager.t1 assert to_hashes([1])[0] not in arc_manager.t2 # touch block 1 (simulate second access) arc_manager.touch(to_hashes([1])) # block 1 should now be in T2 (frequent) assert to_hashes([1])[0] not in arc_manager.t1 assert to_hashes([1])[0] in arc_manager.t2 def test_arc_manager_eviction_with_load(): """ Tests ARC eviction behavior similar to LRU test. Verifies that blocks being loaded (ref_cnt > 0) cannot be evicted. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=True) # prepare and complete store [1, 2, 3, 4] prepare_store_output = arc_manager.prepare_store(to_hashes([1, 2, 3, 4])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[1, 2, 3, 4], store_block_ids=[0, 1, 2, 3], block_hashes_evicted=[], ), ) arc_manager.complete_store(to_hashes([1, 2, 3, 4])) # prepare load [2, 3] (increases ref_cnt) prepare_load_output = arc_manager.prepare_load(to_hashes([2, 3])) verify_load_output(prepare_load_output, [1, 2]) # prepare store [5, 6, 7] with [2, 3] being loaded # should fail because [2, 3] have ref_cnt > 0 assert arc_manager.prepare_store(to_hashes([5, 6, 7])) is None # complete load [2, 3] arc_manager.complete_load(to_hashes([2, 3])) # now prepare store [5, 6, 7] should succeed # ARC will evict blocks one at a time from T1 as needed prepare_store_output = arc_manager.prepare_store(to_hashes([5, 6, 7])) assert prepare_store_output is not None # Should successfully evict enough blocks to make room (at least 1) assert len(prepare_store_output.block_hashes_evicted) >= 1 def test_arc_manager_adaptive_target(): """ Tests ARC's adaptive target adjustment via ghost lists. When a block in B1 (ghost list) is accessed, target_t1_size increases. When a block in B2 is accessed, target_t1_size decreases. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=2) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=False) # store blocks 1, 2 (fills cache) arc_manager.prepare_store(to_hashes([1, 2])) arc_manager.complete_store(to_hashes([1, 2])) initial_target = arc_manager.target_t1_size # store block 3, evicting block 1 (moves to B1 ghost list) arc_manager.prepare_store(to_hashes([3])) arc_manager.complete_store(to_hashes([3])) # block 1 should be in B1 (ghost list) assert to_hashes([1])[0] in arc_manager.b1 # touch block 1 (cache miss, but in B1) # this should increase target_t1_size (favor recency) arc_manager.touch(to_hashes([1])) # target should have increased assert arc_manager.target_t1_size > initial_target def test_arc_manager_t1_t2_eviction_policy(): """ Tests that ARC evicts from T1 or T2 based on target_t1_size. If |T1| >= target_t1_size, evict from T1, otherwise from T2. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=False) # store blocks 1, 2, 3, 4 arc_manager.prepare_store(to_hashes([1, 2, 3, 4])) arc_manager.complete_store(to_hashes([1, 2, 3, 4])) # promote blocks 3, 4 to T2 by touching them arc_manager.touch(to_hashes([3, 4])) # now: T1 = {1, 2}, T2 = {3, 4} assert len(arc_manager.t1) == 2 assert len(arc_manager.t2) == 2 # set target_t1_size to prefer evicting from T1 # (when |T1| >= target, evict from T1) arc_manager.target_t1_size = 1 # store block 5, should evict from T1 (block 1, LRU in T1) output = arc_manager.prepare_store(to_hashes([5])) assert output is not None assert to_hashes([1]) == output.block_hashes_evicted arc_manager.complete_store(to_hashes([5])) # block 1 should be in B1 (ghost list) assert to_hashes([1])[0] in arc_manager.b1 # block 5 should be in T1 assert to_hashes([5])[0] in arc_manager.t1 def test_arc_manager_ghost_list_bounds(): """ Tests that ghost lists (B1, B2) don't grow unbounded. They should be capped at cache_capacity. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=2) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=False) # fill cache with blocks 1, 2 arc_manager.prepare_store(to_hashes([1, 2])) arc_manager.complete_store(to_hashes([1, 2])) # store many blocks to fill ghost lists for i in range(3, 20): arc_manager.prepare_store(to_hashes([i])) arc_manager.complete_store(to_hashes([i])) # ghost lists should not exceed cache_capacity assert len(arc_manager.b1) <= arc_manager.cache_capacity assert len(arc_manager.b2) <= arc_manager.cache_capacity def test_arc_manager_touch_ordering(): """ Tests that touch() correctly updates access patterns. Similar to LRU test but verifies T1/T2 ordering. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=True) # store blocks 1, 2, 3, 4 arc_manager.prepare_store(to_hashes([1, 2, 3, 4])) arc_manager.complete_store(to_hashes([1, 2, 3, 4])) # promote 3, 4 to T2 arc_manager.touch(to_hashes([3, 4])) # T1 = {1, 2}, T2 = {3, 4} # touch [1, 3, 4] - should promote 1 to T2, and move 3,4 to end of T2 arc_manager.touch(to_hashes([1, 3, 4])) # T1 = {2}, T2 = {1, 3, 4} (in that order, with 4 most recent) assert len(arc_manager.t1) == 1 assert len(arc_manager.t2) == 3 # store block 5, should evict from T1 (block 2, only one in T1) prepare_store_output = arc_manager.prepare_store(to_hashes([5])) verify_store_output( prepare_store_output, ExpectedPrepareStoreOutput( block_hashes_to_store=[5], store_block_ids=[1], # reuses block 2's storage block_hashes_evicted=[2], ), ) def test_arc_manager_failed_store(): """ Tests that failed store operations clean up correctly. Similar to LRU test but for ARC. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=True) # store blocks 1, 2, 3, 4 arc_manager.prepare_store(to_hashes([1, 2, 3, 4])) arc_manager.complete_store(to_hashes([1, 2, 3, 4])) # prepare store block 5 (will evict block 1) prepare_store_output = arc_manager.prepare_store(to_hashes([5])) assert prepare_store_output is not None assert len(prepare_store_output.block_hashes_evicted) == 1 # complete store with failure arc_manager.complete_store(to_hashes([5]), success=False) # block 5 should not be in cache assert arc_manager.lookup(to_hashes([5])) == 0 # block 5 should not be in T1 or T2 assert to_hashes([5])[0] not in arc_manager.t1 assert to_hashes([5])[0] not in arc_manager.t2 # evicted block should still be gone (in B1 ghost list) evicted_hash = prepare_store_output.block_hashes_evicted[0] assert evicted_hash in arc_manager.b1 def test_arc_manager_full_scenario(): """ Comprehensive test covering multiple ARC operations in sequence. Similar to the full LRU test but adapted for ARC behavior. """ block_size = 256 cpu_backend = CPUBackend(block_size=block_size, num_blocks=4) arc_manager = ARCOffloadingManager(cpu_backend, enable_events=True) # store [1, 2] arc_manager.prepare_store(to_hashes([1, 2])) arc_manager.complete_store(to_hashes([1, 2])) # store [3, 4, 5] -> evicts [1] prepare_store_output = arc_manager.prepare_store(to_hashes([3, 4, 5])) assert prepare_store_output is not None assert len(prepare_store_output.block_hashes_evicted) == 1 arc_manager.complete_store(to_hashes([3, 4, 5])) # promote some blocks to T2 arc_manager.touch(to_hashes([2, 3])) # T1 has {4, 5}, T2 has {2, 3} assert len(arc_manager.t1) == 2 assert len(arc_manager.t2) == 2 # store [6] -> should evict from T1 (4 is oldest in T1) prepare_store_output = arc_manager.prepare_store(to_hashes([6])) assert prepare_store_output is not None arc_manager.complete_store(to_hashes([6])) # verify blocks 2, 3 (in T2) are still present assert arc_manager.lookup(to_hashes([2])) == 1 assert arc_manager.lookup(to_hashes([3])) == 1 # verify events events = list(arc_manager.take_events()) assert len(events) > 0 # should have store and eviction events
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/kv_offload/test_cpu_gpu.py
tests/v1/kv_offload/test_cpu_gpu.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import time import pytest import torch from vllm.platforms import current_platform from vllm.v1.attention.backends.flash_attn import FlashAttentionBackend from vllm.v1.kv_offload.mediums import CPULoadStoreSpec, GPULoadStoreSpec from vllm.v1.kv_offload.worker.cpu_gpu import CpuGpuOffloadingHandlers BACKENDS_TO_TEST = [FlashAttentionBackend] if not current_platform.is_rocm(): from vllm.v1.attention.backends.flashinfer import FlashInferBackend BACKENDS_TO_TEST.append(FlashInferBackend) from vllm.v1.attention.backends.mla.flashattn_mla import FlashAttnMLABackend BACKENDS_TO_TEST.append(FlashAttnMLABackend) NUM_GPU_BLOCKS = [64] NUM_CPU_BLOCKS = [256] GPU_BLOCK_SIZES = [16] GPU_BLOCKS_PER_CPU_BLOCK = [1, 3] HEAD_SIZES = [64] NUM_HEADS = [8] NUM_LAYERS = [4] DTYPES = [torch.bfloat16] SEEDS = [0] CUDA_DEVICES = ["cuda:0"] NUM_MAPPINGS = [3] @pytest.mark.parametrize("gpu_to_cpu", [True, False]) @pytest.mark.parametrize("num_mappings", NUM_MAPPINGS) @pytest.mark.parametrize("head_size", HEAD_SIZES) @pytest.mark.parametrize("num_heads", NUM_HEADS) @pytest.mark.parametrize("gpu_block_size", GPU_BLOCK_SIZES) @pytest.mark.parametrize("gpu_blocks_per_cpu_block", GPU_BLOCKS_PER_CPU_BLOCK) @pytest.mark.parametrize("num_gpu_blocks", NUM_GPU_BLOCKS) @pytest.mark.parametrize("num_cpu_blocks", NUM_CPU_BLOCKS) @pytest.mark.parametrize("num_layers", NUM_LAYERS) @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("seed", SEEDS) @pytest.mark.parametrize("device", CUDA_DEVICES) @torch.inference_mode() def test_transfer( gpu_to_cpu: bool, num_mappings: int, head_size: int, num_heads: int, gpu_block_size: int, gpu_blocks_per_cpu_block: int, num_gpu_blocks: int, num_cpu_blocks: int, num_layers: int, dtype: torch.dtype, seed: int, device: str, ) -> None: current_platform.seed_everything(seed) # create per-layer GPU KV caches based on available attn_backends attn_backends_list = BACKENDS_TO_TEST gpu_caches = {} attn_backends = {} for i in range(num_layers): layer_name = f"layer {i}" attn_backend = attn_backends_list[i % len(attn_backends_list)] attn_backends[layer_name] = attn_backend gpu_cache_shape = attn_backend.get_kv_cache_shape( num_gpu_blocks, gpu_block_size, num_heads, head_size ) gpu_caches[layer_name] = torch.rand(gpu_cache_shape, dtype=dtype, device=device) # create handler cpu_block_size = gpu_blocks_per_cpu_block * gpu_block_size handlers = CpuGpuOffloadingHandlers( attn_backends=attn_backends, gpu_block_size=gpu_block_size, cpu_block_size=cpu_block_size, num_cpu_blocks=num_cpu_blocks, gpu_caches=gpu_caches, ) # select block mappings gpu_blocks = random.sample( range(num_gpu_blocks), num_mappings * gpu_blocks_per_cpu_block ) cpu_blocks = random.sample(range(num_cpu_blocks), num_mappings) # convert cpu blocks to gpu block size cpu_blocks_in_gpu_block_size = [] for cpu_block in cpu_blocks: base_block_id = cpu_block * gpu_blocks_per_cpu_block for i in range(gpu_blocks_per_cpu_block): cpu_blocks_in_gpu_block_size.append(i + base_block_id) # maybe skip a GPU block to test reading from the middle of a CPU block if not gpu_to_cpu: gpu_blocks = gpu_blocks[gpu_blocks_per_cpu_block - 1 :] cpu_blocks_in_gpu_block_size = cpu_blocks_in_gpu_block_size[ gpu_blocks_per_cpu_block - 1 : ] # set transfer direction if gpu_to_cpu: handler = handlers.gpu_to_cpu_handler src_spec_class = GPULoadStoreSpec dst_spec_class = CPULoadStoreSpec src_blocks = gpu_blocks dst_blocks = cpu_blocks src_blocks_in_gpu_block_size = gpu_blocks dst_blocks_in_gpu_block_size = cpu_blocks_in_gpu_block_size dst_size_in_gpu_blocks = num_cpu_blocks * gpu_blocks_per_cpu_block else: handler = handlers.cpu_to_gpu_handler src_spec_class = CPULoadStoreSpec dst_spec_class = GPULoadStoreSpec src_blocks = cpu_blocks dst_blocks = gpu_blocks src_blocks_in_gpu_block_size = cpu_blocks_in_gpu_block_size dst_blocks_in_gpu_block_size = gpu_blocks dst_size_in_gpu_blocks = num_gpu_blocks # build dst -> src mapping dst_to_src = {} for src_block, dst_block in zip( src_blocks_in_gpu_block_size, dst_blocks_in_gpu_block_size ): dst_to_src[dst_block] = src_block # build transfer specs src_spec = src_spec_class(src_blocks) dst_spec = dst_spec_class(dst_blocks) # clone src and dst tensors before transfer orig_src_caches = [x.clone() for x in handler.src_tensors] orig_dst_caches = [x.clone() for x in handler.dst_tensors] # call transfer function assert handler.transfer_async(1, (src_spec, dst_spec)) assert set({x[0] for x in handler._transfers}) == {1} # wait for transfer to complete end_time = time.time() + 10 while time.time() < end_time: finished = handler.get_finished() if finished: assert finished == [(1, True)] break time.sleep(0.1) # verify src tensors did not change for orig_tensor, tensor in zip(orig_src_caches, handler.src_tensors): assert torch.equal(orig_tensor, tensor) # verify dst tensors for dst_block in range(dst_size_in_gpu_blocks): src_block_candidate = dst_to_src.get(dst_block) for src_cache, dst_cache, orig_dst_cache, kv_dim in zip( handler.src_tensors, handler.dst_tensors, orig_dst_caches, handler.kv_dim_before_num_blocks, ): if kv_dim: # iterate over key, value for i in range(2): if src_block_candidate is not None: expected_value = src_cache[i][src_block_candidate] else: expected_value = orig_dst_cache[i][dst_block] torch.testing.assert_close( dst_cache[i][dst_block].cpu(), expected_value.cpu() ) else: if src_block_candidate is not None: expected_value = src_cache[src_block_candidate] else: expected_value = orig_dst_cache[dst_block] torch.testing.assert_close( dst_cache[dst_block].cpu(), expected_value.cpu() )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/kv_offload/test_worker.py
tests/v1/kv_offload/test_worker.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm.v1.kv_offload.abstract import LoadStoreSpec from vllm.v1.kv_offload.worker.worker import ( OffloadingHandler, OffloadingWorker, TransferResult, TransferSpec, ) class LoadStoreSpec1(LoadStoreSpec): def __init__( self, submit_success: bool = True, async_success: bool = True, exception: bool = False, ): self.finished = False self.submit_success = submit_success self.async_success = async_success self.exception = exception @staticmethod def medium() -> str: return "1" def __repr__(self): return f"{self.medium()}: {id(self)}" class LoadStoreSpec2(LoadStoreSpec): @staticmethod def medium() -> str: return "2" def __repr__(self): return f"{self.medium()}: {id(self)}" class OffloadingHandler1To2(OffloadingHandler): def __init__(self): self.transfers: dict[int, LoadStoreSpec1] = {} def transfer_async(self, job_id: int, spec: TransferSpec) -> bool: src, dst = spec assert isinstance(src, LoadStoreSpec1) assert isinstance(dst, LoadStoreSpec2) if src.exception: raise Exception("An expected exception. Don't worry!") if not src.submit_success: return False self.transfers[job_id] = src return True def get_finished(self) -> list[TransferResult]: finished = [] for job_id, spec in list(self.transfers.items()): if spec.finished: finished.append((job_id, spec.async_success)) del self.transfers[job_id] return finished class OffloadingHandler2To1(OffloadingHandler): def __init__(self): self.transfers: dict[int, LoadStoreSpec1] = {} def transfer_async(self, job_id: int, spec: TransferSpec) -> bool: src, dst = spec assert isinstance(src, LoadStoreSpec2) assert isinstance(dst, LoadStoreSpec1) self.transfers[job_id] = dst return True def get_finished(self) -> list[TransferResult]: finished = [] for job_id, spec in list(self.transfers.items()): if spec.finished: finished.append((job_id, spec.async_success)) del self.transfers[job_id] return finished def test_offloading_worker(): """ Tests OffloadingWorker with 2 handlers. One handler performs 1->2 transfers, and the other handles 2->1. """ worker = OffloadingWorker() handler1to2 = OffloadingHandler1To2() handler2to1 = OffloadingHandler2To1() worker.register_handler(LoadStoreSpec1, LoadStoreSpec2, handler1to2) worker.register_handler(LoadStoreSpec2, LoadStoreSpec1, handler2to1) # 1st transfer 1->2 (exception) src1 = LoadStoreSpec1(exception=True) dst1 = LoadStoreSpec2() assert not worker.transfer_async(1, (src1, dst1)) # 2ed transfer 1->2 (failure to submit) src2 = LoadStoreSpec1(submit_success=False) dst2 = LoadStoreSpec2() assert not worker.transfer_async(2, (src2, dst2)) # 3rd transfer 1->2 (failure) src3 = LoadStoreSpec1(async_success=False) dst3 = LoadStoreSpec2() assert worker.transfer_async(3, (src3, dst3)) # 4th transfer 1->2 (success) src4 = LoadStoreSpec1() dst4 = LoadStoreSpec2() worker.transfer_async(4, (src4, dst4)) assert set(handler1to2.transfers.keys()) == {3, 4} # 5th transfer 2->1 src5 = LoadStoreSpec2() dst5 = LoadStoreSpec1() worker.transfer_async(5, (src5, dst5)) assert set(handler2to1.transfers.keys()) == {5} # no transfer completed yet assert worker.get_finished() == [] # complete 3rd, 4th src3.finished = True src4.finished = True # 6th transfer 1->2 src6 = LoadStoreSpec1() dst6 = LoadStoreSpec2() worker.transfer_async(6, (src6, dst6)) # 7th transfer 2->1 src7 = LoadStoreSpec2() dst7 = LoadStoreSpec1() worker.transfer_async(7, (src7, dst7)) # 6th and 7th transfers started assert 6 in handler1to2.transfers assert 7 in handler2to1.transfers # verify result of 3rd and 4th transfers assert sorted(worker.get_finished()) == [(3, False), (4, True)] # complete 6th and 7th transfers src6.finished = True dst7.finished = True assert sorted(worker.get_finished()) == [(6, True), (7, True)]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/kv_offload/test_cpu_offloading.py
tests/v1/kv_offload/test_cpu_offloading.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import socket import time import msgspec import msgspec.msgpack import pytest import zmq from tqdm import tqdm from vllm import LLM, SamplingParams, TokensPrompt from vllm.config import KVEventsConfig, KVTransferConfig from vllm.distributed.kv_events import BlockStored, KVEventBatch from vllm.platforms import current_platform CPU_BLOCK_SIZES = [48] ATTN_BACKENDS = ["FLASH_ATTN"] if current_platform.is_cuda(): ATTN_BACKENDS.append("FLASHINFER") elif current_platform.is_rocm(): ATTN_BACKENDS = ["TRITON_ATTN"] class MockSubscriber: """Helper class to receive and verify published events""" def __init__( self, endpoint: str, topic: str, ): self.ctx = zmq.Context.instance() self.topic_bytes = topic.encode("utf-8") # Set up subscriber socket self.sub = self.ctx.socket(zmq.SUB) self.sub.setsockopt(zmq.SUBSCRIBE, self.topic_bytes) self.sub.connect(endpoint) self.decoder = msgspec.msgpack.Decoder(type=KVEventBatch) def get_new_cpu_stored_events(self) -> list[BlockStored]: cpu_stored_events: list[BlockStored] = [] poller = zmq.Poller() poller.register(self.sub, zmq.POLLIN) timeout = 1000 # 1 second while True: events = dict(poller.poll(timeout)) if events.get(self.sub) != zmq.POLLIN: return cpu_stored_events topic_bytes, _, payload = self.sub.recv_multipart() assert topic_bytes == self.topic_bytes event_batch = self.decoder.decode(payload) assert isinstance(event_batch, KVEventBatch) for event in event_batch.events: if isinstance(event, BlockStored) and event.medium == "CPU": cpu_stored_events.append(event) timeout = 100 def close(self): """Clean up resources""" self.sub.close() def _latency_test(llm: LLM, subscriber: MockSubscriber): sampling_params = SamplingParams(max_tokens=1) num_times_cpu_better_than_cold = 0 num_tests = 10 total_cold_time = 0.0 total_gpu_hit_time = 0.0 total_cpu_hit_time = 0.0 prompt_token_ids = [0] * 10001 for i in tqdm(range(num_tests), desc="Running tests"): prompt_token_ids[0] = i prompts = [TokensPrompt(prompt_token_ids=prompt_token_ids)] # run generation - this should trigger saving KV cache start_time = time.time() llm.generate(prompts, sampling_params, use_tqdm=False) cold_time = time.time() - start_time total_cold_time += cold_time # run generation again - should hit the GPU prefix cache start_time = time.time() llm.generate(prompts, sampling_params, use_tqdm=False) gpu_hit_time = time.time() - start_time total_gpu_hit_time += gpu_hit_time # reset prefix cache to avoid GPU hit. llm.reset_prefix_cache() assert subscriber.get_new_cpu_stored_events() # run generation again - this should trigger loading from CPU start_time = time.time() llm.generate(prompts, sampling_params, use_tqdm=False) cpu_hit_time = time.time() - start_time total_cpu_hit_time += cpu_hit_time if cpu_hit_time < cold_time: num_times_cpu_better_than_cold += 1 print("Average times:") print(f" Cold: {total_cold_time * 1000 / num_tests:.2f}ms") print(f" GPU hit: {total_gpu_hit_time * 1000 / num_tests:.2f}ms") print(f" CPU hit: {total_cpu_hit_time * 1000 / num_tests:.2f}ms") assert num_times_cpu_better_than_cold >= 0.8 * num_tests def _accuracy_test(llm: LLM, subscriber: MockSubscriber): sampling_params = SamplingParams(max_tokens=1) cpu_block_size = ( llm.llm_engine.vllm_config.kv_transfer_config.kv_connector_extra_config[ "block_size" ] ) subscriber.get_new_cpu_stored_events() # prepend prompt to be cpu block aligned prompt = "Let's count to 10. One, two, three, four," while ( len(llm.generate(prompt, use_tqdm=False)[0].prompt_token_ids) % cpu_block_size != 0 ): prompt = ". " + prompt assert subscriber.get_new_cpu_stored_events() test_count = 100 success_count = 0 for i in range(test_count): if ( llm.generate(prompt, sampling_params, use_tqdm=False)[0].outputs[0].text == " five" ): success_count += 1 assert success_count >= 0.5 * test_count @pytest.mark.parametrize("cpu_block_size", CPU_BLOCK_SIZES) @pytest.mark.parametrize("attn_backend", ATTN_BACKENDS) def test_cpu_offloading(cpu_block_size: int, attn_backend: str) -> None: """ Tests OffloadingConnector with CPUOffloadingSpec. """ # configure OffloadingConnector (spec_name=CPUOffloadingSpec by default) kv_transfer_config = KVTransferConfig( kv_connector="OffloadingConnector", kv_role="kv_both", kv_connector_extra_config={ "num_cpu_blocks": 1000, "block_size": cpu_block_size, }, ) port: int with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(("0.0.0.0", 0)) port = s.getsockname()[1] events_endpoint = f"tcp://*:{port}" kv_events_config = KVEventsConfig( enable_kv_cache_events=True, publisher="zmq", endpoint=events_endpoint, topic="test", ) llm = LLM( model="meta-llama/Llama-3.2-1B-Instruct", gpu_memory_utilization=0.5, kv_events_config=kv_events_config, kv_transfer_config=kv_transfer_config, attention_config={"backend": attn_backend}, ) events_endpoint = events_endpoint.replace("*", "127.0.0.1") subscriber = MockSubscriber(events_endpoint, topic=kv_events_config.topic) try: _latency_test(llm, subscriber) _accuracy_test(llm, subscriber) finally: subscriber.close() del llm
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_scheduler_e2e.py
tests/v1/core/test_scheduler_e2e.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from vllm import LLM MODEL = "hmellor/tiny-random-LlamaForCausalLM" PROMPT = "Hello my name is Robert and I" @pytest.fixture(scope="module") def llm() -> LLM: return LLM( MODEL, enforce_eager=True, enable_prefix_caching=True, long_prefill_token_threshold=2, max_num_batched_tokens=6, max_num_seqs=3, block_size=16, ) def test_concurrent_partial_prefill(llm): outputs = llm.generate([PROMPT] * 3) assert len(outputs) == 3 for output in outputs: assert len(output.outputs) == 1 def test_prefix_cache_stats_is_recorded(llm): # 17 tokens will make sure first 16 tokens are cached in a block input_tokens = {"prompt_token_ids": [101] * 17} _ = llm.generate([input_tokens]) outputs = llm.generate([input_tokens]) assert outputs[0].num_cached_tokens == 16
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_output.py
tests/v1/core/test_output.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.v1.core.sched.output import NewRequestData def _create_new_requests_data(prompt_embeds: torch.Tensor | None) -> NewRequestData: return NewRequestData( req_id="test_req", prompt_token_ids=None, mm_features=[], sampling_params=None, pooling_params=None, block_ids=([],), num_computed_tokens=0, lora_request=None, prompt_embeds=prompt_embeds, ) def test_repr_with_none() -> None: """Test repr when prompt_embeds is None.""" new_requests_data = _create_new_requests_data(None) assert "prompt_embeds_shape=None" in repr(new_requests_data) assert "prompt_embeds_shape=None" in new_requests_data.anon_repr() def test_repr_with_multi_element_tensor() -> None: """Test repr when prompt_embeds is a multi-element tensor.""" prompt_embeds = torch.randn(10, 768) new_requests_data = _create_new_requests_data(prompt_embeds) assert "prompt_embeds_shape=torch.Size([10, 768])" in repr(new_requests_data) assert "prompt_embeds_shape=torch.Size([10, 768])" in new_requests_data.anon_repr()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_encoder_cache_manager.py
tests/v1/core/test_encoder_cache_manager.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from vllm.multimodal.inputs import MultiModalFeatureSpec, PlaceholderRange from vllm.v1.core.encoder_cache_manager import EncoderCacheManager pytestmark = pytest.mark.cpu_test # ------------------ Mock Classes ------------------ # class MockRequest: def __init__(self, request_id, mm_hashes, token_counts): self.request_id = request_id self._token_counts = token_counts self.mm_features = [] for i, mm_hash in enumerate(mm_hashes): feature = MultiModalFeatureSpec( data=None, modality="image", identifier=mm_hash, mm_position=PlaceholderRange(offset=0, length=self._token_counts[i]), ) self.mm_features.append(feature) def get_num_encoder_embeds(self, input_id: int) -> int: return self._token_counts[input_id] # ------------------ Unit Tests ------------------ # def test_basic_allocate_and_reuse(): cache = EncoderCacheManager(cache_size=10) req = MockRequest("r1", ["imgA"], [4]) assert not cache.check_and_update_cache(req, 0) assert cache.can_allocate(req, 0, int(1e9), 0) cache.allocate(req, 0) assert cache.check_and_update_cache(req, 0) assert "r1" in cache.cached["imgA"] assert cache.num_free_slots == 6 # Free twice to bring refcount to 0. cache.free_encoder_input(req, 0) cache.free_encoder_input(req, 0) assert not cache.cached["imgA"] assert "imgA" in cache.freeable assert cache.num_freeable_slots == 10 assert cache.num_free_slots == 6 def test_freeing_decreases_refcount_and_moves_to_freeable(): manager = EncoderCacheManager(cache_size=10) req = MockRequest("req2", ["img3"], [5]) assert manager.can_allocate(req, 0, int(1e9), 0) manager.allocate(req, 0) assert len(manager.cached["img3"]) == 1 manager.free_encoder_input(req, 0) assert not manager.cached["img3"] assert "img3" in manager.freeable assert manager.num_freeable_slots == 10 def test_free_request_frees_all_inputs(): manager = EncoderCacheManager(cache_size=10) req = MockRequest("req3", ["a", "b"], [2, 3]) assert manager.can_allocate(req, 0, int(1e9), 0) manager.allocate(req, 0) assert manager.can_allocate(req, 1, int(1e9), 0) manager.allocate(req, 1) assert len(manager.cached["a"]) == 1 assert len(manager.cached["b"]) == 1 manager.free(req) assert not manager.cached["a"] assert not manager.cached["b"] assert "a" in manager.freeable assert "b" in manager.freeable assert manager.num_freeable_slots == 10 def test_eviction_when_cache_is_full(): manager = EncoderCacheManager(cache_size=10) req1 = MockRequest("req1", ["x"], [6]) req2 = MockRequest("req2", ["y"], [5]) assert manager.can_allocate(req1, 0, int(1e9), 0) manager.allocate(req1, 0) manager.free_encoder_input(req1, 0) assert manager.can_allocate(req2, 0, int(1e9), 0) manager.allocate(req2, 0) # 'x' should have been evicted. assert "x" not in manager.cached assert "x" in manager.get_freed_mm_hashes() def test_get_cached_input_ids(): manager = EncoderCacheManager(cache_size=10) req = MockRequest("reqX", ["m", "n", "o"], [2, 4, 3]) assert manager.can_allocate(req, 0, int(1e9), 0) manager.allocate(req, 0) assert manager.can_allocate(req, 2, int(1e9), 0) manager.allocate(req, 2) cached_ids = manager.get_cached_input_ids(req) assert cached_ids == {0, 2} def test_has_cache_restores_from_freeable(): manager = EncoderCacheManager(cache_size=10) req = MockRequest("reqY", ["imgZ"], [4]) assert manager.can_allocate(req, 0, int(1e9), 0) manager.allocate(req, 0) manager.free_encoder_input(req, 0) # Should restore from freeable. assert manager.check_and_update_cache(req, 0) assert len(manager.cached["imgZ"]) == 1 assert "imgZ" not in manager.freeable assert manager.num_freeable_slots == 6 def test_get_freed_mm_hashes_clears_freed_list(): manager = EncoderCacheManager(cache_size=10) req1 = MockRequest("reqA", ["a"], [5]) req2 = MockRequest("reqB", ["b"], [6]) assert manager.can_allocate(req1, 0, int(1e9), 0) manager.allocate(req1, 0) manager.free_encoder_input(req1, 0) # Should trigger eviction of 'a'. assert manager.can_allocate(req2, 0, int(1e9), 0) manager.allocate(req2, 0) freed = manager.get_freed_mm_hashes() assert "a" in freed assert manager.get_freed_mm_hashes() == [] def test_schedule_request_multi_images_respect_space_limit(): manager = EncoderCacheManager(cache_size=10) req = MockRequest("reqA", ["a", "b"], [5, 6]) compute_budget = 100 num_tokens_to_schedule = 0 assert manager.can_allocate(req, 0, compute_budget, num_tokens_to_schedule) num_tokens_to_schedule += req.get_num_encoder_embeds(0) compute_budget -= req.get_num_encoder_embeds(0) assert not manager.can_allocate(req, 1, compute_budget, num_tokens_to_schedule) def test_schedule_request_multi_images_respect_compute_limit(): manager = EncoderCacheManager(cache_size=100) req = MockRequest("reqA", ["a", "b"], [5, 6]) compute_budget = 10 num_tokens_to_schedule = 0 assert manager.can_allocate(req, 0, compute_budget, num_tokens_to_schedule) num_tokens_to_schedule += req.get_num_encoder_embeds(0) compute_budget -= req.get_num_encoder_embeds(0) assert not manager.can_allocate(req, 1, compute_budget, num_tokens_to_schedule) def test_encoder_cache_with_is_embed_mask(): class MockRequestWithMask(MockRequest): def get_num_encoder_embeds(self, input_id: int) -> int: return self.mm_features[input_id].mm_position.get_num_embeds is_embed = torch.zeros(100, dtype=torch.bool) is_embed[torch.tensor([5, 15, 25, 35, 45, 55, 65, 75])] = True request = MockRequestWithMask("r1", ["img1"], [100]) request.mm_features[0] = MultiModalFeatureSpec( data=None, modality="image", identifier="img1", mm_position=PlaceholderRange(offset=0, length=100, is_embed=is_embed), ) manager = EncoderCacheManager(cache_size=100) manager.allocate(request, 0) assert manager.num_free_slots == 92 assert "img1" in manager.cached old_size = 100 new_size = request.mm_features[0].mm_position.get_num_embeds assert new_size == 8 savings_ratio = old_size / new_size assert savings_ratio == 12.5 def test_encoder_cache_mask_based_retrieval(): class MockRequestWithMask(MockRequest): def get_num_encoder_embeds(self, input_id: int) -> int: return self.mm_features[input_id].mm_position.get_num_embeds is_embed = torch.tensor( [False, False, True, True, False, True, True, True, False, False] ) request = MockRequestWithMask("r1", ["img1"], [10]) request.mm_features[0] = MultiModalFeatureSpec( data=None, modality="image", identifier="img1", mm_position=PlaceholderRange(offset=0, length=10, is_embed=is_embed), ) manager = EncoderCacheManager(cache_size=50) manager.allocate(request, 0) assert request.mm_features[0].mm_position.get_num_embeds == 5 start_idx = 2 end_idx = 8 num_embeds_before = is_embed[:start_idx].sum().item() num_embeds_in_range = is_embed[start_idx:end_idx].sum().item() assert num_embeds_before == 0 assert num_embeds_in_range == 5 start_idx = 0 end_idx = 5 num_embeds_before = is_embed[:start_idx].sum().item() if start_idx > 0 else 0 num_embeds_in_range = is_embed[start_idx:end_idx].sum().item() assert num_embeds_before == 0 assert num_embeds_in_range == 2
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_prefix_caching.py
tests/v1/core/test_prefix_caching.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Compare the with and without prefix caching.""" import copy from collections.abc import Callable import pytest import torch import vllm.v1.core.kv_cache_utils as kv_cache_utils from vllm.distributed.kv_events import AllBlocksCleared, BlockRemoved, BlockStored from vllm.lora.request import LoRARequest from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams from vllm.utils.hashing import sha256, sha256_cbor from vllm.v1.core.block_pool import BlockHashToBlockMap, BlockPool from vllm.v1.core.kv_cache_manager import KVCacheManager, Request from vllm.v1.core.kv_cache_utils import ( BlockHash, BlockHashWithGroupId, KVCacheBlock, get_block_hash, get_group_id, get_request_block_hasher, hash_block_tokens, init_none_hash, make_block_hash_with_group_id, ) from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheGroupSpec, SlidingWindowSpec, ) pytestmark = pytest.mark.cpu_test @pytest.fixture(autouse=True) def _auto_init_hash_fn(request): hash_fn: Callable if "hash_fn" in request.fixturenames: hash_fn = request.getfixturevalue("hash_fn") else: hash_fn = sha256 init_none_hash(hash_fn) def make_request( request_id: str, prompt_token_ids: list[int], block_size: int, hash_fn: Callable, mm_positions: list[PlaceholderRange] | None = None, mm_hashes: list[str] | None = None, prompt_logprobs: int | None = None, cache_salt: str | None = None, lora_request: LoRARequest | None = None, ): mm_features = [] if mm_positions is not None: for j, position in enumerate(mm_positions): identifier = mm_hashes[j] if mm_hashes else f"hash_{j}" mm_feature = MultiModalFeatureSpec( data=MultiModalKwargsItem.dummy("dummy_m"), mm_position=position, identifier=identifier, modality="image", ) mm_features.append(mm_feature) return Request( request_id=request_id, prompt_token_ids=prompt_token_ids, mm_features=mm_features if mm_features else None, sampling_params=SamplingParams(max_tokens=17, prompt_logprobs=prompt_logprobs), pooling_params=None, eos_token_id=100, lora_request=lora_request, cache_salt=cache_salt, block_hasher=get_request_block_hasher(block_size, hash_fn), ) def make_kv_cache_config(block_size: int, num_blocks: int) -> KVCacheConfig: return KVCacheConfig( num_blocks=num_blocks, kv_cache_tensors=[], kv_cache_groups=[ KVCacheGroupSpec( ["layer"], FullAttentionSpec(block_size, 1, 1, torch.float32), ) ], ) def make_kv_cache_config_hybrid_model( block_size: int, num_blocks: int ) -> KVCacheConfig: return KVCacheConfig( num_blocks=num_blocks, kv_cache_tensors=[], kv_cache_groups=[ KVCacheGroupSpec( ["layer1"], FullAttentionSpec(block_size, 1, 1, torch.float32), ), KVCacheGroupSpec( ["layer2"], SlidingWindowSpec( block_size, 1, 1, torch.float32, sliding_window=2 * block_size ), ), KVCacheGroupSpec( ["layer3"], SlidingWindowSpec( block_size, 1, 1, torch.float32, sliding_window=2 * block_size ), ), ], ) @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_prefill(hash_fn): block_size = 16 manager = KVCacheManager( make_kv_cache_config(block_size, 11), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) # Complete 3 blocks (48 tokens) common_token_ids = [i for i in range(3) for _ in range(16)] # Fully cache miss # Incomplete 1 block (7 tokens) unique_token_ids = [3] * 7 all_token_ids = common_token_ids + unique_token_ids req0 = make_request("0", all_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert len(req0.block_hashes) == 3 assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, 55, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([1, 2, 3, 4],) # Check full block metadata parent_block_hash = None for block_id in (1, 2, 3): block_tokens = tuple(all_token_ids[(block_id - 1) * 16 : block_id * 16]) block_hash = hash_block_tokens(hash_fn, parent_block_hash, block_tokens) blk_hash = manager.block_pool.blocks[block_id].block_hash assert blk_hash is not None assert get_block_hash(blk_hash) == block_hash assert get_group_id(blk_hash) == 0 assert manager.block_pool.blocks[block_id].ref_cnt == 1 parent_block_hash = block_hash # Check partial block metadata for block_id in (4,): assert manager.block_pool.blocks[block_id].block_hash is None assert manager.block_pool.blocks[block_id].ref_cnt == 1 # Cache hit in the common prefix when the original block is still in use. # Incomplete 1 block (5 tokens) unique_token_ids = [3] * 5 req1 = make_request("1", common_token_ids + unique_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert len(req1.block_hashes) == 3 assert computed_blocks.get_block_ids() == ([1, 2, 3],) assert num_computed_tokens == 3 * 16 num_new_tokens = 53 - 3 * 16 blocks = manager.allocate_slots( req1, num_new_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([5],) for block in computed_blocks.blocks[0]: assert block.ref_cnt == 2 # At this point, we should have 5 free blocks left. free_block_queue = manager.block_pool.free_block_queue assert free_block_queue.num_free_blocks == 5 manager.free(req0) manager.free(req1) # All blocks should be available. assert free_block_queue.num_free_blocks == 10 # The order should be # [unallocated (6, 7, 8, 9, 10)] # [unique_req0 (4)] # [unique_req1 (5)] # [common (3, 2, 1)] assert [ b.block_id for b in manager.block_pool.free_block_queue.get_all_free_blocks() ] == [6, 7, 8, 9, 10, 4, 5, 3, 2, 1] # Cache hit in the common prefix when the original block is already free. # Incomplete 1 block (6 tokens) unique_token_ids = [3] * 6 req2 = make_request("2", common_token_ids + unique_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req2) assert len(req2.block_hashes) == 3 assert computed_blocks.get_block_ids() == ([1, 2, 3],) assert num_computed_tokens == 3 * 16 num_new_tokens = 53 - 3 * 16 blocks = manager.allocate_slots( req2, num_new_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([6],) # Although we only have 6 free blocks, we have 8 blocks in # the free block queue due to lazy removal. assert free_block_queue.num_free_blocks == 6 assert all([b.ref_cnt == 0 for b in free_block_queue.get_all_free_blocks()]) assert len([b for b in free_block_queue.get_all_free_blocks()]) == 6 manager.free(req2) # Cache miss and eviction. req3 = make_request("3", [99] * (16 * 10), block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req3) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req3, 16 * 10, len(computed_blocks.blocks[0]) * 16, computed_blocks ) # This block ID order also checks the eviction order. assert blocks is not None and blocks.get_block_ids() == ( [7, 8, 9, 10, 4, 5, 6, 3, 2, 1], ) assert free_block_queue.num_free_blocks == 0 assert ( free_block_queue.fake_free_list_head.next_free_block is free_block_queue.fake_free_list_tail ) assert ( free_block_queue.fake_free_list_tail.prev_free_block is free_block_queue.fake_free_list_head ) def test_prefill_hybrid_model(): block_size = 16 manager = KVCacheManager( make_kv_cache_config_hybrid_model(block_size, 21), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) hash_fn = sha256 # Complete 3 blocks (48 tokens) common_token_ids = [i for i in range(3) for _ in range(block_size)] # Fully cache miss # Incomplete 1 block (7 tokens) unique_token_ids = [3] * 7 all_token_ids = common_token_ids + unique_token_ids req0 = make_request("0", all_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert len(req0.block_hashes) == 3 assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, 55, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ( [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ) # Check full block metadata parent_block_hash = None for length, block_ids in zip((1, 2, 3), ((1, 5, 9), (2, 6, 10), (3, 7, 11))): block_tokens = tuple(all_token_ids[(length - 1) * 16 : length * 16]) block_hash = hash_block_tokens(hash_fn, parent_block_hash, block_tokens) for group_id, block_id in enumerate(block_ids): blk_hash = manager.block_pool.blocks[block_id].block_hash assert blk_hash is not None assert get_block_hash(blk_hash) == block_hash assert get_group_id(blk_hash) == group_id assert manager.block_pool.blocks[block_id].ref_cnt == 1 parent_block_hash = block_hash # Check partial block metadata for block_id in (4, 8, 12): assert manager.block_pool.blocks[block_id].block_hash is None assert manager.block_pool.blocks[block_id].ref_cnt == 1 # Cache hit in the common prefix # Incomplete 1 block (5 tokens) unique_token_ids = [3] * 5 req1 = make_request("1", common_token_ids + unique_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert len(req1.block_hashes) == 3 assert computed_blocks.get_block_ids() == ([1, 2, 3], [0, 6, 7], [0, 10, 11]) assert num_computed_tokens == 3 * 16 num_new_tokens = 53 - 3 * 16 blocks = manager.allocate_slots( req1, num_new_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([13], [14], [15]) for block_per_group in computed_blocks.blocks: for block in block_per_group: if block != manager.block_pool.null_block: assert block.ref_cnt == 2 block_hashes = req1.block_hashes manager.free(req0) manager.free(req1) cached_block_hash_to_block_bak = copy.copy( manager.block_pool.cached_block_hash_to_block._cache ) def test_partial_request_hit( request_id: str, hash_to_evict: list[BlockHashWithGroupId], expect_hit_length: int, ): req = make_request( request_id, common_token_ids + unique_token_ids, block_size, sha256 ) for hash_with_group_id in hash_to_evict: manager.block_pool.cached_block_hash_to_block._cache.pop(hash_with_group_id) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req) assert len(req.block_hashes) == 3 assert num_computed_tokens == expect_hit_length * block_size for block_per_group in computed_blocks.blocks: assert len(block_per_group) == num_computed_tokens // block_size for hash_with_group_id in hash_to_evict: manager.block_pool.cached_block_hash_to_block._cache[hash_with_group_id] = ( cached_block_hash_to_block_bak[hash_with_group_id] ) manager.free(req) # Evict the blocks outside sliding window, does not affect the hit length. test_partial_request_hit( "2", [ make_block_hash_with_group_id(block_hashes[0], 1), make_block_hash_with_group_id(block_hashes[0], 2), ], 3, ) # Evict the first block of full attention, makes total cache miss. test_partial_request_hit( "3", [make_block_hash_with_group_id(block_hashes[0], 0)], 0 ) # Evict the last block of all layers, reduces the hit length to 2. test_partial_request_hit( "4", [ make_block_hash_with_group_id(block_hashes[2], 0), make_block_hash_with_group_id(block_hashes[2], 1), make_block_hash_with_group_id(block_hashes[2], 2), ], 2, ) # Evict the last block of full attention, reduces the hit length to 2. test_partial_request_hit( "5", [make_block_hash_with_group_id(block_hashes[2], 0)], 2 ) # Evict the last block of sliding window, reduces the hit length to 2. test_partial_request_hit( "6", [make_block_hash_with_group_id(block_hashes[2], 1)], 2 ) # Evict the last block of sliding window, reduces the hit length to 2. test_partial_request_hit( "7", [make_block_hash_with_group_id(block_hashes[2], 2)], 2 ) # Evict different set of blocks for full attention and sliding window makes # total cache miss. # The cache hit length of full attention is 1 * block_size. # The cache hit length of sliding window is 2 * block_size. # Then it is cache miss as the two type of layers # have different hit length. test_partial_request_hit( "8", [ make_block_hash_with_group_id(block_hashes[2], 0), make_block_hash_with_group_id(block_hashes[0], 1), make_block_hash_with_group_id(block_hashes[0], 2), ], 0, ) def test_prefill_plp(): """Test prefill with APC and some prompt logprobs (plp) requests. 1. Schedule plp request and validate APC block allocation 2. Schedule non-plp request and validate blocks 3. Schedule plp request; no hit should occur; validate blocks """ block_size = 16 manager = KVCacheManager( make_kv_cache_config(block_size, 11), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) # the default hash function is sha256 hash_fn = sha256 # Complete 3 blocks (48 tokens) common_token_ids = [i for i in range(3) for _ in range(16)] # Request #0 is a prompt logprobs request # Fully cache miss # Incomplete 1 block (7 tokens) unique_token_ids = [3] * 7 all_token_ids = common_token_ids + unique_token_ids req0 = make_request("0", all_token_ids, block_size, hash_fn, prompt_logprobs=5) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert len(req0.block_hashes) == 3 assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, 55, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([1, 2, 3, 4],) req0_block_hashes = [b.block_hash for b in blocks.blocks[0]] # Check full block metadata parent_block_hash = None for block_id in (1, 2, 3): block_tokens = tuple(all_token_ids[(block_id - 1) * 16 : block_id * 16]) block_hash = hash_block_tokens(hash_fn, parent_block_hash, block_tokens) blk_hash = manager.block_pool.blocks[block_id].block_hash assert blk_hash is not None assert get_block_hash(blk_hash) == block_hash assert get_group_id(blk_hash) == 0 assert manager.block_pool.blocks[block_id].ref_cnt == 1 parent_block_hash = block_hash # Check partial block metadata for block_id in (4,): assert manager.block_pool.blocks[block_id].block_hash is None assert manager.block_pool.blocks[block_id].ref_cnt == 1 # Request #1 is a non-prompt-logprobs request: # Cache hit in the common prefix when the original block is still in use. # Incomplete 1 block (5 tokens) unique_token_ids = [3] * 5 req1 = make_request("1", common_token_ids + unique_token_ids, block_size, hash_fn) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert len(req1.block_hashes) == 3 assert computed_blocks.get_block_ids() == ([1, 2, 3],) assert num_computed_tokens == 3 * 16 num_new_tokens = 53 - 3 * 16 blocks = manager.allocate_slots( req1, num_new_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([5],) for block in computed_blocks.blocks[0]: assert block.ref_cnt == 2 # At this point, we should have 5 free blocks left. assert manager.block_pool.free_block_queue.num_free_blocks == 5 manager.free(req0) manager.free(req1) # All blocks should be available. assert manager.block_pool.free_block_queue.num_free_blocks == 10 # The order should be # [unallocated (6, 7, 8, 9, 10)] # [unique_req0 (4)] # [unique_req1 (5)] # [common (3, 2, 1)] assert [ b.block_id for b in manager.block_pool.free_block_queue.get_all_free_blocks() ] == [6, 7, 8, 9, 10, 4, 5, 3, 2, 1] # Request #2 is a prompt-logprobs request: # NO cache hit in the common prefix; duplicates request #0 cached blocks unique_token_ids = [3] * 6 req2 = make_request( "2", common_token_ids + unique_token_ids, block_size, hash_fn, prompt_logprobs=5 ) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req2) assert len(req2.block_hashes) == 3 assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req2, 55, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None block_ids = blocks.get_block_ids() # Duplicate cached blocks have different ids but same hashes vs request #0 assert [b.block_hash for b in blocks.blocks[0]] == req0_block_hashes assert block_ids != ([1, 2, 3, 4],) # Request #2 block hashes are valid since request #0 hashes are. # Check block reference counts. for block_id in block_ids[0]: assert manager.block_pool.blocks[block_id].ref_cnt == 1 manager.free(req2) def test_decode(): block_size = 16 manager = KVCacheManager( make_kv_cache_config(block_size, 11), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) # Complete 3 blocks (48 tokens) common_token_ids = [i for i in range(3) for _ in range(16)] # Fully cache miss # Incomplete 1 block (7 tokens) unique_token_ids = [3] * 7 req0 = make_request("0", common_token_ids + unique_token_ids, block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, 55, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([1, 2, 3, 4],) # Append slots without allocating a new block. req0.num_computed_tokens = 55 for _ in range(4): req0.append_output_token_ids(8) new_blocks = manager.allocate_slots( req0, 4, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert new_blocks is not None and len(new_blocks.blocks[0]) == 0 assert ( manager.coordinator.single_type_managers[0] .req_to_blocks[req0.request_id][-1] .block_hash is None ) # Append slots with allocating a new block. req0.num_computed_tokens = 59 # 9 tokens to fill the previous block, and 10 tokens to fill # the preallocated block. for _ in range(9 + 10): req0.append_output_token_ids(7) new_blocks = manager.allocate_slots( req0, 19, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert new_blocks is not None and len(new_blocks.blocks[0]) == 1 assert ( manager.coordinator.single_type_managers[0] .req_to_blocks[req0.request_id][-2] .block_hash is not None ) assert ( manager.coordinator.single_type_managers[0] .req_to_blocks[req0.request_id][-1] .block_hash is None ) def test_evict(): block_size = 16 manager = KVCacheManager( make_kv_cache_config(block_size, 11), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) last_token_id = 5 * 16 + 7 req0 = make_request("0", list(range(last_token_id)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, 5 * 16 + 7, len(computed_blocks.blocks[0]) * 16, computed_blocks ) # 5 full + 1 partial assert blocks is not None and len(blocks.blocks[0]) == 6 # 3 blocks. req1 = make_request( "1", list(range(last_token_id, last_token_id + 3 * 16)), block_size, sha256 ) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req1, 3 * 16, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 3 # 3 full blocks last_token_id += 3 * 16 # 10 - (6 + 3) == 1 assert manager.block_pool.free_block_queue.num_free_blocks == 1 manager.free(req0) manager.free(req1) assert manager.block_pool.free_block_queue.num_free_blocks == 10 assert [ b.block_id for b in manager.block_pool.free_block_queue.get_all_free_blocks() ] == [10, 6, 5, 4, 3, 2, 1, 9, 8, 7] # Touch the first 2 blocks. req2 = make_request("2", list(range(2 * 16 + 3)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req2) assert computed_blocks.get_block_ids() == ([1, 2],) assert num_computed_tokens == 2 * 16 blocks = manager.allocate_slots( req2, 3, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and blocks.get_block_ids() == ([10],) assert manager.block_pool.free_block_queue.num_free_blocks == 7 def test_hash_block_correct_reuse(): """ This tests when a previously cached block is reused as a new block, its hash metadata should be correctly reset. """ block_size = 16 manager = KVCacheManager( make_kv_cache_config(16, 2), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) # Allocate 1 block and cache it. num_tokens = block_size * 1 req = make_request("0", list(range(num_tokens)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req, num_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 1 # Deallocate the block. manager.free(req) # Allocate a new block that's not full, make sure hash info on the # block is cleared. req = make_request("1", list(range(num_tokens - 1)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req, num_tokens - 1, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 1 assert manager.block_pool.blocks[blocks.blocks[0][0].block_id].block_hash is None def test_computed_blocks_not_evicted(): """ Test that the computed blocks are not evicted when getting new blocks for a request if there are any other free blocks. """ block_size = 16 manager = KVCacheManager( make_kv_cache_config(block_size, 3), max_model_len=8192, enable_caching=True, hash_block_size=block_size, ) # Allocate a block and cache it. num_tokens = block_size * 1 req0 = make_request("0", list(range(num_tokens)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req0) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req0, num_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 1 assert blocks.blocks[0][0].block_id == 1 # Allocate another block. req1 = make_request( "1", list(range(num_tokens, num_tokens * 2)), block_size, sha256 ) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req1, num_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 1 assert blocks.blocks[0][0].block_id == 2 # Free the blocks. manager.free(req0) manager.free(req1) # Now if we have a cache hit on the first block, we should evict the second # cached block rather than the first one. req2 = make_request("2", list(range(num_tokens * 2)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req2) assert len(computed_blocks.blocks[0]) == 1 assert computed_blocks.blocks[0][0].block_id == 1 assert num_computed_tokens == block_size blocks = manager.allocate_slots( req2, num_tokens * 2 - num_tokens, len(computed_blocks.blocks[0]) * 16, computed_blocks, ) assert blocks is not None and len(blocks.blocks[0]) == 1 assert blocks.blocks[0][0].block_id == 2 def test_basic_prefix_caching_disabled(): """ This tests that the prefix caching is disabled. """ block_size = 4 manager = KVCacheManager( make_kv_cache_config(block_size, 5), max_model_len=8192, enable_caching=False, hash_block_size=block_size, ) req1 = make_request( "1", list(range(10)), block_size, sha256 ) # 2 blocks and some more computed_blocks, num_computed_tokens = manager.get_computed_blocks(req1) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req1, 10, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 3 # Free the blocks. manager.free(req1) # No caching. req2 = make_request("2", list(range(16)), block_size, sha256) # shared prefix computed_blocks, num_computed_tokens = manager.get_computed_blocks(req2) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req2, 16, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert blocks is not None and len(blocks.blocks[0]) == 4 # New requests should not have any blocks. req3 = make_request("3", list(range(4)), block_size, sha256) computed_blocks, num_computed_tokens = manager.get_computed_blocks(req3) assert not computed_blocks.blocks[0] assert num_computed_tokens == 0 blocks = manager.allocate_slots( req3, 4, len(computed_blocks.blocks[0]) * 16, computed_blocks ) assert not blocks @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_cache_blocks(hash_fn): """ This is a unit test that tests the correctness of the _cache_full_blocks function of KVCacheManager. """ block_size = 4 block_pool = BlockPool( num_gpu_blocks=5, enable_caching=True, hash_block_size=block_size, ) # Req: # Block 0: [0, 1, 2, 3] # Block 1: [4, 5, 6, 7] # Block 2: [8, 9, 10, 11] # Block 3: [12, 13] req = make_request("0", list(range(14)), block_size, hash_fn) # Test that blocks are cached correctly for 2 full blocks from the start. blocks = [KVCacheBlock(block_id=i) for i in range(2)] block_pool.cache_full_blocks( request=req, blocks=blocks, num_cached_blocks=0, num_full_blocks=2, block_size=block_size, kv_cache_group_id=0, ) assert len(block_pool.cached_block_hash_to_block) == 2 assert all([block.block_hash is not None for block in blocks]) # Test that blocks that don't start from the beginning are cached # correctly. blocks += [KVCacheBlock(block_id=2)] block_pool.cache_full_blocks( request=req, blocks=blocks, num_cached_blocks=2, num_full_blocks=3, block_size=block_size, kv_cache_group_id=0, ) assert len(block_pool.cached_block_hash_to_block) == 3 assert blocks[0].block_hash is not None def test_cache_blocks_multi_group(): """ This tests that blocks are cached correctly for different kv cache groups. """ block_size = 4 block_pool = BlockPool( num_gpu_blocks=10, enable_caching=True, hash_block_size=block_size ) # Req: # Block 0/4: [0, 1, 2, 3] # Block 1/5: [4, 5, 6, 7] # Block 2/6: [8, 9, 10, 11] # Block 3/7: [12, 13] req = make_request("0", list(range(14)), block_size, sha256) # Cache the blocks for group 0. blocks = [KVCacheBlock(block_id=i) for i in range(2)] block_pool.cache_full_blocks( request=req, blocks=blocks, num_cached_blocks=0, num_full_blocks=2, block_size=block_size, kv_cache_group_id=0, ) assert len(block_pool.cached_block_hash_to_block) == 2 assert len(req.block_hashes) == 3 assert all([block.block_hash is not None for block in blocks]) # Cache the blocks for group 1. blocks = [KVCacheBlock(block_id=i) for i in range(3)] block_pool.cache_full_blocks( request=req, blocks=blocks, num_cached_blocks=0, num_full_blocks=3, block_size=block_size, kv_cache_group_id=1, ) assert len(block_pool.cached_block_hash_to_block) == 5 assert len(req.block_hashes) == 3 assert all([block.block_hash is not None for block in blocks]) # Block hash 0: hit for group 0 and 1 # Block hash 1: hit for group 0 and 1 # Block hash 2: hit for group 1 assert ( block_pool.get_cached_block(req.block_hashes[0], kv_cache_group_ids=[0]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[1], kv_cache_group_ids=[0]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[2], kv_cache_group_ids=[0]) is None ) assert ( block_pool.get_cached_block(req.block_hashes[0], kv_cache_group_ids=[1]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[1], kv_cache_group_ids=[1]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[2], kv_cache_group_ids=[1]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[0], kv_cache_group_ids=[0, 1]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[1], kv_cache_group_ids=[0, 1]) is not None ) assert ( block_pool.get_cached_block(req.block_hashes[2], kv_cache_group_ids=[0, 1]) is None ) def test_mm_prefix_caching(): """ This tests that the multi-modal prefix caching is correct. """ block_size = 16 manager = KVCacheManager(
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_reset_prefix_cache_e2e.py
tests/v1/core/test_reset_prefix_cache_e2e.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm import EngineArgs, LLMEngine, SamplingParams PROMPTS = [ "A robot may not injure a human being ", "To be or not to be,", "What is the meaning of life?", "What does the fox say? " * 20, # Test long prompt ] def test_reset_prefix_cache_e2e(monkeypatch): # "spawn" is required for test to be deterministic monkeypatch.setenv("VLLM_WORKER_MULTIPROC_METHOD", "spawn") engine_args = EngineArgs( model="Qwen/Qwen3-0.6B", gpu_memory_utilization=0.2, async_scheduling=True, max_num_batched_tokens=32, max_model_len=2048, compilation_config={"mode": 0}, dtype="float16", ) engine = LLMEngine.from_engine_args(engine_args) sampling_params = SamplingParams( temperature=0.0, max_tokens=16, ) # No preempt case: for i, prompt in enumerate(PROMPTS): engine.add_request("ground_truth_" + str(i), prompt, sampling_params) ground_truth_results = {} while engine.has_unfinished_requests(): request_outputs = engine.step() for request_output in request_outputs: if request_output.finished: ground_truth_results[request_output.request_id] = request_output # Preempt case: for i, prompt in enumerate(PROMPTS): engine.add_request("preempted_" + str(i), prompt, sampling_params) step_id = 0 preempted_results = {} while engine.has_unfinished_requests(): if step_id == 10: engine.reset_prefix_cache(reset_running_requests=True) request_outputs = engine.step() for request_output in request_outputs: if request_output.finished: preempted_results[request_output.request_id] = request_output step_id += 1 for i in range(len(PROMPTS)): assert ( ground_truth_results["ground_truth_" + str(i)].outputs[0].text == preempted_results["preempted_" + str(i)].outputs[0].text ), ( f"ground_truth_results['ground_truth_{i}'].outputs[0].text=" f"{ground_truth_results['ground_truth_' + str(i)].outputs[0].text} " f"preempted_results['preempted_{i}'].outputs[0].text=" f"{preempted_results['preempted_' + str(i)].outputs[0].text}" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_kv_cache_metrics.py
tests/v1/core/test_kv_cache_metrics.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from unittest.mock import patch import pytest from vllm.v1.core.kv_cache_metrics import ( BlockMetricsState, KVCacheMetricsCollector, ) from vllm.v1.core.kv_cache_utils import KVCacheBlock class TestBlockMetricsState: def test_init(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() assert state.birth_time_ns == 1000000000 assert state.last_access_ns == 1000000000 assert len(state.access_history) == 0 def test_access_tracking(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() with patch("time.monotonic_ns", return_value=2000000000): state.record_access() assert state.last_access_ns == 2000000000 assert list(state.access_history) == [2000000000] def test_ring_buffer_wraps_at_4(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() for i in range(5): t = 1000000000 + (i + 1) * 1000000000 with patch("time.monotonic_ns", return_value=t): state.record_access() assert len(state.access_history) == 4 assert list(state.access_history) == [ 3000000000, 4000000000, 5000000000, 6000000000, ] def test_lifetime(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() with patch("time.monotonic_ns", return_value=6500000000): assert abs(state.get_lifetime_seconds() - 5.5) < 0.001 def test_idle_time(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() state.last_access_ns = 2000000000 with patch("time.monotonic_ns", return_value=5200000000): assert abs(state.get_idle_time_seconds() - 3.2) < 0.001 def test_reuse_gaps(self): with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() base = 1000000000 for offset in [0, 1.5, 3.0, 5.5]: state.access_history.append(base + int(offset * 1e9)) gaps = state.get_reuse_gaps_seconds() assert len(gaps) == 3 assert gaps[0] == 1.5 and gaps[1] == 1.5 and gaps[2] == 2.5 def test_ring_wrap_only_gives_3_gaps(self): # 5 accesses in size-4 buffer = 3 gaps with patch("time.monotonic_ns", return_value=1000000000): state = BlockMetricsState() for i in range(5): state.access_history.append(1000000000 + i * 1000000000) assert len(state.get_reuse_gaps_seconds()) == 3 class TestKVCacheMetricsCollector: def test_sample_rate_validation(self): with pytest.raises(AssertionError): KVCacheMetricsCollector(sample_rate=-0.1) with pytest.raises(AssertionError): KVCacheMetricsCollector(sample_rate=1.5) with pytest.raises(AssertionError): KVCacheMetricsCollector(sample_rate=0.0) def test_sampling(self): c = KVCacheMetricsCollector(sample_rate=1.0) assert sum(1 for _ in range(100) if c.should_sample_block()) == 100 c = KVCacheMetricsCollector(sample_rate=0.5) samples = sum(1 for _ in range(1000) if c.should_sample_block()) assert 400 < samples < 600 def test_alloc(self): c = KVCacheMetricsCollector(sample_rate=1.0) blocks = [KVCacheBlock(block_id=i) for i in range(5)] with patch("time.monotonic_ns", return_value=1000000000): for block in blocks: c.on_block_allocated(block) assert len(c.block_metrics) == 5 def test_access(self): c = KVCacheMetricsCollector(sample_rate=1.0) block = KVCacheBlock(block_id=0) with patch("time.monotonic_ns", return_value=1000000000): c.on_block_allocated(block) for i in range(3): t = 1000000000 + (i + 1) * 1000000000 with patch("time.monotonic_ns", return_value=t): c.on_block_accessed(block) assert len(c.block_metrics[0].access_history) == 3 def test_evict_no_accesses(self): # lifetime should equal idle if never accessed c = KVCacheMetricsCollector(sample_rate=1.0) block = KVCacheBlock(block_id=0) with patch("time.monotonic_ns", return_value=1000000000): c.on_block_allocated(block) with patch("time.monotonic_ns", return_value=6000000000): c.on_block_evicted(block) events = c.drain_events() assert len(events) == 1 assert abs(events[0].lifetime_seconds - 5.0) < 0.001 assert abs(events[0].idle_seconds - 5.0) < 0.001 def test_evict(self): c = KVCacheMetricsCollector(sample_rate=1.0) block = KVCacheBlock(block_id=0) with patch("time.monotonic_ns", return_value=1000000000): c.on_block_allocated(block) with patch("time.monotonic_ns", return_value=2000000000): c.on_block_accessed(block) with patch("time.monotonic_ns", return_value=3000000000): c.on_block_accessed(block) with patch("time.monotonic_ns", return_value=4000000000): c.on_block_evicted(block) events = c.drain_events() assert len(events) == 1 sample = events[0] assert abs(sample.lifetime_seconds - 3.0) < 0.001 assert abs(sample.idle_seconds - 1.0) < 0.001 assert sample.reuse_gaps_seconds == (1.0,) assert 0 not in c.block_metrics def test_reset(self): c = KVCacheMetricsCollector(sample_rate=1.0) with patch("time.monotonic_ns", return_value=1000000000): for i in range(5): c.on_block_allocated(KVCacheBlock(block_id=i)) assert len(c.block_metrics) == 5 c.reset() assert len(c.block_metrics) == 0 with patch("time.monotonic_ns", return_value=2000000000): c.on_block_allocated(KVCacheBlock(block_id=10)) assert 10 in c.block_metrics def test_huge_time_jump(self): c = KVCacheMetricsCollector(sample_rate=1.0) block = KVCacheBlock(block_id=0) with patch("time.monotonic_ns", return_value=1000000000): c.on_block_allocated(block) with patch("time.monotonic_ns", return_value=9999999999999999): c.on_block_evicted(block) events = c.drain_events() assert len(events) == 1 assert events[0].lifetime_seconds > 0 def test_kv_cache_metrics_collector_smoke() -> None: """Simple smoke test for KVCacheMetricsCollector on CPU.""" collector = KVCacheMetricsCollector(sample_rate=1.0) block = KVCacheBlock(block_id=123) # Allocate at t = 1.0s. with patch("time.monotonic_ns", return_value=1_000_000_000): collector.on_block_allocated(block) # Access at t = 2.0s and t = 3.0s. with patch("time.monotonic_ns", return_value=2_000_000_000): collector.on_block_accessed(block) with patch("time.monotonic_ns", return_value=3_000_000_000): collector.on_block_accessed(block) # Evict at t = 4.0s. with patch("time.monotonic_ns", return_value=4_000_000_000): collector.on_block_evicted(block) events = collector.drain_events() assert len(events) == 1 event = events[0] # Lifetime: 1.0s → 4.0s. assert abs(event.lifetime_seconds - 3.0) < 1e-6 # Idle: last access at 3.0s, evicted at 4.0s. assert abs(event.idle_seconds - 1.0) < 1e-6 # One reuse gap between the two accesses. assert event.reuse_gaps_seconds == (1.0,)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/utils.py
tests/v1/core/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from tests.v1.kv_connector.unit.utils import MockKVConfig from vllm.config import ( CacheConfig, ECTransferConfig, KVTransferConfig, ModelConfig, SchedulerConfig, SpeculativeConfig, VllmConfig, ) from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams from vllm.utils.hashing import sha256 from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash from vllm.v1.core.sched.async_scheduler import AsyncScheduler from vllm.v1.core.sched.scheduler import Scheduler from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheGroupSpec, ) from vllm.v1.request import Request from vllm.v1.structured_output import StructuredOutputManager EOS_TOKEN_ID = 50256 def mock_kv(matched_tokens: int, is_async: bool): return MockKVConfig(matched_tokens=matched_tokens, is_async=is_async) def create_scheduler( model: str = "facebook/opt-125m", max_num_seqs: int = 16, max_num_batched_tokens: int = 8192, enable_chunked_prefill: bool = True, enable_prefix_caching: bool = False, long_prefill_token_threshold: int = 0, disable_chunked_mm_input: bool = False, use_kv_connector: None | bool | MockKVConfig = None, num_blocks: int = 10000, block_size: int = 16, max_model_len: int | None = None, num_speculative_tokens: int | None = None, skip_tokenizer_init: bool = False, async_scheduling: bool = False, use_ec_connector: bool = False, ec_role: str | None = None, ) -> Scheduler | AsyncScheduler: """Create scheduler under test. Args: model: model under test max_num_seqs: max sequences to schedule max_num_batch_tokens: max num tokens to batch enable_prefix_caching: optionally force APC config (True/False) or use default (False) Returns: {class}`Scheduler` instance """ model_config = ModelConfig( model=model, trust_remote_code=True, dtype="float16", seed=42, skip_tokenizer_init=skip_tokenizer_init, ) if max_model_len is None: max_model_len = max_num_batched_tokens scheduler_config = SchedulerConfig( max_num_seqs=max_num_seqs, max_num_batched_tokens=max_num_batched_tokens, max_model_len=max_model_len, long_prefill_token_threshold=long_prefill_token_threshold, disable_chunked_mm_input=disable_chunked_mm_input, enable_chunked_prefill=enable_chunked_prefill, async_scheduling=async_scheduling, is_encoder_decoder=model_config.is_encoder_decoder, ) # Cache config, optionally force APC cache_config = CacheConfig( block_size=block_size, gpu_memory_utilization=0.9, swap_space=0, cache_dtype="auto", enable_prefix_caching=enable_prefix_caching, ) kv_transfer_config = None if isinstance(use_kv_connector, MockKVConfig): kv_transfer_config = KVTransferConfig( kv_connector="MockKVConnector", kv_role="kv_both", kv_connector_extra_config={ "matched_tokens": use_kv_connector.matched_tokens, "is_async": use_kv_connector.is_async, }, ) elif use_kv_connector: kv_transfer_config = KVTransferConfig( kv_connector="ExampleConnector", kv_role="kv_both", kv_connector_extra_config={"shared_storage_path": "local_storage"}, ) speculative_config: SpeculativeConfig | None = None if num_speculative_tokens is not None: speculative_config = SpeculativeConfig( model="ngram", num_speculative_tokens=num_speculative_tokens ) ec_transfer_config = ( ECTransferConfig( ec_connector="ECExampleConnector", ec_role=ec_role, ec_connector_extra_config={"shared_storage_path": "/tmp/ec_test"}, ) if use_ec_connector else None ) vllm_config = VllmConfig( scheduler_config=scheduler_config, model_config=model_config, cache_config=cache_config, kv_transfer_config=kv_transfer_config, speculative_config=speculative_config, ec_transfer_config=ec_transfer_config, ) kv_cache_config = KVCacheConfig( num_blocks=num_blocks, # A large number of blocks to hold all requests kv_cache_tensors=[], kv_cache_groups=[ KVCacheGroupSpec( ["layer"], FullAttentionSpec(block_size, 1, 1, torch.float32, False) ) ], ) cache_config.num_gpu_blocks = num_blocks scheduler_cls = AsyncScheduler if async_scheduling else Scheduler return scheduler_cls( vllm_config=vllm_config, kv_cache_config=kv_cache_config, block_size=block_size, log_stats=True, structured_output_manager=StructuredOutputManager(vllm_config), ) _none_hash_initialized = False def create_requests( num_requests: int, num_tokens: int = 10, mm_hashes_list: list[list[str]] | None = None, mm_positions: list[list[PlaceholderRange]] | None = None, max_tokens: int = 16, stop_token_ids: list[int] | None = None, prompt_logprobs: int | None = None, same_prompt: bool = False, block_size: int = 16, req_ids: list[str] | None = None, ) -> list[Request]: global _none_hash_initialized if not _none_hash_initialized: init_none_hash(sha256) _none_hash_initialized = True block_hasher = get_request_block_hasher(block_size, sha256) sampling_params = SamplingParams( ignore_eos=False, max_tokens=max_tokens, stop_token_ids=stop_token_ids, prompt_logprobs=prompt_logprobs, ) requests = [] if mm_hashes_list is not None: # NOTE: allow manual input; some mm items can have the same identifier # no. of mm_hashes and mm_positions for each request should be identical assert mm_positions is not None, ( "mm_positions must be provided when mm_hashes_list is provided" ) assert len(mm_hashes_list) == len(mm_positions) == num_requests assert [len(h) for h in mm_hashes_list] == [len(p) for p in mm_positions] # Since same identifier would imply they are identical encoder output # Verify mm items with identical identifier are having mm_position.length seen_hashes: dict[str, int] = {} if req_ids: assert len(req_ids) == num_requests else: req_ids = [f"{i}" for i in range(num_requests)] for i in range(num_requests): mm_features = [] for j, position in enumerate( mm_positions[i] if mm_positions is not None else [] ): if mm_hashes_list is not None: identifier = mm_hashes_list[i][j] # Verify if position length is identical position_length = position.length if identifier in seen_hashes: assert seen_hashes[identifier] == position_length, ( f"mm_hash '{identifier}' has inconsistent position lengths: " f"previously {seen_hashes[identifier]}, now {position_length} " f"at request {i}, position {j}" ) else: seen_hashes[identifier] = position_length else: # Unique dummy hash for each mm item identifier = f"hash{i}_{j}" mm_feature = MultiModalFeatureSpec( data=MultiModalKwargsItem.dummy("dummy_m"), mm_position=position, identifier=identifier, modality="image", ) mm_features.append(mm_feature) prompt_token_ids = [0] * num_tokens if same_prompt else [i] * num_tokens request = Request( request_id=req_ids[i], prompt_token_ids=prompt_token_ids, sampling_params=sampling_params, pooling_params=None, mm_features=mm_features if mm_features else None, eos_token_id=EOS_TOKEN_ID, block_hasher=block_hasher, ) requests.append(request) return requests
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/__init__.py
tests/v1/core/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_kv_cache_utils.py
tests/v1/core/test_kv_cache_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import importlib from collections.abc import Callable from typing import Any import pytest import torch import vllm.v1.core.kv_cache_utils as kv_cache_utils from vllm.config import ModelConfig, SchedulerConfig, VllmConfig from vllm.lora.request import LoRARequest from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams from vllm.utils.hashing import sha256, sha256_cbor from vllm.utils.mem_constants import GiB_bytes from vllm.v1.core.kv_cache_manager import KVCacheManager from vllm.v1.core.kv_cache_utils import ( BlockHash, FreeKVCacheBlockQueue, KVCacheBlock, estimate_max_model_len, generate_block_hash_extra_keys, generate_scheduler_kv_cache_config, get_kv_cache_configs, get_max_concurrency_for_kv_cache_config, get_request_block_hasher, hash_block_tokens, init_none_hash, is_kv_cache_spec_uniform, make_block_hash_with_group_id, tensor_data, ) from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheGroupSpec, KVCacheSpec, KVCacheTensor, MLAAttentionSpec, SlidingWindowSpec, UniformTypeKVCacheSpecs, ) from vllm.v1.metrics.stats import CachingMetrics, PrefixCacheStats from vllm.v1.request import Request pytestmark = pytest.mark.cpu_test @pytest.fixture(autouse=True) def _auto_init_hash_fn(request): hash_fn: Callable if "hash_fn" in request.fixturenames: hash_fn = request.getfixturevalue("hash_fn") else: hash_fn = sha256 init_none_hash(hash_fn) def make_request( request_id: str, prompt_token_ids: list[int] | None, block_size: int = 3, hash_fn: Callable = hash, mm_positions: list[PlaceholderRange] | None = None, mm_hashes: list[str] | None = None, cache_salt: str | None = None, prompt_embeds: torch.Tensor | None = None, ): mm_features = [] if mm_positions is not None: for j, position in enumerate(mm_positions): identifier = mm_hashes[j] if mm_hashes else f"hash_{j}" mm_feature = MultiModalFeatureSpec( data=MultiModalKwargsItem.dummy("dummy_m"), mm_position=position, identifier=identifier, modality="image", ) mm_features.append(mm_feature) return Request( request_id=request_id, prompt_token_ids=prompt_token_ids, mm_features=mm_features if mm_features else None, sampling_params=SamplingParams(max_tokens=17), pooling_params=None, eos_token_id=100, lora_request=None, cache_salt=cache_salt, block_hasher=get_request_block_hasher(block_size, hash_fn), prompt_embeds=prompt_embeds, ) def new_kv_cache_spec( block_size=16, num_kv_heads=2, head_size=64, dtype=torch.float32, sliding_window=None, ): return FullAttentionSpec( block_size=block_size, num_kv_heads=num_kv_heads, head_size=head_size, dtype=dtype, sliding_window=sliding_window, ) def new_sliding_window_spec( block_size=16, num_kv_heads=2, head_size=64, dtype=torch.float32, sliding_window=1 ): return SlidingWindowSpec( block_size=block_size, num_kv_heads=num_kv_heads, head_size=head_size, dtype=dtype, sliding_window=sliding_window, ) @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_none_hash(monkeypatch, hash_fn): import vllm.v1.core.kv_cache_utils # case 1: PYTHONHASHSEED is not set, use random with monkeypatch.context() as m: m.delenv("PYTHONHASHSEED", raising=False) reloaded_kv_cache_utils = importlib.reload(vllm.v1.core.kv_cache_utils) reloaded_kv_cache_utils.init_none_hash(hash_fn) assert reloaded_kv_cache_utils.NONE_HASH is not None assert isinstance(reloaded_kv_cache_utils.NONE_HASH, bytes) assert reloaded_kv_cache_utils.NONE_HASH != b"" # case 2: PYTHONHASHSEED is set, use the seed and hash_fn with monkeypatch.context() as m: m.setenv("PYTHONHASHSEED", "python hash seed") reloaded_kv_cache_utils = importlib.reload(vllm.v1.core.kv_cache_utils) reloaded_kv_cache_utils.init_none_hash(hash_fn) assert reloaded_kv_cache_utils.NONE_HASH is not None assert isinstance(reloaded_kv_cache_utils.NONE_HASH, bytes) assert hash_fn("python hash seed") == reloaded_kv_cache_utils.NONE_HASH def test_kv_cache_block(): # Test KVCacheBlock initialization block = KVCacheBlock(block_id=0) assert block.block_id == 0 assert block.ref_cnt == 0 assert block.block_hash is None # Test reference count manipulation block.ref_cnt += 1 assert block.ref_cnt == 1 block.ref_cnt -= 1 assert block.ref_cnt == 0 # Test block hash setting and resetting block_hash = make_block_hash_with_group_id(BlockHash(b"abc"), 0) block.block_hash = block_hash assert block.block_hash == block_hash block.reset_hash() assert block.block_hash is None def test_free_kv_cache_block_queue_initialization(): # Test with a single block block = KVCacheBlock(block_id=0) queue = FreeKVCacheBlockQueue([block]) assert queue.num_free_blocks == 1 assert queue.fake_free_list_head.next_free_block is block assert queue.fake_free_list_tail.prev_free_block is block def test_free_kv_cache_block_queue_operations(): # Create a list of KVCacheBlock objects blocks = [KVCacheBlock(block_id=i) for i in range(5)] # Create a FreeKVCacheBlockQueue with these blocks queue = FreeKVCacheBlockQueue(blocks) # Check initial state assert queue.num_free_blocks == 5 assert queue.fake_free_list_head.next_free_block is blocks[0] assert queue.fake_free_list_tail.prev_free_block is blocks[4] # Pop the first block block1 = queue.popleft() assert block1 == blocks[0] assert queue.num_free_blocks == 4 assert queue.fake_free_list_head.next_free_block is blocks[1] assert queue.fake_free_list_tail.prev_free_block is blocks[4] # Remove a block from the middle block_to_remove = blocks[2] queue.remove(block_to_remove) assert queue.num_free_blocks == 3 assert blocks[1].next_free_block is blocks[3] assert blocks[3].prev_free_block is blocks[1] # Append a block back queue.append(block_to_remove) assert queue.num_free_blocks == 4 assert queue.fake_free_list_tail.prev_free_block is block_to_remove assert block_to_remove.prev_free_block is blocks[4] assert block_to_remove.next_free_block is queue.fake_free_list_tail # Pop blocks until empty for _ in range(4): queue.popleft() assert queue.num_free_blocks == 0 assert queue.fake_free_list_head.next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is queue.fake_free_list_head # Attempt to pop from an empty queue with pytest.raises(ValueError) as e: queue.popleft() assert str(e.value) == "No free blocks available" def test_free_kv_cache_block_queue_append_n(): # Create an empty FreeKVCacheBlockQueue with these blocks queue = FreeKVCacheBlockQueue([]) blocks = [KVCacheBlock(block_id=i) for i in range(6)] # Append 0 block # fake_head->fake_tail queue.append_n([]) assert queue.num_free_blocks == 0 assert queue.fake_free_list_head.next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is queue.fake_free_list_head # Append 1 block # fake_head->b0->fake_tail queue.append_n(blocks[0:1]) assert queue.num_free_blocks == 1 assert queue.fake_free_list_head.next_free_block is blocks[0] assert blocks[0].prev_free_block is queue.fake_free_list_head assert blocks[0].next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is blocks[0] # Append 2 blocks # fake_head->b0->b4->b5->fake_tail queue.append_n(blocks[4:6]) assert queue.num_free_blocks == 3 assert queue.fake_free_list_head.next_free_block is blocks[0] assert blocks[0].prev_free_block is queue.fake_free_list_head assert blocks[0].next_free_block is blocks[4] assert blocks[4].prev_free_block is blocks[0] assert blocks[4].next_free_block is blocks[5] assert blocks[5].prev_free_block is blocks[4] assert blocks[5].next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is blocks[5] # Append 3 blocks # fake_head->b0->b4->b5->b1->b2->b3->fake_tail queue.append_n(blocks[1:4]) assert queue.num_free_blocks == 6 assert queue.fake_free_list_head.next_free_block is blocks[0] assert blocks[0].prev_free_block is queue.fake_free_list_head assert blocks[0].next_free_block is blocks[4] assert blocks[4].prev_free_block is blocks[0] assert blocks[4].next_free_block is blocks[5] assert blocks[5].prev_free_block is blocks[4] assert blocks[5].next_free_block is blocks[1] assert blocks[1].prev_free_block is blocks[5] assert blocks[1].next_free_block is blocks[2] assert blocks[2].prev_free_block is blocks[1] assert blocks[2].next_free_block is blocks[3] assert blocks[3].prev_free_block is blocks[2] assert blocks[3].next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is blocks[3] # Create an empty FreeKVCacheBlockQueue invalid_queue = FreeKVCacheBlockQueue([]) # set prev_free_block to None and this will cause assertation in append_n invalid_queue.fake_free_list_tail.prev_free_block = None with pytest.raises(AssertionError): # Append 1 block # fake_head->fake_tail invalid_queue.append_n(blocks[0:1]) assert invalid_queue.num_free_blocks == 0 assert ( invalid_queue.fake_free_list_head.next_free_block == invalid_queue.fake_free_list_tail ) def test_free_kv_cache_block_queue_popleft_n(): blocks = [KVCacheBlock(block_id=i) for i in range(6)] # Create an empty FreeKVCacheBlockQueue with these blocks queue = FreeKVCacheBlockQueue( [blocks[1], blocks[3], blocks[5], blocks[4], blocks[0], blocks[2]] ) assert queue.num_free_blocks == 6 assert queue.fake_free_list_head.next_free_block is blocks[1] assert blocks[1].prev_free_block is queue.fake_free_list_head assert blocks[1].next_free_block is blocks[3] assert blocks[3].prev_free_block is blocks[1] assert blocks[3].next_free_block is blocks[5] assert blocks[5].prev_free_block is blocks[3] assert blocks[5].next_free_block is blocks[4] assert blocks[4].prev_free_block is blocks[5] assert blocks[4].next_free_block is blocks[0] assert blocks[0].prev_free_block is blocks[4] assert blocks[0].next_free_block is blocks[2] assert blocks[2].prev_free_block is blocks[0] assert blocks[2].next_free_block is queue.fake_free_list_tail assert queue.fake_free_list_tail.prev_free_block is blocks[2] # Pop 0 block # fake_head->b1->b3->b5->b4->b0->b2->fake_tail assert len(queue.popleft_n(0)) == 0 assert queue.num_free_blocks == 6 # Pop 1 block # fake_head->b3->b5->b4->b0->b2->fake_tail result_blocks = queue.popleft_n(1) assert queue.num_free_blocks == 5 assert len(result_blocks) == 1 assert result_blocks[0] is blocks[1] for block in result_blocks: assert block.prev_free_block is None assert block.next_free_block is None # Pop 2 blocks # fake_head->b4->b0->b2->fake_tail result_blocks = queue.popleft_n(2) assert len(result_blocks) == 2 assert queue.num_free_blocks == 3 assert result_blocks[0] is blocks[3] assert result_blocks[1] is blocks[5] for block in result_blocks: assert block.prev_free_block is None assert block.next_free_block is None # Pop 3 blocks # fake_head->fake_tail result_blocks = queue.popleft_n(3) assert len(result_blocks) == 3 assert queue.num_free_blocks == 0 assert result_blocks[0] is blocks[4] assert result_blocks[1] is blocks[0] assert result_blocks[2] is blocks[2] for block in result_blocks: assert block.prev_free_block is None assert block.next_free_block is None def test_free_kv_cache_block_queue_get_all_free_blocks(): # Create a list of KVCacheBlock objects blocks = [KVCacheBlock(block_id=i) for i in range(5)] # Create a FreeKVCacheBlockQueue with these blocks queue = FreeKVCacheBlockQueue(blocks) # Check all blocks are correctly retrieved assert queue.get_all_free_blocks() == blocks # Pop a block and check again queue.popleft() assert queue.get_all_free_blocks() == blocks[1:] # Remove a block and check again block_to_remove = blocks[2] queue.remove(block_to_remove) assert queue.get_all_free_blocks() == blocks[1:2] + blocks[3:] # Append a block back and check again queue.append(block_to_remove) assert queue.get_all_free_blocks() == blocks[1:2] + blocks[3:] + [block_to_remove] def test_generate_block_hash_extra_keys(): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(20)], mm_positions=[ PlaceholderRange(offset=0, length=5), PlaceholderRange(offset=10, length=5), ], mm_hashes=["hash1", "hash2"], ) # Test with no extra keys extra_keys, next_mm_idx = generate_block_hash_extra_keys(request, 0, 5, 0) assert extra_keys == ("hash1",) assert next_mm_idx == 1 # Test with partial overlap extra_keys, next_mm_idx = generate_block_hash_extra_keys(request, 3, 8, 0) assert extra_keys == ("hash1",) assert next_mm_idx == 1 # Test with no overlap extra_keys, next_mm_idx = generate_block_hash_extra_keys(request, 6, 10, 0) assert extra_keys is None assert next_mm_idx == 1 # Test with multiple extra keys extra_keys, next_mm_idx = generate_block_hash_extra_keys(request, 0, 15, 0) assert extra_keys == ("hash1", "hash2") assert next_mm_idx == 2 def test_generate_block_hash_extra_keys_no_mm_inputs(): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], mm_positions=None, mm_hashes=None, ) extra_keys, next_mm_idx = generate_block_hash_extra_keys(request, 0, 5, 0) assert extra_keys is None assert next_mm_idx == 0 def test_generate_block_hash_extra_keys_cache_salt(): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], mm_positions=None, mm_hashes=None, cache_salt="salt", ) # salt is added for the first token extra_keys, _ = generate_block_hash_extra_keys(request, 0, 1, 0) assert extra_keys == ("salt",) extra_keys, _ = generate_block_hash_extra_keys(request, 0, 10, 0) assert extra_keys == ("salt",) # no salt added for other tokens extra_keys, _ = generate_block_hash_extra_keys(request, 1, 2, 0) assert extra_keys is None extra_keys, _ = generate_block_hash_extra_keys(request, 6, 10, 0) assert extra_keys is None # works together with other extra keys request_mm = make_request( request_id="0", prompt_token_ids=[_ for _ in range(20)], mm_positions=[ PlaceholderRange(offset=0, length=5), ], mm_hashes=["hash1"], cache_salt="salt", ) # Test with no extra keys extra_keys, next_mm_idx = generate_block_hash_extra_keys(request_mm, 0, 5, 0) assert extra_keys == ("hash1", "salt") assert next_mm_idx == 1 def test_generate_block_hash_extra_keys_prompt_embeds(): prompt_embeds = torch.randn(10, 3) request = make_request( request_id="0", prompt_token_ids=None, mm_positions=None, mm_hashes=None, prompt_embeds=prompt_embeds, ) # Test with prompt embeds for the first block extra_keys, _ = generate_block_hash_extra_keys(request, 0, 5, 0) expected_embeds = prompt_embeds[0:5] expected_bytes = kv_cache_utils.tensor_data(expected_embeds).tobytes() assert extra_keys == (expected_bytes,) # Test with prompt embeds for the second block extra_keys, _ = generate_block_hash_extra_keys(request, 5, 10, 0) expected_embeds = prompt_embeds[5:10] expected_bytes = kv_cache_utils.tensor_data(expected_embeds).tobytes() assert extra_keys == (expected_bytes,) def test_generate_block_hash_extra_keys_different_prompt_embeds(): prompt_embeds1 = torch.randn(10, 3) prompt_embeds2 = torch.randn(10, 3) request1 = make_request( request_id="0", prompt_token_ids=None, mm_positions=None, mm_hashes=None, prompt_embeds=prompt_embeds1, ) request2 = make_request( request_id="1", prompt_token_ids=None, mm_positions=None, mm_hashes=None, prompt_embeds=prompt_embeds2, ) extra_keys1, _ = generate_block_hash_extra_keys(request1, 0, 5, 0) extra_keys2, _ = generate_block_hash_extra_keys(request2, 0, 5, 0) assert extra_keys1 != extra_keys2 def test_generate_block_hash_extra_keys_lora(): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], ) request.lora_request = LoRARequest( lora_name="test_lora_adapter", lora_int_id=1, lora_path="/path/to/lora" ) extra_keys, _ = generate_block_hash_extra_keys(request, 0, 3, 0) assert extra_keys == ("test_lora_adapter",) request.lora_request = None extra_keys, _ = generate_block_hash_extra_keys(request, 0, 3, 0) assert extra_keys is None @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_hash_block_tokens(hash_fn): parent_block_hash = BlockHash(b"123") curr_block_token_ids = (1, 2, 3) extra_keys = ("key1", "key2") block_hash = hash_block_tokens( hash_fn, parent_block_hash, curr_block_token_ids, extra_keys ) expected = hash_fn((parent_block_hash, curr_block_token_ids, extra_keys)) assert block_hash == expected @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_request_block_hasher(hash_fn): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], block_size=3, hash_fn=hash_fn, mm_positions=[ PlaceholderRange(offset=0, length=3), PlaceholderRange(offset=3, length=3), ], mm_hashes=["hash1", "hash2"], ) block_hashes = request.block_hashes assert len(block_hashes) == 2 assert block_hashes[0] == hash_fn((kv_cache_utils.NONE_HASH, (0, 1, 2), ("hash1",))) assert block_hashes[1] == hash_fn((block_hashes[0], (3, 4, 5), ("hash2",))) @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_hash_tokens_different_mm_input(hash_fn): request1 = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], block_size=3, hash_fn=hash_fn, mm_positions=[ PlaceholderRange(offset=0, length=3), PlaceholderRange(offset=3, length=3), ], mm_hashes=["hash1", "hash2"], ) request2 = make_request( request_id="1", prompt_token_ids=[_ for _ in range(6)], mm_positions=[ PlaceholderRange(offset=0, length=3), PlaceholderRange(offset=3, length=3), ], mm_hashes=["hash3", "hash2"], ) block_hashes1 = request1.block_hashes block_hashes2 = request2.block_hashes assert block_hashes1[0] != block_hashes2[0] assert block_hashes1[1] != block_hashes2[1] @pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor]) def test_hash_request_tokens_no_mm_inputs(hash_fn): request = make_request( request_id="0", prompt_token_ids=[_ for _ in range(6)], block_size=3, hash_fn=hash_fn, mm_positions=None, mm_hashes=None, ) block_hashes = request.block_hashes assert len(block_hashes) == 2 assert block_hashes[0] == hash_fn((kv_cache_utils.NONE_HASH, (0, 1, 2), None)) assert block_hashes[1] == hash_fn((block_hashes[0], (3, 4, 5), None)) def _stats(requests: int, queries: int, hits: int) -> PrefixCacheStats: return PrefixCacheStats(requests=requests, queries=queries, hits=hits) def test_metrics(): """ Test the prefix caching metrics. """ metrics = CachingMetrics(max_recent_requests=5) assert metrics.hit_rate == 0.0 metrics.observe(_stats(1, 20, 9)) # 9 / 20 = 0.45 assert metrics.hit_rate == 0.45 metrics.observe(_stats(4, 80, 16)) # 25 / 100 = 0.25 assert metrics.hit_rate == 0.25 metrics.observe(_stats(1, 10, 2)) # Remove (20, 9) and add (10, 2): 18 / 90 = 0.2 assert metrics.aggregated_requests == 5 assert metrics.aggregated_query_total == 90 assert metrics.aggregated_query_hit == 18 assert metrics.hit_rate == 0.2 metrics.reset() assert metrics.hit_rate == 0.0 assert metrics.aggregated_requests == 0 assert metrics.aggregated_query_total == 0 assert metrics.aggregated_query_hit == 0 assert not metrics.query_queue def test_metrics_empty_stats(): """ Test the prefix caching metrics with empty stats. """ metrics = CachingMetrics(max_recent_requests=5) metrics.observe(_stats(0, 0, 0)) metrics.observe(_stats(1, 20, 9)) metrics.observe(_stats(0, 0, 0)) metrics.observe(_stats(4, 80, 16)) metrics.observe(_stats(0, 0, 0)) metrics.observe(_stats(1, 10, 2)) # Remove (20, 9) and add (10, 2): 18 / 90 = 0.2 assert metrics.aggregated_requests == 5 assert metrics.aggregated_query_total == 90 assert metrics.aggregated_query_hit == 18 assert metrics.hit_rate == 0.2 # Only the latest added stats preserved 10 / 20 = 0.5 metrics.observe(_stats(11, 20, 10)) assert metrics.aggregated_requests == 11 assert metrics.aggregated_query_total == 20 assert metrics.aggregated_query_hit == 10 assert metrics.hit_rate == 0.5 # Only the latest added stats preserved 30 / 40 = 0.75 metrics.observe(_stats(22, 40, 30)) assert metrics.aggregated_requests == 22 assert metrics.aggregated_query_total == 40 assert metrics.aggregated_query_hit == 30 assert metrics.hit_rate == 0.75 def test_get_kv_cache_configs_multiple_workers(): model_config = ModelConfig(max_model_len=16) vllm_config = VllmConfig(model_config=model_config) ref_kv_cache_spec = new_kv_cache_spec() same_kv_cache_specs = [ { "layer1": new_kv_cache_spec(), "layer2": new_kv_cache_spec(), }, { "layer1": new_kv_cache_spec(), "layer2": new_kv_cache_spec(), }, ] # Basic case. All things are the same. kv_cache_configs = get_kv_cache_configs( vllm_config, same_kv_cache_specs, [ ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), ] # Different available memory. This is the case for TP. # Use the smallest memory available. kv_cache_configs = get_kv_cache_configs( vllm_config, same_kv_cache_specs, [ ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 20, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), ] # Different KV cache specs. This is the case for PP. different_layer_specs = [ { "layer1": new_kv_cache_spec(), }, { "layer2": new_kv_cache_spec(), "layer3": new_kv_cache_spec(), }, ] # Different workers have different layers. kv_cache_configs = get_kv_cache_configs( vllm_config, different_layer_specs, [ ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1"], new_kv_cache_spec()), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer3"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer2", "layer3"], new_kv_cache_spec()), ], ), ] # Some layers are the same, some are different. This is the case for TP+PP tp_pp_kv_cache_specs = [ { "layer1": new_kv_cache_spec(), "layer2": new_kv_cache_spec(), }, { "layer1": new_kv_cache_spec(), "layer2": new_kv_cache_spec(), }, { "layer3": new_kv_cache_spec(), }, { "layer3": new_kv_cache_spec(), }, ] kv_cache_configs = get_kv_cache_configs( vllm_config, tp_pp_kv_cache_specs, [ ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer3"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer3"], ref_kv_cache_spec), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer3"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer3"], ref_kv_cache_spec), ], ), ] # Different workers have different types of layers. This is the case for # hybrid models + PP. different_type_layer_specs = [ { "layer1": new_kv_cache_spec(), "layer2": new_kv_cache_spec(), }, { "layer3": new_sliding_window_spec(), "layer4": new_sliding_window_spec(), }, ] kv_cache_configs = get_kv_cache_configs( vllm_config, different_type_layer_specs, [ ref_kv_cache_spec.page_size_bytes * 2 * 10, ref_kv_cache_spec.page_size_bytes * 2 * 10, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer2"] ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1", "layer2"], ref_kv_cache_spec), KVCacheGroupSpec([], new_sliding_window_spec()), ], ), KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer3"] ), KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer4"] ), ], kv_cache_groups=[ KVCacheGroupSpec([], ref_kv_cache_spec), KVCacheGroupSpec(["layer3", "layer4"], new_sliding_window_spec()), ], ), ] # When divided into multiple KVCacheGroups, need to ensure the number of # layers per group is similar. different_type_layer_specs = [ { "layer1": new_kv_cache_spec(), "layer2": new_sliding_window_spec(), "layer3": new_sliding_window_spec(), }, { "layer4": new_kv_cache_spec(), "layer5": new_sliding_window_spec(), "layer6": new_sliding_window_spec(), }, ] kv_cache_configs = get_kv_cache_configs( vllm_config, different_type_layer_specs, [ ref_kv_cache_spec.page_size_bytes * 10, ref_kv_cache_spec.page_size_bytes * 10, ], ) assert kv_cache_configs == [ KVCacheConfig( num_blocks=10, kv_cache_tensors=[ KVCacheTensor( size=ref_kv_cache_spec.page_size_bytes * 10, shared_by=["layer1", "layer2", "layer3"], ), ], kv_cache_groups=[ KVCacheGroupSpec(["layer1"], ref_kv_cache_spec),
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_single_type_kv_cache_manager.py
tests/v1/core/test_single_type_kv_cache_manager.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import pytest import torch from vllm.v1.core.block_pool import BlockPool from vllm.v1.core.kv_cache_utils import ( BlockHash, KVCacheBlock, make_block_hash_with_group_id, ) from vllm.v1.core.single_type_kv_cache_manager import ( ChunkedLocalAttentionManager, SlidingWindowManager, ) from vllm.v1.kv_cache_interface import ChunkedLocalAttentionSpec, SlidingWindowSpec pytestmark = pytest.mark.cpu_test def get_sliding_window_manager(sliding_window_spec, block_pool, enable_caching=True): return SlidingWindowManager( sliding_window_spec, block_pool, enable_caching=enable_caching, kv_cache_group_id=0, ) def get_chunked_local_attention_manager( chunked_local_attention_spec, block_pool, enable_caching=True ): return ChunkedLocalAttentionManager( chunked_local_attention_spec, block_pool, enable_caching=enable_caching, kv_cache_group_id=0, ) def test_chunked_local_attention_possible_cached_prefix(): block_size = 2 chunked_local_attention_spec = ChunkedLocalAttentionSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, attention_chunk_size=4, ) block_pool = BlockPool( num_gpu_blocks=100, enable_caching=True, hash_block_size=block_size ) manager = get_chunked_local_attention_manager( chunked_local_attention_spec, block_pool ) def run_one_case(block_is_cached, tail_token, expect_length): block_hash_list = [ BlockHash(str(i).encode()) for i in range(len(block_is_cached)) ] block_pool.cached_block_hash_to_block._cache.clear() # Mock the block pool with the cached blocks for i, (block_hash, is_cached) in enumerate( zip(block_hash_list, block_is_cached) ): if is_cached: block_pool.cached_block_hash_to_block.insert( make_block_hash_with_group_id(block_hash, 0), block_pool.blocks[i + 10], ) computed_blocks = manager.find_longest_cache_hit( block_hashes=block_hash_list, max_length=len(block_hash_list) * block_size + tail_token, kv_cache_group_ids=[0], block_pool=block_pool, kv_cache_spec=chunked_local_attention_spec, use_eagle=False, alignment_tokens=block_size, )[0] assert len(computed_blocks) == expect_length assert all( block == block_pool.null_block for block in computed_blocks[: (expect_length - 1) // 2] ) run_one_case([True], 0, 1) run_one_case([True], 1, 1) run_one_case([True, False], 0, 2) run_one_case([True, False], 1, 2) run_one_case([True, True], 0, 2) run_one_case([True, True], 1, 2) run_one_case([True, True, False], 0, 2) run_one_case([True, True, False], 1, 2) run_one_case([True, True, True], 0, 3) run_one_case([True, True, True], 1, 3) run_one_case([True, True, True, False], 0, 4) run_one_case([True, True, True, False], 1, 4) run_one_case([random.choice([True, False])] * 8 + [True], 1, 9) run_one_case([random.choice([True, False])] * 8 + [False], 1, 8) run_one_case([random.choice([True, False])] * 8 + [True, True], 1, 10) run_one_case([random.choice([True, False])] * 8 + [True, False], 0, 10) run_one_case([random.choice([True, False])] * 8 + [True, False], 1, 10) run_one_case([random.choice([True, False])] * 8 + [False, True], 0, 10) run_one_case([random.choice([True, False])] * 8 + [False, True], 1, 10) run_one_case([random.choice([True, False])] * 8 + [False, False], 0, 10) run_one_case([random.choice([True, False])] * 8 + [False, False], 1, 10) def test_sliding_window_possible_cached_prefix(): block_size = 2 sliding_window_spec = SlidingWindowSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, sliding_window=4, ) block_pool = BlockPool( num_gpu_blocks=100, enable_caching=True, hash_block_size=block_size ) manager = get_sliding_window_manager(sliding_window_spec, block_pool) def run_one_case(block_is_cached, expect_length): block_hash_list = [ BlockHash(str(i).encode()) for i in range(len(block_is_cached)) ] block_pool.cached_block_hash_to_block._cache.clear() # Mock the block pool with the cached blocks for i, (block_hash, is_cached) in enumerate( zip(block_hash_list, block_is_cached) ): if is_cached: block_pool.cached_block_hash_to_block.insert( make_block_hash_with_group_id(block_hash, 0), block_pool.blocks[i + 10], ) computed_blocks = manager.find_longest_cache_hit( block_hashes=block_hash_list, max_length=len(block_hash_list) * block_size, kv_cache_group_ids=[0], block_pool=block_pool, kv_cache_spec=sliding_window_spec, use_eagle=False, alignment_tokens=block_size, )[0] assert len(computed_blocks) == expect_length assert all( block == block_pool.null_block for block in computed_blocks[: expect_length - 2] ) for i in range(2): if i < expect_length: block_index = expect_length - i - 1 assert computed_blocks[block_index].block_id == block_index + 10 run_one_case([False] * 10, 0) run_one_case([True], 1) run_one_case([True, False], 1) run_one_case([True, True], 2) run_one_case([True, True, False], 2) run_one_case([True, True, True], 3) run_one_case([True, True, True, False], 3) run_one_case( [True, True, False, True, False, False, True, True, False, True, True, True], 12 ) run_one_case( [True, True, False, True, False, False, True, True, False, False, False], 8 ) run_one_case( [True, True, False, True, False, False, True, True, False, False, False, True], 8, ) def test_chunked_local_attention_remove_skipped_blocks(): attention_spec = ChunkedLocalAttentionSpec( block_size=2, num_kv_heads=1, head_size=1, dtype=torch.float32, attention_chunk_size=4, ) block_pool = BlockPool(num_gpu_blocks=2000, enable_caching=True, hash_block_size=2) manager = get_chunked_local_attention_manager(attention_spec, block_pool) null_block_id = block_pool.null_block.block_id def id_to_block_table(ids) -> list[KVCacheBlock]: return [ KVCacheBlock(id_) if id_ != null_block_id else block_pool.null_block for id_ in ids ] def assert_block_id(block_table: list[KVCacheBlock], ids: list[int]): for block, id_ in zip(block_table, ids): if id_ == null_block_id: assert block == block_pool.null_block else: assert block.block_id == id_ original_block_ids = [ 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, ] block_table = id_to_block_table(original_block_ids) manager.req_to_blocks["test"] = block_table manager.remove_skipped_blocks("test", 0) assert_block_id(block_table, original_block_ids) # For 4th token (0-indexed), token 0-3 is out of the local attention window. manager.remove_skipped_blocks("test", 4) assert_block_id(block_table, [null_block_id] * 2) # For 6th token (0-indexed), token 4 - 6 are in local attention window, # token 0 - 3 are out, 2 blocks can be removed. manager.remove_skipped_blocks("test", 6) assert_block_id(block_table, [null_block_id] * 2 + original_block_ids[2:]) # For 12th token (0-indexed), # token 0-11 are out, 6 block can be removed. manager.remove_skipped_blocks("test", 12) assert_block_id(block_table, [null_block_id] * 6) def test_sliding_window_remove_skipped_blocks(): sliding_window_spec = SlidingWindowSpec( block_size=2, num_kv_heads=1, head_size=1, dtype=torch.float32, sliding_window=4, ) block_pool = BlockPool(num_gpu_blocks=2000, enable_caching=True, hash_block_size=2) manager = get_sliding_window_manager(sliding_window_spec, block_pool) null_block_id = block_pool.null_block.block_id def id_to_block_table(ids) -> list[KVCacheBlock]: return [ KVCacheBlock(id_) if id_ != null_block_id else block_pool.null_block for id_ in ids ] def assert_block_id(block_table: list[KVCacheBlock], ids: list[int]): for block, id_ in zip(block_table, ids): if id_ == null_block_id: assert block == block_pool.null_block else: assert block.block_id == id_ original_block_ids = [ 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, ] block_table = id_to_block_table(original_block_ids) manager.req_to_blocks["test"] = block_table manager.remove_skipped_blocks("test", 0) assert_block_id(block_table, original_block_ids) # 4 tokens are computed. Only token 0 is out of the sliding window. As # block 1000 also contains token 1 that is in the sliding window, block 1000 # cannot be removed. manager.remove_skipped_blocks("test", 4) assert_block_id(block_table, original_block_ids) # 5 tokens are computed. Token 0 & 1 are out of the sliding window. # Block 1000 can be removed. manager.remove_skipped_blocks("test", 5) assert_block_id(block_table, [null_block_id] + original_block_ids[1:]) # 6 tokens are computed. Token 0-2 are out of the sliding window. # Cannot remove new block as the block 1001 is still used by token 3. manager.remove_skipped_blocks("test", 6) assert_block_id(block_table, [null_block_id] + original_block_ids[1:]) # 7 tokens are computed. Token 0-3 are out of the sliding window. # Block 1001 can be removed and block 1000 is already removed. manager.remove_skipped_blocks("test", 7) assert_block_id(block_table, [null_block_id] * 2 + original_block_ids[2:]) # 11 tokens are computed. Token 0-7 are out of the sliding window. # Block 1002 & 1003 can be removed now. Block 1003 represents a longer # sequence, and is expected to be evicted earlier than 1002, so the order # of removed blocks should be [1003, 1002]. manager.remove_skipped_blocks("test", 11) assert_block_id(block_table, [null_block_id] * 4 + original_block_ids[4:]) def test_get_num_blocks_to_allocate(): block_size = 2 sliding_window_spec = SlidingWindowSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, sliding_window=4, # Placeholder value, not related to test result ) block_pool = BlockPool( num_gpu_blocks=100, enable_caching=True, hash_block_size=block_size ) manager = get_sliding_window_manager(sliding_window_spec, block_pool) cached_blocks_1 = [KVCacheBlock(i + 1) for i in range(10)] cached_blocks_2 = [block_pool.null_block for _ in range(5)] + [ KVCacheBlock(i + 1) for i in range(5) ] assert ( manager.get_num_blocks_to_allocate("1", 20 * block_size, cached_blocks_1, 0) == 20 ) assert ( manager.get_num_blocks_to_allocate("2", 20 * block_size, cached_blocks_2, 0) == 15 ) def test_evictable_cached_blocks_not_double_allocated(): block_size = 2 sliding_window_length = 2 * block_size sliding_window_spec = SlidingWindowSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, sliding_window=sliding_window_length, ) block_pool = BlockPool( num_gpu_blocks=100, enable_caching=True, hash_block_size=block_size ) manager = get_sliding_window_manager(sliding_window_spec, block_pool) request_id = "req" evictable_block = block_pool.blocks[1] # ref_cnt == 0, eviction candidate num_blocks_to_allocate = manager.get_num_blocks_to_allocate( request_id=request_id, num_tokens=2 * block_size, new_computed_blocks=[evictable_block], total_computed_tokens=block_size, ) # Free capacity check should count evictable cached blocks, but allocation # should only allocate the truly new block. assert num_blocks_to_allocate == 2 manager.allocate_new_computed_blocks( request_id, [evictable_block], num_local_computed_tokens=block_size, num_external_computed_tokens=0, ) new_blocks = manager.allocate_new_blocks(request_id, num_tokens=4) assert len(new_blocks) == 1 assert len(manager.req_to_blocks[request_id]) == 2 def test_chunked_local_attention_get_num_blocks_to_allocate(): block_size = 2 attention_spec = ChunkedLocalAttentionSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, attention_chunk_size=4, # Placeholder value, not related to test result ) block_pool = BlockPool( num_gpu_blocks=100, enable_caching=True, hash_block_size=block_size ) manager = get_chunked_local_attention_manager(attention_spec, block_pool) cached_blocks_1 = [KVCacheBlock(i + 1) for i in range(10)] cached_blocks_2 = [block_pool.null_block for _ in range(5)] + [ KVCacheBlock(i + 1) for i in range(5) ] assert ( manager.get_num_blocks_to_allocate("1", 20 * block_size, cached_blocks_1, 0) == 20 ) assert ( manager.get_num_blocks_to_allocate("2", 20 * block_size, cached_blocks_2, 0) == 15 )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_kv_sharing.py
tests/v1/core/test_kv_sharing.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from vllm.v1.kv_cache_interface import FullAttentionSpec, KVCacheGroupSpec from vllm.v1.worker.utils import add_kv_sharing_layers_to_kv_cache_groups pytestmark = pytest.mark.cpu_test def new_kv_cache_spec(): return FullAttentionSpec(16, 1, 1, torch.float32, False) def test_initialize_kv_cache_for_kv_sharing_different_attn_groups(): """ Test initializing KV cache sharing with different attention groups. Layers in the same KV cache group might be placed in different attn groups if they have different attention backends. """ shared_kv_cache_layers = { "model.layers.2": "model.layers.0", "model.layers.3": "model.layers.1", } # Layers 0 and 1 both belong in KV cache group 0 # However, if they have different attention backends, they will be # placed in different attention groups for KV cache group 0 kv_cache_groups = [ KVCacheGroupSpec(["model.layers.0", "model.layers.1"], new_kv_cache_spec()), ] add_kv_sharing_layers_to_kv_cache_groups( shared_kv_cache_layers=shared_kv_cache_layers, kv_cache_groups=kv_cache_groups, ) # Check that the layers were added to the correct KV cache group assert len(kv_cache_groups) == 1 assert kv_cache_groups[0].layer_names == [ "model.layers.0", "model.layers.1", "model.layers.2", "model.layers.3", ] def test_initialize_kv_cache_for_kv_sharing_same_attn_groups(): """ Test case assuming that all layers in the same KV cache group have the same attention backends. This is true for most models. """ shared_kv_cache_layers = { "model.layers.2": "model.layers.0", "model.layers.3": "model.layers.1", } kv_cache_groups = [ KVCacheGroupSpec(["model.layers.0", "model.layers.1"], new_kv_cache_spec()), ] add_kv_sharing_layers_to_kv_cache_groups( shared_kv_cache_layers=shared_kv_cache_layers, kv_cache_groups=kv_cache_groups, ) # Check that the layers were added to the correct KV cache group assert len(kv_cache_groups) == 1 assert kv_cache_groups[0].layer_names == [ "model.layers.0", "model.layers.1", "model.layers.2", "model.layers.3", ] def test_initialize_kv_cache_for_kv_sharing_no_attn_groups(): """ Test KV sharing set up when no attention groups are provided. This is the case for the TPU model runner, which doesn't have support for attention groups yet. """ shared_kv_cache_layers = { "model.layers.2": "model.layers.0", "model.layers.3": "model.layers.1", } kv_cache_groups = [ KVCacheGroupSpec(["model.layers.0"], new_kv_cache_spec()), KVCacheGroupSpec(["model.layers.1"], new_kv_cache_spec()), ] add_kv_sharing_layers_to_kv_cache_groups( shared_kv_cache_layers=shared_kv_cache_layers, kv_cache_groups=kv_cache_groups, ) # Check that the layers were added to the correct KV cache group assert len(kv_cache_groups) == 2 assert kv_cache_groups[0].layer_names == ["model.layers.0", "model.layers.2"] assert kv_cache_groups[1].layer_names == ["model.layers.1", "model.layers.3"]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_scheduler.py
tests/v1/core/test_scheduler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import dataclasses from unittest.mock import Mock import pytest import torch from vllm.config import ( CacheConfig, ECTransferConfig, KVTransferConfig, ModelConfig, SchedulerConfig, SpeculativeConfig, VllmConfig, ) from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams, StructuredOutputsParams from vllm.utils.hashing import sha256 from vllm.v1.core.encoder_cache_manager import EncoderCacheManager from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash from vllm.v1.core.sched.output import CachedRequestData, SchedulerOutput from vllm.v1.core.sched.scheduler import Scheduler from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheGroupSpec, ) from vllm.v1.outputs import DraftTokenIds, KVConnectorOutput, ModelRunnerOutput from vllm.v1.request import Request, RequestStatus from vllm.v1.structured_output import StructuredOutputManager from .utils import EOS_TOKEN_ID, create_requests, create_scheduler, mock_kv pytestmark = pytest.mark.cpu_test def test_add_requests(): scheduler = create_scheduler() requests = create_requests(num_requests=10) for i, request in enumerate(requests): scheduler.add_request(request) assert request.request_id in scheduler.requests assert len(scheduler.waiting) == i + 1 def test_finish_request(): scheduler = create_scheduler() requests = create_requests(num_requests=10) for request in requests: scheduler.add_request(request) for i, request in enumerate(requests): scheduler.finish_requests(request.request_id, RequestStatus.FINISHED_ABORTED) assert request.request_id not in scheduler.requests assert len(scheduler.waiting) == 9 - i def test_get_num_unfinished_requests(): scheduler = create_scheduler() requests = create_requests(num_requests=10) for request in requests: scheduler.add_request(request) for i, request in enumerate(requests): scheduler.finish_requests(request.request_id, RequestStatus.FINISHED_STOPPED) assert scheduler.get_num_unfinished_requests() == len(requests) - i - 1 @pytest.mark.parametrize( "enable_prefix_caching, prompt_logprobs", [ (False, None), (True, 5), ], ) def test_schedule(enable_prefix_caching: bool, prompt_logprobs: int | None): """Test scheduling. Two cases: default APC/no prompt logprobs; APC=True + prompt logprobs """ scheduler = create_scheduler(enable_prefix_caching=enable_prefix_caching) requests = create_requests(num_requests=10, prompt_logprobs=prompt_logprobs) for request in requests: scheduler.add_request(request) # Test initial scheduling output = scheduler.schedule() assert len(output.scheduled_new_reqs) == len(requests) assert output.scheduled_cached_reqs.num_reqs == 0 assert len(output.finished_req_ids) == 0 # Verify all requests are scheduled. for req_id, num_tokens in output.num_scheduled_tokens.items(): assert num_tokens == len(requests[int(req_id)].prompt_token_ids) # Verify requests moved from waiting to running assert len(scheduler.waiting) == 0 assert len(scheduler.running) == len(requests) for i, request in enumerate(requests): assert scheduler.running[i] == request def test_schedule_multimodal_requests(): scheduler = create_scheduler(model="llava-hf/llava-1.5-7b-hf") mm_positions = [[PlaceholderRange(offset=i, length=100)] for i in range(10)] requests = create_requests( num_requests=10, num_tokens=200, mm_positions=mm_positions, ) for request in requests: scheduler.add_request(request) output = scheduler.schedule() assert len(output.scheduled_new_reqs) == len(requests) assert output.scheduled_cached_reqs.num_reqs == 0 assert len(output.finished_req_ids) == 0 for req_id, num_tokens in output.num_scheduled_tokens.items(): assert num_tokens == len(requests[int(req_id)].prompt_token_ids) assert len(output.scheduled_encoder_inputs) == 10 for req_id, encoder_input in output.scheduled_encoder_inputs.items(): assert len(encoder_input) == 1 def test_schedule_partial_requests(): """Test scheduling behavior with partial requests. This test verifies that: 1. The scheduler can handle multiple partial requests in a single step when constrained by encoder budget. 2. A request in RUNNING state may be unscheduled in subsequent steps if there is insufficient encoder budget. """ scheduler = create_scheduler( model="llava-hf/llava-1.5-7b-hf", max_num_batched_tokens=1024, ) mm_positions = [[PlaceholderRange(offset=100, length=600)] for _ in range(3)] requests = create_requests( num_requests=3, num_tokens=800, mm_positions=mm_positions, ) for request in requests: scheduler.add_request(request) output = scheduler.schedule() assert len(output.scheduled_new_reqs) == 3 assert output.scheduled_cached_reqs.num_reqs == 0 assert len(output.finished_req_ids) == 0 assert scheduler.max_num_encoder_input_tokens == 1024 # The first request is scheduled fully. assert output.num_scheduled_tokens[requests[0].request_id] == 800 # The second request is scheduled partially. # The <img> tokens are not scheduled because of the encoder budget. assert output.num_scheduled_tokens[requests[1].request_id] == 100 # The third request is also scheduled partially. # The <img> tokens are not scheduled because of the encoder budget. assert output.num_scheduled_tokens[requests[2].request_id] == 100 req_to_index = {request.request_id: i for i, request in enumerate(requests)} model_runner_output = ModelRunnerOutput( req_ids=[request.request_id for request in requests], req_id_to_index=req_to_index, # Only the first request has a sampled token id because # the rest requests are still being prefilled. sampled_token_ids=[[0], [], []], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(output, model_runner_output) # Schedule the next step. # Only the first and second requests are scheduled. # The third request is in the RUNNING state but not scheduled in this step # because of the encoder budget. output = scheduler.schedule() assert len(scheduler.running) == 3 assert len(output.scheduled_new_reqs) == 0 assert output.scheduled_cached_reqs.num_reqs == 2 assert len(output.finished_req_ids) == 0 assert output.num_scheduled_tokens[requests[0].request_id] == 1 assert output.num_scheduled_tokens[requests[1].request_id] == 700 assert requests[2].request_id not in output.num_scheduled_tokens def test_no_mm_input_chunking(): # Disable multimodal input chunking. scheduler = create_scheduler( model="llava-hf/llava-1.5-7b-hf", max_num_batched_tokens=1024, disable_chunked_mm_input=True, max_model_len=2048, ) mm_positions = [[PlaceholderRange(offset=400, length=800)]] requests = create_requests( num_requests=1, num_tokens=1200, mm_positions=mm_positions ) for request in requests: scheduler.add_request(request) output = scheduler.schedule() assert len(output.scheduled_new_reqs) == 1 assert output.scheduled_cached_reqs.num_reqs == 0 assert len(output.finished_req_ids) == 0 # We want to only see the 400 text tokens at the start scheduled assert output.num_scheduled_tokens[requests[0].request_id] == 400 req_to_index = {request.request_id: i for i, request in enumerate(requests)} model_runner_output = ModelRunnerOutput( req_ids=[request.request_id for request in requests], req_id_to_index=req_to_index, sampled_token_ids=[[] for _ in range(len(requests))], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(output, model_runner_output) output = scheduler.schedule() assert len(scheduler.running) == 1 assert len(output.scheduled_new_reqs) == 0 assert output.scheduled_cached_reqs.num_reqs == 1 assert len(output.finished_req_ids) == 0 assert output.num_scheduled_tokens[requests[0].request_id] == 800 # Test that we fail if we disable chunked mm input and use too small # of a max_num_batched_tokens for the mm input. with pytest.raises(ValueError): _ = create_scheduler( model="llava-hf/llava-1.5-7b-hf", max_num_batched_tokens=100, disable_chunked_mm_input=True, ) @pytest.mark.parametrize("enable_prefix_caching", [True, False]) def test_schedule_concurrent_partial_requests(enable_prefix_caching: bool): """Test scheduling behavior with concurrent partial requests. This test verifies that: there are multiple long prefill requests in the RUNNING state, and we can schedule them together. """ scheduler = create_scheduler( model="facebook/opt-125m", max_num_batched_tokens=1024, long_prefill_token_threshold=400, enable_prefix_caching=enable_prefix_caching, ) requests = create_requests( num_requests=3, num_tokens=800, ) for request in requests: scheduler.add_request(request) output = scheduler.schedule() assert len(output.scheduled_new_reqs) == 3 assert output.scheduled_cached_reqs.num_reqs == 0 assert len(output.finished_req_ids) == 0 # The first request is scheduled partially - 400. assert output.num_scheduled_tokens[requests[0].request_id] == 400 # The second request is scheduled partially - 400. assert output.num_scheduled_tokens[requests[1].request_id] == 400 # The third request is also scheduled partially - 1024 - 400 - 400 = 224. assert output.num_scheduled_tokens[requests[2].request_id] == 224 req_to_index = {request.request_id: i for i, request in enumerate(requests)} model_runner_output = ModelRunnerOutput( req_ids=[request.request_id for request in requests], req_id_to_index=req_to_index, sampled_token_ids=[[] for _ in range(len(requests))], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(output, model_runner_output) # Schedule the next step. All three requests are running. # Processed the remaining prefills of the first and second requests. output1 = scheduler.schedule() assert len(scheduler.running) == 3 assert len(output1.scheduled_new_reqs) == 0 assert output1.scheduled_cached_reqs.num_reqs == 3 assert len(output1.finished_req_ids) == 0 assert output1.num_scheduled_tokens[requests[0].request_id] == 400 assert output1.num_scheduled_tokens[requests[1].request_id] == 400 assert output1.num_scheduled_tokens[requests[2].request_id] == 224 # Schedule the third step. All three requests are running. # First and second requests are in the decode stage. # All the remaining tokens in the third request are processed. model_runner_output = ModelRunnerOutput( req_ids=[request.request_id for request in requests], req_id_to_index=req_to_index, sampled_token_ids=[[0], [0]] + [[] for _ in range(len(requests) - 2)], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(output1, model_runner_output) output2 = scheduler.schedule() assert len(scheduler.running) == 3 assert len(output2.scheduled_new_reqs) == 0 assert output2.scheduled_cached_reqs.num_reqs == 3 assert len(output2.finished_req_ids) == 0 assert output2.num_scheduled_tokens[requests[0].request_id] == 1 assert output2.num_scheduled_tokens[requests[1].request_id] == 1 assert output2.num_scheduled_tokens[requests[2].request_id] == 800 - 224 - 224 def test_stop_via_update_from_output(): """Test stopping behavior through update_from_output""" scheduler = create_scheduler(num_speculative_tokens=1) # Test case 1: Stop on EOS token requests = create_requests(num_requests=2, max_tokens=10) for req in requests: req.num_computed_tokens = req.num_tokens scheduler.requests[req.request_id] = req scheduler.running.append(req) req.status = RequestStatus.RUNNING scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={requests[0].request_id: 1, requests[1].request_id: 2}, total_num_scheduled_tokens=3, scheduled_encoder_inputs={}, scheduled_spec_decode_tokens={ requests[0].request_id: [], requests[1].request_id: [10], }, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_output = ModelRunnerOutput( req_ids=[req.request_id for req in requests], req_id_to_index={req.request_id: i for i, req in enumerate(requests)}, sampled_token_ids=[ [EOS_TOKEN_ID], [10, 11], ], # First request hits EOS, second continues logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output, model_output) # Verify first request stopped, second continues assert len(scheduler.running) == 1 assert scheduler.running[0].request_id == requests[1].request_id assert requests[0].status == RequestStatus.FINISHED_STOPPED assert requests[0].request_id in scheduler.finished_req_ids assert list(requests[0].output_token_ids) == [EOS_TOKEN_ID] assert list(requests[1].output_token_ids) == [10, 11] # Test case 2: Stop on custom stop token scheduler = create_scheduler(num_speculative_tokens=2) requests = create_requests(num_requests=2, max_tokens=10, stop_token_ids=[42, 43]) for req in requests: req.num_computed_tokens = req.num_tokens scheduler.requests[req.request_id] = req scheduler.running.append(req) req.status = RequestStatus.RUNNING scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={requests[0].request_id: 3, requests[1].request_id: 2}, total_num_scheduled_tokens=5, scheduled_encoder_inputs={}, scheduled_spec_decode_tokens={ requests[0].request_id: [10, 42], requests[1].request_id: [13], }, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_output = ModelRunnerOutput( req_ids=[req.request_id for req in requests], req_id_to_index={req.request_id: i for i, req in enumerate(requests)}, sampled_token_ids=[[10, 42, 12], [13, 14]], # First request hits stop token logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output, model_output) # Verify first request stopped on custom token assert len(scheduler.running) == 1 assert scheduler.running[0].request_id == requests[1].request_id assert requests[0].status == RequestStatus.FINISHED_STOPPED assert requests[0].stop_reason == 42 assert requests[0].request_id in scheduler.finished_req_ids assert list(requests[0].output_token_ids) == [10, 42] assert list(requests[1].output_token_ids) == [13, 14] # Test case 3: Stop on max tokens scheduler = create_scheduler(num_speculative_tokens=2) requests = create_requests(num_requests=2, max_tokens=2) for req in requests: req.num_computed_tokens = req.num_tokens scheduler.requests[req.request_id] = req scheduler.running.append(req) req.status = RequestStatus.RUNNING scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={requests[0].request_id: 3, requests[1].request_id: 1}, total_num_scheduled_tokens=4, scheduled_encoder_inputs={}, scheduled_spec_decode_tokens={ requests[0].request_id: [10, 11], requests[1].request_id: [], }, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_output = ModelRunnerOutput( req_ids=[req.request_id for req in requests], req_id_to_index={req.request_id: i for i, req in enumerate(requests)}, sampled_token_ids=[[10, 11, 12], [13]], # First request exceeds max_tokens logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output, model_output) # Verify first request stopped due to length assert len(scheduler.running) == 1 assert scheduler.running[0].request_id == requests[1].request_id assert requests[0].status == RequestStatus.FINISHED_LENGTH_CAPPED assert requests[0].request_id in scheduler.finished_req_ids assert list(requests[0].output_token_ids) == [10, 11] # Truncated to max_tokens assert list(requests[1].output_token_ids) == [13] # Test case 4: Ignore EOS flag scheduler = create_scheduler(num_speculative_tokens=2) requests = create_requests(num_requests=1, max_tokens=10) requests[0].sampling_params.ignore_eos = True requests[0].num_computed_tokens = requests[0].num_tokens scheduler.requests[requests[0].request_id] = requests[0] scheduler.running.append(requests[0]) scheduler_output = SchedulerOutput( scheduled_new_reqs=[], scheduled_cached_reqs=CachedRequestData.make_empty(), num_scheduled_tokens={requests[0].request_id: 3}, total_num_scheduled_tokens=3, scheduled_encoder_inputs={}, scheduled_spec_decode_tokens={requests[0].request_id: [EOS_TOKEN_ID, 10]}, num_common_prefix_blocks=[], finished_req_ids=set(), free_encoder_mm_hashes=[], ) model_output = ModelRunnerOutput( req_ids=[requests[0].request_id], req_id_to_index={requests[0].request_id: 0}, sampled_token_ids=[[EOS_TOKEN_ID, 10, 11]], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output, model_output) # Verify request continues past EOS assert len(scheduler.running) == 1 assert not requests[0].is_finished() assert list(requests[0].output_token_ids) == [EOS_TOKEN_ID, 10, 11] def test_check_stop_min_tokens(): """Test that requests don't stop when min_tokens requirement isn't met.""" from vllm.v1.core.sched.utils import check_stop # Test case 1: num_output_tokens < min_tokens # Should return False (don't stop) sampling_params = SamplingParams( ignore_eos=False, max_tokens=20, min_tokens=5, ) request = Request( request_id="0", prompt_token_ids=[0, 1, 2], sampling_params=sampling_params, pooling_params=None, eos_token_id=EOS_TOKEN_ID, ) # Simulate having generated 3 output tokens (less than min_tokens=5) request.append_output_token_ids([10, 11, EOS_TOKEN_ID]) # EOS token present result = check_stop(request, max_model_len=100) assert result is False, "Should not stop when num_output_tokens<min_tokens" # Test case 2: num_output_tokens >= min_tokens # Should follow normal stopping logic (stop on EOS) request.append_output_token_ids( [ 10, 11, 12, 13, 14, EOS_TOKEN_ID, ] ) # 6 tokens > min_tokens result = check_stop(request, max_model_len=100) assert result is True, "Should stop on EOS when min_tokens met" assert request.status == RequestStatus.FINISHED_STOPPED # Test case 3: min_tokens = 0, should follow normal stopping logic sampling_params_no_min = SamplingParams( ignore_eos=False, max_tokens=20, min_tokens=0, ) request_no_min = Request( request_id="1", prompt_token_ids=[0, 1, 2], sampling_params=sampling_params_no_min, pooling_params=None, eos_token_id=EOS_TOKEN_ID, ) request_no_min.append_output_token_ids([10, EOS_TOKEN_ID]) result = check_stop(request_no_min, max_model_len=100) assert result is True, "Should stop on EOS when min_tokens=0" assert request_no_min.status == RequestStatus.FINISHED_STOPPED # Test case 4: min_tokens > 0 with stop token (not EOS) sampling_params_stop = SamplingParams( ignore_eos=False, max_tokens=20, min_tokens=5, stop_token_ids=[42], ) request_stop = Request( request_id="2", prompt_token_ids=[0, 1, 2], sampling_params=sampling_params_stop, pooling_params=None, eos_token_id=EOS_TOKEN_ID, ) # Only 3 output tokens, less than min_tokens=5, but has stop token request_stop.append_output_token_ids([10, 11, 42]) result = check_stop(request_stop, max_model_len=100) assert result is False, "Should not stop when num_output_tokens<min_tokens" # Test case 5: min_tokens met, should stop on stop token request_stop.append_output_token_ids( [10, 11, 12, 13, 14, 42] ) # 6 tokens >= min_tokens=5 result = check_stop(request_stop, max_model_len=100) assert result is True, "Should stop on stop token when min_tokens met" assert request_stop.status == RequestStatus.FINISHED_STOPPED assert request_stop.stop_reason == 42 @pytest.mark.parametrize( "enable_prefix_caching, prompt_logprobs", [ (False, None), (True, 5), ], ) def test_schedule_concurrent_batches( enable_prefix_caching: bool, prompt_logprobs: int | None ): scheduler = create_scheduler( max_num_batched_tokens=1024, max_num_seqs=2, enable_prefix_caching=enable_prefix_caching, ) requests = create_requests( num_requests=2, num_tokens=512, prompt_logprobs=prompt_logprobs, ) # Schedule the first request. scheduler.add_request(requests[0]) scheduler_output0 = scheduler.schedule() assert len(scheduler_output0.scheduled_new_reqs) == 1 assert scheduler_output0.num_scheduled_tokens[requests[0].request_id] == 512 # The first request is still running, so only schedule the second request. scheduler.add_request(requests[1]) scheduler_output1 = scheduler.schedule() assert len(scheduler_output1.scheduled_new_reqs) == 1 assert scheduler_output1.num_scheduled_tokens[requests[1].request_id] == 512 # Model output of the first request. model_runner_output = ModelRunnerOutput( req_ids=[requests[0].request_id], req_id_to_index={requests[0].request_id: 0}, sampled_token_ids=[[0]], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output0, model_runner_output) # Schedule the next step. # The first request can be scheduled again while the second # request is still running. scheduler_output2 = scheduler.schedule() assert scheduler_output2.num_scheduled_tokens[requests[0].request_id] == 1 # Model output of the second request. model_runner_output = ModelRunnerOutput( req_ids=[requests[1].request_id], req_id_to_index={requests[1].request_id: 0}, sampled_token_ids=[[0]], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output1, model_runner_output) @pytest.mark.parametrize("enable_chunked_prefill", [True, False]) def test_schedule_order(enable_chunked_prefill: bool): scheduler = create_scheduler( max_num_batched_tokens=1024, max_num_seqs=3, enable_chunked_prefill=enable_chunked_prefill, ) # long requests requests = create_requests(num_requests=2, num_tokens=800) # short requests requests += create_requests(num_requests=2, num_tokens=10) for request in requests: scheduler.add_request(request) scheduler_output1 = scheduler.schedule() if enable_chunked_prefill: # When enable chunked prefill, long requests will be chunked. assert len(scheduler_output1.scheduled_new_reqs) == 2 else: # When disable chunked prefill, should not skip the long requests, # and scheduling subsequent short requests in advance, # even though there is still token budgets remaining. assert len(scheduler_output1.scheduled_new_reqs) == 1 def test_preempt_during_execution(): # NOTE(woosuk): The actual number of available blocks is 10 instead of 11 # because block 0 is reserved as the null block. scheduler = create_scheduler( max_num_batched_tokens=100, block_size=16, num_blocks=11, enable_prefix_caching=False, ) requests = create_requests(num_requests=2, num_tokens=80, block_size=16) # Schedule the first request. scheduler.add_request(requests[0]) scheduler_output0 = scheduler.schedule() assert len(scheduler_output0.num_scheduled_tokens) == 1 assert len(scheduler_output0.scheduled_new_reqs[0].block_ids[0]) == 5 # Schedule the second request while the first request is still running. # This scenario can occur in certain cases, when max_concurrent_batches > 1 # (e.g., when pipeline parallelism is used). scheduler.add_request(requests[1]) scheduler_output1 = scheduler.schedule() assert len(scheduler_output1.num_scheduled_tokens) == 1 assert len(scheduler_output1.scheduled_new_reqs[0].block_ids[0]) == 5 # Get the output of the first request. model_runner_output0 = ModelRunnerOutput( req_ids=[requests[0].request_id], req_id_to_index={requests[0].request_id: 0}, sampled_token_ids=[[0]], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output0, model_runner_output0) # Schedule the first request again. This will cause the preemption # of the second request because the KV cache is full. _ = scheduler.schedule() assert len(scheduler.running) == 1 assert scheduler.running[0] == requests[0] assert requests[1].status == RequestStatus.PREEMPTED model_runner_output1 = ModelRunnerOutput( req_ids=[requests[1].request_id], req_id_to_index={requests[1].request_id: 0}, sampled_token_ids=[[42]], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) scheduler.update_from_output(scheduler_output1, model_runner_output1) # The second request (that is preempted) should be updated with the # sampled token id. assert len(requests[1].output_token_ids) == 1 assert requests[1].output_token_ids[0] == 42 def test_scheduler_reset_prefix_cache(): scheduler = create_scheduler(enable_prefix_caching=True) requests = create_requests(num_requests=10) for request in requests: scheduler.add_request(request) # Initial scheduling, requests should be at the running state now _ = scheduler.schedule() # Verify requests moved from waiting to running assert len(scheduler.waiting) == 0 assert len(scheduler.running) == len(requests) for i, request in enumerate(requests): assert scheduler.running[i] == request # Reset prefix cache should fail since there are still running requests # and they are taking KV cache assert not scheduler.reset_prefix_cache() # Reset prefix cache with reset_running_requests=True. All running requests # Should be pushed back to the waiting queue and kv cache should be freed assert scheduler.reset_prefix_cache(reset_running_requests=True) # Verify requests moved from running to waiting assert len(scheduler.waiting) == len(requests) assert len(scheduler.running) == 0 for i, request in enumerate(requests): assert scheduler.waiting[i] == request # Note - these test cases mirror some of those in test_rejection_sampler.py @pytest.mark.parametrize( "spec_tokens,output_tokens,expected", [ ([[1, 2, 3]], [[1, 2, 3, 4]], (1, 3, 3, [1, 1, 1])), # perfect match ([[1, 2, 3]], [[1, 5]], (1, 3, 1, [1, 0, 0])), # early mismatch ([[1, 2], [3]], [[1, 2, 5], [3, 4]], (2, 3, 3, [2, 1])), # multiple sequences ([[1]], [[1, 2]], (1, 1, 1, [1])), # single token sequence ([[]], [[5]], (0, 0, 0, [0])), # empty sequence ( [[1, 2, 3], [4, 5, 6]], [[1, 2, 7], [4, 8]], (2, 6, 3, [2, 1, 0]), ), # multiple mismatches ], ) def test_schedule_spec_decoding_stats(spec_tokens, output_tokens, expected): """Test scheduling behavior with speculative decoding. This test verifies that: 1. Speculated tokens get scheduled correctly 2. Spec decoding stats properly count number of draft and accepted tokens """ num_spec_tokens = max(1, max(len(t) for t in spec_tokens)) scheduler = create_scheduler(num_speculative_tokens=num_spec_tokens) requests = create_requests(num_requests=len(spec_tokens), num_tokens=1) req_ids = [] req_to_index = {} for i, request in enumerate(requests): scheduler.add_request(request) req_ids.append(request.request_id) req_to_index[request.request_id] = i # Schedule a decode, which will also draft speculative tokens output = scheduler.schedule() assert len(output.scheduled_new_reqs) == len(requests) assert output.total_num_scheduled_tokens == len(requests) for i in range(len(requests)): req_id = requests[i].request_id assert output.num_scheduled_tokens[req_id] == 1 assert req_id not in output.scheduled_spec_decode_tokens model_runner_output = ModelRunnerOutput( req_ids=req_ids, req_id_to_index=req_to_index, sampled_token_ids=[[0] for _ in range(len(requests))], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) engine_core_outputs = scheduler.update_from_output(output, model_runner_output) draft_token_ids = DraftTokenIds(req_ids, spec_tokens) scheduler.update_draft_token_ids(draft_token_ids) for i in range(len(requests)): running_req = scheduler.running[i] # The prompt token assert running_req.num_computed_tokens == 1 # The prompt token and the sampled token assert running_req.num_tokens == 2 # The prompt token, the sampled token, and the speculated tokens assert running_req.num_tokens_with_spec == 2 + len(spec_tokens[i]) # No draft or accepted tokens counted yet assert not engine_core_outputs or ( engine_core_outputs[0].scheduler_stats.spec_decoding_stats is None ) # Schedule the speculated tokens for validation output = scheduler.schedule() assert len(output.scheduled_new_reqs) == 0 # The sampled token and speculated tokens assert output.total_num_scheduled_tokens == len(requests) + sum( len(ids) for ids in spec_tokens ) for i in range(len(requests)): req_id = requests[i].request_id assert output.num_scheduled_tokens[req_id] == 1 + len(spec_tokens[i]) if spec_tokens[i]: assert len(output.scheduled_spec_decode_tokens[req_id]) == len( spec_tokens[i] ) else: assert req_id not in output.scheduled_spec_decode_tokens model_runner_output = ModelRunnerOutput( req_ids=req_ids, req_id_to_index=req_to_index, sampled_token_ids=output_tokens, logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) engine_core_outputs = scheduler.update_from_output(output, model_runner_output) scheduler_stats = ( engine_core_outputs[0].scheduler_stats if engine_core_outputs else None ) if expected[0] == 0: assert scheduler_stats is not None assert scheduler_stats.spec_decoding_stats is None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_priority_scheduler_random.py
tests/v1/core/test_priority_scheduler_random.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import uuid import pytest from vllm.config import VllmConfig from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams from vllm.utils.hashing import get_hash_fn_by_name from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash from vllm.v1.core.sched.output import SchedulerOutput from vllm.v1.outputs import DraftTokenIds, ModelRunnerOutput from vllm.v1.request import Request from .test_scheduler import create_scheduler_with_priority from .utils import EOS_TOKEN_ID pytestmark = pytest.mark.cpu_test def _create_random_request( max_tokens_range: tuple[int, int], num_tokens_range: tuple[int, int], arrival_time_range: tuple[float, float], priority_range: tuple[int, int], num_mm_item_range: tuple[int, int], vllm_config: VllmConfig, ): max_tokens = random.randint(*max_tokens_range) num_tokens = random.randint(*num_tokens_range) priority = random.randint(*priority_range) arrival_time = random.uniform(*arrival_time_range) num_mm_item = random.randint(*num_mm_item_range) mm_positions: list[PlaceholderRange] = [] for mm_start in sorted( random.sample(range(num_tokens), min(num_mm_item, num_tokens)) ): if mm_start + 10 > num_tokens: continue mm_positions.append(PlaceholderRange(offset=mm_start, length=10)) request_id = uuid.uuid4().hex sampling_params = SamplingParams( ignore_eos=False, max_tokens=max_tokens, ) mm_features = [] for j, position in enumerate(mm_positions): identifier = f"{request_id}_hash_{j}" mm_feature = MultiModalFeatureSpec( data=MultiModalKwargsItem.dummy("dummy_m"), mm_position=position, identifier=identifier, modality="image", ) mm_features.append(mm_feature) prompt_token_ids = random.choices(range(100), k=num_tokens) caching_hash_fn = get_hash_fn_by_name( vllm_config.cache_config.prefix_caching_hash_algo ) init_none_hash(caching_hash_fn) block_hasher = get_request_block_hasher( vllm_config.cache_config.block_size, caching_hash_fn ) request = Request( request_id=request_id, prompt_token_ids=prompt_token_ids, sampling_params=sampling_params, pooling_params=None, mm_features=mm_features if mm_features else None, eos_token_id=EOS_TOKEN_ID, arrival_time=arrival_time, priority=priority, block_hasher=block_hasher, ) return request def _mock_execute_model( scheduler_output: SchedulerOutput, num_output_tokens_range: tuple[int, int] ) -> ModelRunnerOutput: request_ids: list[str] = [] request_ids.extend(req.req_id for req in scheduler_output.scheduled_new_reqs) request_ids.extend(scheduler_output.scheduled_cached_reqs.req_ids) random.shuffle(request_ids) num_output_tokens = [ random.randint(*num_output_tokens_range) for _ in range(len(request_ids)) ] sampled_token_ids = [ [random.randint(0, 100) for _ in range(num_tokens)] for num_tokens in num_output_tokens ] return ModelRunnerOutput( req_ids=request_ids, req_id_to_index={req_id: i for i, req_id in enumerate(request_ids)}, sampled_token_ids=sampled_token_ids, logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) def _mock_draft_token_ids( scheduler_output: SchedulerOutput, num_output_tokens_range: tuple[int, int], seen_request_prompt_length: dict[str, int], ) -> DraftTokenIds: request_ids: list[str] = [] sampled_token_ids: list[list[int]] = [] for request in scheduler_output.scheduled_new_reqs: assert request.req_id not in seen_request_prompt_length seen_request_prompt_length[request.req_id] = len(request.prompt_token_ids or []) if request.num_computed_tokens >= seen_request_prompt_length[request.req_id]: num_tokens = random.randint(*num_output_tokens_range) request_ids.append(request.req_id) sampled_token_ids.append( [random.randint(0, 100) for _ in range(num_tokens)] ) for req_id, num_computed_tokens in zip( scheduler_output.scheduled_cached_reqs.req_ids, scheduler_output.scheduled_cached_reqs.num_computed_tokens, ): if num_computed_tokens >= seen_request_prompt_length[req_id]: num_tokens = random.randint(*num_output_tokens_range) request_ids.append(req_id) sampled_token_ids.append( [random.randint(0, 100) for _ in range(num_tokens)] ) return DraftTokenIds(req_ids=request_ids, draft_token_ids=sampled_token_ids) def _chech_valid_scheduler_output( scheduler_output: SchedulerOutput, seen_request_ids: set[str], seen_mm_hashes: set[str], ): for req in scheduler_output.scheduled_new_reqs: assert req.req_id not in seen_request_ids seen_request_ids.add(req.req_id) for req_id in scheduler_output.scheduled_cached_reqs.req_ids: assert req_id in seen_request_ids req_ids = set[str]() req_ids.update(req.req_id for req in scheduler_output.scheduled_new_reqs) req_ids.update(scheduler_output.scheduled_cached_reqs.req_ids) assert set(scheduler_output.num_scheduled_tokens.keys()) == req_ids assert ( sum(scheduler_output.num_scheduled_tokens.values()) == scheduler_output.total_num_scheduled_tokens ) assert set(scheduler_output.scheduled_spec_decode_tokens.keys()) <= req_ids assert set(scheduler_output.scheduled_encoder_inputs.keys()) <= req_ids for req in scheduler_output.scheduled_new_reqs: for mm_feature in req.mm_features: seen_mm_hashes.add(mm_feature.identifier) for mm_hash in scheduler_output.free_encoder_mm_hashes: assert mm_hash in seen_mm_hashes assert scheduler_output.finished_req_ids <= seen_request_ids @pytest.mark.parametrize("enable_prefix_caching", [True, False]) @pytest.mark.parametrize("num_speculative_tokens", [None, 1, 5]) @pytest.mark.parametrize( ("max_input_tokens", "max_output_tokens", "max_num_seqs", "num_blocks"), [ # Standard profile (5000, 500, 256, 10000), # Generation heavy + high max_num_seqs + low num_blocks -> Many preemptions (500, 5000, 1024, 1000), ], ids=["standard", "preemption"], ) def test_priority_scheduling_blast( enable_prefix_caching: bool, num_speculative_tokens: int | None, max_input_tokens: int, max_output_tokens: int, max_num_seqs: int, num_blocks: int, ): random.seed(42) seen_request_prompt_length = dict[str, int]() seen_request_ids = set[str]() seen_mm_hashes = set[str]() scheduler = create_scheduler_with_priority( model="Qwen/Qwen2.5-VL-3B-Instruct", max_num_seqs=max_num_seqs, enable_prefix_caching=enable_prefix_caching, num_blocks=num_blocks, num_speculative_tokens=num_speculative_tokens, ) num_initial_requests = 10 for _ in range(num_initial_requests): req = _create_random_request( max_tokens_range=(1, max_output_tokens), num_tokens_range=(1, max_input_tokens), arrival_time_range=(0, 1), priority_range=(-3, 3), num_mm_item_range=(0, 2), vllm_config=scheduler.vllm_config, ) scheduler.add_request(req) num_initial_requests = 2 for _ in range(num_initial_requests): req = _create_random_request( max_tokens_range=(1, max_output_tokens), num_tokens_range=(1, max_input_tokens), arrival_time_range=(0, 0), priority_range=(4, 4), num_mm_item_range=(0, 2), vllm_config=scheduler.vllm_config, ) scheduler.add_request(req) for _ in range(20000): if len(scheduler.waiting) == 0: num_new_requests = random.randint(0, 2) for _ in range(num_new_requests): req = _create_random_request( max_tokens_range=(1, max_output_tokens), num_tokens_range=(1, max_input_tokens), arrival_time_range=(0, 1), priority_range=(-3, 3), num_mm_item_range=(0, 2), vllm_config=scheduler.vllm_config, ) scheduler.add_request(req) scheduler_output = scheduler.schedule() _chech_valid_scheduler_output( scheduler_output, seen_request_ids, seen_mm_hashes ) model_output = _mock_execute_model( scheduler_output, num_output_tokens_range=(1, 1 + (num_speculative_tokens or 0)), ) scheduler.update_from_output(scheduler_output, model_output) if num_speculative_tokens is not None: scheduler.update_draft_token_ids( _mock_draft_token_ids( scheduler_output, (0, num_speculative_tokens), seen_request_prompt_length, ) )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/core/test_async_scheduler.py
tests/v1/core/test_async_scheduler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections import deque import pytest from vllm.v1.core.sched.output import SchedulerOutput from vllm.v1.outputs import ModelRunnerOutput from vllm.v1.request import RequestStatus from vllm.v1.utils import ConstantList from .utils import create_requests, create_scheduler pytestmark = pytest.mark.cpu_test def _make_model_runner_output( scheduler_output: SchedulerOutput, ) -> ModelRunnerOutput: req_ids = list(scheduler_output.num_scheduled_tokens.keys()) return ModelRunnerOutput( req_ids=req_ids, req_id_to_index={req_id: i for i, req_id in enumerate(req_ids)}, sampled_token_ids=[[i] for i in range(len(req_ids))], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) @pytest.mark.parametrize("max_tokens", [1, 2, 3, 5]) def test_stop_by_max_tokens(max_tokens: int): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=2, max_tokens=max_tokens) req0, req1 = requests expected_total_num_scheduled_tokens = 0 sched_outputs: deque[SchedulerOutput] = deque() scheduler.add_request(req0) sched_outputs.append(scheduler.schedule()) expected_total_num_scheduled_tokens += req0.num_prompt_tokens + max_tokens - 1 scheduler.add_request(req1) sched_outputs.append(scheduler.schedule()) expected_total_num_scheduled_tokens += req1.num_prompt_tokens + max_tokens - 1 total_num_scheduled_tokens = 0 while sched_outputs: sched_output = sched_outputs.popleft() total_num_scheduled_tokens += sched_output.total_num_scheduled_tokens model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) assert scheduler.get_num_unfinished_requests() == 0 assert req0.num_output_tokens == max_tokens assert req1.num_output_tokens == max_tokens # Ensure we aren't scheduling more tokens than necessary. assert total_num_scheduled_tokens == expected_total_num_scheduled_tokens def test_abort(): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=10, max_tokens=20) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) abort_order = [0, 8, 3, 1, 6, 4, 2, 5, 7, 9] abort_order_copy = abort_order.copy() def abort_request(): if not abort_order: return req = requests[abort_order.pop(0)] scheduler.finish_requests(req.request_id, RequestStatus.FINISHED_ABORTED) while sched_outputs: # Abort a scheduled request. abort_request() sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) for i, req in enumerate(requests): assert req.status == RequestStatus.FINISHED_ABORTED assert req.num_output_tokens == abort_order_copy.index(i) def test_preempt(): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=10, max_tokens=20) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) abort_order = [0, 8, 3, 1, 6, 4, 2, 5, 7, 9] abort_order_copy = abort_order.copy() def abort_request(): if not abort_order: return req = requests[abort_order.pop(0)] scheduler.finish_requests(req.request_id, RequestStatus.FINISHED_ABORTED) while sched_outputs: # Abort a scheduled request. abort_request() sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) for i, req in enumerate(requests): assert req.status == RequestStatus.FINISHED_ABORTED assert req.num_output_tokens == abort_order_copy.index(i) def test_prefix_caching_for_prefill_dedup(): CHUNK_SIZE = 1000 BLOCK_SIZE = 16 num_prompt_tokens = 100 scheduler = create_scheduler( async_scheduling=True, max_num_batched_tokens=CHUNK_SIZE, enable_prefix_caching=True, block_size=BLOCK_SIZE, ) requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens, max_tokens=3, same_prompt=True, block_size=BLOCK_SIZE, ) requests_copy = requests.copy() # Two requests with the same prompt. req0 = requests.pop(0) req1 = requests.pop(0) scheduler.add_request(req0) scheduler.add_request(req1) sched_outputs: deque[SchedulerOutput] = deque() sched_output = scheduler.schedule() sched_outputs.append(sched_output) # Make sure prefix caching de-duplicates the prompts in the same step, # so all the blocks except the last are shared between the two requests. assert len(sched_output.num_scheduled_tokens) == 2 num_blocks = num_prompt_tokens // BLOCK_SIZE assert req0.num_cached_tokens == 0 assert req1.num_cached_tokens >= num_blocks * BLOCK_SIZE sched_outputs.append(scheduler.schedule()) while sched_outputs: if requests: scheduler.add_request(requests.pop(0)) sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) # Other requests scheduled after the two requests should also get # prefix cache hit. assert scheduler.get_num_unfinished_requests() == 0 for req in requests_copy[1:]: assert req.num_cached_tokens >= num_blocks * BLOCK_SIZE def test_prefix_caching_for_multi_turn(): CHUNK_SIZE = 1000 BLOCK_SIZE = 16 num_prompt_tokens = 100 num_output_tokens = 200 scheduler = create_scheduler( async_scheduling=True, max_num_batched_tokens=CHUNK_SIZE, enable_prefix_caching=True, block_size=BLOCK_SIZE, ) requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens, max_tokens=num_output_tokens, block_size=BLOCK_SIZE, ) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) # Process the requests. while sched_outputs: sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) assert scheduler.get_num_unfinished_requests() == 0 # Create next-turn requests whose prompts are the full output of the # previous turn. next_turn_requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens + num_output_tokens, max_tokens=num_output_tokens, block_size=BLOCK_SIZE, ) for i, req in enumerate(next_turn_requests): req.prompt_token_ids = requests[i].prompt_token_ids + list( requests[i].output_token_ids ) req._all_token_ids = req.prompt_token_ids.copy() req.all_token_ids = ConstantList(req._all_token_ids) req.block_hashes = [] req.block_hashes = req.get_hash_new_full_blocks() # Schedule the next-turn requests. for req in next_turn_requests: scheduler.add_request(req) sched_outputs.append(scheduler.schedule()) # Make sure the next-turn requests get prefix cache hit by the previous # requests. for req in next_turn_requests: assert req.num_cached_tokens == req.num_prompt_tokens // BLOCK_SIZE * BLOCK_SIZE
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_preprocess_error_handling.py
tests/v1/engine/test_preprocess_error_handling.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch.cuda from vllm import LLM, SamplingParams from vllm.platforms import current_platform from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.core import EngineCore MODEL_NAME = "hmellor/tiny-random-LlamaForCausalLM" def test_preprocess_error_handling(monkeypatch: pytest.MonkeyPatch): """Test that preprocessing errors are handled gracefully.""" if current_platform.is_rocm() or current_platform.is_xpu(): pytest.skip( "Skipped on ROCm/XPU: this test only works with 'fork', " "but ROCm/XPU uses 'spawn'." ) assert not torch.cuda.is_initialized(), ( "fork needs to be used for the engine " "core process and this isn't possible if cuda is already initialized" ) # Store original method to call for non-failing requests original_preprocess = EngineCore.preprocess_add_request # Monkeypatch to make preprocess_add_request raise an exception # only for requests with "FAIL" in the first token def conditional_failing_preprocess(self, request: EngineCoreRequest): # Fail if the first token id is 333 if request.prompt_token_ids and request.prompt_token_ids[0] == 333: raise ValueError("Simulated preprocessing error!") return original_preprocess(self, request) monkeypatch.setattr( EngineCore, "preprocess_add_request", conditional_failing_preprocess ) llm = LLM(model=MODEL_NAME) # Create a failing request by crafting a request with an invalid token # We need to use a direct approach since LLM.generate tokenizes for us from vllm.inputs import TokensPrompt # This should raise an exception due to the preprocessing failure # Special token id to trigger the failure failing_prompt = TokensPrompt(prompt_token_ids=[333]) outputs = llm.generate(failing_prompt, SamplingParams(max_tokens=10)) # type: ignore assert len(outputs) == 1 assert len(outputs[0].outputs[0].token_ids) == 0 assert outputs[0].finished assert outputs[0].outputs[0].finish_reason == "error" # Verify the engine is still functional with a normal request outputs = llm.generate("Hello, my name is", SamplingParams(max_tokens=10)) assert len(outputs) == 1 assert len(outputs[0].outputs[0].token_ids) > 0 assert outputs[0].outputs[0].finish_reason in ("stop", "length")
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_engine_core_client.py
tests/v1/engine/test_engine_core_client.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import importlib import os import signal import time import uuid from dataclasses import dataclass from threading import Thread from types import SimpleNamespace from typing import Any from unittest.mock import MagicMock import pytest import torch from transformers import AutoTokenizer from tests.utils import multi_gpu_test from vllm import SamplingParams from vllm.distributed.kv_events import BlockStored, KVEventBatch, ZmqEventPublisher from vllm.engine.arg_utils import EngineArgs from vllm.platforms import current_platform from vllm.usage.usage_lib import UsageContext from vllm.utils.torch_utils import set_default_torch_num_threads from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.core import EngineCore from vllm.v1.engine.core_client import ( AsyncMPClient, EngineCoreClient, SyncMPClient, ) from vllm.v1.engine.utils import CoreEngineProcManager from vllm.v1.executor.abstract import Executor from ...distributed.conftest import MockSubscriber from ...utils import create_new_process_for_each_test if not current_platform.is_cuda(): pytest.skip(reason="V1 currently only supported on CUDA.", allow_module_level=True) MODEL_NAME = "meta-llama/Llama-3.2-1B-Instruct" TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME) PROMPT = "Hello my name is Robert and I love quantization kernels" PROMPT_TOKENS = TOKENIZER(PROMPT).input_ids _REQUEST_COUNTER = 0 def make_request( params: SamplingParams, prompt_tokens_ids: list[int] | None = None ) -> EngineCoreRequest: if not prompt_tokens_ids: prompt_tokens_ids = PROMPT_TOKENS global _REQUEST_COUNTER _REQUEST_COUNTER += 1 request_id = f"request-{_REQUEST_COUNTER}" return EngineCoreRequest( request_id=request_id, external_req_id=f"{request_id}-{uuid.uuid4()}", prompt_token_ids=prompt_tokens_ids, mm_features=None, sampling_params=params, pooling_params=None, eos_token_id=None, arrival_time=time.time(), lora_request=None, cache_salt=None, data_parallel_rank=None, ) def _reload_envs_module(): import vllm.envs as envs_mod cache_clear = getattr(getattr(envs_mod, "__getattr__", None), "cache_clear", None) if cache_clear is not None: cache_clear() return importlib.reload(envs_mod) def _reload_core_client_module(): module = importlib.import_module("vllm.v1.engine.core_client") return importlib.reload(module) def test_mp_client_uses_env_timeout(monkeypatch: pytest.MonkeyPatch): timeout_value = 654 monkeypatch.setenv("VLLM_ENGINE_READY_TIMEOUT_S", str(timeout_value)) # Ensure that the environment variable is loaded if caching is enabled _reload_envs_module() core_client_mod = _reload_core_client_module() poll_timeouts: list[int] = [] class ShadowSocket: def poll(self, timeout: int) -> int: # Capture the timeout value for each poll call poll_timeouts.append(timeout) return 1 def recv_multipart(self): return (b"\x00\x00", b"ready") class DummySocket: def send_multipart(self, _msg, *, copy: bool = False, track: bool = False): if track: return SimpleNamespace(done=True) def recv_multipart(self, *, copy: bool = False): return (b"", b"") def close(self, *, linger: int = 0): pass def bind(self, _address): pass def connect(self, _address): pass def setsockopt(self, *_args, **_kwargs): pass monkeypatch.setattr(core_client_mod.zmq.Socket, "shadow", lambda *_: ShadowSocket()) monkeypatch.setattr( core_client_mod, "make_zmq_socket", lambda *_, **__: DummySocket() ) parallel_config = SimpleNamespace( data_parallel_size=1, data_parallel_rank=0, data_parallel_index=0, data_parallel_size_local=1, data_parallel_rank_local=None, data_parallel_hybrid_lb=False, data_parallel_external_lb=False, ) vllm_config = SimpleNamespace(parallel_config=parallel_config) client = core_client_mod.MPClient( asyncio_mode=False, vllm_config=vllm_config, executor_class=object, log_stats=False, client_addresses={ "input_address": "inproc://input", "output_address": "inproc://output", }, ) try: # timeout_value is in seconds, but poll receives milliseconds assert poll_timeouts == [timeout_value * 1000] finally: client.shutdown() def loop_until_done(client: EngineCoreClient, outputs: dict): while True: engine_core_outputs = client.get_output().outputs if len(engine_core_outputs) == 0: continue all_finished = True for out in engine_core_outputs: outputs[out.request_id].append(out) if not out.finished: all_finished = False if all_finished: break async def loop_until_done_async(client: EngineCoreClient, outputs: dict): while True: engine_core_outputs = (await client.get_output_async()).outputs if len(engine_core_outputs) == 0: continue all_finished = True for out in engine_core_outputs: outputs[out.request_id].append(out) if not out.finished: all_finished = False if all_finished: break async def loop_until_fully_done_async(client: EngineCoreClient, outputs: dict): while True: engine_core_outputs = (await client.get_output_async()).outputs if len(engine_core_outputs) == 0: continue # Add outputs to the dict for out in engine_core_outputs: outputs[out.request_id].append(out) # Check if all request IDs in outputs have finished if all(outs and outs[-1].finished for outs in outputs.values()): break await asyncio.sleep(0.1) # Dummy utility function to monkey-patch into engine core. def echo(self, msg: str, err_msg: str | None = None, sleep: float | None = None) -> str: print(f"echo util function called: {msg}, {err_msg}") if sleep is not None: time.sleep(sleep) if err_msg is not None: raise ValueError(err_msg) return msg @create_new_process_for_each_test() @pytest.mark.parametrize("multiprocessing_mode", [True, False]) def test_engine_core_client( monkeypatch: pytest.MonkeyPatch, multiprocessing_mode: bool ): with monkeypatch.context() as m: # Monkey-patch core engine utility function to test. m.setattr(EngineCore, "echo", echo, raising=False) engine_args = EngineArgs(model=MODEL_NAME, enforce_eager=True) vllm_config = engine_args.create_engine_config(UsageContext.UNKNOWN_CONTEXT) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=multiprocessing_mode, asyncio_mode=False, vllm_config=vllm_config, executor_class=executor_class, log_stats=False, ) MAX_TOKENS = 20 params = SamplingParams(max_tokens=MAX_TOKENS) """Normal Request Cycle.""" requests = [make_request(params) for _ in range(10)] request_ids = [req.request_id for req in requests] # Add requests to the engine. for request in requests: client.add_request(request) time.sleep(0.01) outputs: dict[str, list] = {req_id: [] for req_id in request_ids} loop_until_done(client, outputs) for req_id in request_ids: assert len(outputs[req_id]) == MAX_TOKENS, ( f"{outputs[req_id]=}, {MAX_TOKENS=}" ) """Abort Request Cycle.""" # Note: this code pathway will only work for multiprocessing # since we have to call get_output() explicitly # Add requests to the engine. for idx, request in enumerate(requests): client.add_request(request) time.sleep(0.01) if idx % 2 == 0: client.abort_requests([request.request_id]) outputs = {req_id: [] for req_id in request_ids} loop_until_done(client, outputs) for idx, req_id in enumerate(request_ids): if idx % 2 == 0: assert len(outputs[req_id]) < MAX_TOKENS, ( f"{len(outputs[req_id])=}, {MAX_TOKENS=}" ) else: assert len(outputs[req_id]) == MAX_TOKENS, ( f"{len(outputs[req_id])=}, {MAX_TOKENS=}" ) """Abort after request is finished.""" # Note: this code pathway will only work for multiprocessing # since we have to call get_output() explicitly request = requests[0] client.add_request(request) time.sleep(10.0) client.abort_requests([request.request_id]) if multiprocessing_mode: """Utility method invocation""" core_client: SyncMPClient = client result = core_client.call_utility("echo", "testarg") assert result == "testarg" with pytest.raises(Exception) as e_info: core_client.call_utility("echo", None, "help!") assert str(e_info.value) == "Call to echo method failed: help!" @pytest.mark.asyncio(loop_scope="function") async def test_engine_core_client_asyncio(monkeypatch: pytest.MonkeyPatch): with monkeypatch.context() as m: # Monkey-patch core engine utility function to test. m.setattr(EngineCore, "echo", echo, raising=False) engine_args = EngineArgs(model=MODEL_NAME, enforce_eager=True) vllm_config = engine_args.create_engine_config( usage_context=UsageContext.UNKNOWN_CONTEXT ) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=True, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=True, ) try: MAX_TOKENS = 20 params = SamplingParams(max_tokens=MAX_TOKENS) """Normal Request Cycle.""" requests = [make_request(params) for _ in range(10)] request_ids = [req.request_id for req in requests] # Add requests to the engine. for request in requests: await client.add_request_async(request) await asyncio.sleep(0.01) outputs: dict[str, list] = {req_id: [] for req_id in request_ids} await loop_until_done_async(client, outputs) for req_id in request_ids: assert len(outputs[req_id]) == MAX_TOKENS, ( f"{outputs[req_id]=}, {MAX_TOKENS=}" ) """Abort Request Cycle.""" # Add requests to the engine. for idx, request in enumerate(requests): await client.add_request_async(request) await asyncio.sleep(0.01) if idx % 2 == 0: await client.abort_requests_async([request.request_id]) outputs = {req_id: [] for req_id in request_ids} await loop_until_done_async(client, outputs) for idx, req_id in enumerate(request_ids): if idx % 2 == 0: assert len(outputs[req_id]) < MAX_TOKENS, ( f"{len(outputs[req_id])=}, {MAX_TOKENS=}" ) else: assert len(outputs[req_id]) == MAX_TOKENS, ( f"{len(outputs[req_id])=}, {MAX_TOKENS=}" ) """Utility method invocation""" core_client: AsyncMPClient = client result = await core_client.call_utility_async("echo", "testarg") assert result == "testarg" with pytest.raises(Exception) as e_info: await core_client.call_utility_async("echo", None, "help!") assert str(e_info.value) == "Call to echo method failed: help!" # Test that cancelling the utility call doesn't destabilize the # engine. util_task = asyncio.create_task( core_client.call_utility_async("echo", "testarg2", None, 0.5) ) # sleep for 0.5 sec await asyncio.sleep(0.05) cancelled = util_task.cancel() assert cancelled # Ensure client is still functional. The engine runs utility # methods in a single thread so this request won't be processed # until the cancelled sleeping one is complete. result = await asyncio.wait_for( core_client.call_utility_async("echo", "testarg3"), timeout=1.0 ) assert result == "testarg3" finally: client.shutdown() @dataclass class MyDataclass: message: str # Dummy utility function to monkey-patch into engine core. def echo_dc( self, msg: str, return_list: bool = False, ) -> MyDataclass | list[MyDataclass]: print(f"echo dc util function called: {msg}") val = None if msg is None else MyDataclass(msg) # Return dataclass to verify support for returning custom types # (for which there is special handling to make it work with msgspec). return [val for _ in range(3)] if return_list else val # Dummy utility function to test dict serialization with custom types. def echo_dc_dict( self, msg: str, return_dict: bool = False, ) -> MyDataclass | dict[str, MyDataclass]: print(f"echo dc dict util function called: {msg}") val = None if msg is None else MyDataclass(msg) # Return dict of dataclasses to verify support for returning dicts # with custom value types. if return_dict: return {"key1": val, "key2": val, "key3": val} else: return val # Dummy utility function to test nested structures with custom types. def echo_dc_nested( self, msg: str, structure_type: str = "list_of_dicts", ) -> Any: print(f"echo dc nested util function called: {msg}, structure: {structure_type}") val = None if msg is None else MyDataclass(msg) if structure_type == "list_of_dicts": # noqa # Return list of dicts: [{"a": val, "b": val}, {"c": val, "d": val}] return [{"a": val, "b": val}, {"c": val, "d": val}] elif structure_type == "dict_of_lists": # Return dict of lists: {"list1": [val, val], "list2": [val, val]} return {"list1": [val, val], "list2": [val, val]} elif structure_type == "deep_nested": # Return deeply nested: {"outer": [{"inner": [val, val]}, # {"inner": [val]}]} return {"outer": [{"inner": [val, val]}, {"inner": [val]}]} else: return val @pytest.mark.asyncio(loop_scope="function") async def test_engine_core_client_util_method_custom_return( monkeypatch: pytest.MonkeyPatch, ): with monkeypatch.context() as m: # Must set insecure serialization to allow returning custom types. m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") # Monkey-patch core engine utility function to test. m.setattr(EngineCore, "echo_dc", echo_dc, raising=False) engine_args = EngineArgs(model=MODEL_NAME, enforce_eager=True) vllm_config = engine_args.create_engine_config( usage_context=UsageContext.UNKNOWN_CONTEXT ) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=True, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=True, ) try: # Test utility method returning custom / non-native data type. core_client: AsyncMPClient = client result = await core_client.call_utility_async("echo_dc", "testarg2", False) assert isinstance(result, MyDataclass) and result.message == "testarg2" result = await core_client.call_utility_async("echo_dc", "testarg2", True) assert isinstance(result, list) and all( isinstance(r, MyDataclass) and r.message == "testarg2" for r in result ) # Test returning None and list of Nones result = await core_client.call_utility_async("echo_dc", None, False) assert result is None result = await core_client.call_utility_async("echo_dc", None, True) assert isinstance(result, list) and all(r is None for r in result) finally: client.shutdown() @pytest.mark.asyncio(loop_scope="function") async def test_engine_core_client_util_method_custom_dict_return( monkeypatch: pytest.MonkeyPatch, ): with monkeypatch.context() as m: # Must set insecure serialization to allow returning custom types. m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") # Monkey-patch core engine utility function to test. m.setattr(EngineCore, "echo_dc_dict", echo_dc_dict, raising=False) engine_args = EngineArgs(model=MODEL_NAME, enforce_eager=True) vllm_config = engine_args.create_engine_config( usage_context=UsageContext.UNKNOWN_CONTEXT ) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=True, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=True, ) try: # Test utility method returning custom / non-native data type. core_client: AsyncMPClient = client # Test single object return result = await core_client.call_utility_async( "echo_dc_dict", "testarg3", False ) assert isinstance(result, MyDataclass) and result.message == "testarg3" # Test dict return with custom value types result = await core_client.call_utility_async( "echo_dc_dict", "testarg3", True ) assert isinstance(result, dict) and len(result) == 3 for key, val in result.items(): assert key in ["key1", "key2", "key3"] assert isinstance(val, MyDataclass) and val.message == "testarg3" # Test returning dict with None values result = await core_client.call_utility_async("echo_dc_dict", None, True) assert isinstance(result, dict) and len(result) == 3 for key, val in result.items(): assert key in ["key1", "key2", "key3"] assert val is None finally: client.shutdown() @pytest.mark.asyncio(loop_scope="function") async def test_engine_core_client_util_method_nested_structures( monkeypatch: pytest.MonkeyPatch, ): with monkeypatch.context() as m: # Must set insecure serialization to allow returning custom types. m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") # Monkey-patch core engine utility function to test. m.setattr(EngineCore, "echo_dc_nested", echo_dc_nested, raising=False) engine_args = EngineArgs(model=MODEL_NAME, enforce_eager=True) vllm_config = engine_args.create_engine_config( usage_context=UsageContext.UNKNOWN_CONTEXT ) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=True, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=True, ) try: core_client: AsyncMPClient = client # Test list of dicts: [{"a": val, "b": val}, {"c": val, "d": val}] result = await core_client.call_utility_async( "echo_dc_nested", "nested1", "list_of_dicts" ) assert isinstance(result, list) and len(result) == 2 for i, item in enumerate(result): assert isinstance(item, dict) if i == 0: assert "a" in item and "b" in item assert ( isinstance(item["a"], MyDataclass) and item["a"].message == "nested1" ) assert ( isinstance(item["b"], MyDataclass) and item["b"].message == "nested1" ) else: assert "c" in item and "d" in item assert ( isinstance(item["c"], MyDataclass) and item["c"].message == "nested1" ) assert ( isinstance(item["d"], MyDataclass) and item["d"].message == "nested1" ) # Test dict of lists: {"list1": [val, val], "list2": [val, val]} result = await core_client.call_utility_async( "echo_dc_nested", "nested2", "dict_of_lists" ) assert isinstance(result, dict) and len(result) == 2 assert "list1" in result and "list2" in result for key, lst in result.items(): assert isinstance(lst, list) and len(lst) == 2 for item in lst: assert isinstance(item, MyDataclass) and item.message == "nested2" # Test deeply nested: {"outer": [{"inner": [val, val]}, # {"inner": [val]}]} result = await core_client.call_utility_async( "echo_dc_nested", "nested3", "deep_nested" ) assert isinstance(result, dict) and "outer" in result outer_list = result["outer"] assert isinstance(outer_list, list) and len(outer_list) == 2 # First dict in outer list should have "inner" with 2 items inner_dict1 = outer_list[0] assert isinstance(inner_dict1, dict) and "inner" in inner_dict1 inner_list1 = inner_dict1["inner"] assert isinstance(inner_list1, list) and len(inner_list1) == 2 for item in inner_list1: assert isinstance(item, MyDataclass) and item.message == "nested3" # Second dict in outer list should have "inner" with 1 item inner_dict2 = outer_list[1] assert isinstance(inner_dict2, dict) and "inner" in inner_dict2 inner_list2 = inner_dict2["inner"] assert isinstance(inner_list2, list) and len(inner_list2) == 1 assert ( isinstance(inner_list2[0], MyDataclass) and inner_list2[0].message == "nested3" ) # Test with None values in nested structures result = await core_client.call_utility_async( "echo_dc_nested", None, "list_of_dicts" ) assert isinstance(result, list) and len(result) == 2 for item in result: assert isinstance(item, dict) for val in item.values(): assert val is None finally: client.shutdown() @pytest.mark.parametrize( "multiprocessing_mode,publisher_config", [(True, "tcp"), (False, "inproc")], indirect=["publisher_config"], ) def test_kv_cache_events( multiprocessing_mode: bool, publisher_config, ): block_size = 16 num_blocks = 2 engine_args = EngineArgs( model=MODEL_NAME, enforce_eager=True, enable_prefix_caching=True, block_size=block_size, ) engine_args.kv_events_config = publisher_config vllm_config = engine_args.create_engine_config(UsageContext.UNKNOWN_CONTEXT) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=multiprocessing_mode, asyncio_mode=False, vllm_config=vllm_config, executor_class=executor_class, log_stats=False, ) endpoint = publisher_config.endpoint.replace("*", "127.0.0.1") subscriber = MockSubscriber( endpoint, topic=publisher_config.topic, decode_type=KVEventBatch ) try: custom_tokens = list(range(num_blocks * block_size)) sampling_params = SamplingParams(max_tokens=1) request = make_request(sampling_params, custom_tokens) client.add_request(request) outputs: dict[str, list] = {request.request_id: []} loop_until_done(client, outputs) result = subscriber.receive_one(timeout=1000) assert result is not None, "No message received" seq, received = result assert seq == 0, "Sequence number mismatch" assert len(received.events) == 1, "We should have exactly one BlockStored event" event = received.events[0] assert isinstance(event, BlockStored), "We should have a BlockStored event" assert len(event.block_hashes) == num_blocks, ( "We should have a BlockStored event with 2 block_hashes" ) assert event.block_size == block_size, ( "Block size should be the same as the block size" ) assert event.parent_block_hash is None, "Parent block hash should be None" assert event.lora_id is None, "Lora id should be None" assert event.lora_name is None, "Lora name should be None" assert len(event.token_ids) == num_blocks * block_size, ( "Token ids should be the same as the custom tokens" ) assert event.token_ids == custom_tokens, ( "Token ids should be the same as the custom tokens" ) finally: client.shutdown() subscriber.close() @pytest.mark.asyncio @pytest.mark.parametrize( "multiprocessing_mode,publisher_config", [(True, "tcp")], indirect=["publisher_config"], ) @multi_gpu_test(num_gpus=4) async def test_kv_cache_events_dp( multiprocessing_mode: bool, publisher_config, ): block_size = 16 num_blocks = 2 dp_size = 2 tp_size = 2 engine_args = EngineArgs( model=MODEL_NAME, enforce_eager=True, enable_prefix_caching=True, data_parallel_size=dp_size, tensor_parallel_size=tp_size, block_size=block_size, ) engine_args.kv_events_config = publisher_config vllm_config = engine_args.create_engine_config(UsageContext.UNKNOWN_CONTEXT) executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): client = EngineCoreClient.make_client( multiprocess_mode=multiprocessing_mode, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=False, ) await asyncio.sleep(1) # Build endpoints for all DP ranks base_endpoint = publisher_config.endpoint.replace("*", "127.0.0.1") endpoints = [] for i in range(dp_size): offset_endpoint = ZmqEventPublisher.offset_endpoint_port(base_endpoint, i) endpoints.append(offset_endpoint) subscriber = MockSubscriber( endpoints, topic=publisher_config.topic, decode_type=KVEventBatch ) try: custom_tokens = list(range(num_blocks * block_size)) sampling_params = SamplingParams(max_tokens=1) all_request_ids = [] # Create and add 25 requests # NOTE: attempts to force routing to both dp groups but can be flaky for i in range(25): await asyncio.sleep(0.01) request = make_request(sampling_params, custom_tokens) await client.add_request_async(request) all_request_ids.append(request.request_id) await asyncio.sleep(0.1) # Initialize outputs dict for all requests outputs: dict[str, list] = {req_id: [] for req_id in all_request_ids} print("processing requests...") await asyncio.wait_for( loop_until_fully_done_async(client, outputs), timeout=20.0 ) # Receive from subscriber until no more messages print("collecting results...") results = [] while True: result = subscriber.receive_one(timeout=1) print(result) if result is None: break results.append(result) # Collect all events and data_parallel_ranks from all results all_dp_ranks = [received.data_parallel_rank for (_, received) in results] unique_dps = set(all_dp_ranks) assert len(unique_dps) == 2, ( f"Expected 2 unique data_parallel_ranks, got {len(unique_dps)}" ) finally: client.shutdown() subscriber.close() @pytest.mark.timeout(20) def test_startup_failure(monkeypatch: pytest.MonkeyPatch): with monkeypatch.context() as m, pytest.raises(Exception) as e_info: # Monkey-patch to extract core process pid while it's starting. core_proc_pid = [None] cepm_ctor = CoreEngineProcManager.__init__ def patched_cepm_ctor(self: CoreEngineProcManager, *args, **kwargs): cepm_ctor(self, *args, **kwargs) core_proc_pid[0] = self.processes[0].pid m.setattr(CoreEngineProcManager, "__init__", patched_cepm_ctor) t = time.time() engine_args = EngineArgs(model=MODEL_NAME) vllm_config = engine_args.create_engine_config( usage_context=UsageContext.UNKNOWN_CONTEXT ) executor_class = Executor.get_class(vllm_config) print(f"VllmConfig creation took {time.time() - t:.2f} seconds.") # Start another thread to wait for engine core process to start # and kill it - simulate fatal uncaught process exit. def kill_first_child(): while (child_pid := core_proc_pid[0]) is None: time.sleep(0.5) print(f"Killing child core process {child_pid}") assert isinstance(child_pid, int) os.kill(child_pid, signal.SIGKILL) Thread(target=kill_first_child, daemon=True).start() _core_client = EngineCoreClient.make_client( multiprocess_mode=True, asyncio_mode=True, vllm_config=vllm_config, executor_class=executor_class, log_stats=True, ) assert "Engine core initialization failed" in str(e_info.value) @create_new_process_for_each_test() def test_engine_core_proc_instantiation_cuda_empty(monkeypatch: pytest.MonkeyPatch): """ Test that EngineCoreProc can be instantiated when CUDA_VISIBLE_DEVICES is empty. This ensures the engine frontend does not need access to GPUs. """ from vllm.v1.engine.core import EngineCoreProc from vllm.v1.executor.abstract import Executor # Create a simple mock executor instead of a complex custom class mock_executor_class = MagicMock(spec=Executor) def create_mock_executor(vllm_config): mock_executor = MagicMock() # Only implement the methods that are actually called during init from vllm.v1.kv_cache_interface import FullAttentionSpec mock_spec = FullAttentionSpec( block_size=16, num_kv_heads=1, head_size=64, dtype=torch.float16 ) mock_executor.get_kv_cache_specs.return_value = [{"default": mock_spec}] mock_executor.determine_available_memory.return_value = [1024 * 1024 * 1024] mock_executor.initialize_from_config.return_value = None mock_executor.max_concurrent_batches = 1 return mock_executor mock_executor_class.side_effect = create_mock_executor with monkeypatch.context() as m: m.setenv("CUDA_VISIBLE_DEVICES", "") # No CUDA devices
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_async_llm.py
tests/v1/engine/test_async_llm.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio from contextlib import ExitStack from unittest.mock import MagicMock import pytest from vllm import SamplingParams from vllm.assets.image import ImageAsset from vllm.config import VllmConfig from vllm.engine.arg_utils import AsyncEngineArgs from vllm.entrypoints.openai.protocol import ( ChatCompletionRequest, ChatCompletionResponse, ErrorResponse, ) from vllm.entrypoints.openai.serving_chat import OpenAIServingChat from vllm.entrypoints.openai.serving_models import BaseModelPath, OpenAIServingModels from vllm.inputs import PromptType from vllm.outputs import RequestOutput from vllm.platforms import current_platform from vllm.sampling_params import RequestOutputKind from vllm.utils.torch_utils import set_default_torch_num_threads from vllm.v1.engine.async_llm import AsyncLLM from vllm.v1.metrics.loggers import ( AggregatedLoggingStatLogger, LoggingStatLogger, PerEngineStatLoggerAdapter, PrometheusStatLogger, ) if not current_platform.is_cuda(): pytest.skip(reason="V1 currently only supported on CUDA.", allow_module_level=True) TEXT_ENGINE_ARGS = AsyncEngineArgs( model="meta-llama/Llama-3.2-1B-Instruct", enforce_eager=True, ) VISION_ENGINE_ARGS = AsyncEngineArgs( model="Qwen/Qwen2-VL-2B-Instruct", enforce_eager=True ) TEXT_PROMPT = "Hello my name is Robert and" VISION_PROMPT_TEMPLATE = ( "<|im_start|>system\nYou are a helpful assistant.<|im_end|>" "\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>" "What is in the image?<|im_end|>\n" "<|im_start|>assistant\n" ) VISION_PROMPT = { "prompt": VISION_PROMPT_TEMPLATE, "multi_modal_data": {"image": ImageAsset("stop_sign").pil_image}, } async def generate( engine: AsyncLLM, request_id: str, prompt: PromptType, output_kind: RequestOutputKind, max_tokens: int, n: int = 1, prompt_logprobs: int | None = None, cancel_after: int | None = None, ) -> tuple[int, str]: # Ensure generate doesn't complete too fast for cancellation test. await asyncio.sleep(0.2) count = 0 sampling_params = SamplingParams( max_tokens=max_tokens, ignore_eos=True, output_kind=output_kind, temperature=0.5, seed=33, n=n, prompt_logprobs=prompt_logprobs, ) async for out in engine.generate( request_id=request_id, prompt=prompt, sampling_params=sampling_params ): num_tokens = sum(len(output.token_ids) for output in out.outputs) if output_kind == RequestOutputKind.DELTA: count += num_tokens else: count = num_tokens if cancel_after is not None and count >= cancel_after: return count, request_id await asyncio.sleep(0.0) return count, request_id @pytest.mark.parametrize( "output_kind", [RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY] ) @pytest.mark.parametrize( "engine_args,prompt", [(TEXT_ENGINE_ARGS, TEXT_PROMPT), (VISION_ENGINE_ARGS, VISION_PROMPT)], ) @pytest.mark.asyncio async def test_load( output_kind: RequestOutputKind, engine_args: AsyncEngineArgs, prompt: PromptType, ): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(engine_args) after.callback(engine.shutdown) NUM_REQUESTS = 100 NUM_EXPECTED_TOKENS = 10 request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] # Create concurrent requests. tasks = [] for request_id in request_ids: tasks.append( asyncio.create_task( generate( engine, request_id, prompt, output_kind, NUM_EXPECTED_TOKENS ) ) ) # Confirm that we got all the EXPECTED tokens from the requests. done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION) for task in pending: task.cancel() for task in done: num_generated_tokens, request_id = await task assert num_generated_tokens == NUM_EXPECTED_TOKENS, ( f"{request_id} generated {num_generated_tokens} but " f"expected {NUM_EXPECTED_TOKENS}" ) assert not engine.output_processor.has_unfinished_requests() @pytest.mark.parametrize( "output_kind", [RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY] ) @pytest.mark.parametrize( "engine_args,prompt", [(TEXT_ENGINE_ARGS, TEXT_PROMPT), (VISION_ENGINE_ARGS, VISION_PROMPT)], ) @pytest.mark.asyncio async def test_abort( output_kind: RequestOutputKind, engine_args: AsyncEngineArgs, prompt: PromptType, ): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(engine_args) after.callback(engine.shutdown) NUM_REQUESTS = 100 NUM_EXPECTED_TOKENS = 100 NUM_EXPECTED_TOKENS_LONG = 50000 REQUEST_IDS_TO_ABORT = range(1, 100, 10) PARALLEL_SAMPLE_REQ_IDS = range(1, 100, 15) request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] # Create concurrent requests. tasks: list[asyncio.Task] = [] for idx, request_id in enumerate(request_ids): max_tokens = ( NUM_EXPECTED_TOKENS_LONG if (idx in REQUEST_IDS_TO_ABORT) else NUM_EXPECTED_TOKENS ) n = 3 if idx in PARALLEL_SAMPLE_REQ_IDS else 1 tasks.append( asyncio.create_task( generate(engine, request_id, prompt, output_kind, max_tokens, n) ) ) # API server cancels requests when they disconnect. for idx in REQUEST_IDS_TO_ABORT: tasks[idx].cancel() await asyncio.sleep(0.1) # Confirm the other requests are okay. for idx, task in enumerate(tasks): # Confirm that it was actually canceled. if idx in REQUEST_IDS_TO_ABORT: with pytest.raises(asyncio.CancelledError): await task else: # Otherwise, make sure the request was not impacted. num_generated_tokens, request_id = await task n = 3 if idx in PARALLEL_SAMPLE_REQ_IDS else 1 expected_tokens = NUM_EXPECTED_TOKENS * n assert num_generated_tokens == expected_tokens, ( f"{request_id} generated {num_generated_tokens} but " f"expected {expected_tokens}" ) # Make sure all aborted requests were really aborted. assert not engine.output_processor.has_unfinished_requests() # Confirm we can do another generation. request_id = f"request-{REQUEST_IDS_TO_ABORT[0]}" task = asyncio.create_task( generate(engine, request_id, prompt, output_kind, NUM_EXPECTED_TOKENS) ) num_generated_tokens, request_id = await task assert num_generated_tokens == NUM_EXPECTED_TOKENS assert not engine.output_processor.has_unfinished_requests() @pytest.mark.parametrize( "output_kind", [RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY] ) @pytest.mark.asyncio async def test_multi_abort(output_kind: RequestOutputKind): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(TEXT_ENGINE_ARGS) after.callback(engine.shutdown) NUM_REQUESTS = 50 NUM_EXPECTED_TOKENS = 100 NUM_EXPECTED_TOKENS_LONG = 50000 REQUEST_IDS_TO_ABORT = [5, 10, 15, 20, 25] PARALLEL_SAMPLE_REQ_IDS = [5, 15, 30, 35] request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] # Create concurrent requests. tasks: list[asyncio.Task] = [] for idx, request_id in enumerate(request_ids): max_tokens = ( NUM_EXPECTED_TOKENS_LONG if (idx in REQUEST_IDS_TO_ABORT) else NUM_EXPECTED_TOKENS ) n = 3 if idx in PARALLEL_SAMPLE_REQ_IDS else 1 tasks.append( asyncio.create_task( generate( engine, request_id, TEXT_PROMPT, output_kind, max_tokens, n ) ) ) # Let requests start await asyncio.sleep(0.5) # Use multi-abort to abort multiple requests at once abort_request_ids = [request_ids[i] for i in REQUEST_IDS_TO_ABORT] await engine.abort(abort_request_ids, internal=False) # Wait for all tasks to complete results = await asyncio.gather(*tasks, return_exceptions=True) # Verify results for idx, result in enumerate(results): if idx in REQUEST_IDS_TO_ABORT: # Aborted requests should return partial results assert isinstance(result, tuple), ( f"Request {idx} should have completed with partial results" ) num_generated_tokens, request_id = result # Should have generated some tokens before abort assert num_generated_tokens > 0, ( f"Aborted request {request_id} should have generated some tokens" ) else: # Non-aborted requests should complete normally assert isinstance(result, tuple), ( f"Request {idx} should have completed successfully" ) num_generated_tokens, request_id = result n = 3 if idx in PARALLEL_SAMPLE_REQ_IDS else 1 expected_tokens = NUM_EXPECTED_TOKENS * n assert num_generated_tokens == expected_tokens, ( f"{request_id} generated {num_generated_tokens} but " f"expected {expected_tokens}" ) # Make sure all aborted requests were cleaned up assert not engine.output_processor.has_unfinished_requests() @pytest.mark.parametrize("n", [1, 3]) @pytest.mark.parametrize( "engine_args,prompt", [(TEXT_ENGINE_ARGS, TEXT_PROMPT), (VISION_ENGINE_ARGS, VISION_PROMPT)], ) @pytest.mark.asyncio async def test_finished_flag( n: int, engine_args: AsyncEngineArgs, prompt: PromptType, ): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(engine_args) after.callback(engine.shutdown) sampling_params = SamplingParams( max_tokens=100, output_kind=RequestOutputKind.DELTA, temperature=1.0, seed=33, n=n, ) outputs = [ out async for out in engine.generate( request_id="request-33", prompt=prompt, sampling_params=sampling_params ) ] # Assert only the last output has the finished flag set assert all(not out.finished for out in outputs[:-1]) assert outputs[-1].finished @pytest.mark.parametrize( "engine_args,prompt", [(TEXT_ENGINE_ARGS, TEXT_PROMPT), (VISION_ENGINE_ARGS, VISION_PROMPT)], ) @pytest.mark.asyncio async def test_mid_stream_cancellation( engine_args: AsyncEngineArgs, prompt: PromptType ): """Test that requests can be cancelled mid-stream.""" with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(engine_args) after.callback(engine.shutdown) NUM_REQUESTS = 100 NUM_TOKENS = 1000 NUM_EXPECTED_TOKENS = 20 request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] # Create concurrent requests that will be cancelled mid-stream tasks = [] for request_id in request_ids: tasks.append( asyncio.create_task( generate( engine, request_id, prompt, RequestOutputKind.DELTA, NUM_TOKENS, cancel_after=NUM_EXPECTED_TOKENS, ) ) ) # Wait for all tasks to complete results = await asyncio.gather(*tasks) # Verify all tasks were cancelled at the expected point for num_generated_tokens, request_id in results: assert num_generated_tokens == NUM_EXPECTED_TOKENS, ( f"{request_id} generated {num_generated_tokens} tokens but " f"expected to cancel after {NUM_EXPECTED_TOKENS}" ) # Make sure no requests are left hanging assert not engine.output_processor.has_unfinished_requests() # Confirm we can reuse the request id after the cancellations. request_id = request_ids[0] task = asyncio.create_task( generate( engine, request_id, prompt, RequestOutputKind.DELTA, NUM_EXPECTED_TOKENS ) ) num_generated_tokens, request_id = await task assert num_generated_tokens == NUM_EXPECTED_TOKENS assert not engine.output_processor.has_unfinished_requests() class MockLoggingStatLogger(LoggingStatLogger): def __init__(self, vllm_config: VllmConfig, engine_index: int = 0): super().__init__(vllm_config, engine_index) self.log = MagicMock() class MockAggregatedStatLogger(AggregatedLoggingStatLogger): def __init__(self, vllm_config: VllmConfig, engine_indexes: list[int]): super().__init__(vllm_config, engine_indexes) self.log = MagicMock() @pytest.mark.asyncio async def test_customize_loggers(monkeypatch): """Test that we can customize the loggers. If a customized logger is provided at the init, it should be added to the default loggers. """ with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args( TEXT_ENGINE_ARGS, stat_loggers=[MockLoggingStatLogger], ) after.callback(engine.shutdown) await engine.do_log_stats() stat_loggers = engine.logger_manager.stat_loggers assert ( len(stat_loggers) == 3 ) # MockLoggingStatLogger + LoggingStatLogger + Promethus Logger print(f"{stat_loggers=}") stat_loggers[0].per_engine_stat_loggers[0].log.assert_called_once() assert isinstance(stat_loggers[1], PerEngineStatLoggerAdapter) assert isinstance(stat_loggers[1].per_engine_stat_loggers[0], LoggingStatLogger) assert isinstance(stat_loggers[2], PrometheusStatLogger) @pytest.mark.asyncio async def test_customize_aggregated_loggers(): """Test that we can customize the aggregated loggers. If a customized logger is provided at the init, it should be added to the default loggers. """ with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args( TEXT_ENGINE_ARGS, stat_loggers=[MockLoggingStatLogger, MockAggregatedStatLogger], ) after.callback(engine.shutdown) await engine.do_log_stats() stat_loggers = engine.logger_manager.stat_loggers assert len(stat_loggers) == 4 # MockLoggingStatLogger + MockAggregatedStatLogger # + LoggingStatLogger + PrometheusStatLogger stat_loggers[0].per_engine_stat_loggers[0].log.assert_called_once() stat_loggers[1].log.assert_called_once() assert isinstance(stat_loggers[2], PerEngineStatLoggerAdapter) assert isinstance(stat_loggers[2].per_engine_stat_loggers[0], LoggingStatLogger) assert isinstance(stat_loggers[3], PrometheusStatLogger) @pytest.mark.asyncio(scope="module") async def test_dp_rank_argument(): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(TEXT_ENGINE_ARGS) after.callback(engine.shutdown) sampling_params = SamplingParams( max_tokens=100, output_kind=RequestOutputKind.DELTA, temperature=1.0, seed=33, ) # Test with valid DP rank. async for _ in engine.generate( request_id="request-34", prompt=TEXT_PROMPT, sampling_params=sampling_params, data_parallel_rank=0, ): pass # Test with out-of-range DP rank. with pytest.raises(ValueError): async for _ in engine.generate( request_id="request-35", prompt=TEXT_PROMPT, sampling_params=sampling_params, data_parallel_rank=1, ): pass @pytest.mark.asyncio(scope="module") async def test_header_dp_rank_argument(): with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(TEXT_ENGINE_ARGS) after.callback(engine.shutdown) MODEL_NAME = "test-model" BASE_MODEL_PATHS = [BaseModelPath(name=MODEL_NAME, model_path=MODEL_NAME)] # Create models first models = OpenAIServingModels( engine_client=engine, base_model_paths=BASE_MODEL_PATHS, ) # Create serving chat instance serving_chat = OpenAIServingChat( engine_client=engine, models=models, response_role="assistant", chat_template=None, chat_template_content_format="auto", request_logger=None, ) # Create a chat completion request req = ChatCompletionRequest( model=MODEL_NAME, messages=[{"role": "user", "content": TEXT_PROMPT}], max_tokens=100, temperature=1.0, seed=33, ) # Test 1: Valid DP rank (0) mock_raw_request = MagicMock() mock_raw_request.headers = {"X-data-parallel-rank": "0"} mock_raw_request.state = MagicMock() # Should succeed with valid rank response = await serving_chat.create_chat_completion(req, mock_raw_request) assert isinstance(response, ChatCompletionResponse), ( "Expected a ChatCompletionResponse for valid DP rank" ) # Test 2: Out-of-range DP rank (1) mock_raw_request.headers = {"X-data-parallel-rank": "1"} # should return ErrorResponse for out-of-range rank response2 = await serving_chat.create_chat_completion(req, mock_raw_request) assert isinstance(response2, ErrorResponse), ( "Expected an ErrorResponse for out-of-range DP rank" ) @pytest.mark.asyncio async def test_check_health(): """Test that check_health returns normally for healthy engine and raises EngineDeadError when the engine is dead. """ from unittest.mock import patch from vllm.v1.engine.exceptions import EngineDeadError with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(TEXT_ENGINE_ARGS) after.callback(engine.shutdown) # Test 1: Healthy engine should not raise any exception await engine.check_health() # Test 2: Mock the errored property to simulate a dead engine with ( patch.object( type(engine), "errored", new_callable=lambda: property(lambda self: True), ), pytest.raises(EngineDeadError), ): await engine.check_health() # Test 3: Verify healthy engine still works after mock await engine.check_health() @pytest.mark.parametrize( "output_kind", [RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY] ) @pytest.mark.asyncio async def test_abort_final_output(output_kind: RequestOutputKind): """Test that abort() returns a final output with correct information.""" with ExitStack() as after: with set_default_torch_num_threads(1): engine = AsyncLLM.from_engine_args(TEXT_ENGINE_ARGS) after.callback(engine.shutdown) request_id = "test-abort-final-output" # Start a long-running request sampling_params = SamplingParams( max_tokens=3000, # Long enough to allow abort ignore_eos=True, output_kind=output_kind, temperature=0.5, seed=42, ) outputs: list[RequestOutput] = [] generated = asyncio.create_task( collect_outputs(engine, request_id, TEXT_PROMPT, sampling_params, outputs) ) # Let it generate some tokens await asyncio.sleep(0.5) # Abort the request await engine.abort(request_id, internal=False) # Wait for generation to complete and return final output final_output = await generated # Verify we got a final output assert final_output is not None assert final_output.finished assert len(final_output.outputs) == 1 assert final_output.outputs[0].finish_reason == "abort" assert final_output.outputs[0].stop_reason is None # Verify num_cached_tokens is set correctly assert hasattr(final_output, "num_cached_tokens") assert final_output.num_cached_tokens >= 0 # If we got intermediate outputs, verify they are consistent if output_kind == RequestOutputKind.DELTA: # For DELTA, sum all intermediate tokens should <= final tokens token_count = sum(len(output.outputs[0].token_ids) for output in outputs) assert token_count > 0 # This would ordinarily be 0, but could end up > 0 if the # final abort is coalesced with another chunk in the output queue. assert len(final_output.outputs[0].token_ids) >= 0 else: # For FINAL_ONLY, we should only get the final output assert len(outputs) == 0 assert len(final_output.outputs[0].token_ids) > 0 assert not engine.output_processor.has_unfinished_requests() async def collect_outputs( engine: AsyncLLM, request_id: str, prompt: PromptType, sampling_params: SamplingParams, outputs_list: list[RequestOutput], ) -> RequestOutput | None: """Helper to collect outputs and return the final one.""" final_output: RequestOutput | None = None async for output in engine.generate( request_id=request_id, prompt=prompt, sampling_params=sampling_params ): if not output.finished: outputs_list.append(output) final_output = output return final_output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_llm_engine.py
tests/v1/engine/test_llm_engine.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random from typing import TYPE_CHECKING import pytest from vllm import LLM from vllm.sampling_params import SamplingParams, StructuredOutputsParams from vllm.v1.metrics.reader import Counter, Gauge, Histogram, Metric, Vector if TYPE_CHECKING: from tests.conftest import VllmRunner else: VllmRunner = object MODEL = "facebook/opt-125m" DTYPE = "half" def _vllm_model( apc: bool, vllm_runner: type[VllmRunner], *, skip_tokenizer_init: bool = False, ): """Set up VllmRunner instance.""" return vllm_runner( MODEL, dtype=DTYPE, max_model_len=128, enforce_eager=True, enable_prefix_caching=apc, gpu_memory_utilization=0.5, skip_tokenizer_init=skip_tokenizer_init, ) @pytest.fixture( # Function scope decouples tests & allows # env var adjustment via monkeypatch scope="function", # Prefix caching params=[False, True], ) def vllm_model(vllm_runner, request): """VllmRunner test fixture parameterized by APC True/False.""" with _vllm_model(request.param, vllm_runner) as vllm_model: yield vllm_model @pytest.fixture(scope="function") def vllm_model_apc(vllm_runner): """VllmRunner test fixture with APC.""" with _vllm_model(True, vllm_runner) as vllm_model: yield vllm_model @pytest.fixture( # Function scope decouples tests & allows # env var adjustment via monkeypatch scope="function", # Prefix caching params=[False, True], ) def vllm_model_skip_tokenizer_init(vllm_runner, request): """VllmRunner test fixture with APC.""" with _vllm_model( request.param, vllm_runner, skip_tokenizer_init=True, ) as vllm_model: yield vllm_model def _get_test_sampling_params( prompt_list: list[str], seed: int | None = 42, structured_outputs: bool = False, ) -> tuple[list[SamplingParams], list[int]]: """Generate random sampling params for a batch.""" def get_mostly_n_gt1() -> int: r"""Mostly n \in [2,20], ~1/3 n=1""" x = random.randint(0, 28) if x < 10: return 1 else: return x - 8 n_list = [get_mostly_n_gt1() for _ in range(len(prompt_list))] # High temperature to maximize the chance of unique completions return [ SamplingParams( temperature=0.95, top_p=0.95, n=n, seed=seed, structured_outputs=StructuredOutputsParams(regex="[0-9]+") if structured_outputs else None, ) for n in n_list ], n_list def test_compatibility_with_skip_tokenizer_init( vllm_model_skip_tokenizer_init: VllmRunner, example_prompts: list[str], ): # Case 1: Structured output request should raise an error. sampling_params_list, _ = _get_test_sampling_params( example_prompts, structured_outputs=True, ) llm: LLM = vllm_model_skip_tokenizer_init.llm with pytest.raises(ValueError): _ = llm.generate(example_prompts, sampling_params_list) def test_parallel_sampling(vllm_model, example_prompts) -> None: """Test passes if parallel sampling `n>1` yields `n` unique completions. Args: vllm_model: VllmRunner instance under test. example_prompt: test fixture providing prompts for testing. """ sampling_params_list, n_list = _get_test_sampling_params(example_prompts) llm: LLM = vllm_model.llm outputs = llm.generate(example_prompts, sampling_params_list) # Validate each request response for out, n in zip(outputs, n_list): completion_counts: dict[str, int] = {} # Assert correct number of completions assert len(out.outputs) == n, f"{len(out.outputs)} completions; {n} expected." for idx in range(n): comp = out.outputs[idx] # Assert correct completion indices assert comp.index == idx, f"Index {comp.index}; expected {idx}." text = comp.text completion_counts[text] = completion_counts.get(text, 0) + 1 # Assert unique completions if len(completion_counts) != n: repeats = {txt: num for (txt, num) in completion_counts.items() if num > 1} raise AssertionError( f"{len(completion_counts)} unique completions; expected" f" {n}. Repeats: {repeats}" ) def test_engine_metrics(vllm_runner, example_prompts): max_tokens = 100 # Use spec decoding to test num_accepted_tokens_per_pos speculative_config = { "method": "ngram", "prompt_lookup_max": 5, "prompt_lookup_min": 3, "num_speculative_tokens": 5, } with vllm_runner( MODEL, speculative_config=speculative_config, disable_log_stats=False, ) as vllm_model: llm: LLM = vllm_model.llm sampling_params = SamplingParams(temperature=0.0, max_tokens=max_tokens) outputs = llm.generate(example_prompts, sampling_params) n_prompts = len(example_prompts) assert len(outputs) == n_prompts total_tokens = 0 for out in outputs: assert len(out.outputs) == 1 total_tokens += len(out.outputs[0].token_ids) assert total_tokens == max_tokens * n_prompts metrics = llm.get_metrics() def find_metric(name) -> list[Metric]: found = [] for metric in metrics: if metric.name == name: found.append(metric) return found num_requests_running = find_metric("vllm:num_requests_running") assert len(num_requests_running) == 1 assert isinstance(num_requests_running[0], Gauge) assert num_requests_running[0].value == 0.0 generation_tokens = find_metric("vllm:generation_tokens") assert len(generation_tokens) == 1 assert isinstance(generation_tokens[0], Counter) assert generation_tokens[0].value == total_tokens request_generation_tokens = find_metric("vllm:request_generation_tokens") assert len(request_generation_tokens) == 1 assert isinstance(request_generation_tokens[0], Histogram) assert "+Inf" in request_generation_tokens[0].buckets assert request_generation_tokens[0].buckets["+Inf"] == n_prompts assert request_generation_tokens[0].count == n_prompts assert request_generation_tokens[0].sum == total_tokens num_accepted_tokens_per_pos = find_metric( "vllm:spec_decode_num_accepted_tokens_per_pos" ) assert len(num_accepted_tokens_per_pos) == 1 assert isinstance(num_accepted_tokens_per_pos[0], Vector) assert len(num_accepted_tokens_per_pos[0].values) == 5 @pytest.mark.parametrize("model", ["meta-llama/Llama-3.2-1B-Instruct"]) def test_skip_tokenizer_initialization(model: str): # This test checks if the flag skip_tokenizer_init skips the initialization # of tokenizer and detokenizer. The generated output is expected to contain # token ids. llm = LLM( model=model, skip_tokenizer_init=True, enforce_eager=True, ) sampling_params = SamplingParams(prompt_logprobs=True, detokenize=True) with pytest.raises(ValueError, match="cannot pass text prompts when"): llm.generate("abc", sampling_params) outputs = llm.generate( {"prompt_token_ids": [1, 2, 3]}, sampling_params=sampling_params ) assert len(outputs) > 0 completions = outputs[0].outputs assert len(completions) > 0 assert completions[0].text == "" assert completions[0].token_ids
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_parallel_sampling.py
tests/v1/engine/test_parallel_sampling.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm import SamplingParams from vllm.outputs import CompletionOutput from vllm.sampling_params import RequestOutputKind from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.parallel_sampling import ParentRequest def test_parent_request_to_output_stream() -> None: parent_request = ParentRequest(make_request(SamplingParams(n=2))) parent_request.child_requests = {"child_id_0", "child_id_1"} output_0 = CompletionOutput( index=0, text="child 0", token_ids=[], cumulative_logprob=None, logprobs=None ) output_1 = CompletionOutput( index=1, text="child 1", token_ids=[], cumulative_logprob=None, logprobs=None ) # Request not finished assert ([output_0], False) == parent_request.get_outputs("child_id_0", output_0) assert ([output_1], False) == parent_request.get_outputs("child_id_1", output_1) assert ([output_0], False) == parent_request.get_outputs("child_id_0", output_0) assert ([output_1], False) == parent_request.get_outputs("child_id_1", output_1) # output_1 finished output_1.finish_reason = "ended" assert ([output_0], False) == parent_request.get_outputs("child_id_0", output_0) assert ([output_1], False) == parent_request.get_outputs("child_id_1", output_1) # Finished output_1 had already returned, DO NOT returned again assert ([output_0], False) == parent_request.get_outputs("child_id_0", output_0) assert parent_request.get_outputs("child_id_1", output_1) == ([], False) # output_0 finished output_0.finish_reason = "ended" assert ([output_0], True) == parent_request.get_outputs("child_id_0", output_0) assert parent_request.get_outputs("child_id_1", output_1) == ([], True) # Finished output_0 had already returned, DO NOT returned again assert parent_request.get_outputs("child_id_0", output_0) == ([], True) assert parent_request.get_outputs("child_id_1", output_1) == ([], True) def test_parent_request_to_output_final_only() -> None: parent_request = ParentRequest( make_request(SamplingParams(n=2, output_kind=RequestOutputKind.FINAL_ONLY)) ) parent_request.child_requests = {"child_id_0", "child_id_1"} output_0 = CompletionOutput( index=0, text="child 0", token_ids=[], cumulative_logprob=None, logprobs=None ) output_1 = CompletionOutput( index=1, text="child 1", token_ids=[], cumulative_logprob=None, logprobs=None ) # Request not finished, return nothing assert parent_request.get_outputs("child_id_0", output_0) == ([], False) assert parent_request.get_outputs("child_id_1", output_1) == ([], False) # output_1 finished, but outputs won't be returned until all child requests finished output_1.finish_reason = "ended" assert parent_request.get_outputs("child_id_0", output_0) == ([], False) assert parent_request.get_outputs("child_id_1", output_1) == ([], False) # output_0 finished, as all child requests finished, the output would be returned output_0.finish_reason = "ended" assert ([output_0, output_1], True) == parent_request.get_outputs( "child_id_0", output_0 ) assert ([output_0, output_1], True) == parent_request.get_outputs( "child_id_1", output_1 ) def make_request(sampling_params: SamplingParams) -> EngineCoreRequest: return EngineCoreRequest( request_id="parent_id", external_req_id="ext_parent_id", prompt_token_ids=None, mm_features=None, sampling_params=sampling_params, pooling_params=None, eos_token_id=None, arrival_time=0.0, lora_request=None, cache_salt=None, data_parallel_rank=None, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_fast_incdec_prefix_err.py
tests/v1/engine/test_fast_incdec_prefix_err.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from transformers import AutoTokenizer from vllm.sampling_params import SamplingParams from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.detokenizer import IncrementalDetokenizer # ruff: noqa: E501 def test_fast_inc_detok_invalid_utf8_err_case(): """ Test edge case where tokenizer can produce non-monotonic, invalid UTF-8 output, which breaks the internal state of tokenizers' DecodeStream. See https://github.com/vllm-project/vllm/issues/17448. Thanks to reproducer from @fpaupier: https://gist.github.com/fpaupier/0ed1375bd7633c5be6c894b1c7ac1be3. """ tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-1b-it") # Create a test request prompt_token_ids = [107, 4606, 236787, 107] params = SamplingParams(skip_special_tokens=True) request = EngineCoreRequest( request_id="test", external_req_id="test-ext", prompt_token_ids=prompt_token_ids, mm_features=None, sampling_params=params, pooling_params=None, eos_token_id=None, arrival_time=0.0, lora_request=None, cache_salt=None, data_parallel_rank=None, ) detokenizer = IncrementalDetokenizer.from_new_request(tokenizer, request) assert detokenizer.__class__.__name__ == "FastIncrementalDetokenizer", ( "Should use FastIncrementalDetokenizer by default" ) # Process tokens incrementally test_tokens = [ 236840, 107, 138, 236782, 107, 140, 236775, 6265, 1083, 623, 121908, 147418, 827, 107, 140, 236775, 6265, 236779, 2084, 1083, 623, 203292, 827, 107, 140, 236775, 6265, 236779, 7777, 1083, 623, 121908, 147418, 569, 537, 236789, 65880, 569, 537, 236789, 62580, 853, 115693, 210118, 35178, 16055, 1270, 759, 215817, 4758, 1925, 1117, 827, 107, 140, 236775, 5654, 1083, 623, 110733, 46291, 827, 107, 140, 236775, 5654, 236779, 2084, 1083, 623, 136955, 56731, 827, 107, 140, 236775, 5654, 236779, 7777, 1083, 623, 194776, 2947, 496, 109811, 1608, 890, 215817, 4758, 1925, 1117, 2789, 432, 398, 602, 31118, 569, 124866, 134772, 509, 19478, 1640, 33779, 236743, 236770, 236819, 236825, 236771, 432, 398, 432, 237167, 827, 107, 140, 236775, 77984, 1083, 623, 2709, 236745, 2555, 513, 236789, 602, 31118, 569, ] output = "" for i, token_id in enumerate(test_tokens): detokenizer.update([token_id], False) finished = i == len(test_tokens) - 1 output += detokenizer.get_next_output_text(finished, delta=True) assert ( output == r"""[ { "source": "Résultats", "source_type": "CONCEPT", "source_description": "Résultats de l'analyse de l'impact des opérations israéliennes sur la frontière libanaise", "target": "Israël", "target_type": "ORGANIZATION", "target_description": "Pays qui a obtenu à sa frontière libanaise « un niveau de calme inédit depuis les années 1960 »", "relationship": "Obtention d'un niveau de""" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/test_engine_core.py
tests/v1/engine/test_engine_core.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy import time import uuid from concurrent.futures import Future, ThreadPoolExecutor import pytest from transformers import AutoTokenizer from vllm import SamplingParams from vllm.config import ( CacheConfig, ECTransferConfig, KVTransferConfig, ModelConfig, SchedulerConfig, VllmConfig, ) from vllm.engine.arg_utils import EngineArgs from vllm.platforms import current_platform from vllm.utils.torch_utils import set_default_torch_num_threads from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.core import EngineCore from vllm.v1.executor.abstract import Executor from vllm.v1.executor.uniproc_executor import UniProcExecutor from vllm.v1.kv_cache_interface import KVCacheConfig from vllm.v1.outputs import ModelRunnerOutput from ...utils import create_new_process_for_each_test, multi_gpu_test if not current_platform.is_cuda(): pytest.skip(reason="V1 currently only supported on CUDA.", allow_module_level=True) MODEL_NAME = "hmellor/tiny-random-LlamaForCausalLM" TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME) # test_engine_core_concurrent_batches assumes exactly 12 tokens per prompt. # Adjust prompt if changing model to maintain 12-token length. PROMPT = "I am Gyoubu Masataka Oniwa" PROMPT_TOKENS = TOKENIZER(PROMPT).input_ids _REQUEST_COUNTER = 0 def make_request() -> EngineCoreRequest: global _REQUEST_COUNTER _REQUEST_COUNTER += 1 request_id = f"request-{_REQUEST_COUNTER}" return EngineCoreRequest( request_id=request_id, external_req_id=f"{request_id}-{uuid.uuid4()}", prompt_token_ids=PROMPT_TOKENS, mm_features=None, sampling_params=SamplingParams(), pooling_params=None, eos_token_id=None, arrival_time=time.time(), lora_request=None, cache_salt=None, data_parallel_rank=None, ) @create_new_process_for_each_test() def test_engine_core(): """Setup the EngineCore.""" engine_args = EngineArgs(model=MODEL_NAME) vllm_config = engine_args.create_engine_config() executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, executor_class=executor_class, log_stats=True ) """Test basic request lifecycle.""" # First request. engine_core.add_request(*engine_core.preprocess_add_request(make_request())) assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 0 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 1 # Second request. engine_core.add_request(*engine_core.preprocess_add_request(make_request())) assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 1 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 2 # Add two requests in a row. engine_core.add_request(*engine_core.preprocess_add_request(make_request())) engine_core.add_request(*engine_core.preprocess_add_request(make_request())) assert len(engine_core.scheduler.waiting) == 2 assert len(engine_core.scheduler.running) == 2 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 4 # Loop through until they are all done. while (outs := engine_core.step_fn()[0].get(0)) and outs.outputs: pass assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 0 """Test abort cycle.""" # Basic abort. req = make_request() request_id = req.request_id engine_core.add_request(*engine_core.preprocess_add_request(req)) assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 0 assert engine_core.scheduler.has_unfinished_requests() assert not engine_core.scheduler.has_finished_requests() _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 1 assert engine_core.scheduler.has_unfinished_requests() assert not engine_core.scheduler.has_finished_requests() engine_core.abort_requests([request_id]) assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 0 assert not engine_core.scheduler.has_unfinished_requests() assert engine_core.scheduler.has_finished_requests() _ = engine_core.step_fn() assert not engine_core.scheduler.has_unfinished_requests() assert not engine_core.scheduler.has_finished_requests() # Add, step, abort 1 of the 3. req0 = make_request() req1 = make_request() req2 = make_request() engine_core.add_request(*engine_core.preprocess_add_request(req0)) engine_core.add_request(*engine_core.preprocess_add_request(req1)) assert len(engine_core.scheduler.waiting) == 2 assert len(engine_core.scheduler.running) == 0 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 2 engine_core.add_request(*engine_core.preprocess_add_request(req2)) assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 2 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 3 # Abort just one. engine_core.abort_requests([req1.request_id]) assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 2 _ = engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 2 # Abort the other requests at the same time. engine_core.abort_requests([req2.request_id, req0.request_id]) assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 0 # Sending duplicate requests with same request_id req0 = make_request() req1 = make_request() req0.request_id = req1.request_id = "test" engine_core.add_request(*engine_core.preprocess_add_request(req0)) while engine_core.scheduler.has_requests(): engine_core.step_fn() engine_core.add_request(*engine_core.preprocess_add_request(req1)) while engine_core.scheduler.has_requests(): engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 0 @create_new_process_for_each_test() def test_engine_core_advanced_sampling(): """ A basic end-to-end test to verify that the engine functions correctly when additional sampling parameters, such as top_p, min_tokens, and presence_penalty, are set. """ """Setup the EngineCore.""" engine_args = EngineArgs(model=MODEL_NAME) vllm_config = engine_args.create_engine_config() executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, executor_class=executor_class, log_stats=True ) """Test basic request lifecycle.""" # First request. request: EngineCoreRequest = make_request() request.sampling_params = SamplingParams( min_tokens=4, presence_penalty=1.0, frequency_penalty=1.0, repetition_penalty=0.1, stop_token_ids=[1001, 1002], ) engine_core.add_request(*engine_core.preprocess_add_request(request)) def _check_engine_state(): assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 0 # Loop through until they are all done. while engine_core.scheduler.has_requests(): engine_core.step_fn() assert len(engine_core.scheduler.waiting) == 0 assert len(engine_core.scheduler.running) == 0 _check_engine_state() # Second request. request2 = make_request() request2.sampling_params = SamplingParams( top_p=0.99, top_k=50, ) engine_core.add_request(*engine_core.preprocess_add_request(request2)) _check_engine_state() @create_new_process_for_each_test() def test_engine_core_concurrent_batches(): """ Test that the engine can handle multiple concurrent batches. """ def make_request_with_max_tokens(req_id: str, max_tokens: int) -> EngineCoreRequest: request = make_request() request.request_id = req_id request.sampling_params.max_tokens = max_tokens return request class DummyExecutor(UniProcExecutor): def initialize_from_config(self, kv_cache_configs: list[KVCacheConfig]) -> None: super().initialize_from_config(kv_cache_configs) # Create a thread pool with a single worker self.thread_pool = ThreadPoolExecutor(max_workers=1) def execute_model( self, scheduler_output, non_block=False, ) -> Future[ModelRunnerOutput | None]: """Make execute_model non-blocking.""" # DummyExecutor used only for testing async case. assert non_block def _execute(): output = self.collective_rpc("execute_model", args=(scheduler_output,)) # Make a copy because output[0] may be reused # by the next batch. return copy.deepcopy(output[0]) # Use the thread pool instead of creating a new thread return self.thread_pool.submit(_execute) def sample_tokens( self, grammar_output, non_block=False ) -> Future[ModelRunnerOutput]: """Make sample_tokens non-blocking.""" # DummyExecutor used only for testing async case. assert non_block def _execute(): output = self.collective_rpc("sample_tokens", args=(grammar_output,)) # Make a copy because output[0] may be reused # by the next batch. return copy.deepcopy(output[0]) # Use the thread pool instead of creating a new thread return self.thread_pool.submit(_execute) @property def max_concurrent_batches(self) -> int: return 2 def shutdown(self): if hasattr(self, "thread_pool"): self.thread_pool.shutdown(wait=False) engine_args = EngineArgs( model=MODEL_NAME, # To test concurrent batches. max_num_seqs=2, # Avoid all requests being scheduled once. enable_prefix_caching=False, max_num_batched_tokens=10, # Reduce startup time. enforce_eager=True, # Test concurrent batch behaviour independently of async scheduling. async_scheduling=False, ) vllm_config = engine_args.create_engine_config() with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, log_stats=False, executor_class=DummyExecutor ) assert engine_core.batch_queue is not None # Add two requests in a row. Each request have 12 prompt tokens. req0 = make_request_with_max_tokens("0", 5) engine_core.add_request(*engine_core.preprocess_add_request(req0)) req1 = make_request_with_max_tokens("1", 5) engine_core.add_request(*engine_core.preprocess_add_request(req1)) # Schedule Batch 1: (10, req0) assert engine_core.step_with_batch_queue()[0] is None assert len(engine_core.batch_queue) == 1 scheduler_output = engine_core.batch_queue[-1][1] assert scheduler_output.num_scheduled_tokens["0"] == 10 # num_computed_tokens should have been updated immediately. assert engine_core.scheduler.requests[req0.request_id].num_computed_tokens == 10 # Schedule Batch 2: (2, req0), (8, req1) assert engine_core.step_with_batch_queue()[0] == {} assert len(engine_core.batch_queue) == 1 scheduler_output = engine_core.batch_queue[-1][1] assert scheduler_output.num_scheduled_tokens["0"] == 2 assert scheduler_output.num_scheduled_tokens["1"] == 8 # num_computed_tokens should have been updated immediately. assert engine_core.scheduler.requests["0"].num_computed_tokens == 12 assert engine_core.scheduler.requests["1"].num_computed_tokens == 8 assert engine_core.scheduler.get_num_unfinished_requests() == 2 # Finish Batch 1 and schedule Batch 3: (4, req1). # Note that req0 cannot be scheduled # because it is in the decoding stage now. engine_core.step_with_batch_queue() assert len(engine_core.batch_queue) == 1 scheduler_output = engine_core.batch_queue[-1][1] assert scheduler_output.num_scheduled_tokens["1"] == 4 # Finish Batch 2. Get first token of req0. # Schedule Batch 4: (1, req0). output = engine_core.step_with_batch_queue()[0].get(0) assert output is not None assert len(output.outputs) == 1 assert engine_core.scheduler.requests[req0.request_id].num_tokens == 13 scheduler_output = engine_core.batch_queue[-1][1] assert scheduler_output.num_scheduled_tokens["0"] == 1 # Finish Batch 3. Get first token of req1. Schedule Batch 5: (1, req1). output = engine_core.step_with_batch_queue()[0].get(0) assert output is not None assert len(output.outputs) == 1 assert engine_core.scheduler.requests[req1.request_id].num_tokens == 13 scheduler_output = engine_core.batch_queue[-1][1] assert scheduler_output.num_scheduled_tokens["1"] == 1 # Loop until req0 is finished. req_id = 0 expected_num_tokens = [ engine_core.scheduler.requests["0"].num_tokens + 1, engine_core.scheduler.requests["1"].num_tokens + 1, ] while engine_core.scheduler.get_num_unfinished_requests() == 2: output = engine_core.step_with_batch_queue()[0] # Every step consumes an output. assert output is not None assert len(output[0].outputs) == 1 if req_id in engine_core.scheduler.requests: assert ( engine_core.scheduler.requests[req_id].num_tokens == expected_num_tokens[req_id] ) expected_num_tokens[req_id] += 1 req_id = (req_id + 1) % 2 @multi_gpu_test(num_gpus=2) def test_engine_core_tp(): """ Test engine can initialize worker in tp properly """ """Setup the EngineCore.""" engine_args = EngineArgs( model=MODEL_NAME, tensor_parallel_size=2, # Reduce startup time. enforce_eager=True, ) vllm_config = engine_args.create_engine_config() executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, executor_class=executor_class, log_stats=True ) def get_worker_cache_config_field(worker, key: str): return getattr(worker.cache_config, key) num_gpu_blocks = engine_core.collective_rpc( get_worker_cache_config_field, args=("num_gpu_blocks",) ) num_cpu_blocks = engine_core.collective_rpc( get_worker_cache_config_field, args=("num_cpu_blocks",) ) assert all(x is not None for x in num_gpu_blocks) assert all(x is not None for x in num_cpu_blocks) @create_new_process_for_each_test() def test_engine_core_invalid_request_id_type(): """Test that engine raises TypeError for non-string request_id.""" engine_args = EngineArgs(model=MODEL_NAME) vllm_config = engine_args.create_engine_config() executor_class = Executor.get_class(vllm_config) with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, executor_class=executor_class, log_stats=True ) # Test with UUID object (common mistake) uuid_request = make_request() uuid_request.request_id = uuid.uuid4() # UUID object instead of string with pytest.raises(TypeError, match="request_id must be a string, got.*UUID"): engine_core.add_request(*engine_core.preprocess_add_request(uuid_request)) # Test with integer int_request = make_request() int_request.request_id = 12345 with pytest.raises(TypeError, match="request_id must be a string, got.*int"): engine_core.add_request(*engine_core.preprocess_add_request(int_request)) # Test with None none_request = make_request() none_request.request_id = None with pytest.raises(TypeError, match="request_id must be a string, got.*NoneType"): engine_core.add_request(*engine_core.preprocess_add_request(none_request)) # Verify engine is still functional after errors valid_request = make_request() engine_core.add_request(*engine_core.preprocess_add_request(valid_request)) assert len(engine_core.scheduler.waiting) == 1 assert len(engine_core.scheduler.running) == 0 @create_new_process_for_each_test() @pytest.mark.parametrize( ("ec_role", "gpu_memory_utilization", "enable_prefix_caching"), [ ("ec_producer", 0.01, False), # NOTE: ec_producer never allows prefix caching ("ec_consumer", 0.7, True), ("ec_consumer", 0.7, False), ], ) @pytest.mark.parametrize("use_kv_connector", [False, True]) def test_encoder_instance_zero_kv_cache( ec_role: str, gpu_memory_utilization: float, enable_prefix_caching: bool, use_kv_connector: bool, ): """EPD (Encoder-Prefill-Decode) Encoder-cache-specific tests This test verifies encoder-only instance initializes with 0 KV cache blocks. Under EPD disagg mode, Encoder instances (EC producer role) only execute vision encoder, so they don't need KV cache for text generation. """ # Form vllm config model_config = ModelConfig( model="llava-hf/llava-1.5-7b-hf", # Multimodal model enforce_eager=True, trust_remote_code=True, dtype="float16", seed=42, ) scheduler_config = SchedulerConfig( max_num_seqs=10, max_num_batched_tokens=512, max_model_len=512, disable_hybrid_kv_cache_manager=True, is_encoder_decoder=model_config.is_encoder_decoder, ) cache_config = CacheConfig( block_size=16, gpu_memory_utilization=gpu_memory_utilization, swap_space=0, cache_dtype="auto", enable_prefix_caching=enable_prefix_caching, ) kv_transfer_config = ( KVTransferConfig( kv_connector="ExampleConnector", kv_role="kv_both", kv_connector_extra_config={"shared_storage_path": "local_storage"}, ) if use_kv_connector else None ) ec_transfer_config = ECTransferConfig( ec_connector="ECExampleConnector", ec_role=ec_role, ec_connector_extra_config={"shared_storage_path": "/tmp/ec_test_encoder"}, ) vllm_config = VllmConfig( model_config=model_config, cache_config=cache_config, scheduler_config=scheduler_config, kv_transfer_config=kv_transfer_config, ec_transfer_config=ec_transfer_config, ) executor_class = Executor.get_class(vllm_config) print(f"executor_class: {executor_class}") with set_default_torch_num_threads(1): engine_core = EngineCore( vllm_config=vllm_config, executor_class=executor_class, log_stats=True ) # Check encoder cache manager exists assert engine_core.scheduler.encoder_cache_manager is not None, ( "encoder_cache_manager should exist" ) if ec_role == "ec_producer": # Check 1: num_blocks should be 0 # NOTE: num_blocks=1 as BlockPool always needs a null_block. kv_cache_config = engine_core.scheduler.kv_cache_manager.kv_cache_config print(f"kv_cache_config: {kv_cache_config}") assert kv_cache_config.num_blocks == 1, ( f"ec_producer should only have 1 KV blocks, " f"got {kv_cache_config.num_blocks}" ) # Check 2: kv_cache_groups should be empty assert len(kv_cache_config.kv_cache_groups) == 0, ( f"ec_producer should have 0 KV cache groups, " f"got {len(kv_cache_config.kv_cache_groups)}" ) # Check 3: kv_cache_tensors should be empty assert len(kv_cache_config.kv_cache_tensors) == 0, ( f"Encoder instance should have 0 KV cache tensors, " f"got {len(kv_cache_config.kv_cache_tensors)}" ) # Check 4: Verify EC connector is initialized and is producer assert engine_core.scheduler.ec_connector is not None, ( "Encoder instance should have EC connector" ) assert engine_core.scheduler.ec_connector.is_producer, ( "Encoder instance EC connector should be producer" ) # Check 5: Verify chunked prefill is disabled assert not vllm_config.scheduler_config.enable_chunked_prefill, ( "Encoder instance should disable chunked prefill (no KV cache)" ) elif ec_role == "ec_consumer": # Check 1: num_blocks should be > 1 kv_cache_config = engine_core.scheduler.kv_cache_manager.kv_cache_config print(f"kv_cache_config: {kv_cache_config}") assert kv_cache_config.num_blocks > 1, ( f"ec_consumer should have >1 KV blocks, got {kv_cache_config.num_blocks}" ) # Check 2: kv_cache_groups should NOT be empty assert len(kv_cache_config.kv_cache_groups) > 0, ( f"ec_consumer should have KV cache groups, " f"got {len(kv_cache_config.kv_cache_groups)}" ) # Check 3: Verify EC connector is consumer assert engine_core.scheduler.ec_connector is not None, ( "Consumer instance should have EC connector" ) assert not engine_core.scheduler.ec_connector.is_producer, ( "Consumer instance EC connector should be consumer" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/tests/v1/engine/conftest.py
tests/v1/engine/conftest.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from transformers import AutoTokenizer from tests.v1.engine.utils import ( FULL_STRINGS, NUM_PROMPT_LOGPROBS_UNDER_TEST, NUM_SAMPLE_LOGPROBS_UNDER_TEST, PROMPT_LEN, TOKENIZER_NAME, DummyOutputProcessorTestVectors, generate_dummy_prompt_logprobs_tensors, generate_dummy_sample_logprobs, ) from vllm.engine.arg_utils import EngineArgs from ...distributed.conftest import publisher_config, random_port # noqa: F401 EngineCoreSampleLogprobsType = list[tuple[torch.Tensor, torch.Tensor]] EngineCorePromptLogprobsType = tuple[torch.Tensor, torch.Tensor] def _build_test_vectors_no_logprobs() -> DummyOutputProcessorTestVectors: """Generate output processor dummy test vectors, without logprobs Returns: DummyOutputProcessorTestVectors instance with no logprobs """ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME) vllm_config = EngineArgs(model=TOKENIZER_NAME).create_engine_config() # Tokenize prompts under test & create dummy generated tokens prompt_tokens = [tokenizer(text).input_ids[:PROMPT_LEN] for text in FULL_STRINGS] generation_tokens = [ tokenizer(text).input_ids[PROMPT_LEN:] for text in FULL_STRINGS ] # Generate prompt strings prompt_strings = [ tokenizer.decode(prompt_tokens, skip_special_tokens=True) for prompt_tokens in prompt_tokens ] prompt_strings_len = [len(prompt_string) for prompt_string in prompt_strings] return DummyOutputProcessorTestVectors( tokenizer=tokenizer, vllm_config=vllm_config, full_tokens=[tokenizer(text).input_ids for text in FULL_STRINGS], prompt_tokens=prompt_tokens, generation_tokens=generation_tokens, prompt_strings=prompt_strings, prompt_strings_len=prompt_strings_len, generation_strings=[ text[prompt_len:] for text, prompt_len in zip(FULL_STRINGS, prompt_strings_len) ], prompt_logprobs=[], generation_logprobs=[], ) @pytest.fixture def dummy_test_vectors() -> DummyOutputProcessorTestVectors: """Generate output processor dummy test vectors, with logprobs Returns: DummyOutputProcessorTestVectors instance with logprobs """ # Build dummy test vectors without logprobs dtv = _build_test_vectors_no_logprobs() # Inject logprobs into dummy test vectors # data structure dtv.generation_logprobs = [ generate_dummy_sample_logprobs( sampled_tokens_list=tokens_list, num_logprobs=NUM_SAMPLE_LOGPROBS_UNDER_TEST, tokenizer=dtv.tokenizer, ) for tokens_list in dtv.generation_tokens ] dtv.prompt_logprobs = [ generate_dummy_prompt_logprobs_tensors( prompt_tokens_list=tokens_list, num_logprobs=NUM_PROMPT_LOGPROBS_UNDER_TEST, tokenizer=dtv.tokenizer, ) for tokens_list in dtv.prompt_tokens ] return dtv
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false