test_registry.py 3.94 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
import warnings

6
import pytest
7
import torch.cuda
8

9
from vllm.model_executor.models import (is_pooling_model,
10
11
                                        is_text_generation_model,
                                        supports_multimodal)
12
13
14
from vllm.model_executor.models.adapters import (as_classification_model,
                                                 as_embedding_model,
                                                 as_reward_model)
15
from vllm.model_executor.models.registry import (_MULTIMODAL_MODELS,
16
17
18
                                                 _SPECULATIVE_DECODING_MODELS,
                                                 _TEXT_GENERATION_MODELS,
                                                 ModelRegistry)
19
20
from vllm.platforms import current_platform

21
from ..utils import create_new_process_for_each_test
22
from .registry import HF_EXAMPLE_MODELS
23
24


25
@pytest.mark.parametrize("model_arch", ModelRegistry.get_supported_archs())
26
def test_registry_imports(model_arch):
27
28
29
    model_info = HF_EXAMPLE_MODELS.get_hf_info(model_arch)
    model_info.check_transformers_version(on_fail="skip")

30
    # Ensure all model classes can be imported successfully
31
32
33
    model_cls, _ = ModelRegistry.resolve_model_cls(model_arch)

    if model_arch in _SPECULATIVE_DECODING_MODELS:
34
35
36
37
38
39
        return  # Ignore these models which do not have a unified format

    if (model_arch in _TEXT_GENERATION_MODELS
            or model_arch in _MULTIMODAL_MODELS):
        assert is_text_generation_model(model_cls)

40
41
42
43
    # All vLLM models should be convertible to a pooling model
    assert is_pooling_model(as_classification_model(model_cls))
    assert is_pooling_model(as_embedding_model(model_cls))
    assert is_pooling_model(as_reward_model(model_cls))
44
45
46

    if model_arch in _MULTIMODAL_MODELS:
        assert supports_multimodal(model_cls)
47
48


49
@create_new_process_for_each_test()
50
51
52
53
54
55
56
@pytest.mark.parametrize("model_arch,is_mm,init_cuda,is_ce", [
    ("LlamaForCausalLM", False, False, False),
    ("MllamaForConditionalGeneration", True, False, False),
    ("LlavaForConditionalGeneration", True, True, False),
    ("BertForSequenceClassification", False, False, True),
    ("RobertaForSequenceClassification", False, False, True),
    ("XLMRobertaForSequenceClassification", False, False, True),
57
])
58
def test_registry_model_property(model_arch, is_mm, init_cuda, is_ce):
59
60
    assert ModelRegistry.is_multimodal_model(model_arch) is is_mm

61
62
    assert ModelRegistry.is_cross_encoder_model(model_arch) is is_ce

63
64
65
66
67
68
69
70
71
72
73
    if init_cuda and current_platform.is_cuda_alike():
        assert not torch.cuda.is_initialized()

        ModelRegistry.resolve_model_cls(model_arch)
        if not torch.cuda.is_initialized():
            warnings.warn(
                "This model no longer initializes CUDA on import. "
                "Please test using a different one.",
                stacklevel=2)


74
@create_new_process_for_each_test()
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@pytest.mark.parametrize("model_arch,is_pp,init_cuda", [
    ("MLPSpeculatorPreTrainedModel", False, False),
    ("DeepseekV2ForCausalLM", True, False),
    ("Qwen2VLForConditionalGeneration", True, True),
])
def test_registry_is_pp(model_arch, is_pp, init_cuda):
    assert ModelRegistry.is_pp_supported_model(model_arch) is is_pp

    if init_cuda and current_platform.is_cuda_alike():
        assert not torch.cuda.is_initialized()

        ModelRegistry.resolve_model_cls(model_arch)
        if not torch.cuda.is_initialized():
            warnings.warn(
                "This model no longer initializes CUDA on import. "
                "Please test using a different one.",
                stacklevel=2)
92
93
94


def test_hf_registry_coverage():
95
96
    untested_archs = (ModelRegistry.get_supported_archs() -
                      HF_EXAMPLE_MODELS.get_supported_archs())
97
98
99
100

    assert not untested_archs, (
        "Please add the following architectures to "
        f"`tests/models/registry.py`: {untested_archs}")