Efficient Attention using a Fixed-Size Memory Representation

Denny Britz, Melody Y. Guan, Minh-Thang LuongEMNLP 2017

Overview

Standard sequence-to-sequence (seq2seq) models rely on attention mechanisms that scale quadratically with sequence length, creating a significant computational bottleneck for long-form text. This paper introduces Memory-Based Attention, a mechanism that replaces the traditional $O(|S| \cdot |T|)$ complexity with a fixed-size memory representation. By predicting a constant number of context vectors during encoding, the model achieves inference speedups of 20% or more while maintaining translation quality comparable to standard attention models.

The Attention Bottleneck

In standard seq2seq architectures, the decoder must "look back" at every single hidden state produced by the encoder to calculate a weighted average for every word it generates. If the source sequence has length $|S|$ and the target has length $|T|$, the complexity is $O(|S| \cdot |T|)$. As sequences grow—such as in document summarization or paragraph translation—this quadratic scaling becomes prohibitively slow. The authors argue that humans maintain a mental "summary" or memory of the source rather than re-reading the entire text for every translated word.

Proposed Method: Memory-Based Attention

The proposed approach shifts the computational load to the encoding phase by introducing a fixed-size memory matrix $C \in \mathbb{R}^{K \times D}$, where $K$ is a small constant (e.g., 32 or 64) and $D$ is the hidden state size.

Comparison of regular attention vs. the proposed memory-based attention architecture.

The process involves two main stages:

  1. Encoding Phase: At each source timestep $t$, the model predicts a score vector $\alpha_t \in \mathbb{R}^K$. The memory matrix $C$ is then formed as a weighted sum of the encoder states.
  2. Decoding Phase: Instead of attending to all $|S|$ encoder states, the decoder only attends to the $K$ rows of the memory matrix. It predicts a score vector $\beta$ to weight these $K$ rows.

This reduces the total complexity to $O(K(|S| + |T|))$, allowing the model to scale linearly with sequence length.

Specialization and Position Encodings

To prevent the $K$ memory slots from learning redundant information, the authors introduced position encodings. This encourages different rows in the memory matrix $C$ to specialize in different parts of the source sentence. For example, $C_1$ might focus on the beginning of the sentence, while $C_K$ focuses on the end.

Visualization of attention specialization, showing how different memory slots (k) focus on specific relative positions (t/T) in the input sequence.

Visualization of the attention scores confirms that even without explicit constraints, the model naturally learns to distribute its attention across the sequence, effectively summarizing the input into the fixed-size memory.

Evaluation and Results

The researchers evaluated the model on a synthetic copying task and real-world WMT’17 translation tasks (English to Czech, German, Finnish, and Turkish).

  • Inference Speedup: The model achieved a 20% speedup in decoding time on real-world tasks. On synthetic tasks with longer sequences (length 200), the speedup reached approximately 30%.
  • Performance Parity: Despite the fixed-size constraint, BLEU scores remained nearly identical to the standard Bahdanau attention baseline across all datasets.
  • Scaling: Performance improves as $K$ increases, with $K=64$ typically being sufficient to match baseline quality.

Conclusion

Memory-Based Attention offers a practical solution to the quadratic scaling of standard attention mechanisms. By summarizing the input into a fixed-size memory, models can achieve significant inference speedups without sacrificing translation quality. Future work may explore dynamic adjustments of $K$ or applying this bottleneck to extremely long-form content where the efficiency gains would be even more pronounced.

Summary updated

BibTeX
@inproceedings{britz2017efficient,
  author = {Denny Britz and Melody Y. Guan and Minh-Thang Luong},
  title = {{Efficient Attention using a Fixed-Size Memory Representation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2017},
  doi = {10.18653/v1/d17-1040},
}
Figure