vllm.v1.kv_cache_interface
¶
Classes:
-
AttentionSpec– -
HiSparseHotPageSpec–Hot-cache pages, which kernels read as flat rows.
-
HiSparsePageSpec–Raw pages HiSparse lays out itself (hot and GPU-resident caches).
-
HiddenStateCacheSpec–Marker for hidden-state cache layers used by extract_hidden_states.
-
KVCacheConfig–The KV cache configuration of a model.
-
KVCacheGroup–Represents a group of model layers that share the same KV cache block table.
-
KVCacheLayerConfig–What one layer needs from the KV cache: the manager that allocates its
-
KVCacheSpec–A base class for specifying the KV cache format of one layer.
-
KVCacheStage–The KV cache of one pipeline stage.
-
KVCacheTensor–A class for specifying how the workers should initialize the KV cache.
-
KVQuantMode–KV cache quantization mode.
-
MambaSpec– -
ManagerConfig–Allocation policy of a layer: the
SingleTypeKVCacheManagerthat -
PrefixLookupGroup–KV cache groups looked up jointly for cache hits: they share a manager
Functions:
-
compute_layer_kv_cache_shape_bytes–Return the 4D logical shape
(B, H, N, C)where C is in bytes. -
compute_layout_strides–Byte strides in logical
[L, B, H, N, C]axis order. -
create_kv_cache_views–View a flat int8 buffer as one 4D
[B, H, N, C]view per layer. -
get_block_table_width–Return the width after optional alignment.
-
get_kv_quant_mode–Map a
kv_cache_dtypestring to aKVQuantMode. -
get_reusable_prefix_length–Bound prefix reuse by known tokens, reserving logits and hash lookahead.
-
group_kernel_blocks–View a kernel-block-granular layer cache with manager blocks as dim 0.
-
is_mamba_prefill_checkpoint_valid–Whether a backend can export the checkpoint in this query.
-
kv_cache_uses_per_token_head_scales–Return True if kv_cache_dtype needs per-token-head scales.
-
prefix_lookup_groups–Partition the prefix-cacheable groups into joint cache-hit lookups.
AttentionSpec
dataclass
¶
Bases: KVCacheSpec
Attributes:
-
cache_role(SparseCacheRole | None) –Role in a sparse-attention index group; None for dense attention.
-
kv_head_major_slots(bool) –Whether
num_head_slotsslots are grouped by KV head (H2ND: head -
max_tp_shards(int | None) –Distinct shards this cache splits into across TP; TP ranks beyond this
-
num_head_slots(int | None) –H of the logical
[B, H, N, C]page when packing diverges from one -
real_page_size_bytes(int) –Alias of
unpadded_page_size_bytes -
state_content_bytes(int | None) –C in bytes when packed; None means dense K/V content.
-
state_content_size_bytes(int) –Bytes per (head slot, stored state) cell of the page.
-
tokens_per_state(int | Fraction) –Tokens covered by one stored state. Ints > 1 compress multiple tokens
-
topk_buffer_width(int | None) –Width of the model's sparse top-k indices buffer, including padding.
cache_role = None
class-attribute
instance-attribute
¶
Role in a sparse-attention index group; None for dense attention.
kv_head_major_slots = False
class-attribute
instance-attribute
¶
Whether num_head_slots slots are grouped by KV head (H2ND: head
h's K and V slots are adjacent), so every KV head is one contiguous run
of the page and the page re-shards across TP by byte offset.
max_tp_shards = None
class-attribute
instance-attribute
¶
Distinct shards this cache splits into across TP; TP ranks beyond this hold replicas. None: unknown, treated as not replicated.
num_head_slots = None
class-attribute
instance-attribute
¶
H of the logical [B, H, N, C] page when packing diverges from one
slot per KV head. None means one slot per KV head. Published by the backend.
real_page_size_bytes
property
¶
Alias of unpadded_page_size_bytes
TODO(lucas): follow up with TPU backend to see if we can remove this property.
state_content_bytes = None
class-attribute
instance-attribute
¶
C in bytes when packed; None means dense K/V content.
state_content_size_bytes
property
¶
Bytes per (head slot, stored state) cell of the page.
tokens_per_state = 1
class-attribute
instance-attribute
¶
Tokens covered by one stored state. Ints > 1 compress multiple tokens
into one state (DSv4 sparse MLA); fractions < 1 store multiple states per
token (Whisper block pooling: Fraction(1, block_pool_size)).
topk_buffer_width = None
class-attribute
instance-attribute
¶
Width of the model's sparse top-k indices buffer, including padding.
HiSparseHotPageSpec
dataclass
¶
HiSparsePageSpec
dataclass
¶
HiddenStateCacheSpec
dataclass
¶
Bases: MLAAttentionSpec
Marker for hidden-state cache layers used by extract_hidden_states.
KVCacheConfig
dataclass
¶
The KV cache configuration of a model.
Methods:
-
num_blocks_of–Number of blocks addressable by the pool backing
tensor. -
select_transfer_block_ids–Select block IDs for externally transferable cache groups.
Attributes:
-
cache_hit_alignment_tokens(int | None) –Token granularity of prefix-cache hits, resolved by the engine core.
-
has_mixed_precision_kv_cache(bool) –Whether device attention caches use more than one precision.
-
hash_block_size(int | None) –Tokens per prefix-cache block hash, resolved by the engine core.
-
hisparse_host_block_stride(int | None) –Physical bytes between consecutive HiSparse host blocks.
-
hisparse_host_num_blocks(int | None) –Capacity of the dedicated HiSparse host-block manager, when enabled.
-
hisparse_shared_host_pool(bool) –Whether local TP ranks share one physical HiSparse host pool.
-
kv_cache_groups(list[KVCacheGroup]) –The kv cache groups of the model.
-
kv_cache_layout(str | None) –The KV cache layout resolved by the engine core, adopted by all workers.
-
kv_cache_tensors(list[KVCacheTensor]) –How should model runner initialize the KV cache tensors for each layer
-
kv_tp_replicas(int) –Consecutive TP ranks holding identical KV for every layer (1: none).
-
needs_kv_cache_zeroing(bool) –Whether newly allocated attention blocks must be zeroed before use.
-
num_blocks(int) –The number of KV cache blocks
-
pipeline_stages(tuple[KVCacheStage, ...]) –The KV cache of every pipeline stage, by PP rank, resolved by the engine
-
prefix_cache_retention_interval(int | None) –Resolved retention policy for local prefix-cache checkpoints.
-
prefix_cacheable_group_ids(tuple[int, ...]) –IDs of transferable groups eligible for hash-addressed stores.
-
prefix_cacheable_groups(tuple[KVCacheGroup, ...]) –Transferable groups eligible for hash-addressed stores.
-
transfer_group_ids(tuple[int, ...]) –IDs of cache groups that participate in external KV transfer.
-
transfer_group_index_by_layer(dict[str, int]) –Transfer-group tuple index for each participating layer.
-
transfer_groups(tuple[KVCacheGroup, ...]) –Cache groups that participate in external KV transfer.
cache_hit_alignment_tokens = None
class-attribute
instance-attribute
¶
Token granularity of prefix-cache hits, resolved by the engine core.
has_mixed_precision_kv_cache
property
¶
Whether device attention caches use more than one precision.
hash_block_size = None
class-attribute
instance-attribute
¶
Tokens per prefix-cache block hash, resolved by the engine core.
hisparse_host_block_stride = None
class-attribute
instance-attribute
¶
Physical bytes between consecutive HiSparse host blocks.
hisparse_host_num_blocks = None
class-attribute
instance-attribute
¶
Capacity of the dedicated HiSparse host-block manager, when enabled.
hisparse_shared_host_pool = False
class-attribute
instance-attribute
¶
Whether local TP ranks share one physical HiSparse host pool.
kv_cache_groups
instance-attribute
¶
The kv cache groups of the model.
For models with only one type of attention, there is only one group that
contains all layers.
For models with multiple types of attention, there will be multiple groups,
see _get_kv_cache_groups_uniform_page_size for more details.
kv_cache_layout = None
class-attribute
instance-attribute
¶
The KV cache layout resolved by the engine core, adopted by all workers.
kv_cache_tensors
instance-attribute
¶
How should model runner initialize the KV cache tensors for each layer
kv_tp_replicas = 1
class-attribute
instance-attribute
¶
Consecutive TP ranks holding identical KV for every layer (1: none).
needs_kv_cache_zeroing = field(init=False)
class-attribute
instance-attribute
¶
Whether newly allocated attention blocks must be zeroed before use.
Resolved across workers before scheduler projection and worker initialization.
num_blocks
instance-attribute
¶
The number of KV cache blocks
pipeline_stages = ()
class-attribute
instance-attribute
¶
The KV cache of every pipeline stage, by PP rank, resolved by the engine core so that both halves of a KV connector see every stage.
prefix_cache_retention_interval = None
class-attribute
instance-attribute
¶
Resolved retention policy for local prefix-cache checkpoints.
prefix_cacheable_group_ids
cached
property
¶
IDs of transferable groups eligible for hash-addressed stores.
prefix_cacheable_groups
cached
property
¶
Transferable groups eligible for hash-addressed stores.
transfer_group_ids
cached
property
¶
IDs of cache groups that participate in external KV transfer.
transfer_group_index_by_layer
cached
property
¶
Transfer-group tuple index for each participating layer.
transfer_groups
cached
property
¶
Cache groups that participate in external KV transfer.
num_blocks_of(tensor)
¶
Number of blocks addressable by the pool backing tensor.
select_transfer_block_ids(block_ids)
¶
Select block IDs for externally transferable cache groups.
KVCacheGroup
dataclass
¶
Represents a group of model layers that share the same KV cache block table. These layers are regarded as one layer in the KV cache manager.
Methods:
-
block_table_width–max_num_blocks_per_req(which accounts for DCP sharding: attention -
from_layers–A group of layers sharing a manager policy and block size.
-
max_allocated_blocks_per_request–Blocks one request at
max_model_lenholds in this group. -
max_num_blocks_per_req–The row length of the worker-side block table for this group, sized
Attributes:
-
distinct_specs(tuple[KVCacheSpec, ...]) –The distinct specs of the whole group's layers, for what kinds of
-
page_size_bytes(int) –Bytes one block of the group occupies across all its layers.
distinct_specs
property
¶
The distinct specs of the whole group's layers, for what kinds of
cache it holds. The same on every pipeline stage, unlike
kv_cache_specs.
page_size_bytes
property
¶
Bytes one block of the group occupies across all its layers.
block_table_width(vllm_config, max_len)
¶
max_num_blocks_per_req (which accounts for DCP sharding: attention
KV is sharded, recurrent state replicated) padded to the manager's
block-table token alignment.
from_layers(layer_configs, **kwargs)
classmethod
¶
A group of layers sharing a manager policy and block size.
max_allocated_blocks_per_request(vllm_config)
¶
Blocks one request at max_model_len holds in this group.
max_num_blocks_per_req(vllm_config, max_len=None)
¶
The row length of the worker-side block table for this group, sized
for max_len tokens (default: max_model_len).
KVCacheLayerConfig
dataclass
¶
What one layer needs from the KV cache: the manager that allocates its blocks, that manager's policy, and the layer's tensor layout.
Attributes:
is_draft = field(default=False, kw_only=True, compare=False)
class-attribute
instance-attribute
¶
Whether a draft model owns this layer; excluded from equality so it never affects grouping.
KVCacheSpec
dataclass
¶
A base class for specifying the KV cache format of one layer.
Methods:
-
copy_with_new_block_size–Create a new KVCacheSpec from self but replacing the block size.
-
get_block_stride_alignment–Bytes the stride between physical blocks must be a multiple of.
Attributes:
-
block_stride_alignment(int | MultipleOf | None) –Required byte alignment between physical blocks, including packed layers.
-
dcp_sharded(bool) –Whether DCP shards this cache's token positions across ranks.
-
has_layer_views(bool) –Whether generic allocation creates per-layer cache views.
-
page_size_bytes(int) –The size of a page with
block_sizetokens in bytes. -
uses_slot_mapping(bool) –Whether the worker computes a per-token slot mapping for this cache.
block_stride_alignment = field(default=None, kw_only=True)
class-attribute
instance-attribute
¶
Required byte alignment between physical blocks, including packed layers.
MultipleOf(n): whole kernel blocks, when blocks are split into kernel
blocks of up to n tokens.
dcp_sharded = field(default=False, kw_only=True)
class-attribute
instance-attribute
¶
Whether DCP shards this cache's token positions across ranks.
has_layer_views
property
¶
Whether generic allocation creates per-layer cache views.
Specs without a per-layer shape keep the raw backing tensor and lay it out themselves. They also have no attention module of their own, so they take no part in attention-backend or kernel-block selection.
page_size_bytes
property
¶
uses_slot_mapping
property
¶
Whether the worker computes a per-token slot mapping for this cache. A cache written at slots that already address its pages takes none.
copy_with_new_block_size(block_size)
¶
Create a new KVCacheSpec from self but replacing the block size.
get_block_stride_alignment()
¶
Bytes the stride between physical blocks must be a multiple of.
KVCacheStage
dataclass
¶
The KV cache of one pipeline stage.
Attributes:
-
bytes_per_block(int) –Bytes one block takes in the device KV cache of each worker of the
bytes_per_block
instance-attribute
¶
Bytes one block takes in the device KV cache of each worker of the
stage: the size the engine core plans that worker's num_blocks with.
KVCacheTensor
dataclass
¶
A class for specifying how the workers should initialize the KV cache.
Placement of a set of same-shaped layers in the KV cache allocation.
Layer layers[l]'s page for block b starts at
offset + l * layer_stride + b * block_stride bytes into the backing
allocation of size bytes. Layer-outermost layouts give each layer a
contiguous region (layer_stride = page * num_blocks,
block_stride = page); block-outermost layouts make each block a
block of all layers' pages (layer_stride = page, block_stride =
the packed block). Tensors whose address ranges overlap
alias the same bytes: cache groups overlay each other, which is sound
because a block ID is owned by one group at a time.
KVQuantMode
¶
Bases: IntEnum
KV cache quantization mode.
Used by attention backends and kernels to dispatch quantization logic
without string matching on kv_cache_dtype.
Attributes:
-
is_nvfp4(bool) –True for NVFP4 packed quantization mode.
-
is_octave(bool) –True for any Octave quantization mode.
-
is_per_token_head(bool) –True for any per-token-head quantization mode.
-
is_turboquant(bool) –True for any turboquant quantization mode.
-
is_ultraquant(bool) –True for the UltraQuant 4-bit KV-cache format.
is_nvfp4
property
¶
True for NVFP4 packed quantization mode.
is_octave
property
¶
True for any Octave quantization mode.
is_per_token_head
property
¶
True for any per-token-head quantization mode.
is_turboquant
property
¶
True for any turboquant quantization mode.
is_ultraquant
property
¶
True for the UltraQuant 4-bit KV-cache format.
MambaSpec
dataclass
¶
Bases: KVCacheSpec
Attributes:
-
state_alignment(int) –Byte alignment of each state within a page.
state_alignment = 1
class-attribute
instance-attribute
¶
Byte alignment of each state within a page.
ManagerConfig
dataclass
¶
Allocation policy of a layer: the SingleTypeKVCacheManager that
enforces it and that manager's Params. Layers whose configs compare
equal may share a block table.
PrefixLookupGroup
¶
Bases: NamedTuple
KV cache groups looked up jointly for cache hits: they share a manager policy, block size and DCP sharding.
compute_layer_kv_cache_shape_bytes(spec, num_blocks, kernel_block_size=None)
¶
Return the 4D logical shape (B, H, N, C) where C is in bytes.
compute_layout_strides(spec, num_blocks, num_layers, layout, kernel_block_size=None, fixed_strides=(None,) * 5)
¶
Byte strides in logical [L, B, H, N, C] axis order.
create_kv_cache_views(raw, spec, num_blocks, layout, kv_cache_tensor, kernel_block_size=None)
¶
View a flat int8 buffer as one 4D [B, H, N, C] view per layer.
Block b of layer l starts at the tensor offset plus its layer and
block stride contributions.
get_block_table_width(max_num_blocks, block_size, *, token_alignment=128)
¶
Return the width after optional alignment.
get_kv_quant_mode(kv_cache_dtype)
¶
Map a kv_cache_dtype string to a KVQuantMode.
get_reusable_prefix_length(num_tokens, num_hash_lookahead_tokens, alignment_tokens=1)
¶
Bound prefix reuse by known tokens, reserving logits and hash lookahead.
This does not establish that the corresponding KV has been computed or cached.
group_kernel_blocks(cache, num_blocks)
¶
View a kernel-block-granular layer cache with manager blocks as dim 0.
Kernel block splitting subdivides each manager block into uniformly strided
kernel blocks, so grouping is a pure view: (num_blocks * ratio, ...)
is_mamba_prefill_checkpoint_valid(query_start, query_end, checkpoint_position, hash_block_size, mamba_block_size, checkpoint_alignment)
¶
Whether a backend can export the checkpoint in this query.
kv_cache_uses_per_token_head_scales(kv_cache_dtype)
¶
Return True if kv_cache_dtype needs per-token-head scales.
prefix_lookup_groups(groups)
¶
Partition the prefix-cacheable groups into joint cache-hit lookups.