Concept · Chapter 7: Transformers
The Transformer Block
A Transformer block is attention followed by a feed-forward network, each wrapped in normalization and a residual connection — and a Transformer is just many identical blocks stacked.
The problem
Attention alone mixes information but can't compute much, and deep stacks of layers are hard to train.
The solution
Pair attention (communication between tokens) with an MLP (computation within tokens), and wrap both in layer norm and residual connections so dozens of blocks can be stacked stably.
The consequence
A simple, uniform, highly parallel unit that scales: the same block design, with small modifications, runs inside BERT, GPT, T5, vision Transformers and today's LLMs.
You should understand first
The recipe
A (decoder, pre-LN) Transformer block is two lines:
Input shape , output shape . Because the shape is preserved, you can stack as many as you can afford.
The whole model around it
Tokens → embeddings
Look up a vector for each token ID; add (or later apply) positional information.N × block
Each block lets tokens exchange information (attention) and then processes each token (FFN), refining the residual stream.Final LayerNorm → LM head
Map each position's vector to one logit per vocabulary entry.Softmax
Turn the last position's logits into a next-token distribution.
Try it
Try it · toy model
Step through a real forward pass of one GPT-style Transformer block — embeddings, positions, multi-head attention, residuals, the MLP and the output softmax — with the tensor shape at every stage.
Can you draw it?
The goal of this chapter is that you can sketch this on a whiteboard: a tall box repeated N times containing LN → attention → add, LN → MLP → add; embeddings and positions at the bottom; LN, linear and softmax at the top; arrows showing the residual path bypassing each sublayer. If you can, you understand the architecture of every GPT-style model.
Why should I care?
As a researcher
Most architecture papers are edits to this block — normalization, attention variant, activation, positions. You need the baseline to see what changed.
As an engineer
Parameter count, memory, FLOPs and latency all follow from the block's shapes: d_model, heads, FFN width and number of layers.
Modern systems that depend on it
- GPT-style LLMs
- BERT-style encoders
- Vision Transformers
- Speech models like Whisper
Historical context
Before
Recurrent networks (LSTMs, GRUs) with attention bolted on; convolutional sequence models.
After
Stacked into encoder-only, decoder-only and encoder–decoder models; refined with pre-LN, RMSNorm, SwiGLU, RoPE and efficient attention.
Used today
Every modern large language model is a stack of (lightly modified) Transformer blocks.
What to remember
- Block (pre-LN): x = x + MHA(LN(x)); then x = x + FFN(LN(x)).
- Attention = communication between tokens; FFN = computation within each token.
- Input and output have the same shape [n × d_model], so blocks stack.
- Around the stack: embeddings + positions below, final LN + LM head + softmax above.
- Scale knobs: layers (N), width (d_model), heads (h), FFN width (usually 4·d_model).
Key papers
Attention Is All You Need
Ashish Vaswani, Noam Shazeer et al. · 2017 · NeurIPS 2017
Introduced the Transformer — the architecture behind BERT, GPT and nearly every modern large language model, and later adapted to vision, audio and more.
How to read it: Section 3 is the architecture — read it with Figure 1 open. Sections 3.2.1–3.2.2 contain the attention equation. You can skim the training details on a first pass.
Deep Residual Learning for Image Recognition
Kaiming He, Xiangyu Zhang et al. · 2015 · CVPR 2016
Residual (skip) connections made very deep networks trainable. Every Transformer block relies on the same trick.
Layer Normalization
Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton · 2016
The normalization used inside Transformers; it keeps activations at a stable scale regardless of batch size.
Watch
3Blue1Brown
Transformers, the tech behind LLMs | Deep Learning Chapter 5
A visual tour of a GPT from input text to next-token probabilities — ideal before or right after Chapter 7.
Andrej Karpathy
Let's build GPT: from scratch, in code, spelled out.
The best way to reach IMPLEMENT level on Transformers: write one yourself, line by line, in PyTorch.