Articles

State Space Models Explained: Mamba and Linear-Time AI

State space models like Mamba process sequences with a fixed-size hidden state instead of attention, scaling linearly with length. How they work and compare.

Chisato Chisato · · 5 min read
Abstract neural network fibers in purple

State space models (SSMs) are a family of sequence models that process input by updating a fixed-size hidden state one step at a time, rather than comparing every token with every other token as attention does. Mamba is the best-known modern example. Because the state does not grow with the sequence, SSMs scale linearly with input length and generate each new token at constant cost, which makes them an appealing alternative or complement to transformers for long contexts.

The problem SSMs address

Transformers are built on self-attention: every token looks at every other token to decide what matters. That is powerful, but it has two costs that grow with sequence length.

  • Training and prefill cost grows quadratically. Doubling the sequence length roughly quadruples the attention work.
  • Inference memory grows linearly. To generate each new token, a transformer keeps keys and values for every previous token in the KV cache, and that cache must be read on every step.

Techniques like FlashAttention and grouped-query attention reduce the constant factors, but the underlying scaling remains. SSMs take a different route: compress the history into a fixed-size state and update it as each token arrives.

What a state space model is

The idea comes from control theory and signal processing. A state space model describes a system with a hidden state that evolves over time in response to inputs. In discrete form, for each step:

h_t = A * h_(t-1) + B * x_t     (update the state)
y_t = C * h_t                    (produce an output)

x_t is the input at step t, h_t is the hidden state, and y_t is the output. The matrices A, B, and C are learned. A controls how the old state decays or persists, B controls how new input enters the state, and C controls what is read out.

If that looks like a recurrent neural network, it is closely related. The difference is in the structure. Early deep-learning SSMs used a linear, time-invariant update: the same A, B, and C at every step. That linearity allows the whole sequence to be computed during training as one long convolution, which runs in parallel on GPUs, while inference can still run step by step like a recurrence. Classic RNNs had neither property cleanly and were slow to train on long sequences.

What makes Mamba different: selectivity

Fixed, input-independent dynamics have a weakness for language. The model treats every token the same way regardless of content, so it cannot easily decide “this word matters, remember it” or “this is filler, let it fade.”

Mamba’s central change is making the SSM selective. The parameters that govern the update, including B, C, and the step size that controls how much the state changes, are computed from the current input. The model can now choose, token by token, what to write into its state and what to forget. In effect, it gains a content-aware gating mechanism similar in spirit to what LSTMs did, but inside a structured, scalable framework.

Selectivity breaks the convolution trick, since the dynamics now vary across the sequence. Mamba compensates with a hardware-aware parallel scan algorithm that computes the recurrence efficiently on GPUs by keeping the expanded state in fast on-chip memory, much as FlashAttention avoids writing large intermediate matrices to slower memory.

SSMs vs transformers

Transformer (attention)State space model (Mamba-style)
How history is storedEvery past token’s keys and valuesA fixed-size compressed state
Training cost vs lengthQuadraticLinear
Memory per generated tokenGrows with contextConstant
Exact recall of earlier tokensStrong: can look back directlyWeaker: depends on what the state kept
Long-context efficiencyExpensiveEfficient
Ecosystem and toolingVery matureYounger

The trade-off is in the second and fifth rows. Attention can retrieve any earlier token precisely because it never throws anything away. An SSM has to decide what to keep in a bounded state, so tasks that need verbatim copying or pinpoint retrieval from far back in a long input are harder for it. Research comparing the two has generally found pure SSMs competitive on many language tasks while lagging on recall-heavy ones.

Hybrid architectures

Because the strengths are complementary, a common design mixes the two: most layers are SSM blocks, with attention layers interleaved at intervals. The SSM layers carry the bulk of the sequence processing cheaply, and the occasional attention layer provides precise lookback. Hybrids keep much of the efficiency gain while recovering recall ability. The same mixing logic appears elsewhere in model design, such as combining dense layers with mixture-of-experts layers.

Where SSMs are useful

  • Very long sequences. Genomics, audio, time series, and long documents, where quadratic attention becomes prohibitive.
  • Streaming and edge inference. Constant memory per step makes SSMs attractive on devices with tight memory budgets and for always-on processing of continuous input.
  • Throughput-sensitive serving. Without a growing KV cache, more concurrent sequences fit in the same memory.

For general-purpose chat models, transformers remain the dominant architecture, partly because of their recall strengths and partly because the tooling, kernels, and serving infrastructure around them are far more mature.

How SSMs relate to context windows

A transformer’s context window is a hard limit on how many tokens it can attend to. An SSM has no equivalent hard limit in principle, since it just keeps updating its state. But that does not mean it remembers everything. The effective memory is whatever survives in the state, which is a softer and less predictable boundary. “Unlimited context” and “perfect recall over unlimited context” are different claims.

The takeaway

State space models replace attention’s look-at-everything approach with a compact hidden state updated token by token, giving linear scaling and constant per-token memory. Mamba made the approach competitive for language by making the state updates input-dependent and computing them with a hardware-efficient scan. The cost is weaker exact recall over long inputs, which is why hybrids that mix SSM layers with some attention are a popular middle ground.

Chisato Chisato · · 5 min read

Prefill vs Decode: The Two Phases of LLM Inference

LLM inference runs in two phases: prefill processes the prompt in parallel, decode generates tokens one at a time. Why they behave so differently.

#AI #LLMs #Machine Learning