vllm.v1.kv_offload.base ¶
Core abstractions for KV cache offloading in vLLM v1.
Classes:
-
BlockIDsLoadStoreSpec–Spec for loading/storing KV blocks from given block numbers.
-
CanonicalKVCacheRef–Per-layer (or group of layers) reference to a specific (by index)
-
CanonicalKVCacheTensor–A canonicalized KV cache tensor whose first dimension is num_blocks.
-
CanonicalKVCaches–Canonicalized block-level representation of the KV caches.
-
GPULoadStoreSpec–Spec for loading/storing a KV block to GPU memory.
-
LoadStoreSpec–Abstract metadata that encapsulates information allowing a worker
-
LookupResult–Result of OffloadingManager.lookup().
-
OffloadingManager– -
OffloadingSpec–Spec for an offloading connector
-
OffloadingWorker–Runs in the worker process. Performs async KV transfers for ONE
Functions:
-
get_offload_block_hash–Extract the block hash from an
OffloadKey. -
get_offload_group_idx–Extract the group index from an
OffloadKey. -
make_offload_key–Pack a block hash and group index into an
OffloadKey.
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 ¶
LookupResult ¶
OffloadingManager ¶
Bases: ABC
Methods:
-
complete_load–Marks previous blocks that were prepared to load as done loading.
-
complete_store–Marks blocks which were previously prepared to be stored, as stored.
-
get_stats–Return collected metrics since last call, or None if disabled.
-
has_pending_work–Whether this manager needs the engine to keep stepping.
-
lookup–Checks whether a single block is offloaded and ready to be read.
-
on_new_request–Called when a new request is first seen by the scheduler.
-
on_request_finished–Called when a request has finished.
-
on_schedule_end–Called once at the end of each scheduler step.
-
prepare_load–Prepare the given blocks to be read.
-
prepare_store–Prepare the given blocks to be offloaded.
-
reset_cache–Evict all tracked blocks and reset internal state.
-
shutdown–Shutdown the manager and release any resources.
-
take_events–Take the offloading events from the manager.
-
touch–Mark the given blocks as recently used.
Source code in vllm/v1/kv_offload/base.py
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 | |
complete_load(keys, req_context) ¶
Marks previous blocks that were prepared to load as done loading.
Parameters:
-
(keys¶Collection[OffloadKey]) –the keys identifying the blocks.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
Source code in vllm/v1/kv_offload/base.py
complete_store(keys, req_context, success=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:
-
(keys¶Collection[OffloadKey]) –the keys identifying the blocks.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
-
(success¶bool, default:True) –whether the blocks were stored successfully.
Source code in vllm/v1/kv_offload/base.py
get_stats() ¶
has_pending_work() ¶
Whether this manager needs the engine to keep stepping.
While True, on_schedule_end() and get_finished_jobs() continue to be called even when no requests are scheduled.
lookup(key, req_context) abstractmethod ¶
Checks whether a single block is offloaded and ready to be read.
Parameters:
-
(key¶OffloadKey) –the key identifying the block to lookup.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
Returns:
-
LookupResult–HIT if the block is offloaded and ready, MISS if not found,
-
LookupResult–HIT_PENDING if found but not yet readable, or RETRY if the
-
LookupResult–lookup should be retried later.
Source code in vllm/v1/kv_offload/base.py
on_new_request(req_context) 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:
-
(req_context¶ReqContext) –per-request context.
Source code in vllm/v1/kv_offload/base.py
on_request_finished(req_context) ¶
Called when a request has finished.
By the time this is called, the scheduler will issue no more submit-side calls for this request, such as prepare_store() and prepare_load(). Completion callbacks for already-submitted transfers (complete_store() and complete_load()) may still arrive afterward.
This hook does NOT imply the data has been persisted. Asynchronous transfers already submitted for this request may still be in flight. Managers that cascade to lower tiers should delay those tiers' on_request_finished() calls until no more lower-tier submit calls can be issued for this request.
Parameters:
-
(req_context¶ReqContext) –per-request context.
Source code in vllm/v1/kv_offload/base.py
on_schedule_end() ¶
Called once at the end of each scheduler step.
Managers may override this to flush deferred work accumulated during the step (e.g., batched promotions).
prepare_load(keys, req_context) abstractmethod ¶
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:
-
(keys¶Collection[OffloadKey]) –the keys identifying the blocks.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
Returns:
-
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(keys, req_context) abstractmethod ¶
Prepare the given blocks to be offloaded. The given blocks will be protected from eviction until complete_store is called.
Parameters:
-
(keys¶Collection[OffloadKey]) –the keys identifying the blocks.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
Returns:
-
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 the offloading events from the manager.
Yields:
-
Iterable[OffloadingEvent]–New OffloadingEvents collected since the last call.
touch(keys, req_context) ¶
Mark the given blocks as recently used. This could in practice mean moving them to the end of an LRU list.
Parameters:
-
(keys¶Collection[OffloadKey]) –the keys identifying the blocks.
-
(req_context¶ReqContext) –per-request context (e.g. kv_transfer_params).
Source code in vllm/v1/kv_offload/base.py
OffloadingSpec ¶
Bases: ABC
Spec for an offloading connector
Methods:
-
build_metric_definitions–Return Prometheus metric definitions emitted by this spec.
-
get_manager–Get an OffloadingManager that will be used
-
get_worker–Get an OffloadingWorker that handles async KV transfers for this spec.
Source code in vllm/v1/kv_offload/base.py
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
build_metric_definitions(extra_config) classmethod ¶
Return Prometheus metric definitions emitted by this spec.
get_manager() abstractmethod ¶
Get an OffloadingManager that will be used by the scheduler-side offloading connector to track offloaded blocks and manage evictions.
get_worker(kv_caches) abstractmethod ¶
Get an OffloadingWorker that handles async KV transfers for this spec.
Parameters:
-
(kv_caches¶CanonicalKVCaches) –Canonicalized KV caches.
Returns:
-
OffloadingWorker–An OffloadingWorker instance for this medium.
Source code in vllm/v1/kv_offload/base.py
OffloadingWorker ¶
Bases: ABC
Runs in the worker process. Performs async KV transfers for ONE offloaded medium (e.g. CPU). Direction is explicit via submit_store / submit_load, so there is no (src_medium, dst_medium) routing.
Methods:
-
submit_load–Async offloaded medium -> GPU.
-
submit_store–Async GPU -> offloaded medium.
Source code in vllm/v1/kv_offload/base.py
get_offload_block_hash(key) ¶
get_offload_group_idx(key) ¶
make_offload_key(block_hash, group_idx) ¶
Pack a block hash and group index into an OffloadKey.