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

4
from functools import partial
5
6
7
8
9
from unittest.mock import patch

import pytest

from vllm import LLM
10
from vllm.config import ModelImpl
11
from vllm.engine.llm_engine import LLMEngine as V0LLMEngine
12
13
from vllm.utils import GiB_bytes
from vllm.v1.core.kv_cache_utils import get_kv_cache_config
14
from vllm.v1.engine.core import EngineCore as V1EngineCore
15

16
from ..utils import create_new_process_for_each_test
17
18
from .registry import (_TRANSFORMERS_BACKEND_MODELS, AUTO_EXAMPLE_MODELS,
                       HF_EXAMPLE_MODELS, HfExampleModels)
19
from .utils import dummy_hf_overrides
20
21


22
@create_new_process_for_each_test()
23
24
25
26
27
def can_initialize(model_arch: str, monkeypatch: pytest.MonkeyPatch,
                   EXAMPLE_MODELS: HfExampleModels):
    """The reason for using create_new_process_for_each_test is to avoid
    the WARNING:
        "We must use the 'spawn' multiprocessing start method. Overriding
28
        VLLM_WORKER_MULTIPROC_METHOD to 'spawn'."
29
    The spawn process causes the _initialize_kv_caches_v1 function below to
30
31
    become ineffective.
    """
32
33

    model_info = EXAMPLE_MODELS.get_hf_info(model_arch)
34
35
    model_info.check_available_online(on_fail="skip")
    model_info.check_transformers_version(on_fail="skip")
36

37
38
    hf_overrides_fn = partial(dummy_hf_overrides,
                              model_arch=model_arch,
39
40
41
42
                              exist_overrides=model_info.hf_overrides,
                              use_original_num_layers=getattr(
                                  model_info, 'use_original_num_layers',
                                  False))
43

44
    # Avoid calling model.forward()
45
    def _initialize_kv_caches_v0(self) -> None:
46
47
48
        self.cache_config.num_gpu_blocks = 0
        self.cache_config.num_cpu_blocks = 0

49
50
51
52
53
    def _initialize_kv_caches_v1(self, vllm_config):
        kv_cache_specs = self.model_executor.get_kv_cache_specs()
        scheduler_kv_cache_config = get_kv_cache_config(
            vllm_config,
            kv_cache_specs[0],
54
            10 * GiB_bytes,
55
56
57
58
        )

        # gpu_blocks (> 0), cpu_blocks, scheduler_kv_cache_config
        return 1, 0, scheduler_kv_cache_config
59
60
61
62

    with (patch.object(V0LLMEngine, "_initialize_kv_caches",
                       _initialize_kv_caches_v0),
          patch.object(V1EngineCore, "_initialize_kv_caches",
63
64
65
                       _initialize_kv_caches_v1), monkeypatch.context() as m):
        if model_info.v0_only:
            m.setenv("VLLM_USE_V1", "0")
66
67
68
        if model_arch == "Phi4FlashForCausalLM":
            # Phi4FlashForCausalLM only supports DIFFERENTIAL_FLASH_ATTN backend
            m.setenv("VLLM_ATTENTION_BACKEND", "DIFFERENTIAL_FLASH_ATTN")
69
70
71
72
73
        if model_arch == "GptOssForCausalLM":
            # FIXME: A hack to bypass FA3 assertion because our CI's L4 GPU
            # has cc==8.9 which hasn't supported FA3 yet. Remove this hack when
            # L4 supports FA3.
            m.setenv("VLLM_ATTENTION_BACKEND", "TRITON_ATTN_VLLM_V1")
74
75
76
77
78
79
        if model_arch == "Florence2ForConditionalGeneration":
            # An encoder-decoder model that's V0-only. Just skip it
            # since V0 is about to be removed.
            pytest.skip("Skipping Florence2ForConditionalGeneration")
        if model_arch == "WhisperForConditionalGeneration":
            m.setenv("VLLM_WORKER_MULTIPROC_METHOD", "spawn")
80
        LLM(
81
            model_info.default,
82
83
            tokenizer=model_info.tokenizer,
            tokenizer_mode=model_info.tokenizer_mode,
84
            revision=model_info.revision,
85
86
87
            enforce_eager=model_info.enforce_eager,
            skip_tokenizer_init=model_info.skip_tokenizer_init,
            dtype=model_info.dtype,
88
89
90
91
            speculative_config={
                "model": model_info.speculative_model,
                "num_speculative_tokens": 1,
            } if model_info.speculative_model else None,
92
            trust_remote_code=model_info.trust_remote_code,
93
            max_model_len=model_info.max_model_len,
94
95
            # these tests seem to produce leftover memory
            gpu_memory_utilization=0.80,
96
            load_format="dummy",
97
98
            model_impl=ModelImpl.TRANSFORMERS
            if model_arch in _TRANSFORMERS_BACKEND_MODELS else ModelImpl.VLLM,
99
            hf_overrides=hf_overrides_fn,
100
            max_num_seqs=model_info.max_num_seqs)
101
102
103
104


@pytest.mark.parametrize("model_arch", HF_EXAMPLE_MODELS.get_supported_archs())
def test_can_initialize(model_arch: str, monkeypatch: pytest.MonkeyPatch):
105
106
    if model_arch == "Lfm2ForCausalLM":
        pytest.skip("Skipping until test supports V1-only models")
107
108
109
110
111
112
113
114
    can_initialize(model_arch, monkeypatch, HF_EXAMPLE_MODELS)


@pytest.mark.parametrize("model_arch",
                         AUTO_EXAMPLE_MODELS.get_supported_archs())
def test_implicit_converted_models(model_arch: str,
                                   monkeypatch: pytest.MonkeyPatch):
    can_initialize(model_arch, monkeypatch, AUTO_EXAMPLE_MODELS)