|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +from contextlib import ExitStack |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from vllm import SamplingParams |
| 11 | +from vllm.engine.arg_utils import AsyncEngineArgs |
| 12 | +from vllm.inputs import PromptType |
| 13 | +from vllm.platforms import current_platform |
| 14 | +from vllm.sampling_params import RequestOutputKind |
| 15 | +from vllm.v1.engine.async_llm import AsyncLLM |
| 16 | +from vllm.v1.engine.core_client import DPAsyncMPClient |
| 17 | + |
| 18 | +engine_args = AsyncEngineArgs( |
| 19 | + model="ibm-research/PowerMoE-3b", |
| 20 | + enforce_eager=True, |
| 21 | + disable_log_requests=True, |
| 22 | + tensor_parallel_size=int(os.getenv("TP_SIZE", 1)), |
| 23 | + data_parallel_size=int(os.getenv("DP_SIZE", 2)), |
| 24 | +) |
| 25 | + |
| 26 | +if not current_platform.supports_v1(engine_args.create_model_config()): |
| 27 | + pytest.skip(reason="Requires V1-supporting platform.", |
| 28 | + allow_module_level=True) |
| 29 | + |
| 30 | + |
| 31 | +async def generate(engine: AsyncLLM, |
| 32 | + request_id: str, |
| 33 | + prompt: PromptType, |
| 34 | + output_kind: RequestOutputKind, |
| 35 | + max_tokens: int, |
| 36 | + prompt_logprobs: Optional[int] = None) -> tuple[int, str]: |
| 37 | + # Ensure generate doesn't complete too fast for cancellation test. |
| 38 | + await asyncio.sleep(0.2) |
| 39 | + |
| 40 | + count = 0 |
| 41 | + sampling_params = SamplingParams(max_tokens=max_tokens, |
| 42 | + ignore_eos=True, |
| 43 | + output_kind=output_kind, |
| 44 | + temperature=0, |
| 45 | + prompt_logprobs=prompt_logprobs) |
| 46 | + async for out in engine.generate(request_id=request_id, |
| 47 | + prompt=prompt, |
| 48 | + sampling_params=sampling_params): |
| 49 | + |
| 50 | + num_tokens = len(out.outputs[0].token_ids) |
| 51 | + if output_kind == RequestOutputKind.DELTA: |
| 52 | + count += num_tokens |
| 53 | + else: |
| 54 | + count = num_tokens |
| 55 | + |
| 56 | + await asyncio.sleep(0.) |
| 57 | + |
| 58 | + return count, request_id |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "output_kind", [RequestOutputKind.DELTA, RequestOutputKind.FINAL_ONLY]) |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_load(output_kind: RequestOutputKind): |
| 65 | + |
| 66 | + with ExitStack() as after: |
| 67 | + |
| 68 | + prompt = "This is a test of data parallel" |
| 69 | + |
| 70 | + engine = AsyncLLM.from_engine_args(engine_args) |
| 71 | + after.callback(engine.shutdown) |
| 72 | + |
| 73 | + NUM_REQUESTS = 100 |
| 74 | + NUM_EXPECTED_TOKENS = 10 |
| 75 | + |
| 76 | + request_ids = [f"request-{i}" for i in range(NUM_REQUESTS)] |
| 77 | + |
| 78 | + # Create concurrent requests. |
| 79 | + tasks = [] |
| 80 | + for request_id in request_ids: |
| 81 | + tasks.append( |
| 82 | + asyncio.create_task( |
| 83 | + generate(engine, request_id, prompt, output_kind, |
| 84 | + NUM_EXPECTED_TOKENS))) |
| 85 | + |
| 86 | + # Confirm that we got all the EXPECTED tokens from the requests. |
| 87 | + done, pending = await asyncio.wait(tasks, |
| 88 | + return_when=asyncio.FIRST_EXCEPTION) |
| 89 | + for task in pending: |
| 90 | + task.cancel() |
| 91 | + for task in done: |
| 92 | + num_generated_tokens, request_id = await task |
| 93 | + assert num_generated_tokens == NUM_EXPECTED_TOKENS, ( |
| 94 | + f"{request_id} generated {num_generated_tokens} but " |
| 95 | + f"expected {NUM_EXPECTED_TOKENS}") |
| 96 | + |
| 97 | + assert not engine.output_processor.has_unfinished_requests() |
| 98 | + |
| 99 | + # testing internals here which may break |
| 100 | + core_client: DPAsyncMPClient = engine.engine_core |
| 101 | + # the engines only synchronize stopping every N steps so |
| 102 | + # allow a small amount of time here. |
| 103 | + for _ in range(10): |
| 104 | + if core_client.num_engines_running == 0: |
| 105 | + break |
| 106 | + await asyncio.sleep(0.5) |
| 107 | + |
| 108 | + assert core_client.num_engines_running == 0 |
| 109 | + assert not core_client.reqs_in_flight |
0 commit comments