AI State of the Art
Ecosystem/Annotated Foundational Papers/12 min
Section 304

"Mamba: Linear-Time Sequence Modeling" (2023)

Listen as a podcast β†’

The Problem: Quadratic Attention Kills Long Sequences

The Transformer's attention mechanism has an Achilles' heel: its complexity is O(n^2). For each token, it must look at all other tokens. With 1,000 tokens, that's 1 million operations. With 1 million tokens? That's 1 trillion elements in the attention matrix. Impossible in practice.

That's why Transformer context windows are limited. Even with Flash Attention (which optimizes the constants), complexity remains quadratic. Doubling context length costs 4x more in compute.

Plot titled Attention vs RNN Complexity with d equals 512. The horizontal axis is sequence length n from 0 to 2000, the vertical axis is operations in millions from 0 to over 2000. The red attention curve, n squared times d, starts below the blue recurrent line, n times d squared, crosses it at the marked point n equals d equals 512, then climbs to about 2000 million operations at n equals 2000, while the blue line only reaches 520 million. The region where attention is more expensive is shaded pink.
The whole motivation for the Mamba paper sits in the gap between these two curves. While sequences stay short, attention is actually cheaper than a recurrence. Past the crossover point, the red curve takes off and the blue one stays a straight line, that straight line is what Mamba is trying to recover without losing quality.Source : Michael Brenndoerfer, Attention Complexity: Quadratic Scaling, Memory Limits and Efficient Transformers

Mamba (Gu and Dao, December 2023) proposes a radical alternative: O(n) complexity, linear. Double the length? It costs just 2x. Theoretically, unlimited context becomes conceivable.

The promise: attention without attention

Mamba rivals the Transformer in performance, but without an attention mechanism. The fundamental debate shifts: the Transformer may not be the only viable architecture for LLMs. This is the first time since 2017 that a credible alternative has emerged.

State Space Models (SSM): The Theoretical Foundation

Mamba is built on State Space Models, a family of models inspired by control theory in engineering (the discipline that models dynamic systems like electrical circuits or mechanical systems).

The core idea is simple: instead of explicitly looking at all previous tokens (as attention does), you maintain a hidden state h(t)h(t) that summarizes all history. This state evolves according to two equations:

hβ€²(t)=Aβ‹…h(t)+Bβ‹…x(t)h'(t) = A \cdot h(t) + B \cdot x(t)

y(t)=Cβ‹…h(t)+Dβ‹…x(t)y(t) = C \cdot h(t) + D \cdot x(t)

The first equation says: "the hidden state updates based on itself (memory) and the current input." The second says: "the output depends on the hidden state and the input."

The matrices AA, BB, CC, DD are the model's learned parameters.

For an analogy: imagine you're reading a very long book. The Transformer's attention is like rereading the entire book at every new sentence. The SSM is like taking notes as you go: you keep a summary of what you've read (the hidden state), and you update it at each page.

S4 (Gu et al., 2021) was the ancestor. It showed that SSMs could be competitive on certain tasks. But classic SSMs had a major problem: the parameters AA, BB, CC were fixed, identical regardless of input. The model couldn't "choose" to retain certain information over others.

Mamba's Innovation: Selectivity

This is where Mamba changes the game. Instead of fixed parameters, Mamba makes the parameters input-dependent:

B(x)B(x), C(x)C(x), Ξ”(x)\Delta(x) become functions of the current input.

Concretely, this means the model can selectively decide what to retain and what to forget in its hidden state. An important token? The model "opens the gate" to integrate it. An irrelevant token? The model lets it pass without modifying its state.

It's conceptually similar to the gating mechanism of LSTMs (the forget/input/output gates), but within the SSM framework. You can see it as a form of implicit attention: the model dynamically decides what matters, but without the quadratic cost of explicit attention.

Figure 1 of the Mamba paper. The input token x_t goes through a projection labelled selection mechanism which produces the parameters B_t, C_t and Delta_t; Delta_t is used to discretize the matrix A; the hidden state h_(t-1) is updated to h_t then read out to produce the output y_t. A memory pyramid at the bottom right indicates that the computation is kept in the GPU's fast SRAM rather than in HBM.
The paper's reference figure. It packs both ideas described above into one picture: the blue projection at the bottom left builds B, C and Delta from the current token (that is selectivity) and the pyramid on the right is the reminder that this computation stays inside the GPU's fast memory. Without that second half, selectivity would be far too slow to be usable.Source : Mamba paper (Gu and Dao, figure 1), via Aman's AI Journal

Concrete Architecture

Each Mamba layer follows this sequence:

  1. Linear expansion: project the input into a larger space
  2. Causal 1D convolution: capture local dependencies
  3. Selective SSM: the heart of Mamba, sequence processing with input-dependent parameters
  4. Gate multiplication: information flow control (like in gated architectures)
  5. Output projection: return to the original dimension
Diagram of a Mamba block. The input splits into two branches, each going through an expanding linear projection. The main branch passes through a 1D convolution, a SiLU activation, then the selective SSM. The side branch passes through a SiLU activation and acts as a multiplicative gate. The two branches are combined by a multiplication, then a projection returns to the original dimension.
The five steps listed above, drawn out. The left branch does the sequence work (local convolution then selective SSM) while the right branch is just a tap: its output is multiplied with the first to meter what gets through. Stacked, this block is what replaces a Transformer's stack of attention blocks.Source : Maarten Grootendorst, A Visual Guide to Mamba and State Space Models

Key properties:

  • O(n) in time and memory complexity
  • No growing KV-cache: unlike Transformers that store keys/values for all previous tokens, Mamba has a fixed-size hidden state
  • Constant cost per token at inference: regardless of context length, generating the next token always costs the same
The hardware challenge: parallel scan

Making parameters input-dependent breaks a key mathematical property of S4: the ability to reformulate the SSM as a convolution (which enabled parallelism). Mamba solves this problem with a parallel scan algorithm implemented in CUDA, optimized for the GPU memory hierarchy (IO-aware, like Flash Attention). This is what makes Mamba fast in practice, not just in theory.

Results: Mamba Delivers on Its Promises

In performance:

  • Mamba-3B outperforms Transformers of the same size on several benchmarks
  • Rivals Transformers that are 2x bigger in parameter count
  • Particularly good on long sequences (expected given the linear complexity)
  • Good at language modeling, audio, and genomics

In speed:

  • 5x more throughput than a Transformer of the same size
  • At inference, cost per token remains constant regardless of context length. A Transformer, however, progressively slows down due to the growing KV-cache

To put this in perspective: a Transformer processing 100 tokens per second at the start of a sequence can drop to 20 tokens per second after 100K tokens of context. Mamba maintains 100 tokens per second throughout.

The Big Debate: Mamba vs Transformer

This is THE architectural question of the moment in AI.

Arguments in favor of Mamba:

  • Linear complexity: scales to very long sequences
  • No KV-cache: fixed memory, no degradation with context
  • Fast and constant inference
  • Potential for contexts of millions of tokens

Arguments in favor of the Transformer:

  • The attention mechanism is simple, well understood, and massively optimized (Flash Attention, PagedAttention, etc.)
  • The optimization ecosystem is mature (years of CUDA engineering)
  • In-context learning seems better with explicit attention (debate ongoing)
  • Scaling beyond 70B parameters is less validated for Mamba
  • Empirical results on very large models still favor Transformers
The verdict: not yet settled

Mamba is promising, but hasn't replaced the Transformer for very large models. Beyond 70B parameters, nearly all production models remain Transformers. The most likely direction for the future? Hybrid architectures: Mamba for layers that process long context, attention for layers that demand precision. Jamba (AI21) and RecurrentGemma (Google) are already exploring this path.