vllm.v1.worker.extensible_kv_cache
¶
KV cache backing allocation that grows after warmup.
Classes:
-
ExtensibleKVCache–Growable backing allocation for the KV cache.
Functions:
-
ensure_kv_cache_blocks–Commit enough of an extensible KV cache to address num_blocks.
-
extend_kv_cache–Commit the final size once post-warmup memory is known.
-
kv_cache_export_handle_types–Handle types other processes import the KV cache through, for
-
kv_cache_lent_for_weight_update–Make room for a layerwise weight update of model within the context.
-
measure_kv_cache_blocks–Size an extensible KV cache from the memory free after warmup.
-
measure_kv_cache_bytes–Bytes the KV cache may occupy given the memory measured after warmup.
-
num_committable_kv_blocks–Blocks warmup may address: all of them, or, for an extensible cache,
-
pad_kv_cache_segments–Start every layer's region on a granule.
-
registers_kv_cache_memory–Whether a KV connector registers the committed KV cache for RDMA, which
ExtensibleKVCache
¶
Growable backing allocation for the KV cache.
Reserves address space for kv_cache_config.num_blocks and commits a
prefix of blocks on demand. Blocks keep fixed offsets, so views over the
reservation and captured CUDA graphs stay valid as more are committed. The
allocation splits into segments holding num_blocks blocks each: one per
layer for block-compact layer-outermost layouts (sized by that layer's
block stride, possibly padded apart by pad_kv_cache_segments), one
overall for block-outermost. Host-resident tensors live outside it.
Methods:
-
allocate–Allocator hook for allocate_kv_cache; commits the minimum.
-
blocks_within–Most blocks whose physical footprint fits in num_bytes.
-
commit–Back the first num_blocks blocks; new blocks are zeroed.
-
committable_blocks–Blocks that can be committed now without eating into the headroom.
-
committed_kv_cache_tensors–Tensor placements matching committed_views for num_blocks.
-
committed_views–Per-layer views whose storage spans exactly the committed blocks.
-
physical_bytes_for–Bytes a commit of num_blocks maps, each segment rounded to granules.
-
recommit–Map fresh zeroed pages for the blocks committed before release.
-
record_transient_peak–Fold the allocated transient since peak tracking last restarted
-
release_physical–Drop physical pages for sleep; addresses and views stay valid.
-
set_torch_memory_limit–Cap what torch's caching allocator reserves on this device, or lift
Attributes:
-
min_committed_blocks(int) –The null block, plus one under block-outermost layouts, whose views
-
physical_bytes(int) –Physically mapped bytes, including granule rounding.
min_committed_blocks
property
¶
The null block, plus one under block-outermost layouts, whose views
start inside block 0 and read past it (see pad_kv_cache_segments).
physical_bytes
property
¶
Physically mapped bytes, including granule rounding.
_locate(start)
¶
Segment index and offset within it of allocation byte start.
_segments(tensors, layout)
¶
Segment offsets and block strides covering the allocation.
Block-compact layer-outermost layouts give each layer a num_blocks-block region, which may be padded apart. Otherwise the segments are implied by the one block stride.
allocate(size)
¶
Allocator hook for allocate_kv_cache; commits the minimum.
blocks_within(num_bytes)
¶
Most blocks whose physical footprint fits in num_bytes.
commit(num_blocks, defragment=False, shrink=False)
¶
Back the first num_blocks blocks; new blocks are zeroed.
Grow-only unless shrink is set, which remaps a smaller prefix and discards contents. defragment remaps each segment as one driver allocation (discarding contents): UCX cannot RDMA a range spanning several allocations.
committable_blocks()
¶
Blocks that can be committed now without eating into the headroom.
Bounds what warmup may commit: free memory plus the committed prefix, less the sizing margin and the larger of reserved_headroom_bytes and a share of the headroom, never below what is already committed nor above the capacity.
committed_kv_cache_tensors(kv_cache_config, layout, num_blocks)
¶
Tensor placements matching committed_views for num_blocks.
size is the committed total, as every device tensor describes the one allocation; host-resident tensors are unchanged. Under block-outermost layouts the committed blocks are one prefix of it, so the capacity placements still hold. Under layer-outermost layouts each layer's view has its own storage over its committed prefix: describe it as a one-layer tensor placed at the start of that storage.
committed_views(kv_cache_config, layout, kernel_block_sizes=None)
¶
Per-layer views whose storage spans exactly the committed blocks.
kv_cache_config.num_blocks must equal the committed count. Connectors
derive extents from untyped_storage().nbytes(), so they must not see the
reservation. Host-resident layers are omitted. None for layouts whose
layers span several segments.
physical_bytes_for(num_blocks)
¶
Bytes a commit of num_blocks maps, each segment rounded to granules.
recommit()
¶
Map fresh zeroed pages for the blocks committed before release.
record_transient_peak()
¶
Fold the allocated transient since peak tracking last restarted into reserved_headroom_bytes; returns the allocator stats.
release_physical()
¶
Drop physical pages for sleep; addresses and views stay valid.
set_torch_memory_limit(num_bytes)
¶
Cap what torch's caching allocator reserves on this device, or lift the cap (None). At the cap it releases cached segments before growing, rather than taking memory other allocations need.
_release_for_weight_update(kv_cache, model, *, reclaimable, fail_if_short)
¶
Whether kv_cache was released to fit the update; see the caller.
ensure_kv_cache_blocks(runner, num_blocks)
¶
Commit enough of an extensible KV cache to address num_blocks.
extend_kv_cache(runner, kv_cache_config, num_blocks)
¶
Commit the final size once post-warmup memory is known.
Afterwards the runner matches one that allocated num_blocks up front: views are rebuilt over the committed bytes (same addresses) and the KV connector is created against final memory. kv_cache_config is the engine's config the cache was allocated from.
kv_cache_export_handle_types(vllm_config)
¶
Handle types other processes import the KV cache through, for
VmmDriver.create.
kv_cache_lent_for_weight_update(kv_cache, model, *, reclaimable, fail_if_short)
¶
Make room for a layerwise weight update of model within the context.
Only an extensible KV cache (kv_cache, None for a standard one) leaves the free memory short, since it keeps free just the transients warmup measured. When short, release the cache's physical memory for the context if it holds nothing to keep (reclaimable); else fail before any weight changes, when not even the largest layer fits and fail_if_short, or warn. Torch's memory cap is lifted for the context.
measure_kv_cache_blocks(kv_cache, vllm_config, device, *, init_free_memory, requested_memory)
¶
Size an extensible KV cache from the memory free after warmup.
Keeps free the larger of reserved_headroom_bytes (the profiled or warmup allocated peak) and torch_reserve_growth_bytes. Torch's cache is emptied first: its blocks may not fit the large workspaces the first real steps allocate. Then caps torch's caching allocator to leave KV_CACHE_NON_TORCH_RESERVE_BYTES free for allocations outside it.
measure_kv_cache_bytes(*, init_free_memory, free_memory, committed_bytes, requested_memory, transient_peak_bytes=0, torch_reserve_growth_bytes=0, margin_floor_bytes=KV_CACHE_MARGIN_FLOOR_BYTES, margin_fraction=KV_CACHE_MARGIN_FRACTION)
¶
Bytes the KV cache may occupy given the memory measured after warmup.
Everything resident except the committed KV prefix is needed by the engine;
the cache gets the rest of the budget, capped by the device headroom (free
memory plus the committed prefix) less a margin of
max(margin_floor_bytes, margin_fraction * transient_peak_bytes). The
larger of transient_peak_bytes and torch_reserve_growth_bytes is then kept
free in full. The margin guards physical memory, so it never reduces an
explicit budget that already leaves that much free.
num_committable_kv_blocks(runner)
¶
Blocks warmup may address: all of them, or, for an extensible cache, the count agreed across ranks so that every rank warms up the same shapes.
pad_kv_cache_segments(kv_cache_config, layout, granule, *, registers_memory=False)
¶
Start every layer's region on a granule.
Committing the null block then backs each layer's first granule, which TMA gathers with out-of-bounds indices touch beyond the rows they address. It also lets a defragmenting commit of any block count back each segment with one physical chunk, as RDMA registration (registers_memory) requires.
Only address space grows: commits already map whole granules per segment.
Block-outermost layouts (one segment at offset 0, see allocate) are
returned as is, and so are other layouts that are not block-compact, unless
registers_memory.
registers_kv_cache_memory(vllm_config)
¶
Whether a KV connector registers the committed KV cache for RDMA, which needs each segment backed by one physical chunk.