Sequence models and state spaces
Derive recurrent state updates, convolution kernels, and the limits of fixed-memory sequence summaries.
On this page
The question for today
What you’ll understand
- Unroll a linear state recurrence into a causal convolution.
- Distinguish fixed-coefficient state models from input-selective dynamics.
- Compare sequence-length scaling with information retention and hardware costs.
A sequence model must decide what to remember. Attention can revisit stored token representations, while a recurrent model compresses prior inputs into a state updated at every step. State-space layers make that update algebraically structured. Their efficiency comes from the form of the computation and the implementation, not from a promise that every long dependency fits in a small state. Work through the state dimensions before comparing slogans about linear time.
A state is a compressed history
Consider one scalar input u_t and a state h_t of width N. A discrete linear time-invariant model uses an N by N transition matrix, an N-dimensional input map, and a readout vector. The state contains every retained effect of the past. With a stable transition, older inputs often decay; this helps numerical behavior but can also erase useful information. A state of fixed width has a fixed storage budget at inference, whereas a conventional attention cache grows with the sequence.
Continuous-time state equations are often discretized before processing sampled data. The sampling interval and discretization rule matter: multiplying a continuous matrix by a step size is not generally the exact discrete transition. Structured parameterizations can make transitions and kernel computation efficient. A generic dense N by N matrix update still costs order N² per token, so sequence-linear complexity alone does not establish a small constant factor or good hardware utilization.
Unrolling exposes a convolution
Starting from zero state, substitute the recurrence into itself. The current output becomes a weighted sum of earlier inputs with weights determined by powers of the transition matrix. Because fixed coefficients depend on lag rather than absolute position, these weights form a causal convolution kernel. This gives different computational views of the same linear operation: recurrent updates for streaming and structured kernel or scan methods for parallel training.
Selective state-space models make parts of the update depend on the current input so the model can choose what to retain or forget. This breaks the simple fixed convolution kernel across a sequence. Specialized scan algorithms and fused kernels recover efficient execution for suitable update forms. Do not infer that all recurrences can be parallelized equally well. Distinguish the algebraic dependency graph, the arithmetic count, and the amount of intermediate state an implementation materializes.
A fair comparison follows the workload
At autoregressive inference, a recurrent state may offer constant memory per layer with respect to context length. That does not mean constant total memory: model parameters, batch size, state width, and temporary buffers remain. Attention can retain explicit past keys and values, providing a different retrieval mechanism at a growing memory cost. Hybrid architectures combine these mechanisms because compressing a history and looking up a particular historical detail are different capabilities.
Evaluate both short and long contexts, including tasks requiring selective recall rather than only smooth signal prediction. Measure training throughput and token-generation latency separately; an implementation fast on long training sequences may have different decode behavior. Record hardware, state size, precision, and batch size. Check numerical stability over very long streams and whether resetting the state at document boundaries matches deployment. A theoretical asymptotic advantage is a hypothesis to test in a concrete regime.
Work it through
Unroll a two-step memory
For scalar state h_t=0.5h_(t−1)+u_t, output y_t=h_t, and h_0=0, process inputs 2, 0, and 1.
At the first step h_1=0.5×0+2=2, so y_1=2.
At the second step h_2=0.5×2+0=1. The first input still contributes but has been halved.
At the third step h_3=0.5×1+1=1.5. Unrolling gives 0.25×2+0.5×0+1×1=1.5, matching the recurrence.
The lag kernel is 1, 0.5, 0.25, and so on. It retains a decaying weighted summary rather than a separately addressable copy of each input.
The living notebook
Explore the model
Interactive lab · oscillator
Observe state dynamics
The lab solves one analytic damped oscillator with initial displacement one and velocity zero. Damping ratio ζ and natural frequency ω₀ change how this physical two-component state evolves. It illustrates decay and oscillatory memory, not S4, Mamba, input-selective transitions, or a learned sequence benchmark.
- Displacement x(t)
- 1
- Velocity v(t)
- 0
- Regime
- underdamped
Unit mass, x(0) = 1 and v(0) = 0. The analytic solution includes underdamped, critically damped and overdamped cases; time is measured in seconds. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forState dynamics determine whether information persists, decays, or oscillates; useful learned memory requires more than labeling a layer recurrent.
Pause & reason
Check your understanding
Your turn
Try it yourself
In the scalar recurrence h_t=0.8h_(t−1)+u_t with zero initial state, what is the contribution of u_1=5 to h_4 if later inputs are zero? What if the transition were 1.2?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The first input passes through three subsequent transition multiplications before time four.
Compare with the solution
For transition 0.8, its contribution is 5×0.8³=2.56. For transition 1.2 it is 5×1.2³=8.64. The latter grows rather than decays. This scalar comparison illustrates stability; a matrix model needs analysis of its transition dynamics and finite-horizon amplification, not just an average coefficient.
Keep the idea
Take it with you
- A recurrent state trades explicit historical storage for a structured summary.
- Fixed linear recurrences can be expanded into causal lag kernels.
- Selective updates and efficient kernels change both modeling and execution assumptions.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.