test_api_server.py 3.67 KB
Newer Older
1
import os
2
3
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
def _query_server(prompt: str, max_tokens: int = 5) -> dict:
13
14
15
    response = requests.post("http://localhost:8000/generate",
                             json={
                                 "prompt": prompt,
16
                                 "max_tokens": max_tokens,
17
18
19
20
21
22
23
                                 "temperature": 0,
                                 "ignore_eos": True
                             })
    response.raise_for_status()
    return response.json()


24
25
26
27
def _query_server_long(prompt: str) -> dict:
    return _query_server(prompt, max_tokens=500)


28
@pytest.fixture
29
30
def api_server(tokenizer_pool_size: int, engine_use_ray: bool,
               worker_use_ray: bool):
31
32
    script_path = Path(__file__).parent.joinpath(
        "api_server_async_engine.py").absolute()
33
    commands = [
34
35
36
37
        sys.executable, "-u",
        str(script_path), "--model", "facebook/opt-125m", "--host",
        "127.0.0.1", "--tokenizer-pool-size",
        str(tokenizer_pool_size)
38
    ]
39
40
41
42
43
44

    # Copy the environment variables and append `VLLM_ALLOW_ENGINE_USE_RAY=1`
    # to prevent `--engine-use-ray` raises an exception due to it deprecation
    env_vars = os.environ.copy()
    env_vars["VLLM_ALLOW_ENGINE_USE_RAY"] = "1"

45
46
47
48
    if engine_use_ray:
        commands.append("--engine-use-ray")
    if worker_use_ray:
        commands.append("--worker-use-ray")
49
    uvicorn_process = subprocess.Popen(commands, env=env_vars)
50
51
52
53
    yield
    uvicorn_process.terminate()


54
@pytest.mark.parametrize("tokenizer_pool_size", [0, 2])
55
56
57
58
@pytest.mark.parametrize("worker_use_ray", [False, True])
@pytest.mark.parametrize("engine_use_ray", [False, True])
def test_api_server(api_server, tokenizer_pool_size: int, worker_use_ray: bool,
                    engine_use_ray: bool):
59
60
61
62
63
64
65
66
67
68
69
    """
    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
70
        prompts = ["warm up"] * 1
71
72
73
        result = None
        while not result:
            try:
74
75
                for r in pool.map(_query_server, prompts):
                    result = r
76
                    break
77
            except requests.exceptions.ConnectionError:
78
79
80
81
82
83
84
85
86
87
88
89
                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
90
        prompts = ["test prompt"] * 100
91
92
93
        for result in pool.map(_query_server, prompts):
            assert result

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

        # check cancellation stats
Simon Mo's avatar
Simon Mo committed
103
104
105
        # give it some times to update the stats
        time.sleep(1)

106
107
108
109
110
111
112
        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
113
        prompts = ["test prompt after canceled"] * 100
114
115
        for result in pool.map(_query_server, prompts):
            assert result