State Space Models offer linear-time sequence modeling with content-aware selective filtering, challenging Transformers for long-context inference.
Why This Matters
State Space Models (SSMs) provide a principled alternative to Transformers for long-sequence modeling. In production systems handling long contexts (e.g., code generation, genomic analysis), Transformer attention's quadratic cost becomes a bottleneck. Mamba achieves linear-time inference with constant-memory state, making it viable for million-token contexts where attention-based models are prohibitively expensive.
Core Idea
SSMs originate from continuous-time control theory: a latent state evolves over time driven by input, and observations are linear projections of that state. Mamba's key innovation is making the SSM parameters input-selective — the model learns to gate which information enters and exits the state, mimicking attention's ability to focus on relevant tokens without the
O(n2)O(n^2)O(n2)
cost.
Technical Details
The continuous-time SSM is defined as:
x′(t)=Ax(t)+Bu(t),y(t)=Cx(t)+Du(t)
x'(t) = Ax(t) + Bu(t), \quad y(t) = Cx(t) + Du(t)
x′(t)=Ax(t)+Bu(t),y(t)=Cx(t)+Du(t)
where
x(t)∈RNx(t) \in \mathbb{R}^Nx(t)∈RN
is latent state,
u(t)u(t)u(t)
is input, and
A∈RN×NA \in \mathbb{R}^{N \times N}A∈RN×N
,
B∈RN×1B \in \mathbb{R}^{N \times 1}B∈RN×1
,
C∈R1×NC \in \mathbb{R}^{1 \times N}C∈R1×N
. Using zero-order hold discretization with step
Δ\DeltaΔ
:
Aˉ=exp(ΔA),Bˉ=(ΔA)−1(exp(ΔA)−I)⋅ΔB
\bar{A} = \exp(\Delta A), \quad \bar{B} = (\Delta A)^{-1}(\exp(\Delta A) - I) \cdot \Delta B
Aˉ=exp(ΔA),Bˉ=(ΔA)−1(exp(ΔA)−I)⋅ΔB
The recurrent update becomes:
xk=Aˉxk−1+Bˉuk,yk=Cxk
x_k = \bar{A}x_{k-1} + \bar{B}u_k, \quad y_k = Cx_k
xk=Aˉxk−1+Bˉuk,yk=Cxk
Mamba's selective mechanism makes
BBB
,
CCC
, and
Δ\DeltaΔ
input-dependent:
Bk=LinearB(xk),Ck=LinearC(xk),Δk=softplus(LinearΔ(xk))
B_k = \text{Linear}B(x_k), \quad C_k = \text{Linear}_C(x_k), \quad \Delta_k = \text{softplus}(\text{Linear}\Delta(x_k))
Bk=LinearB(xk),Ck=LinearC(xk),Δk=softplus(LinearΔ(xk))
The parallel scan algorithm computes this recurrence in
O(nlogn)O(n \log n)O(nlogn)
during training. Inference is
O(1)O(1)O(1)
O(1) per token with fixed state size N
NNN
, yielding constant-memory decoding regardless of sequence length.
How It Works
Project input: Map token
uku_kuk
to expanded dimension
D⋅ND \cdot ND⋅N
.
Generate selective parameters: Compute input-dependent
BkB_kBk
,
CkC_kCk
,
Δk\Delta_kΔk
from
uku_kuk
.
Discretize: Convert continuous
(A,B)(A, B)(A,B)
to discrete
(Aˉk,Bˉk)(\bar{A}_k, \bar{B}_k)(Aˉk,Bˉk)
using
Δk\Delta_kΔk
.
Recurrent scan: Apply parallel scan (training) or sequential update (inference) to compute hidden states
xkx_kxk
.
Output projection: Compute
yk=Ckxky_k = C_k x_kyk=Ckxk
, then project through gating (SiLU) to output dimension.
Key Insights
Selectivity is essential: Non-selective SSMs (S4) cannot do in-context retrieval; making
B,C,ΔB, C, \DeltaB,C,Δ
input-dependent enables content-aware filtering.
Diagonal + low-rank structure on
AAA
enables
O(n)O(n)O(n)
recurrence; Mamba uses diagonal
AAA
matrices exclusively.
Hardware-aware design: The scan kernel is IO-bound, not compute-bound — Mamba's CUDA kernel fuses discretization, scan, and output projection to minimize memory reads.
Linear decoding cost: Unlike KV-cache which grows linearly, SSM state is fixed-size
O(ND)O(ND)O(ND)
, making generation memory-constant.
Sources
- Gu, A. & Dao, T. "Mamba: Linear-Time Sequence Modeling with Selective State Spaces." arXiv:2312.00752 (2023). https://arxiv.org/abs/2312.00752
- Gu, A. & Dao, T. "Mamba-3: Improved Sequence Modeling using State Space Principles." arXiv:2603.15569 (2026). https://arxiv.org/abs/2603.15569