Skip to content

vllm.v1.kv_cache_interface

Classes:

Functions:

AttentionSpec dataclass

Bases: KVCacheSpec

Attributes:

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

Bases: HiSparsePageSpec

Hot-cache pages, which kernels read as flat rows.

HiSparsePageSpec dataclass

Bases: KVCacheSpec

Raw pages HiSparse lays out itself (hot and GPU-resident caches).

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:

Attributes:

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:

Attributes:

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 (bool) –

    Whether a draft model owns this layer; excluded from equality so it never

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:

Attributes:

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

The size of a page with block_size tokens in bytes.

Returns:

  • int –

    The page size

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 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 = 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.