Transformers from the ground up
Multi-head attention
One attention pattern per layer is not enough. Several heads, each with its own projections, look for different relations and are concatenated back into one vector.
Why one head is a bottleneck
A single softmax produces one distribution per token. A token may need to track several things at once. It might track the previous word, the subject of the sentence and a pronoun’s referent. Those relations compete for the same weights. The result is an average that serves none of them well.
Multi-head attention gives the layer several independent ways to look. Each head has its own query, key and value projections. Each can learn a different similarity function. The heads run in parallel and their outputs are concatenated, joined end to end. A later projection then mixes them. Until that point the layer keeps several relations separate.
The number of heads is a hyperparameter, a setting you choose. It is not a consequence of the data. More heads mean more independent patterns and more score matrices to store. Fewer heads mean each one has to average more relations. The total width is usually held fixed. The choice is therefore about how to divide the same budget. A head that is too narrow cannot represent a rich similarity. A head that is too wide leaves little room for the others.
Split, attend, concatenate
The model dimension is divided by the number of heads. With a width of 512 and 8 heads, each head works in 64 dimensions. The total compute stays close to that of one full-width head. Each head attends in a smaller subspace. What changes is the number of independent patterns the layer can represent.
After attention, the head outputs are concatenated back to the full width. They then pass through an output projection. That projection is what lets information from different heads interact. Without it, the heads would stay separate channels until the next layer. The block would be a sum of independent attention results.
The parameter count is easy to estimate. Each head has three projections of model width by head width. The output projection is model width by model width. With a fixed total width, the attention parameters are roughly the same either way. Few wide heads and many narrow heads cost about the same. The difference is in what the softmax can separate.
The built-in layer
import torch
from torch import nn
layer = nn.MultiheadAttention(embed_dim=512, num_heads=8, batch_first=True)
x = torch.randn(4, 32, 512) # batch, tokens, d_model
out, weights = layer(x, x, x, need_weights=True)
print(out.shape) # (4, 32, 512)
print(weights.shape) # (4, 32, 32)Two heads from one vector
Run the split, then give each head its own small projection matrix instead of slicing the input, and compare the two outputs.
Heads are not modules
A head is often described as the one that tracks syntax or the one that resolves pronouns. Some heads do behave that way in some models. But heads are not designed for a job, and they are not guaranteed to keep one. Ablating a single head, which means removing it, often changes very little. Other heads carry redundant information.
The useful exceptions are worth knowing. Induction heads copy a token that followed the same prefix earlier. They appear in many trained transformers and seem to matter for in-context learning, the ability to use examples in the prompt. That is a discovered regularity, not a slot in the architecture. Treat head labels as hypotheses to test, not as documentation.
Head pruning is the practical version of this question. Remove a head and check the loss. If the loss barely moves, the head was redundant for that test set. If the loss rises, the head was carrying something. The result depends on the task. That is another reason a head’s name should not travel from one model to another.
When you change the model width, decide whether to keep the head dimension fixed. Doubling the width and doubling the head count keeps each head at the same size; doubling only the width makes every head wider and changes what the softmax can represent.
The cost of many heads
The score matrix is n × n per head. Attention memory therefore grows with the number of heads, even when the total width is fixed. For a long sequence that memory is often the limit, not the parameter count. Flash attention reduces the materialised matrix. It does not change the asymptotic cost.
Grouped-query attention is the common compromise. Several query heads share one key and value head. That shrinks the cache used during generation while keeping the query side expressive. Multi-query attention is the extreme case: one key and value head for all queries. The choice trades quality against the memory bandwidth of inference.
Two heads, two patterns
Run both heads on the same three tokens and compare their weight rows. Then swap the two query vectors and confirm the patterns swap with them.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
With a model width of 512 and 8 heads, how wide is each head?
