Zenaique

Match GPT-2 versus Llama-2 attention design choices to their differences

Match pairs·Medium·4.0 · 0·~2 min·Asked atArize AiHclStability Ai·Relevant atMeta
Attempt it

Drag each answer to line up with its matching prompt

Positional encoding

Both use the same scaled dot product attention, softmax(QK^T / sqrt(d_k)) V

Normalization layer

GPT-2 uses GELU vs Llama-2 uses SwiGLU

KV-head sharing (70B scale)

GPT-2 learned absolute PE added at input vs Llama-2 RoPE applied inside attention

Activation in FFN block

GPT-2 plain multi-head attention vs Llama-2 70B grouped query attention (8 KV heads)

Attention math core

GPT-2 uses LayerNorm (mean + variance) vs Llama-2 uses RMSNorm (variance only, no bias)

TL;DR

GPT-2 to Llama-2: PE moved to RoPE, LayerNorm became RMSNorm, GELU became SwiGLU, 70B added GQA. The softmax(QK^T/sqrt(d_k))V core stayed identical.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Think of two cars from the same family. The 2019 sedan and the 2023 sedan look similar, but the newer one has a smarter steering system, lighter suspension, and a more efficient engine that runs on the same fuel. The seats, the steering wheel, the brakes work the same way. GPT-2 and Llama-2 are like that pair. The core engine, scaled dot-product attention, is unchanged. Around it, every component got swapped for a quieter, faster, or cheaper version: rotary position info instead of added embeddings, simpler normalization, a gated activation, and KV sharing at scale.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

GPT-2 (2019) and Llama-2 (2023) are decoder-only transformers with the same overall shape: alternating attention and FFN blocks, residual stream, learned token embeddings, standard scaled dot-product attention. The four-year gap between them was filled with engineering work, and Llama-2 swapped four specific components for empirically-better versions while keeping the architectural core unchanged.

The pattern matters because it tells you what kind of progress was happening: not a new attention mechanism, not a fundamental rethink, but careful incremental swaps each justified by an ablation study. This deep dive walks each of the four changes, explains why each one was the right move at the time, names where each component sits in the modern stack, and ends with the components Llama-2 itself retained that the next generation (MoE, MLA) now abandons.

Positional encoding: learned absolute → RoPE

GPT-2's PE is a learned (max_len, d_model) table added to token embeddings at the input. Each position 0 through max_len gets its own learned vector. The position information enters once at the input layer and the model has to propagate it through every transformer block.

Where the design breaks

Two failure modes: at training-time max_len you have no PE vector for positions beyond it (hard cap on context length), and even within the window the absolute formulation does not naturally encode the (m - n) shifts that attention scores want.

What RoPE does

RoPE rotates Q and K vectors by position-dependent angles before the attention dot product. The rotation angle at position m is:

θm=m/100002i/d\theta_m = m / 10000^{2i/d}

Applied to even-odd pairs of the Q and K dimensions, the rotation has the property that q_m · k_n factors through (m - n), giving the attention dot product a built-in relative-position property without a learnable table.

Why this matters

No learnable PE parameters. No hard cap on context (rotations are well-defined at any m). Direct relative-position dependence in the dot product. Long-context extension techniques (RoPE base scaling, NTK-aware, YaRN, LongRoPE) all build on this foundation; a learned absolute PE has no analogous extension story.

Every modern open-weight LLM (Llama 4 Maverick, Qwen 3.5, Mistral Large 3, DeepSeek V4) inherits RoPE. The learned-absolute design did not survive the long-context era.

Normalization: LayerNorm → RMSNorm
Activation: GELU → SwiGLU
Head sharing: MHA → GQA at 70B
What stayed the same, and what Llama-2 itself now lacks
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
ComponentGPT-2 (2019)Llama-2 (2023)
Positional encodingLearned absolute PE at inputRoPE applied to Q, K inside attention
NormalizationLayerNorm (mean + variance, with bias)RMSNorm (variance only, no bias)
FFN activationGELUSwiGLU (gated, three weight matrices)
KV-head sharing (70B)MHA (one KV per query head)GQA (64 query, 8 KV)
Attention math coresoftmax(QK^T/sqrt(d_k)) Vsoftmax(QK^T/sqrt(d_k)) V (identical)

Real products, models, and research that use this idea.

  • Llama-2 70B uses GQA with 64 query heads grouped onto 8 KV heads; the 7B and 13B variants keep plain MHA.
  • Mistral 7B and Mixtral 8x7B inherited Llama-2's stack: RoPE, RMSNorm, SwiGLU, GQA on the larger variants.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does Llama-2 70B specifically pick GQA with 8 KV heads, not 4 or 16?
A

Meta's GQA ablations swept group sizes and found 8 KV heads landed in the sweet spot: 8x KV cache reduction relative to 64 (the largest possible saving), with negligible quality loss versus MHA. Smaller (4 KV) gave more savings but the perplexity hit started showing; larger (16 KV) saved less without quality benefit. The decision is empirical, anchored to specific ablation numbers, not derived from theory.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Assuming Llama-2 changed the attention math itself. The core softmax(QK^T/sqrt(d_k))V is identical to GPT-2; what moved is the surrounding engineering, PE, normalization, FFN, head sharing.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • The four components that changed between GPT-2 and Llama-2

  • Why RoPE replaced learned absolute PE

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium