vllm.v1.core.kv_cache_manager
KVCacheBlocks
dataclass
¶
The allocation result of KVCacheManager, work as the interface between Scheduler and KVCacheManager, to hide KVCacheManager's internal data structure from the Scheduler.
Source code in vllm/v1/core/kv_cache_manager.py
blocks
instance-attribute
¶
blocks: tuple[list[KVCacheBlock], ...]
blocks[i][j] refers to the i-th kv_cache_group and the j-th block of tokens. We don't use block of tokens as the outer dimension because it assumes all kv_cache_groups have the same number of blocks, which is true for now but will be broken if we want to give different block_size to different kv_cache_groups in the future.
__add__
¶
__add__(other: KVCacheBlocks) -> KVCacheBlocks
get_block_ids
¶
Converts the KVCacheBlocks instance to block_ids.
Returns:
Type | Description |
---|---|
list[int]
|
tuple[list[int], ...]: A tuple of lists where |
...
|
|
tuple[list[int], ...]
|
|
Source code in vllm/v1/core/kv_cache_manager.py
get_unhashed_block_ids
¶
Get block_ids of unhashed blocks from KVCacheBlocks instance.
Source code in vllm/v1/core/kv_cache_manager.py
new_empty
¶
new_empty() -> KVCacheBlocks
KVCacheManager
¶
Source code in vllm/v1/core/kv_cache_manager.py
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
caching_hash_fn
instance-attribute
¶
coordinator
instance-attribute
¶
coordinator = get_kv_cache_coordinator(
kv_cache_config=kv_cache_config,
max_model_len=max_model_len,
use_eagle=use_eagle,
enable_caching=enable_caching,
caching_hash_fn=caching_hash_fn,
enable_kv_cache_events=enable_kv_cache_events,
)
prefix_cache_stats
instance-attribute
¶
prefix_cache_stats = (
PrefixCacheStats() if log_stats else None
)
req_to_block_hashes
instance-attribute
¶
req_to_block_hashes: defaultdict[str, list[BlockHash]] = (
defaultdict(list)
)
__init__
¶
__init__(
kv_cache_config: KVCacheConfig,
max_model_len: int,
enable_caching: bool = True,
caching_hash_algo: str = "builtin",
use_eagle: bool = False,
log_stats: bool = False,
enable_kv_cache_events: bool = False,
) -> None
Source code in vllm/v1/core/kv_cache_manager.py
allocate_slots
¶
allocate_slots(
request: Request,
num_new_tokens: int,
num_new_computed_tokens: int = 0,
new_computed_blocks: Optional[KVCacheBlocks] = None,
num_draft_tokens: int = 0,
num_lookahead_tokens: int = 0,
delay_cache_blocks: bool = False,
) -> Optional[KVCacheBlocks]
Add slots for a request with new tokens to append.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to allocate slots. |
required |
num_new_tokens
|
int
|
The number of tokens to allocate, including external tokens. Note that this does not include tokens that have already been computed locally (i.e. new_computed_blocks). |
required |
num_new_computed_tokens
|
int
|
The number of new computed tokens just hitting the prefix caching, excluding external tokens. |
0
|
new_computed_blocks
|
Optional[KVCacheBlocks]
|
The cached blocks for the above new computed tokens. |
None
|
num_lookahead_tokens
|
int
|
The number of speculative tokens to allocate. This is used by spec decode proposers with kv-cache such as eagle. |
0
|
delay_cache_blocks
|
bool
|
Whether to skip caching the blocks. This is used by P/D when allocating blocks used in a KV transfer which will complete in a future step. |
False
|
Blocks layout:
-----------------------------------------------------------------------
| < computed > | < new computed > | < new > | < pre-allocated > |
-----------------------------------------------------------------------
| < required > |
--------------------------------------------------
| < full > |
------------------------------------------------
| <new full> |
--------------
Returns:
Type | Description |
---|---|
Optional[KVCacheBlocks]
|
A list of new allocated blocks. |
Source code in vllm/v1/core/kv_cache_manager.py
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 291 292 293 294 295 296 |
|
cache_blocks
¶
Cache the blocks for the request, if enabled.
Source code in vllm/v1/core/kv_cache_manager.py
create_empty_block_list
¶
create_empty_block_list() -> KVCacheBlocks
free
¶
free(request: Request) -> None
Free the blocks allocated for the request. We free the blocks in reverse order so that he tail blocks are evicted first when caching is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to free the blocks. |
required |
Source code in vllm/v1/core/kv_cache_manager.py
free_block_hashes
¶
free_block_hashes(request: Request) -> None
Discard the block hashes for the request.
NOTE: Unlike free
, this method should be called only when the request
is finished, not when it is preempted.
Source code in vllm/v1/core/kv_cache_manager.py
get_block_ids
¶
get_computed_blocks
¶
get_computed_blocks(
request: Request,
) -> tuple[KVCacheBlocks, int]
Get the computed (cached) blocks for the request. Note that the computed blocks must be full.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to get the computed blocks. |
required |
Returns:
Type | Description |
---|---|
tuple[KVCacheBlocks, int]
|
A tuple containing: - A list of blocks that are computed for the request. - The number of computed tokens. |
Source code in vllm/v1/core/kv_cache_manager.py
get_num_common_prefix_blocks
¶
Calculate the number of common prefix blocks shared by all requests in the RUNNING state for each kv cache group.
The function determines this by selecting any request and iterating
through its blocks. A block is considered a common prefix block if its
ref_cnt
equals the total number of requests in the RUNNING state.
NOTE(woosuk): The number of requests in the RUNNING state is greater than or equal to the number of requests scheduled in the current step. This is because the RUNNING state only indicates that: 1. The request has not yet finished, and 2. The request holds its blocks unfreed.
While all scheduled requests must be in the RUNNING state, the inverse is not necessarily true. There may be RUNNING requests that are not scheduled in the current step.
This can result in an edge case where the number of common prefix blocks is 0, even though all scheduled requests share a common prefix. This occurs because there may be unscheduled RUNNING requests that do not share the common prefix. Currently, this case cannot be easily detected, so the function returns 0 in such cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
Any request in the RUNNING state, used to identify the common prefix blocks. |
required |
num_running_requests
|
int
|
The total number of requests in the RUNNING state. This can be different from the number of scheduled requests in the current step. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
list[int]: The number of common prefix blocks for each kv cache |
list[int]
|
group. |
Source code in vllm/v1/core/kv_cache_manager.py
make_prefix_cache_stats
¶
make_prefix_cache_stats() -> Optional[PrefixCacheStats]
Get (and reset) the prefix cache stats.
Returns:
Type | Description |
---|---|
Optional[PrefixCacheStats]
|
The current prefix caching stats, or None if logging is disabled. |
Source code in vllm/v1/core/kv_cache_manager.py
reset_prefix_cache
¶
reset_prefix_cache() -> bool
Reset prefix cache. This function may be used in RLHF flows to invalidate prefix caching after the weights are updated, or used for resetting prefix caching status for benchmarking.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the prefix cache is successfully reset, |
bool
|
False otherwise. |
Source code in vllm/v1/core/kv_cache_manager.py
take_events
¶
take_events() -> list[KVCacheEvent]
Take the KV cache events from the block pool.
Returns:
Type | Description |
---|---|
list[KVCacheEvent]
|
A list of KV cache events. |