test_classification.py 9.67 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5

import pytest
import requests
6
7
import torch
import torch.nn.functional as F
8

9
from tests.utils import RemoteOpenAIServer
10
from vllm.entrypoints.openai.protocol import ClassificationResponse, PoolingResponse
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

MODEL_NAME = "jason9693/Qwen2.5-1.5B-apeach"
DTYPE = "float32"  # Use float32 to avoid NaN issue


@pytest.fixture(scope="module")
def server():
    args = [
        "--enforce-eager",
        "--max-model-len",
        "512",
        "--dtype",
        DTYPE,
    ]

    with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
        yield remote_server


@pytest.mark.parametrize("model_name", [MODEL_NAME])
31
def test_single_input_classification(server: RemoteOpenAIServer, model_name: str):
32
33
34
35
    input_text = "This product was excellent and exceeded my expectations"

    classification_response = requests.post(
        server.url_for("classify"),
36
        json={"model": model_name, "input": input_text},
37
38
39
    )

    classification_response.raise_for_status()
40
    output = ClassificationResponse.model_validate(classification_response.json())
41
42
43
44
45
46
47
48

    assert output.object == "list"
    assert output.model == MODEL_NAME
    assert len(output.data) == 1
    assert hasattr(output.data[0], "label")
    assert hasattr(output.data[0], "probs")


49
50
51
52
53
54
55
56
57
58
@pytest.mark.parametrize("model_name", [MODEL_NAME])
def test_add_special_tokens_false(server: RemoteOpenAIServer, model_name: str):
    response = requests.post(
        server.url_for("classify"),
        json={"model": model_name, "input": "hello", "add_special_tokens": False},
    )
    response.raise_for_status()
    ClassificationResponse.model_validate(response.json())


59
@pytest.mark.parametrize("model_name", [MODEL_NAME])
60
def test_multiple_inputs_classification(server: RemoteOpenAIServer, model_name: str):
61
62
63
64
65
66
67
68
69
70
71
    input_texts = [
        "The product arrived on time and works perfectly",
        "I'm very satisfied with my purchase, would buy again",
        "The customer service was helpful and resolved my issue quickly",
        "This product broke after one week, terrible quality",
        "I'm very disappointed with this purchase, complete waste of money",
        "The customer service was rude and unhelpful",
    ]

    classification_response = requests.post(
        server.url_for("classify"),
72
        json={"model": model_name, "input": input_texts},
73
    )
74
    output = ClassificationResponse.model_validate(classification_response.json())
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    assert len(output.data) == len(input_texts)
    for i, item in enumerate(output.data):
        assert item.index == i
        assert hasattr(item, "label")
        assert hasattr(item, "probs")
        assert len(item.probs) == item.num_classes
        assert item.label in ["Default", "Spoiled"]


@pytest.mark.parametrize("model_name", [MODEL_NAME])
def test_truncate_prompt_tokens(server: RemoteOpenAIServer, model_name: str):
    long_text = "hello " * 600

    classification_response = requests.post(
        server.url_for("classify"),
91
        json={"model": model_name, "input": long_text, "truncate_prompt_tokens": 5},
92
93
94
    )

    classification_response.raise_for_status()
95
    output = ClassificationResponse.model_validate(classification_response.json())
96
97
98
99
100
101
102
103
104

    assert len(output.data) == 1
    assert output.data[0].index == 0
    assert hasattr(output.data[0], "probs")
    assert output.usage.prompt_tokens == 5
    assert output.usage.total_tokens == 5


@pytest.mark.parametrize("model_name", [MODEL_NAME])
105
106
107
def test_invalid_truncate_prompt_tokens_error(
    server: RemoteOpenAIServer, model_name: str
):
108
109
    classification_response = requests.post(
        server.url_for("classify"),
110
        json={"model": model_name, "input": "test", "truncate_prompt_tokens": 513},
111
112
113
114
    )

    error = classification_response.json()
    assert classification_response.status_code == 400
115
    assert "truncate_prompt_tokens" in error["error"]["message"]
116
117
118
119
120
121


@pytest.mark.parametrize("model_name", [MODEL_NAME])
def test_empty_input_error(server: RemoteOpenAIServer, model_name: str):
    classification_response = requests.post(
        server.url_for("classify"),
122
        json={"model": model_name, "input": ""},
123
124
125
126
    )

    error = classification_response.json()
    assert classification_response.status_code == 400
127
    assert "error" in error
128
129
130


@pytest.mark.parametrize("model_name", [MODEL_NAME])
131
def test_batch_classification_empty_list(server: RemoteOpenAIServer, model_name: str):
132
133
    classification_response = requests.post(
        server.url_for("classify"),
134
        json={"model": model_name, "input": []},
135
136
    )
    classification_response.raise_for_status()
137
    output = ClassificationResponse.model_validate(classification_response.json())
138
139
140
141

    assert output.object == "list"
    assert isinstance(output.data, list)
    assert len(output.data) == 0
142
143
144
145
146
147


@pytest.mark.asyncio
async def test_invocations(server: RemoteOpenAIServer):
    request_args = {
        "model": MODEL_NAME,
148
        "input": "This product was excellent and exceeded my expectations",
149
150
    }

151
152
153
    classification_response = requests.post(
        server.url_for("classify"), json=request_args
    )
154
155
    classification_response.raise_for_status()

156
157
158
    invocation_response = requests.post(
        server.url_for("invocations"), json=request_args
    )
159
160
161
162
163
164
    invocation_response.raise_for_status()

    classification_output = classification_response.json()
    invocation_output = invocation_response.json()

    assert classification_output.keys() == invocation_output.keys()
165
    for classification_data, invocation_data in zip(
166
167
        classification_output["data"], invocation_output["data"]
    ):
168
169
        assert classification_data.keys() == invocation_data.keys()
        assert classification_data["probs"] == pytest.approx(
170
171
            invocation_data["probs"], rel=0.01
        )
172
173
174
175


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
176
async def test_use_activation(server: RemoteOpenAIServer, model_name: str):
177
178
    input_text = ["This product was excellent and exceeded my expectations"]

179
    async def get_outputs(use_activation):
180
181
        response = requests.post(
            server.url_for("classify"),
182
183
184
185
186
            json={
                "model": model_name,
                "input": input_text,
                "use_activation": use_activation,
            },
187
        )
188
        outputs = response.json()
189
        return torch.tensor([x["probs"] for x in outputs["data"]])
190

191
192
193
    default = await get_outputs(use_activation=None)
    w_activation = await get_outputs(use_activation=True)
    wo_activation = await get_outputs(use_activation=False)
194

195
196
197
198
199
200
201
202
203
    assert torch.allclose(default, w_activation, atol=1e-2), (
        "Default should use activation."
    )
    assert not torch.allclose(w_activation, wo_activation, atol=1e-2), (
        "wo_activation should not use activation."
    )
    assert torch.allclose(F.softmax(wo_activation, dim=-1), w_activation, atol=1e-2), (
        "w_activation should be close to activation(wo_activation)."
    )
204
205
206
207


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
208
async def test_score(server: RemoteOpenAIServer, model_name: str):
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    # score api is only enabled for num_labels == 1.
    response = requests.post(
        server.url_for("score"),
        json={
            "model": model_name,
            "text_1": "ping",
            "text_2": "pong",
        },
    )
    assert response.json()["error"]["type"] == "BadRequestError"


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
223
async def test_rerank(server: RemoteOpenAIServer, model_name: str):
224
225
226
227
228
229
230
231
232
233
    # rerank api is only enabled for num_labels == 1.
    response = requests.post(
        server.url_for("rerank"),
        json={
            "model": model_name,
            "query": "ping",
            "documents": ["pong"],
        },
    )
    assert response.json()["error"]["type"] == "BadRequestError"
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
async def test_pooling_classify(server: RemoteOpenAIServer, model_name: str):
    input_text = "This product was excellent and exceeded my expectations"
    response = requests.post(
        server.url_for("pooling"),
        json={
            "model": model_name,
            "input": input_text,
            "encoding_format": "float",
            "task": "classify",
        },
    )
    poolings = PoolingResponse.model_validate(response.json())
    assert len(poolings.data) == 1
    assert len(poolings.data[0].data) == 2


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
async def test_pooling_token_classify(server: RemoteOpenAIServer, model_name: str):
    # token_classify uses ALL pooling, which does not support chunked prefill.
    task = "token_classify"
    response = requests.post(
        server.url_for("pooling"),
        json={
            "model": model_name,
            "input": "test",
            "encoding_format": "float",
            "task": task,
        },
    )
    assert response.json()["error"]["type"] == "BadRequestError"
    assert response.json()["error"]["message"].startswith(
        f"Task {task} is not supported"
    )


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
@pytest.mark.parametrize("task", ["embed", "token_embed", "plugin"])
async def test_pooling_not_supported(
    server: RemoteOpenAIServer, model_name: str, task: str
):
    response = requests.post(
        server.url_for("pooling"),
        json={
            "model": model_name,
            "input": "test",
            "encoding_format": "float",
            "task": task,
        },
    )
    assert response.json()["error"]["type"] == "BadRequestError"
    assert response.json()["error"]["message"].startswith(
        f"Task {task} is not supported"
    )