test_api_server.py 3.56 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
import os
4
5
6
7
8
9
10
11
import subprocess
import sys
import time
from multiprocessing import Pool
from pathlib import Path

import pytest
import requests
12
13
import os
from ..utils import models_path_prefix
14
15


16
def _query_server(prompt: str, max_tokens: int = 5) -> dict:
17
18
19
    response = requests.post("http://localhost:8000/generate",
                             json={
                                 "prompt": prompt,
20
                                 "max_tokens": max_tokens,
21
22
23
24
25
26
27
                                 "temperature": 0,
                                 "ignore_eos": True
                             })
    response.raise_for_status()
    return response.json()


28
29
30
31
def _query_server_long(prompt: str) -> dict:
    return _query_server(prompt, max_tokens=500)


32
@pytest.fixture
33
def api_server(tokenizer_pool_size: int, distributed_executor_backend: str):
34
35
    script_path = Path(__file__).parent.joinpath(
        "api_server_async_engine.py").absolute()
36
    commands = [
37
38
39
40
        sys.executable,
        "-u",
        str(script_path),
        "--model",
zhuwenwen's avatar
zhuwenwen committed
41
        os.path.join(models_path_prefix, "facebook/opt-125m"),
42
43
44
45
46
47
        "--host",
        "127.0.0.1",
        "--tokenizer-pool-size",
        str(tokenizer_pool_size),
        "--distributed-executor-backend",
        distributed_executor_backend,
48
    ]
49

50
51
52
53
    # API Server Test Requires V0.
    my_env = os.environ.copy()
    my_env["VLLM_USE_V1"] = "0"
    uvicorn_process = subprocess.Popen(commands, env=my_env)
54
55
56
57
    yield
    uvicorn_process.terminate()


58
@pytest.mark.parametrize("tokenizer_pool_size", [0, 2])
59
@pytest.mark.parametrize("distributed_executor_backend", ["mp", "ray"])
60
def test_api_server(api_server, tokenizer_pool_size: int,
61
                    distributed_executor_backend: str):
62
63
64
65
66
67
68
69
70
71
72
    """
    Run the API server and test it.

    We run both the server and requests in separate processes.

    We test that the server can handle incoming requests, including
    multiple requests at the same time, and that it can handle requests
    being cancelled without crashing.
    """
    with Pool(32) as pool:
        # Wait until the server is ready
73
        prompts = ["warm up"] * 1
74
75
76
        result = None
        while not result:
            try:
77
78
                for r in pool.map(_query_server, prompts):
                    result = r
79
                    break
80
            except requests.exceptions.ConnectionError:
81
82
83
84
85
86
87
88
89
90
91
92
                time.sleep(1)

        # Actual tests start here
        # Try with 1 prompt
        for result in pool.map(_query_server, prompts):
            assert result

        num_aborted_requests = requests.get(
            "http://localhost:8000/stats").json()["num_aborted_requests"]
        assert num_aborted_requests == 0

        # Try with 100 prompts
93
        prompts = ["test prompt"] * 100
94
95
96
        for result in pool.map(_query_server, prompts):
            assert result

97
    with Pool(32) as pool:
98
        # Cancel requests
99
        prompts = ["canceled requests"] * 100
100
101
        pool.map_async(_query_server_long, prompts)
        time.sleep(0.01)
102
103
104
105
        pool.terminate()
        pool.join()

        # check cancellation stats
Simon Mo's avatar
Simon Mo committed
106
107
108
        # give it some times to update the stats
        time.sleep(1)

109
110
111
112
113
114
115
        num_aborted_requests = requests.get(
            "http://localhost:8000/stats").json()["num_aborted_requests"]
        assert num_aborted_requests > 0

    # check that server still runs after cancellations
    with Pool(32) as pool:
        # Try with 100 prompts
116
        prompts = ["test prompt after canceled"] * 100
117
118
        for result in pool.map(_query_server, prompts):
            assert result