4.1.9 · D2Transformer Architecture

Visual walkthrough — Residual connections and layer norm placement

2,352 words11 min readBack to topic

Step 1 — What a layer even is: a box that changes a list of numbers

WHAT. A Transformer processes each token as a vector — an ordered list of numbers (for real models is 512, 768, ...). Call that list . A "sublayer" is just a box that eats and produces another list of the same length, . The box is either multi-head attention or a feed-forward network — but for this whole page we only care that it is some function.

WHY. Before we can talk about "adding the input back" or "where LayerNorm goes", we need one clean mental object: a list of numbers going into a box and a list coming out. Everything else is arranging boxes.

PICTURE. Look at the figure: the blue arrow is the input list flowing into the orange box . Out comes , a new list. Nothing has been added yet — this is a plain transformation.

Figure — Residual connections and layer norm placement

Step 2 — The residual: don't replace the input, add to it

WHAT. Instead of throwing away and keeping only , we do We add the two lists number-by-number. The output is "the input, plus a correction wanted to make".

WHY. Two reasons, and both are drawable. (1) If the box learns nothing useful at the start, and — the layer harmlessly passes information through instead of destroying it. (2) It creates a straight path from to that skips the box entirely. Hold that straight path in mind; it is the whole point of Step 4.

PICTURE. The green arrow is the new character: it carries around the box and drops it onto the sum at the circle marked . The box's output (orange) also flows into that circle. The straight green line is the skip connection.

Figure — Residual connections and layer norm placement

Step 3 — Why "add" gives a free +1 backward (building the derivative from scratch)

WHAT. Training asks a backward question: if I nudge the input by a tiny amount, how much does the output move? That ratio "output-wiggle per input-wiggle" is the derivative, written . For lists it is a grid of such ratios called the Jacobian — entry says how much output-number moves when input-number wiggles.

Differentiate term by term: Here is the identity matrix — the Jacobian of "copy unchanged" is a grid with s on the diagonal and s off it, because nudging input-number moves output-number by exactly the same amount and touches no other.

WHY the derivative and not something simpler? Because "how much the error at the output blames the input" is a rate of change, and rate of change is precisely what a derivative measures. No other tool answers "wiggle in → wiggle out". We need separated from because that lone is the guarantee.

PICTURE. The green skip path contributes the flat (a diagonal of s, drawn as a highlighted diagonal). The orange box path contributes , which can be tiny or huge. During backprop the gradient multiplies by this sum, so even if the box's part collapses to zero, the keeps a full-strength signal flowing. That is the highway.

Figure — Residual connections and layer norm placement

Step 4 — LayerNorm: the box that flattens a list to zero-mean, unit-spread

WHAT. LayerNorm takes a list and re-centres and re-scales it so its numbers have average and spread , then lets the model re-stretch and re-shift with learned knobs :

  • — the mean, the balance point of the list.
  • — the variance, average squared distance from the balance point (how spread out the numbers are).
  • — the standard deviation, a plain "typical distance"; is a tiny safety number so we never divide by zero.
  • means multiply lists element-by-element; are learned per-feature dials.

WHY subtract-then-divide? Subtracting slides the whole list so its centre sits at ; dividing by squeezes or stretches it to a standard width of . A neural box behaves best when its inputs are neither drifting off to huge numbers nor squashed near zero — LayerNorm forces that good regime per token, independent of sequence length (why not Batch Norm: batch stats depend on padding; see training stability).

PICTURE. Left: a wild input list, numbers scattered far from centre. After subtracting the pile slides onto ; after dividing by it is squeezed to width . The output (green) is tidy.

Figure — Residual connections and layer norm placement

Step 5 — Post-LN: normalize after the add (the LayerNorm sits ON the highway)

WHAT. The original 2017 Transformer wraps the whole residual sum inside LayerNorm: So the sequence is: box → add the skip → then normalise everything.

WHY look at this first? Because it is the natural first guess ("normalise the layer's output") and it exposes the danger. Differentiate it: The clean from Step 3 is now multiplied on the left by . That means our precious never travels alone — it is first passed through the LayerNorm Jacobian, whose entries shrink with the spread of . At initialization the box output is random-scaled, varies, so the effective highway strength varies across depth.

PICTURE. The LayerNorm box (blue) straddles the merge point — every backward arrow, including the green skip's arrow, must pass through it. The highway has a tollbooth on it. Notice the deeper layers get an uneven gradient size (drawn as thicker/thinner arrows).

Figure — Residual connections and layer norm placement

Step 6 — Pre-LN: normalize before the box (the highway stays clean)

WHAT. Move LayerNorm inside the branch, before the box, and add the raw back: Sequence: normalise a copy → box → add the original (not the normalised one) back.

WHY this fixes it. Differentiate: The now stands alone and un-multiplied — nothing sits on the green highway. LayerNorm's Jacobian is trapped inside the branch term, so it only affects the correction the box adds, never the guaranteed identity flow. The gradient reaches every depth at full strength; norms stay uniform across layers.

PICTURE. The LayerNorm box has moved off the highway and onto the orange branch only. The green skip line runs from straight to the sum, tollbooth-free. Backward arrows on the green line are all equally thick at every depth.

Figure — Residual connections and layer norm placement

Step 7 — Edge & degenerate cases (never leave the reader stranded)

WHAT / WHY / PICTURE for each corner:

  • The box learns nothing, . Then . Post-LN gives (still scaled/​bottlenecked). Pre-LN gives — a perfect identity pass. Pre-LN degrades gracefully to "do nothing", Post-LN does not.
  • Spread collapses, (all features equal). Division would blow up; the under the root keeps finite. Both architectures survive only because of — this is what that tiny constant is for.
  • Huge box output at init (variance of large). Post-LN: has wild , swings, deep-layer gradients uneven → divergence without warmup. Pre-LN: raw path is untouched, only the branch is affected → stays stable. See layer scaling & init.
  • Very deep stack ( layers). Post-LN multiplies Jacobians along the highway → product can shrink/​grow exponentially. Pre-LN's highway is a sum of 's → the raw signal survives to the top no matter how deep.

PICTURE. Two mini-stacks side by side: Post-LN's backward signal (red) fades with depth; Pre-LN's (green) stays constant. The tollbooth-per-layer versus clean-lane contrast is the message.

Figure — Residual connections and layer norm placement

The one-picture summary

Figure — Residual connections and layer norm placement

Everything above compressed: two blocks side by side. Post-LN — box, add skip, then LayerNorm on the merged highway; backward gradient = , the is trapped behind a tollbooth. Pre-LN — LayerNorm on the branch only, raw skip straight through; backward gradient = , a lone clean . The green highway is unbroken only on the right.

Recall Feynman retelling — say it back in plain words

Every layer is a box that rewrites a list of numbers. Instead of trusting the box, we keep the original list and add the box's change to it — that's the residual. When we ask "how much does the output move if the input wiggles", the answer splits into two pieces: a guaranteed "" from the kept-original copy, plus whatever the box contributes. That guaranteed is a highway the training signal can always drive down, so deep networks stop losing their gradient.

LayerNorm is a separate box that tidies a list to average-zero, spread-one so the next box gets clean input. The only question is where you park it. Park it after the add (Post-LN) and it sits on the highway — now the guaranteed has to squeeze through LayerNorm first, its strength wobbles with the data, deep layers get uneven gradients, and you must warm the learning rate up slowly or training explodes. Park it before the box (Pre-LN) and it only touches the branch, never the highway; the guaranteed rides through untouched at every depth. Same two ingredients, different parking spot, completely different training life.

Corner cases: a tiny stops division-by-zero if the list is flat; if the box does nothing, Pre-LN passes the input perfectly while Post-LN still scales it; stack it very deep and Post-LN multiplies many tollbooths together (bad) while Pre-LN just keeps adding clean 's (good).

Recall

In , what is the Jacobian and why does it help? ::: ; the constant never vanishes, so a full-strength gradient always flows backward. Where does Post-LN place LayerNorm relative to the add? ::: After the residual add — LayerNorm wraps , sitting on the highway. Where does Pre-LN place LayerNorm? ::: Before the sublayer, inside the branch; the raw is added back un-normalized. Why does Pre-LN give more uniform gradients across depth? ::: Its backward Jacobian is — the stands alone, so the residual highway is never bottlenecked by LayerNorm. What is the tiny inside LayerNorm for? ::: To keep finite when the variance , preventing division by zero. Does Pre-LN eliminate learning-rate warmup? ::: No — it is more forgiving of warmup, but large models still use warmup.


Prerequisites & neighbours: 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