| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | import requests |
| 6 | |
| 7 | from tests.utils import RemoteOpenAIServer |
| 8 | from vllm.entrypoints.pooling.pooling.protocol import PoolingResponse |
| 9 | |
| 10 | MODEL_NAME = "jason9693/Qwen2.5-1.5B-apeach" |
| 11 | DTYPE = "float32" # Use float32 to avoid NaN issue |
| 12 | input_text = "This product was excellent and exceeded my expectations" |
| 13 | input_tokens = [1986, 1985, 572, 9073, 323, 33808, 847, 16665] |
| 14 | |
| 15 | |
| 16 | @pytest.fixture(scope="module") |
| 17 | def server(): |
| 18 | args = [ |
| 19 | "--enforce-eager", |
| 20 | "--max-model-len", |
| 21 | "512", |
| 22 | "--dtype", |
| 23 | DTYPE, |
| 24 | "--pooler-config.task", |
| 25 | "token_classify", |
| 26 | ] |
| 27 | |
| 28 | with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: |
| 29 | yield remote_server |
| 30 | |
| 31 | |
| 32 | @pytest.mark.asyncio |
| 33 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 34 | async def test_pooling_token_classify(server: RemoteOpenAIServer, model_name: str): |
| 35 | task = "token_classify" |
| 36 | response = requests.post( |
| 37 | server.url_for("pooling"), |
| 38 | json={ |
| 39 | "model": model_name, |
| 40 | "input": input_text, |
| 41 | "encoding_format": "float", |
| 42 | "task": task, |
| 43 | }, |
| 44 | ) |
| 45 | poolings = PoolingResponse.model_validate(response.json()) |
| 46 | assert len(poolings.data) == 1 |
| 47 | assert len(poolings.data[0].data) == 8 |
| 48 | assert isinstance(poolings.data[0].data[0], list) |
| 49 | assert len(poolings.data[0].data[0]) == 2 |
| 50 | |
| 51 | |
| 52 | @pytest.mark.asyncio |
| 53 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 54 | @pytest.mark.parametrize("task", ["classify", "embed", "token_embed", "plugin"]) |
| 55 | async def test_pooling_not_supported( |
| 56 | server: RemoteOpenAIServer, model_name: str, task: str |
| 57 | ): |
| 58 | response = requests.post( |
| 59 | server.url_for("pooling"), |
| 60 | json={ |
| 61 | "model": model_name, |
| 62 | "input": input_text, |
| 63 | "encoding_format": "float", |
| 64 | "task": task, |
| 65 | }, |
| 66 | ) |
| 67 | assert response.json()["error"]["type"] == "BadRequestError" |
| 68 | |
| 69 | if task == "plugin": |
| 70 | err_msg = "No IOProcessor plugin installed." |
| 71 | elif task == "classify": |
| 72 | err_msg = "Try switching the model's pooling_task via" |
| 73 | else: |
| 74 | err_msg = f"Unsupported task: {task!r}" |
| 75 | assert response.json()["error"]["message"].startswith(err_msg) |