vllm.distributed.kv_transfer.kv_connector.v1.base
KVConnectorBase_V1 Class for Distributed KV Cache & Hidden State communication in vLLM v1
The class provides the following primitives
Scheduler-side: runs in the scheduler, binds metadata, which is used by the worker-side to load/save KV cache. get_num_new_matched_tokens() - get number of new tokens that exist in the remote KV cache. Might be called multiple times for a given request and should be side-effect free. update_state_after_alloc() - update KVConnector state after temporary buffer alloc by the CacheManager. request_finished() - called when a request is finished, with the computed kv cache blocks for the request. Returns whether KV cache should be freed now or will be freed asynchronously and optionally returns KV transfer params.
Worker-side: runs in each worker, loads/saves KV cache to/from the Connector based on the metadata. start_load_kv() - starts loading all KVs (maybe async) wait_for_layer_load() - blocks until layer i load is done
save_kv_layer() - starts saving KV for layer i (maybe async)
wait_for_save() - blocks until all saves are done
get_finished() - called with ids of finished requests, returns
ids of requests that have completed async sending/recving.
KVConnectorBase_V1
¶
Bases: ABC
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
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 |
|
__init__
¶
__init__(vllm_config: VllmConfig, role: KVConnectorRole)
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
_get_connector_metadata
¶
_get_connector_metadata() -> KVConnectorMetadata
Get the connector metadata.
This function should only be called inside the connector.
Returns:
Name | Type | Description |
---|---|---|
ConnectorMetadata |
KVConnectorMetadata
|
the connector metadata. |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
bind_connector_metadata
¶
bind_connector_metadata(
connector_metadata: KVConnectorMetadata,
) -> None
Set the connector metadata from the scheduler.
This function should be called by the model runner every time before the model execution. The metadata will be used for runtime KV cache loading and saving.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
connector_metadata
|
dict
|
the connector metadata. |
required |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
build_connector_meta
abstractmethod
¶
build_connector_meta(
scheduler_output: SchedulerOutput,
) -> KVConnectorMetadata
Build the connector metadata for this step.
This function should NOT modify fields in the scheduler_output. Also, calling this function will reset the state of the connector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheduler_output
|
SchedulerOutput
|
the scheduler output object. |
required |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
clear_connector_metadata
¶
Clear the connector metadata.
This function should be called by the model runner every time after the model execution.
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
get_finished
¶
Notifies worker-side connector ids of requests that have finished generating tokens.
Returns:
Type | Description |
---|---|
Optional[set[str]]
|
ids of requests that have finished asynchronous transfer |
Optional[set[str]]
|
(requests that previously returned True from request_finished()), |
tuple[Optional[set[str]], Optional[set[str]]]
|
tuple of (sending/saving ids, recving/loading ids). |
tuple[Optional[set[str]], Optional[set[str]]]
|
The finished saves/sends req ids must belong to a set provided in a |
tuple[Optional[set[str]], Optional[set[str]]]
|
call to this method (this call or a prior one). |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
get_num_new_matched_tokens
abstractmethod
¶
Get number of new tokens that can be loaded from the external KV cache beyond the num_computed_tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
the request object. |
required |
num_computed_tokens
|
int
|
the number of locally computed tokens for this request |
required |
Returns:
Type | Description |
---|---|
tuple[int, bool]
|
A tuple with the following elements:
- The number of tokens that can be loaded from the
external KV cache beyond what is already computed.
- |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
register_kv_caches
¶
Initialize with the KV caches. Useful for pre-registering the KV Caches in the KVConnector (e.g. for NIXL).
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
request_finished
¶
Called when a request has finished, before its blocks are freed.
Returns:
Type | Description |
---|---|
bool
|
True if the request is being saved/sent asynchronously and blocks |
Optional[dict[str, Any]]
|
should not be freed until the request_id is returned from |
tuple[bool, Optional[dict[str, Any]]]
|
get_finished(). |
tuple[bool, Optional[dict[str, Any]]]
|
Optional KVTransferParams to be included in the request outputs |
tuple[bool, Optional[dict[str, Any]]]
|
returned by the engine. |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
save_kv_layer
abstractmethod
¶
save_kv_layer(
layer_name: str,
kv_layer: Tensor,
attn_metadata: AttentionMetadata,
**kwargs,
) -> None
Start saving a layer of KV cache from vLLM's paged buffer to the connector. This is called from within attention layer to enable async copying during execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name
|
str
|
the name of the layer. |
required |
kv_layer
|
Tensor
|
the paged KV buffer of the current layer in vLLM. |
required |
attn_metadata
|
AttentionMetadata
|
the attention metadata. |
required |
**kwargs
|
additional arguments for the save operation. |
{}
|
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
start_load_kv
abstractmethod
¶
start_load_kv(
forward_context: ForwardContext, **kwargs
) -> None
Start loading the KV cache from the connector to vLLM's paged KV buffer. This is called from the forward context before the forward pass to enable async loading during model execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forward_context
|
ForwardContext
|
the forward context. |
required |
**kwargs
|
additional arguments for the load operation |
{}
|
Note
The number of elements in kv_caches and layer_names should be the same.
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
update_state_after_alloc
abstractmethod
¶
update_state_after_alloc(
request: Request,
blocks: KVCacheBlocks,
num_external_tokens: int,
)
Update KVConnector state after block allocation.
If get_num_new_matched_tokens previously returned True for a request, this function may be called twice for that same request - first when blocks are allocated for the connector tokens to be asynchronously loaded into, and second when any additional blocks are allocated, after the load/transfer is complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
the request object. |
required |
blocks
|
KVCacheBlocks
|
the blocks allocated for the request. |
required |
num_external_tokens
|
int
|
the number of tokens that will be loaded from the external KV cache. |
required |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
wait_for_layer_load
abstractmethod
¶
wait_for_layer_load(layer_name: str) -> None
Block until the KV for a specific layer is loaded into vLLM's paged buffer. This is called from within attention layer to ensure async copying from start_load_kv is complete.
This interface will be useful for layer-by-layer pipelining.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name
|
str
|
the name of that layer |
required |
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
wait_for_save
abstractmethod
¶
Block until all the save operations is done. This is called as the forward context exits to ensure that the async saving from save_kv_layer is complete before finishing the forward.
This prevents overwrites of paged KV buffer before saving done.
Source code in vllm/distributed/kv_transfer/kv_connector/v1/base.py
KVConnectorMetadata
¶
Abstract Metadata used to communicate between the Scheduler KVConnector and Worker KVConnector.