AI / ML Theory
Chapter 6 · 20 min

6.9 — Residual connections (skip connections)

Goal: Define a residual block y = x + f(x); derive why it eliminates the multiplicative gradient erosion of deep stacks; understand identity initialisation; place residuals inside the Transformer as the glue that makes 96-layer stacks trainable.

1. Why this session exists

We have all the parts of one Transformer block — attention (6.2–6.7) and FFN (6.8) — but we cannot yet stack 32, 96, or 128 of them. A plain composition fL∘⋯∘f1f_L \circ \dots \circ f_1 of LL non-linear sub-layers is, in practice, untrainable for large LL. The reason is the multiplicative chain rule: the gradient at layer ℓ\ell acquires a factor for every layer above it, and the product of LL such factors typically collapses to 00 or explodes to ∞\infty as LL grows. We met this in 2.9.

The fix is structural, not numerical. Replace each sub-layer's pure composition y=f(x)y = f(x) with the residual form

y=x+f(x),y = x + f(x),

so that the layer adds a correction to its input rather than overwriting it. The identity term xx creates a direct path along which the gradient flows un-attenuated, no matter how deep the stack. Introduced by He, Zhang, Ren & Sun (2015) for ResNet — the architectural innovation that unlocked ≥100\geq 100-layer vision networks — residual connections are imported wholesale into the Transformer as the only reason 96-layer GPT-3 and 80-layer LLaMA-70B can be trained at all.

This session derives the gradient-flow argument, defines identity initialisation, and shows where residuals live inside one Transformer block. We use them everywhere in the rest of the course.


2. The plain-stack problem

Let fℓ:Rd→Rdf_\ell: \mathbb{R}^d \to \mathbb{R}^d be the operation at layer ℓ\ell (one Transformer sub-layer in our case). A plain stack computes

xℓ=fℓ(xℓ−1),ℓ=1,…,L,x_\ell = f_\ell(x_{\ell-1}), \qquad \ell = 1, \dots, L,

and the final loss is L(xL)\mathcal{L}(x_L). By the chain rule (in the numerator convention of 0.2),

∂L∂x0=∂L∂xL⋅∂xL∂xL−1⋅∂xL−1∂xL−2⋯∂x1∂x0=∂L∂xL⋅∏ℓ=L1Jℓ,\frac{\partial \mathcal{L}}{\partial x_0} = \frac{\partial \mathcal{L}}{\partial x_L} \cdot \frac{\partial x_L}{\partial x_{L-1}} \cdot \frac{\partial x_{L-1}}{\partial x_{L-2}} \cdots \frac{\partial x_1}{\partial x_0} = \frac{\partial \mathcal{L}}{\partial x_L} \cdot \prod_{\ell = L}^{1} J_\ell,

where Jℓ=∂fℓ/∂xℓ−1∈Rd×dJ_\ell = \partial f_\ell / \partial x_{\ell-1} \in \mathbb{R}^{d \times d} is the Jacobian of the ℓ\ell-th sub-layer. The product is taken in the natural order — outer first, then progressively deeper — which is what numerator-layout chain rule prescribes.

2.1 The product-of-Jacobians collapse

Let ∥⋅∥\|\cdot\| denote the operator (spectral) norm. Then

∥∏ℓ=L1Jℓ∥  ≤  ∏ℓ=L1∥Jℓ∥,\Big\| \prod_{\ell = L}^{1} J_\ell \Big\| \;\leq\; \prod_{\ell = L}^{1} \|J_\ell\|,

and the right-hand side is a product of LL non-negative scalars. If the typical spectral norm satisfies ∥Jℓ∥≈c\|J_\ell\| \approx c, then ∥∏Jℓ∥≲cL\|\prod J_\ell\| \lesssim c^L, which

  • vanishes geometrically if c<1c < 1 (every layer scales the gradient down a touch and after LL layers nothing survives), or
  • explodes geometrically if c>1c > 1 (the gradient blows up and training diverges).

The boundary c=1c = 1 is unstable: any random initialisation puts cc either side. Concretely, with a ReLU/GELU sub-layer and standard Xavier/He initialisation, one finds ∥Jℓ∥<1\|J_\ell\| < 1 for most ℓ\ell (the non-linearity zeros out roughly half its inputs, halving the effective rank), and the gradient vanishes as LL grows. This is the precise statement of "deep networks don't train".


3. The residual fix

A residual block with residual function fℓf_\ell computes

 xℓ=xℓ−1+fℓ(xℓ−1). \boxed{\ x_\ell = x_{\ell-1} + f_\ell(x_{\ell-1}). \ }

The mapping x↦x+f(x)x \mapsto x + f(x) is a correction: fℓf_\ell does not need to reproduce xℓ−1x_{\ell-1} in its output — the identity path delivers it for free. fℓf_\ell only learns the deviation from identity that the layer should add.

Two consequences follow immediately.

3.1 The Jacobian is an additive identity

By linearity of differentiation,

Jℓ=∂xℓ∂xℓ−1=I+∂fℓ∂xℓ−1=I+Jℓ(f),J_\ell = \frac{\partial x_\ell}{\partial x_{\ell-1}} = I + \frac{\partial f_\ell}{\partial x_{\ell-1}} = I + J_\ell^{(f)},

where Jℓ(f)J_\ell^{(f)} is the Jacobian of the residual function alone. The Jacobian of a residual block is an identity plus a perturbation.

3.2 Gradient flow is unimpeded

Compose LL residual blocks. The full Jacobian becomes

∏ℓ=L1Jℓ=∏ℓ=L1(I+Jℓ(f)).\prod_{\ell = L}^{1} J_\ell = \prod_{\ell = L}^{1} \big(I + J_\ell^{(f)}\big).

Expand this product. Each term in the expansion is an ordered subset S⊆{1,…,L}S \subseteq \{1, \dots, L\} of layers: pick Jℓ(f)J_\ell^{(f)} for ℓ∈S\ell \in S and II for ℓ∉S\ell \notin S. The number of terms is 2L2^L, and they collect by cardinality ∣S∣=k|S| = k:

∏ℓ=L1(I+Jℓ(f))=I  +  ∑ℓ=1LJℓ(f)  +  ∑ℓ1<ℓ2Jℓ2(f)Jℓ1(f)  +  ⋯\prod_{\ell = L}^{1} \big(I + J_\ell^{(f)}\big) = I \;+\; \sum_{\ell = 1}^{L} J_\ell^{(f)} \;+\; \sum_{\ell_1 < \ell_2} J_{\ell_2}^{(f)} J_{\ell_1}^{(f)} \;+\; \cdots

(ordered correctly; lower indices innermost). The leading II is the identity path: gradients flow from output to input completely un-attenuated through the chain of skip connections. The first-order sum is a "linear combination of single-layer contributions" — also un-attenuated. Higher-order terms add products of increasing length, but they no longer dominate because the identity term is always present.

The numerical upshot: even if every ∥Jℓ(f)∥<1\|J_\ell^{(f)}\| < 1 (so that pure compositions would collapse), the gradient ∂L/∂x0\partial \mathcal{L} / \partial x_0 stays Θ(1)\Theta(1) because the leading II keeps a direct line from xLx_L to x0x_0.

 ∂L∂x0=∂L∂xL⋅(I+(corrections)). \boxed{\ \frac{\partial \mathcal{L}}{\partial x_0} = \frac{\partial \mathcal{L}}{\partial x_L} \cdot \Big( I + \text{(corrections)} \Big). \ }

This is the entire reason ResNet-152 and GPT-3-175B exist. Identity paths break the multiplicative chain rule.


4. Identity initialisation

There is a second benefit, complementary to gradient flow. If we initialise each residual function so that fℓ≈0f_\ell \approx 0 at training start (small weights), then

xℓ=xℓ−1+fℓ(xℓ−1)≈xℓ−1,x_\ell = x_{\ell-1} + f_\ell(x_{\ell-1}) \approx x_{\ell-1},

i.e. the network at initialisation behaves like a chain of identity maps: xL≈x0x_L \approx x_0. The model starts as a shallow effective network and the optimiser is free to introduce non-trivial fℓf_\ell one layer at a time, in whichever order helps the loss. This is sometimes called identity initialisation or "the residual network at t=0t = 0 is a shallow network".

The practical implementation differs by author but is uniformly cheap:

  • ResNet-original (He, 2015)[1]. Initialise the last BatchNorm γ\gamma in each block to 00 — so the block output is exactly xx at t=0t = 0. (Goyal et al., 2017; "Bag of Tricks" Tweaks.)
  • Fixup (Zhang et al., 2019)[3]. Initialise the last convolutional layer of each block to zero; scale the others by a factor depending on depth. Trains 100-layer ResNets without any normalisation.
  • GPT / Pre-LN Transformer. Standard initialisation (small Gaussian) combined with pre-norm (6.10) already produces near-identity behaviour at start; no special trick is needed.

The lesson is general: let the deep network start as a shallow one and grow into a deep one. Residual connections + small initialisation get this for free.


5. Residuals inside a Transformer block

A Transformer block has two sub-layers — attention and FFN — and both are wrapped in residual connections. The standard "post-LN" original AIAYN block (we will revisit pre-LN in 6.10) computes

x′=LayerNorm ⁣(x+MHA(x)),y=LayerNorm ⁣(x′+FFN(x′)).\begin{aligned} x' &= \text{LayerNorm}\!\big(x + \text{MHA}(x)\big), \\ y &= \text{LayerNorm}\!\big(x' + \text{FFN}(x')\big). \end{aligned}

Two residual connections per block. Across L=96L = 96 blocks (GPT-3 scale), 192192 skip connections. The output is xL=x0+∑ℓ(MHA-correctionℓ+FFN-correctionℓ)x_L = x_0 + \sum_\ell \big(\text{MHA-correction}_\ell + \text{FFN-correction}_\ell\big) to first order — the residual stream view.

5.1 The residual stream as a shared bus

Reading the same equation from a different angle: every sub-layer reads the current state xℓ−1x_{\ell-1}, computes a correction fℓ(xℓ−1)f_\ell(x_{\ell-1}), and adds it back into the stream. There is a single dd-dimensional vector flowing from input embedding to output projection, and every layer is a read-modify-write on that vector.

This perspective — coined the residual stream by the Anthropic interpretability team — is the cornerstone of modern mechanistic interpretability (Chapter 11). It only makes sense because of the residual structure: the additive identity path is what gives the stream a single, persistent identity across layers.

stream:x0  ⟶  x0+f1  ⟶  x0+f1+f2  ⟶  ⋯  ⟶  x0+∑ℓfℓ.\text{stream:} \quad x_0 \;\longrightarrow\; x_0 + f_1 \;\longrightarrow\; x_0 + f_1 + f_2 \;\longrightarrow\; \cdots \;\longrightarrow\; x_0 + \sum_\ell f_\ell.

This is also why Transformer interpretability has been able to make so much progress: a layer that replaced the stream (a plain y=f(x)y = f(x) composition) would erase the linear addressability that lets us decompose model behaviour by layer and by head.


6. What residuals do not fix

Two cautions, common to all skip-connection arguments:

(a) Residuals do not prevent exploding activations. The forward pass xℓ=xℓ−1+fℓ(xℓ−1)x_\ell = x_{\ell-1} + f_\ell(x_{\ell-1}) accumulates the magnitudes of fℓf_\ell across ℓ\ell. Without normalisation, ∥xL∥\|x_L\| grows roughly as L⋅∥f∥\sqrt{L} \cdot \|f\| (random walk) or even linearly. This is why every Transformer pairs residuals with LayerNorm/RMSNorm (3.8): the norm rescales the stream back to unit-scale every block.

(b) Residuals require shape compatibility. y=x+f(x)y = x + f(x) needs f(x)f(x) to live in the same vector space as xx. In a Transformer this is automatic — every sub-layer maps Rd→Rd\mathbb{R}^d \to \mathbb{R}^d. In ResNet, a strided convolution that changes the spatial resolution must be paired with a 1×11 \times 1 projection on the skip path (the "projection shortcut"). We won't worry about this in language models; dd is held constant across all attention/FFN sub-layers.


7. The arithmetic of "identity + correction"

A small numerical illustration ties the gradient-flow argument to a concrete picture. Let d=1d = 1 for simplicity, set fℓ(x)=αxf_\ell(x) = \alpha x with α=−0.1\alpha = -0.1 at every layer, and stack L=50L = 50 layers.

Plain stack: xL=αL⋅x0=(−0.1)50≈10−50x_L = \alpha^L \cdot x_0 = (-0.1)^{50} \approx 10^{-50}. The forward signal has vanished. Symmetrically, ∂xL/∂x0=αL≈10−50\partial x_L / \partial x_0 = \alpha^L \approx 10^{-50}. Untrainable.

Residual stack: xℓ=xℓ−1+αxℓ−1=(1+α)xℓ−1x_\ell = x_{\ell-1} + \alpha x_{\ell-1} = (1 + \alpha) x_{\ell-1}, so xL=(1+α)Lx0=(0.9)50≈5×10−3x_L = (1 + \alpha)^L x_0 = (0.9)^{50} \approx 5 \times 10^{-3}. The signal is small but not catastrophic, and the Jacobian is (1+α)L≈5×10−3(1 + \alpha)^L \approx 5 \times 10^{-3}, eighteen orders of magnitude larger than the plain stack. With α\alpha near zero (small initialisation, §4) the residual stack is essentially the identity at t=0t = 0 and the optimiser can lift each αℓ\alpha_\ell off zero gradually.

This is the same story with d≫1d \gg 1: the spectral spread of (I+Jℓ(f))(I + J_\ell^{(f)}) around 1 is much more forgiving than the spectral spread of Jℓ(f)J_\ell^{(f)} alone around 0 or 1.


8. Where this lands

Residual connections are the single architectural component without which deep stacks of any kind — ResNet, Transformer, U-Net, modern diffusion models — would not train. Three things to keep:

y=x+f(x)⟹∂y∂x=I+∂f∂x.y = x + f(x) \quad\Longrightarrow\quad \frac{\partial y}{\partial x} = I + \frac{\partial f}{\partial x}.
  1. The identity term gives the gradient a direct path from output to input — multiplicative collapse is broken.
  2. Initialising f≈0f \approx 0 makes the deep network start as a shallow one; layers grow non-trivial as training proceeds.
  3. Combined with norms (3.8), residuals turn a Transformer into a residual stream — a shared dd-vector bus along which every block reads, transforms, and writes.

Next session examines where the LayerNorm sits relative to the residual — pre-norm vs post-norm — and why every modern Transformer puts the norm inside the residual rather than outside.

Self-test

(a) Write the Jacobian of one residual block y=x+f(x)y = x + f(x) in numerator layout. Then expand the Jacobian of two stacked residual blocks z=y+g(y)z = y + g(y) in terms of ∂f/∂x\partial f/\partial x and ∂g/∂y\partial g/\partial y. Identify the "identity path" term.

(b) Suppose every residual sub-layer has ∥Jℓ(f)∥≤0.2\|J_\ell^{(f)}\| \leq 0.2. Bound the spectral norm of the full LL-layer Jacobian ∏ℓ(I+Jℓ(f))\prod_\ell (I + J_\ell^{(f)}) above and below. Compare with the plain stack ∏ℓJℓ(f)\prod_\ell J_\ell^{(f)} for L=50L = 50.

(c) Argue that "identity initialisation" makes a 9696-layer Transformer behave like a shallow network at t=0t = 0. What does this imply about the rank of xL−x0x_L - x_0 when training starts?

(d) A Transformer block applies two residual additions per layer (one around attention, one around FFN). For a stack of LL blocks, how many identity paths run from input to output? Compare to a ResNet with LL residual blocks.

(e) Veit, Wilber & Belongie (2016) argue that a residual network behaves like an "ensemble of shallow networks". Translate this into the 2L2^L expansion of §3.2: how does each subset S⊆{1,…,L}S \subseteq \{1, \dots, L\} correspond to one ensemble member?


References

  1. [1]

    He, K., Zhang, X., Ren, S., & Sun, J. (2015). "Deep residual learning for image recognition" (ResNet). arXiv:1512.03385 — Identity skip connections; the architectural innovation imported wholesale into the Transformer (6.9).

    ↩
  2. [2]

    Veit, A., Wilber, M. J., & Belongie, S. (2016). "Residual networks behave like ensembles of relatively shallow networks". arXiv:1605.06431 — Implicit-ensemble view of residual networks (6.9 Aside).

    ↩
  3. [3]

    Zhang, H., Dauphin, Y. N., & Ma, T. (2019). "Fixup initialization: Residual learning without normalization". arXiv:1901.09321 — Identity-initialisation tricks for very deep residual stacks (6.9 §4).

    ↩