What is a sequence model?#
A sequence model can be seen as a function that maps one sequence to another, and the Transformer is the classic example. Models of this kind handle many types of problems and data, including text, images, audio and video. For example:
- Language models: GPT, Claude, Gemini and others
- Vision-language models: LLaVA, and LLaVA-Med, which I covered in another article
- Image models: the many vision transformers
Most of these state-of-the-art models are built on Transformers, which means one architecture can handle problems in different modalities. Take LLaVA: an image is encoded into feature vectors by a vision encoder, text is turned into tokens by a tokenizer, and then a single Transformer-based model can classify, or act as a chatbot. ViT does something similar.
Why are Transformers so convenient?#
A Transformer block consists of multi-head attention, layer norm, feed forward (MLP), dropout and skip connections. Multi-head attention is the most important part.

Once you have a Transformer block, scaling the model up is easy: stack more blocks, or use a Mixture of Experts (MoE, such as Mixtral 8×7B). Model size can be adjusted to whatever you need. This simplicity is one reason Transformer-based models became so popular.
Scaling laws#
Transformers also come with an important rule of thumb, scaling laws. These are empirical observations that let you predict a model’s performance before training, from the dataset size, total compute and model size.
DeepMind’s Chinchilla scaling law is one of the best known. Here can be read as the loss, is the number of model parameters, and is the number of training tokens:
Its main conclusion is that for a fixed compute budget, model size and the number of training tokens should be scaled in equal proportion, at roughly 20 training tokens per parameter. In other words, growing the model and the data together should lower the loss. The figure below is from OpenAI’s earlier scaling-law study: test loss falls steadily as compute, data and parameters increase.

The limits of Transformers#
Transformers have proved powerful across many applications, and their performance improves readily with more resources. But there is a problem: because of attention, the computation grows quadratically with the context length. So the number of input and output tokens is capped, and they are hard to apply directly to long sequences such as video.
There are two mainstream answers for long sequences:
- Retrieval-Augmented Generation (RAG) works like an extension: it retrieves data from an external database and gives a tailored answer. It’s like being allowed a cheat sheet in an exam. You have to organize and condense the information onto the sheet beforehand so you can find what you need during the exam.
- A larger context window lets the model read more tokens at once, for example a context window of a million tokens.
Either way, it is better to cut the computation so that replies over long sequences come faster. Mamba is designed to handle long sequences efficiently in both training and inference.
State space models#
Before Mamba, we need to meet its predecessors. We start with the state space model (SSM), which comes from control theory and describes a dynamic system with differential equations:
Here is the input signal, is the state, and is the output. is the state matrix, which multiplies the state; and are the input and output matrices; acts like a skip connection and is usually set to 0 to drop the term.

The idea of an SSM is to remember past information in a smaller state, a bit like an RNN. That is how it handles long sequences.
Discretization: the recurrent and convolutional views#
The most important step in an SSM is discretization, because it determines how efficient training and inference are. After discretization, the continuous system can be turned into two views.
The first is the recurrent view. Discretizing with the trapezoid (bilinear) method and a step size turns the system into a recurrence:
At inference time we can generate outputs with this formula. The next output depends only on the current state and the current input, so inference is very efficient, just like an RNN.
The second is the convolutional view. Unrolling the recurrence writes the output as a convolution of the input with a kernel:
During training the whole sequence is known, so the convolution can be computed in parallel. This is where SSMs differ from RNNs, which must be trained one step at a time.
The matrix A and HiPPO#
In an SSM, and are learnable, and the choice of is crucial. The convolution kernel needs , which is still expensive, so should ideally be a normal matrix: normal matrices are unitarily diagonalizable, which makes powers easy to compute.
SSMs also initialize with the HiPPO (High-Order Polynomial Projection Operator) matrix. The HiPPO matrix is derived from a specific measure and helps the state remember earlier information; the proofs and experiments are in an earlier paper by the same author. The version used in S4 (HiPPO-LegS) is:
This matrix is not normal, though. The authors decompose it into a normal matrix plus a low-rank matrix, then use their algorithm to speed up both the recurrent and the convolutional computation. Empirically, SSMs initialized with HiPPO perform well, but not when is initialized randomly.
Structured state space models (S4)#
Put all this together and you get S4:
S4 = SSM + HiPPO + Structured matrices
That is, use a state space model, replace with the HiPPO matrix, and learn the parameters with structured matrices and a dedicated algorithm.

S4 has some useful properties:
- Resolution invariance: because the model comes from a continuous system, it can be trained on low-resolution data and used on high-resolution data. This is especially handy for audio, where lowering the sample rate speeds up training.
- Efficient training: training can use the convolution and run in parallel.
- Efficient inference: inference can use the recurrence.
Mamba#
Mamba: Linear-Time Sequence Modeling with Selective State Spaces by Albert Gu and Tri Dao (code on GitHub) sets out to solve the problems that attention brings. It adds two improvements on top of SSMs:
- A selective mechanism, which lets the model decide, based on the input, what to focus on or ignore.
- A hardware-aware algorithm, which designs the computation around the GPU’s memory hierarchy.
According to the paper, Mamba reaches 5× higher inference throughput than Transformers and state-of-the-art results across modalities such as language, audio and genomics. It is also the first attention-free model to match Transformer performance: Mamba-3B outperforms Transformers of the same size.
Notation and discretization#
The Mamba paper uses slightly different notation: is the state, the input, and the output. The continuous and discretized systems are:
Here the discretization is the zero-order hold (ZOH):
Discretization is deeply tied to the continuous-time system, which gives the model extra properties such as resolution invariance and keeps it properly normalized. The figure below shows the state space model inside Mamba. The orange part is the state; extracts features and information from the state, and and handle the input and output.

The first improvement: the selective mechanism#
In S4, , , and don’t depend on the input; the model is time-invariant. Mamba makes , and depend on the input by adding the sequence length as a dimension in the algorithm, so the model becomes time-varying:

where:
, and are linear layers, is softplus (a smooth approximation of ReLU), and the selective mechanism lives mainly in and .
The selective mechanism is related to gating in RNNs. Theorem 1 of the paper looks at a special case: when , , , and , the selective SSM recurrence becomes:
This is very similar to the forget gate in an RNN: a gate decides what to keep, the past state or the current input. The proof is in the paper’s appendix.
The second improvement: a hardware-aware algorithm#
Once the selective mechanism is added, and change over time, so the model can no longer be trained as a convolution. The authors solve this with three classic techniques:
- Kernel fusion
- Parallel scan
- Recomputation
Parallel scan is the most important; it is also used to compute linear RNNs. The real bottleneck is not the arithmetic but the I/O between two kinds of GPU memory: HBM (High Bandwidth Memory) and SRAM (Static Random-Access Memory), and SRAM is much faster than HBM. The main idea is to compute the key steps, discretization and the scan, in SRAM to reduce I/O.
Mamba’s architecture#
H3 is one branch of SSM work. Mamba combines H3 with a gated MLP into a single Mamba block; the gated MLP is often used as an alternative to self-attention with fewer parameters. Mamba blocks can be stacked just like Transformer blocks.

Results#
The first experiment covers scaling laws on The Pile with models from 125M to 1.3B parameters. Mamba follows scaling laws just like Transformers do, with similar results at longer sequence lengths.

The second is efficiency. Thanks to the hardware-aware algorithm, Mamba’s scan is faster than both FlashAttention-2 and convolution, which gives Mamba higher inference throughput, about five times that of a Transformer.

Conclusion#
Mamba starts from state space models, uses HiPPO and S4 to lay the groundwork for remembering long sequences and computing efficiently, adds a selective mechanism so the model can choose what to remember based on content, and wins back efficiency with a hardware-aware algorithm.
For me, the most interesting thing about this paper is that it offers a path beyond attention. Attention-free sequence models are worth following, worth trying for training and inference, and could even be used to build foundation models.
References#
- Albert Gu and Tri Dao. Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv preprint arXiv:2312.00752, 2023.
- Albert Gu, Karan Goel, and Christopher Ré. Efficiently Modeling Long Sequences with Structured State Spaces. arXiv preprint arXiv:2111.00396, 2021.
- Jordan Hoffmann et al. Training Compute-Optimal Large Language Models. arXiv preprint arXiv:2203.15556, 2022.
- Jared Kaplan et al. Scaling Laws for Neural Language Models. arXiv preprint arXiv:2001.08361, 2020.
- Haotian Liu, Chunyuan Li, Qingyang Wu, and Yong Jae Lee. Visual Instruction Tuning. Advances in Neural Information Processing Systems, 2023.
- Alexey Dosovitskiy et al. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. arXiv preprint arXiv:2010.11929, 2020.
- Sebastian Raschka. Build a Large Language Model From Scratch.
- Loïck Bourdois. Introduction to State Space Models (SSM).
- Albert Gu’s talk on S4, 2022.