Mamba lets a state space model choose what to remember, matching Transformers on language with linear cost.
Today's language models use attention, which compares every token with every other token. Double the text and the work grows four times, and the memory keeps growing too. Faster alternatives existed, but they were weaker at language. This paper builds one that matches Transformers on language while costing far less on long inputs.
Mamba: Linear-Time Sequence Modeling with Selective State Spaces. Albert Gu (Carnegie Mellon University) and Tri Dao (Princeton University). arXiv:2312.00752, December 2023 (v2 May 2024). Read it at arxiv.org/abs/2312.00752.
Let the model's memory update depend on the token it is reading, so it can keep what matters and ignore the rest, then write the code so this runs fast on a GPU.
The paper's key sentence: efficient models need a small state, effective models need a state that holds the needed information, so what matters is how well the state compresses the context.
A state space model (SSM) reads a sequence one step at a time and keeps a hidden state , a fixed-size list of numbers that summarises everything so far. At each step it does:
Here is the current input, is the output, and , , are learned numbers. says how much of the old state to keep. says how much of the new input to write in. The state never grows, so each step costs the same, however long the text is.
Toy example (made-up numbers, one-number state). Rule: , inputs where 5 is the important token and 1 is filler. The state goes 2.5, 1.75, 1.375, 1.188. The important 5 fades away and the filler mixes in. Same rule for every token, so the model cannot protect what matters.
Earlier SSMs used the same , for every token (they are "time-invariant"). The paper shows two small tasks they fail. In Selective Copying the model must copy certain tokens and skip noise tokens placed at random positions. In Induction Heads it must spot an earlier pattern and complete it. Both need to look at what a token is.
Mamba makes the step size and the matrices and functions of the current input (small learned linear layers; goes through softplus, a function that keeps it positive). and come from . A large means reset and write the input in. A small means keep the state and ignore the input.
Toy example (one-number state, , , so and ). Same inputs 5, 1, 1, 1. Choose for the important token and for filler. The state goes 4.97, 4.93, 4.89, 4.85. The important value is kept and the filler barely touches it.
The important token arrives with a large step, so 5 is written into the state (state 4.97)
The paper shows that, in the simplest case, this is exactly the gate of an RNN (a recurrent network), which is the same kind of switch.
Fixed-rule SSMs can be computed as one big convolution, which is fast on GPUs. With input-dependent rules that shortcut is gone, so the paper computes the recurrence directly but in a hardware-aware way, using three known techniques:
The paper reports the efficient scan is 40 times faster than a standard implementation.
For length , attention compares about pairs. For 1,000 tokens that is about 1,000,000 pairs, while a recurrence does 1,000 steps. At generation time Mamba keeps a fixed-size state, while a Transformer keeps a cache that grows with the text.
| Model | Parameters | Average accuracy |
|---|---|---|
| Pythia (Transformer) | 2.8B | 59.1 |
| Mamba | 2.8B | 63.3 |
| Pythia (Transformer) | 6.9B | 61.7 |
Mamba-2.8B beat a Transformer of the same size and also one more than twice as large.
The paper also reports results on DNA and audio modelling, and quality that kept improving with context up to 1 million tokens.
These results show Mamba is competitive at the sizes tested. They do not show how it compares to the biggest models.