Multi-Head Attention
The baseline attention design that gives every query head its own key-value head pair.
Multi-head attention (MHA) is the baseline transformer attention design, splitting attention into several parallel heads so different heads can focus on different relationships while each head keeps its own key and value pair.
At a glance
Released
June 2017
Authors
Ashish Vaswani, Noam Shazeer, Niki Parmar, et al.
Optimizes
- Expressiveness
What It Is
Multi-head attention (MHA) is the original scaled dot-product attention design used in the Transformer. Instead of running one large attention lookup, the model splits the work across several heads. Each head computes its own attention distribution over keys and values, and the head outputs are then merged.Why It Exists
MHA optimizes for representational breadth. Distinct query heads can attend to different relationships in the same sequence without sharing key-value parameters.How It Works
Linear projections produce multiple query heads, multiple key heads, and multiple value heads. Each query head attends to its matching key and value head, producing several outputs that are concatenated and projected again.QueriesqqqqValuesKeysVVVVkkkkV to kV to kV to kV to kk to qk to qk to qk to q
Math Or Compute Schema
The formula below is the per-head attention computation used inside multi-head attention. Each head index i pairs its own query, key, and value tensors before outputs are merged.Compared To Nearby Modules
Compared with multi-query attention, MHA stores one key-value pair per query head, so cache size grows with head count. Compared with grouped-query attention, MHA avoids shared key-value groups and keeps the largest cache footprint in the head-sharing family.| Comparison dimension | Multi-Head Attention | Multi-Query Attention | Grouped-Query Attention |
|---|---|---|---|
| Key-value head count | H key heads and H value heads | 1 key head and 1 value head | G key heads and G value heads |
| Query-head flexibility | H independent query and key-value head pairs | H query heads share one key-value pair | H distinct query heads grouped into G shared key-value pairs |
| Cache footprint per token | 2H tensors (full multi-head attention cache) | 2 tensors (single shared key-value cache) | 2G tensors (G keys plus G values) |