IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python module

max.nn.kv_cache

Key-value cache types for MAX layers.

Defines cache parameters, host-side buffers, and the input values that attention layers consume, for paged and recurrent caches alike.

Cache configuration​

CacheLeafParamInterfaceWhat every child of a cache tree contributes: leaves, inputs, cost.
HostStagingAllocHow a tiered KV connector allocates its pinned host staging region.
KVCacheAssignmentsAssignments of request blocks to KV cache pages for a replica.
KVCacheBufferA collection of KVCache buffers for one data-parallel replica.
KVCacheBufferInterfaceInterface for a KV cache buffer (single leaf or a tree of leaves).
KVCacheParamInterfaceA cache leaf a model reads through an attention op.
KVCacheParamsConfiguration parameters for key-value cache management in transformer models.
KVCacheGroupIdIdentifies the caches a model reuses and evicts together.
MHAKVCacheParamsKV cache parameters for multi-head attention (MHA).
MLAKVCacheParamsKV cache parameters for multi-latent attention (MLA).
MSAKVCacheParamsKV cache parameters for multi-step attention (MSA).
KVCacheQuantizationConfigConfiguration for KVCache quantization.
KVConnectorTypeIdentifies which off-device backing store the KV cache uses.
KVConnectorConfigInterfaceThe KV connector configuration contract: a type plus per-tier settings.
NullKVConnectorConfigConnector config for no off-device backing store.
KVCacheMemoryOne logical (child, kind) KV tensor as per-TP-shard uint8 views.
MultiKVCacheBufferA tree of KVCache buffers for one data-parallel replica.
MultiKVCacheParamsAggregates multiple cache parameter sets into a recursive tree.

Cache inputs​

KVCacheInputsThe KV-cache inputs one model step consumes: a tree with one entry per device, holding either that device's paged-cache inputs or its recurrent-state leaves.
KVCacheInputsPerDeviceSymbolic graph input types for a single device's paged KV cache.
BatchCharacteristicsUpper-bound batch shape used to prepare decode attention metadata.
PagedCacheValuesPaged KV cache inputs for one device, with cache blocks and lengths as graph values.

Recurrent state​

CacheLeafKindWhat a child of a cache tree holds.
KVLeafRegionOne addressable region of the cache pool.
PagedKVLeafRegionA leaf the graph reaches through a per-forward page table.
RecurrentKVLeafRegionA leaf addressed by row: one block holds one request's whole state.
RecurrentLeafInputsOne state leaf's graph inputs on one device.
RecurrentStateBufferOne replica's recurrent state pool, viewed as the pages it holds.
RecurrentStateInputsPerDeviceOne device's recurrent-state leaves.
RecurrentStateParamsA cache leaf whose entry is a state rather than a span of tokens.
RecurrentStateRegionShape and dtype of one kind of recurrent state, for one pool leaf.
ScratchRecurrentKVLeafRegionA row-addressed leaf drawn once per request and never published.

Attention dispatch​

AttnKeyA resolved decode-attention dispatch shape.
AttnKeyInterfaceCommon base for resolved attention keys.
MHAAttnKeyDecode dispatch metadata for multi-head attention (MHA).
MLAAttnKeyDecode dispatch metadata for multi-latent attention (MLA).
MSAAttnKeyDecode dispatch metadata for multi-step attention (MSA).

Metrics​

KVCacheMetricsMetrics for the KV cache.

Constants​

KVHashAlgoSupported hash algorithms for KV-cache block identity.
PACKED_PAGE_STRIDEpage_stride sentinel meaning the pages are packed.

Functions​

build_max_lengths_tensorsBuilds two [1] uint32 scalar buffers of maximum lengths.
compute_max_seq_len_fitting_in_cacheComputes the maximum sequence length that can fit in the available memory.
compute_num_device_blocksComputes the number of blocks that can be allocated based on the available cache memory.
estimated_memory_sizeComputes the estimated memory size of the KV cache used by all replicas.
max_pages_per_requestReturns the most KV pages one request can address.
packed_page_strideReturns the packed page_stride sentinel, typed to match like.
padded_lut_colsRounds a page lookup-table inner dim up to a kernel-safe width.
recurrent_leafReturns the first state a cache keeps, or None if it keeps none.
recurrent_leavesReturns every state a cache keeps, in tree order.
spec_decode_cache_slackComputes the extra KV positions a request may occupy past max_seq_len.