Learning Hard Retrieval Decoder Attention for Transformers

Hongfei Xu, Qiuhui Liu, Josef van Genabith, Deyi Xiong

The Transformer architecture has set new standards in natural language processing, yet its autoregressive decoding process remains a significant bottleneck for real-time applications. In a paper presented at EMNLP 2021, researchers propose Hard Retrieval Attention, a method that streamlines the attention mechanism into a high-speed lookup operation, achieving a 1.43x speedup in translation without compromising quality.

The Computational Cost of Soft Attention

In standard Transformers, the decoder generates text one token at a time. For each new token, it must calculate "soft" attention scores across the entire sequence. This involves computing a weighted average of all previous tokens, requiring expensive matrix-vector multiplications and softmax normalizations. As sequence lengths increase, these operations consume more time and memory, creating a "speed limit" for inference.

The authors pose a fundamental question: Is it necessary to look at every token, or can the model function by selecting only the most relevant one?

Hard Retrieval Attention (HRA)

The proposed solution transforms the attention mechanism into a retrieval operation. Instead of calculating a weighted sum, the model is trained to identify and retrieve a single vector from memory for each attention head.

  1. Hard Selection: During inference, the model skips the softmax and normalization steps. It simply finds the index of the highest dot-product score and retrieves that specific vector.
  2. Computational Savings: This approach replaces a full matrix-vector multiplication with a simple indexing operation. By avoiding the linear projection for all values in a sequence, the model significantly reduces the FLOPs required for decoding.

Training for Sparsity

A critical finding of the research is that standard pre-trained Transformers cannot be converted to hard attention "on the fly." Switching to hard retrieval at test time without specific training leads to a catastrophic drop in translation quality.

Performance comparison of different training and decoding regimes.

To overcome this, the authors introduced Hard Retrieval Training. They used multinomial sampling to "sharpen" attention distributions during training and employed a gradient trick to ensure the model remains differentiable. By passing gradients directly through the hard selection to the underlying scores, the model learns to prioritize the most informative "anchor" tokens.

Results and Performance

The researchers evaluated HRA on standard benchmarks, including WMT14 (English-to-German and English-to-French) and a diverse set of 12 language pairs from WMT17.

Decoding speed and translation quality across different attention configurations.

As shown in the experimental results, applying hard retrieval to the decoder's self-attention and cross-attention mechanisms yields a 1.43x speedup while maintaining or even slightly improving BLEU scores (e.g., 27.73 vs. 27.55 on En-De).

However, the study also revealed a limitation: the encoder does not handle hard retrieval well. When hard attention was applied to the encoder, translation quality dropped significantly (to 26.60 BLEU). This suggests that while the decoder can operate by "pointing" to specific tokens, the encoder requires a global, soft view of the input to build a rich contextual representation.

Conclusion

Hard Retrieval Attention provides a practical method for accelerating Transformer inference. By shifting from weighted averages to discrete lookups in the decoder, models can achieve significant speed gains. While the encoder still requires traditional soft attention, the efficiency improvements in the decoder represent a major step toward more responsive and scalable NLP systems.

Summary updated

BibTeX
@inproceedings{xu2021learning,
  author = {Hongfei Xu and Qiuhui Liu and Josef van Genabith and Deyi Xiong},
  title = {{Learning Hard Retrieval Decoder Attention for Transformers}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.findings-emnlp.67},
}
Figure