NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Memformer: A Memory-Augmented Transformer for Sequence Modeling

Qingyang Wu, Zhenzhong Lan, Kun Qian, Jing Gu, Alborz Geramifard, Zhou Yu

Abstract

Memformer introduces a Transformer architecture equipped with an external, fixed-size dynamic memory that allows for linear time complexity and constant memory space complexity when processing long sequences. By utilizing a novel "Memory Replay Back-Propagation" (MRBP) training scheme, it achieves performance comparable to state-of-the-art models like Transformer-XL while using 8.1x less memory and running 3.2x faster during inference.

Introduction

Standard Transformers suffer from a quadratic computational bottleneck ($O(N^2)$), making them prohibitively expensive for very long sequences. While various "X-formers" have attempted to solve this with sparse or linear attention, most still require memory that grows linearly with sequence length. Existing recurrent approaches, like Transformer-XL, cache previous hidden states to extend context. However, these models don't effectively "compress" information; they store a massive history that eventually hits a theoretical maximum temporal range.

Memformer seeks to maintain a "theoretically infinite" temporal range using a fixed-size memory buffer, much like how humans encode sensory information into compressed neuronal representations.

Methods

Memformer employs an encoder-decoder architecture with several key innovations centered around an External Dynamic Memory (EDM). Instead of caching all previous hidden states, Memformer maintains a constant number of $k$ memory vectors (slots).

The Memformer architecture featuring an external dynamic memory with dedicated reader and writer modules.

Memory Reading & Writing

The encoder interacts with these slots through cross-attention. During "Reading," the input sequence attends to memory slots to retrieve past context. During "Writing," each memory slot attends to itself and the current encoder outputs to update its state. Crucially, slots do not attend to each other during writing, which prevents interference and helps preserve distinct information over time.

Biased Memory Normalization (BMN)

To prevent memory saturation and gradient instability, the authors introduced a forgetting mechanism. It uses a learnable bias vector and normalization to gradually "push" unused information toward a terminal state, effectively filtering out noise.

Memory Replay Back-Propagation (MRBP)

Training recurrent models over long sequences usually requires Back-Propagation Through Time (BPTT), which is memory-intensive. MRBP is a more efficient version of gradient checkpointing; it only traverses the critical path during the forward pass and recomputes the local computational graph during the backward pass, significantly saving GPU memory with minimal speed degradation.

Key Results

The model was evaluated on MNIST Autoregressive Image Generation and the WikiText-103 language modeling benchmark.

  • Efficiency: In large-scale settings, Memformer used 8.1x less memory and was 3.2x faster during inference than the baselines. As shown in the complexity analysis, Memformer maintains linear scaling as sequence length increases.
  • Image Generation: Memformer achieved a perplexity of 1.555, outperforming the Transformer-XL baseline (1.611) while using only 10% of the FLOPs.
  • Language Modeling: On WikiText-103, Memformer (with 4 encoders and 16 decoders) achieved a perplexity of 22.74, surpassing the Transformer-XL base model.
Comparison of computational complexity (FLOPs) across sequence lengths, showing Memformer's linear scaling.

Discussion and Insights

The most significant takeaway is that a fixed-size memory can outperform a raw-cache memory if the compression and update mechanisms are efficient. By restricting memory slots to only attend to themselves and the input, the model prevents "information bleeding," allowing different slots to specialize in retaining different types of long-term information. Furthermore, the BMN mechanism highlights that for a fixed-size memory to work, the model must have a learned way to discard trivial information.

While Memformer achieves constant memory space complexity, the fixed number of slots ($k$) acts as a bottleneck. For extremely complex or diverse sequences, a very small $k$ might not capture enough nuance. Future work could explore scaling Memformer to even larger datasets and investigating more sophisticated memory architectures for interactive systems like long-form dialogue agents.

Summary updated

BibTeX
@inproceedings{wu2022memformer,
  author = {Qingyang Wu and Zhenzhong Lan and Kun Qian and Jing Gu and Alborz Geramifard and Zhou Yu},
  title = {{Memformer: A Memory-Augmented Transformer for Sequence Modeling}},
  booktitle = {AACL/IJCNLP (Findings)},
  year = {2022},
  doi = {10.18653/v1/2022.findings-aacl.29},
}
Figure