| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import pytest |
| 4 | import torch |
| 5 | from transformers import AutoModelForSequenceClassification |
| 6 | |
| 7 | from vllm.platforms import current_platform |
| 8 | |
| 9 | |
| 10 | @pytest.mark.parametrize( |
| 11 | "model", |
| 12 | [ |
| 13 | pytest.param( |
| 14 | "jason9693/Qwen2.5-1.5B-apeach", |
| 15 | marks=[ |
| 16 | pytest.mark.core_model, |
| 17 | pytest.mark.cpu_model, |
| 18 | pytest.mark.slow_test, |
| 19 | ], |
| 20 | ), |
| 21 | ], |
| 22 | ) |
| 23 | @pytest.mark.parametrize("dtype", ["half"] if current_platform.is_rocm() else ["float"]) |
| 24 | def test_models( |
| 25 | hf_runner, |
| 26 | vllm_runner, |
| 27 | example_prompts, |
| 28 | model: str, |
| 29 | dtype: str, |
| 30 | ) -> None: |
| 31 | with vllm_runner(model, max_model_len=512, dtype=dtype) as vllm_model: |
| 32 | vllm_outputs = vllm_model.classify(example_prompts) |
| 33 | |
| 34 | with hf_runner( |
| 35 | model, dtype=dtype, auto_cls=AutoModelForSequenceClassification |
| 36 | ) as hf_model: |
| 37 | hf_outputs = hf_model.classify(example_prompts) |
| 38 | |
| 39 | # check logits difference |
| 40 | for hf_output, vllm_output in zip(hf_outputs, vllm_outputs): |
| 41 | hf_output = torch.tensor(hf_output) |
| 42 | vllm_output = torch.tensor(vllm_output) |
| 43 | |
| 44 | # the tolerance value of 1e-2 is selected based on the |
| 45 | # half datatype tests in |
| 46 | # tests/models/language/pooling/test_embedding.py |
| 47 | assert torch.allclose( |
| 48 | hf_output, |
| 49 | vllm_output, |
| 50 | rtol=2e-3 if dtype == "float" else 1e-2, |
| 51 | ) |
| 52 | |
| 53 | |
| 54 | @pytest.mark.core_model |
| 55 | def test_bert_model_runner_v2(hf_runner, vllm_runner, monkeypatch) -> None: |
| 56 | model = "cross-encoder/ms-marco-TinyBERT-L-2-v2" |
| 57 | score_inputs = ( |
| 58 | "What is the capital of France?", |
| 59 | [ |
| 60 | "Paris.", |
| 61 | "Paris is the capital and largest city of France.", |
| 62 | "William Shakespeare wrote Hamlet in the early seventeenth century.", |
| 63 | ], |
| 64 | ) |
| 65 | prompt_batches = [ |
| 66 | ["short input"], |
| 67 | [ |
| 68 | "short input", |
| 69 | "a longer input that exercises mixed sequence lengths", |
| 70 | ], |
| 71 | ] |
| 72 | |
| 73 | with hf_runner( |
| 74 | model, dtype="half", auto_cls=AutoModelForSequenceClassification |
| 75 | ) as hf_model: |
| 76 | # HfRunner uses problem_type to preserve the model's |
| 77 | # sbert_ce_default_activation_function=Identity raw logits. |
| 78 | hf_model.config.problem_type = "regression" |
| 79 | hf_outputs = [hf_model.classify(prompts) for prompts in prompt_batches] |
| 80 | |
| 81 | text_1, text_2 = score_inputs |
| 82 | text_pairs = [[text_1, document] for document in text_2] |
| 83 | with hf_runner(model, dtype="half", is_cross_encoder=True) as hf_model: |
| 84 | hf_scores = hf_model.predict(text_pairs).tolist() |
| 85 | |
| 86 | monkeypatch.setenv("VLLM_USE_V2_MODEL_RUNNER", "1") |
| 87 | with vllm_runner( |
| 88 | model, |
| 89 | runner="pooling", |
| 90 | dtype="half", |
| 91 | max_model_len=64, |
| 92 | ) as vllm_model: |
| 93 | assert vllm_model.llm.llm_engine.vllm_config.use_v2_model_runner |
| 94 | vllm_outputs = [vllm_model.classify(prompts) for prompts in prompt_batches] |
| 95 | vllm_scores = vllm_model.score(*score_inputs) |
| 96 | |
| 97 | for hf_batch, vllm_batch in zip(hf_outputs, vllm_outputs): |
| 98 | hf_tensor = torch.tensor(hf_batch) |
| 99 | vllm_tensor = torch.tensor(vllm_batch) |
| 100 | assert vllm_tensor.shape == hf_tensor.shape |
| 101 | assert torch.allclose(vllm_tensor, hf_tensor, rtol=1e-2, atol=1e-4) |
| 102 | |
| 103 | assert torch.allclose( |
| 104 | torch.tensor(vllm_scores), |
| 105 | torch.tensor(hf_scores), |
| 106 | rtol=1e-2, |
| 107 | atol=1e-4, |
| 108 | ) |