Abstract
Transformers have outperformed recurrent neural networks (RNNs) in natural language generation. But this comes with a significant computational cost, as the attention mechanism's complexity scales quadratically with sequence length. Efficient transformer variants have received increasing interest in recent works. Among them, a linear-complexity recurrent variant has proven well suited for autoregressive generation. It approximates the softmax attention with randomized or heuristic feature maps, but can be difficult to train and may yield suboptimal accuracy. This work aims to convert a pretrained transformer into its efficient recurrent counterpart, improving efficiency while maintaining accuracy. Specifically, we propose a swap-then-finetune procedure: in an off-the-shelf pretrained transformer, we replace the softmax attention with its linear-complexity recurrent alternative and then finetune. With a learned feature map, our approach provides an improved tradeoff between efficiency and accuracy over the standard transformer and other recurrent variants. We also show that the finetuning process has lower training cost relative to training these recurrent variants from scratch. As many models for natural language tasks are increasingly dependent on large-scale pretrained transformers, this work presents a viable approach to improving inference efficiency without repeating the expensive pretraining process.
Introduction
Transformers have become the cornerstone of modern natural language processing (NLP). However, their core mechanism—softmax attention—suffers from a "long-sequence problem" due to its $O(N^2)$ computational complexity relative to sequence length $N$. During autoregressive generation, such as text completion, every new token requires a full look-back at all preceding tokens, leading to increasing latency and memory consumption as the sequence grows.
While "Linear Transformers" utilize kernel tricks to approximate attention with $O(N)$ complexity, they are often difficult to train from scratch and struggle to match the accuracy of standard quadratic models. This paper introduces T2R (Transformer-to-RNN), a method to bridge this gap by converting high-performing, pretrained Transformers into efficient, linear-complexity RNNs through a "swap-then-finetune" procedure.
Methodology: The T2R Procedure
The authors propose a three-step process to transform an existing Transformer model into a recurrent format:
- The Swap: The standard exponential similarity function in the attention mechanism is replaced with a dot product of feature maps: $sim(q, k) = \phi(q) \cdot \phi(k)$.
- Learnable Feature Map: Unlike previous methods that use fixed random features, T2R employs a learnable one-layer MLP with a ReLU activation. This ensures non-negative features for training stability and allows the model to learn optimal data projections.
- Recurrent Formulation: By leveraging the associativity of matrix multiplication, linear attention is reformulated as a recurrent state update. Instead of a growing KV cache, the model maintains a fixed-size "RNN state" matrix, enabling constant-time inference per token.
Following the swap, the model is initialized with pretrained Transformer weights and finetuned on the target task. This leverages the rich representations already learned during large-scale pretraining.
Experimental Setup
T2R was evaluated on several high-stakes benchmarks:
- Language Modeling: WikiText-103 (103M tokens).
- Machine Translation: WMT14 (English-German, English-French) and WMT17 (Chinese-English).
These datasets test the model's ability to handle long-range dependencies and maintain high-precision sequence mapping.
Key Results and Efficiency
The T2R approach demonstrates significant gains in inference efficiency while preserving competitive accuracy.
- Decoding Speed: T2R achieves a massive speedup in generation. As shown in benchmarking results, T2R maintains high throughput even as sentence length increases, whereas the standard Transformer's speed drops sharply due to its quadratic complexity.
- Memory Footprint: The method results in a 70% reduction in attention memory. Because the RNN state size is fixed, memory usage remains constant regardless of sequence length.
- Training Efficiency: Finetuning a pretrained model via T2R is approximately 5x faster in terms of GPU hours compared to training a linear transformer from scratch.
In terms of performance, T2R reached 19.6 perplexity on WikiText-103 (compared to 18.5 for the original Transformer), outperforming other linear baselines like ELU or RFA. In translation tasks, T2R matched the BLEU scores of original Transformer Large models.
Insights and Discussion
A key insight of this work is that pretraining serves as a powerful prior. T2R models initialized with pretrained weights outperformed those trained from scratch by over 2.0 perplexity points. Furthermore, the learnable MLP feature map allows the model to approximate the "peaky" distributions of standard softmax attention, which is crucial for accuracy.
The authors also explored "hybrid" architectures, where only a subset of layers are converted to T2R. For instance, keeping every fourth layer as quadratic attention can provide a "middle ground" that balances near-RNN speeds with higher accuracy.
Conclusion
T2R presents a practical pathway for deploying large-scale Transformers in resource-constrained or latency-sensitive environments. While a small performance gap remains, the massive gains in speed and memory efficiency make it a compelling alternative to standard architectures. Future work may explore more complex feature maps and the application of this conversion to massive models where the quadratic bottleneck is most severe.
Summary updated