2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-12-25 02:22:22 +08:00
|
|
|
"""Compare the classification outputs of HF and vLLM models.
|
2024-10-27 02:53:35 +09:00
|
|
|
|
|
|
|
Run `pytest tests/models/test_cls_models.py`.
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
from transformers import AutoModelForSequenceClassification
|
|
|
|
|
|
|
|
|
2024-11-15 12:23:09 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"model",
|
|
|
|
[
|
|
|
|
pytest.param("jason9693/Qwen2.5-1.5B-apeach",
|
|
|
|
marks=[pytest.mark.core_model, pytest.mark.cpu_model]),
|
|
|
|
],
|
|
|
|
)
|
2024-10-27 02:53:35 +09:00
|
|
|
@pytest.mark.parametrize("dtype", ["float"])
|
|
|
|
def test_classification_models(
|
|
|
|
hf_runner,
|
|
|
|
vllm_runner,
|
|
|
|
example_prompts,
|
|
|
|
model: str,
|
|
|
|
dtype: str,
|
|
|
|
) -> None:
|
2024-11-14 10:54:59 +08:00
|
|
|
with vllm_runner(model, dtype=dtype) as vllm_model:
|
|
|
|
vllm_outputs = vllm_model.classify(example_prompts)
|
2025-01-20 15:00:59 +08:00
|
|
|
|
2024-11-15 12:23:09 +08:00
|
|
|
# This test is for verifying whether the model's extra_repr
|
|
|
|
# can be printed correctly.
|
2025-01-20 15:00:59 +08:00
|
|
|
def print_model(model):
|
|
|
|
print(model)
|
|
|
|
|
|
|
|
vllm_model.apply_model(print_model)
|
2024-11-14 10:54:59 +08:00
|
|
|
|
2024-10-27 02:53:35 +09:00
|
|
|
with hf_runner(model,
|
|
|
|
dtype=dtype,
|
|
|
|
auto_cls=AutoModelForSequenceClassification) as hf_model:
|
|
|
|
hf_outputs = hf_model.classify(example_prompts)
|
|
|
|
|
|
|
|
# check logits difference
|
|
|
|
for hf_output, vllm_output in zip(hf_outputs, vllm_outputs):
|
|
|
|
hf_output = torch.tensor(hf_output)
|
|
|
|
vllm_output = torch.tensor(vllm_output)
|
|
|
|
|
|
|
|
assert torch.allclose(hf_output, vllm_output, 1e-3)
|