State Space Models for Language › Prefill, Decode, and What "Constant Memory" Counts
Language AI HandbookAll chapters The complexity discussion so far has been about arithmetic. Practical deployment cares about three separate quantities, and they scale differently.
- Prefill work. The cost of processing a prompt of length in one pass.
- Decode work. The cost of a single update. The attention row below counts the query at the th processed position, with earlier keys plus its own.
- Persistent storage. The bytes held per sequence after processing positions.
Starting a new step with a cache of positions gives eligible keys once the new key is added. Repeated decode steps can dominate a long completion, depending on the prompt, the completion length, and the implementation. When device memory is the bottleneck, the amount of persistent storage per sequence helps determine how many concurrent sequences fit.
Use a single-sequence calculation with 24 layers, state dimension , and width 1024 on each side. The attention side has 1024 total dimensions for keys and 1024 for values, and matches query and key heads one to one. The state space mixer has 1024 inner channels. These equal widths give a worked comparison of core operations rather than a parameter-matched pair of model configurations.
PythonIn [20]Expand
d_model = 1024 # summed head width and SSM inner width in this example
n_state = 16 # SSM state dimension per channel
n_layers = 24 # number of blocks
L = 4096 # processed positions and eligible keys for query L
bytes_per_scalar = 2 # fp16
# Attention products, counting two FLOPs per multiply-add.
attn_prefill_flops = (
2 * L * (L + 1) * d_model
) # QK^T and weighted V over causal pairs
attn_decode_flops = 4 * L * d_model # one query over L keys, including its own
# Per-layer SSM core: update with a precomputed write, then read out the state.
ssm_prefill_flops = 4 * n_state * d_model * L
ssm_decode_flops = 4 * n_state * d_model
# Persistent per-sequence storage
kv_elements = 2 * L * d_model * n_layers # K and V
ssm_elements = n_state * d_model * n_layers # one state per channel
conv_elements = 4 * d_model * n_layers # depthwise conv buffer, width 4
kv_bytes = kv_elements * bytes_per_scalar
ssm_bytes = ssm_elements * bytes_per_scalar
conv_bytes = conv_elements * bytes_per_scalar
ssm_total_bytes = ssm_bytes + conv_bytes
expanded_state_bytes = (
1 * 2048 * 8192 * 16 * 4
) # batch, channels, length, state, fp32ConsoleOut [21]Expand
configuration: d=1024, N=16, layers=24, L=4096 quantity attention SSM prefill arithmetic (FLOPs per layer) 34,368,126,976 268,435,456 decode arithmetic at L (FLOPs per layer) 16,777,216 65,536 persistent storage (bytes per sequence) 402,653,184 983,040 ratio, prefill arithmetic: 128.0x ratio, decode arithmetic: 256.0x ratio, persistent storage: 409.6x SSM storage across 24 layers: 786,432 bytes of recurrent state + 196,608 bytes of convolution buffer Expanded float32 state array at batch 1: 1.00 GiB
The table counts the two main attention products. Computing the query-key dot product and adding a weighted value uses roughly FLOPs per allowed pair when a multiply-add is counted as two FLOPs and is the sum of key widths over the heads, equal here to the sum of value widths. A length- causal prefill has pairs. At the th processed position the decode count uses allowed pairs. For the SSM core, take the write vector as given. The diagonal update uses FLOPs per channel, and the state readout uses , so the proxy rounds to . The literal worked-example loop also computes and adds . Both operations are outside this estimate.
The table reports arithmetic per layer and persistent storage per sequence across all 24 layers. It counts the attention cache after positions and includes both the recurrent state and the convolution buffer on the SSM side. Each uses two bytes per element. Model weights, temporary activations, allocator overhead, and cache compression are outside the storage count. For the arithmetic comparison, projections, softmax, discretization, and scan overhead are outside the count. The plots therefore describe the scaling of these selected operations rather than wall-clock inference time.
The arithmetic table above is easier to read as scaling curves. The next two cells reuse the fixed dimensions from that configuration and plot the per-layer work as the prefix length grows.
PythonIn [22]Expand
import numpy as np
scale_lengths = np.array([2**k for k in range(6, 19)], dtype=float)
attention_prefill_curve = 2 * scale_lengths * (scale_lengths + 1) * d_model
ssm_prefill_curve = 4 * n_state * d_model * scale_lengths
attention_decode_curve = 4 * scale_lengths * d_model
ssm_decode_curve = np.full_like(scale_lengths, 4 * n_state * d_model)VisualizationOut [23]Expand

The Mamba-1 implementation has a causal convolution before the state space layer. Its v2.2.4 default buffer length is four entries per inner channel. At the same dtype as the state, the buffer uses as many bytes as the state, independent of prefix length. Mamba-1 reference block, v2.2.4.
PythonIn [24]Expand
prefix_lengths = np.array([2**k for k in range(6, 19)], dtype=float)
kv_storage = 2 * prefix_lengths * d_model * n_layers * bytes_per_scalar
ssm_storage = np.full_like(prefix_lengths, ssm_bytes)
ssm_storage_with_conv = np.full_like(prefix_lengths, ssm_total_bytes)VisualizationOut [25]Expand

For the plotted dimensions, the SSM state and convolution buffer stay constant and the full attention cache grows in proportion to the processed prefix length. At the same per-head key and value widths and dtype, fewer key-value heads lower the cache payload. At the same number of cached elements, fewer bytes per element also lower it.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!