NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Gated recurrent neural networks discover attention

Nicolas Zucchet, Seijin Kobayashi, Yassir Akram, Johannes von Oswald, Maxime Larcher, Angelika Steger, Joao Sacramento · Submission to ICLR 2024 · Rejected

TL;DR

Modern recurrent neural networks (RNNs) that combine linear recurrence with multiplicative gating can mathematically implement exact linear self-attention using a finite number of neurons. Crucially, standard gradient descent naturally discovers this attention mechanism in practice: when trained on sequence prediction or in-context linear regression, gated RNNs learn the exact same attention-driven algorithms (such as one step of gradient descent) that Transformers use.

What Problem Does This Paper Address?

Transformers and RNNs are often viewed as fundamentally competing paradigms. Transformers offer powerful in-context learning and parallel training via self-attention, but scale quadratically in inference memory. Conversely, modern gated linear RNNs (such as Linear Recurrent Units, or LRUs) achieve constant-memory inference and rival Transformer performance on long-context benchmarks.

The paper asks a fundamental conceptual question: Are modern gated RNNs and attention-based Transformers truly distinct, or can gated RNNs natively implement self-attention under the hood? The authors explore whether the combination of linear recurrence and multiplicative gating (e.g., Gated Linear Units, or GLUs) provides a strong inductive bias to discover attention mechanisms without requiring infinite-width universality theorems.

Methods

  • Constructive Equivalence Proof: The authors show how a simplified gated linear RNN—defined by $h_{t+1} = \lambda \odot h_t + g^{\text{in}}(x_t)$ and $y_t = D g^{\text{out}}(h_t)$ with bilinear gating $g(x) = (W_m x) \odot (W_x x)$—can precisely implement causally-masked linear self-attention:
  • Input Gating ($g^{\text{in}}$): Computes the outer product of keys and values ($v_t k_t^\top$) and forms the query vector ($q_t$).
  • Recurrent Dynamics ($\lambda$): Uses perfect integrator neurons ($\lambda = 1$) to accumulate key-values over time, while query units use perfect reset ($\lambda = 0$) to pass only the current query.
  • Output Gating ($g^{\text{out}}$) & Readout ($D$): Computes the dot product between accumulated key-values and queries.
  • Architectural Analysis of Popular RNNs:
  • LRUs: Can implement linear self-attention using two stacked layers (or one layer if equipped with both input and output GLUs).
  • LSTMs: Theoretically capable of implementing linear attention in a single layer using the input gate, candidate cell state, and output gate.
  • GRUs: Mathematically cannot implement linear attention because their gating cannot isolate an input-only bilinear product without interference from the prior hidden state.
  • Reverse-Engineering Trained Networks: After training student gated RNNs via gradient descent, the authors prune inactive units, match polynomial expansions of the input-output mappings, and verify parameter structures against the theoretical construction.

Datasets

The authors evaluate the models on controlled synthetic benchmarks:

  1. Teacher-Student Imitation: Synthetically generated sequences ($L=32, d=4$ or $6$) where inputs $x_t \sim \mathcal{N}(0, I)$ and target labels are generated by a random teacher linear self-attention layer.
  2. In-Context Linear Regression: Sequences of pairs $(x_t, y_t)$ where $y_t = W^* x_t$, with $W^* \sim \mathcal{N}(0, \frac{1}{3} I)$ and inputs $x_t \sim \mathcal{U}(-\sqrt{3}, \sqrt{3})$. Models are prompted to predict $y_{T+1}$ given $x_{T+1}$. An out-of-distribution test set evaluates generalization on $W^*$ with doubled variance ($\sigma^2 = \frac{2}{3}$).

Key Insights and Ideas

  • Finite-Neuron Equivalence: Unlike classical universal approximation theorems requiring infinite hidden dimensions, linear attention requires only $d(d+1)/2 + d$ recurrent neurons (by exploiting the symmetry of outer products when key and value matrices match).
  • Gradient Descent Discovers the Exact Mechanism: When trained to mimic attention, gated RNNs spontaneously partition hidden units into exact memory integrators ($\lambda \approx 1$) for key-values and instantaneous pass-throughs ($\lambda \approx 0$) for queries, recovering the theoretical weight structure.
  • Emergence of Gradient Descent In-Context: When trained on linear regression, gated RNNs learn to execute one step of gradient descent with the mathematically optimal learning rate ($\eta^* = \frac{1}{T + d_x - 1/5}$)—the identical in-context algorithm that linear attention Transformers converge to.
  • Multiplication is the Secret Sauce: Replacing multiplicative gating with a 1-hidden-layer MLP of equal parameter count severely degrades the network's ability to discover attention.
  • The GRU Paradox: GRUs cannot mathematically implement attention, yet nonlinear GRUs outperform linear attention on in-context linear regression, indicating that alternative, highly capable in-context algorithms exist outside of attention-based mechanisms.

Key Results

  • Teacher Identification: Student gated RNNs replicate the teacher linear self-attention layer down to near-zero loss ($4.97 \times 10^{-8}$) and achieve a normalized polynomial coefficient distance of $3.73 \times 10^{-4}$, demonstrating true functional equivalence beyond the training distribution.
  • Minimal Overparameterization: Networks require only ~2× the minimal theoretical neuron threshold to achieve functional identification.
  • In-Context Regression Match: On $d=6$ linear regression, the trained gated RNN matches the optimal one-step gradient descent loss ($0.0945$ vs. theoretical $0.0947$), with non-gradient polynomial residual norms vanishing to $1.35 \times 10^{-3}$.
  • Architectural Comparison: In empirical training, standard LSTMs struggle to discover the attention solution despite theoretical capability, while adding input gating to LRUs (LRU+) drastically accelerates convergence to attention behavior.

Limitations and Future Work

  • Parameter Scaling Bottleneck: The exact construction requires $O(d^2)$ neurons and $O(d^4)$ parameters to represent a $d$-dimensional attention layer with $O(d^2)$ parameters, highlighting a parameter efficiency penalty unless the attention matrices are sparse.
  • Linearized Attention Only: The equivalence strictly covers linear self-attention, not standard softmax-based attention with unbounded context memory.
  • Toy Settings: Experiments are restricted to synthetic regression and teacher-student tasks; establishing whether deep gated RNNs discover attention on large-scale natural language tasks remains open.

Summary updated

BibTeX
@misc{zucchet2023gated,
  author = {Nicolas Zucchet and Seijin Kobayashi and Yassir Akram and Johannes von Oswald and Maxime Larcher and Angelika Steger and Joao Sacramento},
  title = {{Gated recurrent neural networks discover attention}},
  year = {2023},
  eprint = {2309.01775},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.01775},
}
Figure