Skip to content

vllm.v1.core.encoder_cache_manager

Classes:

Functions:

EncoderCacheManager

Manages caching of encoder outputs for multimodal models in vLLM V1.

The EncoderCacheManager handles the lifecycle of multimodal encoder outputs (such as vision embeddings from images) during request processing. It provides memory-aware caching to avoid recomputing encoder outputs when the same multimodal inputs appear in different stages of request processing.

This manager is particularly important for: - Vision-language models (e.g., LLaVA) where image encoder outputs are cached - Any multimodal model where encoder computation is expensive and cacheable

The cache operates at the granularity of individual multimodal input items within requests, allowing for fine-grained memory management and enabling chunked processing of multimodal inputs.

Cache is enabled to share embeddings of same multimodal data item (identified by their hash value) between different requests, and eviction takes place at allocation time when there's no free space for new embeddings. Oldest cached embeddings with no request referenced will be first evicted.

NOTE: The EncoderCacheManager operates on the level of multimodal embeddings instead of encoder tokens (i.e. all tokens that represent the multimodal data in the input sequence). This means all break/text tokens in-between multimodal embeddings are not considered with respect to the cache size and the number of free slots.

Parameters:

  • cache_size

    (int) –

    Limit the size of the cache, measured by the number of encoder embeddings from the input sequence.

Attributes:

  • cache_size –

    Total cache capacity in encoder embeddings.

  • num_free_slots –

    Current available cache capacity in encoder embeddings.

  • num_freeable_slots –

    Capacity that can be immediately reclaimed by evicting entries with zero references (in encoder embeddings).

  • cached (dict[str, set[str]]) –

    Mapping from mm_hash to a set of request IDs that currently reference the cached entry. If the set is empty, the entry exists but is not referenced by any request and is eligible for reclamation.

  • num_embeds_by_hash (dict[str, int]) –

    Mapping from mm_hash to the number of encoder embeddings stored for that cache entry.

  • freeable (OrderedDict[str, int]) –

    List of tuples (mm_hash, num_encoder_embeds) representing entries whose no current running request is needed and that can be freed to make space when needed.

  • freed (list[str]) –

    List of mm_hash strings that were actually evicted since the last call to get_freed_mm_hashes(). This list is cleared on return.

  • events (list[ECCacheEvent]) –

    Encoder cache events collected since the last call to take_events(). Empty unless EC cache events are enabled.

Methods:

  • allocate –

    Allocate cache space for a multimodal input's encoder output.

  • can_allocate –

    Check if there's sufficient cache space for a multimodal input.

  • check_and_update_cache –

    Check if encoder output for a specific multimodal input is cached.

  • free –

    Free all encoder input cache reference held by request.

  • free_encoder_input –

    Free the request's reference to the encoder input (mm_data).

  • get_cached_input_ids –

    Get all cached multimodal input IDs for a request.

  • get_cached_num_encoder_embeds –

    Return the encoder-embedding count stored for a cached input.

  • get_freed_mm_hashes –

    Get and clear the list of recently freed encoder cache entries.

  • reset –

    Reset the encoder cache to its initial state.

  • take_events –

    Take the encoder cache events collected since the last call.

  • try_rollback_allocations –

    Undo unexecuted allocations if no other request depends on them.

allocate(request, input_id)

Allocate cache space for a multimodal input's encoder output.

This reserves cache space for storing the encoder output of the specified multimodal input. The actual encoder output storage happens in the model runner; this method updates the manager's bookkeeping.

Note

This method assumes can_allocate() returned True for the same input.

can_allocate(request, input_id, encoder_compute_budget, num_embeds_to_schedule)

Check if there's sufficient cache space for a multimodal input. If there is, return True and update EncoderCacheManager state.

If there is not enough free space in num_free_slots but there is enough reclaimable space in num_freeable_slots, entries will be evicted from freeable (their mm_hash appended to freed) until enough space is available, and then this method returns True. Older entries are evicted first.

Returns False only if the requested number of tokens exceeds both the free and reclaimable capacities combined.

Parameters:

  • request

    (Request) –

    The request containing the multimodal input.

  • input_id

    (int) –

    Index of the multimodal input within the request.

  • encoder_compute_budget

    (int) –

    Number of encoder embeddings allowed to be computed when this method is invoked.

  • num_embeds_to_schedule

    (int) –

    Number of encoder embeddings already scheduled to be allocated with cache space when this method is invoked.

Returns:

  • bool –

    True if there's enough capacity to hold the encoder output for this

  • bool –

    input (possibly after reclaiming freeable entries); otherwise

  • bool –

    False.

Note: This method does not allocate physical memory for the encoder output but only the state of EncoderCacheManager.

check_and_update_cache(request, input_id)

Check if encoder output for a specific multimodal input is cached.

If the encoder output is cached, update cached to add the request id to the set of request ids that reference the cached encoder output. If the encoder output was previously not referenced by any request, update freeable and num_freeable_slots accordingly.

Parameters:

  • request

    (Request) –

    The request containing the multimodal input

  • input_id

    (int) –

    Index of the multimodal input within the request

Returns:

  • bool –

    True if the encoder output for this input is already cached

free(request)

Free all encoder input cache reference held by request.

For each cached input ID, free_encoder_input is invoked. The data stays in memory until eviction is triggered by a future attempt allocation called by 'can_allocate'.

Typically called when a request is finished, cancelled, or aborted.

free_encoder_input(request, input_id)

Free the request's reference to the encoder input (mm_data).

When the reference set for the corresponding mm_hash becomes empty, the entry is appended to freeable and num_freeable_slots is increased by the number of encoder embeddings for that input.

The entry is NOT physically freed until capacity is needed (e.g., by can_allocate).

get_cached_input_ids(request)

Get all cached multimodal input IDs for a request.

get_cached_num_encoder_embeds(request, input_id)

Return the encoder-embedding count stored for a cached input.

get_freed_mm_hashes()

Get and clear the list of recently freed encoder cache entries.

Returns:

  • list[str] –

    List of mm_hash strings that were actually evicted since the last

  • list[str] –

    call to be used by the scheduler to notify workers about which

  • list[str] –

    encoder outputs can be removed from their caches. The internal

  • list[str] –

    list is cleared after this call.

reset()

Reset the encoder cache to its initial state.

This clears all cached encoder outputs and resets capacity tracking. Called when model weights are updated to invalidate stale embeddings.

take_events()

Take the encoder cache events collected since the last call.

Returns:

try_rollback_allocations(request, input_ids)

Undo unexecuted allocations if no other request depends on them.

EncoderDecoderCacheManager

Bases: EncoderCacheManager

Methods:

  • reset –

    Reset the encoder cache to its initial state.

reset()

Reset the encoder cache to its initial state.

compute_mm_encoder_budget(scheduler_config, mm_max_toks_per_item)

Compute the encoder cache budget based on the model and scheduler configurations for a multimodal model.

Parameters:

  • scheduler_config

    (SchedulerConfig) –

    Scheduler configuration.

  • mm_max_toks_per_item

    (Mapping[str, int]) –

    The maximum number of tokens per item for each non-text modality.

Returns:

  • int –
    • Compute budget for encoder execution, measured in number of tokens from the input sequence.
  • int –
    • Space budget for encoder cache size, measured in number of tokens from the input sequence.