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

import numpy as np
5
6
7
import pytest
import torch

8
from vllm.config import ModelConfig, ParallelConfig, VllmConfig
9
from vllm.multimodal import MULTIMODAL_REGISTRY
10
11
12
13
14
15
16
from vllm.multimodal.cache import (
    MultiModalCache,
    MultiModalProcessorCacheItem,
    MultiModalProcessorCacheItemMetadata,
    engine_receiver_cache_from_config,
    processor_cache_from_config,
)
17
from vllm.multimodal.hasher import MultiModalHasher
18
19
20
21
22
23
from vllm.multimodal.inputs import (
    MultiModalFieldElem,
    MultiModalKwargsItem,
    MultiModalKwargsItems,
    MultiModalSharedField,
)
24
25
from vllm.multimodal.processing import PromptInsertion

26
27
pytestmark = pytest.mark.cpu_test

28

29
30
31
32
33
def _dummy_elem(
    modality: str,
    key: str,
    size: int,
    *,
34
    rng: np.random.RandomState | None = None,
35
36
):
    if rng is None:
37
        data = torch.empty((size,), dtype=torch.int8)
38
    else:
39
        data = torch.from_numpy(rng.randint(4, size=(size,), dtype=np.int8))
40
41
42
43

    return MultiModalFieldElem(
        modality=modality,
        key=key,
44
        data=data,
45
46
47
48
        field=MultiModalSharedField(1),
    )


49
50
51
52
def _dummy_item(
    modality: str,
    size_by_key: dict[str, int],
    *,
53
    rng: np.random.RandomState | None = None,
54
):
55
56
57
    return MultiModalKwargsItem.from_elems(
        [_dummy_elem(modality, key, size, rng=rng) for key, size in size_by_key.items()]
    )
58
59


60
61
62
def _dummy_items(
    size_by_key_modality: dict[str, dict[str, int]],
    *,
63
    rng: np.random.RandomState | None = None,
64
):
65
66
67
68
69
70
    return MultiModalKwargsItems.from_seq(
        [
            _dummy_item(modality, size_by_key, rng=rng)
            for modality, size_by_key in size_by_key_modality.items()
        ]
    )
71
72
73
74
75
76
77


@pytest.mark.parametrize(
    ("item", "expected_size"),
    [
        (_dummy_item("a", {"a1": 100}), 100),
        (_dummy_item("a", {"a1": 100, "a2": 110}), 210),
78
        (_dummy_items({"a": {"a1": 100, "a2": 110}, "b": {"b1": 120, "b2": 130}}), 460),  # noqa: E501
79
80
81
82
83
84
        (
            _dummy_items(
                {"a": {"a1": 100, "a2": 110}, "b": {"b1": 120, "b2": 130}}
            ).get_data(),
            460,
        ),  # noqa: E501
85
86
87
88
89
90
91
92
    ],
)
def test_cache_item_size(item, expected_size):
    cache = MultiModalCache.get_lru_cache(2048, type(item))

    cache[""] = item
    assert cache.currsize == expected_size

93
    prompt_update = PromptInsertion("dummy", "target", "insertion").resolve(0)
94
95
96
97
98

    cache[""] = MultiModalProcessorCacheItem(item, [prompt_update])
    assert cache.currsize == expected_size

    cache[""] = MultiModalProcessorCacheItemMetadata(item, [prompt_update])
99
    assert cache.currsize == expected_size
100
101
102
103
104
105
106
107


def _create_vllm_config(
    *,
    mm_processor_cache_gb: float,
    enable_ipc: bool,
):
    return VllmConfig(
108
109
        model_config=ModelConfig(
            model="llava-hf/llava-onevision-qwen2-0.5b-ov-hf",
110
111
112
            mm_processor_cache_gb=mm_processor_cache_gb,
        ),
        parallel_config=ParallelConfig(data_parallel_size=1 if enable_ipc else 2),
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    )


def _compare_caches(
    config_0: VllmConfig,
    config_1: VllmConfig,
    *,
    item_capacity: int = 8,
    hit_rate: float = 0.5,
    max_items_per_iter: int = 3,
    is_cached_calls_per_iter: int,
    n_iter: int = 100,
    seed: int = 0,
):
127
    cache_0_p0 = processor_cache_from_config(config_0, MULTIMODAL_REGISTRY)
128
    cache_0_p1 = engine_receiver_cache_from_config(config_0, MULTIMODAL_REGISTRY)
129
    cache_1_p0 = processor_cache_from_config(config_1, MULTIMODAL_REGISTRY)
130
    cache_1_p1 = engine_receiver_cache_from_config(config_1, MULTIMODAL_REGISTRY)
131
132

    cache_size_gb = max(
133
134
        config_0.model_config.multimodal_config.mm_processor_cache_gb,
        config_1.model_config.multimodal_config.mm_processor_cache_gb,
135
136
137
138
139
140
141
142
143
    )
    item_size_gb = int(cache_size_gb / item_capacity)

    rng = np.random.RandomState(seed)
    all_items = [
        _dummy_item("item", {"key": item_size_gb}, rng=rng)
        for _ in range(int(item_capacity / hit_rate))
    ]
    all_hashes = [
144
        MultiModalHasher.hash_kwargs(item=item.get_data()) for item in all_items
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    ]

    # Should not be used since there is nothing to convert to text
    prompt_update = PromptInsertion("dummy", "target", "insertion")

    for it in range(n_iter):
        num_items_to_select = rng.randint(0, max_items_per_iter)
        item_idxs_to_select = rng.choice(len(all_items), num_items_to_select)

        selected_items = [all_items[idx] for idx in item_idxs_to_select]
        selected_hashes = [all_hashes[idx] for idx in item_idxs_to_select]

        if cache_0_p0 is None:
            cache_0_p0_out = selected_items
        else:
            for _ in range(is_cached_calls_per_iter):
                cache_0_p0.is_cached(selected_hashes)
            cache_0_p0_out = [
163
164
                item
                for item, _ in cache_0_p0.get_and_update(
165
166
167
168
169
170
171
172
173
174
175
                    [(item, prompt_update.content) for item in selected_items],
                    selected_hashes,
                )
            ]

        if cache_1_p0 is None:
            cache_1_p0_out = selected_items
        else:
            for _ in range(is_cached_calls_per_iter):
                cache_1_p0.is_cached(selected_hashes)
            cache_1_p0_out = [
176
177
                item
                for item, _ in cache_1_p0.get_and_update(
178
179
180
181
182
183
184
185
                    [(item, prompt_update.content) for item in selected_items],
                    selected_hashes,
                )
            ]

        if cache_0_p1 is None:
            cache_0_p1_out = cache_0_p0_out
        else:
186
            cache_0_p1_out = cache_0_p1.get_and_update(cache_0_p0_out, selected_hashes)
187
188
189
190

        if cache_1_p1 is None:
            cache_1_p1_out = cache_1_p0_out
        else:
191
            cache_1_p1_out = cache_1_p1.get_and_update(cache_1_p0_out, selected_hashes)
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

        assert cache_0_p1_out == cache_1_p1_out, f"Failed at {it=}"


@pytest.mark.parametrize("is_cached_calls_per_iter", [1, 2, 3])
def test_ipc_enable_disable_consistency(is_cached_calls_per_iter):
    cache_size_gb = 1 / (1 << 20)

    vllm_config_ipc_enabled = _create_vllm_config(
        mm_processor_cache_gb=cache_size_gb,
        enable_ipc=True,
    )
    vllm_config_ipc_disabled = _create_vllm_config(
        mm_processor_cache_gb=0,
        enable_ipc=False,
    )
    vllm_config_cache_disabled = _create_vllm_config(
        mm_processor_cache_gb=cache_size_gb,
        enable_ipc=True,
    )

    _compare_caches(
        vllm_config_ipc_enabled,
        vllm_config_ipc_disabled,
        is_cached_calls_per_iter=is_cached_calls_per_iter,
    )
    _compare_caches(
        vllm_config_ipc_disabled,
        vllm_config_cache_disabled,
        is_cached_calls_per_iter=is_cached_calls_per_iter,
    )
    _compare_caches(
        vllm_config_cache_disabled,
        vllm_config_ipc_enabled,
        is_cached_calls_per_iter=is_cached_calls_per_iter,
    )