RWKV: Reinventing RNNs for the Transformer Era

Bo Peng, Eric Alcaide, Quentin Anthony, Alon Albalak, Samuel Arcadinho, Stella Biderman, Huanqi Cao, Xin Cheng, Michael Chung, Matteo Grella, Kranthi Kiran Gv, Xuzheng He, Haowen Hou, Jiaju Lin, Przemyslaw Kazienko, Jan Kocon, Jiaming Kong, Bartlomiej Koptyra, Hayden Lau, Krishna Sri Ipsit Mantri, Ferdinand Mom, Atsushi Saito, Guangyu Song, Xiangru Tang, Bolun Wang, Johan S. Wind, Stanislaw Wozniak, Ruichong Zhang, Zhenyuan Zhang, Qihang Zhao, Peng Zhou, Qinghua Zhou, Jian Zhu, Rui-Jie Zhu

TL;DR

The RWKV (Receptance Weighted Key Value) architecture bridges the gap between the parallelizable training of Transformers and the efficient, constant-memory inference of Recurrent Neural Networks (RNNs). By scaling a dense RNN to 14 billion parameters for the first time, the authors demonstrate that linear-complexity models can match the performance of state-of-the-art Transformers while drastically reducing computational overhead for long sequences.

What Problem Does This Paper Address?

For years, the NLP landscape has been dominated by a fundamental trade-off. Transformers offer excellent performance and can be trained in parallel, but their self-attention mechanism has a "quadratic bottleneck": memory and compute requirements scale with the square of the sequence length ($O(T^2)$). This makes processing long documents or maintaining long-term conversations prohibitively expensive.

RNNs, on the other hand, scale linearly ($O(T)$) and use constant memory during inference. However, they are notoriously difficult to train because their sequential nature prevents parallelization across time steps, and they often struggle with "vanishing gradients," losing track of information from the distant past. RWKV aims to provide the best of both worlds: the scalability and performance of a Transformer with the efficiency of an RNN.

Methods

The core innovation of RWKV is a novel linear attention mechanism that allows the model to be formulated in two mathematically equivalent ways:

  1. Transformer-like (Parallel Mode): During training, the model can be parallelized across the temporal dimension, similar to a standard Transformer, by treating the interaction as a specialized convolution.
  2. RNN-like (Serial Mode): During inference, the model can be written as a recurrent update, where each new token only requires the current input and a small, fixed-size "hidden state" from the previous step.

The architecture consists of stacked residual blocks containing two main components: Time-Mixing and Channel-Mixing.

  • WKV Operator: Instead of the standard $QK^T$ dot-product attention, RWKV uses a "Weighted Key Value" operator. It employs a trainable, channel-wise time decay vector ($w$) that determines how quickly information from past tokens fades away.
  • Token Shift: A simple but effective trick where the model linearly interpolates between the current input and the input from the previous time step, helping the model capture local temporal dependencies.
  • Stability Techniques: To train deep models (up to 40 layers), the authors introduced "Small Init Embedding" and custom initialization strategies to ensure gradients remain stable throughout the network.
Figure 2: Elements within an RWKV block (left) and the complete RWKV residual block, equipped with a final head for language modeling (right).

Datasets

The models were trained on The Pile, a massive 800GB diverse text dataset. Evaluation was conducted across a battery of standard benchmarks:

  • NLP Tasks: 12 benchmarks including ARC (Easy & Challenge), BoolQ, COPA, HellaSwag, LAMBADA, OpenBookQA, PIQA, ReCoRD, SciQ, and Winogrande.
  • Long Context: The Long Range Arena (LRA) benchmark, which tests the ability to handle sequences up to 16,000 tokens.
  • Perplexity: Evaluated on the Enwik8 dataset.

Key Insights and Ideas

  • RNNs Can Scale: The most significant "aha moment" is that the perceived performance ceiling for RNNs was likely a scaling issue, not a fundamental architectural flaw. At 14B parameters, RWKV is the largest dense RNN ever trained and holds its own against Transformer giants.
  • Linear Scaling is Real: As shown in Figure 4, RWKV follows the same log-log scaling laws as Transformers. This suggests that as we throw more compute and data at these models, they continue to improve predictably.
  • Prompt Engineering Matters (A Lot): Because RWKV is an RNN, it cannot "look back" at an instruction once it has moved past it. The authors found that re-ordering prompts to put the question after the context (rather than before) significantly improved performance, sometimes doubling the F1 score.
  • Information Funneling: Causal tracing revealed that RWKV retrieves information early in the layers and then propagates it through time, a distinct behavior compared to the layer-by-layer "re-reading" seen in standard Transformers.
Figure 4: Scaling laws curves for RWKV models

Key Results

  • Competitive Performance: RWKV models perform on par with similarly sized, well-known Transformers like Pythia, OPT, and BLOOM across a wide range of NLP tasks.
  • Inference Efficiency: RWKV exhibits linear scaling in text generation time. While Transformers get slower as the generated text gets longer, RWKV maintains a near-constant speed per token.
  • Long Context Mastery: On the LRA benchmark, RWKV outperformed most "efficient Transformer" variants (like Longformer or Performer) and ranked second only to the S4 state-space model.
  • Context Utilization: Figure 6 demonstrates that RWKV effectively uses longer context windows to achieve lower test loss, proving it isn't just "forgetting" everything beyond a few hundred tokens.
Figure 6: RWKV shows decreasing mean test loss as a function of context length on the Pile (Gao et al., 2020)

Limitations and Future Work

Despite its success, RWKV has clear limitations. The "funneling" of information into a single hidden state vector creates a bottleneck; it may struggle with tasks requiring the recall of very specific, minute details from a massive context (the "needle in a haystack" problem).

Future work aims to:

  • Refine the time-decay formulations to increase model expressivity.
  • Explore initial states to better prime the model for specific tasks.
  • Apply the architecture to encoder-decoder and multimodal (image/video) settings, where the efficiency gains could be even more impactful.

Summary updated

BibTeX
@inproceedings{peng2023rwkv,
  author = {Bo Peng and Eric Alcaide and Quentin Anthony and Alon Albalak and Samuel Arcadinho and Stella Biderman and Huanqi Cao and Xin Cheng and Michael Chung and Matteo Grella and Kranthi Kiran GV and Xuzheng He and Haowen Hou and Jiaju Lin and Przemyslaw Kazienko and Jan Kocon and Jiaming Kong and Bartlomiej Koptyra and Hayden Lau and Krishna Sri Ipsit Mantri and Ferdinand Mom and Atsushi Saito and Guangyu Song and Xiangru Tang and Bolun Wang and Johan S. Wind and Stanislaw Wozniak and Ruichong Zhang and Zhenyuan Zhang and Qihang Zhao and Peng Zhou and Qinghua Zhou and Jian Zhu and Rui-Jie Zhu},
  title = {{RWKV: Reinventing RNNs for the Transformer Era}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.936},
}
Figure