What Changed Since 2017: Modern Transformer Upgrades
Decoder-only block and masked attention
Original transformer had an encoder and a decoder (useful for translation, e.g. French -> English). Language models use only the right part. -> Decoder-only models.
Block order: tokens -> token embeddings -> positional information -> repeated transformer blocks (attention, add + normalize, feed-forward) -> linear layer -> softmax -> next token.
Masked multi-head attention
- Model is only a next-token predictor.
- While predicting token t, it may see only tokens before t.
- Training text 'my name is Richard': step 0 sees 'my' -> output 'name'; step 1 sees 'my name' -> 'is'; step 2 sees 'my name is' -> 'Richard'.
The Problem: the full training text is available, so unmasked attention could read the answer. The model would copy, not learn.
The Solution: in the attention score matrix (tokens x tokens), set every value above the diagonal to negative infinity. Softmax turns them into 0. Works for any sequence length.
The mask is negative infinity, not zero, because softmax(-inf) = 0 and the remaining weights still sum to 1.
Four upgrades overview
| Part | Old | Modern |
|---|---|---|
| Normalization | LayerNorm (post-layer) | RMSNorm (pre-layer) |
| Feed-forward activation | ReLU | SwiGLU |
| Positional encoding | Sinusoidal (absolute, fixed) | RoPE (rotary, relative) |
| Attention | Multi-head | Grouped Query Attention |
Used by Llama, Gemma, Mistral and similar models. The core idea of attention is unchanged. These are memory and compute optimizations.
RMSNorm and pre-normalization
Normalization only keeps numbers from getting too big or too small. It is not related to softmax.
LayerNorm does four things: compute mean and centre at zero, compute variance, normalize, then scale and shift. RMSNorm removes the mean subtraction and bias. Experiments showed centering around zero does not matter.
- Epsilon - tiny constant so we never divide by zero.
- Fewer calculations and fewer parameters. Faster, and it runs after every layer.
Pre-norm vs post-norm
- Old (2019-2020): layer -> normalize (post-norm).
- Now: normalize -> layer (pre-norm).
- Numbers are bounded from the very first step, which matters for deep stacks (e.g. 30 layers).
SwiGLU feed-forward
The Problem: ReLU zeroes every negative value abruptly with a sharp corner. Inputs like -0.3 should be dimmed, not switched off.
The Solution: SiLU. Same as ReLU for large positive values, but near the origin it curves smoothly towards zero.
SwiGLU is not one function. It is a gating design -> Gated Linear Unit with SiLU inside.
- Path 1 (gate): input -> linear -> SiLU. Decides which features matter.
- Path 2 (raw): input -> linear, no activation. Carries the information.
- Multiply both. Unimportant features are suppressed, important ones pass.
- The model learns what to gate during training. Nobody hand-codes it.
RoPE: rotary positional embeddings
The Problem with sinusoidal encoding
- Absolute encoding: token 47 only knows 'I am position 47'.
- No relative distance. 'The cat sat on the mat' at positions 1-5 and again at positions 7-11 means the same, but gets different position signals.
- Model has to relearn the same relation at every position.
The Solution: RoPE - rotate vectors by position so only the gap matters.
Example
Clock analogy: hands at 12 and 2 are 60 degrees apart. Move them to 7 and 9 and the gap is still 60 degrees. In 'my name is Rishab', 'my' and 'Rishab' are two words apart wherever the phrase appears.
- No positional embedding is added to token embeddings.
- Q and K vectors are split into pairs (x1, x2).
- Each pair is rotated by m*theta, where m is the token position and theta is a frequency (sine/cosine based).
- Rotation uses a 2D rotation matrix.
Why only Q and K?
- Attention score = Q . K, a similarity score. a.b = |a||b|cos(theta).
- Rotating both by the same amount keeps the angle between them, so the score depends only on relative distance.
- V holds the actual content of the token. It should not change with position.
- Low dimension pairs rotate fast -> nearby relationships.
- High dimension pairs rotate slowly -> distant relationships.
- Sinusoidal says only 'I am token 47'. RoPE also lets attention work out 'I follow token 5'. Relative information is added to what the model already knows.
Sinusoidal has no trainable parameters in the lecture's framing; RoPE rotates Q and K inside attention instead of adding to embeddings.
Grouped Query Attention (GQA)
The Problem: KV cache grows with sequence length. Memory and bandwidth become the bottleneck.
| Type | K/V sharing | Result |
|---|---|---|
| Multi-head (MHA) | Q1-K1V1, Q2-K2V2, ... each its own | Best quality, most memory (8 K/V for 8 queries) |
| Multi-query (MQA) | All queries share one K, V | Too much compression, quality drops |
| Grouped query (GQA) | Q1,Q2 -> K1V1; Q3,Q4 -> K2V2 | Almost no loss, 4 K/V for 8 queries |
Example
Queries are news reporters; K and V are the camera and production unit. MHA: each reporter has their own. MQA: one crew for all, so reporters on different stories are stuck. GQA: reporters on the same event (a tsunami: financial loss vs physical damage) share a crew.
- Group size 2 worked best in experiments. Larger groups hurt performance.
- KV cache memory cut by 2x, faster memory transfer.
- Chosen by benchmarking MHA, MQA and GQA.
Glossary
- Decoder-only
- Architecture using only the transformer decoder; used by all LLMs.
- Masked attention
- Attention where future tokens are hidden with negative infinity before softmax.
- RMSNorm
- Normalization that divides by root mean square, with no mean centering.
- Pre-norm
- Normalizing before attention and FFN rather than after.
- SiLU
- Smooth ReLU-like activation, x times sigmoid(x).
- SwiGLU
- Gated FFN: a SiLU gate path multiplied with a raw path.
- RoPE
- Rotary positional embedding; rotates Q and K by position to encode relative distance.
- GQA
- Grouped Query Attention; groups of query heads share one key and value.
- Epsilon
- Tiny constant added to avoid division by zero.