# State-Space Models (e.g. Mamba) vs Transformers

> How state-space models like Mamba compare with transformers, what each gains and gives up, and where hybrids fit, explained for your AI interview.

**In short:** State-space models like Mamba keep one fixed-size memory that they update as each token arrives, so long inputs stay cheap. Transformers look back at every earlier token, which keeps every detail within reach but costs more as the text grows.

AI and LLM interview question Q071, Transformer architecture. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/state-space-models-e-g-mamba-vs-transformers

## Say it in 30 seconds

Most large language models are built on transformers. A transformer looks back at every earlier token each time it writes the next one. A state-space model, like Mamba, reads the text in order and keeps one fixed-size memory it updates as it goes. That keeps long inputs cheap. The cost is that it can lose a detail it never kept, so some designs, like Jamba, mix both.

## Say it in 2 minutes

Most large language models are built on the transformer. Its key part is attention. When the model writes the next word, attention looks back over the earlier tokens in the window, and the model keeps those past tokens in a memory called the KV cache. That gives it a sharp view of the whole text, but the memory grows with every token, and over a whole text the work grows faster than the text does.

A state-space model, such as Mamba, works more like a reader with a notebook. It reads one token at a time and updates a fixed-size state, a summary of what matters so far. Its cost per token stays steady however long the text gets. The Mamba paper, from December 2023, reported strong language results against transformers of the same size.

The trade-off is that the state is a summary. A detail the model did not keep is gone, while a transformer still holds every token in its cache. So each design wins on a different problem. The Mamba-2 paper from 2024 says the two families are closely related, and Jamba mixes both kinds of layers in one model.

In an interview, I would say the choice depends on the input. For a long sensor stream, a state-space model keeps the cost flat. For exact lookup across a long contract, I would test a transformer or a hybrid on my own data first.

## What are state-space models, in plain words?

A language model reads text as a list of tokens, small pieces of words. Each new token depends on what came before, so the model needs a way to remember the past.

A transformer remembers by looking back. A state-space model remembers by keeping a running summary. Both answer the same question, but they keep very different amounts of the past.

Think of a night-shift nurse handing over a long ward. She could reread every patient's full chart each time someone asks a question, which is the transformer way. Or she could keep one notebook of key facts and update it every hour, which is the state-space way. The notebook is quick to use and never grows, but a fact she never wrote down is lost.

Here is the same idea in a model. Asked about a 300-page contract, a transformer looks back at every earlier word for each new word. A state-space model answers from one running summary of the contract so far.

- **Token:** A small piece of text, often part of a word, that the model reads and writes one at a time.
- **Attention:** The transformer step that compares the new token with the earlier tokens to decide what matters.
- **KV cache:** The memory where a transformer keeps the keys and values of its past tokens, so it can look back while it writes.
- **State:** A fixed-size summary that a state-space model updates as each token arrives.

## How does each one remember what it has read?

A transformer keeps the whole past. Attention lets each new token look back over the window, which the Mamba paper describes as the ability to route information densely within a context window. The cost is memory: every past token has to be stored, so the cache grows with the text.

A state-space model keeps only a state of fixed size. The Mamba paper puts the trade-off plainly: a small state is efficient, but the state has to hold everything the model needs from the text. Mamba adds one idea on top. Its update depends on the current token, so the model can choose what to keep and what to forget. The paper calls this selection.

*Diagram, Two ways to remember the text: Same new token, two paths. The transformer keeps every past token, so each new token scans a longer cache. The state-space path keeps one fixed summary, so each token's update stays the same size.*
1. The new token goes to the transformer path, which keeps every earlier token.
2. The same token goes to the state path, which updates one summary.
3. Each new token on the transformer path scans a longer cache.
4. The state path's work stays steady for each new token.

## What do you gain, and what do you give up?

- **Long input: The state-space model wins on cost.** Its work grows in a straight line with the length of the text. Over a whole text, attention's work grows with the square of its length, and each new token scans a longer cache.
- **Exact recall: The transformer keeps every detail.** It can look back at any token in its window. A state-space model can lose a detail that its summary dropped.
- **Track record: Transformers came first.** Transformers have been the main architecture behind deep learning's success in language modelling. The 2024 Mamba-2 paper says state-space models have recently been shown to match or beat them, at small to medium scale.

> "quadratic scaling with respect to the window length" (Gu and Dao, Mamba paper (2023))
>
> "state-space models (SSMs) such as Mamba have recently been shown to match or outperform Transformers at small to medium scale" (Dao and Gu, Mamba-2 paper (2024))

> **Tip:** Asked to pick one, name the cost you can afford: speed on long input, or exact recall across the whole text.

## Why do some newer models mix both?

Some designs use both kinds of layers in one model. The Jamba paper from March 2024 studies how to combine Transformer and Mamba layers, and reports a model that gives high throughput and a small memory footprint.

> "Jamba interleaves blocks of Transformer and Mamba layers, enjoying the benefits of both model families" (Lieber and colleagues, Jamba paper (2024))

*Diagram, A hybrid stack, repeated: A hybrid stack mixes Mamba layers and attention layers in one model. The mix is a design choice, and the paper studies how to combine the two kinds of layers.*
1. The text enters the stack of blocks.
2. The layers interleave: Mamba and attention blocks take turns, as the Jamba paper describes.
3. The stack hands its result to the output.

### How would you choose for a factory's sensor stream?

A factory has 400 compressors, and each one sends a reading every second. That is about 34.6 million readings a day, with no natural end. The team wants an alarm when a machine starts to drift.

- **State-space model: Steady cost, a fixed memory.** It keeps one summary per machine and updates it with each reading. Cost per reading stays flat, so the stream can run for months.
- **Transformer: Memory grows with the stream.** A full transformer keeps every past reading in its cache, so memory and work climb as the stream grows. Capping the window means dropping old readings.

I would start with the state-space model for the alarm. Then I would replay a year of past failures and check which ones it missed, before trusting it. If the team also needs to point at the earlier event behind a flagged reading, a hybrid or a transformer on a recent slice is worth testing.

> **Tip:** Count the missed faults before you celebrate the low cost.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Transformers look back at every token; state-space models keep a fixed-size state.
- [ ] Over a whole text, attention's work grows with length squared; each new token scans a longer cache.
- [ ] A fixed state can lose detail, so exact recall is the trade-off.
- [ ] Name the papers: Mamba (2023), Mamba-2 (2024), and Jamba (2024) for hybrids.
- [ ] Hybrids mix both kinds of layers, as a design choice.
- [ ] Choose by the input, and test on your own data first.

**Common traps**

- Trap: "Mamba replaces transformers everywhere." Better: The Mamba paper says foundation models are almost universally transformers. Mamba trades some exact recall for speed on long input.
- Trap: "A bigger state holds everything." Better: A bigger state holds more, but it is still a fixed summary. Hybrids add attention layers for exact lookup.
- Trap: "Linear cost always means faster." Better: Cost per token helps long input. Speed also depends on the hardware, and Mamba needed a special hardware-aware algorithm to run fast.

## Can you spot the right trade-off?

**Spot the trade-off:** A bank's fraud team must check one customer's five-year transaction history for a single question. Which design keeps every old detail within reach?

A. A state-space model, because its cost per token is flat
B. A transformer, because attention can look back at any earlier transaction
C. A bigger state, because it holds more
D. A lower temperature setting

**Answer:** B. Attention keeps the earlier tokens in its cache, so exact look-up across the history is what it is built for. A flat cost is a real benefit, but a fixed state may have dropped one old transaction.

**Quick check:** A legal team must find one exact clause across 40,000 contracts. Why might it pick a hybrid like Jamba?

A. It needs no memory at all
B. It mixes Mamba and attention layers, aiming for the benefits of both
C. It is always cheaper than both
D. It has no state and no cache

**Answer:** B. The Jamba paper says it interleaves Transformer and Mamba layers to get the benefits of both families. Attention layers help with exact lookup, and the Mamba layers keep long input cheaper. A hybrid still looks back at earlier tokens, so it is not free of memory.

## Can you answer these from memory?

**Flash cards**

- **What does a transformer keep?** Every past token in its cache, which attention looks back at.
- **What does a state-space model keep?** One fixed-size state, updated as each new token arrives.
- **Which model's work grows with the square of the text length?** Attention's, over a whole text. The Mamba paper calls it quadratic-time training.
- **Who wrote the Mamba paper?** Albert Gu and Tri Dao, submitted in December 2023.
- **What does Mamba-2 claim?** That state-space models and transformers are closely related, per the 2024 Mamba-2 paper.
- **What is a hybrid model?** A stack mixing state-space layers and attention layers, like Jamba.

## Likely follow-up questions

- Q068 How does the final hidden state become the next token? https://designervenkat.online/interview/ai/how-does-the-final-hidden-state-become-the-next-token
- Q069 What is an attention sink? https://designervenkat.online/interview/ai/what-is-an-attention-sink
- Q070 How do long-context extensions like RoPE scaling work? https://designervenkat.online/interview/ai/how-do-long-context-extensions-like-rope-scaling-work
- Q073 Walk through one decoder block's forward pass. https://designervenkat.online/interview/ai/walk-through-one-decoder-block-forward-pass
- Q074 What uses memory at inference time? https://designervenkat.online/interview/ai/what-uses-memory-at-inference-time
