TL;DR
Mamba is a sequence model built from a state space model whose step size and input and output projections are computed from the current token. That lets it decide, token by token, what to keep in its fixed-size state and what to forget. A hardware-aware scan makes the recurrence fast on GPUs, and the authors report Transformer-level language modeling with cost that grows linearly in sequence length and no attention layers.
Where you'll see it: long-context language, DNA and audio models, and the family of later architectures that reuse selective state space layers.
The problem
By 2023, most foundation models were Transformers. Attention lets every position read every other position inside a context window, which is powerful. It has two costs. Training scales quadratically with the window length, and autoregressive inference keeps a key-value cache that grows with every token generated. The cache is why long-context generation is slow and memory-hungry.
Many cheaper alternatives existed: linear attention, gated convolutions, recurrent models and structured state space models (SSMs). They scale linearly or near-linearly. The paper's diagnosis is that they did not match attention on discrete, information-dense data such as text. The authors identify the cause as a lack of content-based reasoning. A standard SSM applies the same dynamics at every step. Its update matrices do not depend on what the current token is, so it cannot decide to store one token and ignore the next.
The paper makes this concrete with two synthetic tasks. In Selective Copying, the tokens to memorise are scattered at random positions among filler tokens. A model with fixed dynamics can solve plain copying by counting positions, but it cannot solve the randomised version without looking at content. In induction heads, the model must notice that a token it saw earlier was followed by some other token, and reproduce that pattern when the first token reappears.
Key idea
Think of the model's state as a notebook with a fixed number of pages. A time-invariant SSM writes every incoming token with the same pen pressure, so important details get overwritten by filler. Mamba lets the current token set the pressure: strong writes for tokens worth keeping, and near-zero writes for filler, while the old notes stay put.
flowchart LR X[Current token x_t] --> P[Compute step size, B and C from x_t] P --> S[Update state h_t: forget old, write new] S --> Y[Output y_t from C and h_t] S -->|carried to next token| S
The state stays a fixed size, so inference needs constant memory per token and no growing cache. The price is that the dynamics now change over time. That breaks the convolution trick that earlier SSMs used for fast training, so the paper also needs a new algorithm, described below.
The method
The SSM recurrence
A continuous-time linear SSM maps an input signal to an output through a hidden state of size :
To run it on a discrete token sequence, the paper uses a step size and the zero-order hold (ZOH) rule. This gives discrete matrices
and the recurrence
Here is a learned state matrix, and are learned projections, and is the state after reading token . In a time-invariant model all of are fixed, so and are the same at every step. The recurrence can then be unrolled as a convolution with a fixed kernel . See State Space Model for the background.
Selection: making the parameters depend on the input
Mamba makes , and functions of the current input :
The softplus keeps the step size positive. The matrix stays input-independent. The paper notes that making selective as well would change the model only through , so selecting already covers it. The recurrence now has time-varying coefficients, and .
Why the step size is the key knob
The paper's simplest case is one state dimension with and . Then and , so
This is the gate of a recurrent network. The retained fraction and the written fraction sum to one. A large wipes the old state and writes the current input. A small keeps the old state and barely writes the input. The paper's Theorem 1 generalises this to the ZOH discretisation with input-dependent gates. It is also why the authors find the most important of the three selective parameters in their ablation. The connection is to Gated Recurrent Unit and the gating in Recurrent Neural Networks.
Take and an input , and compare two step sizes.
- Large step, : , so and .
- . The state is dominated by the new input.
- Small step, : , so and .
- . The state is mostly the old value, and the input is a small correction.
Interpretation: the same input can either overwrite memory or be nearly ignored, depending on a value the model computes from that input. These numbers are analytic values computed by hand for this example, not output from the paper.
The hardware-aware scan
Selection creates a practical problem. The natural implementation expands the state to size for batch , length , channel count and state size , and writes it to GPU high-bandwidth memory (HBM). That tensor is times larger than the input. The paper's answer uses three classic techniques.
- Kernel fusion. The kernel loads the small parameters , , and from HBM into fast on-chip SRAM. It discretises and runs the recurrence there, and writes only the output of size back to HBM.
- Parallel scan. The recurrence looks sequential, but it is an associative scan, so a work-efficient parallel scan algorithm can compute it in parallel across the length axis.
- Recomputation. The intermediate states are not stored for the backward pass. They are recomputed from the inputs when needed. The authors say this gives the fused layer the same memory footprint as an optimised Transformer with FlashAttention, see GPU Memory Hierarchy for why memory traffic dominates here.
The paper reports that the scan is up to 3 times faster than previous SSM implementations on A100 GPUs, and scales linearly in sequence length, against pseudo-linear scaling for convolution-based SSMs.
The Mamba block
The selective SSM is placed inside a simpler block that replaces the separate attention and MLP blocks of a Transformer with one repeated unit. The input is projected up by an expansion factor , one branch passes through the selective SSM, and the other branch is a gate with a SiLU activation. The product is projected back down. The authors say the design is inspired by gated MLPs in the SwiGLU style, and the blocks are stacked with standard normalization and Residual Connections between them. The paper also adds an optional Layer Normalization layer inside the block.
Parameter counts are the reason for the choice of . Each block has about parameters, most of them in the linear projections. The paper stacks two blocks to match the parameters of a Transformer layer with its attention and MLP. The SSM parameters themselves are a small share.
Results
The paper reports results from its own training runs, on the Pile and on other datasets. The numbers below are the paper's, not reproduced here. Zero-shot accuracy and Pile perplexity come from Table 1, where each Mamba model is compared with the Pythia model of similar size, all trained on 300 billion tokens with the same tokenizer.
| Model | Pile perplexity (lower is better) | Average zero-shot accuracy |
|---|---|---|
| Pythia-160M | 29.64 | 40.6 |
| Mamba-130M | 10.56 | 44.7 |
| Pythia-410M | 9.95 | 48.2 |
| Mamba-370M | 8.28 | 50.0 |
| Pythia-1.4B | 7.51 | 55.2 |
| Mamba-1.4B | 6.80 | 59.7 |
| Pythia-2.8B | 6.73 | 59.1 |
| Mamba-2.8B | 6.22 | 63.3 |
Each Mamba model beats the Pythia model closest in size (130M against 160M, 370M against 410M) on both columns, and Mamba-1.4B, at half the size, has a slightly higher average accuracy than Pythia-2.8B (59.7 against 59.1) with a close perplexity (6.80 against 6.73). The paper's introduction describes the largest model as "Mamba-3B", while Table 1 lists the 2.8B row. The table is used here.
Other results, each as the paper reports them:
- Synthetic tasks. Selection solves Selective Copying, while gated architectures without selection only partly improve. On induction heads, the model trained at length 256 generalises perfectly to sequences of up to about one million tokens. The paper describes this as roughly 4000 times longer than the training length. No other method in the comparison went beyond 2 times.
- DNA. At about 40 million parameters, Mamba matches a Transformer++ recipe and HyenaDNA with roughly 3 to 4 times fewer parameters. Its perplexity keeps improving as context grows to one million, while HyenaDNA gets worse.
- Audio. On the SC09 speech generation benchmark, a small Mamba model beats GAN and diffusion baselines on the automated fidelity metrics reported.
- Speed. The fused scan is faster than FlashAttention-2 for sequence lengths beyond about 2 thousand. The text says it is 20 to 40 times faster than a standard PyTorch scan, and the Figure 12 caption says 40 times. Inference throughput is 4 to 5 times that of a Transformer of similar size, because there is no key-value cache to limit the batch size.
Why it mattered
The paper's own claim is narrower than "attention is obsolete": a non-attention model matched a strong Transformer recipe on language at the scales tested (up to about 3 billion parameters), with linear-time training and constant-memory inference. That made subquadratic architectures a serious option for foundation models. Later work built on selective state spaces, including Mamba-2 (2024) and hybrid models that mix Mamba blocks with attention layers.
Reading guide
Read the paper in this order. Each prerequisite is linked where it first matters.
- Section 1, Introduction. Transformer and Attention Mechanism for the cost of the key-value cache and the quadratic training cost.
- Section 2, State Space Models. State Space Model for the continuous system and discretisation. Recurrent Neural Network for the recurrent view, and the convolution view that follows from it.
- Section 3.1, Selection as compression. Attention Mechanism and Recurrent Neural Network again, to see the trade-off between a large cache and a finite state.
- Sections 3.2 and 3.5, Selective SSMs and gating. Selective State Space Model for the definition the paper introduces, and Gated Recurrent Unit for the gating connection in Theorem 1.
- Section 3.3, Hardware-aware scan. GPU Memory Hierarchy for why keeping the state in SRAM matters.
- Section 3.4, Architecture. Residual Connection and Layer Normalization for the surrounding block.
- Section 4, Experiments. No new prerequisites. Perplexity and zero-shot accuracy are used as they are.
Check yourself
QuestionIn plain words, what does it mean for an SSM to be time-invariant, and why does that stop it from solving Selective Copying?Show answer
A time-invariant SSM uses the same and at every step, so it writes and forgets every token in the same way. Selective Copying places the tokens to remember at random positions among filler. The model cannot tell a token to keep from filler by its content, so it cannot choose correctly. Making , and depend on the input lets the model decide per token.
QuestionTake one state dimension with , , , input and step size . What is ?Show answer
and . Then . The state now holds a little over half of the new input.
QuestionWhy does the selective scan avoid writing the expanded state to GPU memory?Show answer
Writing that tensor to HBM costs memory traffic about times the size of the input, and in most operations memory bandwidth, not arithmetic, sets the speed. The fused kernel loads the small parameters into SRAM, discretises and scans there, and writes only the output back. The intermediate states are recomputed in the backward pass instead of being stored.
QuestionYou need to model 1-million-token DNA sequences on one GPU. Which result from the paper supports trying Mamba, and what would you check before trusting it?Show answer
The paper reports that Mamba's DNA perplexity improves with context up to one million, while HyenaDNA's gets worse, and that its cost grows linearly in length. Before trusting this, check the paper's own setting: the scaling results use the HG38 genome with models of about 40 million parameters. Reproduce the comparison on your own data and hardware, because the reported gains are for the paper's pretraining setup and dataset.
Sources
- Gu, A. and Dao, T. (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. https://arxiv.org/abs/2312.00752
- Gu, A., Goel, K., Re, C. (2021). Efficiently Modeling Long Sequences with Structured State Spaces. https://arxiv.org/abs/2111.00396
- Dao, T. et al. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. https://arxiv.org/abs/2205.14135
- Vaswani, A. et al. (2017). Attention Is All You Need. https://arxiv.org/abs/1706.03762