| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import pytest |
| 4 | import torch |
| 5 | import torch.nn.functional as F |
| 6 | |
| 7 | from tests.models.utils import softmax |
| 8 | from vllm.config import PoolerConfig |
| 9 | |
| 10 | |
| 11 | @pytest.mark.parametrize( |
| 12 | "model", |
| 13 | ["jason9693/Qwen2.5-1.5B-apeach", "papluca/xlm-roberta-base-language-detection"], |
| 14 | ) |
| 15 | @pytest.mark.parametrize("dtype", ["half"]) |
| 16 | def test_classify_models_using_activation( |
| 17 | hf_runner, |
| 18 | vllm_runner, |
| 19 | example_prompts, |
| 20 | model: str, |
| 21 | dtype: str, |
| 22 | ) -> None: |
| 23 | with vllm_runner( |
| 24 | model, |
| 25 | max_model_len=512, |
| 26 | dtype=dtype, |
| 27 | pooler_config=PoolerConfig(use_activation=False), |
| 28 | ) as vllm_model: |
| 29 | wo_activation_out = vllm_model.classify(example_prompts) |
| 30 | |
| 31 | with vllm_runner( |
| 32 | model, |
| 33 | max_model_len=512, |
| 34 | dtype=dtype, |
| 35 | pooler_config=PoolerConfig(use_activation=True), |
| 36 | ) as vllm_model: |
| 37 | w_activation_out = vllm_model.classify(example_prompts) |
| 38 | |
| 39 | for wo_activation, w_activation in zip(wo_activation_out, w_activation_out): |
| 40 | wo_activation = torch.tensor(wo_activation) |
| 41 | w_activation = torch.tensor(w_activation) |
| 42 | |
| 43 | assert not torch.allclose(wo_activation, w_activation, atol=1e-2), ( |
| 44 | "pooler_config is not working" |
| 45 | ) |
| 46 | assert torch.allclose( |
| 47 | softmax(wo_activation), w_activation, 1e-3 if dtype == "float" else 1e-2 |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | @pytest.mark.parametrize( |
| 52 | "model", |
| 53 | [ |
| 54 | "intfloat/multilingual-e5-small", |
| 55 | ], |
| 56 | ) |
| 57 | @pytest.mark.parametrize("dtype", ["half"]) |
| 58 | def test_embed_models_using_normalize( |
| 59 | hf_runner, |
| 60 | vllm_runner, |
| 61 | example_prompts, |
| 62 | model: str, |
| 63 | dtype: str, |
| 64 | ) -> None: |
| 65 | with vllm_runner( |
| 66 | model, |
| 67 | max_model_len=512, |
| 68 | dtype=dtype, |
| 69 | pooler_config=PoolerConfig(use_activation=False), |
| 70 | ) as vllm_model: |
| 71 | wo_normalize = torch.tensor(vllm_model.embed(example_prompts)) |
| 72 | |
| 73 | with vllm_runner( |
| 74 | model, |
| 75 | max_model_len=512, |
| 76 | dtype=dtype, |
| 77 | pooler_config=PoolerConfig(use_activation=True), |
| 78 | ) as vllm_model: |
| 79 | w_normalize = torch.tensor(vllm_model.embed(example_prompts)) |
| 80 | |
| 81 | assert not torch.allclose(wo_normalize, w_normalize, atol=1e-2), ( |
| 82 | "pooler_config normalize is not working" |
| 83 | ) |
| 84 | assert torch.allclose( |
| 85 | F.normalize(wo_normalize, p=2, dim=-1), w_normalize, atol=1e-2 |
| 86 | ), "w_normal should be close to normal(wo_normal)." |
| 87 | |
| 88 | |
| 89 | @pytest.mark.parametrize( |
| 90 | "model", |
| 91 | [ |
| 92 | "internlm/internlm2-1_8b-reward", |
| 93 | ], |
| 94 | ) |
| 95 | @pytest.mark.parametrize("dtype", ["half"]) |
| 96 | def test_reward_models_using_activation( |
| 97 | hf_runner, |
| 98 | vllm_runner, |
| 99 | example_prompts, |
| 100 | model: str, |
| 101 | dtype: str, |
| 102 | ) -> None: |
| 103 | with vllm_runner( |
| 104 | model, |
| 105 | max_model_len=1024, |
| 106 | dtype=dtype, |
| 107 | pooler_config=PoolerConfig(use_activation=False), |
| 108 | ) as vllm_model: |
| 109 | wo_activation = vllm_model.token_classify(example_prompts) |
| 110 | |
| 111 | with vllm_runner( |
| 112 | model, |
| 113 | max_model_len=1024, |
| 114 | dtype=dtype, |
| 115 | pooler_config=PoolerConfig(use_activation=True), |
| 116 | ) as vllm_model: |
| 117 | w_activation = vllm_model.token_classify(example_prompts) |
| 118 | |
| 119 | for wo, w in zip(wo_activation, w_activation): |
| 120 | wo = torch.tensor(wo) |
| 121 | w = torch.tensor(w) |
| 122 | |
| 123 | assert not torch.allclose(wo, w, atol=1e-2), ( |
| 124 | "pooler_config activation is not working" |
| 125 | ) |
| 126 | assert torch.allclose(softmax(wo), w, atol=1e-2), ( |
| 127 | "w_activation should be close to activation(wo_activation)." |
| 128 | ) |
| 129 | |
| 130 | |
| 131 | @pytest.mark.parametrize( |
| 132 | "model", |
| 133 | [ |
| 134 | "intfloat/multilingual-e5-small", |
| 135 | ], |
| 136 | ) |
| 137 | @pytest.mark.parametrize("dtype", ["half"]) |
| 138 | def test_multi_vector_retrieval_models_using_normalize( |
| 139 | hf_runner, |
| 140 | vllm_runner, |
| 141 | example_prompts, |
| 142 | model: str, |
| 143 | dtype: str, |
| 144 | ) -> None: |
| 145 | with vllm_runner( |
| 146 | model, |
| 147 | max_model_len=512, |
| 148 | dtype=dtype, |
| 149 | pooler_config=PoolerConfig(use_activation=False, task="token_embed"), |
| 150 | ) as vllm_model: |
| 151 | wo_normalize = vllm_model.token_embed(example_prompts) |
| 152 | |
| 153 | with vllm_runner( |
| 154 | model, |
| 155 | max_model_len=512, |
| 156 | dtype=dtype, |
| 157 | pooler_config=PoolerConfig(use_activation=True, task="token_embed"), |
| 158 | ) as vllm_model: |
| 159 | w_normalize = vllm_model.token_embed(example_prompts) |
| 160 | |
| 161 | for wo, w in zip(wo_normalize, w_normalize): |
| 162 | assert not torch.allclose(wo, w, atol=1e-2), ( |
| 163 | "pooler_config normalize is not working" |
| 164 | ) |
| 165 | assert torch.allclose(F.normalize(wo, p=2, dim=-1), w, atol=1e-2), ( |
| 166 | "w_normal should be close to normal(wo_normal)." |
| 167 | ) |