# Mamba: Linear-Time Sequence Modeling with Selective State Spaces

> Makes state space model parameters depend on the input, so a recurrent model can choose what to remember. A hardware-aware scan keeps it linear-time, and Mamba-2.8B matches Transformers twice its size with 5× faster inference.

- arXiv: https://arxiv.org/abs/2312.00752
- Authors: Albert Gu, Tri Dao
- Published: 2023-12-01
- Topic: Architectures
- Visual carousel: https://instaxiv.com/p/2312.00752
- Explanation by InstaXiv, a secondary educational summary. Cite the original paper for exact results. InstaXiv is not affiliated with or endorsed by arXiv or Cornell University.

## Linear-time sequence models that finally match Transformers on language

**5×** higher inference throughput than Transformers

## Fast recurrent models could not choose what to remember

**Structured SSMs:** Linear-time and parallelizable, but their dynamics are fixed for every input. They cannot decide to ignore filler or focus on a key token.

**Selective SSMs:** Make the step size and input and output projections functions of the current token. The model can now reset, skip or keep state based on content.

Selection gives recurrence the content-awareness attention gets for free.

## Input-dependent parameters, computed in fast memory

![Figure 1](https://cdn.instaxiv.com/figures/2312.00752/figure1.webp)

B, C and the step size Δ are projected from each input, so the state update is content-aware. The expanded state lives only in GPU SRAM, never in HBM. (Figure 1)

## A recurrence whose dynamics depend on the input

$$
\begin{gathered} \amber{\bar{A}_t} = \exp(\Delta_t A) \\ h_t = \amber{\bar{A}_t}\,h_{t-1} + \cyan{\bar{B}_t}\,x_t \\ y_t = C_t\,h_t \end{gathered}
$$

The step $\Delta_t$ is computed from $x_t$: a large step resets the state to focus on the current token, a small one preserves context.

- $\bar{A}_t$: State transition, discretized with step $\Delta_t$
- $\bar{B}_t$: How much of the current input enters the state
- $C_t$: Readout of the state, also chosen per token
- $\Delta_t$: Learned step size: the selection gate

## Three ideas make selective SSMs fast

1. **Kernel fusion**: Load parameters into SRAM, discretize and run the recurrence there, and write only outputs back to HBM.
2. **Parallel scan**: Input-dependence rules out convolutions, so a work-efficient parallel scan computes the recurrence in linear time.
3. **Recompute in backward**: Intermediate states are recomputed rather than stored, matching the memory of a FlashAttention Transformer.

## One uniform block replaces attention and the MLP

![Figure 3](https://cdn.instaxiv.com/figures/2312.00752/figure4.webp)

The H3 block and the gated MLP merge into a single Mamba block, stacked uniformly. No attention layers and no separate MLP blocks. (Figure 3)

## Matches a Transformer twice its size

| Method | Avg. zero-shot accuracy |
| --- | --- |
| Pythia-2.8B | 59.1 |
| Pythia-6.9B | 61.7 |
| GPT-J-6B | 63.0 |
| **Mamba-2.8B (+4.2 vs Pythia)** | **63.3** |

_Average over LAMBADA, HellaSwag, PIQA, ARC-E, ARC-C and WinoGrande (%), Table 3._

## Open questions after Mamba

- **Continuous signals trade off**: Selection helps discrete data like text and DNA, but can hurt continuous signals such as audio waveforms.
- **Scale tested to 3B**: Language results stop near 3B parameters; whether the edge holds at frontier scale was open.

Best used when: Long sequences or high-throughput generation, where attention's quadratic cost and KV cache hurt.

## Key takeaways

- Input-dependent state space parameters let recurrent models select what to keep, as attention does.
- A hardware-aware scan makes selection fast, with linear scaling in sequence length.
- Mamba layers now appear in hybrid models alongside attention.

## Links

- arXiv: https://arxiv.org/abs/2312.00752
- PDF: https://arxiv.org/pdf/2312.00752
- Code: https://github.com/state-spaces/mamba

## Citation

```bibtex
@article{gu2023mamba,
  title   = {Mamba: Linear-Time Sequence Modeling with Selective State Spaces},
  author  = {Gu, Albert and Dao, Tri},
  journal = {arXiv preprint arXiv:2312.00752},
  year    = {2023},
  url     = {https://arxiv.org/abs/2312.00752}
}
```
