Finetuning Pretrained Transformers into RNNs

Jungo Kasai, Hao Peng, Yizhe Zhang, Dani Yogatama, Gabriel Ilharco, Nikolaos Pappas, Yi Mao, Weizhu Chen, Noah A. Smith

Introduction

Transformers have revolutionized natural language processing, yet their reliance on the softmax attention mechanism imposes a significant computational burden. Specifically, the time and memory complexity of standard attention scales quadratically ($O(N^2)$) with sequence length $N$. This scaling makes long-context inference prohibitively expensive. While "Linear Transformers" offer a theoretical solution by approximating attention with kernels, they are difficult to train from scratch and often fail to match the performance of their quadratic counterparts.

This paper introduces Transformer-to-RNN (T2R), a "swap-then-finetune" methodology that converts pretrained Transformers into efficient Recurrent Neural Networks (RNNs). By leveraging the knowledge already embedded in large-scale pretrained models, T2R achieves near-Transformer accuracy with constant-time inference and a fixed memory footprint.

The T2R Methodology

The core of the T2R approach is a three-step pipeline designed to transition from quadratic attention to linear recurrence without losing the model's learned capabilities.

  1. Architecture Swap: The standard softmax attention is replaced with a linear-complexity alternative. Unlike previous methods that use fixed kernels, T2R employs a learned feature map—a single-layer Multi-Layer Perceptron (MLP) with ReLU activation—to project queries and keys into a hidden feature space.
  2. Recurrent Reformulation: By exploiting the associativity of matrix multiplication, the attention operation is rearranged into a recurrent state update. This transformation allows the model to process sequences token-by-token while maintaining a fixed-size context buffer, effectively turning the Transformer into an RNN.
  3. Efficient Finetuning: The converted model is finetuned on the target task. Because the model inherits weights from a pretrained Transformer, this process is significantly faster—often up to 5x—than training a linear model from scratch.
Architectural comparison between the standard Pretrained Transformer and the T2R recurrent formulation.

Key Insights: Why Pretraining and MLPs Matter

The success of T2R hinges on two critical design choices: the use of pretrained weights and learned feature maps.

  • Pretraining as a Foundation: The authors demonstrate that starting from a pretrained Transformer is the "secret sauce" for high-performing linear models. A T2R model finetuned from a pretrained base significantly outperforms the same architecture trained from scratch (e.g., 19.6 vs 20.8 perplexity on WikiText-103).
  • Learned Feature Maps: Fixed approximations like ELU or random features often struggle to capture the "peaky" attention distributions characteristic of Transformers. The learned MLP feature map allows the model to adapt its attention patterns during finetuning to better mimic the original teacher model.

Experimental Results

The T2R method was evaluated on language modeling (WikiText-103) and machine translation (WMT benchmarks).

Accuracy and Performance

On WikiText-103, T2R achieved a test perplexity of 19.6, narrowing the gap to the original Transformer (18.5). In translation tasks, T2R produced BLEU scores nearly identical to the baseline (e.g., 28.7 vs 28.9 on WMT14 EN-DE).

Inference Efficiency

The most dramatic improvements are seen in decoding speed and memory usage. While the decoding speed of standard Transformers plummets as sequence length increases, T2R maintains a high, near-constant throughput. For a sequence of 512 tokens, T2R is approximately 4x faster than the original Transformer. Furthermore, the fixed RNN state results in a 70% reduction in memory footprint for long sequences.

Decoding speed (tokens/s) vs. sentence length, showing T2R's constant-time advantage over the quadratic Transformer.

Conclusion

T2R provides a practical path for deploying massive Transformer models in resource-constrained environments. By converting pretrained models into RNNs, researchers can enjoy the accuracy of state-of-the-art architectures with the efficiency of linear-time inference. Future work may further close the remaining accuracy gap through advanced distillation techniques or hybrid architectures that selectively retain quadratic layers.

Summary updated

BibTeX
@inproceedings{kasai2021finetuning,
  author = {Jungo Kasai and Hao Peng and Yizhe Zhang and Dani Yogatama and Gabriel Ilharco and Nikolaos Pappas and Yi Mao and Weizhu Chen and Noah A. Smith},
  title = {{Finetuning Pretrained Transformers into RNNs}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.830},
}
Figure