Topic 2 of 6 · 5 min read
Pre-norm, RMSNorm and SwiGLU blocks
Goal: Read a modern transformer block implementation and name each change from the original design and its purpose.
A modern transformer block keeps the GPT-2 skeleton (attention, then a feed-forward network, each added to a residual stream) and swaps three parts: norm before the sublayer, RMSNorm instead of LayerNorm, and a gated SwiGLU feed-forward. The Llama, Qwen, Mistral and Gemma families share this layout, so reading one block teaches you most of them.
Check yourself · 1 of 3
In a pre-norm block, where does the normalisation sit?
Why it matters
You will meet these names in config files, model cards and papers. If you can read a block, you can tell what a repository really implements, and spot a port that quietly got one of them wrong.
The three changes
- Pre-norm. The original transformer normalised after adding the residual. Modern blocks normalise the input to each sublayer and leave the residual path untouched:
x = x + attn(norm(x)). The clean residual path lets gradients flow through dozens of layers, so deep models train without careful warm-up tricks. - RMSNorm. LayerNorm subtracts the mean and divides by the standard deviation, then scales and shifts. RMSNorm only divides by the root mean square and scales: no mean, no bias. It is cheaper and works as well in practice.
- SwiGLU. The old feed-forward was
down(gelu(up(x))). SwiGLU adds a gate:down(silu(gate(x)) * up(x)). That is three matrices instead of two, so the hidden size is reduced (often to about two thirds of 4x) to keep parameters similar.
class RMSNorm(nn.Module):
def __init__(self, d, eps=1e-5):
super().__init__()
self.w = nn.Parameter(torch.ones(d))
self.eps = eps
def forward(self, x):
return self.w * x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
class Block(nn.Module):
def forward(self, x):
x = x + self.attn(self.norm1(x))
h = self.norm2(x)
return x + self.down(F.silu(self.gate(h)) * self.up(h))
Two smaller habits travel with them: no bias terms in the linear layers, and often no dropout.
Before and after
Weak: a port that writes x = self.norm1(x + self.attn(x)). That is post-norm. It loads the weights without error, then produces poor text, because the weights were trained with the norm in a different place.
Better: compare the port to the reference line by line and run a logits check on one prompt against the original implementation.
What goes wrong
- Mixing the order of gate and up projections when loading weights, which shapes match but values do not.
- Using LayerNorm epsilon values from a different model; small differences shift outputs in half precision.
- Counting parameters with two feed-forward matrices instead of three.
Examples
Spot the post-norm
x = self.norm1(x + self.attn(x))
x = self.norm2(x + self.mlp(x))
Norm wraps the sum, so this is post-norm. A Llama-style block is x = x + self.attn(self.norm1(x)). Weights trained for one do not work in the other.
Feed-forward parameter count
Hidden size 4096, feed-forward size 14336. SwiGLU has gate, up and down: 3 * 4096 * 14336 = 176,160,768 parameters per layer. A two-matrix design at the same size would have 117,440,512, so the extra matrix is a real cost, not a rounding detail.
Key points
- Pre-norm normalises the input of each sublayer and keeps the residual path clean, which helps deep models train.
- RMSNorm drops mean subtraction and bias, making it cheaper than LayerNorm with similar quality.
- SwiGLU gates the feed-forward with silu(gate(x)) * up(x), using three matrices instead of two.
- Because SwiGLU has three matrices, the hidden size is shrunk to keep parameter count near the older design.
- A port with matching tensor shapes can still be wrong; compare logits against the reference on one prompt.
Common mistakes
- Calling any block with a residual connection 'modern' without checking where the norm sits.
- Assuming RMSNorm has a bias or mean subtraction because LayerNorm does.
- Counting two feed-forward matrices for a SwiGLU model and getting parameters wrong by a third.
- Trusting a ported model because tensor shapes load, without a logits comparison.
Check yourself · 2 of 3
What does RMSNorm remove compared with LayerNorm?
Check yourself · 3 of 3
A SwiGLU feed-forward has hidden size 4096 and intermediate size 11008. How many parameters does one layer's feed-forward have (no biases)?
Practice items, not counted towards your mastery.
Start here
- The Big LLM Architecture Comparison (Sebastian Raschka) (opens in a new tab)ArticleFree
Side-by-side walkthrough of recent open-weight designs: attention variants, MoE layouts, norm placement.
- RoFormer: Enhanced Transformer with Rotary Position Embedding (opens in a new tab)PaperFree
The original rotary embedding paper: how rotating queries and keys encodes relative position.
- YaRN: Efficient Context Window Extension of Large Language Models (opens in a new tab)PaperFree
A scaling method that stretches rotary models to longer contexts with little extra fine-tuning.
More for this topic
- Llama model documentation (Hugging Face Transformers) (opens in a new tab)DocsFree
Documents every config.json field of a Llama-style model, the format most open models copy.