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.
-
UnitaryKVCacheCoordinator–KV cache coordinator for models with only one KV cache group. This is the
Functions:
-
replay_boundaries–Positions a later request replaying a prompt can resume at.
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 manager policy for efficient batch processing
Source code in vllm/v1/core/kv_cache_coordinator.py
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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
_align_cacheable(num_tokens)
¶
Largest prefix of num_tokens a future cache hit could match.
Hits are scheduler_block_size-aligned (see
find_longest_cache_hit) unless fine-grained partial hash hits are
enabled, in which case no rounding applies -- rounding even to
hash_block_size would re-register a privatized Mamba tail.
Source code in vllm/v1/core/kv_cache_coordinator.py
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
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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
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 manager policy 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 device blocks needed to be allocated for the request.
-
get_num_cacheable_tokens–Return the prefix eligible for hashing in this group.
-
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 -
reset_prefix_cache–Reset each manager's pool once.
Source code in vllm/v1/core/kv_cache_coordinator.py
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 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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
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, skip_zeroing_group_ids=())
¶
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.
-
(skip_zeroing_group_ids¶tuple[int, ...], default:()) –Groups whose external-token blocks are written by an async load and must not be zeroed.
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, prefill_end=0)
¶
Get the number of device 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. -
(prefill_end¶int, default:0) –The token index the request's prefill ends at, the same value the scheduler splits chunks against. Under sparse retention Mamba reserves a prefill checkpoint only on the chunk reaching it; under dense retention every chunk publishes a state, so it is ignored (0).
Returns:
-
int–The number of blocks to allocate.
Source code in vllm/v1/core/kv_cache_coordinator.py
get_num_cacheable_tokens(num_computed_tokens, kv_cache_group_id)
¶
Return the prefix eligible for hashing in this group.
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
reset_prefix_cache()
¶
Reset each manager's pool once.
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
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.
Source code in vllm/v1/core/kv_cache_coordinator.py
replay_boundaries(num_prompt_tokens, scheduler_block_size, use_eagle, num_hash_lookahead_tokens)
¶
Positions a later request replaying a prompt can resume at.
A hit is the shortest across all groups, so every group retains state
at each position; EAGLE groups also keep the block above, which they
match and drop back from (see reachable_block_mask).
Two positions are reachable: a resend of the identical prompt is capped
at num_tokens - 1 (its last token is recomputed for logits), a
longer sibling matches the final aligned block. They differ only on a
block-aligned prompt, where retaining just the higher one collapses the
resend's hit to 0. The alignment is the scheduler block size, not the
finer hash granularity, which would over-estimate the reach.
A lookahead hash exists only once the tokens after its block are known, so
a resend reaches at most num_tokens - num_hash_lookahead_tokens.