Cheat sheets
Transformer architecture
Attention, blocks, shapes, families, and costs on one page.
Attention: , with , , .
| Stage | Purpose |
|---|---|
| Token + position embeddings | identity and order |
| Layer normalization | stable activations (pre-norm in modern models) |
| Multi-head self-attention | mix information across positions |
| Residual connection | preserve signal, help gradients |
| Feed-forward network () | transform each position; most parameters |
| Final norm + output head | logits over the vocabulary |
Shapes (sequence length , width , heads, ):
| Tensor | Shape |
|---|---|
| per head | |
| , attention weights | per head |
| Block output |
Parameters per block: attention ; FFN ; two layer norms . GPT-2 small (, 12 layers): 124M total.
| Family | Mask | Use |
|---|---|---|
| Encoder-only | none (bidirectional) | classification, embeddings, rerankers |
| Decoder-only | causal | generation, chat LLMs |
| Encoder-decoder | causal decoder + cross-attention | translation, summarization |
Costs: attention per layer; KV cache grows with × layers × width; generation with a KV cache costs per new token.
Lessons: Self-attention · The transformer architecture · Lab: attention