6.1.11Scaling & Efficient Architectures

State-space models (Mamba, S4)

2,625 words12 min readdifficulty · medium2 backlinks

The State-Space Foundation

WHY continuous? Even though we process discrete tokens, the continuous formulation gives us powerful tools from control theory and signal processing. We'll discretize it carefully.

From Continuous to Discrete: The Key Step

For neural networks processing discrete sequences, we need to discretize the continuous dynamics. Using the zero-order hold assumption (input is constant between timesteps), the exact solution is:

WHY this specific discretization? The matrix exponential eAΔe^{\mathbf{A}\Delta} is the exact solution to the ODE for constant input. Using simpler methods (like Euler) would accumulate errors over long sequences.

HOW to compute eAΔe^{\mathbf{A}\Delta}? For general matrices this is expensive, but S4 uses structured matrices (next section) where it becomes efficient.

S4: Structured State Spaces

The original SSM approach hit a wall: the state dimension NN needs to be large (thousands) to have sufficient memory capacity, but this makes the A\mathbf{A} matrix huge and expensive.

The Convolution View

Here's the crucial dual perspective: the SSM can be viewed as either:

  1. Recurrent: hk=Aˉhk1+Bˉxkh_k = \bar{\mathbf{A}} h_{k-1} + \bar{\mathbf{B}} x_k (for generation)
  2. Convolutional: y=Kˉxy = \bar{\mathbf{K}} \ast x (for training)

The SSM convolution kernel is:

WHY convolutional view? Convolutions can be computed in O(nlogn)O(n \log n) using FFT, and they parallelize perfectly across the sequence (unlike the recurrent form). Training uses convolution, inference uses recurrence.

HOW does DPLR structure help? With diagonal-plus-low-rank A\mathbf{A}, the powers Aˉk\bar{\mathbf{A}}^k and kernel Kˉ\bar{\mathbf{K}} can be computed efficiently using the Cauchy kernel trick and FFT. The details involve complex analysis, but the result is O(NlogN)O(N \log N) computation per layer.

Mamba: Selective State Spaces

S4 was a breakthrough but had a key limitation: the matrices Aˉ,Bˉ,C\bar{\mathbf{A}}, \bar{\mathbf{B}}, \mathbf{C} are input-independent. The same filter is applied to every token.

WHY selective? Consider processing a document: the model might want to remember a name mentioned in the first sentence for the entire sequence, but forget filler words immediately. Fixed SSMs can't make this choice—Mamba can.

HOW is this computed efficiently? This breaks the convolution view (kernel is now input-dependent), so we can't use FFT. Mamba uses specialized hardware-aware kernels that fuse the recurrent computation with careful memory management, achieving speed comparable to FFT-based S4 despite being recurrent.

The Mamba Architecture Block

A full Mamba block combines the selective SSM with gating and projections:

Training vs Inference Modes

Connections to Other Models

  • Transformers: SSMs are an alternative to attention, trading off flexibility for efficiency
  • RNNs and LSTMs: SSMs are the "spiritual successor" with better long-term memory and paralelization
  • Convolutional Neural Networks: The convolution view links SSMs to time-series convolutions
  • Linear Attention: Some linear attention variants are mathematically equivalent to certain SSM configurations
  • Mixture of Experts: Mamba can be combined with MoE for even greater efficiency
Recall Explain to a 12-year-old

Imagine you're listening to a long song and trying to remember it. Your brain doesn't remember every single sound equally—some parts (the chorus, the melody) you remember strongly, while other parts (a quiet drum beat) you forget quickly.

A Transformer is like writing down every single sound on a huge piece of paper and constantly looking back at the whole paper. If the song is really long (like 100,000 notes!), your paper becomes gigantic and it takes forever to check everything.

A State-Space Model (SSM) is like having a small notebook where you keep summary notes. When you hear a new sound, you update your notebook based on the old notes and the new sound. Your notebook stays small no matter how long the song is!

S4 made the notebook super organized (structured) so you can flip through it really fast. Mamba made it even smarter: when an important sound comes (like the singer saying "I love you"), the notebook says "Write this in pen!" so it stays there a long time. When boring sounds come, it says "Write in pencil" so they fade away quickly.

This is why SSMs can handle super duper long sequences (like reading an entire Harry Potter book at once!) while Transformers would get tired halfway through.


Flashcards

#flashcards/ai-ml

What are the two key equations defining a continuous state-space model? :: h(t)=Ah(t)+Bx(t)h'(t) = \mathbf{A}h(t) + \mathbf{B}x(t) (state evolution) and y(t)=Ch(t)+Dx(t)y(t) = \mathbf{C}h(t) + \mathbf{D}x(t) (observation)

What is the time complexity of SSMs compared to Transformers for sequence length nn?
SSMs are O(n)O(n) or O(nlogn)O(n \log n), while Transformers are O(n2)O(n^2) due to the attention matrix
How do we discretize a continuous SSM for neural networks?
Define Aˉ=eAΔ\bar{\mathbf{A}} = e^{\mathbf{A}\Delta} and Bˉ=(A1(eAΔI))B\bar{\mathbf{B}} = (\mathbf{A}^{-1}(e^{\mathbf{A}\Delta} - \mathbf{I})) \mathbf{B}, then use recurrence hk=Aˉhk1+Bˉxkh_k = \bar{\mathbf{A}} h_{k-1} + \bar{\mathbf{B}} x_k
What structural constraint does S4 place on the A\mathbf{A} matrix?
S4 uses diagonal-plus-low-rank (DPLR) structure: A=Λpq\mathbf{A} = \Lambda - \mathbf{p}\mathbf{q}^*, enabling efficient computation via FFT
What are the two computational views of an SSM?
Recurrent view (for inference): hk=Aˉhk1+Bˉxkh_k = \bar{\mathbf{A}} h_{k-1} + \bar{\mathbf{B}} x_k, and Convolutional view (for training): y=Kˉxy = \bar{\mathbf{K}} \ast x
What is the SSM convolution kernel Kˉ\bar{\mathbf{K}}?
Kˉ=(CBˉ,CAˉBˉ,CAˉ2Bˉ,,CAˉL1Bˉ)\bar{\mathbf{K}} = (\mathbf{C}\bar{\mathbf{B}}, \mathbf{C}\bar{\mathbf{A}}\bar{\mathbf{B}}, \mathbf{C}\bar{\mathbf{A}}^2\bar{\mathbf{B}}, \ldots, \mathbf{C}\bar{\mathbf{A}}^{L-1}\bar{\mathbf{B}})—the impulse response of the system
What is the key innovation of Mamba over S4?
Mamba makes SSM parameters input-dependent (selective): Bˉ(x),C(x),Δ(x)\bar{\mathbf{B}}(x), \mathbf{C}(x), \Delta(x) depend on the input token, allowing the model to choose what to remember
How does Mamba's selectivity mechanism work?
The timestep parameter Δ(x)\Delta(x) is computed per-token via a learned linear layer; large Δ\Delta gives slow decay (long memory), small Δ\Delta gives fast decay (quick forgetting)
Why can't Mamba use the FFT-based convolution trick from S4?
Because the kernel is now input-dependent (different for each sequence), breaking the time-invariance required for FFT convolution
What is the per-token computational cost of SSM inference vs Transformer inference?
SSMs: O(1)O(1) (just update fixed-size state). Transformers: O(n)O(n) (attend to all previous tokens)
What initialization strategy helps SSMs maintain long-term memory?
HiPPO initialization, which sets the eigenvalues of A\mathbf{A} to specific values that create stable memory channels
At what sequence length do SSMs typically become faster than Transformers?
Around 4K-8K tokens, depending on hardware and implementation details; for <2K tokens, optimized Transformers (FlashAttention) are often faster

Concept Map

motivates

achieves

enables

models as

state evolves via

needs

uses

gives exact

yields

large N is costly

makes efficient

extended by

Transformers O n squared

State-Space Models

Linear Time O n

Very Long Sequences 100K+

Continuous Dynamical System

h prime = A h + B x

Discretization

Zero-Order Hold

Matrix Exponential e^ A delta

Discrete Recurrence h_k = A_bar h + B_bar x

S4 Structured Matrices

Mamba Selective SSM

Hinglish (regional understanding)

Intuition Hinglish mein samjho

State-space models (SSMs) ek bahut hi powerful alternative hai Transformers ke liye, especially jab sequence bohot lambi ho. Socho aise—Transformer ko puri sequence yad rakhni padti hai har step pe, jaise tum ek bohot badi book ko har page khol ke dekhte raho. Jitni badi book, utna zyada kaam. But SSMek smart notebook rakhta hai jo continuously update hota rehta hai. Sirf ek chhoti si "state" yad rakhni hai, aur har naye token ke sath usko update kar lo. Yeh concept signal processing aur control theory se ata hai—hum ek continuous dynamical system banate hain jo time ke saath evolve hota hai.

S4 (Structured

Go deeper — visual, from zero

Test yourself — Scaling & Efficient Architectures

Connections