Matrix-State Recurrences

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

From Vector States to Associative Key–Value Matrices

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

  • Associative Recall: Language and code continually bind a key to a value (rate = 0.05) and retrieve it hundreds of tokens later.
  • Outer-Product Matrix State: Upgrading \mathbf{h}_t \in \mathbb{R}^d to \mathbf{S}_t \in \mathbb{R}^{d_v \times d_k} writes key-addressed rank-1 outer products and queries via \mathbf{S}_t \mathbf{q}_t.

Outer-Product Writes and Cross-Talk in Toy Dimension 2

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

  • Exact up to d_k Orthogonal Keys: In \mathbb{R}^2, orthogonal \mathbf{k}_1 = (1, 0)^\top, \mathbf{k}_2 = (0, 1)^\top experience zero interference (\mathbf{k}_1^\top \mathbf{k}_2 = 0).
  • Cross-Talk Once n > d_k: Adding a 3rd key \mathbf{k}_3 = (1, 1)^\top / \sqrt{2} with v_3 = 7 leaks 7/\sqrt{2} into \mathbf{S}\mathbf{k}_1 \approx 7.95.

Matrix Recurrence Is Unnormalized Linear Attention

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

  • Left Association (Recurrent): Accumulates \mathbf{S}_t \in \mathbb{R}^{d_v \times d_k} in O(d_v d_k) state and O(d_v d_k) work per decoded token.
  • Right Association (Attention): Computes dot products \mathbf{k}_s^\top \mathbf{q}_t across chunks via parallel Tensor Core GEMMs.

Exact Retrieval-Error Law for Random Unit Keys

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}

  • Crowding, Not Aging: For random unit keys on the sphere in \mathbb{R}^{d_k}, every stored pair experiences expected squared error (n - 1)/d_k.
  • 1D Codebook Projection: Along any wrong codebook vector \mathbf{c} \in \mathbb{R}^{d_v}, variance drops to (n - 1)/(d_k d_v).

Empirical Error and Codebook Recall vs. Pair Load n

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

Why Nearest-Neighbor Recall Scales with Entries dk dv

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?

  • Isotropic spread across d_v value dimensions: The total interference vector has squared norm \approx n / d_k, but it points in a random direction in \mathbb{R}^{d_v}.
  • 1D projection onto any wrong codebook vector \mathbf{c} \in \mathbb{R}^{d_v}: Only the component along \mathbf{c} competes with the true value \mathbf{v}_j, and that 1D projection has variance: \textrm{Var}\big(\mathbf{c}^\top (\mathbf{S}\mathbf{k}_j - \mathbf{v}_j)\big) = \frac{n - 1}{d_k\, d_v}
  • The O(d_k d_v) entry law: With d_v = d_k, doubling d_k quadruples the state entries d_k^2 and quadruples the number of pairs recalled at \sim 95\% accuracy (32 \to 128 \to 512 pairs).

Pitfall: Key Correlation Creates an Irreducible Floor

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)

  • Non-Vanishing Overlap Floor: When keys share a common direction \mathbf{c} with weight \rho > 0, the floor (n - 1)\rho^4 never shrinks as d_k \to \infty.
  • Empirical Collapse: At d_k = 64, \rho = 0.5, 64-pair recall drops from 1.00 to 0.49.

Bounded-Interference Memory via Input-Dependent Decay

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)

  • Effective Sliding Window: At constant decay a, a pair written \ell steps ago carries weight a^\ell, summing to \frac{1}{1 - a} (10 pairs at a = 0.9; 100 pairs at a = 0.99).
  • Bounded Steady-State Interference: Squared weights sum to \frac{1}{1 - a^2}, bounding expected squared interference at any length T by \frac{1}{(1 - a^2)d_k}.

Mamba and Mamba-2 as Decayed Matrix-State Recurrences

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})

State Space Duality (SSD): Recurrence vs. Masked Dual

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}

  • State Space Duality (Dao & Gu, 2024): The O(T d_v d_k) primal recurrence \mathbf{S}_t = a_t \mathbf{S}_{t-1} + \mathbf{v}_t \mathbf{k}_t^\top and the O(T^2 d) causally masked linear attention dual compute the exact same output \mathbf{Y} with L_{ts} = \prod_{r=s+1}^t a_r.

Block-Semiseparable Structure and Chunked Training

Hardware-Efficient Chunking

Partitioning T tokens into T/m chunks of length m combines Tensor Core GEMMs with linear scaling:

  1. Intra-chunk (blue diagonal m \times m blocks): Evaluate (\mathbf{L}_{\text{diag}} \odot \mathbf{Q}_c \mathbf{K}_c^\top)\mathbf{V}_c in parallel across all chunks via batched matrix multiplication.
  2. Inter-chunk scan (orange off-diagonal blocks): Because L_{ts} factors across chunk boundaries, every off-diagonal block of \mathbf{L} has rank 1 (and rank \le d_k in \mathbf{L}\odot\mathbf{Q}\mathbf{K}^\top). Scanning the d_v \times d_k chunk-end states \mathbf{S}^{(c)} = \gamma^{(c)}\mathbf{S}^{(c-1)} + \text{writes}^{(c)} across T/m chunks takes \lceil\log_2(T/m)\rceil rounds.
  3. Past-state readout: Add (a_1\cdots a_t)\mathbf{S}^{(c-1)}\mathbf{q}_t to each chunk position.

Verifying Equivalence of Recurrent, Dual, and Chunked

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).

The Gated Linear Attention (GLA) Sequence Mixer

Layer Architecture

  • Width-4 Causal Conv + SiLU: Binds adjacent key and value tokens (t-1, t) before outer-product writing.
  • \ell_2-Normalized \mathbf{Q}, \mathbf{K} & Log-Uniform Decays: Enforces unit-norm keys and spans initial head horizons \tau_h \in [4, 256].
  • Per-Head RMSNorm & SiLU Gate: Stabilizes readout scale before output projection \mathbf{W}_o.

Multi-Query Associative Recall Benchmark Setup

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):

  • Sequence format: Present n key–value pairs (distinct key tokens from 128 possible keys, each followed by a value token from 4 classes), then filler tokens, and finally all n keys again in random permutation as queries.
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]

Varying State Entries at Constant Parameter Count

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

Trained GLA Obeys the Matrix Capacity Law—Until Rebound

Interpreting Recall & Next Step

The multi-query recall curves confirm the theoretical scaling law and expose one remaining flaw:

  • Capacity scales with state entries h \cdot d_h^2: At 4 pairs, all three head configurations achieve \ge 98\% accuracy. At 64 pairs, doubling state entries from 128 (8 \times 4^2) to 256 (4 \times 8^2) lifts recall from \sim 0.50 to \sim 0.75—roughly doubling the number of pairs recalled at a fixed accuracy level without adding a single trainable parameter.
  • The additive overwrite flaw: Because \mathbf{S}_t = a_t \mathbf{S}_{t-1} + \mathbf{v}_t \mathbf{k}_t^\top only adds outer products, binding an existing key \mathbf{k} to a new value \mathbf{v}^{\text{new}} returns a superposition of old and new values. Teaching the matrix state to read before writing is the goal of §12.7.

Concept Check: Capacity, Chunk Memory, and Duality

Check Your Understanding

Work out the following three questions:

  1. At width d = 256, compare the per-layer state entries of an 8-head GatedLinearAttention layer (d_h = 32) against a 4-head layer (d_h = 64). Which holds more key–value pairs, and by what factor?
  2. For one head with T = 4{,}096 and d_k = d_v = 64 in 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?
  3. Under a constant decay a = 0.95, what is the effective number of past pairs 1/(1 - a) retained, and what is the maximum expected squared interference 1/((1 - a^2)d_k) at d_k = 64?

Answers:

  1. 8 heads of 32 \times 32 hold 8 \times 1{,}024 = \mathbf{8{,}192\text{ entries}}; 4 heads of 64 \times 64 hold 4 \times 4{,}096 = \mathbf{16{,}384\text{ entries}}—twice as many state entries and \sim 2\times the pair capacity at the exact same W_{qkv} parameter count.
  2. Full dual matrix: 4{,}096^2 \times 4\text{ B} = \mathbf{64\text{ MB}}; chunked (64 chunks of 64 \times 64 masks + 64 chunk states of 64 \times 64): 2 \times 64 \times 64^2 \times 4\text{ B} = \mathbf{2\text{ MB}} (32\times smaller, scaling linearly with T).
  3. Effective window: 1/(1 - 0.95) = \mathbf{20\text{ pairs}}; steady-state interference bound: 1 / ((1 - 0.95^2) \times 64) \approx 1 / (0.0975 \times 64) \approx \mathbf{0.16}.

Summary: Matrix-State Recurrences and Linear Attention

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