test_executor.py 3.81 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
import asyncio
import os
6
from typing import Any, Callable, Optional, Union
7
8
9
10
11
12

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
13
from vllm.executor.uniproc_executor import UniProcExecutor
14
from vllm.sampling_params import SamplingParams
15
16
import os
from ..utils import models_path_prefix
17
18
from vllm.utils import SUPPORT_TC, gpuname
import vllm.envs as envs
19
20
21
22
23
24


class Mock:
    ...


25
class CustomUniExecutor(UniProcExecutor):
26

27
    def collective_rpc(self,
28
                       method: Union[str, Callable],
29
                       timeout: Optional[float] = None,
30
31
                       args: tuple = (),
                       kwargs: Optional[dict] = None) -> list[Any]:
32
33
34
        # Drop marker to show that this was ran
        with open(".marker", "w"):
            ...
35
        return super().collective_rpc(method, timeout, args, kwargs)
36
37


38
CustomUniExecutorAsync = CustomUniExecutor
39

zhuwenwen's avatar
zhuwenwen committed
40
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "distilbert/distilgpt2")])
41
42
43
44
45
46
47
48
49
50
51
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)


zhuwenwen's avatar
zhuwenwen committed
52
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "distilbert/distilgpt2")])
53
def test_custom_executor(model, tmp_path):
54
    cwd = os.path.abspath(".")
55
    os.chdir(tmp_path)
56
57
58
59
    try:
        assert not os.path.exists(".marker")

        engine_args = EngineArgs(
60
61
            model=model,
            distributed_executor_backend=CustomUniExecutor,
62
            enforce_eager=True,  # reduce test time
zhuwenwen's avatar
zhuwenwen committed
63
            block_size=64 if envs.VLLM_USE_FLASH_ATTN_PA else 16,
64
        )
65
66
67
68
69
70
71
72
73
74
75
        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)


76
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "distilbert/distilgpt2")])
77
def test_custom_executor_async(model, tmp_path):
78
    cwd = os.path.abspath(".")
79
    os.chdir(tmp_path)
80
81
82
83
    try:
        assert not os.path.exists(".marker")

        engine_args = AsyncEngineArgs(
84
85
86
            model=model,
            distributed_executor_backend=CustomUniExecutorAsync,
            enforce_eager=True,  # reduce test time
zhuwenwen's avatar
zhuwenwen committed
87
            block_size=64 if envs.VLLM_USE_FLASH_ATTN_PA else 16,
88
        )
89
90
91
92
93
94
95
96
97
98
99
100
101
        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)
102
103


104
@pytest.mark.parametrize("model", [os.path.join(models_path_prefix, "distilbert/distilgpt2")])
105
106
107
108
109
110
111
112
113
def test_respect_ray(model):
    # even for TP=1 and PP=1,
    # if users specify ray, we should use ray.
    # users might do this if they want to manage the
    # resources using ray.
    engine_args = EngineArgs(
        model=model,
        distributed_executor_backend="ray",
        enforce_eager=True,  # reduce test time
zhuwenwen's avatar
zhuwenwen committed
114
        block_size=64 if envs.VLLM_USE_FLASH_ATTN_PA else 16,
115
116
    )
    engine = LLMEngine.from_engine_args(engine_args)
117
    assert engine.model_executor.uses_ray