vllm.v1.core.kv_cache_utils ¶
KV-Cache Utilities.
BlockHashWithGroupId module-attribute ¶
FreeKVCacheBlockQueue ¶
This class organizes a list of KVCacheBlock objects to a doubly linked list of free blocks. We implement this class instead of using Python builtin deque to support removing a block in the middle of the queue in O(1) time. To close the performance gap to the builtin deque which is implemented in C++, this class does not allocate any Python objects when manipulating the linked list. Instead, this class manipulates the prev_free_block and next_free_block attributes of the given blocks.
The queue is ordered by block ID in the beginning. When a block is allocated and then freed, it will be appended back with the eviction order: 1. The least recent used block is at the front (LRU). 2. If two blocks have the same last accessed time (allocated by the same sequence), the one with more hash tokens (the tail of a block chain) is at the front. Note that we maintain this order by reversing the block order when free blocks of a request. This operation is outside of this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks | list[KVCacheBlock] | A list of KVCacheBlock objects. | required |
Source code in vllm/v1/core/kv_cache_utils.py
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
__init__ ¶
__init__(blocks: list[KVCacheBlock]) -> None
Source code in vllm/v1/core/kv_cache_utils.py
append ¶
append(block: KVCacheBlock) -> None
Put a block back into the free list and increase num_free_blocks by 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block | KVCacheBlock | The block to append. | required |
Source code in vllm/v1/core/kv_cache_utils.py
append_n ¶
append_n(blocks: list[KVCacheBlock]) -> None
Put a list of blocks back into the free list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks | list[KVCacheBlock] | The blocks to append. | required |
Source code in vllm/v1/core/kv_cache_utils.py
get_all_free_blocks ¶
get_all_free_blocks() -> list[KVCacheBlock]
Get all free blocks in the free list. Mainly used for testing.
Returns:
| Type | Description |
|---|---|
list[KVCacheBlock] | A list of free blocks. |
Source code in vllm/v1/core/kv_cache_utils.py
popleft ¶
popleft() -> KVCacheBlock
Pop the first free block and reduce num_free_blocks by 1.
Returns:
| Type | Description |
|---|---|
KVCacheBlock | The first free block. |
Source code in vllm/v1/core/kv_cache_utils.py
popleft_n ¶
popleft_n(n: int, **kwargs) -> list[KVCacheBlock]
Pop the first n free blocks and reduce num_free_blocks by n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | The number of blocks to pop. | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheBlock] | A list of n free blocks. |
Source code in vllm/v1/core/kv_cache_utils.py
remove ¶
remove(block: KVCacheBlock) -> None
Remove a block in the free list and reduce num_free_blocks by 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block | KVCacheBlock | The block to remove. | required |
Source code in vllm/v1/core/kv_cache_utils.py
KVCacheBlock dataclass ¶
KV-cache block metadata.
Source code in vllm/v1/core/kv_cache_utils.py
__init__ ¶
__init__(
block_id: int,
ref_cnt: int = 0,
_block_hash: Optional[BlockHashWithGroupId] = None,
prev_free_block: Optional[KVCacheBlock] = None,
next_free_block: Optional[KVCacheBlock] = None,
is_null: bool = False,
_type_info: str = "default",
last_accessed: float = 0,
) -> None
__repr__ ¶
__repr__() -> str
Source code in vllm/v1/core/kv_cache_utils.py
decr_ref ¶
incr_ref ¶
PrefixCachingMetrics ¶
Metrics for prefix caching with a hit rate of the max recent N requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_recent_requests | int | The number of the max recent requests to aggregate. Defaults to 1000. | 1000 |
Source code in vllm/v1/core/kv_cache_utils.py
__init__ ¶
__init__(max_recent_requests: int = 1000)
Source code in vllm/v1/core/kv_cache_utils.py
observe ¶
observe(stats: PrefixCacheStats)
Observe the prefix caching for a set of requests.
This function is called with information gathered when new requests are being scheduled and are looking for computed blocks.
When there are more than max_recent_requests requests, the oldest set of requests are removed from the metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats | PrefixCacheStats | The prefix cache stats. | required |
Source code in vllm/v1/core/kv_cache_utils.py
WAFreeQueue ¶
Bases: FreeKVCacheBlockQueue
This class (WAQ) is an enhanced version of FreeKVCacheBlockQueue (FeQ).
The main difference between WAQ and FeQ is that WAQ organizes multiple double-linked queues, totaling num_free_blocks blocks. Each double-linked queue represents a workload category; all blocks in a given queue belong to the same category.
Initially, there is only one default queue since blocks have no assigned category. During allocation, each KVCacheBlock is assigned a type_info tag indicating its workload category.
When append is called, WAQ inserts the KVCacheBlock into its category's queue. If the queue doesn't exist, it will be created.
When remove is called, the block is removed from its category's queue in O(1) time.
When popLeft is called, WAQ considers all queues and selects the block with the lowest reuse probability. Since blocks within each queue remain sorted by last accessed time (LRU), only the first block in each queue needs evaluation. The block with the minimal reuse probability is chosen as the victim.
When a block is allocated and then freed, it will also be appended back with the eviction order: 1. Insert to the corresponding workload queue. 2. The least recent used block is at the front (LRU). 3. If two blocks have the same last accessed time (allocated by the same sequence), the one with more hash tokens (the tail of a block chain) is at the front. Note that we maintain this order by reversing the block order when free blocks of a request. This operation is outside of this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks | list[KVCacheBlock] | A list of KVCacheBlock objects. | required |
wa_offline_param_path | Optional[str] | The path of the offline parameter. | required |
Source code in vllm/v1/core/kv_cache_utils.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 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 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
default_free_list_head instance-attribute ¶
default_free_list_head: Optional[KVCacheBlock] = blocks[0]
default_free_list_tail instance-attribute ¶
default_free_list_tail: Optional[KVCacheBlock] = blocks[-1]
workload_to_head_tail instance-attribute ¶
__init__ ¶
__init__(
blocks: list[KVCacheBlock],
wa_offline_param_path: Optional[str],
) -> None
Source code in vllm/v1/core/kv_cache_utils.py
_get_priority ¶
Source code in vllm/v1/core/kv_cache_utils.py
append ¶
append(block: KVCacheBlock) -> None
Put a block back into the free list and increase num_free_blocks by 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block | KVCacheBlock | The block to append. | required |
Source code in vllm/v1/core/kv_cache_utils.py
append_n ¶
append_n(blocks: list[KVCacheBlock]) -> None
Put a list of blocks back into the free list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks | list[KVCacheBlock] | The blocks to append. | required |
get_all_free_blocks ¶
get_all_free_blocks() -> list[KVCacheBlock]
Get all free blocks in the free list. Mainly used for testing.
Returns:
| Type | Description |
|---|---|
list[KVCacheBlock] | A list of free blocks. |
Source code in vllm/v1/core/kv_cache_utils.py
popleft ¶
popleft() -> KVCacheBlock
Pop the least priority free block and reduce num_free_blocks by 1.
Returns:
| Type | Description |
|---|---|
KVCacheBlock | The first free block. |
Source code in vllm/v1/core/kv_cache_utils.py
popleft_n ¶
popleft_n(n: int, **kwargs) -> list[KVCacheBlock]
Pop the first n free blocks and reduce num_free_blocks by n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | The number of blocks to pop. | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheBlock] | A list of n free blocks. |
Source code in vllm/v1/core/kv_cache_utils.py
remove ¶
remove(block: KVCacheBlock) -> None
Remove a block in the free list and reduce num_free_blocks by 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block | KVCacheBlock | The block to remove. | required |
Source code in vllm/v1/core/kv_cache_utils.py
_gen_lora_extra_hash_keys ¶
Generate extra keys related to LoRA for block hash computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | The request object. | required |
Returns:
| Type | Description |
|---|---|
list[int] | Return LoRA id of the request if it is a LoRA request. Return empty |
list[int] | list otherwise. |
Source code in vllm/v1/core/kv_cache_utils.py
_gen_mm_extra_hash_keys ¶
_gen_mm_extra_hash_keys(
request: Request,
start_token_idx: int,
end_token_idx: int,
start_mm_idx: int,
) -> tuple[list[Any], int]
Generate extra keys related to MultiModal request for block hash computation. For multi-modal inputs, the extra keys are (mm_hash, start_offset) that indicate a mm input contained in the block and its starting offset in the block tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | The request object. | required |
start_token_idx | int | The start token index of the block. | required |
end_token_idx | int | The end token index of the block. | required |
start_mm_idx | int | The start multi-modal index of the block. | required |
Returns:
| Type | Description |
|---|---|
tuple[list[Any], int] | A tuple of extra keys and the next multi-modal index. |
Source code in vllm/v1/core/kv_cache_utils.py
_get_kv_cache_groups_uniform_page_size ¶
_get_kv_cache_groups_uniform_page_size(
kv_cache_spec: dict[str, KVCacheSpec],
) -> list[KVCacheGroupSpec]
Generates the KV cache groups for hybrid models with multiple attention types but still with a uniform page size (physical memory per block per layer) for all layers.
Detailed explanation about kv cache management of hybrid models: The layers in the models are repeated with some patterns, e.g., a model with 10 full attention layers and 20 sliding window attention layers can be regarded as repeating the pattern (1 * full, 2 * sw) 10 times. The KVCacheManager allocates different block tables for each of the 3 layers in the pattern, and repeats each of them 10 times to generate the block_table for the 30 layers in the model. Therefore, we can group the layers in the model into 3 kv_cache_groups, each of which contains 10 layers in the model. The KVCacheManager allocates the block_table for each group based on its kv_cache spec, and the model runner applies the block table to each layer in the group. For example: 1. A model only uses full attention. The pattern is (num_hidden_layers * full), so there is only one group and the block table is shared by all layers. It is already handled by _get_kv_cache_config_uniform_type. 2. A model with 10 full attention layers and 20 sliding window attention layers. There are 3 layers in the pattern (1 * full, 2 * sw), so there are 3 kv_cache_groups, each of which represents 10 layers.
To simplify the implementation, we make the following assumptions: 1. Physical memory per block: Must be the same across all KV cache groups. Breaking this assumption is non-trivial due to memory fragmentation concerns when allocating blocks of different sizes. 2. Tokens per block (block_size): Currently, we directly use CacheConfig.block_size for all layers. It can be extended to vary by KV cache group, but within each KV cache group, all layers must share the same block size. 3. Physical memory per token per layer: This property is decided by model config. Currently we only support models that have the same physical memory per token per layer for all layers. Can be relaxed with a simple extension, but still need to keep physical memory per block the same for all groups. 4. Number of layers per group: Currently assumed the same for all layers. Can be relaxed with a simple extension, but still need to keep physical memory per block the same for all groups. 5. Attention type within groups: All layers in a group must share the same attention type. One exception is that, when --disable-hybrid-kv-cache-manager is true, the single group for full attention layers may also include attention layers using sliding window or LLaMA 4 local attention. See unify_hybrid_kv_cache_specs for more details. 6. Support for multiple attention types: The design for most components is general to an arbitrary number of attention types. But find_longest_cache_hit only supports one attention type or two types of full-attention plus exactly one another type. The general implementation of this function is feasible but we don't know how to implement it cleanly yet.
As we assume tokens per block, physical memory per token per layer, and number of layers per group are the same now, we can ensure that physical memory per block is the same for all groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_spec | dict[str, KVCacheSpec] | The KVCacheSpec of each attention layer in the model | required |
Returns: The generated KVCacheGroupSpecs
Source code in vllm/v1/core/kv_cache_utils.py
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 | |
_get_kv_cache_groups_uniform_spec ¶
_get_kv_cache_groups_uniform_spec(
kv_cache_specs: dict[str, KVCacheSpec],
) -> list[KVCacheGroupSpec]
Generates the KV cache configuration for a model with the same KV cache spec for all layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_specs | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheGroupSpec] | The generated KVCacheGroupSpecs |
Source code in vllm/v1/core/kv_cache_utils.py
_get_kv_cache_groups_uniform_type ¶
_get_kv_cache_groups_uniform_type(
spec: UniformTypeKVCacheSpecs,
) -> list[KVCacheGroupSpec]
Generates the KV cache configuration for a model with one type of KV cache but different hidden sizes. All layers are merged into one group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | UniformTypeKVCacheSpecs | The UniformTypeKVCacheSpecs of the model | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheGroupSpec] | The generated KVCacheGroupSpecs |
Source code in vllm/v1/core/kv_cache_utils.py
_report_kv_cache_config ¶
_report_kv_cache_config(
vllm_config: VllmConfig, kv_cache_config: KVCacheConfig
) -> None
Log resolved KV cache configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_config | KVCacheConfig | The resolved KV cache configuration | required |
Source code in vllm/v1/core/kv_cache_utils.py
check_enough_kv_cache_memory ¶
check_enough_kv_cache_memory(
vllm_config: VllmConfig,
kv_cache_spec: dict[str, KVCacheSpec],
available_memory: int,
)
Checks whether available_memory is enough for the KV cache to hold at least one request with the model's max_model_len.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_spec | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |
available_memory | int | Memory available for KV cache in bytes. | required |
Raises:
| Type | Description |
|---|---|
ValueError | If there is not enough memory available for the KV cache. |
Source code in vllm/v1/core/kv_cache_utils.py
create_kv_cache_group_specs ¶
create_kv_cache_group_specs(
kv_cache_spec: dict[str, KVCacheSpec],
grouped_layer_names: list[list[str]],
) -> list[KVCacheGroupSpec]
Create KVCacheGroupSpec object for each kv cache group layer. The layers in the same group should share the same KVCacheSpec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_spec | dict[str, KVCacheSpec] | A mapping from each layer name to its corresponding KVCacheSpec. | required |
grouped_layer_names | list[list[str]] | A list of kv cache groups, where each element is a list of layer names that belong to the same group and should share the same KVCacheSpec. | required |
Returns: A list of KVCacheGroupSpec objects, one for each group.
Source code in vllm/v1/core/kv_cache_utils.py
estimate_max_model_len ¶
estimate_max_model_len(
vllm_config: VllmConfig,
kv_cache_spec: dict[str, KVCacheSpec],
available_memory: int,
) -> int
Estimates the maximum model length that can fit in the available memory using binary search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_spec | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |
available_memory | int | Memory available for KV cache in bytes. | required |
Returns:
| Type | Description |
|---|---|
int | The estimated maximum model length that can fit in the available memory. |
Source code in vllm/v1/core/kv_cache_utils.py
generate_block_hash_extra_keys ¶
generate_block_hash_extra_keys(
request: Request,
start_token_idx: int,
end_token_idx: int,
start_mm_idx: int,
) -> tuple[Optional[tuple[Any, ...]], int]
Generate extra keys for the block hash. The extra keys can come from the multi-modal inputs and request specific metadata (e.g., LoRA ID).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | The request object. | required |
start_token_idx | int | The start token index of the block. | required |
end_token_idx | int | The end token index of the block. | required |
start_mm_idx | int | The start multi-modal index of the block. | required |
Returns:
| Type | Description |
|---|---|
tuple[Optional[tuple[Any, ...]], int] | A tuple of extra keys and the next multi-modal index. |
Source code in vllm/v1/core/kv_cache_utils.py
generate_scheduler_kv_cache_config ¶
generate_scheduler_kv_cache_config(
kv_cache_configs: list[KVCacheConfig],
) -> KVCacheConfig
Generate the KV cache configuration for the scheduler.
Source code in vllm/v1/core/kv_cache_utils.py
get_block_hash ¶
get_block_hash(key: BlockHashWithGroupId) -> BlockHash
get_group_id ¶
get_group_id(key: BlockHashWithGroupId) -> int
get_kv_cache_config_from_groups ¶
get_kv_cache_config_from_groups(
vllm_config: VllmConfig,
kv_cache_groups: list[KVCacheGroupSpec],
kv_cache_specs: dict[str, KVCacheSpec],
available_memory: int,
) -> KVCacheConfig
Generate the KV cache configuration from the KV cache groups and spec of each layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_groups | list[KVCacheGroupSpec] | The KV cache groups | required |
kv_cache_specs | dict[str, KVCacheSpec] | The KV cache spec of each attention layer in the model | required |
available_memory | int | Memory available for KV cache in bytes | required |
Returns: The generated KVCacheConfig
Source code in vllm/v1/core/kv_cache_utils.py
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 | |
get_kv_cache_configs ¶
get_kv_cache_configs(
vllm_config: VllmConfig,
kv_cache_specs: list[dict[str, KVCacheSpec]],
available_memory: list[int],
) -> list[KVCacheConfig]
Generates the KV cache configurations for a model. Since we use a shared centralized controller for all workers, we need the kv_cache_config to be consistent across all workers to make sure the KV cache allocation can be applied to all workers. However, different workers may have different memory available, and different type of layers (when pipeline parallel is enabled). To handle the difference between workers, the current implementation is: 1. Merge the KV cache specs of all workers to get the KVCacheSpecs for the whole model. 2. Generate the KV cache groups based on the layer ratio of the whole model. 3. Generate the KV cache configs for each worker based on the KV cache grouping strategy. (This is reasonable because the layer ratio of different PP stages are similar.) 4. Change the num_blocks of each worker to the smallest among all workers and shrink tensor sizes proportionally to avoid allocating unused memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_specs | list[dict[str, KVCacheSpec]] | List of dict[layer_name, KVCacheSpec] for each worker. | required |
available_memory | list[int] | Memory available for KV cache in bytes for each worker. | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheConfig] | The generated KVCacheConfigs for each worker. |
Source code in vllm/v1/core/kv_cache_utils.py
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 | |
get_kv_cache_groups ¶
get_kv_cache_groups(
vllm_config: VllmConfig,
kv_cache_spec: dict[str, KVCacheSpec],
) -> list[KVCacheGroupSpec]
Split the layers in the model into groups with the same KV cache spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
kv_cache_spec | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |
Returns:
| Type | Description |
|---|---|
list[KVCacheGroupSpec] | The generated KVCacheGroups |
Source code in vllm/v1/core/kv_cache_utils.py
get_max_concurrency_for_kv_cache_config ¶
get_max_concurrency_for_kv_cache_config(
vllm_config: VllmConfig, kv_cache_config: KVCacheConfig
) -> float
Get the maximum concurrency for the given KV cache configuration.
Source code in vllm/v1/core/kv_cache_utils.py
get_num_blocks ¶
get_num_blocks(
vllm_config: VllmConfig,
num_layers: int,
available_memory: int,
page_size: int,
) -> int
Get the number of kv cache blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The global VllmConfig | required |
num_layers | int | The number of layers | required |
available_memory | int | Memory available for KV cache in bytes. | required |
page_size | int | The page size of the KV cache. | required |
Source code in vllm/v1/core/kv_cache_utils.py
get_request_block_hasher ¶
get_request_block_hasher(
block_size: int, caching_hash_fn: Callable[[Any], bytes]
) -> Callable[[Request], list[BlockHash]]
Returns a function which computes the list of un-computed block hashes of a request.
Source code in vllm/v1/core/kv_cache_utils.py
get_uniform_page_size ¶
get_uniform_page_size(
kv_cache_spec: dict[str, KVCacheSpec],
) -> int
Get the page size of the KV cache.
Source code in vllm/v1/core/kv_cache_utils.py
hash_block_tokens ¶
hash_block_tokens(
hash_function: Callable[[Any], bytes],
parent_block_hash: Optional[BlockHash],
curr_block_token_ids: Sequence[int],
extra_keys: Optional[tuple[Any, ...]] = None,
) -> BlockHash
Computes a hash value corresponding to the contents of a block and the contents of the preceding block(s). The hash value is used for prefix caching. We use LRU cache for this function to avoid recomputing hash values for the same block contents. Args: hash_function: The hash function used to compute block hash. parent_block_hash: The hash of the parent block. None if this is the first block. curr_block_token_ids: A list of token ids in the current block. The current block is assumed to be full. extra_keys: Extra keys for the block. Returns: The hash value of the block and the token ids in the block. The entire tuple is used as the hash key of the block.
Source code in vllm/v1/core/kv_cache_utils.py
init_none_hash ¶
Source code in vllm/v1/core/kv_cache_utils.py
is_kv_cache_page_size_uniform ¶
is_kv_cache_page_size_uniform(
kv_cache_spec: dict[str, KVCacheSpec],
) -> bool
Whether all layers in the given KVCacheSpec have the same page size. Args: kv_cache_spec: The KVCacheSpec of each attention layer in the model
Returns:
| Type | Description |
|---|---|
bool | True if all layers have the same page size, False otherwise. |
Source code in vllm/v1/core/kv_cache_utils.py
is_kv_cache_spec_uniform ¶
is_kv_cache_spec_uniform(
kv_cache_spec: dict[str, KVCacheSpec],
) -> bool
Whether all layers in the given KVCacheSpec have the same KV cache spec. Note that we regard FullAttentionSpec with and without sliding window as the same type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_spec | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |
Returns:
| Type | Description |
|---|---|
bool | True if all layers have the same type, False otherwise. |
Source code in vllm/v1/core/kv_cache_utils.py
is_kv_cache_type_attention_free ¶
is_kv_cache_type_attention_free(
kv_cache_spec: dict[str, KVCacheSpec],
) -> bool
make_block_hash_with_group_id ¶
make_block_hash_with_group_id(
block_hash: BlockHash, group_id: int
) -> BlockHashWithGroupId
Pack a BlockHash and group id into a BlockHashWithGroupId.
The group id is encoded using 4 bytes in big-endian order and appended to the block hash bytes. This representation avoids creating tuples while still allowing us to recover both components when needed.
Source code in vllm/v1/core/kv_cache_utils.py
max_memory_usage_bytes ¶
max_memory_usage_bytes(
vllm_config: VllmConfig,
kv_cache_specs: Iterable[KVCacheSpec],
) -> int
Get the maximum memory usage in bytes for the given KV cache specs.
Source code in vllm/v1/core/kv_cache_utils.py
may_override_num_blocks ¶
may_override_num_blocks(
vllm_config: VllmConfig, num_blocks: int
) -> int
Override the number of kv cache blocks if num_gpu_blocks_override is set.
Source code in vllm/v1/core/kv_cache_utils.py
maybe_convert_block_hash ¶
maybe_convert_block_hash(
hash_bytes: BlockHash,
) -> ExternalBlockHash
need_extra_keys ¶
Check whether the blocks allocated to this request need extra hash keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | The request. | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | Whether blocks allocated to this request need extra hash keys. |
Source code in vllm/v1/core/kv_cache_utils.py
unify_hybrid_kv_cache_specs ¶
unify_hybrid_kv_cache_specs(
kv_cache_spec: dict[str, KVCacheSpec],
)
This function tries to convert the KV cache specs to one type if the model is a hybrid model with multiple type of KV cache. It will convert all SlidingWindowSpec to FullAttentionSpec if both types are present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_spec | dict[str, KVCacheSpec] | The kv cache spec of each attention layer in the model | required |