vllm.core.block.prefix_caching_block
Token blocks.
BlockTracker
¶
Used to track the status of a block inside the prefix caching allocator
Source code in vllm/core/block/prefix_caching_block.py
ComputedBlocksTracker
¶
Tracks the computed blocks for each sequence.
Internally, it maintains a map from sequence id to the list of block hashes for the sequence. We cache the hashes of the full blocks for each sequence, and make sure the hash is calculated in the same way as the allocator. When a sequence is being decoded, we also update the sequence's hash accordingly and incrementally.
From the sequence hash, with prefix caching enabled, we could also calculate the number of cached tokens for the sequence by looking up the number of cached block hashes in the allocator.
Source code in vllm/core/block/prefix_caching_block.py
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 |
|
_seq_id_to_num_tokens_computed
instance-attribute
¶
__init__
¶
__init__(
allocator: DeviceAwareBlockAllocator,
block_size: int,
enable_caching: bool,
)
Source code in vllm/core/block/prefix_caching_block.py
_update_seq_hashes
¶
_update_seq_hashes(seq: Sequence) -> None
Incrementally update the sequence's block hashes and record them.
Source code in vllm/core/block/prefix_caching_block.py
get_num_cached_tokens
¶
Source code in vllm/core/block/prefix_caching_block.py
remove_seq
¶
remove_seq(seq_id: int) -> None
Stop tracking the sequence.
Source code in vllm/core/block/prefix_caching_block.py
LastAccessBlocksTracker
¶
Manages the last access time of the tracked sequences, in order to allow an efficient update of allocator's block last access times
Source code in vllm/core/block/prefix_caching_block.py
PrefixCachingBlock
¶
Bases: Block
A block implementation that supports prefix caching.
The PrefixCachingBlock class represents a block of token IDs with prefix caching capabilities. It wraps a NaiveBlock internally and provides additional functionality for content hashing and promoting immutable blocks with the prefix caching allocator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Optional[PrefixCachingBlock]
|
The previous block in the sequence. |
required |
token_ids
|
List[int]
|
The initial token IDs to be stored in the block. |
required |
block_size
|
int
|
The maximum number of token IDs that can be stored in the block. |
required |
allocator
|
BlockAllocator
|
The prefix caching block allocator associated with this block. |
required |
block_id
|
Optional[int]
|
The physical block index of this block. Defaults to None. |
None
|
extra_hash
|
Optional[int]
|
The hash value of additional factors such as adapters that influence the block, apart from the token_ids. |
None
|
Source code in vllm/core/block/prefix_caching_block.py
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 |
|
_block
instance-attribute
¶
_block = NaiveBlock(
prev_block=prev_block,
token_ids=token_ids,
block_size=block_size,
block_id=block_id,
allocator=_allocator,
)
content_hash
property
¶
Return the content-based hash of the current block, or None if it is not yet defined.
For the content-based hash to be defined, the current block must be full.
__init__
¶
__init__(
prev_block: Optional[Block],
token_ids: List[int],
block_size: int,
allocator: BlockAllocator,
block_id: Optional[int] = None,
computed: bool = False,
extra_hash: Optional[int] = None,
)
Source code in vllm/core/block/prefix_caching_block.py
_update_num_tokens_total
¶
Incrementally computes the number of tokens that there is till the current block (included)
Source code in vllm/core/block/prefix_caching_block.py
append_token_ids
¶
Appends the given token IDs to the block and registers the block as immutable if the block becomes full.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_ids
|
List[int]
|
The token IDs to be appended to the block. |
required |
Source code in vllm/core/block/prefix_caching_block.py
hash_block_tokens
classmethod
¶
hash_block_tokens(
is_first_block: bool,
prev_block_hash: Optional[int],
cur_block_token_ids: List[int],
extra_hash: Optional[int] = None,
) -> int
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.
- is_first_block (bool): A flag indicating if the block is the first in the sequence.
- prev_block_hash (Optional[int]): The hash of the previous block. None if this is the first block.
- cur_block_token_ids (List[int]): A list of token ids in the current block. The current block is assumed to be full.
- extra_hash (Optional[int]): The hash value of additional factors such as adapters that influence the block, apart from the token_ids.
Returns: - int: The computed hash value for the block.
Source code in vllm/core/block/prefix_caching_block.py
PrefixCachingBlockAllocator
¶
Bases: BlockAllocator
A block allocator that implements prefix caching.
The PrefixCachingBlockAllocator maintains a cache of blocks based on their content hash. It reuses blocks with the same content hash to avoid redundant memory allocation. The allocator also supports copy-on-write operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_blocks
|
int
|
The total number of blocks to manage. |
required |
block_size
|
int
|
The size of each block in tokens. |
required |
block_ids(Optional[Iterable[int]],
|
optional
|
An optional iterable of block IDs. If not provided, block IDs will be assigned sequentially from 0 to num_blocks - 1. |
required |
Source code in vllm/core/block/prefix_caching_block.py
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 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 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 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 |
|
_block_pool
instance-attribute
¶
_block_pool = BlockPool(
_block_size,
_create_block,
self,
num_blocks * extra_factor,
)
_hashless_allocator
instance-attribute
¶
_hashless_allocator = NaiveBlockAllocator(
create_block=_create_block,
num_blocks=num_blocks,
block_size=block_size,
block_ids=block_ids,
block_pool=_block_pool,
)
__init__
¶
__init__(
num_blocks: int,
block_size: int,
block_ids: Optional[Iterable[int]] = None,
eviction_policy: EvictionPolicy = LRU,
)
Source code in vllm/core/block/prefix_caching_block.py
_allocate_block_id
¶
_allocate_block_id() -> BlockId
First tries to allocate a block id from the hashless allocator, and if there are no blocks, then tries to evict an unused cached block.
Source code in vllm/core/block/prefix_caching_block.py
_create_block
¶
_create_block(
prev_block: Optional[Block],
token_ids: List[int],
block_size: int,
allocator: BlockAllocator,
block_id: Optional[int] = None,
computed: bool = False,
extra_hash: Optional[int] = None,
) -> Block
Source code in vllm/core/block/prefix_caching_block.py
_decr_refcount_cached_block
¶
_decr_refcount_cached_block(block: Block) -> None
Source code in vllm/core/block/prefix_caching_block.py
_decr_refcount_hashless_block
¶
_decr_refcount_hashless_block(block: Block) -> None
Source code in vllm/core/block/prefix_caching_block.py
_free_block_id
¶
_free_block_id(block: Block) -> None
Decrements the refcount of the block. The block may be in two possible states: (1) immutable/cached or (2) mutable/hashless. In the first case, the refcount is decremented directly and the block may be possibly added to the evictor. In other case, hashless allocator free(..) with keep_block_object=True is called to only free the block id (since the block object may be reused by the caller)
Source code in vllm/core/block/prefix_caching_block.py
_incr_refcount_cached_block
¶
_incr_refcount_cached_block(block: Block) -> None
Source code in vllm/core/block/prefix_caching_block.py
_maybe_allocate_evicted_block_id
¶
Source code in vllm/core/block/prefix_caching_block.py
_maybe_allocate_hashless_block_id
¶
Source code in vllm/core/block/prefix_caching_block.py
_track_block_id
¶
_untrack_block_id
¶
allocate_immutable_block
¶
allocate_immutable_block(
prev_block: Optional[Block],
token_ids: List[int],
extra_hash: Optional[int] = None,
device: Optional[Device] = None,
) -> Block
Allocates an immutable block with the given token IDs, reusing cached blocks if possible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Optional[Block]
|
The previous block in the sequence. |
required |
token_ids
|
List[int]
|
The token IDs to be stored in the block. |
required |
Returns:
Name | Type | Description |
---|---|---|
Block |
Block
|
The allocated immutable block. |
Source code in vllm/core/block/prefix_caching_block.py
allocate_immutable_blocks
¶
allocate_immutable_blocks(
prev_block: Optional[Block],
block_token_ids: List[List[int]],
extra_hash: Optional[int] = None,
device: Optional[Device] = None,
) -> List[Block]
Source code in vllm/core/block/prefix_caching_block.py
allocate_mutable_block
¶
allocate_mutable_block(
prev_block: Optional[Block],
extra_hash: Optional[int] = None,
device: Optional[Device] = None,
) -> Block
Allocates a mutable block. If there are no free blocks, this will evict unused cached blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Block
|
The previous block in the sequence. None is not allowed unlike it is super class. |
required |
Returns:
Name | Type | Description |
---|---|---|
Block |
Block
|
The allocated mutable block. |
Source code in vllm/core/block/prefix_caching_block.py
block_is_computed
¶
clear_copy_on_writes
¶
cow_block_if_not_appendable
¶
Performs a copy-on-write operation on the given block if it is not appendable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
Block
|
The block to check for copy-on-write. |
required |
Returns:
Name | Type | Description |
---|---|---|
BlockId |
BlockId
|
The block index of the new block if a copy-on-write operation was performed, or the original block index if no copy-on-write was necessary. |
Source code in vllm/core/block/prefix_caching_block.py
find_cached_blocks_prefix
¶
Given a list of block hashes, return the prefix of the block hashes that are all cached.
Since a block's block hash includes the hashes of all previous blocks, and we only allocate/deallocate blocks in the entire sequence, so if a block is cached, then all previous blocks are also cached. With this property, we can use binary search to find the prefix of cached blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_hashes
|
List[int]
|
The list of block hashes. |
required |
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: The prefix of the |
Source code in vllm/core/block/prefix_caching_block.py
fork
¶
Creates a new sequence of blocks that shares the same underlying memory as the original sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
last_block
|
Block
|
The last block in the original sequence. |
required |
Returns:
Type | Description |
---|---|
List[Block]
|
List[Block]: The new sequence of blocks that shares the same memory as the original sequence. |
Source code in vllm/core/block/prefix_caching_block.py
free
¶
Release the block (look at free_block_id(..) docs)
Source code in vllm/core/block/prefix_caching_block.py
get_common_computed_block_ids
¶
Return the block ids that are common for a given sequence group.
Only those blocks that are immutable and already be marked compyted would be taken consideration.
Source code in vllm/core/block/prefix_caching_block.py
get_num_free_blocks
¶
Source code in vllm/core/block/prefix_caching_block.py
get_num_full_blocks_touched
¶
Returns the number of full blocks that will be touched by swapping in/out.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks
|
List[Block]
|
List of blocks to be swapped. |
required |
Returns: int: the number of full blocks that will be touched by swapping in/out the given blocks. Non full blocks are ignored when deciding the number of blocks to touch.
Source code in vllm/core/block/prefix_caching_block.py
get_physical_block_id
¶
Returns the zero-offset block id on certain block allocator given the absolute block id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
absolute_id
|
int
|
The absolute block id for the block in whole allocator. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The rzero-offset block id on certain device. |
Source code in vllm/core/block/prefix_caching_block.py
is_block_cached
¶
mark_blocks_as_accessed
¶
Mark blocks as accessed, used in prefix caching.
If the block is added into evictor, we need to update corresponding info in evictor's metadata.
Source code in vllm/core/block/prefix_caching_block.py
mark_blocks_as_computed
¶
promote_to_immutable_block
¶
Once a mutable block is full, it can be promoted to an immutable block. This means that its content can be referenced by future blocks having the same prefix.
Note that if we already have a cached block with the same content, we will replace the newly-promoted block's mapping with the existing cached block id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
Block
|
The mutable block to be promoted. |
required |
Returns:
Name | Type | Description |
---|---|---|
BlockId |
BlockId
|
Either the original block index, or the block index of the previously cached block matching the same content. |
Source code in vllm/core/block/prefix_caching_block.py
reset_prefix_cache
¶
reset_prefix_cache() -> bool
Reset prefix cache. This function may be used in RLHF flows to invalid 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/core/block/prefix_caching_block.py
swap_in
¶
Execute the swap in actions. Change the block id from old allocator to current allocator for each block to finish the block table update.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks
|
List[Block]
|
List of blocks to be swapped in. |
required |