Transformers and language models
Self-attention
The operation at the core of every modern language model. Each position builds a weighted average of the others, with weights set by query-key matches.
Every modern language model is built around one operation: self-attention. It takes a sequence of token vectors and lets each position pull in information from the other positions it needs. The weights are not fixed. They depend on the content, so the model decides at each step which tokens matter.
Older sequence models read tokens one after another. Information from far back had to pass through every step in between. Attention lets any position connect to any other in one step, and all positions can be computed in parallel. That is why transformers train quickly and scale well.
The mechanism in one line: each position builds a query, every position offers a key, and the query-key match sets the weights used to mix the values.
Concepts in this topic
Read in this order. Each page builds on the one before it.
| Step | Concept | What it answers |
|---|---|---|
| 1 | Softmax and dot products | How do raw scores become weights? |
| 2 | Queries, keys and values | How does a position find what it needs? |
| 3 | Attention masks | How do you stop a model from reading the future or the padding? |
| 4 | Multi-head attention | Why run several attention patterns side by side? |
| 5 | Cost and long context | Why does long input get expensive, and what helps? |
When attention looks wrong
Check these first. Most bugs are in one of them.
- Did you scale by the head size
√d_k, not by√d_model? - Is the causal mask upper-triangular with
diagonal=1, so each token can still see itself? - Does softmax run over the keys (the last axis), not over heads?
- Are you training in FP16? Prefer BF16, or a stable softmax kernel. Raw FP16 softmax is a common source of NaNs.
- Did you compare your op with a library implementation on random inputs before blaming the optimizer?
Common mistakes
- Mixing up the three names. Self-attention takes Q, K and V from the same input. Cross-attention takes Q from one sequence and K and V from another. Causal attention is self-attention with future positions masked.
- Assuming the full
n × nmatrix exists. Fused kernels such as FlashAttention compute the result without storing that matrix, so you cannot always inspect the weights. - Leaving training mode on in production. Dropout should be off at inference. Call
model.eval(), or quality drops silently.
Key takeaways
- Self-attention is a content-based weighted average of value vectors.
- Weights come from query-key scores, scaled by
√d_kand normalized with softmax. - Masks act on scores before softmax: causal masks stop future peeking, padding masks ignore filler.
- Multi-head attention splits the width across heads, then concatenates and projects back.
- Cost grows with
n², which is the root of long-context pressure.
Further learning
Grant Sanderson's visual walkthrough of attention is the best intuition before the paper.
The Illustrated Transformer (Jay Alammar)Discussions: Hacker News (65 points, 4 comments), Reddit r/MachineLearning (29 points, 3 comments) Translations: Arabic, Chinese (Simplified) 1, Chinese (Simplified) 2, French 1, French 2, Italian, Japanese, Korean, P…jalammar.github.ioRelated
Sequence models and the road to attention
RNNs, LSTMs and seq2seq — the problems they hit, and how attention emerged to fix them before transformers took over.
The transformer architecture
Every component of a modern decoder-only LLM, from embeddings to the LM head, with parameter and FLOP counts.
FlashAttention
Why standard attention is HBM-bound, how tiling plus online softmax computes exact attention in on-chip SRAM, and what FlashAttention-2/3 and decode variants change.
How transformers work (practitioner view)
Just enough transformer internals to predict model behavior — attention, context, and why the KV cache shapes pricing.