A transformer is a stack of weighted averages

Attention mixes values, the MLP transforms each position, and the residual stream carries the sentence forward. The diagram is less mysterious once you name the operations.

A transformer block is two sublayers with residual connections: attention, which lets positions exchange information, and an MLP, which transforms each position on its own. Stack that, normalize, and add an unembedding.

Attention is a weighted average

For each token, project the residual stream into queries, keys, and values. Score the query against every visible key, divide by the square root of the head dimension, and pass the scores through a softmax. The output is the weighted sum of the values. With a causal mask, a token can only see itself and the past.

scores  = q @ k.transpose(-2, -1) / math.sqrt(d)
weights = scores.masked_fill(mask, float("-inf")).softmax(-1)
out     = weights @ v

Ignore the batch and head dimensions; that is the operation. Multi-head attention runs several of these in parallel with smaller dimensions, concatenates the results, and projects them back.2 The weights are a distribution over positions, so calling the output a weighted average is literal, not a metaphor.

It is also not a full explanation of behavior. Attention weights are one lens, and a contentious one; a high weight does not always mean a position caused the output.1 The weighted-average view is still the right place to start.

The MLP is per-position

After attention, each position goes through a two-layer MLP with a nonlinearity. There is no mixing across tokens here; the MLP sees one position’s vector at a time. A large fraction of the parameters live in these layers. One useful interpretation is that the MLP acts as a key-value memory over learned patterns; it is an intuition, not a literal lookup table.

The residual stream is the shared vector that every block reads from and writes to. Attention and the MLP add their outputs to it, and normalization keeps the scale sane. At the end, an unembedding projects the residual stream to vocabulary logits — in generation, usually from the last position.

Why this mental model helps

Once the block is a weighted average plus a per-position transform, some practical questions get easier:

  • Long context is a cache problem. Every layer stores keys and values for every token; the arithmetic is in The KV cache is the real context window.
  • Retrieval is an input problem. If a fact has to come from a document, the document must be in the context. Attention cannot select a passage that was never retrieved; see RAG is a retrieval problem.
  • A model ignoring the middle of a long prompt is a position and attention effect, not simply a prompting failure. The paper is on the reading list.
  • “It knows the fact but won’t use it” is usually a question about whether the right context reached the right position, not about the size of the model.

The block is repetitive by design. Once you see the weighted average, the rest is scale, normalization, and a very large number of parameters.

  1. Attention weights are not a causal explanation by themselves. They tell you where a head looked, not what the rest of the network did with what it found. ↩
  2. Multi-head attention is not several full-size attentions. Each head has a smaller dimension, and the concatenation is projected back. Comparing head counts across models without the head dimension is not a comparison. ↩