Dive into Deep Learning · §12.6
Matrix-State Recurrences and Gated Linear Attention
outer-product key–value memory · capacity & interference laws · state space duality · chunked training
Motivation
\mathbf{S}_t = \mathbf{S}_{t-1} + \mathbf{v}_t \mathbf{k}_t^\top, \qquad \mathbf{y}_t = \mathbf{S}_t \mathbf{q}_t
rate = 0.05) and retrieve it hundreds of tokens later.Geometric Intuition
\mathbf{S} = 3\,\mathbf{k}_1^\top + 5\,\mathbf{k}_2^\top \implies \mathbf{S}\mathbf{k}_1 = 3, \quad \mathbf{S}\mathbf{k}_2 = 5
Left vs. Right Associativity
\mathbf{y}_t = \Big(\sum_{s=1}^{t} \mathbf{v}_s \mathbf{k}_s^\top\Big)\mathbf{q}_t = \sum_{s=1}^{t} (\mathbf{k}_s^\top \mathbf{q}_t)\,\mathbf{v}_s
Signal vs. Interference
\mathbf{S}\mathbf{k}_j = \mathbf{v}_j + \sum_{i \neq j} (\mathbf{k}_i^\top \mathbf{k}_j)\,\mathbf{v}_i, \qquad \mathbb{E}\,\|\mathbf{S}\mathbf{k}_j - \mathbf{v}_j\|^2 = \frac{n - 1}{d_k}
Theoretical vs. Measured Capacity
Measured squared error and nearest-neighbor recall (256-vector codebook) vs. the line (n - 1)/d_k:
recall at n = 8 16 32 64 128 256 512
dₖ = 32 1.00 0.99 0.96 0.74 0.39 0.16 0.07
dₖ = 64 1.00 1.00 1.00 1.00 0.96 0.72 0.37
dₖ = 128 1.00 1.00 1.00 1.00 1.00 1.00 0.94
dₖ = 64, ρ = 0.5 1.00 0.99 0.83 0.49 0.21 0.07 0.03
Isotropic Error Projection
Look at the d_k = d_v = 64 row of the table: at n = 64 pairs, the squared error is (64-1)/64 \approx 1.0 (as large as the unit signal \|\mathbf{v}_j\|^2 = 1), yet nearest-neighbor recall is 1.00 (0.96 at n = 128). Why?
Anisotropic Interference
\mathbb{E}\,\big\|\mathbf{S}\mathbf{k}_j - \mathbf{v}_j\big\|^2 \approx (n - 1)\Big(\rho^4 + \frac{(1 - \rho^2)^2}{d_k}\Big)
Forgetting Stale Pairs
\mathbf{S}_t = a_t\, \mathbf{S}_{t-1} + \mathbf{v}_t \mathbf{k}_t^\top, \qquad a_t \in (0, 1)
Unifying SSMs and Linear Attention
Stacking the H channel states \mathbf{h}_t^{(c)} \in \mathbb{R}^N of Mamba’s selective SSM (§12.5) as rows of a matrix \mathbf{S}_t \in \mathbb{R}^{H \times N} exposes the exact same outer-product memory structure:
\mathbf{S}_t = \mathbf{E}_t \odot \mathbf{S}_{t-1} + (\boldsymbol{\Delta}_t \odot \mathbf{x}_t)\,\mathbf{B}_t^\top, \qquad \mathbf{y}_t = \mathbf{S}_t \mathbf{C}_t
| Architecture | Value \mathbf{v}_t | Key \mathbf{k}_t & Query \mathbf{q}_t | Decay Structure on \mathbf{S}_{t-1} \in \mathbb{R}^{d_v \times d_k} |
|---|---|---|---|
| Linear Attention | \mathbf{v}_t \in \mathbb{R}^{d_v} | \mathbf{k}_t, \mathbf{q}_t \in \mathbb{R}^{d_k} | No decay (a_t = 1) |
| RetNet | \mathbf{v}_t \in \mathbb{R}^{d_v} | \mathbf{k}_t, \mathbf{q}_t \in \mathbb{R}^{d_k} | Fixed scalar a \in (0, 1) per head |
| Mamba-2 / Scalar GLA | \mathbf{v}_t \in \mathbb{R}^{d_v} | \mathbf{k}_t, \mathbf{q}_t \in \mathbb{R}^{d_k} | Input-dependent scalar a_t \in (0, 1) per head |
| Vector-Decay GLA | \mathbf{v}_t \in \mathbb{R}^{d_v} | \mathbf{k}_t, \mathbf{q}_t \in \mathbb{R}^{d_k} | Per-key-coordinate \mathbf{S}_{t-1}\textrm{diag}(\boldsymbol{\alpha}_t) (\boldsymbol{\alpha}_t \in (0, 1)^{d_k}) |
| Mamba-1 (§12.5) | \boldsymbol{\Delta}_t \odot \mathbf{x}_t \in \mathbb{R}^H | \mathbf{B}_t, \mathbf{C}_t \in \mathbb{R}^N | Full entrywise matrix \mathbf{E}_t = \exp(\textrm{diag}(\boldsymbol{\Delta}_t)\boldsymbol{\Lambda}) |
Primal and Dual Forms
\mathbf{y}_t = \sum_{s=1}^{t} L_{ts}\,(\mathbf{k}_s^\top \mathbf{q}_t)\,\mathbf{v}_s \iff \mathbf{Y} = (\mathbf{L} \odot \mathbf{Q}\mathbf{K}^\top)\,\mathbf{V}
Hardware-Efficient Chunking
Partitioning T tokens into T/m chunks of length m combines Tensor Core GEMMs with linear scaling:
Numerical Verification
Comparing sequential linear_attention_recurrent, full T \times T linear_attention_dual, and chunked_linear_attention (m \in \{16, 64, 256\}) at T = 512, d_k = d_v = 64:
dual form relative deviation from the recurrence: 1.5e-06
chunks of 16 relative deviation from the recurrence: 2.2e-07
chunks of 64 relative deviation from the recurrence: 2.5e-07
chunks of 256 relative deviation from the recurrence: 8.8e-07
All three algorithms match to \sim 10^{-6} relative error while chunking reduces peak working memory from O(T^2) to O(Tm + (T/m)d_v d_k).
Layer Architecture
Synthetic Stress Test
Does a trained neural network obey the O(d_k d_v) capacity law derived for random unit keys? We test on multi-query associative recall (Arora et al., 2024):
tokens: [273 3 15 3 200 2 407 0 273 0 516 516 200 407 273 15]
answers: [2 0 0 3] written twice: [False False True False]
Experimental Design & Results
We fix a 2-block SeqLM at width d = 32 (about 37{,}000 parameters) and partition the width into h \in \{8, 4, 2\} heads of dimension d_h = 32/h \in \{4, 8, 16\}. Each head holds a d_h \times d_h matrix, so total state entries per layer scale as h \cdot d_h^2 \in \{128, 256, 512\} at nearly identical parameter count:
pairs 4 8 16 32 64
8 heads of 4, 128 entries 0.99/0.99 0.95/0.95 0.84/0.84 0.67/0.66 0.53/0.51
4 heads of 8, 256 entries 1.00/1.00 0.99/0.99 0.95/0.96 0.87/0.88 0.72/0.73
2 heads of 16, 512 entries 1.00/1.00 1.00/1.00 1.00/0.99 1.00/0.96 0.99/0.91
Interpreting Recall & Next Step
The multi-query recall curves confirm the theoretical scaling law and expose one remaining flaw:
Check Your Understanding
Work out the following three questions:
GatedLinearAttention layer (d_h = 32) against a 4-head layer (d_h = 64). Which holds more key–value pairs, and by what factor?float32 (4 bytes), how much memory does the full T \times T dual mask \mathbf{L} \odot \mathbf{Q}\mathbf{K}^\top occupy compared to chunked evaluation with chunk size m = 64?Answers:
Key Takeaways
| Property / Mechanism | Mathematical Form | Scaling & Modeling Role |
|---|---|---|
| Outer-Product Memory | \mathbf{S}_t = \mathbf{S}_{t-1} + \mathbf{v}_t \mathbf{k}_t^\top \in \mathbb{R}^{d_v \times d_k}, \mathbf{y}_t = \mathbf{S}_t \mathbf{q}_t | Equivalence with causal linear attention \sum_{s \le t} (\mathbf{k}_s^\top \mathbf{q}_t)\mathbf{v}_s |
| Random-Key Interference | \mathbb{E}\|\mathbf{S}\mathbf{k}_j - \mathbf{v}_j\|^2 = (n - 1)/d_k | Isotropic error projects onto codebook with variance (n-1)/(d_k d_v) |
| Recall Capacity Scaling | n_{\max} \propto d_k d_v per head (d^2 / h per layer) | Fewer, wider heads store proportionally more pairs at equal params |
| State Space Duality (SSD) | \mathbf{Y} = (\mathbf{L} \odot \mathbf{Q}\mathbf{K}^\top)\mathbf{V}, L_{ts} = \prod_{r=s+1}^t a_r | Unifies decayed matrix recurrence and masked linear attention |
| Chunked Training | Length-m diagonal GEMMs + (T/m)-step scan | Exploits rank-1 off-diagonal decay blocks for O(T) memory & Tensor Core speed |