vllm.v1.core.kv_cache_coordinator ¶
Classes:
-
HybridKVCacheCoordinator–KV cache coordinator for hybrid models with multiple KV cache types, and
-
KVCacheCoordinator–Coordinate the KV cache of different KV cache groups.
-
KVCacheCoordinatorNoPrefixCache–KV cache coordinator to use if prefix caching is disabled or unsupported.
-
SpecGroup–KV cache groups that share one spec, batched together for a single
-
UnitaryKVCacheCoordinator–KV cache coordinator for models with only one KV cache group. This is the
HybridKVCacheCoordinator ¶
Bases: KVCacheCoordinator
KV cache coordinator for hybrid models with multiple KV cache types, and thus multiple kv cache groups.
Methods:
-
find_longest_cache_hit–Find the longest cache hit using an iterative fixed-point algorithm.
-
find_longest_cache_hit_per_group–Like find_longest_cache_hit but evaluates each group independently.
-
verify_and_split_kv_cache_groups–Groups KV cache groups by their spec type for efficient batch processing
Source code in vllm/v1/core/kv_cache_coordinator.py
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 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |
find_longest_cache_hit(block_hashes, max_cache_hit_length) ¶
Find the longest cache hit using an iterative fixed-point algorithm.
Each attention type either accepts the current candidate length or reduces it. If any type reduces the length, restart checks over all types. This converges because length monotonically decreases and is bounded below by 0.
Parameters:
-
(block_hashes¶list[BlockHash]) –The block hashes of the request.
-
(max_cache_hit_length¶int) –The maximum length of the cache hit.
Returns:
-
tuple[tuple[list[KVCacheBlock], ...], int, int]–A tuple containing: - A tuple of the cache hit blocks for each single type manager. - The number of tokens of the reconciled (combined) cache hit. -
num_uncached_common_prefix_tokens: a shared prefix that a sparse-retention group has not cached yet (0 unless hybrid).
Source code in vllm/v1/core/kv_cache_coordinator.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | |
find_longest_cache_hit_per_group(block_hashes, max_cache_hit_length) ¶
Like find_longest_cache_hit but evaluates each group independently.
Returns:
Source code in vllm/v1/core/kv_cache_coordinator.py
verify_and_split_kv_cache_groups() ¶
Groups KV cache groups by their spec type for efficient batch processing during cache hit lookup.
Source code in vllm/v1/core/kv_cache_coordinator.py
KVCacheCoordinator ¶
Bases: ABC
Coordinate the KV cache of different KV cache groups.
Methods:
-
allocate_new_blocks–Allocate new blocks for the request to give it at least
num_tokens -
allocate_new_computed_blocks–Add the new computed blocks to the request. Optionally allocate new
-
cache_blocks–Cache the blocks for the request.
-
find_longest_cache_hit–Returns the per-group hit blocks, the hit length, and the number of
-
free–Free the blocks for the request.
-
get_blocks–Get the blocks for the request.
-
get_num_blocks_to_allocate–Get the number of blocks needed to be allocated for the request.
-
get_num_common_prefix_blocks–Get the number of common prefix blocks for all requests with allocated
-
new_step_starts–Notify each manager that a new step is starting.
-
pop_blocks_for_free–Pop the request's bookkeeping from all single-type managers and
-
remove_skipped_blocks–Remove the blocks that are no longer needed from
blocksand replace
Source code in vllm/v1/core/kv_cache_coordinator.py
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 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 | |
allocate_new_blocks(request_id, num_tokens, num_tokens_main_model, num_encoder_tokens=0) ¶
Allocate new blocks for the request to give it at least num_tokens token slots.
Parameters:
-
(request_id¶str) –The request ID.
-
(num_tokens¶int) –The total number of tokens that need a slot (including tokens that are already allocated).
-
(num_tokens_main_model¶int) –The number of tokens for the main model (aka target model in spec decode). w/o spec decode, it is num_tokens; with spec decode, it is num_tokens - num_lookahead_tokens.
-
(num_encoder_tokens¶int, default:0) –The number of encoder tokens for allocating blocks for cross-attention.
Returns:
-
tuple[list[KVCacheBlock], ...]–The new allocated blocks.
Source code in vllm/v1/core/kv_cache_coordinator.py
allocate_new_computed_blocks(request_id, new_computed_blocks, num_local_computed_tokens, num_external_computed_tokens) ¶
Add the new computed blocks to the request. Optionally allocate new blocks for external computed tokens (if any).
Parameters:
-
(request_id¶str) –The request ID.
-
(new_computed_blocks¶tuple[Sequence[KVCacheBlock], ...]) –The new computed blocks just hitting the prefix cache.
-
(num_local_computed_tokens¶int) –The number of local computed tokens.
-
(num_external_computed_tokens¶int) –The number of external computed tokens.
Source code in vllm/v1/core/kv_cache_coordinator.py
cache_blocks(request, num_computed_tokens) ¶
Cache the blocks for the request.
Parameters:
-
(request¶Request) –The request.
-
(num_computed_tokens¶int) –The total number of tokens that need to be cached (including tokens that are already cached).
Source code in vllm/v1/core/kv_cache_coordinator.py
find_longest_cache_hit(block_hashes, max_cache_hit_length) abstractmethod ¶
Returns the per-group hit blocks, the hit length, and the number of num_uncached_common_prefix_tokens (a shared prefix that a sparse-retention group has not cached yet; 0 unless hybrid).
Source code in vllm/v1/core/kv_cache_coordinator.py
free(request_id) ¶
Free the blocks for the request.
Parameters:
get_blocks(request_id) ¶
Get the blocks for the request.
get_num_blocks_to_allocate(request_id, num_tokens, new_computed_blocks, num_encoder_tokens, total_computed_tokens, num_local_computed_tokens, num_tokens_main_model, apply_admission_cap=False) ¶
Get the number of blocks needed to be allocated for the request.
Parameters:
-
(request_id¶str) –The request ID.
-
(num_tokens¶int) –The total number of tokens that need a slot (including tokens that are already allocated).
-
(new_computed_blocks¶tuple[Sequence[KVCacheBlock], ...]) –The new computed blocks just hitting the prefix caching.
-
(num_encoder_tokens¶int) –The number of encoder tokens for allocating blocks for cross-attention.
-
(total_computed_tokens¶int) –Include both local and external tokens.
-
(num_local_computed_tokens¶int) –The number of local prefix-cache computed tokens.
-
(num_tokens_main_model¶int) –The number of tokens for the main model (aka target model in spec decode). w/o spec decode, it is num_tokens; with spec decode, it is num_tokens - num_lookahead_tokens.
-
(apply_admission_cap¶bool, default:False) –If True, apply the recycling-aware per-request admission cap (SWA / chunked-local). Set only by the full-sequence admission gate; per-step allocation must leave it False so the predictor matches
allocate_new_blocks.
Returns:
-
int–The number of blocks to allocate.
Source code in vllm/v1/core/kv_cache_coordinator.py
get_num_common_prefix_blocks(running_request_id) ¶
Get the number of common prefix blocks for all requests with allocated KV cache for each kv cache group.
Parameters:
-
(running_request_id¶str) –The request ID of any running request, used to identify the common prefix blocks.
Returns:
Source code in vllm/v1/core/kv_cache_coordinator.py
new_step_starts() ¶
pop_blocks_for_free(request_id) ¶
Pop the request's bookkeeping from all single-type managers and return its blocks without returning them to the block pool. The caller must eventually pass the returned blocks to block_pool.free_blocks, freeing them in reverse order (so that tail blocks are evicted first).
Parameters:
Returns:
-
list[KVCacheBlock]–The request's blocks in allocation order.
Source code in vllm/v1/core/kv_cache_coordinator.py
remove_skipped_blocks(request_id, processed_computed_tokens, num_prompt_tokens=None) ¶
Remove the blocks that are no longer needed from blocks and replace the removed blocks with null_block.
Parameters:
-
(request_id¶str) –The request ID.
-
(processed_computed_tokens¶int) –Computed-token prefix length covering fully processed and committed tokens only (safe to free).
-
(num_prompt_tokens¶int | None, default:None) –Optional prompt length. R-SWA managers use this to free gap blocks between the prefill tail and decode window; other manager types ignore it.
Source code in vllm/v1/core/kv_cache_coordinator.py
KVCacheCoordinatorNoPrefixCache ¶
Bases: KVCacheCoordinator
KV cache coordinator to use if prefix caching is disabled or unsupported. In contrast to UnitaryKVCacheCoordinator and HybridKVCacheCoordinator, supports arbitrary numbers of KV cache groups (including 0 groups). Does not implement any features related to prefix caching.
Source code in vllm/v1/core/kv_cache_coordinator.py
SpecGroup ¶
Bases: NamedTuple
KV cache groups that share one spec, batched together for a single cache-hit lookup.
use_eagle is True iff any member group is an EAGLE/MTP group. Members sharing a spec are cached and looked up jointly, so the EAGLE last-block drop is necessarily decided for the whole spec group.
Source code in vllm/v1/core/kv_cache_coordinator.py
UnitaryKVCacheCoordinator ¶
Bases: KVCacheCoordinator
KV cache coordinator for models with only one KV cache group. This is the case for models with only one KV cache type, e.g., all attention layers use full attention or all attention layers use sliding window attention.