# What Is Layer Norm? Pre-Norm vs Post-Norm, Explained

> What layer norm does, why pre-norm and post-norm put it in different places, and what that changes in training, for your AI interview.

**In short:** Layer norm rescales one token's numbers to a steady average and spread. Post-norm puts it after each block adds its input back, while pre-norm puts it before the block. Xiong and colleagues found that pre-norm's gradients are well-behaved at the start.

AI and LLM interview question Q052, Transformer architecture. Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-layer-norm-pre-norm-vs-post-norm

## Say it in 30 seconds

Layer norm rescales one token's numbers so their average is zero and their spread is one, which keeps the numbers steady from layer to layer. The question is where it sits. The original Transformer put it after each block adds its input back, which is post-norm. Pre-norm puts it before the block. Xiong and colleagues found that pre-norm's gradients are well-behaved at the start, and that it reached comparable results without warm-up in their tests. Pre-norm also needs one final norm before the prediction, and I'd test quality on the real task before switching.

## Say it in 2 minutes

A transformer passes every token through a stack of blocks, and each block adds new numbers to it. Those numbers can drift: some grow very large, others shrink towards zero. Layer norm fixes the scale. For one token, it takes that token's own numbers, subtracts their average, divides by their spread, and then applies two learned values that let the model choose its scale again. It looks only at that one token, never at other examples in the batch. Batch norm does the opposite and averages across the batch.

The hard question is where the norm goes. The 2017 Transformer paper wrapped each sub-layer in a residual connection and then applied layer normalisation. That is post-norm. Pre-norm moves the norm inside the branch, so the skip path carries the input untouched. Xiong and colleagues showed that post-norm's gradients near the output are large at the start, which is why it usually needs a warm-up stage. In their tests, pre-norm reached comparable results without that stage.

The cost is one extra step. A pre-norm stack's final output is not normalised, so the model adds one final layer norm right before the prediction. Meta's Llama paper also uses pre-norm, normalising the input of each sub-layer to improve training stability. It uses RMSNorm, a variant of layer norm.

In an interview, I'd name the two placements, explain why warm-up matters, and say that the choice depends on testing against my own task.

## What is layer norm, in plain words?

A transformer passes every token through a stack of blocks, and each block adds new numbers to it. Those numbers can drift: some grow very large, others shrink towards zero. Layer norm rescales one token's numbers so they have an average of 0 and a spread of 1. It then applies a learned gain (a scale) and bias (an offset), so the model can still pick its own scale.

Think of a loan officer comparing applicants. One sends income as a monthly figure, another as a yearly one, a third as a weekly average. She restates each on the same scale before she compares them. Layer norm does that for each token before the next step. The AI version: a claims-triage model at an insurer gets one token whose numbers come out ten times bigger than its neighbour's. Layer norm rescales each token on its own, so the next layer sees them on one scale.

It works on one token's own numbers. It never looks at other examples in the batch, so it behaves the same whether a batch holds one example or a thousand.

- **Layer norm:** Rescales one token's numbers to a steady average and spread, then applies a learned gain and bias.
- **Residual connection:** The input copied around a block and added back to the block's output.
- **Sub-layer:** One block inside a transformer layer, such as attention or the feed-forward part.
- **Gradient:** The signal that tells each weight which way to move during training.
- **Warm-up:** Starting training with a small learning rate and raising it slowly.

## How does layer norm work, step by step?

1. **Average the token's numbers:** Add them up and divide by how many there are.
2. **Measure the spread:** Work out how far the numbers typically sit from that average.
3. **Rescale each number:** Subtract the average, then divide by the spread. A tiny extra amount stops it dividing by zero.
4. **Apply a learned gain and bias:** Two values per feature, learned during training, let the model set its own scale again.

Here it is in plain Python. We run it on two copies of one token's numbers, the second ten times bigger. After the norm, they match.

```python
# One token's hidden numbers from a claims-triage model (made-up), before and after layer norm.
import math
import statistics

def layer_norm(x, eps=1e-5):
    mean = statistics.fmean(x)
    var = statistics.pvariance(x)
    return [(v - mean) / math.sqrt(var + eps) for v in x]

calm = [4.0, 8.0, 12.0, 16.0]
spiky = [40.0, 80.0, 120.0, 160.0]  # same pattern, ten times the scale
for name, vec in [("calm", calm), ("spiky", spiky)]:
    out = layer_norm(vec)
    print(name, [round(v, 3) for v in out], round(statistics.fmean(out), 3), round(statistics.pstdev(out), 3))
```

Output:

```text
calm [-1.342, -0.447, 0.447, 1.342] 0.0 1.0
spiky [-1.342, -0.447, 0.447, 1.342] 0.0 1.0
```

- **Batch norm: Across examples.** It averages each feature over a mini-batch of training examples, so its result depends on the batch size.
- **Layer norm: Across one token's features.** It averages over one token's own numbers, one training case at a time, so batch size makes no difference.

> **Tip:** Say 'across one token's features', never 'across the batch'. Across the batch describes batch norm.

## Where does the norm go: pre-norm or post-norm?

Post-norm is the 2017 design. Each sub-layer's output is added to its input, and the sum is then normalised. Pre-norm normalises the input first, runs the sub-layer on that copy, and adds the untouched input back. The sub-layer is either attention or the feed-forward block.

> "We employ a residual connection [11] around each of the two sub-layers, followed by layer normalization [1]." (Vaswani and colleagues, the 2017 Transformer paper)
>
> "for the original-designed Post-LN Transformer, which places the layer normalization between the residual blocks, the expected gradients of the parameters near the output layer are large" (Xiong and colleagues, on post-norm)
>
> "if the layer normalization is put inside the residual blocks (recently proposed as Pre-LN Transformer), the gradients are well-behaved at initialization" (Xiong and colleagues, on pre-norm)

*Diagram, Where the layer norm sits: Both designs take the same token vector and add the sub-layer's output back to it. Post-norm normalises after that add, so the sum passes through a norm. Pre-norm normalises before the sub-layer and leaves the skip path untouched.*
1. Post-norm sends the vector into the sub-layer first.
2. The sub-layer's output is added to its input.
3. The sum is normalised before it moves on.
4. Early on, the gradients near the output are large, so training needs a warm-up.
5. Pre-norm normalises the input before the branch.
6. The normalised copy goes into the sub-layer.
7. The sub-layer's output is added to the untouched input.
8. At the start, the gradients stay well behaved.

Xiong and colleagues, from ICML 2020, showed that the placement matters at the start. Post-norm's gradients near the output are large, so training usually needs a learning rate warm-up. Pre-norm reached comparable results without that stage in their tests.

The cost is one extra step. A pre-norm stack's final output is not normalised, so the model adds one final layer norm right before the prediction. Meta's Llama paper also uses pre-norm, normalising the input of each sub-layer to improve training stability. It uses RMSNorm, a variant of layer norm.

### How would you fix a deep model that diverges at the start?

A team trains a deep post-norm model. In the first steps the loss jumps and then turns to NaN, which means not a number, so training is dead. They chose a high learning rate to save time. Here is how I'd work through it.

1. **Check the norm placement:** Confirm the model is post-norm. Xiong and colleagues tie that design's early instability to large gradients near the output.
2. **Add a warm-up:** Start with a small learning rate and raise it over the first stretch of steps. It works, but it means a slower start and more settings to tune.
3. **Or move the norm into the block:** Pre-norm reached comparable results without warm-up in their tests. The cost is one extra final norm, and quality must be measured again.
4. **Test on a small run first:** A short run shows whether the loss stays finite before the team spends days of compute.

> **Tip:** A good answer names both fixes, what each costs, and a small test before the full run.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Layer norm rescales one token's numbers to a steady average and spread.
- [ ] It works across one token's features, never across the batch.
- [ ] Post-norm puts it after the add. Pre-norm puts it before the sub-layer.
- [ ] Post-norm usually needs a warm-up. Pre-norm reached comparable results without one in Xiong's tests.
- [ ] Pre-norm needs one final norm before the prediction.
- [ ] Test on your own task before switching designs.

**Common traps**

- Trap: "Layer norm works across the batch, like batch norm." Better: It works across one token's features. Batch norm is the one that averages across the batch.
- Trap: "Pre-norm is always better, so use it everywhere." Better: It trained more smoothly at the start in one paper's tests. Check quality on your own task before switching.
- Trap: "The norm makes the model smaller." Better: It rescales the numbers. It removes no weights and shrinks nothing. It keeps the numbers steady, so the next layer sees them on one scale.

## Can you spot the mistake?

**Spot the mistake:** A teammate's design note for a claims-triage model has one wrong line. Which one?

A. Layer norm rescales one token's numbers to an average near zero and a spread near one.
B. Layer norm averages each feature across the batch, so a batch of one breaks it.
C. Pre-norm normalises the input, then adds the untouched input back after the sub-layer.
D. Post-norm adds the sub-layer's output to its input, then normalises the sum.

**Answer:** B. Layer norm works across one token's features, not across the batch. Averaging across the batch is batch norm, and that is why batch size changes batch norm's result but not layer norm's. The other three lines describe the two placements correctly.

**Quick check:** A deep post-norm model diverges in its first steps. Which change is most likely to help, with the design left as it is?

A. Remove the residual connections
B. Add a learning rate warm-up
C. Double the number of attention heads
D. Raise the learning rate so training finishes sooner

**Answer:** B. Warm-up keeps the first steps small. Xiong and colleagues tie post-norm's early instability to large gradients, and warm-up is what they found helpful. Removing residuals changes the design, and a higher learning rate makes those first steps bigger, which is the problem.

## Can you answer these from memory?

**Flash cards**

- **What is layer norm?** Rescales one token's numbers to an average of 0 and a spread of 1, then applies a learned gain and bias.
- **Layer norm vs batch norm?** Layer norm: across one token's features. Batch norm: across the examples in a batch.
- **Post-norm, in order?** Add the sub-layer's output to its input, then normalise the sum.
- **Pre-norm, in order?** Normalise the input, run the sub-layer, then add the untouched input back.
- **Pre-norm's trade-off?** The final output is not normalised, so the model needs one more layer norm before the prediction.
- **Why does post-norm need warm-up?** Its gradients near the output are large at the start, so the first steps must be small.

## Likely follow-up questions

- Q053 What do residual connections do? https://designervenkat.online/interview/ai/what-do-residual-connections-do
- Q054 What does the feed-forward block do? https://designervenkat.online/interview/ai/what-does-the-feed-forward-block-do
- Q041 What is a transformer? https://designervenkat.online/interview/ai/what-is-a-transformer
- Q426 Vanishing and exploding gradients? (full answer coming)
- Q427 What is batch normalisation? (full answer coming)

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
