|
| 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 vllm import LLM, PoolingParams |
| 10 | +from vllm.distributed import cleanup_dist_env_and_memory |
| 11 | + |
| 12 | +from ...models.utils import softmax |
| 13 | + |
| 14 | +MODEL_NAME = "internlm/internlm2-1_8b-reward" |
| 15 | + |
| 16 | +prompts = ["The chef prepared a delicious meal."] |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(autouse=True) |
| 20 | +def v1(run_with_both_engines): |
| 21 | + # Simple autouse wrapper to run both engines for each test |
| 22 | + # This can be promoted up to conftest.py to run for every |
| 23 | + # test in a package |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="module") |
| 28 | +def llm(): |
| 29 | + # pytest caches the fixture so we use weakref.proxy to |
| 30 | + # enable garbage collection |
| 31 | + llm = LLM(model=MODEL_NAME, |
| 32 | + max_num_batched_tokens=32768, |
| 33 | + tensor_parallel_size=1, |
| 34 | + gpu_memory_utilization=0.75, |
| 35 | + enforce_eager=True, |
| 36 | + trust_remote_code=True, |
| 37 | + seed=0) |
| 38 | + |
| 39 | + with llm.deprecate_legacy_api(): |
| 40 | + yield weakref.proxy(llm) |
| 41 | + |
| 42 | + del llm |
| 43 | + |
| 44 | + cleanup_dist_env_and_memory() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.skip_global_cleanup |
| 48 | +def test_pooling_params(llm: LLM): |
| 49 | + |
| 50 | + def get_outputs(softmax): |
| 51 | + outputs = llm.reward(prompts, |
| 52 | + pooling_params=PoolingParams(softmax=softmax), |
| 53 | + use_tqdm=False) |
| 54 | + return torch.cat([x.outputs.data for x in outputs]) |
| 55 | + |
| 56 | + default = get_outputs(softmax=None) |
| 57 | + w_softmax = get_outputs(softmax=True) |
| 58 | + wo_softmax = get_outputs(softmax=False) |
| 59 | + |
| 60 | + assert torch.allclose(default, w_softmax, |
| 61 | + atol=1e-2), "Default should use softmax." |
| 62 | + assert not torch.allclose(w_softmax, wo_softmax, |
| 63 | + atol=1e-2), "wo_softmax should not use softmax." |
| 64 | + assert torch.allclose( |
| 65 | + softmax(wo_softmax), w_softmax, |
| 66 | + atol=1e-2), "w_softmax should be close to softmax(wo_softmax)." |
0 commit comments