| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import weakref |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from vllm import LLM, PoolingParams |
| 9 | from vllm.exceptions import VLLMValidationError |
| 10 | |
| 11 | MODEL_NAME = "intfloat/multilingual-e5-small" |
| 12 | |
| 13 | PROMPTS = [ |
| 14 | "Hello, my name is", |
| 15 | "The president of the United States is", |
| 16 | "The capital of France is", |
| 17 | "The future of AI is", |
| 18 | ] |
| 19 | |
| 20 | TOKEN_IDS = [ |
| 21 | # Using ID={0, 1, 2, 3} results in NaN values, |
| 22 | # so we add this offset of 1000 |
| 23 | [1000], |
| 24 | [1000, 1001], |
| 25 | [1000, 1002, 1001], |
| 26 | [1000, 1003, 1001, 1002], |
| 27 | ] |
| 28 | |
| 29 | |
| 30 | @pytest.fixture(scope="module") |
| 31 | def llm(vllm_runner): |
| 32 | with vllm_runner( |
| 33 | MODEL_NAME, |
| 34 | max_model_len=None, |
| 35 | max_num_batched_tokens=32768, |
| 36 | tensor_parallel_size=1, |
| 37 | gpu_memory_utilization=0.75, |
| 38 | enforce_eager=True, |
| 39 | seed=0, |
| 40 | enable_chunked_prefill=None, |
| 41 | ) as runner: |
| 42 | # pytest caches yielded fixtures until after teardown, so use a proxy to |
| 43 | # avoid retaining the LLM while VllmRunner.__exit__ releases ROCm memory. |
| 44 | yield weakref.proxy(runner.llm) |
| 45 | |
| 46 | |
| 47 | @pytest.mark.skip_global_cleanup |
| 48 | def test_multiple_pooling_params(llm: LLM): |
| 49 | pooling_params = [ |
| 50 | PoolingParams(), |
| 51 | PoolingParams(), |
| 52 | PoolingParams(), |
| 53 | PoolingParams(), |
| 54 | ] |
| 55 | |
| 56 | # Multiple PoolingParams should be matched with each prompt |
| 57 | outputs = llm.encode(PROMPTS, pooling_params=pooling_params, pooling_task="embed") |
| 58 | assert len(PROMPTS) == len(outputs) |
| 59 | |
| 60 | # Exception raised, if the size of params does not match the size of prompts |
| 61 | with pytest.raises(VLLMValidationError): |
| 62 | outputs = llm.encode( |
| 63 | PROMPTS, pooling_params=pooling_params[:3], pooling_task="embed" |
| 64 | ) |
| 65 | |
| 66 | # Single PoolingParams should be applied to every prompt |
| 67 | single_pooling_params = PoolingParams() |
| 68 | outputs = llm.encode( |
| 69 | PROMPTS, pooling_params=single_pooling_params, pooling_task="embed" |
| 70 | ) |
| 71 | assert len(PROMPTS) == len(outputs) |
| 72 | |
| 73 | # pooling_params is None, default params should be applied |
| 74 | outputs = llm.encode(PROMPTS, pooling_params=None, pooling_task="embed") |
| 75 | assert len(PROMPTS) == len(outputs) |
| 76 | |
| 77 | |
| 78 | def test_right_side_truncation(llm: LLM): |
| 79 | # Embeddings models should truncate the end of the prompt |
| 80 | tokenizer = llm.get_tokenizer() |
| 81 | assert tokenizer.truncation_side == "right" |