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
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 | |
complete_load ¶
complete_load(keys: Iterable[OffloadKey])
Marks previous blocks that were prepared to load as done loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Iterable[OffloadKey] | the keys identifying the blocks. | required |
complete_store ¶
Marks blocks which were previously prepared to be stored, as stored. Following this call, the blocks become loadable. If if_success is False, blocks that were not marked as stored will be removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys | Iterable[OffloadKey] | the keys identifying the blocks. | 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
prepare_load abstractmethod ¶
prepare_load(
keys: Sequence[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 | Sequence[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: Sequence[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 | Sequence[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
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. |
OffloadingSpec ¶
Bases: ABC
Spec for an offloading connector
Source code in vllm/v1/kv_offload/base.py
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.