For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Python class
KVCacheParams
KVCacheParamsβ
class max.nn.kv_cache.KVCacheParams(dtype, head_dim, num_layers, devices, enable_prefix_caching=False, enable_dp_cross_replica_prefix_copy=True, per_layer_buffers=False, kv_connector=None, kv_hash_algo='ahash64', kv_hash_seed=None, kv_connector_config=None, host_kvcache_swap_space_gb=None, page_size=128, data_parallel_degree=1, kvcache_quant_config=None, speculative_method=None, num_draft_tokens=0)
Bases: KVCacheParamInterface
Configuration parameters for key-value cache management in transformer models.
This class encapsulates all configuration options for managing KV caches during inference, including parallelism settings, and memory management.
-
Parameters:
-
- dtype (DType)
- head_dim (int)
- num_layers (int)
- devices (Sequence[DeviceRef])
- enable_prefix_caching (bool)
- enable_dp_cross_replica_prefix_copy (bool)
- per_layer_buffers (bool)
- kv_connector (KVConnectorType | None)
- kv_hash_algo (Literal['ahash64', 'sha256', 'sha256_64'])
- kv_hash_seed (bytes | None)
- kv_connector_config (Any)
- host_kvcache_swap_space_gb (float | None)
- page_size (int)
- data_parallel_degree (int)
- kvcache_quant_config (KVCacheQuantizationConfig | None)
- speculative_method (Literal['eagle', 'mtp', 'dflash'] | None)
- num_draft_tokens (int)
allocate_buffers()β
allocate_buffers(total_num_pages)
Allocates the buffers for the KV cache.
-
Parameters:
-
total_num_pages (int)
-
Return type:
build_runtime_inputs()β
build_runtime_inputs(assignments, buffers)
Builds the runtime KV-cache leaf spanning all replicas.
assignments and buffers are indexed by data-parallel replica.
The returned KVCacheInputs lists one
KVCacheInputsPerDevice per (replica, TP shard), in the
same replica-major order as get_symbolic_inputs().
bytes_per_blockβ
property bytes_per_block: int
Returns the number of bytes per cache block.
When TP>1, each block is sharded across the devices in the tensor parallel group. This method returns the total memory needed to store a block across these devices. Includes memory needed for scales if quantization is enabled.
-
Returns:
-
The number of bytes per cache block.
data_parallel_degreeβ
data_parallel_degree: int = 1
Degree of data parallelism. Devices are grouped replica-major, with
n_devices // data_parallel_degree TP shards per replica.
devicesβ
Devices to use for the KV cache.
devices_per_replicaβ
Returns the devices per replica.
dtypeβ
dtype: DType
Data type for storing key and value tensors in the cache.
dtype_shorthandβ
property dtype_shorthand: str
Returns a shorthand textual representation of the data type.
-
Returns:
-
βbf16β for bfloat16 dtype, βf32β otherwise.
enable_dp_cross_replica_prefix_copyβ
enable_dp_cross_replica_prefix_copy: bool = True
Whether a prefix-cache block resident on another data-parallel (DP)
replicaβs device may be materialized locally via a device-to-device copy
to serve a cache hit. When False, cross-replica reuse is only served from
the shared external tier via the KV connector (or recomputed). Only
relevant when data_parallel_degree > 1 and prefix caching is enabled.
enable_prefix_cachingβ
enable_prefix_caching: bool = False
Whether to enable prefix caching for efficient reuse of common prompt prefixes.
get_symbolic_inputs()β
get_symbolic_inputs(namespace='')
Computes the symbolic inputs for the KV cache.
-
Parameters:
-
namespace (str) β Prefix disambiguating this cacheβs per-pool page-count dim from sibling caches in a multi-group tree (empty for a single-group cache).
-
Returns:
-
The symbolic inputs for the KV cache.
-
Return type:
head_dimβ
head_dim: int
Dimensionality of each attention head.
host_kvcache_swap_space_gbβ
Amount of host memory (in GB) to reserve for KV cache swapping. Required when local or tiered connector is used.
is_fp8_kv_dtypeβ
property is_fp8_kv_dtype: bool
Whether the KV cache stores FP8 data, for dispatch resolution.
Unlike quantized_kv_cache (which also requires valid scale config),
this checks only the storage dtypeβmatching the compile-time detection
in the MLA decode kernel.
TODO(SERVOPT-1094): Once SnapMLA uses a valid scale_dtype, this
can be replaced by quantized_kv_cache.
kv_cache_scale_dtypeβ
property kv_cache_scale_dtype: DType
Returns the dtype of the KV cache scales.
-
Returns:
-
The dtype of the KV cache scales.
kv_connectorβ
kv_connector: KVConnectorType | None = None
Type of KV cache connector to use (null, local, tiered, dkv).
kv_connector_configβ
kv_connector_config: Any = None
Connector-specific configuration (KVConnectorConfig from the pipelines layer).
kv_dimβ
property kv_dim: int
kv_hash_algoβ
kv_hash_algo: Literal['ahash64', 'sha256', 'sha256_64'] = 'ahash64'
Hash algorithm used for KV-cache block identity.
kv_hash_seedβ
Resolved 32-byte cluster seed for sha256/sha256_64. None for ahash64.
Set by KVCacheConfig.to_params via resolve_kv_hash_seed.
kvcache_quant_configβ
kvcache_quant_config: KVCacheQuantizationConfig | None = None
KVCache quantization config. Currently only FP8 quantization supported.
n_devicesβ
property n_devices: int
Returns the number of devices.
-
Returns:
-
The number of devices.
n_kv_heads_per_deviceβ
property n_kv_heads_per_device: int
num_draft_tokensβ
num_draft_tokens: int = 0
Total draft tokens generated per speculative iteration.
Zero when no speculative decoding is configured.
num_layersβ
num_layers: int
Number of layers in the model.
page_sizeβ
page_size: int = 128
Number of tokens per page (block).
This value is expressed in tokens, not bytes. The byte footprint of a page is derived from pipeline configuration.
Current constraints: the page size must be a multiple of 128 and at least 128.
per_layer_buffersβ
per_layer_buffers: bool = False
When True, allocate one standalone single-layer buffer per layer
instead of one [..., num_layers, ...] multi-layer buffer.
Each attention dispatch then binds only its own per-layer buffer, so the
pool total can exceed a per-allocation size cap (e.g. a deviceβs maximum
single allocation) while every individual buffer stays under it. Defaults
to False (one multi-layer buffer), keeping all other backends and
models byte-identical.
quantized_kv_cacheβ
property quantized_kv_cache: bool
Returns whether FP8 KV cache quantization is enabled.
-
Returns:
-
Truewhen the cache dtype isfloat8_e4m3fnorfloat8_e4m3fnuzand a valid quantization scale dtype is configured;Falseotherwise.
replicates_kv_across_tpβ
property replicates_kv_across_tp: bool
Whether every device holds identical KV state.
shape_per_blockβ
Returns the shape of each cache block.
-
Returns:
-
The shape of the cache block.
shape_per_layer_blockβ
Returns the block shape for a single-layer buffer.
Same as shape_per_block but with the layer dimension pinned to
1. Used when per_layer_buffers is set: the pool allocates
num_layers such buffers per device instead of one multi-layer
buffer. The attention kernel derives num_layers from this dim, so a
single-layer buffer (num_layers == 1) with layer_idx == 0 is
self-consistent.
shape_per_scale_blockβ
Returns the shape of each scale block used for KVCache quantization
-
Returns:
-
The shape of the KVCache quantization scales block.
speculative_methodβ
speculative_method: Literal['eagle', 'mtp', 'dflash'] | None = None
Speculative decoding method propagated from SpeculativeConfig
tensor_parallel_degreeβ
property tensor_parallel_degree: int
Returns the tensor parallel degree.
-
Returns:
-
The tensor parallel degree.
unflatten_basic_kv_tree()β
unflatten_basic_kv_tree(it)
Unflattens a basic KV tree from a graph-input iterator.
Requires that the model is a basic height-1 tree. This method does not work on nested trees.
-
Parameters:
-
Return type:
-
tuple[list[KVCacheInputsPerDevice[TensorValue, BufferValue]], β¦]
unflatten_kv_inputs()β
unflatten_kv_inputs(it)
Unflattens the KV cache inputs from a graph-input iterator.
-
Parameters:
-
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!