Order the operations inside a single pre-norm transformer block forward pass
- 1Add the FFN output back onto the residual stream
- 2Add the attention output back onto the residual stream
- 3Run the position-wise feed-forward network on the normalized vector
- 4Apply LayerNorm (or RMSNorm) again to the updated residual stream
- 5Run multi-head attention on the normalized vector
- 6Apply LayerNorm (or RMSNorm) to the residual stream x
Pre-norm block: norm, attention, add residual, norm again, FFN, add residual. The pattern (norm -> sublayer -> add) repeats twice per block.
Imagine a sandwich assembly line where bread comes through twice. The first time it arrives, a quality inspector measures it and rescales it (that's the norm), then a chef adds the filling (attention), then the sandwich is reassembled onto the original bread (the residual add). Then the whole thing goes through a second station: another inspector rescales the new sandwich, another chef adds a different filling (the feed-forward network), and another reassembly step. Pre-norm means the inspector comes BEFORE the chef both times. That ordering matters enormously: it lets you stack the assembly line a hundred stations deep without the bread getting soggy. The original 2017 paper put the inspector after the chef, and that version stops working past about 24 stations.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: list the six steps in order, contrast with post-norm, explain why the residual is an identity path, and mention the final norm before the LM head.
class PreNormBlock(nn.Module):
def __init__(self, d_model):
self.norm1 = RMSNorm(d_model)
self.attn = GroupedQueryAttention(d_model)
self.norm2 = RMSNorm(d_model)
self.ffn = SwiGLU_FFN(d_model)
def forward(self, x):
# Step 1-3: attention sublayer
x = x + self.attn(self.norm1(x))
# Step 4-6: FFN sublayer
x = x + self.ffn(self.norm2(x))
return xReal products, models, and research that use this idea.
- Llama 4 Maverick, Llama 3.1, Mistral Large 3, and Qwen 3.5 all use the pre-norm order exactly as listed.
- GPT-2 (2019) was the first widely used pre-norm transformer; every frontier model since has followed.
- The original BERT (2018) used post-norm; the practical training tricks (warmup, careful learning rate) were necessary because of that choice.
- Open source reference implementations like nanoGPT and Hugging Face's `LlamaDecoderLayer` show the six step pre-norm structure explicitly.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat changes in the six step order if you switch to post-norm?
QWhy is there an extra norm at the very end of a pre-norm stack but not a post-norm stack?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Putting the norm after the residual add (that's post-norm, the original 2017 design). Or forgetting that the norm runs twice per block: once before attention, once before FFN.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.