vllm.v1.core.block_pool
BlockPool
¶
BlockPool that manages KVCacheBlocks. It provides methods to allocate, free and cache the kv cache blocks. The free_block_queue stores the free blocks in eviction order to enable allocation, free, and cache eviction. The cached_block_hash_to_block maps between block hash and cached block to support finding cached blocks by their block hash.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_gpu_blocks
|
int
|
The number of blocks in the pool. |
required |
enable_caching
|
bool
|
Whether to enable prefix caching. |
required |
enable_kv_cache_events
|
bool
|
Whether to enable kv cache events. |
False
|
Source code in vllm/v1/core/block_pool.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 |
|
blocks
instance-attribute
¶
blocks: list[KVCacheBlock] = [
KVCacheBlock(idx) for idx in range(num_gpu_blocks)
]
cached_block_hash_to_block
instance-attribute
¶
cached_block_hash_to_block: dict[
BlockHashWithGroupId, dict[int, KVCacheBlock]
] = defaultdict(dict)
__init__
¶
Source code in vllm/v1/core/block_pool.py
_maybe_evict_cached_block
¶
_maybe_evict_cached_block(block: KVCacheBlock) -> bool
If a block is cached in cached_block_hash_to_block
, we reset its hash
metadata and evict it from the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
KVCacheBlock
|
The block to evict. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the block is evicted, False otherwise. |
Source code in vllm/v1/core/block_pool.py
cache_full_blocks
¶
cache_full_blocks(
request: Request,
blocks: list[KVCacheBlock],
block_hashes: list[BlockHash],
num_cached_blocks: int,
num_full_blocks: int,
block_size: int,
kv_cache_group_id: int,
hash_fn: Callable,
) -> None
Cache a list of full blocks for prefix caching.
This function takes a list of blocks that will have their block hash
metadata to be updated and cached. Given a request, it computes the
block hashes for the blocks starting from num_cached_blocks
to
num_full_blocks
, updating the metadata for each block
and caching them in the cached_block_hash_to_block
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to cache the blocks. |
required |
blocks
|
list[KVCacheBlock]
|
All blocks in the request. |
required |
block_hashes
|
list[BlockHash]
|
Block hashes of the blocks in the request. Note that |
required |
num_cached_blocks
|
int
|
The number of blocks that are already cached. |
required |
num_full_blocks
|
int
|
The number of blocks that are full and should be cached after this function. |
required |
block_size
|
int
|
Number of tokens in each block. |
required |
kv_cache_group_id
|
int
|
The id of the KV cache group. |
required |
hash_fn
|
Callable
|
The hash function to use for block hashes. |
required |
Source code in vllm/v1/core/block_pool.py
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 |
|
free_blocks
¶
free_blocks(ordered_blocks: Iterable[KVCacheBlock]) -> None
Free a list of blocks. The blocks should be ordered by their eviction priority, where the first block will be evicted first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ordered_blocks
|
Iterable[KVCacheBlock]
|
A list of blocks to free ordered by their eviction priority. |
required |
Source code in vllm/v1/core/block_pool.py
get_cached_block
¶
get_cached_block(
block_hash: BlockHash, kv_cache_group_ids: list[int]
) -> Optional[list[KVCacheBlock]]
Get the cached block by the block hash for each group in
kv_cache_group_ids
, or None if cache miss for any group.
If there are duplicated blocks, we return the first block in the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_hash
|
BlockHash
|
The hash value of the block. |
required |
kv_cache_group_ids
|
list[int]
|
The ids of the KV cache groups. |
required |
Returns:
Type | Description |
---|---|
Optional[list[KVCacheBlock]]
|
The cached blocks if exists, or None. |
Source code in vllm/v1/core/block_pool.py
get_new_blocks
¶
get_new_blocks(num_blocks: int) -> list[KVCacheBlock]
Get new blocks from the free block pool.
Note that we do not check block cache in this function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_blocks
|
int
|
The number of blocks to allocate. |
required |
Returns:
Type | Description |
---|---|
list[KVCacheBlock]
|
A list of new block. |
Source code in vllm/v1/core/block_pool.py
get_num_free_blocks
¶
get_num_free_blocks() -> int
get_usage
¶
get_usage() -> float
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/v1/core/block_pool.py
take_events
¶
take_events() -> list[KVCacheEvent]
Atomically takes all events and clears the queue.
Returns:
Type | Description |
---|---|
list[KVCacheEvent]
|
A list of KV cache events. |
Source code in vllm/v1/core/block_pool.py
touch
¶
touch(blocks: tuple[list[KVCacheBlock], ...]) -> None
Touch a block increases its reference count by 1, and may remove the block from the free queue. This is used when a block is hit by another request with the same prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks
|
tuple[list[KVCacheBlock], ...]
|
A list of blocks to touch. |
required |