ML Systems Lab Open interactive version →
Intermediate 29 min read Transformerarchitecturepositional encodingencoder-decoder

Transformer Architecture

Self-attention, positional encoding, encoder vs decoder, pre-norm vs post-norm

Attention has a surprising blind spot. Because it looks at all the words at once and just computes weighted averages, it does not inherently know their *order* — "the dog bit the man" and "the man bit the dog" contain the exact same words, and to raw attention they look identical. So the first thing a Transformer has to add is a sense of *position*.


Positional encoding: telling the model where each word sits

The fix is to stamp each word's representation with a position signal before the first layer — a little pattern that says "I am word 1," "I am word 2," and so on. The original Transformer used sine and cosine waves of different frequencies for this (the original paper only hedged that this *may* let the model extrapolate to unseen lengths — in practice that extrapolation is weak); modern models like LLaMA use RoPE, which bakes *relative* position straight into the attention comparison. RoPE's relative encoding is friendlier to extrapolation than absolute position encodings, but base RoPE still degrades past the trained context length without added scaling tricks (position interpolation, NTK-aware/YaRN scaling). Either way, once positions are added, the model can finally tell word order apart.


The Transformer block

Stack the pieces and you get the repeating block every Transformer is built from: normalise the inputs, run multi-head attention (words look at each other), add the result back through a residual shortcut, normalise again, run a small two-layer feed-forward network (FFN) on each word, and add that back too. Two details matter. The FFN is deliberately *wide* — usually 4× the model's width in the middle — because it acts as the model's *memory*, where a lot of its factual knowledge is stored; shrinking it is reported in interpretability and scaling studies to cost the model retrievable facts (this module doesn't walk through a worked number for that drop, unlike the √d_k computation in the attention module — treat the direction as right, not the exact magnitude). And the residual shortcuts are not decoration: they give the gradient a direct path back to every layer — necessary, but not sufficient, for stacking dozens of these blocks without the signal dying (exactly the vanishing-gradient fix from earlier). Residuals give the gradient a path; whether that path stays well-scaled as depth grows still depends on where LayerNorm sits relative to it, which is why Pre-LN vs Post-LN (below) matters.


Two flavours: encoder and decoder

The same block comes in two modes, set by *who is allowed to look at whom*. Encoder-only (BERT-style) lets every word see every other word, in both directions — great for *understanding* tasks like classification, where you want the fullest possible context. Decoder-only (GPT-style) masks the future, so each word can only see the words *before* it — which is exactly what you need to *generate* text one token at a time. Decoder-only models also get a training bonus: *every* token in a sequence is a prediction target at once, giving them far more learning signal per pass than BERT's "predict just the 15% we masked," which is a big part of why decoder-only models dominate at scale.

Key points

Takeaway

The Transformer's power rests on three mutually dependent components: direct all-to-all attention for O(1) path length, residual connections that route gradients to every layer simultaneously, and a 4×-expanded FFN that stores and retrieves factual knowledge — each degrades measurably without the others.

Recap

Check your understanding

Q1. Why does the transformer use positional encodings, and why does standard sinusoidal encoding have a theoretical property that could support extrapolation to longer sequences than seen in training — even though, per the original paper and in practice, that extrapolation benefit is weak?

Q2. The feed-forward sublayer in a transformer block has two linear layers with a nonlinearity in between: FFN(x) = W₂·ReLU(W₁x + b₁) + b₂. Select the TWO correct statements about its role and 4× width.

Q3. Why does training a transformer require a learning rate warmup, and what happens without it?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →