Topic 6 of 6 · 5 min read
State-space and hybrid architectures
Goal: Explain when a hybrid state-space model's linear-time cost helps and what it gives up compared with full attention.
A state-space layer such as Mamba reads the sequence once and carries a fixed-size summary forward, so its cost grows linearly with length and it needs no growing KV cache; the price is that it cannot look back at exact earlier tokens the way attention can. Hybrid models mix a few attention layers with many state-space layers to get most of both.
Check yourself · 1 of 3
What is the main reason a state-space layer needs less memory when generating long text?
Why it matters
Full attention compares every token with every earlier one, so compute grows with the square of the length and the KV cache grows linearly. At 100k to 1M tokens that is expensive. Linear-time layers keep memory flat during generation. If your workload is long documents, streams or many concurrent long sessions, that matters.
How the layers differ
- Attention: stores all past keys and values. Exact lookup of any earlier token. Cost per generated token grows with context.
- State-space (Mamba): keeps a fixed-size state updated per token, with input-dependent ("selective") gating that decides what to remember. Cost per token is constant (see the Mamba paper).
- Hybrid: for example, most layers are state-space and a small share are attention, with some designs adding MoE feed-forwards too. Attention layers handle precise recall; state-space layers handle bulk sequence mixing.
| Property | Full attention | State-space | Hybrid |
|---|---|---|---|
| Cost vs length | Quadratic to prefill | Linear | Mostly linear |
| Memory at decode | Grows with length | Fixed state | Small cache for attention layers |
| Exact recall of a distant fact | Strong | Weaker, lossy | Good, from attention layers |
| Ecosystem and tooling | Mature | Younger | Younger |
Choosing
Use a hybrid when contexts are very long or many sessions share a GPU and memory is the limit. Stay with attention when tasks need exact quotation or retrieval from anywhere in context, or when kernels, quantisation tools and serving engines for your stack are important to you.
Before and after
Weak: "State-space models replace transformers, so use one for everything." That ignores recall and tooling. Better: benchmark your task at your context length, including a needle-in-a-haystack test, against an attention model of similar size.
What goes wrong
- Expecting verbatim recall from a pure state-space model on a long input.
- Comparing speed at short context, where attention is already cheap and kernels are mature.
- Assuming your serving engine supports the new layer type; check before choosing.
Examples
Where the cache goes
A full-attention model at 128k tokens stores 128k keys and values per layer per sequence, for every active user. A state-space layer stores one fixed state per sequence no matter how long the input is. In a hybrid, only the few attention layers pay the growing cost.
A recall test that exposes the trade
Place a 12-digit code at 20 % depth in a 100k-token document and ask for it at the end. Run the same test across attention, hybrid and pure state-space models of similar size. Differences in exact recall show up here long before they appear in average benchmark scores.
Key points
- State-space layers carry a fixed-size state, so decoding cost and memory do not grow with context.
- Attention keeps every past key and value, which gives exact lookup but a growing cache and quadratic prefill.
- Hybrids interleave a few attention layers with many state-space layers to recover precise recall.
- The state-space advantage shows at long contexts and high concurrency, not at short prompts.
- Test recall at your length and confirm your serving stack supports the layer before committing.
Common mistakes
- Assuming linear cost means better quality at all lengths.
- Benchmarking only at short context where the advantage does not show.
- Expecting exact quotation of distant text from a pure state-space layer.
- Choosing a hybrid without checking inference engine support.
Check yourself · 2 of 3
What does a hybrid model add attention layers back for?
Check yourself · 3 of 3
Which workload most justifies trying a hybrid state-space model?
Practice items, not counted towards your mastery.
Start here
- The Big LLM Architecture Comparison (Sebastian Raschka) (opens in a new tab)ArticleFree
Side-by-side walkthrough of recent open-weight designs: attention variants, MoE layouts, norm placement.
- RoFormer: Enhanced Transformer with Rotary Position Embedding (opens in a new tab)PaperFree
The original rotary embedding paper: how rotating queries and keys encodes relative position.
- YaRN: Efficient Context Window Extension of Large Language Models (opens in a new tab)PaperFree
A scaling method that stretches rotary models to longer contexts with little extra fine-tuning.
More for this topic
- Mamba: Linear-Time Sequence Modeling with Selective State Spaces (opens in a new tab)PaperFree
The selective state-space layer behind most hybrid models, with its linear-time scaling.