Dive into Deep Learning · §12.3
Linear Recurrences and Parallel Associative Scans
affine state transitions · minimal GRU (minGRU) · associative prefix scans · complexity vs. attention
Motivation
Linear Recurrence Basics
h_t = a\, h_{t-1} + x_t = \sum_{s=1}^{t} a^{t-s} x_s
Time-Varying Decays
\mathbf{h}_t = \mathbf{a}(\mathbf{x}_t) \odot \mathbf{h}_{t-1} + \mathbf{b}(\mathbf{x}_t)
The minGRU Architecture
\mathbf{z}_t = \sigma(\mathbf{W}_\textrm{z}\mathbf{x}_t + \mathbf{b}_\textrm{z}), \quad \mathbf{h}_t = \mathbf{z}_t \odot \mathbf{h}_{t-1} + (1 - \mathbf{z}_t) \odot \tilde{\mathbf{h}}_t
Expressivity Trade-Off
minGRU cannot tell whether a memory slot is already full and overwrites on later digits.Algebra of Parallel Scans
(\mathbf{a}_1, \mathbf{b}_1) \bullet (\mathbf{a}_2, \mathbf{b}_2) = (\mathbf{a}_1 \odot \mathbf{a}_2,\; \mathbf{a}_2 \odot \mathbf{b}_1 + \mathbf{b}_2)
Parallel Prefix Scan
Scan Code Architecture
associative_scan slices earlier = x[:, :-s] and later = x[:, s:] at doubling strides s = 1, 2, 4, \ldots:
h0): linear_scan(a, b, h0) folds a nonzero initial state \mathbf{h}_0 into the first step’s write (\mathbf{b}_1 \leftarrow \mathbf{a}_1 \odot \mathbf{h}_0 + \mathbf{b}_1) before launching the prefix scan, enabling chunk-by-chunk state passing.Scan Implementation
linear_scan(a, b, h0) folds an optional initial state \mathbf{h}_0 into the first write (\mathbf{b}_1 \leftarrow \mathbf{a}_1 \odot \mathbf{h}_0 + \mathbf{b}_1) and runs the associative prefix scan along the time axis:
max deviation: 4.8e-07
Even on a non-power-of-two length (T = 100), the parallel scan and sequential loop agree to float32 rounding error (\sim 10^{-7}).
Language Modeling Benchmark
Using our 4-block SeqLM benchmark (128-byte windows, 1{,}000 AdamW steps), we compare GRU (d = 256), minGRU (d = 256), and a parameter-matched minGRU, d=302 (3.81\text{M} parameters):
validation 0-15 16-63 64-127 ms/step
GRU 3,817,216 1.299/1.300 1.490/1.491 1.273/1.274 1.270/1.271 33/32
minGRU 2,764,544 1.343/1.348 1.520/1.520 1.319/1.323 1.317/1.324 26/26
minGRU, d=302 3,816,932 1.328/1.329 1.508/1.507 1.303/1.304 1.301/1.303 26/28
At equal width (2.76\text{M} vs. 3.81\text{M} params), minGRU trails GRU by \sim 0.05 nats/byte (1.35 vs. 1.30); matching parameter count (minGRU, d=302) closes the gap to \sim 0.03 nats/byte while beating the attention baseline (1.38).
Inside the Trained Model
How much of minGRU’s prediction quality comes from context carried in \mathbf{h}_{t-1} versus current-token features in the residual stream? We evaluate the trained model with \mathbf{h}_{t-1} = \mathbf{0} forced at every step (StateReset, so \mathbf{h}_t = \mathbf{b}_t):
validation loss: 1.343 nats per byte, with the state reset at every step: 2.481
Gate Timescale Analysis
Inspecting the update gates \mathbf{z}_t \in (0, 1)^d of the first minGRU block on a validation batch before and after 1{,}000 training steps:
gate above half-life in steps share at initialization trained
0.5 1 49.0% 53.4%
0.9 7 1.3% 17.1%
0.99 69 0.0% 0.1%
Work, Depth, and Memory
GRU: \Theta(T d^2) FLOPs across \Theta(T) serial steps with \Theta(d) generation state.minGRU Scan: \Theta(T d^2) projection FLOPs in \Theta(\log_2 T) depth while keeping \Theta(d) inference state.Hardware Benchmark
Forward + backward runtime (ms) in float16 at width d = 256 with total tokens per batch held constant at B \times T = 65{,}536 as T sweeps from 512 to 16{,}384:
T 512 1024 2048 4096 8192 16384
GRU 20.2 35.6 69.7 113.3 197.6 425.6
minGRU 4.0 4.3 4.5 5.0 5.2 5.6
attention 2.1 2.4 3.2 4.7 7.9 13.7
PyTorch Scaling Analysis
With total batch tokens fixed at B \times T = 65{,}536, an ideal linear-time layer has a completely horizontal curve:
GRU (O(T) depth): Slowest at every length, growing >15\times from T = 512 to 16{,}384 because doubling T doubles the number of dependent kernel steps while halving batch parallelism B.minGRU (doubling scan): Performs \lceil \log_2 T \rceil memory passes (9 rounds at T = 512 to 14 rounds at T = 16{,}384), rising by less than 50\% across a 32\times increase in sequence length.attention (O(T^2 d) work): Stays flat up to T = 2{,}048 while projections and fixed overheads dominate, crosses the doubling scan near T = 4{,}096, and climbs toward 2\times per doubling—trailing minGRU by 2.5\times at T = 16{,}384.Check Your Understanding
Answer these three questions on linear recurrences and parallel scans:
GRU runtime double each time T doubles even though its total FLOP count B T d^2 is constant?Answers:
GRU waits for \mathbf{h}_{t-1} and pays a fixed kernel/synchronization latency, so doubling T doubles the sequential critical-path latency.Key Takeaways
| Mechanism / Model | Mathematical Definition | Key Scaling & Modeling Property |
|---|---|---|
| Affine Vector Recurrence | \mathbf{h}_t = \mathbf{a}_t \odot \mathbf{h}_{t-1} + \mathbf{b}_t | Unrolls to \sum_{s \le t} (\prod_{r=s+1}^t \mathbf{a}_r) \odot \mathbf{b}_s; forward & gradient decay at same rate |
Minimal GRU (MinGRU) |
\mathbf{z}_t = \sigma(\mathbf{W}_\textrm{z}\mathbf{x}_t + \mathbf{b}_\textrm{z}), \mathbf{b}_t = (1-\mathbf{z}_t)\odot\tilde{\mathbf{h}}_t | Drops \mathbf{h}_{t-1} from gates (2d^2 params); trails GRU by 0.03 nats/byte at equal size |
| Associative Combine (\bullet) | (\mathbf{a}_1, \mathbf{b}_1) \bullet (\mathbf{a}_2, \mathbf{b}_2) = (\mathbf{a}_1 \odot \mathbf{a}_2, \mathbf{a}_2 \odot \mathbf{b}_1 + \mathbf{b}_2) | Compresses any block of steps into one pair of width d in O(d) work |
| Doubling vs. Tree Scan | \lceil \log_2 T \rceil vs. 2\lceil \log_2 T \rceil parallel rounds | O(T \log T) vs. O(T) combines; flat wall-clock time up to T = 16{,}384 |
| Attention Crossover | 8Td^2 projections vs. 2T^2 d attention scores | Quadratic attention cost dominates once T \gtrsim 4d and grows linearly with T per token |