Multi-query attention (MQA) is a variant of multi-head attention in which queries are produced separately for each head, while keys and values are produced only once and shared by all heads. It was proposed by Shazeer in 2019 in the paper "Fast Transformer Decoding: One Write-Head is All You Need". The goal is to speed up Transformer generation, where text is produced one token at a time.
The only difference from multi-head attention is the number of linear layers for keys and values. Head is computed as follows.
- : the query linear-layer weights, separate for each head (same as multi-head attention).
- : the key and value linear-layer weights, shared by all heads. In multi-head attention these were , separate for each head.
- : scaled dot-product attention.
The paper explains that MQA is obtained from multi-head attention code simply by removing the "heads" dimension from the keys, values, and their linear layers. The output linear layer and the queries are unchanged.
Picture eight researchers (heads): instead of each fetching a different reference book from the shelf, they all share one book and only their questions (queries) differ. The effort of carrying books from the shelf drops to one-eighth.
In the figure at the top of the article, the left side shows the computation flow and the right side compares the number of key/value heads.

