arXiv(opens in a new tab)Computer ScienceMay 2023Cited by 14 papers on PaperVantage

Manuscript Offprint

arXiv:2305.13048

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, G. Kranthikiran, Xingjian Du, Xuzheng He, Haowen Hou, Przemyslaw Kazienko, Jan Kocoń, Jiaming Kong, Bartlomiej Koptyra, Hayden Lau, Krishna Sri Ipsit Mantri, Ferdinand Mom, Atsushi Saito, Xiangru Tang, Bolun Wang, J. S. Wind, Stansilaw Wozniak, Ruichong Zhang, Zhenyuan Zhang, Qihang Zhao, P. Zhou, Jian Zhu, Rui Zhu

Abstract

Transformers have revolutionized almost all natural language processing (NLP) tasks but suffer from memory and computational complexity that scales quadratically with sequence length. In contrast, recurrent neural networks (RNNs) exhibit linear scaling in memory and computational requirements but struggle to match the same performance as Transformers due to limitations in parallelization and scalability. We propose a novel model architecture, Receptance Weighted Key Value (RWKV), that combines the efficient parallelizable training of transformers with the efficient inference of RNNs.

Our approach leverages a linear attention mechanism and allows us to formulate the model as either a Transformer or an RNN, thus parallelizing computations during training and maintains constant computational and memory complexity during inference. We scale our models as large as 14 billion parameters, by far the largest dense RNN ever trained, and find RWKV performs on par with similarly sized Transformers, suggesting future work can leverage this architecture to create more efficient models. This work presents a significant step towards reconciling trade-offs between computational efficiency and model performance in sequence processing tasks.

1 Introduction

Deep learning has greatly advanced artificial intelligence, impacting a range of scientific and industrial uses. These often involve complex sequential data processing tasks such as natural language understanding, conversational AI, time-series analysis, and indirectly sequential formats like images and graphs Brown et al. (2020); Ismail Fawaz et al. (2019); Wu et al. (2020); Albalak et al. (2022). Predominant among these techniques include RNNs and Transformers Vaswani et al. (2017), each with specific benefits and drawbacks. RNNs require less memory, particularly for handling long sequences. However, they suffer from the vanishing gradient problem and non-parallelizability in the time dimension during training, limiting their scalability Hochreiter (1998); Le and Zuidema (2016).

Figure 1: Average performance of RWKV models compared to transformers across twelve NLP tasks. For further details, see section 5.

Transformers emerged as a powerful alternative, adept at managing local and long-range dependencies and supporting parallelized training Tay et al. (2022). Models such as GPT-3 Brown et al. (2020), ChatGPT OpenAI (2022); Kocoń et al. (2023), LLaMA Touvron et al. (2023), and Chinchilla Hoffmann et al. (2022) showcase the potential of Transformers in NLP. However, the self-attention mechanism’s quadratic complexity makes it computationally and memory intensive for tasks involving long sequences and constrained resources. This has stimulated research to enhance Transformers’ scalability, sometimes sacrificing some of their effectiveness Wang et al. (2020); Zaheer et al. (2020); Dao et al. (2022a).

ModelTimeSpace
TransformerO(T2d)O(T^{2}d)O(T2+Td)O(T^{2}+Td)
ReformerO(Tlog⁡Td)O(T\log{T}d)O(Tlog⁡T+Td)O(T\log{T}+Td)
PerformerO(Td2log⁡d)O(Td^{2}\log{d})O(Tdlog⁡d+d2log⁡d)O(Td\log{d}+d^{2}\log{d})
Linear TransformersO(Td2)O(Td^{2})O(Td+d2)O(Td+d^{2})
AFT-fullO(T2d)O(T^{2}d)O(Td)O(Td)
AFT-localO(Tsd)O(Tsd)O(Td)O(Td)
MEGAO(cTd)O(cTd)O(cd)O(cd)
RWKV (ours)O(Td)O(\mathbf{Td})O(d)O(\mathbf{d})

Table 1: Inference complexity comparison with different Transformers. Here TT denotes the sequence length, dd the feature dimension, cc is MEGA’s chunk size of quadratic attention, and ss is the size of a local window for AFT.

To tackle these challenges, we introduce the Receptance Weighted Key Value (RWKV) model, combining the strengths of RNNs and Transformers while circumventing key drawbacks. RWKV alleviates memory bottleneck and quadratic scaling associated with Transformers Katharopoulos et al. (2020) with efficient linear scaling, while maintaining the expressive properties of the Transformer, such as parallelized training and robust scalability. RWKV reformulates the attention mechanism with a variant of linear attention, replacing traditional dot-product token interaction with more effective channel-directed attention. This implementation, without approximation, offers the lowest computational and memory complexity; see Table 1.

The motivation behind RWKV is to balance computational efficiency with expressive capacity in neural networks. It offers a solution for handling large-scale models with billions of parameters, exhibiting competitive performance at a reduced computational cost. Experiments suggest RWKV addresses scaling and deployment challenges in AI, especially for sequential data processing, pointing towards more sustainable and efficient AI models.

Our contributions in this paper are as follows:

  • The introduction of RWKV, a novel architecture combining RNNs and Transformer advantages while mitigating their limitations.
  • Detailed experiments demonstrating RWKV’s performance and efficiency on benchmark datasets for large-scale models.
  • The release of pretrained models, from 169 million to 14 billion parameters, trained on the Pile Gao et al. (2020); Biderman et al. (2022).

References (31)