Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 12

Multi-Head Attention: Why Parallel Instead of Single?

Recommended resources

Introduction

In our last lesson, we completed our understanding of a single, functional attention unit by incorporating causal masking. This ensures that our model respects the arrow of time, a critical feature for autoregressive generation. The resulting mechanism, masked scaled dot-product attention, allows a token to query its predecessors and construct a contextually informed representation.

A natural and important question arises from this: is one such attention mechanism enough? While powerful, a single attention calculation forces the model to average all the diverse and subtle relationships between words into a single weighted sum. This can be a significant bottleneck. This lesson delves into the motivation behind multi-head attention, a core innovation of the Transformer architecture that addresses this limitation. We will explore why performing multiple attention computations in parallel is profoundly more effective than relying on a single one.


The Limitation of a Single Perspective

The scaled dot-product attention mechanism you've learned so far can be visualized as the block on the left in the diagram below. For each token (query), it produces a single set of attention weights over all other tokens (keys) and uses them to compute a weighted average of their values.

This diagram contrasts a single Scaled Dot-Product Attention mechanism (left) with the Multi-Head Attention architecture (right). The multi-head version runs several attention computations in parallel and combines their results.

Consider the sentence: "The delivery truck, which was late, blocked the narrow street."
A single attention mechanism, when processing the word "street", needs to capture several relationships simultaneously:

  • Syntactic: "street" is the object of the verb "blocked".
  • Semantic: "street" is related to "delivery truck".
  • Adjectival: "street" is described by "narrow".
  • Positional: "street" appears at the end of the sentence.

Forcing a single set of attention weights to learn an optimal "average" of all these relationships is a demanding constraint. It's akin to trying to capture a complex, multi-faceted object with a single photograph from a fixed angle; you inevitably lose information. The model might learn that "blocked" and "narrow" are important, but it may struggle to learn different kinds of importance.


The Multi-Head Solution: Parallel Views

The key insight of the Transformer paper is that instead of having one large, monolithic attention mechanism, it's more effective to have multiple smaller attention "heads" that operate in parallel. This is the architecture shown on the right side of the diagram above.

The core idea is to allow the model to learn different relationships in different representation subspaces. Before calculating attention, the model doesn't just create one set of Query, Key, and Value vectors. Instead, it uses several independent sets of projection matrices to transform the initial token embeddings into multiple, distinct Q, K, and V sets. Each of these sets constitutes a "head."

This is conceptually similar to running multiple different queries against a database to retrieve different facets of information. Each head can "ask" a different type of question about the sequence, allowing the model to specialize.

To get a concise and clear overview of this motivation, the following article by Sebastian Raschka is an excellent starting point.

Why do transformer-based LLMs use multi-head attention instead of ...

This article directly addresses our central question, explaining why a single attention mechanism is too limited and how multiple heads provide a more expressive alternative.

Please read the article from the beginning to the end of the third benefit. Focus on the three reasons provided for why multi-head attention helps: specialization of dependencies, operating in multiple subspaces, and creating a richer combined representation.


Specialization: The Power of Diverse Perspectives

As the article highlights, the primary motivation for multi-head attention is specialization. By running multiple attention mechanisms in parallel, each with its own trainable weights, different heads can learn to focus on different types of patterns in the input.

One head might become an expert in tracking syntactic dependencies, another in resolving pronoun references (anaphora), and a third in identifying relationships between entities. The model isn't explicitly programmed to do this; rather, the architecture provides the capacity for this specialization to emerge during training.

The following video from 3Blue1Brown provides excellent examples of the different contextual updates that a model might need to perform.

Attention in transformers, step-by-step | Deep Learning Chapter 6

This segment from "Attention in transformers, step-by-step" illustrates various types of contextual relationships that motivate the need for multiple, distinct attention mechanisms.

Watch from this timestamp. Notice the different examples given: physical properties ("crashed the car"), disambiguation based on surrounding words ("Harry Potter" vs. "Prince Harry"), and other non-grammatical associations. A single head would struggle to capture all these possibilities.

Another great, concrete example is how a single word can play different roles. One attention head might learn to interpret a word as a noun, while another head learns to interpret it in its role as a verb, depending on the context.

Attention is all you need (Transformer) - Model explanation (including math), Inference and Training

In this clip from Umar Jamil's "Attention is all you need", the presenter gives a clear reason why multiple heads are useful for words that can have multiple grammatical roles.

Please watch the segment from this point. This provides a very tangible example of specialization.

The power of this approach is that the final representation is not a single, compromised average, but a rich combination of these specialized views. We can even visualize this specialization. The original "Attention Is All You Need" paper included visualizations showing that different heads indeed learn to focus on different word-to-word relationships.

The video below discusses and shows one such visualization, making the abstract idea of specialization concrete.

Attention is all you need (Transformer) - Model explanation (including math), Inference and Training

This next segment from the same video showcases a visualization of attention heads at work.

Watch from this clip. The presenter points out how for the word "making", some heads (blue, red, green) relate it to "difficult", while another head (violet/pink) relates it to "2009". This is clear evidence of different heads learning different types of relationships from the same input.


A Glimpse into the Mechanism

So, how does each head learn a different representation subspace? The key is that each of the heads has its own, separate, learnable weight matrices: , , and for . The input token embeddings are multiplied by these matrices to project them into the lower-dimensional space for that specific head.

Because these weight matrices are initialized randomly and updated independently via backpropagation, each head is free to develop its own unique way of interpreting the sequence.

The following clip provides a high-level overview of this parallel structure, which sets the stage perfectly for our next lesson.

Attention in transformers, step-by-step | Deep Learning Chapter 6

This final clip from 3Blue1Brown explains how the outputs of these parallel heads are combined.

Please watch from this point. The key idea is that each of the 96 (in GPT-3) heads proposes a change to the token's embedding. These changes are then combined to produce the final, refined embedding.

This brings us to the full picture. Multi-head attention isn't just running the same attention calculation multiple times. It is about learning multiple, distinct, and parallel ways to project and interpret the sequence data, and then combining those diverse interpretations into a single, powerful output.


Conclusion

In this lesson, we have explored the crucial motivation behind replacing a single attention mechanism with a multi-head architecture. A single head acts as a representational bottleneck, forcing the model to average diverse linguistic patterns into one view. Multi-head attention elegantly solves this.

Here are the key takeaways:

  • Motivation: A single attention head is limited in its ability to capture the many different kinds of relationships (syntactic, semantic, positional) present in a sequence.
  • The Solution: Multi-head attention performs multiple attention computations in parallel, with each "head" operating in a different, learned representation subspace.
  • Primary Benefit (Specialization): This parallel structure allows different heads to specialize in detecting different types of patterns, such as short-range syntax, long-range dependencies, or specific semantic relationships.
  • Result: By combining the outputs of these specialized heads, the model produces a much richer and more nuanced contextual representation for each token than a single head could achieve.

We have now established the "why" of multi-head attention. In our next lesson, we will dive into the precise mechanics, detailing how the outputs of the parallel heads are calculated, concatenated, and projected to form the final output of the multi-head attention sub-layer.