Skip to content

vllm.model_executor.layers.mamba.mamba_utils

Classes:

Functions:

Attributes:

MambaStateCopyFunc = Callable[[torch.Tensor, list[int], int, int], MambaCopySpec] module-attribute

Type alias for a function that computes a MambaCopySpec for copying state slices. Parameters: state: torch.Tensor - the Mamba state tensor (e.g., conv or temporal states). block_ids: list[int] - the list of block indices for the state to copy. cur_block_idx: int - current block index within block_ids to copy from. num_accepted_tokens: int - number of accepted tokens used to compute the copy offset. Range: 1 .. 1 + num_speculative_tokens (inclusive).

MambaCopySpec dataclass

Data class specifying the memory-copy parameters for Mamba states used for prefix caching.

Attributes:

  • start_addr (int) –

    Starting address for the memory copy operation.

  • num_elements (int) –

    Number of elements to copy from the starting address.

MambaStateDtypeCalculator

Methods:

append_replayssm_ring(base_dtypes, model_dtype) classmethod

Append the ReplaySSM ring dtypes to a base (conv, ssm) tuple: (x_cache, B_cache, dt_cache, trackers) = (activation, activation, fp32, int32).

gated_delta_net_state_dtype(model_dtype, mamba_cache_dtype, mamba_ssm_cache_dtype='auto', use_replayssm=False) classmethod

(conv, ssm), plus the ReplaySSM (d, k, g) rings and the int32 ring trackers when enabled.

The d/k rings use fp16 for bf16 activations; g is float32.

MambaStateShapeCalculator

Methods:

_orient_conv_shape(dim, state_len) staticmethod

Return (dim, state_len) for DS layout, (state_len, dim) for SD.

append_replayssm_ring(base_shapes, n_groups, tp_world_size, logical_window, backend, num_speculative_tokens=0) classmethod

Append the physical ReplaySSM ring shapes (x, B, dt) and the per-slot ring trackers (ring_start, num_committed).

The fp32 dt ring goes after the x and B rings: FlashInfer reads the state and the x and B rings with 16-byte copies, while dt and the trackers need only 4-byte alignment. With dt after them, every 16-byte-sensitive state starts at a multiple of 16 bytes for the supported head_dim / d_state values, without padding.

base_shapes[1] is (nheads // tp, head_dim, state_size); B_cache extends n_groups for head shards as in mamba2_state_shape.

extra_groups_for_head_shards(ngroups, tp_size) classmethod

Compute the increase in group numbers to account for replication in order to accompany the head shards.

gated_delta_net_state_shape(tp_world_size, num_k_heads, num_v_heads, head_k_dim, head_v_dim, conv_kernel_size, num_spec=0, replayssm_buffer_len=None) classmethod

Append the ReplaySSM (d, k, g) rings of length replayssm_buffer_len and the per-slot ring trackers (ring_start, num_committed) when it is set.

get_conv_copy_spec(state, block_ids, cur_block_idx, num_accepted_tokens)

Return a MambaCopySpec for copying a convolutional state slice.

Works for both SD layout (num_blocks, state_len, dim) and DS layout (num_blocks, dim, state_len).

get_conv_state_layout() cached

Return the SSM conv state layout.

SD = (state_len, dim) — dim is the innermost contiguous dimension. DS = (dim, state_len) — TP-sharded dim is on dim-1 (like HND for KV cache), consistent with SSM temporal state layout.

get_temporal_copy_spec(state, block_ids, cur_block_idx, num_accepted_tokens)

Return a MambaCopySpec for copying a temporal state slice.

is_conv_state_dim_first()

True when the conv state is stored as (dim, state_len) per block.