Faster Transformer Decoding: N-gram Masked Self-Attention

Ciprian Chelba, Mia Chen, Ankur Bapna, Noam ShazeerarXiv 2020

TL;DR

Standard Transformer decoding suffers from quadratic complexity because each new token attends to every previously generated token. This paper demonstrates that by restricting the decoder's "look-back" window to just the last few tokens (an N-gram assumption), we can achieve near-baseline translation quality with significantly lower computational and memory overhead.

What Problem Does This Paper Address?

The Transformer architecture has revolutionized NLP, but its decoding process remains a bottleneck. In machine translation (MT), the decoder generates tokens one by one. In a standard Transformer, each new token performs "self-attention" over all previously generated tokens in the sequence.

As the generated sentence grows to length $T$, the computational complexity of this step scales at $O(T^2)$. For long sequences, this becomes expensive. The authors set out to see if we actually need that full history. They hypothesize that in translation, the source sentence provides the bulk of the information, making long-range dependencies in the target-side history less critical than previously assumed.

Methods

The authors propose N-gram Masked Self-Attention.

In a standard Transformer decoder, the model estimates the probability of the next token $t_k$ based on all previous tokens $t_1, \dots, t_{k-1}$ and the source sentence $S$. The proposed method truncates this target-side window. Instead of attending to the entire history, the self-attention mechanism is restricted to the previous $N-1$ tokens.

This changes the complexity from $O(T^2)$ to $O(N \cdot T)$. Mathematically, this is equivalent to making an N-gram assumption on the target sequence conditioning, while still maintaining full attention over the source sentence.

Beyond raw FLOPs, this approach offers a major architectural advantage: fixed-size memory buffers. Instead of a growing cache of keys and values, the decoder can use a circular buffer of size $N-1$. This significantly reduces memory bandwidth requirements, which is often the primary bottleneck for inference on hardware like TPUs.

Datasets

The researchers evaluated their approach on two standard machine translation benchmarks:

  • WMT’18 EnDe (English-German): Using newstest2012 for development and newstest2017 for testing.
  • WMT’14 EnFr (English-French): Using a concatenation of newstest2012 and 2013 for development and newstest2014 for testing.

Both models utilized bilingual word-piece vocabularies of 32k tokens.

Key Insights and Ideas

  • The Source Sentence is the Heavy Lifter: The authors observed a massive gap in perplexity (PPL) between a standalone Language Model (PPL ~99.5) and a conditional Neural Machine Translation model (PPL ~13.5) on the same target data. This empirical evidence confirms that the source sentence provides the vast majority of the predictive signal, allowing the target history to be simplified.
  • Locality Matters: Language is inherently local. The experiments show that a window of just 4 to 8 tokens captures enough syntactic and immediate semantic context to produce high-quality translations.
  • Graceful Degradation: Performance doesn't fall off a cliff when the window is shortened. As seen in the results, even very small $N$ values maintain respectable BLEU scores, suggesting that the "infinite" context of standard Transformers is often overkill for the decoder side of MT.

Key Results

The results show that N-gram masking loses very little quality compared to the full-attention baseline.

  • WMT’18 EnDe: The baseline achieved a BLEU score of 28.6. An 8-gram model reached 28.2—a negligible drop of 0.4 BLEU points.
  • WMT’14 EnFr: The results were even more impressive here. The 6-gram model achieved a BLEU of 41.1, matching the baseline exactly. Even a 4-gram model stayed within 0.7 BLEU points of the baseline.
Table 1: WMT’18 EnDe experiments showing BLEU and log-perplexity across different N-gram orders.
Table 2: WMT’14 EnFr experiments showing similar stability in BLEU scores even with small N-gram windows.

The authors estimate that for typical sentence lengths ($T \approx 16–25$), this method could yield a 2x to 3x speed-up for the self-attention component of the decoder.

Limitations and Future Work

While the theoretical speedups and memory savings are clear, the authors note that real-world performance depends heavily on implementation. In many current systems, the feed-forward layers (which remain unchanged) may dominate the computation time, potentially masking the gains from faster attention.

Furthermore, this study focused on training and validation metrics. The authors suggest that a more thorough investigation into actual wall-clock decoding speed and memory footprint at inference time—especially during beam search—is the necessary next step to fully validate the utility of N-gram Masked Self-Attention. Finally, while this works for MT, it remains to be seen if it holds for tasks like long-form document generation where long-range target dependencies might be more vital.

Summary updated

BibTeX
@misc{chelba2020faster,
  author = {Ciprian Chelba and Mia Chen and Ankur Bapna and Noam Shazeer},
  title = {{Faster Transformer Decoding: N-gram Masked Self-Attention}},
  year = {2020},
  eprint = {2001.04589},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2001.04589},
}
Figure