What is multi-head attention?

Multi-head attention is a Transformer component that splits the attention computation into several independent heads so each can learn different kinds of relationships between tokens in parallel.

It is also called MHA. It differs from self-attention in structure: self-attention describes one attention computation, while multi-head attention runs it several times in parallel at smaller sizes and combines the results. One head may track grammatical relations while another follows a name far back in context.

Topic
Machine learning, models and training
Also known as
MHA
Origin
2017, Vaswani et al., Attention Is All You Need

How multi-head attention works

  1. Each token vector is turned into query, key and value vectors with separate linear projections for each head.
  2. The model dimension is split across heads; with dimension 4,096 and 32 heads each works with 4,096 / 32 = 128 dimensions.
  3. Each head computes scaled dot-product attention independently and produces its own output.
  4. All head outputs are concatenated and projected back to the full dimension.

In more detail

Scaled dot-product and the output projection

Each head takes the dot product of queries and keys, divides it by the square root of the key dimension, applies softmax and weights the values; the division keeps dot-product magnitudes from growing with head dimension and pushing softmax into near-zero gradient regions. After concatenation, a final output projection matrix mixes information across all heads.

KV cache size and the memory-bandwidth bottleneck

In autoregressive decoding, standard MHA's KV cache grows linearly with sequence length, layer count and the number of KV heads (per token, twice the product of layers, KV heads, head dimension and bytes per value). Because loading this cache from GPU HBM dominates decoding latency (memory-bandwidth bound), modern LLMs moved from MHA to MQA, GQA and multi-head latent attention (MLA), often combined with FlashAttention kernels during prefill.

Quadratic complexity and FlashAttention

In standard MHA, attention computation scales quadratically (O(N^2)) in sequence length N during training and prompt prefill. IO-aware kernels such as FlashAttention compute exact multi-head attention in SRAM tiles without materialising the full N x N attention matrix in GPU HBM, making long-context MHA feasible without changing mathematical outputs.

Commonly confused terms

TermHow it differs
Grouped-query attentionGrouped-Query Attention (GQA) shares each key-value head across a group of query heads to shrink the KV cache, whereas standard MHA maintains an independent key and value head for every query head.
Cross-attentionCross-attention takes queries from one sequence (e.g., decoder) and keys and values from another (e.g., encoder or image tokens), whereas self-attention derives all three from the same sequence; both are typically implemented in multi-head form.

Why multi-head attention matters for on-premises AI

Multi-head attention is a large part of in-house inference cost: each head's keys and values are stored in the KV cache, which takes most GPU memory in long contexts. That is why most new models moved to grouped-query attention, sharing keys and values across several query heads so the same memory supports longer context or more concurrent users. When choosing a model the attention type is an input to the memory plan.

ON-PREMISES EXAMPLE

A bank comparing two candidate models calculates memory for the in-house server that will process long credit files; the model with grouped-query attention has a smaller KV cache and accepts more concurrent requests on the same GPU.

Common questions

Why is multi-head attention better than one large attention?

Different heads can focus on different kinds of relationship, while one large attention must fold that variety into a single weight distribution. Total compute stays roughly the same.

Does every head do something meaningful?

Not always. Some heads show interpretable patterns, others are redundant and removing them changes performance very little.

Sources

In the glossary: Glossary, M to Q

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.