State Space Models for Language › Prefill, Decode, and What "Constant Memory" Counts

Michael BrenndoerferNovember 17, 20266 min read
Language AI HandbookAll chapters
Part 19 of 67Alternative Sequence and Generative Architectures
Chapter 133State Space Models for Language › Prefill, Decode, and What "Constant Memory" Counts
Reading section 10 of 13 · Prefill, Decode, and What "Constant Memory" Counts · 6 minSection 10 of 13 · 6 min

Section 10 of 13 · 6 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

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 LL in one pass.
  • Decode work. The cost of a single update. The attention row below counts the query at the LLth processed position, with L−1L-1 earlier keys plus its own.
  • Persistent storage. The bytes held per sequence after processing LL positions.

Starting a new step with a cache of LL positions gives L+1L+1 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 N=16N=16, 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]
CollapseExpand
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, fp32
ConsoleOut [21]
CollapseExpand
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 4d4d FLOPs per allowed pair when a multiply-add is counted as two FLOPs and dd is the sum of key widths over the heads, equal here to the sum of value widths. A length-LL causal prefill has L(L+1)/2L(L+1)/2 pairs. At the LLth processed position the decode count uses LL allowed pairs. For the SSM core, take the write vector wk=Bˉkukw_k=\bar B_k u_k as given. The diagonal update xk=Aˉkxk−1+wkx_k=\bar A_k x_{k-1}+w_k uses 2N2N FLOPs per channel, and the state readout CxkCx_k uses 2N−12N-1, so the proxy rounds to 4N4N. The literal worked-example loop also computes Bˉkuk\bar B_k u_k and adds DukDu_k. 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 LL 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]
CollapseExpand
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]
CollapseExpand
Log-log plot of attention and SSM prefill and decode arithmetic versus length.
Per-layer arithmetic for the attention products and SSM core from 64 to 262,144 positions. The length counts prompt positions for prefill and eligible keys for the attention decode query. Attention prefill grows quadratically, attention decode grows linearly, and the SSM core is linear for prefill and flat for decode.

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 4/N4/N as many bytes as the state, independent of prefix length. Mamba-1 reference block, v2.2.4.

PythonIn [24]
CollapseExpand
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]
CollapseExpand
Per-sequence KV cache grows with prefix length. Recurrent state stays flat, and including the convolution buffer raises that flat line.
Persistent per-sequence storage between decode steps, as a function of prefix length, for the same width and layer count. The key-value cache grows linearly. The SSM state and its convolution buffer are flat.

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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy