| 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 | import torch |
| 8 | |
| 9 | from tests.models.utils import softmax |
| 10 | from vllm import LLM, PoolingParams |
| 11 | |
| 12 | MODEL_NAME = "internlm/internlm2-1_8b-reward" |
| 13 | |
| 14 | prompts = ["The chef prepared a delicious meal."] |
| 15 | |
| 16 | |
| 17 | @pytest.fixture(scope="module") |
| 18 | def llm(vllm_runner): |
| 19 | with vllm_runner( |
| 20 | MODEL_NAME, |
| 21 | max_model_len=None, |
| 22 | max_num_batched_tokens=32768, |
| 23 | tensor_parallel_size=1, |
| 24 | gpu_memory_utilization=0.75, |
| 25 | enforce_eager=True, |
| 26 | trust_remote_code=True, |
| 27 | seed=0, |
| 28 | enable_chunked_prefill=None, |
| 29 | ) as runner: |
| 30 | # pytest caches yielded fixtures until after teardown, so use a proxy to |
| 31 | # avoid retaining the LLM while VllmRunner.__exit__ releases ROCm memory. |
| 32 | yield weakref.proxy(runner.llm) |
| 33 | |
| 34 | |
| 35 | @pytest.mark.skip_global_cleanup |
| 36 | def test_config(llm: LLM): |
| 37 | vllm_config = llm.llm_engine.vllm_config |
| 38 | assert vllm_config.cache_config.enable_prefix_caching |
| 39 | assert vllm_config.scheduler_config.enable_chunked_prefill |
| 40 | |
| 41 | |
| 42 | def test_pooling_params(llm: LLM): |
| 43 | def get_outputs(use_activation): |
| 44 | outputs = llm.encode( |
| 45 | prompts, |
| 46 | pooling_params=PoolingParams(use_activation=use_activation), |
| 47 | pooling_task="token_classify", |
| 48 | use_tqdm=False, |
| 49 | ) |
| 50 | return torch.cat([x.outputs.data for x in outputs]) |
| 51 | |
| 52 | default = get_outputs(use_activation=None) |
| 53 | w_activation = get_outputs(use_activation=True) |
| 54 | wo_activation = get_outputs(use_activation=False) |
| 55 | |
| 56 | assert torch.allclose(default, w_activation, atol=1e-2), ( |
| 57 | "Default should use activation." |
| 58 | ) |
| 59 | assert not torch.allclose(w_activation, wo_activation, atol=1e-2), ( |
| 60 | "wo_activation should not use activation." |
| 61 | ) |
| 62 | assert torch.allclose(softmax(wo_activation), w_activation, atol=1e-2), ( |
| 63 | "w_activation should be close to activation(wo_activation)." |
| 64 | ) |