test_custom_executor.py 2.86 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
4
import asyncio
import os
5
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
6
7
8
9
10
11

import pytest

from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs
from vllm.engine.async_llm_engine import AsyncLLMEngine
from vllm.engine.llm_engine import LLMEngine
12
from vllm.executor.uniproc_executor import UniProcExecutor
13
from vllm.sampling_params import SamplingParams
14
15
import os
from ..utils import models_path_prefix
16
17
18
19
20
21


class Mock:
    ...


22
class CustomUniExecutor(UniProcExecutor):
23

24
    def collective_rpc(self,
25
                       method: Union[str, Callable],
26
27
28
                       timeout: Optional[float] = None,
                       args: Tuple = (),
                       kwargs: Optional[Dict] = None) -> List[Any]:
29
30
31
        # Drop marker to show that this was ran
        with open(".marker", "w"):
            ...
32
        return super().collective_rpc(method, timeout, args, kwargs)
33
34


35
CustomUniExecutorAsync = CustomUniExecutor
36
37


38
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "facebook/opt-125m")])
39
40
41
42
43
44
45
46
47
48
49
def test_custom_executor_type_checking(model):
    with pytest.raises(ValueError):
        engine_args = EngineArgs(model=model,
                                 distributed_executor_backend=Mock)
        LLMEngine.from_engine_args(engine_args)
    with pytest.raises(ValueError):
        engine_args = AsyncEngineArgs(model=model,
                                      distributed_executor_backend=Mock)
        AsyncLLMEngine.from_engine_args(engine_args)


50
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "facebook/opt-125m")])
51
def test_custom_executor(model, tmp_path):
52
    cwd = os.path.abspath(".")
53
    os.chdir(tmp_path)
54
55
56
57
    try:
        assert not os.path.exists(".marker")

        engine_args = EngineArgs(
58
59
60
            model=model,
            distributed_executor_backend=CustomUniExecutor,
        )
61
62
63
64
65
66
67
68
69
70
71
        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)


72
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "facebook/opt-125m")])
73
def test_custom_executor_async(model, tmp_path):
74
    cwd = os.path.abspath(".")
75
    os.chdir(tmp_path)
76
77
78
79
    try:
        assert not os.path.exists(".marker")

        engine_args = AsyncEngineArgs(
80
            model=model, distributed_executor_backend=CustomUniExecutorAsync)
81
82
83
84
85
86
87
88
89
90
91
92
93
        engine = AsyncLLMEngine.from_engine_args(engine_args)
        sampling_params = SamplingParams(max_tokens=1)

        async def t():
            stream = await engine.add_request("0", "foo", sampling_params)
            async for x in stream:
                ...

        asyncio.run(t())

        assert os.path.exists(".marker")
    finally:
        os.chdir(cwd)