Part III · Beyond SSMs Week 11 Published

Linear attention and Hyena: long convolution and gated recurrence

Linear attention read as a matrix-state linear recurrence — the dual of Chapter 9's scan, entered from the attention side — then gated into the LTV/selectivity face (GLA, RetNet), pivoted to Hyena's implicit LTI long convolution, and landed honestly on the associative-recall capacity limit of any finite state.

On this page
  1. 11.1 From softmax to separable scores
  2. 11.2 Linear attention as a matrix-state recurrence
  3. 11.3 Gating: GLA, RetNet, and the LTV reading
  4. 11.4 Hyena: the FFT long convolution
  5. 11.5 Implicit and selective filters
  6. 11.6 Counter-evidence: associative recall and capacity
  7. 11.7 What’s next
  8. 11.8 Exercises
  9. Exercise 11.1 (short)
  10. Exercise 11.2 (short, code)
  11. Exercise 11.3 (short)
  12. Exercise 11.4 (theory) — solution in §11.9
  13. Exercise 11.5 (theory) — solution in §11.9
  14. Exercise 11.6 (theory) — solution in §11.9
  15. 11.9 Full solutions to theory exercises
  16. Solution to Exercise 11.4
  17. Solution to Exercise 11.5
  18. Solution to Exercise 11.6
  19. 11.10 Companion code

Linear attention and Hyena: long convolution and gated recurrence

11.1 From softmax to separable scores

Chapter 9 reached a striking identity from the state-space side: a scalar-state selective SSM is masked linear attention (Theorem 9.5). The scan’s path-ordered taps reassemble into a single lower-triangular matrix M=L(CB)diag(Δ)M = L \circ (\outputmat \inputmat^\top)\,\mathrm{diag}(\stepsize), with LL a causal decay mask replacing the softmax. This chapter enters that same duality from the other side. We start with attention and rediscover the state space.

Causal attention computes, for each position tt,

yt=jtsim(qt,kj)vjjtsim(qt,kj),sim(q,k)=exp(qk).y_t = \frac{\sum_{j \le t} \mathrm{sim}(q_t, k_j)\, v_j}{\sum_{j \le t} \mathrm{sim}(q_t, k_j)}, \qquad \mathrm{sim}(q, k) = \exp(q^\top k).

The exponential couples every query to every key: the normalizer cannot be factored across jj, so the only way to evaluate it is to materialize all O(L2)O(\seqlen^2) scores. The move that breaks the coupling Katharopoulos et al. (2020) is to choose a feature map ϕ:RdRdk\phi : \mathbb{R}^{d} \to \mathbb{R}^{d_k} with non-negative entries and write the similarity as an inner product of features,

sim(q,k)=ϕ(q)ϕ(k)    0.\mathrm{sim}(q, k) = \phi(q)^\top \phi(k) \;\ge\; 0 .

Separability is the whole engine. Because ϕ(qt)ϕ(kj)\phi(q_t)^\top \phi(k_j) factors, the numerator and denominator each become a query contracted against a running sum over keys — and a running sum is a state.

11.2 Linear attention as a matrix-state recurrence

Fix ϕ(x)=elu(x)+1>0\phi(x) = \mathrm{elu}(x) + 1 > 0. Define a matrix state StRdk×dvS_t \in \mathbb{R}^{d_k \times d_v} and a normalizer ztRdkz_t \in \mathbb{R}^{d_k} by

St=St1+ϕ(kt)vt,zt=zt1+ϕ(kt),yt=Stϕ(qt)ztϕ(qt),S_t = S_{t-1} + \phi(k_t)\,v_t^\top, \qquad z_t = z_{t-1} + \phi(k_t), \qquad y_t = \frac{S_t^\top \phi(q_t)}{z_t^\top \phi(q_t)},

with S0=0S_0 = 0, z0=0z_0 = 0. This is a bona fide state-space recurrence: the hidden state is a matrix, the transition is the identity (A=IA = I, pure accumulation with no forgetting — the LTI face of Chapter 8), and the input is injected as the rank-one outer product ϕ(kt)vt\phi(k_t) v_t^\top. It is Chapter 9’s scan with the carry promoted from a vector to a matrix and the decay switched off.

The same operator has a second, parallel face. Stacking Qϕ=[ϕ(q1);;ϕ(qL)]Q_\phi = [\phi(q_1); \dots; \phi(q_\seqlen)] and Kϕ=[ϕ(k1);;ϕ(kL)]K_\phi = [\phi(k_1); \dots; \phi(k_\seqlen)], the unnormalized output is a masked matrix product.

Theorem 11.1 (Linear attention: two faces of one operator).

For any feature map ϕ\phi, the recurrent form (St=St1+ϕ(kt)vtS_t = S_{t-1} + \phi(k_t)v_t^\top, output Stϕ(qt)S_t^\top \phi(q_t)) and the parallel form

Y=(L(QϕKϕ))V,Ltj=1[jt],Y = \big(L \circ (Q_\phi K_\phi^\top)\big)\, V, \qquad L_{tj} = \mathbb{1}[\,j \le t\,],

with LL the all-ones causal mask, compute the identical output. The recurrent form costs O(Ldkdv)O(\seqlen\, d_k\, d_v) time and O(dkdv)O(d_k\, d_v) memory; the parallel form costs O(L2d)O(\seqlen^2 d). This is exactly Theorem 9.5 with the decay mask specialized to ones.

The proof (Exercise 11.4, §11.9) is one line of reassociation: St=jtϕ(kj)vjS_t = \sum_{j \le t} \phi(k_j) v_j^\top, so Stϕ(qt)=jt(ϕ(qt)ϕ(kj))vjS_t^\top \phi(q_t) = \sum_{j \le t} (\phi(q_t)^\top \phi(k_j))\, v_j, which is precisely row tt of (L(QϕKϕ))V(L \circ (Q_\phi K_\phi^\top))V. The mask LL supplies the causal cumulative sum that the recurrence performs incrementally. The companion confirms the two routes agree to machine precision in float64: maxyrecypar=5.6×1016\max|y_{\mathrm{rec}} - y_{\mathrm{par}}| = 5.6 \times 10^{-16} at L=64\seqlen = 64, flat across sequence lengths.

Two panels: left, the maximum difference between recurrent and parallel linear attention versus sequence length, flat near machine epsilon; right, a log-log plot of recurrent O(L) versus parallel O(L-squared) cost.
The two faces of linear attention. Left: the recurrent (scan) and parallel (masked-matmul) forms agree to $\sim 10^{-16}$ in float64 across sequence length — they are the same operator. Right: their costs diverge — recurrent $O(\seqlen)$ versus parallel $O(\seqlen^2)$ — which is why the recurrent form is the inference mode and the parallel form the training mode. Produced by companions/ch11/jax/linear_attention.py; the identity is pinned in tests/test_linear_attention.py::test_recurrent_equals_parallel_normalized.

The normalizer, and why this chapter is float64. The denominator ztϕ(qt)z_t^\top \phi(q_t) is a large-over-large quotient: both zt=jtϕ(kj)z_t = \sum_{j\le t} \phi(k_j) and the numerator grow like O(t)O(t), so the ratio is a difference of accumulations prone to catastrophic cancellation in low precision. The recurrent and parallel forms accumulate that quotient in different orders (sequential cumsum versus a single matmul), so their agreement is a direct probe of arithmetic precision. In float64 they match to 2×10162 \times 10^{-16} at L=512\seqlen = 512; in float32 the same computation caps out at 1.2×1071.2 \times 10^{-7} — a precision gap of nearly 10910^{9}. The 101210^{-12} identity pin is simply unreachable in single precision, which is why every companion in this chapter runs float64. (Gated linear attention in §11.3 sidesteps the issue by dropping the normalizer for an explicit decay plus an output normalization — cleaner numerically.)

11.3 Gating: GLA, RetNet, and the LTV reading

Ungated linear attention never forgets. To make it selective — to let the state forget — attach a per-step gate γt(0,1)dk\gamma_t \in (0,1)^{d_k} to the transition:

St=diag(γt)St1+ϕ(kt)vt,yt=Stϕ(qt).S_t = \mathrm{diag}(\gamma_t)\, S_{t-1} + \phi(k_t)\, v_t^\top, \qquad y_t = S_t^\top \phi(q_t).

This is the Chapter 9 selectivity move read from the attention side. Two regimes sit inside it:

  • RetNet Sun et al. (2023) takes γtγ\gamma_t \equiv \gamma, a fixed head-wise decay. The transition is constant, so the operator is LTI with forgetting — the Chapter 8 face plus a decay. Its parallel decay mask Ltj=γtjL_{tj} = \gamma^{\,t-j} is Toeplitz (it depends only on the lag tjt - j).
  • GLA Yang et al. (2024) and HGRN2 Qin et al. (2024) make γt=σ(Wxt)\gamma_t = \sigma(W x_t) data-dependent. The transition now changes every step, so the operator is LTV — input-dependent forgetting, exactly the selectivity of Chapter 9. Its decay mask Ltj=i=j+1tγiL_{tj} = \prod_{i=j+1}^{t} \gamma_i is not Toeplitz; the diagonals vary, just as Chapter 9’s selective decay mask does.

The masked-parallel form generalizes by the GLA “secondary” rescaling: with the cumulative log-gate gt=itlogγig_t = \sum_{i \le t} \log \gamma_i, set q~t=ϕ(qt)egt\tilde q_t = \phi(q_t) \odot e^{g_t} and k~j=ϕ(kj)egj\tilde k_j = \phi(k_j) \odot e^{-g_j}; then q~tk~j=ϕ(qt)diag ⁣(i=j+1tγi)ϕ(kj)\tilde q_t^\top \tilde k_j = \phi(q_t)^\top \mathrm{diag}\!\big(\prod_{i=j+1}^t \gamma_i\big) \phi(k_j) is the gated score. This is what makes the duality exact.

Theorem 11.2 (Gated linear attention is masked linear attention with a semiseparable decay mask).

The gated recurrence St=diag(γt)St1+ϕ(kt)vtS_t = \mathrm{diag}(\gamma_t) S_{t-1} + \phi(k_t)v_t^\top, yt=Stϕ(qt)y_t = S_t^\top \phi(q_t), has a masked-parallel form. For a scalar per-step gate γi\gamma_i it is exactly Y=(Lγ(QϕKϕ))VY = (L_\gamma \circ (Q_\phi K_\phi^\top))\,V with the 1-semiseparable decay mask Lγ,tj=i=j+1tγiL_{\gamma,\,tj} = \prod_{i=j+1}^{t} \gamma_i, and under logγi=AiΔi\log \gamma_i = \statemat_i\, \stepsize_i this LγL_\gamma coincides exactly with the decay mask of Theorem 9.5 — gated linear attention and the scalar-state selective SSM are the same operator, read from opposite sides. For a per-feature gate γt(0,1)dk\gamma_t \in (0,1)^{d_k} the decay acts per channel and does not collapse to a single scalar mask; the parallel form instead rescales features by the cumulative gate (q~t=ϕ(qt)egt\tilde q_t = \phi(q_t)\odot e^{g_t}, k~j=ϕ(kj)egj\tilde k_j = \phi(k_j)\odot e^{-g_j}, gt=itlogγig_t = \sum_{i\le t}\log\gamma_i), recovering the same recurrence (§11.9).

The proof is Exercise 11.5 (§11.9): unroll the gated recurrence, contract with ϕ(qt)\phi(q_t), and recognize the product-of-gates as the cumulative decay that Theorem 9.4‘s segsum exponentiates. The companion both checks the recurrent–masked identity (max\max difference 2.8×10142.8 \times 10^{-14} for a data-dependent gate) and verifies the cross-chapter bridge against the actual Chapter 9 code: the GLA scalar mask and Chapter 9’s segsum mask agree to 0.00.0 (bit-identical) under logγi=AiΔi\log \gamma_i = \statemat_i \stepsize_i.

Two heatmaps of lower-triangular decay masks. Left, RetNet's constant-gamma mask is banded and Toeplitz. Right, GLA's input-dependent mask is lower-triangular but its diagonals visibly vary, so it is not Toeplitz.
The decay mask is where selectivity becomes visible. Left: RetNet's constant gate gives $L_{tj} = \gamma^{t-j}$, a Toeplitz (lag-only) mask — the LTI face. Right: GLA's input-dependent gate gives $L_{tj} = \prod_{i=j+1}^{t}\gamma_i$, whose diagonals vary along the sequence — the LTV face, the same non-Toeplitz structure Chapter 9's selective decay mask shows. Produced by companions/ch11/jax/gated_linear_attention.py; the recurrent–masked identity and the Chapter 9 bridge are pinned in tests/test_gated_linear_attention.py.

11.4 Hyena: the FFT long convolution

The second operator family of this chapter abandons attention entirely — yet it is the same structural idea in new dress, not a digression: Hyena’s convolution is one more instance of the LTI/LTV fork that organized §§11.2–11.3, as §11.5 will make explicit. Hyena Poli et al. (2023) interleaves implicit long convolutions with elementwise multiplicative gating. The convolution is the object to teach: a causal long convolution is a linear time-invariant operator — the convolutional view of an SSM (Proposition 8.1) — computable in O(LlogL)O(\seqlen \log \seqlen) by the FFT rather than the O(L2)O(\seqlen^2) explicit Toeplitz product. For input uRB×L×Du \in \mathbb{R}^{B \times \seqlen \times D} and a per-channel filter kk,

yb,t,d=s=0tkd,tsub,s,d+biasdub,t,d.y_{b,t,d} = \sum_{s=0}^{t} k_{d,\,t-s}\, u_{b,s,d} + \mathrm{bias}_d\, u_{b,t,d}.

The one subtlety is causality under the cyclic FFT.

Theorem 11.3 (Zero-padding to 2L computes the causal convolution).

Padding uu and the filter kk to length 2L2\seqlen, multiplying their real FFTs pointwise, inverse-transforming, and truncating to the first L\seqlen samples yields exactly the lower-triangular Toeplitz product above. Padding to 2L2\seqlen (in fact any N2L1N \ge 2\seqlen - 1) is sufficient: it sends the cyclic FFT’s wrap-around terms onto the zero-padded tail of kk, where they contribute nothing. The un-padded length-L\seqlen transform wraps late taps onto early outputs and breaks causality, so some padding past L\seqlen is required.

The proof is Exercise 11.6(a) (§11.9). The companion makes both halves concrete: the O(LlogL)O(\seqlen \log \seqlen) FFT route reproduces the O(L2)O(\seqlen^2) explicit-Toeplitz oracle to 2.7×10152.7 \times 10^{-15} in float64 (the predecessor implementation ran float32 at a 10410^{-4} tolerance — this is a 101110^{11} tightening), while the un-padded variant disagrees with the oracle by 7.17.1, an O(1)O(1) violation of causality.

Two panels: left, the maximum difference between FFT convolution and the explicit Toeplitz reference versus sequence length, flat near machine epsilon; right, a log-log plot of FFT O(L log L) versus Toeplitz O(L-squared) cost.
Hyena's primitive. Left: the $2\seqlen$-padded FFT convolution matches the explicit causal-Toeplitz oracle to $\sim 10^{-15}$ in float64 across sequence length (Theorem, §11.4). Right: the cost separation $O(\seqlen \log \seqlen)$ versus $O(\seqlen^2)$ that motivates the FFT for long filters. Produced by companions/ch11/jax/fftconv.py; the identity and the $2\seqlen$-padding necessity are pinned in tests/test_fftconv.py.

11.5 Implicit and selective filters

What does the long convolution mean in the dynamical-systems vocabulary of this book? Everything hinges on whether the filter is fixed or input-dependent.

When the filter kk is static across the batch and sequence, y=kuy = k * u is a single linear time-invariant convolution — one kernel, the convolutional face of an SSM (Proposition 8.1). S4 lives exactly here: its kernel K=(CBˉ,CAˉBˉ,CAˉ2Bˉ,)K = (\outputmat \discB, \outputmat \discA \discB, \outputmat \discA^2 \discB, \dots) is materialized from a structured continuous-time ODE, and the same FFT machinery of §11.4 applies. Hyena keeps the unrestricted kernel and pays the log\log factor, where S4 and Mamba fix the kernel shape — a structured semiseparable matrix (Theorem 9.4) — to buy O(L)O(\seqlen).

When the filter is made input-dependent — the “selective Hyena” variant, where the convolution kernel itself is a function of the input — time-invariance breaks, exactly as Chapter 9’s input-dependent (Δt,Bt,Ct)(\stepsize_t, \inputmat_t, \outputmat_t) broke it. No single kernel exists; no convolution theorem applies; the operator rejoins the LTV family of §11.3. This is the same fork seen three times now: a fixed transition is LTI and admits a convolution or a closed-form mask; an input-dependent transition is LTV and admits only the scan. Hyena’s gating, GLA’s decay, and Mamba’s selection are three spellings of one move.

11.6 Counter-evidence: associative recall and capacity

Beyond-SSM does not mean better-than-SSM. The honest place to end is where linear attention loses: multi-query associative recall (MQAR) — store KK key–value pairs, then answer queries by retrieving the value bound to a key Arora et al. (2024) . On this task, linear attention degrades as KK grows while selective SSMs (Mamba) and softmax attention stay near-perfect. The mechanism is the §11.2 insight turned into a limitation, and it is provable.

Proposition 11.4 (Finite state bounds associative-recall capacity).

The linear-attention state S=i=1Kϕ(ki)viS = \sum_{i=1}^{K} \phi(k_i)\, v_i^\top is a sum of KK rank-one matrices, so rankSmin(K,dk,dv)\operatorname{rank} S \le \min(K, d_k, d_v). If K>dkK > d_k, the feature vectors {ϕ(ki)}Rdk\{\phi(k_i)\} \subset \mathbb{R}^{d_k} are linearly dependent, so the readout Sϕ(kj)S^\top \phi(k_j) cannot reproduce KK linearly independent values: there exist queries for which exact recall is impossible, regardless of ϕ\phi. Capacity is bounded by the feature dimension.

The proof is Exercise 11.6(b) (§11.9). The companion turns the bound into a measured fact (a fixed-weight mechanism, not a trained model — the trained-model accuracy curves are cited to Zoology Arora et al. (2024) ). Storing KK random unit keys with orthonormal values and reading each back, the per-binding retrieval error 1Kjv^jvj\frac{1}{K}\sum_j \lVert \hat v_j - v_j \rVert behaves exactly as the rank bound predicts:

  • Below capacity, exact. With KdkK \le d_k orthonormal keys the features are independent, the state is full-rank, and recall is exact — error 5×1016\sim 5 \times 10^{-16}.
  • Past capacity, degrading. With generic keys the error grows like K/dk\sqrt{K/d_k}: at K=64K = 64 it is 1.991.99 for dk=16d_k = 16 and 0.980.98 for dk=64d_k = 64 (capacity scales with the state size), rising to 2.812.81 at K=128K = 128.
  • The oracle stays exact. Softmax attention’s exponential sharpening concentrates the readout on the self-match, so its error is 0.0000.000 independent of KK — the capacity-unbounded baseline that linear attention cannot match.
Retrieval error versus number of key-value pairs K. Two linear-attention curves, for state size 16 and 64, rise as K grows, with the larger state rising more slowly; the softmax oracle curve stays flat at zero.
The capacity wall (fixed-weight mechanism, not a trained model). Linear-attention retrieval error grows like $\sqrt{K/d_k}$ as the number of stored pairs exceeds the state size; enlarging the feature dimension lowers it (here $d_k$: $16 \to 64$ cuts the error in half), but no finite state escapes the trend. Softmax attention stays at zero error for all $K$. The structural cause is the §11.6 rank bound: a rank-$d_k$ state cannot hold unboundedly many independent associations. Produced by companions/ch11/jax/mqar_recall.py; pinned in tests/test_mqar_recall.py (exact sub-capacity recall, growth with $K$, and capacity scaling with $d_k$).

Gating (§11.3) helps at the margin — a decay lets the state shed stale bindings — but it reweights a finite state, it does not raise the rank ceiling. Closing the gap needs either a genuinely larger structured state (the selective-SSM route of Chapters 9–10) or a smarter write rule, which is the subject of Chapter 12.

11.7 What’s next

This chapter built the linear-attention half of the “beyond-SSM” family: an additive matrix-state recurrence (A=IA = I), gated into the LTV/selectivity face (GLA, RetNet), with Hyena’s implicit LTI long convolution as the sibling operator and a hard capacity limit (§11.6) at the bottom.

Chapter 9 promised this chapter would develop “linear attention and the delta-rule lineage” as the attention-side reading of structured contractions. We have built the linear-attention half; the delta-rule half sharpens exactly the §11.6 capacity limit. Instead of blindly accumulating ϕ(k)v\phi(k)v^\top, the delta rule overwrites the value currently associated with a key — a rank-one correction

St=(Iβtϕ(kt)ϕ(kt))St1+βtϕ(kt)vt,S_t = \big(I - \beta_t\, \phi(k_t)\phi(k_t)^\top\big) S_{t-1} + \beta_t\, \phi(k_t) v_t^\top,

turning the identity-transition recurrence into a genuinely data-dependent write. That is Chapter 12 (the online-learning reading: DeltaNet as explicit-Euler, Longhorn as backward-Euler). Chapter 13 adds exponential gates and matrix memory (xLSTM, RWKV-7); Chapter 14 mixes these layers with attention into the production hybrids. The structure is one family; we are still mapping its faces.

11.8 Exercises

Three short problems (solutions inline) and three longer ones (solutions in §11.9).

Exercise 11.1 (short)

Take dk=2d_k = 2, ϕ=elu+1\phi = \mathrm{elu}{+}1, and two key–value pairs (k1,v1),(k2,v2)(k_1, v_1), (k_2, v_2) with scalar values. Write S2S_2 explicitly as a sum of two outer products, and verify by hand that the unnormalized readout S2ϕ(q)S_2^\top \phi(q) for a query qq equals row t=2t{=}2 of the masked-attention form (L(QϕKϕ))V(L \circ (Q_\phi K_\phi^\top))V.

Solution

S2=ϕ(k1)v1+ϕ(k2)v2S_2 = \phi(k_1)v_1^\top + \phi(k_2)v_2^\top (a 2×12 \times 1 matrix since vv is scalar). Then S2ϕ(q)=v1ϕ(k1)ϕ(q)+v2ϕ(k2)ϕ(q)=j2(ϕ(q)ϕ(kj))vjS_2^\top \phi(q) = v_1\,\phi(k_1)^\top\phi(q) + v_2\,\phi(k_2)^\top \phi(q) = \sum_{j \le 2} (\phi(q)^\top \phi(k_j))\, v_j. Row t=2t{=}2 of the masked form is jL2j(ϕ(q2)ϕ(kj))vj\sum_j L_{2j}(\phi(q_2)^\top\phi(k_j))v_j with L2j=1L_{2j} = 1 for j{1,2}j \in \{1, 2\} — identical (taking q=q2q = q_2). The outer-product sum and the masked row are two evaluations of the same contraction.

Exercise 11.2 (short, code)

Run companions/ch11/jax/linear_attention.py. Confirm the recurrent and parallel forms agree to <1012< 10^{-12}, and explain in one sentence why the mask LL here is all ones below the diagonal, whereas Chapter 9’s mask carried a decay.

Solution

The script prints maxyrecypar5.6×1016\max|y_{\mathrm{rec}} - y_{\mathrm{par}}| \approx 5.6 \times 10^{-16}. The mask is all-ones because ungated linear attention has transition A=IA = I: nothing decays between positions, so the weight on key jj at query tt is 11 for every jtj \le t. Chapter 9’s mask carried exp(AΔ)\exp(\statemat \sum \stepsize) because its transition Aˉt=eAΔt<1\discA_t = e^{\statemat \stepsize_t} < 1 decays the contribution of older positions. Gating (§11.3) restores that decay.

Exercise 11.3 (short)

Show that a constant gate γtγ\gamma_t \equiv \gamma reduces the GLA decay mask to RetNet’s Ltj=γtjL_{tj} = \gamma^{\,t-j}, and explain in one line why a constant gate makes the mask Toeplitz while an input-dependent γt\gamma_t does not.

Solution

Ltj=i=j+1tγi=i=j+1tγ=γtjL_{tj} = \prod_{i=j+1}^{t} \gamma_i = \prod_{i=j+1}^{t} \gamma = \gamma^{\,t-j}. This depends only on the lag tjt - j, so LL is constant along each diagonal — Toeplitz. With γt\gamma_t input-dependent the product i=j+1tγi\prod_{i=j+1}^t \gamma_i depends on which steps are spanned, not just how many, so equal-lag entries on a diagonal differ — the mask is no longer Toeplitz. (The companion’s RetNet mask has Toeplitz residual exactly 00.)

Exercise 11.4 (theory) — solution in §11.9

Prove the recurrent–parallel equivalence of §11.2: the recurrent matrix-state form and the masked-parallel form compute the same output, and state the time/memory cost of each.

Exercise 11.5 (theory) — solution in §11.9

Prove the gated-linear-attention duality of §11.3, and identify the exact correspondence logγi=AiΔi\log \gamma_i = \statemat_i \stepsize_i under which the scalar-gate GLA mask coincides with the Chapter 9 decay mask of Theorem 9.5.

Exercise 11.6 (theory) — solution in §11.9

(a) Prove the FFT-causality theorem of §11.4 — that padding past L\seqlen (to 2L2\seqlen) lets the FFT compute the causal convolution, while the un-padded transform does not. (b) Prove the rank bound of the §11.6 capacity proposition, and connect it to the K/dk\sqrt{K/d_k} growth of retrieval error in the §11.6 capacity figure.

11.9 Full solutions to theory exercises

Solution to Exercise 11.4

Unrolling the recurrence from S0=0S_0 = 0 gives St=jtϕ(kj)vjS_t = \sum_{j \le t} \phi(k_j) v_j^\top. Hence

Stϕ(qt)=jtvjϕ(kj)ϕ(qt)=jt(ϕ(qt)ϕ(kj))vj.S_t^\top \phi(q_t) = \sum_{j \le t} v_j\, \phi(k_j)^\top \phi(q_t) = \sum_{j \le t} \big(\phi(q_t)^\top \phi(k_j)\big)\, v_j .

Row tt of the parallel form is [(L(QϕKϕ))V]t=jLtj(ϕ(qt)ϕ(kj))vj=jt(ϕ(qt)ϕ(kj))vj[(L \circ (Q_\phi K_\phi^\top))V]_t = \sum_j L_{tj}\,(\phi(q_t)^\top \phi(k_j))\, v_j = \sum_{j \le t}(\phi(q_t)^\top\phi(k_j)) v_j, since Ltj=1[jt]L_{tj} = \mathbb{1}[j \le t]. The two are identical. The recurrent form stores only StRdk×dvS_t \in \mathbb{R}^{d_k \times d_v} and does O(dkdv)O(d_k d_v) work per step: O(Ldkdv)O(\seqlen d_k d_v) time, O(dkdv)O(d_k d_v) memory. The parallel form materializes the L×L\seqlen \times \seqlen score matrix: O(L2d)O(\seqlen^2 d) time. \blacksquare

Solution to Exercise 11.5

Unrolling the gated recurrence,

St=jt(i=j+1tdiag(γi))ϕ(kj)vj=jtdiag ⁣(i=j+1tγi)ϕ(kj)vj.S_t = \sum_{j \le t} \Big(\textstyle\prod_{i=j+1}^{t} \mathrm{diag}(\gamma_i)\Big) \phi(k_j) v_j^\top = \sum_{j \le t} \mathrm{diag}\!\Big(\textstyle\prod_{i=j+1}^{t}\gamma_i\Big)\phi(k_j)v_j^\top .

Contracting with ϕ(qt)\phi(q_t),   yt=jtvjcϕ(qt)c(i=j+1tγi,c)ϕ(kj)c\;y_t = \sum_{j\le t} v_j \sum_c \phi(q_t)_c \big(\prod_{i=j+1}^t \gamma_{i,c}\big)\phi(k_j)_c. Writing gt,c=itlogγi,cg_{t,c} = \sum_{i\le t} \log\gamma_{i,c} gives i=j+1tγi,c=egt,cgj,c\prod_{i=j+1}^t \gamma_{i,c} = e^{g_{t,c}-g_{j,c}}, so with q~t=ϕ(qt)egt\tilde q_t = \phi(q_t)\odot e^{g_t} and k~j=ϕ(kj)egj\tilde k_j = \phi(k_j)\odot e^{-g_j} we get yt=jt(q~tk~j)vj=[(Lγ(QϕKϕ))V]ty_t = \sum_{j\le t}(\tilde q_t^\top \tilde k_j) v_j = [(L_\gamma \circ (Q_\phi K_\phi^\top))V]_t with Lγ,tj=i=j+1tγiL_{\gamma,tj} = \prod_{i=j+1}^t \gamma_i. For a scalar gate γi,c=γi\gamma_{i,c} = \gamma_i, the mask is Lγ,tj=i=j+1tγiL_{\gamma,tj} = \prod_{i=j+1}^t \gamma_i; under logγi=AiΔi\log\gamma_i = \statemat_i \stepsize_i this is exp(i=j+1tAiΔi)\exp(\sum_{i=j+1}^t \statemat_i \stepsize_i), exactly the Theorem 9.5 mask Ltj=exp(Ai=j+1tΔi)L_{tj} = \exp(\statemat \sum_{i=j+1}^t \stepsize_i). The off-diagonal blocks of LγL_\gamma have rank one (a product of a column egte^{g_t} and a row egje^{-g_j}), so LγL_\gamma is 1-semiseparable. The companion confirms the masks are bit-identical (0.00.0 difference). \blacksquare

Solution to Exercise 11.6

(a) Zero-pad u,ku, k to length N=2LN = 2\seqlen. The pointwise product of their DFTs is the cyclic convolution (uNk)[n]=mu[m]k[(nm)modN](u \circledast_N k)[n] = \sum_m u[m]\,k[(n-m)\bmod N]. For 0n<L0 \le n < \seqlen and 0m<L0 \le m < \seqlen (both zero beyond L1\seqlen - 1), the index nmn - m ranges over (L1)(L1)-(\seqlen{-}1) \dots (\seqlen{-}1). A negative index wraps to N+(nm)[L+1,2L1]N + (n-m) \in [\seqlen{+}1,\,2\seqlen{-}1], where the padded kk is zero — so wrapped terms contribute nothing, leaving (uNk)[n]=mnu[m]k[nm](u\circledast_N k)[n] = \sum_{m\le n} u[m]k[n-m], the causal linear convolution. With N=LN = \seqlen (no padding) the same wrap sends m>nm > n to L+(nm)[1,L1]\seqlen + (n-m) \in [1, \seqlen{-}1], where kk is nonzero, injecting acausal terms. Hence any N2L1N \ge 2\seqlen - 1 suffices (the companion uses 2L2\seqlen), while N=LN = \seqlen fails. The companion’s un-padded variant differs from the oracle by 7.17.1. \blacksquare

(b) S=i=1Kϕ(ki)viS = \sum_{i=1}^K \phi(k_i) v_i^\top is a sum of KK rank-one matrices, so rankSK\operatorname{rank} S \le K; it is also dk\le d_k (its columns lie in Rdk\mathbb{R}^{d_k}) and dv\le d_v. Thus rankSmin(K,dk,dv)\operatorname{rank} S \le \min(K, d_k, d_v). If K>dkK > d_k, the KK vectors ϕ(ki)Rdk\phi(k_i) \in \mathbb{R}^{d_k} are linearly dependent: some ϕ(km)=imciϕ(ki)\phi(k_m) = \sum_{i\ne m} c_i \phi(k_i), so the readout v^m=Sϕ(km)\hat v_m = S^\top\phi(k_m) is a fixed linear combination of the readouts for the other keys and cannot independently equal vmv_m for arbitrary value assignments. Exact recall of KK independent bindings is therefore impossible. The K/dk\sqrt{K/d_k} growth in the §11.6 capacity figure is the quantitative shadow of this: with random unit keys the interference ijkj,kivi\sum_{i\ne j}\langle k_j, k_i \rangle v_i has K\sim K terms each of size 1/dk\sim 1/\sqrt{d_k}, so its norm scales as K/dk\sqrt{K/d_k} — zero only when the keys are orthonormal, which requires KdkK \le d_k. \blacksquare

11.10 Companion code

The companions live in companions/ch11/{jax,torch,julia} and are float64 throughout (§11.2 explains why). The JAX modules are canonical and produce every figure; the torch modules mirror them and are checked for cross-framework parity to <109< 10^{-9}; the Julia module ports Hyena’s convolution to a third language using only the standard library.

# JAX (canonical; emits the four figures to public/figures/ch11/)
PYTHONPATH=. python companions/ch11/jax/linear_attention.py
PYTHONPATH=. python companions/ch11/jax/gated_linear_attention.py
PYTHONPATH=. python companions/ch11/jax/fftconv.py
PYTHONPATH=. python companions/ch11/jax/mqar_recall.py

# Tests: JAX identities (< 1e-12), torch parity (< 1e-9), Julia FFT identity
.venv/bin/pytest companions/ch11/jax companions/ch11/torch -q
julia --project=companions/ch11/julia companions/ch11/julia/runtests.jl
  • linear_attention.py — the matrix-state recurrence and its masked-parallel twin (§11.2), the capacity-rank object (§11.6), and the float64-vs-float32 normalizer demonstration.
  • gated_linear_attention.py — GLA/RetNet gating, the decay masks, and the cross-chapter bridge to Theorem 9.5 built on Chapter 9’s own segsum (§11.3).
  • fftconv.py — Hyena’s 2L2\seqlen-padded FFT convolution against the explicit Toeplitz oracle, plus the un-padded variant that fails causality (§11.4). The torch port documents the buffers-vs-Parameters distinction (a fixed mask is a buffer; a learned filter is a Parameter).
  • mqar_recall.py — the fixed-weight associative-recall mechanism behind §11.6: exact recall below capacity, K/dk\sqrt{K/d_k} degradation above it, and the softmax oracle at zero error.