Why Multi-Head Attention

Several attention steps side by side, each tracking a different relationship.

🟡 intermediate4 min readtransformers
A single attention step can only focus on one kind of relationship at a time. Multi-head attention runs several smaller attention steps side by side, each with its own learned view of the words. One head might track which noun a pronoun refers to, another might track word order, another might link a verb to its object. Their results are then joined back together. It costs about the same as one large attention step, but captures several different relationships at once.

What is Multi Head Attention?

Single head attention calculates a single weighted average across tokens.

However, a word in a sentence often has multiple different relationships simultaneously:

In the sentence "The cat ate the fish because it was hungry":

  1. Relationship A (Grammar): "ate" connects to subject "cat".
  2. Relationship B (Pronoun Coreference): "it" connects to "cat".
  3. Relationship C (Action Target): "ate" connects to object "fish".

A single attention head averages these competing signals into a single blurred score.

Multi Head Attention projects Queries, Keys, and Values into hh independent subspace heads (for example, h=8h = 8 or h=16h = 16), allowing the network to attend to multiple relationship types in parallel.

  Input Token Embeddings [N x 512]
            │
            ├─► [ Head 1 Projection ] ──► Scaled Dot Product Attention ──► Output 1 [N x 64] (Grammar Head)
            ├─► [ Head 2 Projection ] ──► Scaled Dot Product Attention ──► Output 2 [N x 64] (Pronoun Head)
            │   ...
            └─► [ Head 8 Projection ] ──► Scaled Dot Product Attention ──► Output 8 [N x 64] (Topic Head)
                                                        │
                                                        ▼
                               Concatenate [ Head 1; ...; Head 8 ] ──► [ Output Linear W_O ] ──► Final [N x 512]

Mathematical Formulation

Given model dimension dmodeld_{\text{model}} and hh heads:

dk=dv=dmodelhd_k = d_v = \frac{d_{\text{model}}}{h}

For each head i∈{1,…,h}i \in \{1, \dots, h\}:

headi=Attention(QWiQ,KWiK,VWiV)\text{head}_i = \text{Attention}\left( Q W_i^Q, K W_i^K, V W_i^V \right)

Concatenate all heads and project:

MultiHead(Q,K,V)=Concat(head1,…,headh)WO\text{MultiHead}(Q, K, V) = \text{Concat}\left( \text{head}_1, \dots, \text{head}_h \right) W^O

Where WiQ∈Rdmodel×dkW_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}, WiK∈Rdmodel×dkW_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}, WiV∈Rdmodel×dvW_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v}, and WO∈Rhdv×dmodelW^O \in \mathbb{R}^{h d_v \times d_{\text{model}}}.

Does Multi Head Attention Cost More FLOPs?

Counter intuitively, No!

Because each head operates on a reduced sub dimension dk=dmodel/hd_k = d_{\text{model}} / h:

h×(Cost for dimension dmodelh)=Cost for full dimension dmodelh \times \left( \text{Cost for dimension } \frac{d_{\text{model}}}{h} \right) = \text{Cost for full dimension } d_{\text{model}}

Processing 8 parallel heads of dimension 64 takes the exact same computational FLOPs as processing 1 single head of dimension 512.

You get rich multi aspect feature representation learning at zero extra computational cost!

Say this out loud

Multi Head Attention splits Queries, Keys, and Values into h parallel heads operating on reduced sub dimensions d_k = d_model / h. This allows the model to attend to multiple distinct relationship types simultaneously, like grammar, pronouns, and topic associations. Outputs from all heads are concatenated and linearly projected, costing the exact same compute as single head attention.

Followups to expect

  1. What is Grouped Query Attention (GQA)? A memory efficient variant used in LLaMA 3 where multiple Query heads share a single Key and Value head group, cutting KV cache memory requirements during inference.
  2. What do individual attention heads learn in practice? Probing studies show specific heads specialize in distinct jobs: positional lookup (prev/next token), syntactic parsing, coreference resolution, and Induction Heads for in context pattern repetition.

Check yourself

Question 1 of 3

Why does Multi Head Attention outperform single head attention when processing complex text sequences?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min