Attention lets every position in a sequence read from every other position, with weights computed from the data itself. The transformer (Vaswani et al., 2017) stacks attention with a per-position MLP and residual connections, and drops recurrence entirely. Nearly every large language model, and many vision and audio models, are variations of that block. This page is the shape-and-cost reference I wanted when reading the lecture slides: what each matrix looks like, where the parameters live, and why long contexts are expensive.

Every word looks at every other word and decides which ones matter for it. Image: Yuening Jia, CC BY-SA 3.0, via Wikimedia Commons.
Mental Model
- Each token produces a query, a key and a value. A query is compared with all keys, the scores become weights through a softmax, and the output is the weighted sum of the values.
- Attention itself has no notion of order. Position information has to be injected, either added to the embeddings or rotated into queries and keys.
- Multi-head attention runs several smaller attentions in parallel so that different heads can look for different relations.
- A transformer layer is attention (mixing information across tokens) followed by an MLP (processing each token on its own), each wrapped in a residual connection and a normalization.
- The score matrix is , so cost grows quadratically with sequence length. Generation avoids recomputation with a KV cache.
1. Scaled Dot-Product Attention
For a sequence of tokens with model width , the layer computes
with , , . Then
The shapes are , the softmax is applied along each row, and the result times is . Row of the weight matrix says how much token reads from each token , and the rows sum to 1.
Why Divide by
If the entries of and are independent with mean 0 and variance 1, then has mean 0 and variance . With the raw scores have a standard deviation of 8, so the softmax is close to one-hot and its gradient is nearly zero for most entries. Dividing by brings the variance back to 1. I checked this with 100,000 random vector pairs of length 64: the variance of the raw dot product came out at 64.4, and at 1.006 after scaling.
2. Multi-Head Attention
Instead of one attention with width , use heads with . Each head has its own ; the head outputs are concatenated back to width and mixed by an output matrix . The total cost is about the same as single-head attention at full width, but the model gets different attention patterns per layer.
3. Causal Masking
A language model that predicts token must not see tokens after . Before the softmax, set the scores for to , which gives them weight exactly 0. The weight matrix becomes lower triangular, and the first row is always because the first token can only attend to itself. Encoders (BERT-style) and cross-attention skip the mask.
import numpy as np
def softmax(x):
e = np.exp(x - x.max(-1, keepdims=True))
return e / e.sum(-1, keepdims=True)
def attention(Q, K, V, causal=False):
scores = Q @ K.T / np.sqrt(Q.shape[-1])
if causal:
scores = np.where(np.tri(*scores.shape, dtype=bool), scores, -np.inf)
w = softmax(scores)
return w @ V, w
rng = np.random.default_rng(0)
n, d_model, h = 4, 8, 2
d_k = d_model // h
X = rng.normal(size=(n, d_model))
Wq, Wk, Wv, Wo = (rng.normal(size=(d_model, d_model)) / np.sqrt(d_model) for _ in range(4))
split = lambda M: M.reshape(n, h, d_k).transpose(1, 0, 2) # (h, n, d_k)
Q, K, V = split(X @ Wq), split(X @ Wk), split(X @ Wv)
heads = [attention(Q[i], K[i], V[i], causal=True)[0] for i in range(h)]
out = np.concatenate(heads, axis=-1) @ Wo
_, w = attention(Q[0], K[0], V[0], causal=True)
print(out.shape, w.sum(-1), np.triu(w, 1).sum())Running this prints (4, 8), row sums of 1 for all four rows, and 0 for everything above the diagonal.
4. Positional Information
Without positions, shuffling the input tokens just shuffles the outputs. The original transformer added sinusoidal vectors to the token embeddings:
Each pair of dimensions is a sine and cosine at a different wavelength, from up to . Learned absolute position embeddings are the other common choice.
Rotary position embeddings (RoPE, Su et al., 2021) take a different route. They rotate each consecutive pair of dimensions of and by an angle proportional to the position. Because rotations compose, the dot product depends only on the offset , so relative position enters the attention score directly. Many recent open models use RoPE.
5. The Transformer Block
A pre-norm block, as used in most current models, is
The original paper normalized after the residual addition (post-norm). Pre-norm keeps a clean identity path through the whole stack, which makes deep models easier to train without careful warm-up. The MLP is two linear layers with a nonlinearity, expanding to (typically ) and projecting back.
Parameters of One Layer
Ignoring biases, with , , (the base model of the original paper):
| Part | Count | Parameters |
|---|---|---|
| 1,048,576 | ||
| MLP up and down | 2,097,152 | |
| Two LayerNorms (scale and shift) | 2,048 | |
| Total | 3,147,776 |
The number of heads does not change the count, since the head width shrinks as the head count grows. About two thirds of a layer’s weights are in the MLP. At , the attention and MLP weights come to 2,359,296 and 4,718,592, so 12 such layers hold about 85 million parameters before the embedding table.
6. Cost and the KV Cache
The score matrix has entries per head, so compute is and a naive implementation uses memory. Doubling the context quadruples the attention cost. Exact fast kernels such as FlashAttention compute the same result in tiles without materializing the full matrix, which cuts memory traffic but not the quadratic compute.
During generation the new token’s query needs the keys and values of all earlier tokens. Those do not change, so they are stored: the KV cache. Each new token then costs per layer instead of recomputing the whole prefix. The price is memory: each token stores values per layer. For 32 layers, and 16-bit values that is bytes, half a MiB per token, or about 2 GiB for a 4096-token context. Sharing keys and values across heads (multi-query or grouped-query attention) shrinks it.
Common mistake
Forgetting the mask during training of a decoder-only model. The loss drops to nearly zero because each position can read its own target, and generation then produces garbage.
Related
- MPL 05: Transformers and MPL 06: Vision Transformers, the lectures behind this page
- Foundation Models 03, the path from n-gram counts to neural next-token prediction
- Embeddings, what the input vectors to the first layer are
- Fine-Tuning and LoRA, which adapts exactly the projection matrices counted above
- Convolutional Networks, the architecture attention displaced in vision
- Graph Neural Networks, where attention over neighbours gives GAT