4.1.9 · D3Transformer Architecture

Worked examples — Residual connections and layer norm placement

3,210 words15 min readBack to topic

This page lives under Residual connections and layer norm placement. We already built the ideas there. Here we compute — we push real numbers through Post-LN and Pre-LN blocks so you can see what each equation does. If you have never divided by a standard deviation before, don't worry: every symbol gets earned here again, from zero.


What the symbols mean (fast rebuild from zero)

Before any example, let us re-anchor every piece of notation to a picture, so no line surprises you.

The whole game of this note: where do we put the LayerNorm box relative to that fork? The two placements are drawn side-by-side below — refer back to it as you work each example.

  • Post-LN: fork → add → then normalize.
  • Pre-LN: normalize inside the branch → add.
Figure — Residual connections and layer norm placement

What to see in figure s01. Two vertical block diagrams share the picture. On the left (Post-LN, magenta title), information enters at x_in (bottom), splits at the fork: one copy runs through the violet Sublayer F box, and the other — the orange highway — runs to the right and up. They meet at the magenta add box (). Crucially the orange highway then feeds into the navy LayerNorm box before reaching x_out at the top — trace it: the highway is swallowed by the wall. On the right (Pre-LN, violet title), the order flips: x_in first enters the navy LayerNorm box, then the violet Sublayer F, and only the branch is normalized — the orange highway shoots straight up the right edge, past every box, untouched, into the magenta add box. Fix these two orange paths in your memory: the left one hits a wall, the right one never does. That single visual difference explains everything in Ex 5.


The scenario matrix

Every worked example below is tagged with the cell it covers. Together they touch every cell.

# Case class What could go wrong / be special Example
A Ordinary Post-LN forward normalize a normal residual sum Ex 1
B Ordinary Pre-LN forward LN inside branch, highway untouched Ex 2
C Zero / degenerate input () all features equal → divide by zero Ex 3
D Large-scale sublayer output high-variance at init Ex 4
E Gradient through the highway the "" Jacobian term, both variants Ex 5
F Depth accumulation / limiting behaviour residual stream growing over layers Ex 6
G Real-world word problem choosing an architecture for a task Ex 7
H Exam-style twist non-trivial, sign traps Ex 8









Recall Self-check (reveal after guessing)

Post-LN forces output mean/variance to ::: and (statistics are pinned every layer). Pre-LN leaves the residual stream ::: un-normalized, so it accumulates (variance grows like ). The role of in is ::: to prevent division by zero when all features are equal (). The gradient term that Pre-LN protects but Post-LN does not is ::: the "" identity Jacobian of the residual add. Over 10 layers, per-layer derivative vs gives ::: (survives) vs (vanishes).

Related deep dives & prerequisites: 4.1.01-Self-attention-mechanism, 4.1.08-Multi-head-attention, 4.3.01-Positional-encoding, 4.1.12-Encoder-decoder-attention, 5.1.03-Gradient-flow-in-deep-networks, 6.2.05-Layer-scaling-and-initialization, 4.2.02-Training-stability-and-convergence.