4.1.5 · D4Transformer Architecture

Exercises — Multi-head attention

2,081 words9 min readBack to topic

These problems build from "just read the definition" up to "design and reason about the whole block." Every problem has a full solution hidden in a collapsible callout — try first, then reveal. Symbols used here all come from the parent note; if you meet one you forgot, that's your cue to peek back.

Prerequisite ideas live in Self-Attention, Query-Key-Value Intuition, and Linear Projections in Neural Networks.


Level 1 — Recognition

Problem 1.1

State the formula for the per-head dimension in terms of (the input width per token) and (the number of heads). Then compute for , .

Recall Solution

The parent note defines The word "dimension" here just means how many numbers describe one token inside a head. We split the full width evenly across the heads.

Problem 1.2

A multi-head attention block has input . Here is the number of tokens (rows) and the width (columns). What is the shape of the final output of the whole block, and why is it the same as the input width?

Recall Solution

Output shape is — same width as the input. Why: concatenating heads each of width gives width . The final matrix maps that back to . Same width in, same width out — so blocks can be stacked without reshaping.


Level 2 — Application

Problem 2.1

With , , : give the shapes of , , the score matrix , and .

Recall Solution

.

  • — it takes a 512-wide token and squeezes it to 64.
  • .
  • — one score for every (token, token) pair. Look at Figure s01: each cell is "how much does row-token look at column-token."
  • .
Figure — Multi-head attention

Problem 2.2

For a single head with , the raw dot product . Compute the scaled score. Why do we divide by and not, say, ?

Recall Solution

Why : if and have entries with variance , the dot product is a sum of such products, so its variance grows like and its typical size like . Dividing by cancels exactly that growth, keeping scores at a stable size no matter how big is. Dividing by would over-shrink them (killing the signal); dividing by is the Goldilocks amount.

Problem 2.3

Three tokens give scaled scores , , for query token 3. Compute the softmax attention weights. Which token wins, and by roughly how much?

Recall Solution

Softmax turns scores into probabilities: Numerators: , , . Sum . Token 3 (attending to itself) wins with . Because softmax uses the exponential, a score gap of (from to ) already makes token 3 roughly times more likely than token 1.


Level 3 — Analysis

Problem 3.1

A student claims: "Since attention cost is , using heads makes the block slower than single-head attention." Is this correct? Show the FLOP comparison.

Recall Solution

Incorrect. The key is that each head runs in the reduced width , not the full .

  • Single head, full width: cost .
  • heads, each width : cost . The and the cancel. So multi-head is the same order of cost as one full-width head, yet buys different views. That is precisely the design trick.

Problem 3.2

Below is a broken forward pass. Find the error.

"For , I set each , computed 8 heads of width 64, then summed them to get a output, and applied ."

Recall Solution

The error is summing instead of concatenating.

  • Summing collapses the 8 distinct 64-wide views into a single 64-wide vector — the specialized patterns each head learned get averaged away, destroying the whole point of multiple heads.
  • The correct step is concatenation: , then . (The stated is a downstream symptom of the same mistake.)

Problem 3.3

Explain why every head must use a different learned . What exactly happens if two heads share identical projection matrices?

Recall Solution

Each projection is like a different question asked of the same tokens (see Query-Key-Value Intuition). If head and head share , then , , , so they produce byte-for-byte identical outputs. In the concatenation those two 64-wide blocks are duplicates — you paid for 2 heads but got 1 distinct view. Different matrices let head track, say, syntax while head tracks co-reference. Diversity of projections is the source of multi-head power.


Level 4 — Synthesis

Problem 4.1

Derive the total parameter count of a multi-head attention block (all plus ; ignore biases). Show it does not depend on . Then evaluate for .

Recall Solution

Per head: three matrices of shape . Across heads: The cancels — splitting into more heads doesn't add parameters. Plus adds . Total: For : parameters ( M).

Problem 4.2

Set up (symbolically) the full forward pass for a causal single query at the last position of an sequence, one head, . Given projected vectors and value scalars packed as , compute the output for the query at position 4 (it may attend to all 4 keys).

Recall Solution

Raw scores : , , , . Scale by : . Softmax: ; sum . Weighted sum of values: First component: . Second component: .


Level 5 — Mastery

Problem 5.1

Steel-man the claim "more heads is always better," then refute it with a limiting-case argument. What happens as ?

Recall Solution

Steel-man: more heads more independent subspaces more relationship types captured, at no extra cost (Problem 3.1) and no extra parameters (Problem 4.1). Sounds free. Refutation via limit: . As grows, shrinks. At the extreme , each head has : its query and key are single numbers, so the score can encode almost nothing — the head is nearly blind. Expressiveness per head collapses. There is a sweet spot (empirically ): enough width for a rich subspace, enough heads for diversity. So "more heads" trades diversity against per-head capacity — not a free lunch.

Problem 5.2

Consider the degenerate input where all tokens have identical value vectors . Prove that every head's output at every position equals , regardless of the attention weights. What does this tell you about where attention's power actually comes from?

Recall Solution

For any position , the output is . With all : because softmax weights in each row sum to exactly . The attention pattern becomes irrelevant. Lesson: attention only ever produces a convex combination (weighted average) of the value vectors. If the values carry no variety, the fanciest attention map buys nothing. Power comes from (a) the diversity of the projected values and (b) which values get emphasized — not from the weights alone.

Problem 5.3

In Cross-Attention, queries come from a decoder sequence of length and keys/values from an encoder sequence of length . What shape is the score matrix , and what is the output length? Why does multi-head still work unchanged?

Recall Solution

, , so Softmax runs over each of the rows (across the keys), then gives . Output length (one enriched vector per query token). Multi-head machinery is identical — concatenate over , apply — because concatenation and act on the width axis, which never depended on whether . See Figure s02.

Figure — Multi-head attention

Recall Quick self-test

What cancels the in the FLOP count? ::: Each head runs in width , so . Why concatenate heads instead of summing? ::: Summing averages away each head's distinct learned pattern; concatenation preserves all views. Total parameters of MHA (no biases)? ::: , independent of . Output length of cross-attention? ::: (the number of query tokens).