12  State Space Models

The recurrent networks of Chapter 8 read text one token after another and carry what they have read in a fixed-size state. Generating the millionth token therefore costs as much as generating the tenth. The Transformers of Chapter 11 keep every token instead: answering a question about a long report holds a key and a value for every token of the report (Section 11.3), so both memory and per-token work grow with context length. Yet in Section 11.2 a plain recurrent network as large as our GPT fell behind after the first few bytes of context and never caught up. Can a better recurrent network close that gap, and at what length does the growing cost of attention begin to matter?

Two obstacles stand in the way. Classical recurrent networks update their state nonlinearly, forcing training to proceed one step at a time and leaving most of a GPU idle. And a fixed-size state must forget, so it has to choose what to keep. The answers come from three directions: gates from the 1990s (Hochreiter and Schmidhuber 1997; Gers et al. 2000), the linear dynamical systems of control theory (Kalman 1960), brought into deep learning as trainable layers (Gu et al. 2020, 2022), and attention without the softmax (Katharopoulos et al. 2020). They meet in recurrent layers that train in parallel and compute a form of linear attention. Every recurrent layer in this chapter has a state, an update that folds in each token, and a readout that computes the output. Control engineers call such a system a state space model; the chapter’s title uses the name in this broad sense, while Section 12.4 uses it in the narrow sense of a linear system in continuous time.

We begin with gates, which let a network learn what to keep, and with the 2014 encoder–decoder, whose single state vector creates a bottleneck we can measure. Three changes then make the state fast and selective: a linear update that can be evaluated across all positions in parallel and timed against attention (Section 12.3.4); multi-timescale decays discretized from continuous time; and input-dependent step sizes that let each token decide how much it writes and forgets. Extending the state from a vector to a matrix turns it into an associative memory of key–value pairs whose capacity we can calculate and whose write rule we can sharpen with the delta rule. Finally, interleaving a few full attention layers restores the exact recall that no fixed state possesses—a hybrid pattern adopted by several production language models (Lieber et al. 2024; Qwen Team 2025).

On a first reading, Section 12.2, the continuous-time derivation in Section 12.4, and the chunked algorithms of Section 12.6 and Section 12.7 can wait. Small synthetic tasks, each designed to isolate one memory capability, test the mechanisms, and the byte-level text benchmark of Section 11.2 checks how each mixer performs on language.