- Introduced by
- Albert Gu
- Year
- 2023
- Type
- Neural network architecture
- Family
- Deep Learning, Sequence Models, State Space Models
What does it do?
Mamba is a sequence model architecture introduced by Gu and Dao in 2023.
It combines state space models with input-dependent selectivity, so its computational cost grows linearly with sequence length rather than quadratically as in standard attention, while aiming to retain strong performance on language modeling.
The intuition behind the method
Transformers compute attention over all previous tokens, so their cost grows quadratically with sequence length. State space models (SSMs) process a sequence with a recurrent state update whose cost grows linearly, but fixed, input-independent dynamics limit how well they can select which information to keep. Mamba makes the state transition parameters depend on the current input, so the model can decide, token by token, what to store and what to discard. The recurrence is still computed efficiently, which keeps the cost linear in sequence length. The original work reports language modeling performance competitive with transformers under its own experimental setup; exact figures, model sizes and benchmark conditions should be taken from the original paper before drawing conclusions. The trade-off is that the state is a fixed-size summary rather than a record of every earlier token.
History
Gu and Dao introduced Mamba in a preprint in December 2023 (arXiv:2312.08956). The original work reports that state space models with input-dependent selectivity can be competitive with transformers on language modeling while keeping linear cost in sequence length. The approach has since been extended to other domains, including vision.
When to use it
Choose Mamba when sequences are very long and the quadratic cost of attention is a practical barrier, for example in long-document language modeling. It is also a reasonable choice for streaming or online inference, where the per-step cost and memory should stay bounded. Other long-sequence domains, such as genomics, may be worth evaluating, but results there should be checked against established baselines. Do not choose it by default for short inputs, where attention's cost is modest and the efficiency advantage is small. Avoid it when a task depends on exact retrieval of specific earlier tokens, since a fixed-size state compresses history. If you need a mature ecosystem of pretrained models and tooling, transformers are usually easier to adopt.
Limitations and considerations
- Selectivity makes the dynamics time-varying, so the fast convolutional training used by earlier SSMs no longer applies; an efficient scan implementation is required.
- Compressing the entire history into a fixed-size state makes exact recall of specific earlier tokens weaker than with attention, which retains every position.
- Because the architecture is recent, there are fewer optimized implementations, pre-trained models and established recipes than for transformers.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Computational cost grows linearly with sequence length, which makes very long inputs tractable where full attention is costly.
- Incremental generation keeps a fixed-size recurrent state, so each new token does not require revisiting the keys and values of all earlier tokens.
- Input-dependent state transitions let the model choose per token what to retain, which is the selectivity the architecture is built around.
- The recurrence is formulated so that it can run as a parallel scan on accelerators, which is what makes the linear-cost design practical in training.
Applications
- Language modeling over very long documents, where the cost of full attention is a concern.
- Streaming or online sequence processing, where per-step cost and memory should remain bounded.
- Genomics and other long biological sequences, which may be suitable for evaluation; established baselines should be used for comparison.
How it works
How it works
The input sequence is projected into the model's internal representation. In Mamba, several of the parameters that govern the state update are computed from the input at each position rather than being fixed for all inputs.
At each position, the hidden state is updated recurrently. The state transition, controlled by parameters derived from the current input, determines how much of the previous state is retained and how much of the new input is written into it.
The recurrence is evaluated with a parallel scan rather than a step-by-step loop, which lets training use parallel hardware while keeping the cost linear in sequence length. Because the transitions vary with the input, this scan cannot reuse the fixed convolution kernel used by earlier linear SSMs.
The hidden states are projected to the output dimension, giving a representation at each position that feeds the next layer or a task-specific head.
Common pitfalls
- Initialisation of the state matrix and of the step-size parameter controlling the discretisation affects whether training is stable; poor choices can cause vanishing or exploding gradients. The state size and step-size parameterisation are design choices, so use the settings reported in the original paper as a starting point and monitor training stability.
- Implementing the selective recurrence as a naive Python loop over positions removes the speed advantage of the parallel scan, so the cost can appear much higher than the linear formulation implies.
- For short sequences the efficiency gain is small, so a Mamba model may not outperform a transformer of comparable size on short-context tasks.
Frequently asked questions
How does Mamba achieve linear complexity while maintaining transformer-like expressiveness?
Traditional SSMs use fixed state transitions, which limits their ability to select which information matters for a given input. Mamba makes the state transition parameters depend on the input, so the model can selectively retain or discard information. Because the model still updates a recurrent state rather than comparing all pairs of positions, its cost stays linear in sequence length. Whether this matches transformer performance depends on the task and on the benchmark reported.
What is parallel scanning and why is it important?
A parallel scan is an algorithm for computing cumulative operations over a sequence while combining partial results in a tree-like pattern rather than one step at a time. For Mamba, it allows the recurrent state updates to be computed in parallel across positions during training, which avoids the slow sequential dependency of a plain recurrence. This is what makes the selective recurrence practical on parallel accelerators.
When is Mamba preferable to transformers?
Mamba is most relevant when sequences are very long, when transformer memory and compute become prohibitive, or when processing must be streaming or online with bounded per-step cost. For short inputs, or for tasks that depend on exact retrieval of specific earlier tokens, transformers may remain the stronger choice. The decision depends on sequence length, latency requirements, task type and available compute.
Related methods
Sources
Gu, A., & Dao, C. (2023). Mamba: Linear-time sequence modeling with selective state spaces. arXiv preprint arXiv:2312.08956.
Open publication reference