Order the operations inside a single pre-norm transformer block forward pass
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.