|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# ruff: noqa: SIM117 |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | + |
| 6 | +from tests.models.language.pooling.mteb_utils import mteb_test_embed_models |
| 7 | +from tests.models.utils import Dtype, EmbedModelInfo |
| 8 | +from vllm.config import _STR_DTYPE_TO_TORCH_DTYPE |
| 9 | + |
| 10 | +high_precision_data_types = [ |
| 11 | + Dtype(dtype="auto"), # hybrid |
| 12 | + Dtype(dtype="float32"), |
| 13 | + Dtype(dtype="hybrid"), |
| 14 | + Dtype(dtype="float32", attn_dtype="float16"), |
| 15 | + Dtype(dtype="float32", attn_dtype="bfloat16") |
| 16 | +] |
| 17 | +low_precision_data_types = [Dtype(dtype="float16"), Dtype(dtype="bfloat16")] |
| 18 | +data_types = high_precision_data_types + low_precision_data_types |
| 19 | +embed_model = "intfloat/e5-small" |
| 20 | +generate_model = "EleutherAI/pythia-70m" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("dtype", data_types) |
| 24 | +def test_dtype(vllm_runner, dtype: Dtype): |
| 25 | + with vllm_runner(embed_model, |
| 26 | + dtype=dtype.dtype, |
| 27 | + max_model_len=None, |
| 28 | + attn_dtype=dtype.attn_dtype) as vllm_model: |
| 29 | + model_config = vllm_model.model.llm_engine.model_config |
| 30 | + if dtype.dtype == "hybrid" or dtype.dtype == "auto": |
| 31 | + assert model_config.dtype == torch.float32 |
| 32 | + assert model_config.attn_dtype == torch.float16 |
| 33 | + elif dtype.attn_dtype == "auto": |
| 34 | + assert model_config.dtype == model_config.attn_dtype |
| 35 | + else: |
| 36 | + assert model_config.dtype == _STR_DTYPE_TO_TORCH_DTYPE[dtype.dtype] |
| 37 | + assert model_config.attn_dtype == _STR_DTYPE_TO_TORCH_DTYPE[ |
| 38 | + dtype.attn_dtype] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("dtype", data_types) |
| 42 | +def test_embed_models_mteb(hf_runner, vllm_runner, dtype: Dtype): |
| 43 | + model_info = EmbedModelInfo(embed_model, |
| 44 | + architecture="BertModel", |
| 45 | + dtype=dtype) |
| 46 | + |
| 47 | + if model_info.dtype in high_precision_data_types: |
| 48 | + mteb_test_embed_models(hf_runner, vllm_runner, model_info) |
| 49 | + else: |
| 50 | + with pytest.raises(AssertionError): |
| 51 | + mteb_test_embed_models(hf_runner, vllm_runner, model_info) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("model", [generate_model]) |
| 55 | +@pytest.mark.parametrize("dtype", data_types) |
| 56 | +@pytest.mark.parametrize("max_tokens", [32]) |
| 57 | +@pytest.mark.parametrize("num_logprobs", [4]) |
| 58 | +def test_generate_models(hf_runner, vllm_runner, example_prompts, model: str, |
| 59 | + dtype: Dtype, max_tokens: int, |
| 60 | + num_logprobs: int) -> None: |
| 61 | + if dtype.attn_dtype == "auto" and dtype.dtype != "hybrid": |
| 62 | + with vllm_runner(model, dtype=dtype.dtype, |
| 63 | + attn_dtype=dtype.attn_dtype): |
| 64 | + pass |
| 65 | + else: |
| 66 | + with pytest.raises(ValueError): |
| 67 | + with vllm_runner(model, |
| 68 | + dtype=dtype.dtype, |
| 69 | + attn_dtype=dtype.attn_dtype): |
| 70 | + pass |
0 commit comments