Inferensys

Glossary

Multi-Head Attention

A module that runs multiple scaled dot-product attention operations in parallel, allowing the model to jointly attend to information from different representation subspaces.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
ARCHITECTURE

What is Multi-Head Attention?

Multi-head attention is a core Transformer module that runs multiple scaled dot-product attention operations in parallel, enabling the model to jointly attend to information from different representation subspaces at different positions.

Multi-head attention is a mechanism that linearly projects the Query, Key, and Value (QKV) vectors into h distinct lower-dimensional subspaces, computing scaled dot-product attention in each head independently. This parallelization allows the model to focus on different types of relationships simultaneously—one head might track syntactic dependencies while another resolves coreference links—rather than averaging these signals into a single, noisy representation. The outputs from all heads are concatenated and linearly projected back to the model's hidden dimension.

The primary advantage over single-head attention is the ability to learn diverse relational patterns from the same input sequence. By reducing the dimension per head, the total computational cost remains roughly equivalent to a single full-dimensional attention operation, yet the model gains significantly richer representational capacity. This design is the foundational building block of the Transformer block and directly addresses the limitation of a single attention function potentially being dominated by the most prominent similarity signal.

PARALLELIZED REPRESENTATION SUBSPACES

Key Characteristics

Multi-Head Attention is the core innovation that allows Transformer models to simultaneously focus on different aspects of the input sequence. Instead of a single attention function, it projects the Query, Key, and Value vectors into multiple lower-dimensional subspaces, enabling the model to learn diverse syntactic and semantic relationships.

01

Parallel Attention Heads

The module runs h independent scaled dot-product attention operations in parallel. Each head operates on its own learned linear projections of the Query, Key, and Value vectors (typically with dimensions d_k = d_v = d_model / h). This allows different heads to specialize in distinct relational patterns—one head might track syntactic dependencies while another resolves coreference links.

02

Concatenation and Linear Projection

After computing attention in parallel, the outputs from all h heads are concatenated along the feature dimension and passed through a final linear transformation (W^O). This step synthesizes the information from the disparate representation subspaces back into a single d_model dimensional output, combining the diverse contextual signals captured by each head.

03

Computational Complexity

Despite running multiple attention operations, the total computational cost is comparable to single-head attention with full dimensionality. By reducing the dimension of each head (d_k = d_model / h), the matrix multiplications are cheaper. The primary bottleneck remains the quadratic complexity of computing the N x N attention score matrix, which scales with the square of the sequence length.

04

Joint Attention Diversity

The model jointly attends to information from different representation subspaces at different positions. This prevents the averaging effect that can occur with a single attention function. For example, in the sentence 'The animal didn't cross the street because it was too wide,' one head can focus on the syntactic subject while another attends to the adjectival modifier, resolving the ambiguity of 'it'.

05

Scaled Dot-Product Foundation

Each head computes attention using the formula: Attention(Q, K, V) = softmax(QK^T / √d_k)V. The scaling factor 1/√d_k prevents the dot products from growing too large in magnitude, which would push the softmax function into regions with extremely small gradients. This temperature control is critical for stable training.

06

Efficient Variants

Standard multi-head attention stores separate Key and Value projections for each head, which inflates the KV cache during inference. Optimized variants address this:

  • Multi-Query Attention (MQA): All heads share a single Key and Value projection, drastically reducing memory bandwidth.
  • Grouped-Query Attention (GQA): Heads are partitioned into groups sharing Key/Value projections, balancing quality and speed. Used in Meta Llama 2.
MULTI-HEAD ATTENTION EXPLAINED

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the parallel attention mechanism that allows Transformer models to jointly attend to information from different representation subspaces.

Multi-head attention is a module that runs multiple scaled dot-product attention operations in parallel, allowing the model to jointly attend to information from different representation subspaces at different positions. Instead of performing a single attention function with d_model-dimensional keys, values, and queries, the model linearly projects the queries, keys, and values h times with different, learned linear projections to d_k, d_k, and d_v dimensions. Each of these projections feeds an independent attention head that computes its own attention-weighted output. The outputs of all heads are then concatenated and projected once more, yielding the final multi-head output. This design enables the model to simultaneously focus on different types of relationships—such as syntactic dependencies, coreference links, and semantic associations—within the same input sequence. The original Transformer paper used h=8 parallel attention heads, with d_k = d_v = d_model/h = 64.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.