Transformers and language models
Encoder vs decoder transformers
BERT looks both ways, T5 has an encoder and a decoder, and GPT looks only left. Decoder-only won for generative LLMs because one stack serves pretraining, SFT and chat.
"Transformer" is a family, not one diagram. The original 2017 model was encoder–decoder. BERT dropped the decoder. GPT dropped the encoder. Those three attention patterns still explain almost every open checkpoint.
Concept
Causal attention means each position may only attend to itself and earlier positions. That is the constraint that makes next-token prediction honest. See attention masks.
The big idea
| Family | Attention pattern | Typical use |
|---|---|---|
| Encoder-only (BERT) | Bidirectional over the input | Classification, embeddings, NLU |
| Encoder-decoder (T5, original transformer) | Bidirectional encoder plus causal decoder that cross-attends to the encoder | Classic seq2seq translation, some FLAN models |
| Decoder-only (GPT, Llama) | Causal self-attention only | Generative LLMs. Today's default |
Decoder-only won for LLMs because one stack serves pretraining, SFT and chat. Scaling recipes and inference stacks optimized for it. Next-token prediction transfers surprisingly far.
Walking through GPT-2 versus Llama
Same skeleton, different defaults. If you can redraw both, you can read almost any open LLM modeling_*.py.
| Piece | GPT-2 (nanoGPT) | Modern Llama-style |
|---|---|---|
| Norm | LayerNorm | RMSNorm |
| Norm placement | Pre-norm | Pre-norm |
| Position | Learned absolute wpe | RoPE on Q/K |
| Attention | Multi-head, dense KV | Often grouped-query attention |
| MLP | GELU, 4d expansion | SwiGLU, ~8/3 d width |
| Tied embeddings | Optional | Common |
Follow a single new decode token through a Llama-style stack:
- Id
t→ rowE[t](d,). - RoPE rotates Q/K using position index
n. - For each layer: RMSNorm → attention (query against cached K/V plus itself) → add → RMSNorm → SwiGLU → add.
- Final RMSNorm →
W_U→ logits (V,). - Sampler picks next id. Append. Repeat.
Training differs only in that all positions run in parallel with a causal mask and loss on every position's next-token target. If you can narrate those five steps with shapes, interviews and code review get easier.
Prefer reading one reference implementation (nanoGPT or Llama) end to end over collecting blog diagrams.
After L layers, take h ∈ R^{n × d} (or just the last position at decode time). The LM head is a linear map R^d → R^V. Training loss is cross-entropy of softmax(logits) versus the next-token id. At inference, sampling turns logits into a token.
Common mistakes
- Calling BERT a language model in the GPT sense. BERT is bidirectional. It does not generate left to right by default.
- Comparing a T5 to a Llama by parameter count alone. One has an encoder. The attention patterns differ.
- Dropping the causal mask in a decoder-only trainer. The model then cheats by reading the future.
Check yourself
- Why did decoder-only win for chat LLMs? Answer: One stack serves pretrain, SFT and chat, and the inference stack optimized for causal decode.
- Name two Llama-style replacements for GPT-2 defaults. Answer: RMSNorm instead of LayerNorm, RoPE instead of learned absolute positions, SwiGLU instead of GELU 4d (any two).
Key takeaways
- BERT looks both ways. T5 has encoder plus decoder. GPT looks only left.
- Decoder-only is the default generative LLM.
- GPT-2 versus Llama is the same skeleton with different norms, positions, attention and MLP.
- Narrating one decode step with shapes is the fluency test.
Further learning
Karpathy builds a GPT end to end in code — watch after you can sketch the block on a whiteboard.
Back to the map: the transformer architecture.
Related
The transformer architecture
Every component of a modern decoder-only LLM, from embeddings to the LM head, with parameter and FLOP counts.
Attention masks
A mask blocks some query-key pairs before softmax. Causal masks stop a language model from reading future tokens, and padding masks ignore filler in a batch.
The transformer block
A modern decoder block is pre-norm, attention, residual, pre-norm, MLP, residual. The residual stream is the highway. Sublayers read it and write an additive update.