vllm.v1.kv_offload.base ¶
Core abstractions for KV cache offloading in vLLM v1.
BlockIDsLoadStoreSpec ¶
Bases: LoadStoreSpec, ABC
Spec for loading/storing KV blocks from given block numbers.
Source code in vllm/v1/kv_offload/base.py
CanonicalKVCacheRef dataclass ¶
Per-layer (or group of layers) reference to a specific (by index) CanonicalKVCacheTensor and records the un-padded page size used by that layer.
Source code in vllm/v1/kv_offload/base.py
CanonicalKVCacheTensor dataclass ¶
A canonicalized KV cache tensor whose first dimension is num_blocks.
For attention backends where the raw tensor has num_blocks at a non-leading physical dimension (e.g. FlashAttention's (2, num_blocks, ...) layout), the tensor is split so that each resulting CanonicalKVCacheTensor starts with (num_blocks, ...).
Source code in vllm/v1/kv_offload/base.py
CanonicalKVCaches dataclass ¶
Canonicalized block-level representation of the KV caches.
Composed of
- Unique list of KV cache data tensors, each with shape (num_blocks, page_size_in_bytes) and int8 dtype.
- Per-group data references of the tensors. i.e. how each KV cache group maps to the tensors.
Source code in vllm/v1/kv_offload/base.py
GPULoadStoreSpec ¶
Bases: BlockIDsLoadStoreSpec
Spec for loading/storing a KV block to GPU memory.
If there are multiple KV groups, the blocks are expected to be ordered by the group index. In that case, group_sizes[i] determines the number of blocks per the i-th KV group, and thus sum(group_sizes) == len(block_ids). group_sizes=None indicates a single KV group.
If block_indices is given, each group (determined by group_sizes) of block IDs will correspond to logically contiguous blocks, e.g. blocks 5-10 of a some request. block_indices[i] will represent the block index of the first block in group #i. Thus, len(block_indices) == len(group_sizes) = number of KV cache groups. This information is required in order to support off/loading from offloaded blocks which are larger than GPU blocks. In such cases, the first GPU block per each group may be unaligned to the offloaded block size, and so knowing block_indices[i] allows the worker to correctly skip part of the first matching offloaded block.
Source code in vllm/v1/kv_offload/base.py
LoadStoreSpec ¶
OffloadingManager ¶
Bases: ABC
Source code in vllm/v1/kv_offload/base.py
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 | |
complete_load ¶
complete_load(
keys: Collection[OffloadKey], req_context: ReqContext
)
Marks previous blocks that were prepared to load as done loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Collection[OffloadKey] | the keys identifying the blocks. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
Source code in vllm/v1/kv_offload/base.py
complete_store ¶
complete_store(
keys: Collection[OffloadKey],
req_context: ReqContext,
success: bool = True,
)
Marks blocks which were previously prepared to be stored, as stored. Following this call, the blocks become loadable. If success is False, blocks that were not marked as stored will be removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Collection[OffloadKey] | the keys identifying the blocks. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
success | bool | whether the blocks were stored successfully. | True |
Source code in vllm/v1/kv_offload/base.py
lookup abstractmethod ¶
lookup(
key: OffloadKey, req_context: ReqContext
) -> bool | None
Checks whether a single block is offloaded and ready to be read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | OffloadKey | the key identifying the block to lookup. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
Returns:
| Type | Description |
|---|---|
bool | None | True if the block is offloaded and ready, False if not, |
bool | None | or None if the lookup should be retried later. |
bool | None | Returning None will delay the request handling by the vLLM |
bool | None | scheduler. |
Source code in vllm/v1/kv_offload/base.py
on_new_request abstractmethod ¶
Called when a new request is first seen by the scheduler.
Returns a RequestOffloadingContext indicating how this request's blocks should be offloaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_context | ReqContext | per-request context. | required |
Source code in vllm/v1/kv_offload/base.py
on_request_finished ¶
Called when a request has finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_context | ReqContext | per-request context. | required |
prepare_load abstractmethod ¶
prepare_load(
keys: Collection[OffloadKey], req_context: ReqContext
) -> LoadStoreSpec
Prepare the given blocks to be read. The given blocks will be protected from eviction until complete_load is called. It assumes all given blocks are offloaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Collection[OffloadKey] | the keys identifying the blocks. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
Returns:
| Type | Description |
|---|---|
LoadStoreSpec | A LoadStoreSpec that can be used by a worker to locate and load |
LoadStoreSpec | the actual offloaded KV data. |
Source code in vllm/v1/kv_offload/base.py
prepare_store abstractmethod ¶
prepare_store(
keys: Collection[OffloadKey], req_context: ReqContext
) -> PrepareStoreOutput | None
Prepare the given blocks to be offloaded. The given blocks will be protected from eviction until complete_store is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Collection[OffloadKey] | the keys identifying the blocks. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
Returns:
| Type | Description |
|---|---|
PrepareStoreOutput | None | A PrepareStoreOutput indicating which blocks need storing, |
PrepareStoreOutput | None | where to store them (LoadStoreSpec), and list of blocks that |
PrepareStoreOutput | None | were evicted as a result. |
PrepareStoreOutput | None | None is returned if the blocks cannot be stored. |
Source code in vllm/v1/kv_offload/base.py
reset_cache ¶
shutdown ¶
take_events ¶
take_events() -> Iterable[OffloadingEvent]
Take the offloading events from the manager.
Yields:
| Type | Description |
|---|---|
Iterable[OffloadingEvent] | New OffloadingEvents collected since the last call. |
touch ¶
touch(
keys: Collection[OffloadKey], req_context: ReqContext
)
Mark the given blocks as recently used. This could in practice mean moving them to the end of an LRU list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Collection[OffloadKey] | the keys identifying the blocks. | required |
req_context | ReqContext | per-request context (e.g. kv_transfer_params). | required |
Source code in vllm/v1/kv_offload/base.py
OffloadingSpec ¶
Bases: ABC
Spec for an offloading connector
Source code in vllm/v1/kv_offload/base.py
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 | |
get_handlers abstractmethod ¶
get_handlers(
kv_caches: CanonicalKVCaches,
) -> Iterator[
tuple[
type[LoadStoreSpec],
type[LoadStoreSpec],
OffloadingHandler,
]
]
Get offloading handlers along with their respective src and dst types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kv_caches | CanonicalKVCaches | Canonicalized KV caches. | required |
Yields:
| Type | Description |
|---|---|
tuple[type[LoadStoreSpec], type[LoadStoreSpec], OffloadingHandler] | Tuples of (src_type, dst_type, offloading_handler). |
Source code in vllm/v1/kv_offload/base.py
get_manager abstractmethod ¶
get_manager() -> OffloadingManager
Get an OffloadingManager that will be used by the scheduler-side offloading connector to track offloaded blocks and manage evictions.
make_offload_key ¶
Pack a block hash and group index into an OffloadKey.