Concepts / Stacking Multiple Attention Layers

Stacking Multiple Attention Layers

Multi-head attention extends causal attention across several heads, each operating independently with its own weights.

  • Programming

Where the Attention Story Ends

Up to this point, attention has been built as a single causal attention class. That class processes an input sequence using one set of weights. The final step in this line of work is to extend the class so that it covers several heads at once. This extension is known as multi-head attention.

The idea behind the extension is less exotic than its name suggests. Instead of asking one attention mechanism to do all of the work, we run several attention mechanisms side by side and gather what they produce. This article explains what that extension means, how its pieces fit together, and why the approach is worth its added cost. It starts from the single-head case, then looks at how a multi-head module is organized.

One Head as the Baseline

A single causal attention module can be regarded as single-head attention. In this arrangement there is exactly one set of attention weights, and that one set processes the input sequence. Every token passes through the same weights, and the module produces one set of context vectors as its result.

It helps to read the single-head module from left to right. The input tokens enter, one weight configuration transforms them as a sequence, and one context vector matrix leaves. Keep this picture in mind. The multi-head version keeps the same overall flow. What changes is how many weight sets sit in the middle.

What a Head Is

Definition

A head is one independent instance of the self-attention mechanism. Each head has its own weights, and therefore each head gives its own independent view of the same input.

In practical terms, building multi-head attention means creating several instances of the self-attention mechanism. Each instance has its own weights. After each instance has done its work, the outputs of the instances are combined into a single result. The instances do not share weights. That is what makes each head an independent view of the same input rather than a copy of one view.

The difference from the single-head module is therefore about ownership of weights. A single causal attention module owns one set of query, key, and value weights. A head, in the multi-head sense, owns its own query, key, and value weight matrices and produces its own context vectors. The attention module is the container, and the heads are the parts that each own a separate set of weights.

Multi-headattention moduleHead 1own Q, K, V weightsContext vectors Z1Head 2own Q, K, V weightsContext vectors Z2containscontainsproducesproduces
What does one attention module contain, and what does each head own separately? Each head has its own query, key, and value weight matrices.

Tracing Two Heads Through the Module

The two-head case is the smallest version that shows the full structure. Take a three-token sequence as a running example, with tokens labeled 1, 2, and 3. Each token has already been turned into an embedded vector. In a two-head module those embedded vectors enter both heads unchanged.

Where a single-head module had one value weight matrix, a two-head module has two, one per head. The same change applies to the query and key weight matrices. So head 1 runs its own query, key, and value weights over the sequence and produces context vectors $Z_1$. Head 2 runs a separate set of weights over the same embedded input and produces context vectors $Z_2$. The two heads do not see each other's weights, so each one is an independent attempt at the same input.

Token 1 vectorHead 1own Q, K, V weightsZ1 context vectorsZ combined matrixToken 2 vectorHead 2own Q, K, V weightsZ2 context vectorsToken 3 vectorentersentersentersentersentersentersproducesproducescombined intocombined into
What path does each token's vector take through the two heads, and how are the two head outputs joined back into one result?

Combining the Head Outputs

Each head produces its own set of context vectors. In the two-head diagram these are labeled $Z_1$ and $Z_2$. The module does not return two separate results. Those two sets are combined into a single context vector matrix, written $Z$. The combining step is what turns several independent head outputs back into one result for the rest of the model.

The same pattern extends beyond two heads. Each additional head adds another set of weight matrices and another set of context vectors to be combined. The number of heads changes how many parallel streams feed the merge, but the overall shape of the process stays the same: enter, split into heads, process per head, merge.

What do you think happens?

In a two-head module, how many query weight matrices are there?

  • One

  • Two

  • Three

  • It depends on the sequence length

Reveal answer
Answer:

Two

Each head has its own query, key, and value weight matrices, so a two-head module has one query matrix per head, giving two in total. The same change applies to the key and value matrices.

Two Stages of Building a Multi-Head Module

The expansion from causal attention to multi-head attention is approached in two stages. The first stage builds the idea intuitively by stacking several causal attention modules, each acting as a single head, and then combining their results. The second stage implements the same multi-head module in a more complicated but more computationally efficient way.

Stacking is the right starting point because it makes the structure visible. Each stacked module is a head you already understand, and the multi-head module is simply several of them arranged so that their outputs can be merged. The later efficient version computes the same kind of result with a different organization. Understanding the stacked version first makes that later step easier to follow.

Why the Extra Heads Are Worth Their Cost

Multi-head attention is computationally intensive. It is worth that cost because of the kind of pattern recognition it supports. Models based on transformers, which include many large language models, rely on this ability to handle complex patterns in data.

The reason is that giving the model several independent heads means different heads can, in principle, attend to the input in different ways. Because each head has its own weights, the heads are not forced to learn the same view. The combined output carries those differences forward into the rest of the model.

Single Head Versus Multi-Head Side by Side

The clearest way to see the difference is to compare the two configurations on the three dimensions that matter here: weights, outputs, and cost. The single-head module has one set of weights and one output. The two-head module has two sets of weights, two head-level outputs, and one merged output.

One set of Q, K, VweightsOne set of contextvectors ZOne attentioncomputationTwo sets of Q, K, VweightsZ1 and Z2 merged intoZSeparate attentionper head
How do the number of weight matrices, the number of output streams, and the attention computations differ between one head and two heads?
AspectSingle-head attentionTwo-head multi-head attention
Weight setsOne set of query, key, and value weightsTwo sets, one query, key, and value set per head
OutputsOne set of context vectorsContext vectors Z1 and Z2, combined into one matrix Z
IndependenceOne set of weights processes the sequenceEach head is an independent instance with its own weights
CostLower work and memoryHigher work and memory, growing with head count
Pattern recognitionOne attention view of the inputSeveral views that can attend to the input in different ways

Single-head and two-head attention compared

Mistakes Beginners Make with Heads

  • Common mistake

    Thinking that a head is a separate model or a separate input

    What it looks like

    Assuming head 2 receives a different sequence from head 1

    Why it fails

    Every head processes the same embedded input. The heads differ because each has its own weights, not because they see different tokens.

    Do this instead

    Treat a head as one independent attention mechanism with its own query, key, and value weights, all operating on the same input.

  • Common mistake

    Assuming the heads share one set of weights

    What it looks like

    Believing a two-head module has only one value weight matrix

    Why it fails

    The instances do not share weights. A two-head module has two value weight matrices, one per head, and the same change applies to the query and key matrices.

    Do this instead

    Count the weight matrices per head. Every head brings its own query, key, and value matrices.

  • Common mistake

    Expecting the output to stay in separate pieces

    What it looks like

    Expecting two separate final outputs from a two-head module

    Why it fails

    The head outputs Z1 and Z2 are combined into one context vector matrix Z.

    Do this instead

    Follow the merge step. The module returns one combined result.

  • Common mistake

    Ignoring the computational cost when adding heads

Causal Visibility Within Each Head

Each head in this module is a causal attention module, so the causal constraint applies inside every head. A causal mask decides which positions a token may draw on. The general rule is that each position is limited to itself and earlier positions. Using the same three-token running example, the table of visibility below shows which positions each token may use inside a head.

Token 1allowed: 1 | blocked: 2, 3Token 2allowed: 1, 2 | blocked: 3Token 3allowed: 1, 2, 3 | blocked:none
Which key positions is each query position allowed to see, and which are blocked by the causal mask?

Because the mask is applied within each head, both head 1 and head 2 in the two-head module obey the same visibility pattern. The heads differ in their weights, not in which earlier positions a token is allowed to draw on.

Checking Your Understanding of Head Counts

EASY

A multi-head module is built with three heads. How many value weight matrices does the module contain in total, and how many context-vector sets must be combined to form the final matrix?

Hints
  • Each head owns its own query, key, and value weight matrices.
  • Each head produces its own set of context vectors, and those sets are merged into one matrix.

Key Takeaways

  • Multi-head attention extends causal attention by running several attention mechanisms side by side, and each head is an independent instance with its own query, key, and value weights.
  • A single causal attention module is single-head attention with one set of weights, while a two-head module has one weight set per head and produces Z1 and Z2 that are combined into one matrix Z.
  • Adding heads adds weight matrices and context-vector sets to combine, so the module becomes more computationally intensive.
  • That added cost is justified because independent heads let different parts of the model attend to the input in different ways, which supports the complex pattern recognition associated with transformer-based large language models.

Related concepts

More concepts starting with S