Memformer: The Memory-Augmented Transformer

Qingyang Wu, Zhenzhong Lan, Jing Gu, Zhou Yu

Abstract

Transformers have reached remarkable success in sequence modeling. However, these models have efficiency issues as they need to store all the history token-level representations as memory. We present Memformer, an efficient neural network for sequence modeling, that utilizes an external dynamic memory to encode and retrieve past information. Our model achieves linear time complexity and constant memory space complexity when processing long sequences. We also propose a new optimization scheme, memory replay back-propagation (MRBP), which promotes long-range back-propagation through time with a significantly reduced memory requirement. Experimental results show that Memformer has achieved comparable performance compared to the baselines by using 8.1x less memory space and 3.2x faster on inference. Analysis of the attention pattern shows that our external memory slots can encode and retain important information through timesteps.

Introduction

Standard Transformers suffer from a "quadratic bottleneck": the computational cost grows with the square of the sequence length ($O(N^2)$). While many "efficient" Transformers reduce this to linear complexity ($O(N)$), they still require memory that grows linearly with the sequence length to store past information. Existing solutions like Transformer-XL use a cache of raw hidden states from previous segments. However, these raw states are not compressed, meaning the model needs a massive cache to "remember" distant context, leading to high memory consumption and slow processing. Memformer addresses this by processing theoretically infinite sequences with a fixed (constant) memory footprint.

Methods

The core of Memformer is the External Dynamic Memory (EDM)—a set of $k$ vectors (memory slots) that store high-level representations of the past.

Overall architecture of Memformer, showing the interaction between the encoder, decoder, and external dynamic memory.

The architecture consists of several key components:

  1. Memory Reading: Each encoder layer uses a cross-attention mechanism where the input sequence "attends" to the memory slots to retrieve relevant historical information.
  2. Memory Writing: At the final encoder layer, the memory slots are updated. A sparse attention pattern is used: each memory slot attends only to itself and the current input tokens, preventing slot interference.
  3. Biased Memory Normalization (BMN): To manage "forgetting," memory vectors are projected onto a sphere with a learnable bias, allowing the model to discard trivial information.
  4. Memory Replay Back-Propagation (MRBP): This novel training scheme recomputes the local computational graph during the backward pass, drastically reducing the memory needed for training on long sequences compared to standard Back-Propagation Through Time (BPTT).

Key Results and Insights

Memformer was evaluated on WikiText-103 and MNIST image generation. It achieves better perplexity than Transformer-XL base while using significantly fewer resources.

Comparison of FLOPs and GPU memory consumption between Memformer, Transformer-XL, and Vanilla Transformer.
  • Memory Efficiency: Memformer uses 8.1x less GPU memory than Transformer-XL in large-scale settings.
  • Inference Speed: It is 3.2x faster during inference on language modeling tasks.
  • Performance: On WikiText-103, Memformer achieved a perplexity of 22.74, outperforming both Transformer-XL base (23.94) and Compressive Transformer (23.23).
  • Compression as a Bottleneck: Forcing the model to compress history into a fixed number of slots encourages the learning of abstract, high-level representations.
  • Slot Specialization: Analysis shows that different memory slots specialize in tracking specific entities or acting as long-term storage.

Conclusion

Memformer provides a scalable solution for long-sequence modeling by maintaining a constant memory footprint. While the fixed memory size is a hyperparameter that requires tuning, the efficiency gains make it highly suitable for interactive systems and long-form document analysis. Future work may explore dynamic memory sizing or application to even more complex interactive tasks.

Summary updated

Abstract

Transformers have reached remarkable success in sequence modeling. However, these models have efficiency issues as they need to store all the history token-level representations as memory. We present Memformer, an efficient neural network for sequence modeling, that utilizes an external dynamic memory to encode and retrieve past information. Our model achieves linear time complexity and constant memory space complexity when processing long sequences. We also propose a new optimization scheme, memory replay back-propagation (MRBP), which promotes long-range back-propagation through time with a significantly reduced memory requirement. Experimental results show that Memformer has achieved comparable performance compared to the baselines by using 8.1x less memory space and 3.2x faster on inference. Analysis of the attention pattern shows that our external memory slots can encode and retain important information through timesteps.
BibTeX
@inproceedings{wu2022memformer,
  author = {Qingyang Wu and Zhenzhong Lan and Jing Gu and Zhou Yu},
  title = {{Memformer: The Memory-Augmented Transformer}},
  booktitle = {AACL/IJCNLP (Findings)},
  year = {2022},
  doi = {10.18653/v1/2022.findings-aacl.29},
}
Figure