Retentive Network: A Successor to Transformer for Large Language Models

Yutao Sun, Li Dong, Shaohan Huang, Shuming Ma, Yuqing Xia, Jilong Xue, Jianyong Wang, Furu WeiSubmission to ICLR 2024 · Rejected

TL;DR

Retentive Network (RetNet) breaks the "impossible triangle" of language modeling by simultaneously achieving parallel training, $O(1)$ inference cost, and Transformer-competitive performance. By replacing softmax attention with a multi-scale retention mechanism that features an explicit exponential decay, RetNet can be computed equivalently as a parallel operation during training, a recurrent state update during autoregressive generation, or a chunkwise hybrid for long sequences.

What Problem Does This Paper Address?

Modern Large Language Models (LLMs) rely on the Transformer architecture, which trains efficiently via parallel matrix operations across sequence lengths. However, generation is memory- and latency-bound: decoding token-by-token requires storing an ever-growing Key-Value (KV) cache ($O(N)$ memory and time complexity per step).

Existing attempts to solve this typically trade off capability:

  • Linearized attention approximates softmax with kernels, but suffers from degraded modeling quality and difficulty tracking positions.
  • Recurrent models & element-wise RNNs (e.g., RWKV) offer $O(1)$ inference but sacrifice either training parallelism or expressive capacity.
  • State space models (e.g., S4, Hyena) add architectural complexity or still suffer from $O(N)$ inference steps.

The authors set out to shatter this trade-off triangle without performance loss.

Methods

RetNet replaces standard multi-head self-attention with Multi-Scale Retention (MSR). The core trick lies in formalizing a linear recurrence and showing how it mathematically unrolls into parallel attention-like operations:

  1. Three Computation Paradigms:
  2. Parallel Representation (Training): Removing the non-linear $\text{softmax}$ allows the sequence mapping to be computed via standard matrix multiplication:
    $$\text{Retention}(X) = (QK^\top \odot D)V$$
    where $D_{nm} = \gamma^{n-m}$ for $n \ge m$ (and $0$ otherwise) acts as both a causal mask and an exponential decay over relative distance. Complex position embeddings (like xPos/RoPE) are absorbed directly into $Q$ and $K$.

  3. Recurrent Representation (Inference): At step $n$, hidden state updates follow an RNN-style linear relation:
    $$S_n = \gamma S_{n-1} + K_n^\top V_n, \quad \text{Retention}(X_n) = Q_n S_n$$
    This operates in $O(1)$ time and memory per step—eliminating the standard KV cache.

  4. Chunkwise Recurrent Representation (Long Contexts): Sequences are split into chunks. Within each chunk, computation is parallelized across GPUs; cross-chunk history is passed recurrently via hidden states. This achieves $O(N)$ linear complexity for long sequences.

  5. Gated Multi-Scale Design:
  6. Each retention head is assigned a distinct scalar decay factor $\gamma \in (0, 1)$ across heads, allowing different heads to capture varying temporal horizons.
  7. Outputs are gated with a Swish activation ($swish(X W_G) \odot Y$) and normalized using GroupNorm rather than LayerNorm to stabilize heads operating at disparate variance scales.

Datasets

  • Pre-training: A curated 100-billion-token corpus compiled from The Pile, C4, and The Stack, trained with a 2,048-token context length.
  • Language Modeling Evaluation: In-domain validation perplexity and out-of-domain evaluations on PG22 (Project Gutenberg 2019–2022), QMSum, GovReport, and SummScreen.
  • Downstream Tasks: Zero-shot and 4-shot evaluations on HellaSwag, BoolQ, COPA, PIQA, Winograd, Winogrande, and StoryCloze.
  • Note: No new datasets or benchmarks were created; all evaluations used established benchmarks.

Key Insights and Ideas

  • Softmax was the bottleneck to dual-form recurrence: Softmax couples every token to all previous tokens via normalization, forcing the model to retain all historical keys and values. Replacing softmax with an explicit relative decay matrix ($\gamma^{n-m}$) enables exact conversion into a constant-size recurrent state matrix $S_n \in \mathbb{R}^{d \times d}$.
  • Matrix-valued states beat element-wise recurrence: While models like RWKV reduce states to element-wise operations, RetNet maintains full $d \times d$ outer-product state matrices ($K_n^\top V_n$), preserving the high representation capacity of Transformers.
  • Scale-invariant normalization stabilizes linear attention: RetNet uses GroupNorm's scale-invariance to rescale retention scores and prevent numerical blowups/underflows without altering gradients or outputs.

Key Results

  • Language Modeling & Scaling: RetNet scales on par with standard Transformers, and empirically starts outperforming Transformers once scaled beyond 2B parameters. On downstream tasks at 6.7B, RetNet matched or outperformed Transformers (69.51% vs. 66.07% average 0-shot accuracy).
  • Outperforming Efficient Architectures: At 200M scale, RetNet outperformed Linear Transformers, RWKV, H3, and Hyena across both in-domain perplexity (26.05 vs. 29.97 for H3) and long-document datasets.
  • Inference Speed & Memory:
  • For a 6.7B model with an 8k context, RetNet decodes $8.4\times$ faster and saves 70% GPU memory compared to a Transformer with KV cache.
  • Memory consumption remains flat across sequence length (additional state memory is only ~3% over base weights).
  • Latency is virtually flat across varying batch sizes and context lengths.
  • Training Efficiency: Using pure PyTorch without custom CUDA kernels, chunkwise RetNet trained with 25–50% less memory and achieved $7\times$ the speed of standard Transformers, matching or exceeding highly optimized FlashAttention implementations.

Limitations and Future Work

  • Lack of Custom Kernels: All reported training experiments relied on vanilla PyTorch code; custom fused kernels (akin to FlashAttention) are left to future work.
  • Model Scale: The largest pre-trained models evaluated for general language modeling in this paper reached 6.7B parameters on 100B tokens (though inference was benchmarked up to a 70B configuration). Verification at hundreds of billions or trillions of tokens is still required.
  • Edge Deployment: Due to constant memory overhead and $O(1)$ decoding, the authors highlight edge and mobile deployment as a key area for exploration.

Summary updated

Abstract

In this work, we propose Retentive Network (RetNet) as a foundation architecture for large language models, simultaneously achieving training parallelism, low-cost inference, and good performance. We theoretically derive the connection between recurrence and attention. Then we propose the retention mechanism for sequence modeling, which supports three computation paradigms, i.e., parallel, recurrent, and chunkwise recurrent. Specifically, the parallel representation allows for training parallelism. The recurrent representation enables low-cost $O(1)$ inference, which improves decoding throughput, latency, and GPU memory without sacrificing performance. The chunkwise recurrent representation facilitates efficient long-sequence modeling with linear complexity, where each chunk is encoded parallelly while recurrently summarizing the chunks. Experimental results on language modeling show that RetNet achieves favorable scaling results, parallel training, low-cost deployment, and efficient inference. The intriguing properties make RetNet a strong successor to Transformer for large language models.
BibTeX
@misc{sun2023retentive,
  author = {Yutao Sun and Li Dong and Shaohan Huang and Shuming Ma and Yuqing Xia and Jilong Xue and Jianyong Wang and Furu Wei},
  title = {{Retentive Network: A Successor to Transformer for Large Language Models}},
  year = {2023},
  eprint = {2307.08621},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.08621},
}
Figure