vllm.v1.core.kv_cache_coordinator
HybridKVCacheCoordinator
¶
Bases: KVCacheCoordinator
KV cache coordinator for hybrid models with multiple KV cache types, and
thus multiple kv cache groups.
To simplify find_longest_cache_hit
, it only supports the combination of
two types of KV cache groups, and one of them must be full attention.
May extend to more general cases in the future.
Source code in vllm/v1/core/kv_cache_coordinator.py
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 |
|
__init__
¶
__init__(
kv_cache_config: KVCacheConfig,
max_model_len: int,
use_eagle: bool,
enable_caching: bool,
caching_hash_fn: Callable,
enable_kv_cache_events: bool,
)
Source code in vllm/v1/core/kv_cache_coordinator.py
find_longest_cache_hit
¶
find_longest_cache_hit(
block_hashes: list[BlockHash], max_cache_hit_length: int
) -> tuple[tuple[list[KVCacheBlock], ...], int]
Find the longest cache hit for the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_hashes
|
list[BlockHash]
|
The block hashes of the request. |
required |
max_cache_hit_length
|
int
|
The maximum length of the cache hit. |
required |
Returns:
Type | Description |
---|---|
tuple[tuple[list[KVCacheBlock], ...], int]
|
A tuple containing: - A list of the cache hit blocks for each single type manager. - The number of tokens of the longest cache hit. |
Source code in vllm/v1/core/kv_cache_coordinator.py
verify_and_split_kv_cache_groups
¶
Verifies that the model has exactly two types of KV cache groups, and one of them is full attention. Then, split the kv cache groups into full attention groups and other groups.
Source code in vllm/v1/core/kv_cache_coordinator.py
KVCacheCoordinator
¶
Bases: ABC
Coordinate the KV cache of different KV cache groups.
Source code in vllm/v1/core/kv_cache_coordinator.py
14 15 16 17 18 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 |
|
block_pool
instance-attribute
¶
block_pool = BlockPool(
num_blocks, enable_caching, enable_kv_cache_events
)
single_type_managers
instance-attribute
¶
single_type_managers = tuple(
get_manager_for_kv_cache_spec(
kv_cache_spec=kv_cache_spec,
block_pool=block_pool,
kv_cache_group_id=i,
caching_hash_fn=caching_hash_fn,
)
for (i, kv_cache_group) in enumerate(kv_cache_groups)
)
__init__
¶
__init__(
kv_cache_config: KVCacheConfig,
max_model_len: int,
use_eagle: bool,
enable_caching: bool,
caching_hash_fn: Callable,
enable_kv_cache_events: bool,
)
Source code in vllm/v1/core/kv_cache_coordinator.py
allocate_new_blocks
¶
allocate_new_blocks(
request_id: str, num_tokens: int
) -> tuple[list[KVCacheBlock], ...]
Allocate new blocks for the request to give it at least num_tokens
token slots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
num_tokens
|
int
|
The total number of tokens that need a slot (including tokens that are already allocated). |
required |
Returns:
Type | Description |
---|---|
tuple[list[KVCacheBlock], ...]
|
The new allocated blocks. |
Source code in vllm/v1/core/kv_cache_coordinator.py
cache_blocks
¶
Cache the blocks for the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request. |
required |
block_hashes
|
list[BlockHash]
|
The block hashes of the request. |
required |
num_tokens
|
The total number of tokens that need to be cached (including tokens that are already cached). |
required |
Source code in vllm/v1/core/kv_cache_coordinator.py
find_longest_cache_hit
abstractmethod
¶
free
¶
free(request_id: str) -> None
Free the blocks for the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
get_blocks
¶
get_blocks(
request_id: str,
) -> tuple[list[KVCacheBlock], ...]
Get the blocks for the request.
get_num_blocks_to_allocate
¶
get_num_blocks_to_allocate(
request_id: str,
num_tokens: int,
new_computed_blocks: tuple[list[KVCacheBlock], ...],
) -> int
Get the number of blocks needed to be allocated for the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
num_tokens
|
int
|
The total number of tokens that need a slot (including tokens that are already allocated). |
required |
new_computed_blocks
|
tuple[list[KVCacheBlock], ...]
|
The new computed blocks just hitting the prefix caching. |
required |
Returns:
Type | Description |
---|---|
int
|
The number of blocks. |
Source code in vllm/v1/core/kv_cache_coordinator.py
get_num_common_prefix_blocks
¶
Get the number of common prefix blocks for a request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
block_hashes
|
The block hashes of the request. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
The number of common prefix blocks. |
Source code in vllm/v1/core/kv_cache_coordinator.py
remove_skipped_blocks
¶
Remove the blocks that are no longer needed from blocks
and replace
the removed blocks with null_block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
num_computed_tokens
|
int
|
The number of tokens that have been computed. |
required |
Source code in vllm/v1/core/kv_cache_coordinator.py
save_new_computed_blocks
¶
save_new_computed_blocks(
request_id: str,
new_computed_blocks: tuple[list[KVCacheBlock], ...],
) -> None
Add the new computed blocks to the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str
|
The request ID. |
required |
new_computed_blocks
|
tuple[list[KVCacheBlock], ...]
|
The new computed blocks just hitting the prefix cache. |
required |
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
__init__
¶
__init__(
kv_cache_config: KVCacheConfig,
max_model_len: int,
use_eagle: bool,
enable_caching: bool,
caching_hash_fn: Callable,
enable_kv_cache_events: bool,
)
Source code in vllm/v1/core/kv_cache_coordinator.py
find_longest_cache_hit
¶
find_longest_cache_hit(
block_hashes: list[BlockHash], max_cache_hit_length: int
) -> tuple[tuple[list[KVCacheBlock], ...], int]
Source code in vllm/v1/core/kv_cache_coordinator.py
get_kv_cache_coordinator
¶
get_kv_cache_coordinator(
kv_cache_config: KVCacheConfig,
max_model_len: int,
use_eagle: bool,
enable_caching: bool,
caching_hash_fn: Callable,
enable_kv_cache_events: bool,
) -> KVCacheCoordinator