test_launch_render.py 5.13 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
"""E2E tests for render endpoints via `vllm launch` (GPU-less serving)."""
4
5
6
7
8

import httpx
import pytest
import pytest_asyncio

9
from ...utils import RemoteLaunchRenderServer
10
11
12
13
14
15
16

MODEL_NAME = "hmellor/tiny-random-LlamaForCausalLM"


@pytest.fixture(scope="module")
def server():
    args: list[str] = []
17
18
    with RemoteLaunchRenderServer(MODEL_NAME, args, max_wait_seconds=120) as srv:
        yield srv
19
20
21
22
23
24
25
26
27
28


@pytest_asyncio.fixture
async def client(server):
    async with httpx.AsyncClient(
        base_url=server.url_for(""), timeout=30.0
    ) as http_client:
        yield http_client


29
30
31
# -- Chat Completion Render --


32
@pytest.mark.asyncio
33
async def test_chat_render_basic(client):
34
    response = await client.post(
35
        "/v1/chat/completions/render",
36
37
        json={
            "model": MODEL_NAME,
38
            "messages": [{"role": "user", "content": "Hello, how are you?"}],
39
40
41
42
43
44
45
        },
    )

    assert response.status_code == 200
    data = response.json()

    assert isinstance(data, list)
46
    assert len(data) == 2
47

48
49
50
51
52
53
54
55
    conversation, engine_prompts = data

    assert isinstance(conversation, list)
    assert conversation[0]["role"] == "user"

    assert isinstance(engine_prompts, list)
    assert len(engine_prompts) > 0
    first_prompt = engine_prompts[0]
56
57
58
    assert "prompt_token_ids" in first_prompt
    assert "prompt" in first_prompt
    assert isinstance(first_prompt["prompt_token_ids"], list)
59
    assert all(isinstance(t, int) for t in first_prompt["prompt_token_ids"])
60
61
62


@pytest.mark.asyncio
63
async def test_chat_render_multi_turn(client):
64
65
66
67
68
    response = await client.post(
        "/v1/chat/completions/render",
        json={
            "model": MODEL_NAME,
            "messages": [
69
70
71
                {"role": "user", "content": "Hello"},
                {"role": "assistant", "content": "Hi there!"},
                {"role": "user", "content": "How are you?"},
72
73
74
75
76
            ],
        },
    )

    assert response.status_code == 200
77
    conversation, engine_prompts = response.json()
78

79
    assert len(conversation) == 3
80
    assert conversation[0]["role"] == "user"
81
82
    assert conversation[1]["role"] == "assistant"
    assert conversation[2]["role"] == "user"
83
    assert len(engine_prompts) > 0
84
    assert len(engine_prompts[0]["prompt_token_ids"]) > 0
85
86
87


@pytest.mark.asyncio
88
async def test_chat_render_invalid_model(client):
89
    response = await client.post(
90
        "/v1/chat/completions/render",
91
        json={
92
93
            "model": "nonexistent-model",
            "messages": [{"role": "user", "content": "Hello"}],
94
95
96
        },
    )

97
98
    assert response.status_code == 404
    assert "error" in response.json()
99
100


101
# -- Completion Render --
102
103
104


@pytest.mark.asyncio
105
async def test_completion_render_basic(client):
106
    response = await client.post(
107
        "/v1/completions/render",
108
109
        json={
            "model": MODEL_NAME,
110
            "prompt": "Once upon a time",
111
112
113
114
115
116
        },
    )

    assert response.status_code == 200
    data = response.json()

117
118
    assert isinstance(data, list)
    assert len(data) > 0
119

120
121
122
123
124
125
    first_prompt = data[0]
    assert "prompt_token_ids" in first_prompt
    assert "prompt" in first_prompt
    assert isinstance(first_prompt["prompt_token_ids"], list)
    assert len(first_prompt["prompt_token_ids"]) > 0
    assert "Once upon a time" in first_prompt["prompt"]
126
127
128


@pytest.mark.asyncio
129
async def test_completion_render_multiple_prompts(client):
130
131
132
    response = await client.post(
        "/v1/completions/render",
        json={
133
134
            "model": MODEL_NAME,
            "prompt": ["Hello world", "Goodbye world"],
135
136
137
        },
    )

138
    assert response.status_code == 200
139
    data = response.json()
140
141
142
143
144
145
146
147

    assert isinstance(data, list)
    assert len(data) == 2

    for prompt in data:
        assert "prompt_token_ids" in prompt
        assert "prompt" in prompt
        assert len(prompt["prompt_token_ids"]) > 0
148
149
150


@pytest.mark.asyncio
151
async def test_completion_render_invalid_model(client):
152
    response = await client.post(
153
        "/v1/completions/render",
154
        json={
155
156
            "model": "nonexistent-model",
            "prompt": "Hello",
157
158
159
160
        },
    )

    assert response.status_code == 404
161
    assert "error" in response.json()
162
163
164


@pytest.mark.asyncio
165
166
async def test_render_is_fast(client):
    """Render should complete quickly since there is no inference."""
167
168
169
170
171
172
173
174
175
176
177
178
179
    import time

    start = time.perf_counter()
    response = await client.post(
        "/v1/completions/render",
        json={
            "model": MODEL_NAME,
            "prompt": "Tell me a very long story about " * 10,
        },
    )
    elapsed = time.perf_counter() - start

    assert response.status_code == 200
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    assert elapsed < 2.0


# -- Health & Models --


@pytest.mark.asyncio
async def test_health_endpoint(client):
    response = await client.get("/health")
    assert response.status_code == 200


@pytest.mark.asyncio
async def test_models_endpoint(client):
    response = await client.get("/v1/models")
    assert response.status_code == 200
    data = response.json()
    assert "data" in data
    model_ids = [m["id"] for m in data["data"]]
    assert MODEL_NAME in model_ids