| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import weakref |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from vllm import LLM, PoolingRequestOutput |
| 8 | from vllm.config import PoolerConfig |
| 9 | from vllm.tasks import PoolingTask |
| 10 | |
| 11 | MODEL_NAME = "jason9693/Qwen2.5-1.5B-apeach" |
| 12 | |
| 13 | prompt = "The chef prepared a delicious meal." |
| 14 | prompt_token_ids = [785, 29706, 10030, 264, 17923, 15145, 13] |
| 15 | num_labels = 2 |
| 16 | |
| 17 | |
| 18 | @pytest.fixture(scope="module") |
| 19 | def llm(vllm_runner): |
| 20 | with vllm_runner( |
| 21 | MODEL_NAME, |
| 22 | max_model_len=None, |
| 23 | pooler_config=PoolerConfig(task="token_classify"), |
| 24 | max_num_batched_tokens=32768, |
| 25 | tensor_parallel_size=1, |
| 26 | gpu_memory_utilization=0.75, |
| 27 | enforce_eager=True, |
| 28 | seed=0, |
| 29 | enable_chunked_prefill=None, |
| 30 | ) as runner: |
| 31 | # pytest caches yielded fixtures until after teardown, so use a proxy to |
| 32 | # avoid retaining the LLM while VllmRunner.__exit__ releases ROCm memory. |
| 33 | yield weakref.proxy(runner.llm) |
| 34 | |
| 35 | |
| 36 | @pytest.mark.skip_global_cleanup |
| 37 | def test_str_prompts(llm: LLM): |
| 38 | outputs = llm.encode(prompt, pooling_task="token_classify", use_tqdm=False) |
| 39 | assert len(outputs) == 1 |
| 40 | assert isinstance(outputs[0], PoolingRequestOutput) |
| 41 | assert outputs[0].prompt_token_ids == prompt_token_ids |
| 42 | assert outputs[0].outputs.data.shape == (len(prompt_token_ids), num_labels) |
| 43 | |
| 44 | |
| 45 | @pytest.mark.skip_global_cleanup |
| 46 | def test_token_ids_prompts(llm: LLM): |
| 47 | outputs = llm.encode( |
| 48 | [prompt_token_ids], pooling_task="token_classify", use_tqdm=False |
| 49 | ) |
| 50 | assert len(outputs) == 1 |
| 51 | assert isinstance(outputs[0], PoolingRequestOutput) |
| 52 | assert outputs[0].prompt_token_ids == prompt_token_ids |
| 53 | assert outputs[0].outputs.data.shape == (len(prompt_token_ids), num_labels) |
| 54 | |
| 55 | |
| 56 | @pytest.mark.skip_global_cleanup |
| 57 | def test_score_api(llm: LLM): |
| 58 | err_msg = "This model does not support the Scoring API." |
| 59 | with pytest.raises(ValueError, match=err_msg): |
| 60 | llm.score("ping", "pong", use_tqdm=False) |
| 61 | |
| 62 | |
| 63 | @pytest.mark.parametrize("task", ["classify", "embed", "token_embed", "plugin"]) |
| 64 | def test_unsupported_tasks(llm: LLM, task: PoolingTask, caplog_vllm): |
| 65 | if task == "plugin": |
| 66 | err_msg = "No IOProcessor plugin installed." |
| 67 | elif task == "classify": |
| 68 | err_msg = "Try switching the model's pooling_task via.+" |
| 69 | else: |
| 70 | err_msg = "Embedding API is not supported by this model.+" |
| 71 | |
| 72 | with pytest.raises(ValueError, match=err_msg): |
| 73 | llm.encode(prompt, pooling_task=task, use_tqdm=False) |