State space models vs Transformers
36 sourced facts · updatedState space models (Mamba) or transformers: what is the difference?
A transformer lets every token look back at every earlier token, so its cost grows with the length of the input. A state space model reads the input once and keeps one fixed-size state, so compute grows linearly and memory stays constant. The price is a lossy memory: early details can be overwritten. The sources show newer models mixing the two, with a few attention layers kept to carry exact retrieval. [1][2][3][4]
In the knowledge graph: Both turn a sequence into its next step: one carries a running state, the other looks back at everything.
The difference between State space models and Transformers
| State space models | Transformers | |
|---|---|---|
| What it keeps of the past | One fixed-size state, updated token by token. Albert Gu calls it a brain: it compresses what it saw. [2][3] | A key and a value vector for every earlier token: ten tokens need ten sets, a thousand tokens need a thousand. Gu calls this a database that can look back exactly. [5][6] |
| Cost as the input grows | Compute grows only linearly and memory stays constant. [1][2] | Attention costs quadratic computation, because every element interacts pairwise with every other, and memory grows linearly with context on top of that. In NVIDIA's words, processing the 10-millionth token takes a million times longer than the 10th. [7][8] |
| Exact recall | Weak. The price of the fixed state is weak fine-grained recall and retrieval. One lab found its linearized versions had trouble learning dependencies at long context. [3][9] | Strong. Some attention heads do the retrieval: after removing the top 50 heads found by one method, Qwen3-8B fell from 0.401 to 0.000 ROUGE-L on a non-literal retrieval benchmark. [10][11] |
| Fit with GPUs | Recurrence is cheap only in theory until it can run in parallel. Linearizing it makes that possible, and a parallel scan turns L sequential steps into about log2 L parallel steps. The cost is that the linear form is less expressive. [12][13][14] | Transformers are matrix multiplications and scale on the hardware that exists. FlashAttention-style kernels tile the attention matrices to fit in fast memory. [15] |
| What serving costs in memory | A constant slab per sequence, but not a small one: in NVIDIA's analysis one SSM layer's FP32 state is 4 MiB per sequence, equal to the KV of 8K tokens. [16] | A shelf that grows with context and with users. For the 7B model in one workshop it is 131 KB per token, and at a 4K context and 80 users it comes to about 42 GB, more than a 24 GB card holds. [17][18] |
| Where it is used | Sequential data such as time series and voice, and long document pages: one document model chose a state-space backbone for long pages and accepted somewhat lossy recall to avoid the compute cost of a transformer. [19][20] | The loop behind every token a model writes: attention works out which tokens refer to which, and the feed-forward layers are where stored knowledge mostly sits. [21][22] |
| How serving goes wrong | Silently. Attention writes its cache before reading it, but a Mamba decode step reads the state first, so a stale slot corrupts the answer. One engine returned gibberish on about 1 in 1,000 requests, and nothing crashed. [23][24] | At the edge of memory or in the kernel: the shelf outgrows the card, or a tuning change backfires. NVIDIA's larger FlashAttention tiles made the kernel 18-43% slower. [18][25] |
When to use State space models
- The input is long and sequential, such as time series, voice or a long document page, and the cost of attention over all of it is the problem. [19][20][7]
- The task is long-horizon generation where bounded attention caches limit access to distant history: one motion-generation model uses Mamba to keep resumable recurrent states plus a persistent content memory. [26]
- You can accept lossy memory for constant memory. The sources say the update rule decides what is written and how strongly it overwrites, and a gated, correctable memory did best in one comparison of three subquadratic designs. [27][28][8]
When to use Transformers
- You need to find one specific fact buried in a long context. NVIDIA says pure SSMs struggle with precise associative recall, and Albert Gu names weak fine-grained recall as the price of the fixed state. [4][3]
- You want to avoid immature tooling. A lab that linearized recurrent nets found the state-space versions had trouble learning long-context dependencies, and open inference kernels for hybrids were immature when Olmo Hybrid was post-trained. [9][29]
- You would rather shrink attention's cost than change architecture. The transformer sources list fixes: grouped-query attention cut key-value memory 4x in the workshop's example (8 key-value heads in place of 32), and quantizing the cache lets the same memory hold more tokens or users. [30][31][13]
Using State space models and Transformers together
The sources show the two mixed: hybrids keep a few attention layers among the state-space layers. Long-range retrieval is mainly carried by the full-attention layers. One hybrid Mamba-attention model with 55 billion active parameters was extended to 1M tokens of context, and Albert Gu says today's models use at least 3:1 or 4:1 SSM to attention layers, where older papers found about 10:1 best. Hybrids are not free: Olmo Hybrid gained about 2x in pretraining, but post-training results were mixed and RL took more compute than the dense 7B model. [32][33][34][29][4]
Does a model need a very long context, or is a short one with frequent compaction enough?
The sources disagree. One says a very long context is worth building, since a hybrid with a one-million-token context suits long-running autonomous agent tasks. Another says frequent compaction kept task accuracy with short contexts, so long-context models may be unnecessary. [35][36][8]