|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import asyncio |
| 5 | +import os |
| 6 | +from typing import Any, Callable, Optional, Union |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs |
| 11 | +from vllm.sampling_params import SamplingParams |
| 12 | +from vllm.v1.engine.async_llm import AsyncLLM |
| 13 | +from vllm.v1.engine.llm_engine import LLMEngine |
| 14 | +from vllm.v1.executor.multiproc_executor import MultiprocExecutor |
| 15 | + |
| 16 | + |
| 17 | +class Mock: |
| 18 | + ... |
| 19 | + |
| 20 | + |
| 21 | +class CustomMultiprocExecutor(MultiprocExecutor): |
| 22 | + |
| 23 | + def collective_rpc(self, |
| 24 | + method: Union[str, Callable], |
| 25 | + timeout: Optional[float] = None, |
| 26 | + args: tuple = (), |
| 27 | + kwargs: Optional[dict] = None, |
| 28 | + non_block: bool = False, |
| 29 | + unique_reply_rank: Optional[int] = None) -> list[Any]: |
| 30 | + # Drop marker to show that this was ran |
| 31 | + with open(".marker", "w"): |
| 32 | + ... |
| 33 | + return super().collective_rpc(method, timeout, args, kwargs) |
| 34 | + |
| 35 | + |
| 36 | +CustomMultiprocExecutorAsync = CustomMultiprocExecutor |
| 37 | +MODEL = "Qwen/Qwen3-0.6B" |
| 38 | + |
| 39 | + |
| 40 | +def test_custom_executor_type_checking(): |
| 41 | + with pytest.raises(ValueError): |
| 42 | + engine_args = EngineArgs( |
| 43 | + model=MODEL, |
| 44 | + gpu_memory_utilization=0.2, |
| 45 | + max_model_len=8192, |
| 46 | + distributed_executor_backend=Mock, |
| 47 | + ) |
| 48 | + LLMEngine.from_engine_args(engine_args) |
| 49 | + with pytest.raises(ValueError): |
| 50 | + engine_args = AsyncEngineArgs(model=MODEL, |
| 51 | + gpu_memory_utilization=0.2, |
| 52 | + max_model_len=8192, |
| 53 | + distributed_executor_backend=Mock) |
| 54 | + AsyncLLM.from_engine_args(engine_args) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize("distributed_executor_backend", [ |
| 58 | + CustomMultiprocExecutor, |
| 59 | + "tests.v1.executor.test_executor.CustomMultiprocExecutor" |
| 60 | +]) |
| 61 | +def test_custom_executor(distributed_executor_backend, tmp_path): |
| 62 | + cwd = os.path.abspath(".") |
| 63 | + os.chdir(tmp_path) |
| 64 | + try: |
| 65 | + assert not os.path.exists(".marker") |
| 66 | + |
| 67 | + engine_args = EngineArgs( |
| 68 | + model=MODEL, |
| 69 | + gpu_memory_utilization=0.2, |
| 70 | + max_model_len=8192, |
| 71 | + distributed_executor_backend=distributed_executor_backend, |
| 72 | + enforce_eager=True, # reduce test time |
| 73 | + ) |
| 74 | + engine = LLMEngine.from_engine_args(engine_args) |
| 75 | + sampling_params = SamplingParams(max_tokens=1) |
| 76 | + |
| 77 | + engine.add_request("0", "foo", sampling_params) |
| 78 | + engine.step() |
| 79 | + |
| 80 | + assert os.path.exists(".marker") |
| 81 | + finally: |
| 82 | + os.chdir(cwd) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("distributed_executor_backend", [ |
| 86 | + CustomMultiprocExecutorAsync, |
| 87 | + "tests.v1.executor.test_executor.CustomMultiprocExecutorAsync" |
| 88 | +]) |
| 89 | +def test_custom_executor_async(distributed_executor_backend, tmp_path): |
| 90 | + cwd = os.path.abspath(".") |
| 91 | + os.chdir(tmp_path) |
| 92 | + try: |
| 93 | + assert not os.path.exists(".marker") |
| 94 | + |
| 95 | + engine_args = AsyncEngineArgs( |
| 96 | + model=MODEL, |
| 97 | + gpu_memory_utilization=0.2, |
| 98 | + max_model_len=8192, |
| 99 | + distributed_executor_backend=distributed_executor_backend, |
| 100 | + enforce_eager=True, # reduce test time |
| 101 | + ) |
| 102 | + engine = AsyncLLM.from_engine_args(engine_args) |
| 103 | + sampling_params = SamplingParams(max_tokens=1) |
| 104 | + |
| 105 | + async def t(): |
| 106 | + stream = engine.generate(request_id="0", |
| 107 | + prompt="foo", |
| 108 | + sampling_params=sampling_params) |
| 109 | + async for x in stream: |
| 110 | + ... |
| 111 | + |
| 112 | + asyncio.run(t()) |
| 113 | + |
| 114 | + assert os.path.exists(".marker") |
| 115 | + finally: |
| 116 | + os.chdir(cwd) |
0 commit comments