# Fast Transformer Decoding: One Write-Head is All You Need

Noam Shazeer · arXiv · 2019-11-6

- **Problem** Incremental Transformer decoding is slowed by the memory traffic of reloading large key and value tensors at every step.
- **Method** Shares one set of keys and one set of values across all attention heads while keeping a separate query per head, and applies this to every attention layer of the translation model.
- **Results** Decoder time per output token fell from 46 µs to 3.8 µs for greedy decoding on TPUv2, on WMT14 English-German with sequence length 128.

## Why incremental decoding is bound by memory traffic

Transformer replaces recurrence with attention layers that move information within and between sequences, which lets training process all positions of a sequence together. Generation cannot be parallelized in the same way, because the output at one position becomes part of the input that the next position depends on. Each decoding step therefore has to reload the cached keys and values, so on modern accelerators the speed of incremental inference is set by memory bandwidth rather than by arithmetic.

For batched attention over whole sequences, the arithmetic cost is \\(\\Theta(bnd^2)\\) and the memory traffic is \\(O(bnd + bhn^2 + d^2)\\), under the assumptions m = n, k = v = d/h and n ≤ d. The ratio of memory access to arithmetic is \\(O(1/k + 1/(bn))\\), which is low enough for accelerators whose compute capacity can be two orders of magnitude above their memory bandwidth.

The \\(1/b\\) term can be reduced with a larger batch, as far as memory allows. The \\(n/d\\) term is harder to remove, because it comes from reloading the cache at each step. Earlier work reduces the number of attended positions, either through local neighborhoods or by compressing memory positions, as in Liu et al. (2018), Zhang et al. (2018) and Povey et al.

(2018). This paper takes a different route: it shrinks the keys and values by dropping their heads dimension while keeping the heads dimension in the queries.

## Sharing one key and value set across heads

In multi-head attention each of the h heads has its own query, key and value projections, \\(P\_q\\), \\(P\_k\\) and \\(P\_v\\), and the outputs are passed through a projection \\(P\_o\\) and summed. The keys and values are therefore stored per head, as tensors of shape \[b, h, m, k\] and \[b, h, m, v\] in the incremental setting. Each decoding step appends one new key and one new value per head to these caches.

Multi-query attention keeps h query heads, but \\(P\_k\\) and \\(P\_v\\) project to a single set of keys and values with shapes \[d, k\] and \[d, v\], so every head reads the same memory. In the code this comes down to dropping the heads index from the einsum equations that involve K, V, \\(P\_k\\) and \\(P\_v\\), while queries, logits and the output projection keep their heads dimension.

During incremental decoding the cache therefore holds tensors of shape \[b, m, k\] and \[b, m, v\] with no head axis, and each head attends over this one shared cache. The per-head outputs are still projected by \\(P\_o\\) and summed, so the form of the layer output does not change.

With this change the incremental ratio becomes \\(\\Theta(1/d + n/(dh) + 1/b)\\), which the analysis describes as reducing the \\(n/d\\) term by a factor of h. The memory access per call is \\(\\Theta(bnd + bn^2k + nd^2)\\), where the cache term carries no factor for the number of heads. The authors expect a large batch size b to make this gain substantial, and the experiments are meant to show that the speed gains are real and that quality stays high.

## Models and training setup

The translation baseline is an encoder-decoder Transformer with 6 layers, \\(d\_{model}\\) 1024, \\(d\_{ff}\\) 4096, h = 8 and \\(d\_k = d\_v = 128\\), trained on WMT 2014 English-German. Each training batch held 128 examples, each a 256-token input and a 256-token target, on a 32-core TPUv3 cluster, with about 2 hours per model.

The multi-query model replaces every attention layer, covering encoder self-attention, decoder self-attention and encoder-decoder attention. Its feed-forward hidden layers are widened from 4096 to 5440, which brings its total parameter count to that of the baseline.

The local variants limit decoder self-attention to the current position and the previous 31 positions, a window of 32, while the other attention layers stay unchanged. Their purpose is to show that local attention and multi-query attention are orthogonal.

The alternatives reduce the number of heads h, or the key and value dimensions \\(d\_k\\) and \\(d\_v\\), and their feed-forward layers are widened so that the parameter count matches the baseline.

## Translation quality on WMT 2014 English-German

Dev scores come from greedy decoding and report ln(PPL) per subword token and BLEU, while test BLEU was computed with greedy decoding and with beam-4 search. The table of WMT14 EN-DE results collects these scores for each attention variant.

On dev, the multi-query model has ln(PPL) 1.439 against 1.424 for the multi-head baseline, and greedy BLEU 26.5 against 26.7. The multi-query model is therefore slightly behind on both dev metrics, with the gap smaller than for any alternative that reduces h, \\(d\_k\\) or \\(d\_v\\).

On the test set the multi-query model scores 27.5 with beam 1 and 28.5 with beam 4, against 27.7 and 28.4 for the baseline. With beam-4 decoding the multi-query model has the highest BLEU of the variants in this comparison.

In the local pair, the multi-query local model has dev ln(PPL) 1.437 against 1.427 for the multi-head local model. Test BLEU with beam 4 is 28.2 for multi-query local against 28.3 for multi-head local, so the local multi-query model again trails the local baseline by a small margin.

Reducing heads gives ln(PPL) of 1.518 with one head, 1.480 with two heads of \\(d\_k = 64\\), 1.488 with four heads and 1.513 with eight heads of \\(d\_k = 16\\). Dev BLEU for these alternatives is 25.8, 26.2, 26.1 and 25.8, all below the multi-query model's 26.5 on the same metric.

## Billion-Word language modeling results

The language-model experiments use transformer-decoder models on the Billion-Word LM Benchmark, with 6 layers, \\(d\_{ff}\\) 8192 and 192 million parameters per model. They ran for 136K steps, which the paper gives as 10 epochs, at a batch size of 64K tokens.

Scores are per-word perplexity on the dev set, not per-subword-token perplexity. The multi-query model is slightly worse than the baseline, and better than the alternatives that reduce h, \\(d\_k\\) and \\(d\_v\\).

## Training and decoding cost on TPUv2

Speed was measured on one TPUv2 with 8 cores, and Table 2 reports the timings separately from the quality scores in Table 1. A training step of 32,768 input tokens and 32,768 target tokens took 433 ms for the baseline and 425 ms for the multi-query model. Per input-plus-target token, training costs 13.2 µs for the baseline and 13.0 µs for the multi-query model.

For incremental greedy inference on a batch of 1024 sequences, 128 per core, with source and target lengths of 128, the baseline encoder took 222 ms and each decoder step 47 ms. The multi-query encoder took 195 ms and each decoder step took 3.9 ms. Amortized per output token, the encoder costs 1.7 µs for the baseline and 1.5 µs for the multi-query model, while the decoder costs 46 µs and 3.8 µs respectively.

With beam-4 search, the amortized cost is 2.0 + 203 µs per token for the multi-head model and 1.6 + 32 µs for the multi-query model, where the second term is the decoder. With local attention, the decoder cost is 23 µs per token for the multi-head local model and 3.3 µs for the multi-query local model in greedy decoding.

## Limits of the timing and the evidence

Fixed input shapes forced padding and masking in the decoder self-attention, so memory tensors were padded to the maximum length of 128, or to the window of 32 for local attention. Each decoding step therefore took the same time regardless of position, and the authors note that growing the tensors incrementally could save time near the start of a sequence.

The quality cost is described by the authors as minor, and the measured gaps are small but present: dev ln(PPL) 1.439 against 1.424 on WMT14, and slightly worse per-word perplexity on the Billion-Word benchmark.

The analysis is theoretical and assumes m = n, k = v = d/h and n ≤ d. Experiments cover two benchmarks, WMT 2014 English-German and the Billion-Word LM Benchmark, and speed was measured only on TPUv2.

Learning rates, dropout and label smoothing are not given in the text, which marks the configuration details as to be added before publication.

## Paper info

- **Authors**: Noam Shazeer
- **Venue**: arXiv · Preprint
- **First published**: 2019-11-6
- **Added**: 2026-10-20
- **Updated**: 2019-11-6
- **Version**: v1
- **arXiv**: 1911.02150
- **Category**: cs.NE · cs.CL · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1911.02150v1) · [PDF ↗](https://arxiv.org/pdf/1911.02150v1)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Noam Shazeer (2019). Fast Transformer Decoding: One Write-Head is All You Need. arXiv:1911.02150v1. https://arxiv.org/abs/1911.02150v1

**BibTeX**

```
@misc{shazeer2019fast,
  title = {{Fast Transformer Decoding: One Write-Head is All You Need}},
  author = {Noam Shazeer},
  year = {2019},
  eprint = {1911.02150},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1911.02150v1}
}
```

## Other paper commentary

- [Dynamic Routing Between Capsules](https://refivu.com/en/papers/a4e87f62-36cc-47df-991e-87dd01b1a4ef-dynamic-routing-between-capsules.md)
- [Distilling the Knowledge in a Neural Network](https://refivu.com/en/papers/d39fc594-ec58-4492-b1f4-d2d3600a6455-distilling-the-knowledge-in-a-neural-network.md)
- [Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation](https://refivu.com/en/papers/16116a1b-83a4-4d2d-b5ff-31f5b005ba3f-train-short-test-long-attention-with-linear-biases.md)
- [Mamba: Linear-Time Sequence Modeling with Selective State Spaces](https://refivu.com/en/papers/b149783d-1b22-47f2-94df-544d57bb2621-mamba-linear-time-sequence-modeling-with-selective-state.md)
- [RWKV: Reinventing RNNs for the Transformer Era](https://refivu.com/en/papers/f86c302a-2e46-481a-b0a7-850579f6f3c8-rwkv-reinventing-rnns-for-the-transformer-era.md)
- [Highway Networks](https://refivu.com/en/papers/78251486-9138-48d8-a03c-8447a3433591-highway-networks.md)
- [Finetuned Language Models Are Zero-Shot Learners](https://refivu.com/en/papers/3dedf278-64d7-4653-93ff-bf4504ae70ac-finetuned-language-models-are-zero-shot-learners.md)
- [Neural Machine Translation of Rare Words with Subword Units](https://refivu.com/en/papers/0950e659-f6cc-4852-b244-bbb2742bf059-neural-machine-translation-of-rare-words-with-subword.md)
- [Fast Inference from Transformers via Speculative Decoding](https://refivu.com/en/papers/41a03564-b5d1-4b7f-8db4-502dbc43fbe0-fast-inference-from-transformers-via-speculative-decoding.md)
- [Constitutional AI: Harmlessness from AI Feedback](https://refivu.com/en/papers/91e9437c-36ce-49cd-965c-18d9ca4664d6-constitutional-ai-harmlessness-from-ai-feedback.md)
- [Scaling Instruction-Finetuned Language Models](https://refivu.com/en/papers/6f9b141d-190c-4397-9e41-4f796f4ef56e-scaling-instruction-finetuned-language-models.md)
- [GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers](https://refivu.com/en/papers/46a31638-80a6-4626-a5e7-028ae5ccea93-gptq-accurate-post-training-quantization-for-generative-pre.md)

## Terms in this paper

- [Transformer](https://refivu.com/en/terms/48e8be2570bc-transformer)
- [perplexity](https://refivu.com/en/terms/2b204e513801-perplexity)
- [multi-head attention](https://refivu.com/en/terms/370ae5c971cd-multi-head-attention)
- [Multi-Query Attention](https://refivu.com/en/terms/6127e5d0c706-multi-query-attention)
- [local attention](https://refivu.com/en/terms/a310e53f9ebe-local-attention)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/65f892e7-2a21-4ce7-9e89-c75f7ec6b23c-fast-transformer-decoding-one-write-head-is-all)
