vllm.multimodal.cache ¶
Classes:
-
BaseMultiModalCache–Abstract base class to read/write multi-modal items from cache.
-
BaseMultiModalProcessorCache–The required interface for caches on P0.
-
BaseMultiModalReceiverCache–The required interface for caches on P1.
-
MultiModalCache– -
MultiModalCacheMissError–Raised by the P1 receiver cache when items are requested with no data and
-
MultiModalProcessorCacheItem–The data to store inside
MultiModalProcessorOnlyCache. -
MultiModalProcessorCacheItemMetadata–The metadata to store inside
MultiModalProcessorSenderCache. -
MultiModalProcessorOnlyCache–The cache which is used on P0 when IPC caching is disabled.
-
MultiModalProcessorSenderCache–The cache which is used on P0 when IPC caching is enabled.
-
MultiModalReceiverCache–The cache which is used on P1 when IPC caching is enabled.
-
ShmObjectStoreReceiverCache–The cache which is used on P1 Worker Process when IPC caching is enabled.
-
ShmObjectStoreSenderCache–The cache which is used on P0 when IPC caching is enabled.
BaseMultiModalCache ¶
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–Insert
valueif it fits incache. -
clear_cache–Clear the underlying cache.
-
get_and_update–Possibly update a sequence of multi-modal items based on whether they
-
get_and_update_item–Possibly update a multi-modal item based on whether it is
Source code in vllm/multimodal/cache.py
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
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–Trueif the item was cached, otherwiseFalse.
Source code in vllm/multimodal/cache.py
clear_cache() abstractmethod ¶
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.
Source code in vllm/multimodal/cache.py
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:
Returns:
-
_O–The update multi-modal item.
Source code in vllm/multimodal/cache.py
BaseMultiModalProcessorCache ¶
Bases: BaseMultiModalCache[MultiModalProcessorCacheInItem, MultiModalProcessorCacheOutItem]
The required interface for caches on P0.
Methods:
-
close–Close the underlying cache, if needed.
-
invalidate–Drop
mm_hashfrom 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.
-
touch_sender_cache_item–Update the cache eviction order for a multi-modal item.
Source code in vllm/multimodal/cache.py
close() ¶
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:
Returns:
Source code in vllm/multimodal/cache.py
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:
Returns:
-
bool–Trueif the item is cached, otherwiseFalse.
Source code in vllm/multimodal/cache.py
make_stats(*, delta=False) abstractmethod ¶
Get (and reset) the multi-modal cache stats.
Returns:
-
CacheInfo–The current multi-modal caching stats.
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:
Source code in vllm/multimodal/cache.py
BaseMultiModalReceiverCache ¶
Bases: BaseMultiModalCache[MultiModalKwargsItem | None, MultiModalKwargsItem]
The required interface for caches on P1.
Methods:
-
get_and_update_features–Update multimodal features with cached encoder outputs.
-
touch_receiver_cache_item–Update the cache eviction order for a multi-modal item.
Source code in vllm/multimodal/cache.py
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).
Source code in vllm/multimodal/cache.py
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.
Source code in vllm/multimodal/cache.py
MultiModalCache ¶
Methods:
-
get_item_complexity–Get the number of leaf elements in a multi-modal cache value.
Source code in vllm/multimodal/cache.py
get_item_complexity(value) classmethod ¶
Get the number of leaf elements in a multi-modal cache value.
This provides a measure of structural complexity that can be useful for debugging cache performance and understanding data patterns.
Parameters:
-
(value¶MultiModalCacheValue) –The multi-modal cache value to analyze.
Returns:
-
int–The number of leaf elements in the nested structure.
Source code in vllm/multimodal/cache.py
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.
Source code in vllm/multimodal/cache.py
MultiModalProcessorCacheItem ¶
The data to store inside MultiModalProcessorOnlyCache.
Parameters:
-
(item¶MultiModalKwargsItem) –The processed tensor data corresponding to a multi-modal item.
-
(prompt_updates¶Sequence[ResolvedPromptUpdate]) –The prompt updates corresponding to
item.
Source code in vllm/multimodal/cache.py
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).
Source code in vllm/multimodal/cache.py
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.
Source code in vllm/multimodal/cache.py
MultiModalProcessorSenderCache ¶
Bases: BaseMultiModalProcessorCache
The cache which is used on P0 when IPC caching is enabled.
How to update each item:
-
If the item is already in the cache, clear the input to avoid unnecessary IPC.
-
If the item is not in the cache, store the metadata of that item so that the eviction policy remains the same as the cache on P1, and return the input. By only storing the metadata, we avoid keeping the data itself in memory inside P0.
Source code in vllm/multimodal/cache.py
MultiModalReceiverCache ¶
Bases: BaseMultiModalReceiverCache
The cache which is used on P1 when IPC caching is enabled.
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) into the cache, and return the input.
Source code in vllm/multimodal/cache.py
ShmObjectStoreReceiverCache ¶
Bases: BaseMultiModalReceiverCache
The cache which is used on P1 Worker Process when IPC caching is enabled.
How to update each item:
- If the item has an address, replace the input with the cached item.
- If not, return the input.
Methods:
-
touch_receiver_cache_item–Touch the item in shared memory cache to prevent eviction.
Source code in vllm/multimodal/cache.py
touch_receiver_cache_item(mm_hash, mm_item=None) ¶
Touch the item in shared memory cache to prevent eviction. Increments reader_count on receiver side.
Source code in vllm/multimodal/cache.py
ShmObjectStoreSenderCache ¶
Bases: BaseMultiModalProcessorCache
The cache which is used on P0 when IPC caching is enabled.
How to update each item:
-
If the item is already in the cache, clear the input to avoid unnecessary IPC.
-
If the item is not in the cache, store the data in shared memory.
Methods:
-
remove_dangling_items–Remove items that are no longer in the shared memory cache.
-
touch_sender_cache_item–Touch the item in shared memory cache to prevent eviction.
Source code in vllm/multimodal/cache.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | |