mm_input_cache.py 3 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
3
from collections.abc import Sequence
from typing import Optional
4

5
from vllm.envs import VLLM_MM_INPUT_CACHE_GIB
6
from vllm.multimodal import MultiModalKwargs
7
from vllm.multimodal.processing import ProcessingCache
8
from vllm.utils import is_list_of
9

10
11
12
# The idea of multimodal preprocessing caching is based on having a client and
# a server, where the client executes in the frontend process (=P0) and the
# server in the core process (=P1).
13
#
14
# -- Client:
15
16
#  - BaseMultiModalProcessor to process MultiModalData into MultiModalKwargs
#    with built-in caching functionality, with mm_hash as its identifier.
17
18
#  - MirroredProcessingCache to keep track of the cached entries and
#    determine whether to send the MultiModalKwargs to P1.
19
20
#
# -- Server:
21
#  - MirroredProcessingCache to store the MultiModalKwargs from P0.
22
#
23
# The caching for both client and server is mirrored, and this allows us
24
# to avoid the serialization of "mm_inputs" (like pixel values) between
25
26
# client (=P0) and server (=P1) processes if the mm_hash is found in the client
# cache.
27

28
# Both Client and Server must use the same cache size
29
# (to perform mirrored caching). This cache size is set by the environment
30
# variable VLLM_MM_INPUT_CACHE_GIB.
31

32

33
class MirroredProcessingCache:
34

35
    def __init__(self, model_config):
36
37
38
39
        mm_config = model_config.multimodal_config
        disable_mm_preprocessor_cache = mm_config is not None and \
            not mm_config.disable_mm_preprocessor_cache
        self.use_cache = not disable_mm_preprocessor_cache
40
41
        self.mm_cache = ProcessingCache.get_lru_cache(VLLM_MM_INPUT_CACHE_GIB,
                                                      MultiModalKwargs)
42

43
    def get_and_update_p0(
44
        self,
45
        mm_inputs: Sequence[MultiModalKwargs],
46
        mm_hashes: list[str],
47
    ) -> Sequence[Optional[MultiModalKwargs]]:
48
49
        assert len(mm_inputs) == len(mm_hashes)

50
        if not self.use_cache:
51
            assert is_list_of(mm_inputs, MultiModalKwargs)
52
53
            return mm_inputs

54
55
        full_mm_inputs = list[Optional[MultiModalKwargs]]()
        for mm_input, mm_hash in zip(mm_inputs, mm_hashes):
56
            if self.mm_cache.get(mm_hash) is not None:
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                mm_input = None
            else:
                self.mm_cache[mm_hash] = mm_input

            full_mm_inputs.append(mm_input)

        return full_mm_inputs

    def get_and_update_p1(
        self,
        mm_inputs: Sequence[Optional[MultiModalKwargs]],
        mm_hashes: list[str],
    ) -> Sequence[MultiModalKwargs]:
        assert len(mm_inputs) == len(mm_hashes)

        if not self.use_cache:
            assert is_list_of(mm_inputs, MultiModalKwargs)
            return mm_inputs

        full_mm_inputs = list[MultiModalKwargs]()
77
78
        for mm_input, mm_hash in zip(mm_inputs, mm_hashes):
            if mm_input is None:
79
                mm_input = self.mm_cache[mm_hash]
80
            else:
81
                self.mm_cache[mm_hash] = mm_input
82
83
84
85

            full_mm_inputs.append(mm_input)

        return full_mm_inputs