vllm.v1.core.encoder_cache_manager ¶
Classes:
-
EncoderCacheManager–Manages caching of encoder outputs for multimodal models in vLLM V1.
-
EncoderDecoderCacheManager–
Functions:
-
compute_mm_encoder_budget–Compute the encoder cache budget based on the model and scheduler
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.
-
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.
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_freed_mm_hashes–Get and clear the list of recently freed encoder cache entries.
-
reset–Reset the encoder cache to its initial state.
Source code in vllm/v1/core/encoder_cache_manager.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 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 | |
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.
Source code in vllm/v1/core/encoder_cache_manager.py
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
freeableentries); otherwise -
bool–False.
Note: This method does not allocate physical memory for the encoder output but only the state of EncoderCacheManager.
Source code in vllm/v1/core/encoder_cache_manager.py
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
Source code in vllm/v1/core/encoder_cache_manager.py
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.
Source code in vllm/v1/core/encoder_cache_manager.py
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).
Source code in vllm/v1/core/encoder_cache_manager.py
get_cached_input_ids(request) ¶
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.
Source code in vllm/v1/core/encoder_cache_manager.py
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.
Source code in vllm/v1/core/encoder_cache_manager.py
EncoderDecoderCacheManager ¶
Bases: EncoderCacheManager
Methods:
-
reset–Reset the encoder cache to its initial state.
Source code in vllm/v1/core/encoder_cache_manager.py
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.