Skip to content

vllm.multimodal.cache.base

Classes:

BaseMultiModalCache

Bases: ABC, Generic[_I, _O]

Abstract base class to read/write multi-modal items from cache.

The idea of multi-modal caching is based on having a client and server where the client executes in the frontend process (=P0) and the server in the core process (=P1). The data flow is as follows:

              is_cached() x N    get_and_update()
P0: From API -----------------> -----------------> To P1

             get_and_update()
P1: From P0 -----------------> To model

is_cached() can be called any number of times in P0. However, get_and_update() must be called in P0 and P1 one after another so that their cache eviction order remains the same.

This ensures that the keys in P0 and P1 caches are mirrored, allowing us to determine whether a key is cached in P1 by looking up the P0 cache, without having to communicate with P1.

Methods:

cache_if_fits(cache, key, value)

Insert value if it fits in cache.

cachetools.Cache raises ValueError("value too large") when a single item exceeds maxsize. An item bigger than the whole processor cache can never be a hit, so skip the insert and serve it uncached instead of aborting engine startup.

LRU P0/P1 caches call this so they stay mirrored. SHM subclasses do not use it; they already skip oversize items in put().

Parameters:

  • cache

    (LRUCache[str, _V]) –

    The LRU cache to update.

  • key

    (str) –

    Cache key (typically the multi-modal item hash).

  • value

    (_V) –

    Value to insert.

Returns:

  • bool –

    True if the item was cached, otherwise False.

clear_cache() abstractmethod

Clear the underlying cache.

get_and_update(mm_items, mm_hashes)

Possibly update a sequence of multi-modal items based on whether they are in the underlying cache.

This update is done out-of-place and updates the cache eviction order.

Parameters:

  • mm_items

    (Sequence[_I]) –

    The multi-modal items to update.

  • mm_hashes

    (list[str]) –

    The hash of each item in mm_items.

Returns:

  • list[_O] –

    A new list of updated multi-modal items.

get_and_update_item(mm_item, mm_hash) abstractmethod

Possibly update a multi-modal item based on whether it is in the underlying cache.

This update is done out-of-place and updates the cache eviction order.

Parameters:

  • mm_item

    (_I) –

    The multi-modal item to update.

  • mm_hash

    (str) –

    The hash of mm_item.

Returns:

  • _O –

    The update multi-modal item.

BaseMultiModalProcessorCache

Bases: BaseMultiModalCache[MultiModalProcessorCacheInItem, MultiModalProcessorCacheOutItem]

The required interface for caches on P0.

Methods:

  • close –

    Close the underlying cache, if needed.

  • get_cached_item –

    Return a locally stored payload without updating eviction order.

  • invalidate –

    Drop mm_hash from this P0 shadow cache to recover from P0/P1 drift.

  • is_cached –

    Check whether a sequence of multi-modal items are

  • is_cached_item –

    Check whether a multi-modal item is

  • make_stats –

    Get (and reset) the multi-modal cache stats.

  • release_sender_touches –

    Release the items that were touched but not updated afterwards.

  • touch_sender_cache_item –

    Update the cache eviction order for a multi-modal item.

  • validate_input_item –

    Validate externally supplied cache metadata before engine handoff.

close()

Close the underlying cache, if needed.

get_cached_item(mm_hash)

Return a locally stored payload without updating eviction order.

Metadata-only and shared-memory sender caches do not retain a local payload, so they return None.

invalidate(mm_hash)

Drop mm_hash from this P0 shadow cache to recover from P0/P1 drift.

No-op by default; shadow caches that can drift from P1 override this.

is_cached(mm_hashes)

Check whether a sequence of multi-modal items are in the underlying cache.

This DOES NOT update the cache eviction order.

Parameters:

  • mm_hashes

    (list[str]) –

    The hash of each item to check.

Returns:

  • list[bool] –

    For each item, True if the item is cached, otherwise False.

is_cached_item(mm_hash) abstractmethod

Check whether a multi-modal item is in the underlying cache.

This DOES NOT update the cache eviction order.

Parameters:

  • mm_hash

    (str) –

    The hash of the item to check.

Returns:

  • bool –

    True if the item is cached, otherwise False.

make_stats(*, delta=False) abstractmethod

Get (and reset) the multi-modal cache stats.

Returns:

  • CacheInfo –

    The current multi-modal caching stats.

release_sender_touches()

Release the items that were touched but not updated afterwards.

touch_sender_cache_item(mm_hash) abstractmethod

Update the cache eviction order for a multi-modal item.

This is used to touch the item in the cache without changing its value.

Parameters:

  • mm_hash

    (str) –

    The hash of the multi-modal item.

validate_input_item(mm_item, mm_hash)

Validate externally supplied cache metadata before engine handoff.

BaseMultiModalReceiverCache

Bases: BaseMultiModalCache[MultiModalKwargsItem | None, MultiModalKwargsItem]

The required interface for caches on P1.

Methods:

get_and_update_features(mm_features)

Update multimodal features with cached encoder outputs. Touch all identifier at first before update to avoid item in updated list evict during update.

Uses mm_hash for cache key to share across LoRAs (falls back to identifier for backward compatibility).

touch_receiver_cache_item(mm_hash, mm_item=None) abstractmethod

Update the cache eviction order for a multi-modal item.

This is used to touch the item in the cache without changing its value.

Parameters:

  • mm_hash

    (str) –

    The hash of the multi-modal item.

  • mm_item

    (MultiModalKwargsItem | None, default: None ) –

    The multi-modal item itself. This is optional and may not be needed by some cache implementations.

MultiModalCacheMissError

Bases: RuntimeError

Raised by the P1 receiver cache when items are requested with no data and are not cached.

P0 (frontend) keeps a metadata-only shadow of P1 (engine) and sends data=None on a shadow hit. The two caches are updated in different orders across processes, so they can drift -- leaving P0 referencing items P1 has evicted. Raising (instead of asserting) lets the engine return a retryable response and have P0 drop the stale entries (BaseMultiModalProcessorCache.invalidate) so the client resends the data. Carries every drifted mm_hash in the request so P0 can drop them all in one pass -- one retry then recovers the whole request, not one item per retry.

MultiModalProcessorCacheItem

The data to store inside MultiModalProcessorOnlyCache.

Parameters:

MultiModalProcessorCacheItemMetadata

The metadata to store inside MultiModalProcessorSenderCache.

Parameters:

  • item

    (MultiModalKwargsItem) –

    The processed tensor data corresponding to a multi-modal item. Since P1 already stores the tensor data, we only store its size metadata in P0 to reduce memory usage. The size metadata is still needed to keep the same cache eviction policy as P0.

  • prompt_updates

    (Sequence[ResolvedPromptUpdate]) –

    The prompt updates corresponding to item. This needs to stay on P0 because for some models, they are dependent on the processed tensor data (cached on P1).

MultiModalProcessorOnlyCache

Bases: BaseMultiModalProcessorCache

The cache which is used on P0 when IPC caching is disabled.

How to update each item:

  • If the item is in the cache, replace the input with the cached item.
  • If the item is not in the cache, store that item (which includes tensor data and metadata) into the cache, and return the input.