datarekha

Anatomy of a modern LLM

What changed from the 2017 transformer to a modern LLM — RMSNorm, RoPE, grouped-query attention, and the SwiGLU gated FFN — with Qwen3 as the worked example.

11 min read Advanced NLP & Transformers Lesson 29 of 44

What you'll learn

  • The five upgrades that separate a modern LLM from the 2017 transformer
  • What RMSNorm, RoPE, GQA, and SwiGLU each replaced and why
  • How a SwiGLU gated feed-forward actually works, with a worked example
  • How dense and MoE variants of the same block differ — Qwen3 as the case study

Before you start

The 2017 “Attention Is All You Need” transformer is still the skeleton of every LLM — but almost every organ has been swapped. If you opened LLaMA, Mistral, or Qwen3 expecting the original block, you’d find five quiet upgrades. This lesson walks the modern decoder block and explains each change on its own terms.

What actually changed

2017 transformerModern LLMWhy
LayerNorm, post-normRMSNorm, pre-normcheaper; far more stable to train deep
Absolute positional embeddingsRoPErelative positions; extrapolates to longer context
Multi-head attention (MHA)Grouped-query attention (GQA)shrinks the KV cache at inference
ReLU/GELU MLPSwiGLU gated FFNa learned per-feature gate; better quality per param
Dense FFN(optionally) Mixture of Expertsscale parameters without scaling compute

Everything else — residual connections, the attention-then-FFN rhythm, stacking L identical blocks — is unchanged. Let’s take the new organs one at a time.

RMSNorm, and why pre-norm

The original block applied LayerNorm after the residual add (post-norm), which makes very deep stacks hard to train. Modern LLMs put the norm before each sublayer (pre-norm) so a clean residual highway runs uninterrupted from input to output — gradients flow, and 100-layer models train stably.

They also drop LayerNorm for RMSNorm, which skips the mean-centering and just rescales by the root-mean-square. Fewer operations, no bias term, and in practice no quality loss:

import numpy as np
x = np.array([2.0, -1.0, 0.5, 3.0, -2.0])

# LayerNorm: subtract mean, divide by std, then scale/shift
ln = (x - x.mean()) / (x.std() + 1e-5)

# RMSNorm: no mean-centering — just divide by the root-mean-square
rms = x / (np.sqrt(np.mean(x**2)) + 1e-5)

print("LayerNorm:", np.round(ln, 3))
print("RMSNorm  :", np.round(rms, 3))
# RMSNorm drops the mean subtraction and the bias — fewer ops, same stability.
LayerNorm: [ 0.813 -0.813  0.     1.356 -1.356]
RMSNorm  : [ 1.047 -0.523  0.262  1.57  -1.047]

LayerNorm re-centers around zero (note its output averages to 0); RMSNorm skips that step and only rescales — fewer operations, no bias, and in practice no quality loss.

RoPE — position without a position embedding

The original added a fixed positional vector to each token. Modern models use rotary positional embeddings (RoPE): they rotate the query and key vectors by an angle proportional to position before the dot product. Because attention then depends on the difference of rotation angles, the model sees relative position for free — and you can stretch RoPE to context lengths it never trained on. (Full derivation in Positional encodings & RoPE.)

GQA — the same attention, a smaller cache

Multi-head attention gives every query head its own key and value heads. At inference you must cache K and V for every token, and that KV cache is usually the memory bottleneck. Grouped-query attention lets several query heads share one K/V head — e.g. 32 query heads but only 8 K/V heads cuts the cache 4×, with negligible quality loss. (Details and the MHA→MQA→GQA→MLA ladder in Multi-head attention.)

SwiGLU — the gated feed-forward

The biggest change inside the block is the FFN. The original was Linear → ReLU → Linear. Modern models use SwiGLU: split the input into two linear paths — a value path and a gate path — pass the gate through SiLU (x·σ(x)), then multiply them elementwise before projecting down:

SwiGLU(x) = ( SiLU(x · W_gate) ⊙ (x · W_up) ) · W_down

Why is a multiply better than a ReLU? A ReLU can only switch a feature fully on or off. The gate is a smooth, learned, per-feature volume knob — the network decides how much of each feature to let through. Where SiLU(gate) ≈ 0 the feature is suppressed no matter how large its value; where the gate opens, the feature passes and can even be amplified.

SwiGLU: a value path scaled by a SiLU gatexLinear · W_gateSiLUgate path → smooth 0…large knobLinear · W_upvalue pathelementwise multiplyLinear · W_down→ outgate ≈ 0 → feature suppressed whatever its value · gate open → passes and can amplify
SwiGLU splits the input into a value path and a gate path; SiLU turns the gate into a smooth per-feature knob, and the elementwise product decides how much of each feature survives.

Run four features through the gate and watch the volume knob act independently on each:

import numpy as np
def silu(z): return z / (1.0 + np.exp(-z))   # SiLU(z) = z·σ(z)

value = np.array([ 1.8, -2.0,  0.9,  3.1])   # the up path    (x·W_up)
gate  = np.array([ 2.5, -3.0,  0.1,  1.2])   # the gate path  (x·W_gate)
out   = value * silu(gate)                    # elementwise gated value

print("value      :", np.round(value, 2))
print("SiLU(gate) :", np.round(silu(gate), 3))
print("gated out  :", np.round(out, 3))
value      : [ 1.8 -2.   0.9  3.1]
SiLU(gate) : [ 2.31  -0.142  0.052  0.922]
gated out  : [4.159 0.285 0.047 2.859]

Feature index 2 has a healthy value of 0.9, but its gate is nearly shut (SiLU(0.1) ≈ 0.05), so it leaves as 0.047 — effectively switched off. Feature 0’s open gate (SiLU(2.5) ≈ 2.31) amplifies it from 1.8 to 4.16. That independent, per-feature control is exactly what a single ReLU cannot do.

(To keep the parameter count fair, SwiGLU uses a smaller hidden width — about ⅔ — since it has three weight matrices instead of two.)

Putting the block together

Stack it and you have a modern decoder layer — L of these, then a final RMSNorm and the output projection:

x ─►┌─ RMSNorm ─► GQA attention (RoPE on Q,K) ─┐─► + ─►┌─ RMSNorm ─► SwiGLU FFN ─┐─► + ─►
    └──────────── residual ───────────────────┘       └──────── residual ───────┘

In one breath

  • A modern LLM is the 2017 transformer with five organs swapped; the residual + attention-then-FFN skeleton is untouched.
  • RMSNorm + pre-norm: drop the mean-centering and put the norm inside the residual branch, so the highway stays clean and deep stacks train stably.
  • RoPE rotates Q and K for relative position that extrapolates to longer context; GQA lets several query heads share one K/V head, shrinking the inference KV cache (e.g. 32 query / 8 K-V heads = 4× smaller).
  • SwiGLU replaces the ReLU MLP with SiLU(x·W_gate) ⊙ (x·W_up) then ·W_down — a smooth per-feature volume knob (a near-shut gate kills a feature whatever its value; an open gate amplifies). MoE optionally swaps the dense FFN for many experts with a router, scaling parameters without scaling per-token compute — the dense-vs-MoE split in Qwen3.

Check yourself

Quick check

0/3
Q1Why do modern LLMs use pre-norm (normalise before each sublayer) instead of the original post-norm?
Q2What makes SwiGLU's gated FFN more expressive than a plain ReLU MLP?
Q3A dense Qwen3 model and an MoE Qwen3 model share the same block. What's the key difference?

What to remember

  • A modern LLM is the 2017 transformer with five organs swapped: RMSNorm + pre-norm, RoPE, GQA, SwiGLU, and optionally MoE.
  • RMSNorm drops mean-centering (cheaper); pre-norm keeps the residual highway clean for stable deep training.
  • RoPE rotates Q/K for relative position that extrapolates; GQA shares K/V heads to shrink the inference KV cache.
  • SwiGLU multiplies a value path by a learned SiLU gate — a smooth per-feature volume knob that beats a ReLU MLP. MoE scales parameters without scaling per-token compute.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

FAQCommon questions

Questions about this lesson

What are the main differences between the 2017 transformer and a modern LLM?

Five swaps, on the same skeleton: LayerNorm becomes RMSNorm and moves before each sublayer (pre-norm) for cheaper, more stable deep training; absolute positional embeddings become RoPE for relative position that extrapolates; multi-head attention becomes grouped-query attention to shrink the inference KV cache; the ReLU/GELU MLP becomes a SwiGLU gated feed-forward; and large models optionally replace the dense FFN with a Mixture of Experts. Residual connections and the attention-then-FFN structure are unchanged.

Why RMSNorm instead of LayerNorm in modern LLMs?

RMSNorm rescales activations by their root-mean-square only, dropping LayerNorm's mean-centering and bias term. That's fewer operations and one fewer parameter group, with no measurable quality loss on language modeling — so LLaMA, Mistral, and Qwen all use it. Modern blocks also apply the norm before each sublayer (pre-norm) so the residual path stays clean end to end.

What does 'dense' vs 'MoE' mean for a model like Qwen3?

In a dense model every parameter is used for every token. In a Mixture-of-Experts model the single feed-forward is replaced by many expert FFNs plus a router that activates only a few experts per token, so total parameter count (and capacity) grows while per-token compute stays roughly fixed. Qwen3's small model is dense; its larger variants are MoE — the same decoder block, two ways to scale.

Practice this in an interview

All questions
What is SwiGLU and why did modern LLMs replace the ReLU/GELU MLP with it?

SwiGLU is a gated feed-forward layer: it projects the input into two paths, passes one (the gate) through SiLU, multiplies the two elementwise, then projects down — SiLU(x·W_gate) ⊙ (x·W_up) · W_down. The elementwise gate is a smooth, learned, per-feature volume control, so the network can decide how much of each feature to pass rather than a hard ReLU on/off. It gives better quality per parameter, which is why LLaMA, Mistral, and Qwen use it; to keep the parameter budget equal it uses a smaller (~2/3) hidden width since it has three weight matrices instead of two.

What is RMSNorm and why do modern LLMs like Llama use it instead of LayerNorm?

RMSNorm normalizes activations by their root-mean-square only, dropping the mean-centering and bias terms used in LayerNorm. It is roughly 10 to 20 percent cheaper with no measurable quality loss on language modeling, which is why Llama, Mistral, and most modern open-weight LLMs adopt it.

Describe the components of a transformer block and the difference between pre-norm and post-norm.

A transformer block has a multi-head self-attention sublayer and a position-wise feed-forward sublayer, each wrapped in a residual connection and normalization. Post-norm (the original transformer) applies normalization after the residual add, while pre-norm applies it inside the residual branch before the sublayer; pre-norm gives more stable gradients and is standard in modern deep LLMs.

How does LLMOps differ from classical MLOps, and what new operational challenges do LLMs introduce?

LLMOps extends classical MLOps to handle foundation model scale, prompt-based configuration, non-deterministic outputs, and evaluation without a scalar ground truth. Key new concerns include prompt versioning, output quality evaluation via LLM judges or human review, hallucination monitoring, cost management, and RAG pipeline observability.

Related lessons

Explore further

Skip to content