core.inference.contexts.mamba_replay_metadata#
ReplaySSM state for Mamba-2 speculative decoding.
ReplaySSM (https://tridao.me/blog/2026/replayssm/) keeps each slot’s live SSM state as a
lazily-updated checkpoint plus a per-(layer, slot) circular ring buffer of the SSM inputs
cached since that checkpoint (post-conv x|B at conv-state precision, raw fp32 dt).
Verification outputs are computed from the checkpoint and the ring, the checkpoint is
re-materialized only on (early-)flush steps, and speculative rollback is a device-side move of
the ring cursors. All of it is persistent GPU state that the kernels update in place, so unlike
the per-step Mamba metadata nothing here rides the bookkeeping H2D.
DynamicInferenceContext holds a MambaReplayMetadata only when mamba_replay_ssm is enabled
with speculative decoding; otherwise MambaReplayMetadata.create returns None and no buffer or
bookkeeping is spent.
Module Contents#
Classes#
ReplaySSM geometry, ring buffers, ring cursors, and per-step scratch. |
Functions#
Raise unless the model’s recurrent layers are Mamba-2. |
Data#
API#
- core.inference.contexts.mamba_replay_metadata._DT_CACHE_DTYPE#
None
- core.inference.contexts.mamba_replay_metadata._validate_mamba2_model(
- state_config: Optional[megatron.core.inference.config.MambaInferenceStateConfig],
Raise unless the model’s recurrent layers are Mamba-2.
The ReplaySSM kernels implement only the Mamba-2 recurrence. Gated DeltaNet has its own layer symbol; Gated Delta Product mixers sit in Mamba (“M”) layers but report a nonzero Householder count. (A PP stage holding no SSM layer reports a count of 0, but it also never runs an SSM decode.)
- class core.inference.contexts.mamba_replay_metadata.MambaReplayMetadata(
- num_speculative_tokens: int,
- buffer_len: int,
- conv_states_shape: Tuple[int, ...],
- ssm_states_shape: Tuple[int, ...],
- conv_states_dtype: torch.dtype,
ReplaySSM geometry, ring buffers, ring cursors, and per-step scratch.
The geometry is fixed at construction because the context’s memory budget needs it before
max_requestsis known;allocateanddeallocatethen follow the context’s tensor lifecycle. Attribute names are part of the op-layer contract:MambaMixer.ssm_decodereadswindow,flush_threshold,write_pos,origin,is_flush,qsl, andbc_preby attribute access, and the per-layer rings throughlayer_caches.Initialization
- Parameters:
num_speculative_tokens – Draft tokens per decode step (at least 1).
buffer_len – Committed-history buffer length
B, in tokens.conv_states_shape – Per-request conv state shape
(conv_dim, d_conv).ssm_states_shape – Per-request SSM state shape
(nheads, headdim, d_state).conv_states_dtype – Conv state dtype; the post-conv cache is stored at this precision.
- PERSISTENT_TENSORS#
(‘conv_cache’, ‘dt_cache’, ‘write_pos’, ‘origin’, ‘is_flush’)
- static create(
- enabled: bool,
- num_speculative_tokens: int,
- buffer_len: int,
- state_config: Optional[megatron.core.inference.config.MambaInferenceStateConfig],
The ReplaySSM state, or None when ReplaySSM is not in use.
- Parameters:
enabled – Whether
mamba_replay_ssmis requested.num_speculative_tokens – Draft tokens per decode step. ReplaySSM is inert without speculative decoding, so 0 returns None (after validating the model).
buffer_len – Committed-history buffer length
B, in tokens.state_config – The model’s Mamba inference state config; None for models without recurrent layers.
- Raises:
ValueError – If enabled for a model whose recurrent layers are not Mamba-2, or with
buffer_len < 1 + num_speculative_tokens.
- ring_bytes_per_request(num_layers: int) int#
Bytes of ring buffers one request adds across
num_layersMamba layers.
- allocate(
- num_layers: int,
- num_slots: int,
- device: torch.device,
Reserve the ring buffers, cursors, and scratch.
- Parameters:
num_layers – Number of Mamba layers on this rank.
num_slots – Number of Mamba state slots, including any reserved dummy slot; this also bounds the number of decode rows in a step.
device – Device for the buffers.
- deallocate() None#
Release the buffers, mirroring
allocate.Used by the context’s suspend path, which drops its tensors and rebuilds them from
initialize_all_tensorson resume.
- layer_caches(
- mamba_layer_idx: int,
The
(post_conv_cache, dt_cache)rings of one Mamba layer.
- reset_slots(slots: Sequence[int]) None#
Empty the rings of the given state slots.
Call whenever a slot’s live SSM state becomes exact for its request (zeroed, restored from the prefix cache, or imported by a disaggregated handoff), so the kernels treat it as the checkpoint with no cached inputs.
- assert_rings_empty(slots: Sequence[int]) None#
Assert that the live SSM states of the given slots are exact.
A slot’s live state may only be read out as a full state (e.g. saved into the prefix cache) while its ring is empty; otherwise the ring must be folded in first.
- commit(
- accepted_draft_counts: torch.Tensor,
- prefill_status: torch.Tensor,
- state_batch_indices: torch.Tensor,
Commit one speculative step: the entire ReplaySSM rollback.
Advances each decode row’s ring cursors past its committed tokens (the bonus token plus the accepted drafts) and precomputes the next step’s flush flags, all on device. Rejected drafts stay behind the new write position and are overwritten by the next step’s scatter, so no state is copied. Prefill rows are left untouched.
- Parameters:
accepted_draft_counts –
(batch,)accepted draft tokens per row.prefill_status –
(batch,)1 for prefill rows, 0 for decode rows.state_batch_indices –
(batch,)Mamba state slot per row.
- warmup_commit() None#
Compile the commit kernel ahead of the first decode step.
The commit runs eagerly after each decode step, outside the CUDA graphs, so its first Triton compile would otherwise land on the first real step. Every row is marked as prefill, so the call leaves the cursors untouched.