Attention at scale. Layers all the way down.
Everything we've covered so far—tokenization, embeddings, attention—comes together in the transformer. A transformer is a stack of layers. Each layer takes in a sequence of vectors, applies attention, then feedforward processing, and outputs a new sequence of vectors. Stack enough of these, and the model can do remarkable things.
There's no magic here. No understanding in the human sense. Just: learned representations, stacked and composed, trained on massive data to predict the next token. That's all a language model is. The transformer architecture is what makes that training tractable.
A transformer is a feedforward block + attention block, repeated N times. Each layer refines the representations. Depth (more layers) beats width (more neurons per layer). GPT-3 has 96 layers. That's it.
A transformer layer has two sub-blocks:
1. Multi-head self-attention: Tokens attend to other tokens in the same layer. The output is a context-enriched sequence.
2. Feedforward network: A standard two-layer neural network applied position-by-position. Same weights shared across all positions (but different weights than attention).
Each sub-block has a residual connection (skip connection) and layer normalization. These stabilize training and enable gradient flow through very deep networks.
Here's what happens across a transformer stack. Research on intermediate layers (BERT-style probing) shows learned representations evolve systematically:
Early layers preserve surface form. Middle layers extract structure. Late layers perform computation. This hierarchical composition is what makes transformers generalize: the same building blocks recombine to handle novel inputs.
Transformers are trained with self-supervised learning on text. The task: predict the next token given all previous tokens. This is called causal language modeling (CLM) or autoregressive modeling.
The key insight: you don't need labeled data. Text is labels. The next token is the label. Just keep predicting the next token across billions of examples, and the network learns to build useful internal representations.
Scale is not incidental. It's the primary driver of capability. More parameters + more data + more compute = better performance on nearly every benchmark. This relationship (the "scaling laws") was discovered empirically and has held across model generations. We don't fully understand why it works so well, but it does.
The original "Attention Is All You Need" paper described an encoder-decoder architecture (for translation). Most modern LLMs are decoder-only (GPT architecture). The difference:
Encoder: Sees the full sequence bidirectionally. Each token attends to all others. Good for understanding tasks (classification, NER).
Decoder: Causal mask. Each token only attends to previous tokens. Can't see future. Good for generation. What GPT uses.
Encoder-Decoder: Both. Used by T5, BART, and some translation models. Less common now.
Attention is permutation-invariant: "the dog bites" and "the bites dog" produce the same attention patterns without position information. Position is injected via positional encodings: fixed or learned vectors added to token embeddings that encode where in the sequence a token sits.
Modern models (RoPE, ALiBi) use rotary or bias-based approaches that let attention naturally incorporate position without adding to embeddings. This also allows generalization to longer contexts than seen during training (up to a point).
A transformer stacks attention + feedforward layers. Each layer refines token representations through learned attention patterns. Depth builds hierarchy: surface form → syntax → semantics → reasoning. Trained via next-token prediction on massive text. Scale is the primary driver of capability. Everything else follows from that.