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, João Sacramento · OpenReview

Abstract

Recent architectural developments have enabled recurrent neural networks (RNNs) to reach and even surpass the performance of Transformers on certain sequence modeling tasks. These modern RNNs feature a prominent design pattern: linear recurrent layers interconnected by feedforward paths with multiplicative gating. Here, we show how RNNs equipped with these two design elements can exactly implement (linear) self-attention, the main building block of Transformers. By reverse-engineering a set of trained RNNs, we find that gradient descent in practice discovers our construction. In particular, we examine RNNs trained to solve simple in-context learning tasks on which Transformers are known to excel and find that gradient descent instills in our RNNs the same attention-based in-context learning algorithm used by Transformers. Our findings highlight the importance of multiplicative interactions in neural networks and suggest that certain RNNs might be unexpectedly implementing attention under the hood.

Introduction

For years, the deep learning community has viewed Transformers and Recurrent Neural Networks (RNNs) as distinct, competing families. Transformers excel at capturing long-range dependencies but suffer from "unbounded memory" costs as the KV cache grows with sequence length. RNNs offer constant-memory inference but have historically struggled to match Transformer performance. Recently, a new generation of "Linear RNNs" (such as S4, LRU, and Mamba) has emerged, rivaling Transformers on many tasks. This paper addresses a fundamental question: Why are these modern RNNs so effective? The authors hypothesize that these models are actually "attention layers in disguise."

Theoretical Framework: RNNs as Attention

The core of the paper is a constructive proof showing that a simplified gated linear RNN can exactly replicate a linear self-attention layer using a finite number of neurons. The "secret sauce" is multiplicative gating (input and output gates), which allows the RNN to simulate the key-value-query operations of attention.

The theoretical construction of a gated RNN implementing linear self-attention through input gating, recurrent accumulation, and output gating.

In this construction, the input gate computes the outer product corresponding to current key-values, the recurrent neurons accumulate these values over time, and the output gate performs the query-vector multiplication. This mechanism highlights that element-wise multiplication is the bridge: without it, an RNN is just a linear dynamical system; with it, it can compute the complex interactions necessary for attention.

Experimental Validation

The authors validated this theory by training "student" RNNs to mimic the output of a "teacher" linear attention layer. They also examined RNNs trained on In-Context Learning (ICL) tasks, such as linear regression and associative recall.

Key findings include:

  • Discovery via Gradient Descent: Gradient descent naturally pushes RNNs toward an attention-like weight configuration when trained on sequence tasks. Student RNNs achieved near-zero loss ($10^{-8}$) and their weight matrices showed the exact block structure predicted by the theoretical proof.
  • Identical Algorithms: In linear regression tasks, trained RNNs implemented a function virtually identical to one step of gradient descent—the same algorithm used by trained linear attention layers.
  • The GRU Limitation: Interestingly, while LSTMs can implement attention, standard GRUs cannot. The GRU's update gate forces a trade-off between the hidden state and the input that prevents the clean "outer product" accumulation required for attention.

The Overparametrization Threshold

A striking result is the identification of a "phase transition" in performance. RNNs only successfully "discovered" the attention mechanism once they reached a sufficient number of hidden neurons, roughly $d^2/2$ for a $d$-dimensional input.

Delta loss as a function of recurrent neurons, showing a sharp phase transition in performance once a sufficient construction size is reached.

Discussion and Future Work

While this work focuses on linear self-attention, standard Transformers use non-linear softmax attention. Whether RNNs can discover softmax-style attention remains an open question. Furthermore, the current theoretical construction is less parameter-efficient than native attention, suggesting that future research should explore more "attention-biased" RNN architectures. These findings suggest that certain RNNs might be unexpectedly implementing attention under the hood, providing a unified perspective on sequence modeling architectures.

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 João Sacramento},
  title = {{Gated recurrent neural networks discover attention}},
  year = {2023},
  eprint = {2309.01775},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.01775},
}
Figure