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 of non-linear sub-layers is, in practice, untrainable for large . The reason is the multiplicative chain rule: the gradient at layer acquires a factor for every layer above it, and the product of such factors typically collapses to or explodes to as grows. We met this in 2.9.
The fix is structural, not numerical. Replace each sub-layer's pure composition with the residual form
so that the layer adds a correction to its input rather than overwriting it. The identity term 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 -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 be the operation at layer (one Transformer sub-layer in our case). A plain stack computes
and the final loss is . By the chain rule (in the numerator convention of 0.2),
where is the Jacobian of the -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 denote the operator (spectral) norm. Then
and the right-hand side is a product of non-negative scalars. If the typical spectral norm satisfies , then , which
- vanishes geometrically if (every layer scales the gradient down a touch and after layers nothing survives), or
- explodes geometrically if (the gradient blows up and training diverges).
The boundary is unstable: any random initialisation puts either side. Concretely, with a ReLU/GELU sub-layer and standard Xavier/He initialisation, one finds for most (the non-linearity zeros out roughly half its inputs, halving the effective rank), and the gradient vanishes as grows. This is the precise statement of "deep networks don't train".
3. The residual fix
A residual block with residual function computes
The mapping is a correction: does not need to reproduce in its output — the identity path delivers it for free. 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,
where 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 residual blocks. The full Jacobian becomes
Expand this product. Each term in the expansion is an ordered subset of layers: pick for and for . The number of terms is , and they collect by cardinality :
(ordered correctly; lower indices innermost). The leading 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 (so that pure compositions would collapse), the gradient stays because the leading keeps a direct line from to .
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 at training start (small weights), then
i.e. the network at initialisation behaves like a chain of identity maps: . The model starts as a shallow effective network and the optimiser is free to introduce non-trivial one layer at a time, in whichever order helps the loss. This is sometimes called identity initialisation or "the residual network at is a shallow network".
The practical implementation differs by author but is uniformly cheap:
- ResNet-original (He, 2015)[1]. Initialise the last BatchNorm in each block to — so the block output is exactly at . (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
Two residual connections per block. Across blocks (GPT-3 scale), skip connections. The output is 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 , computes a correction , and adds it back into the stream. There is a single -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.
This is also why Transformer interpretability has been able to make so much progress: a layer that replaced the stream (a plain 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 accumulates the magnitudes of across . Without normalisation, grows roughly as (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. needs to live in the same vector space as . In a Transformer this is automatic — every sub-layer maps . In ResNet, a strided convolution that changes the spatial resolution must be paired with a projection on the skip path (the "projection shortcut"). We won't worry about this in language models; 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 for simplicity, set with at every layer, and stack layers.
Plain stack: . The forward signal has vanished. Symmetrically, . Untrainable.
Residual stack: , so . The signal is small but not catastrophic, and the Jacobian is , eighteen orders of magnitude larger than the plain stack. With near zero (small initialisation, §4) the residual stack is essentially the identity at and the optimiser can lift each off zero gradually.
This is the same story with : the spectral spread of around 1 is much more forgiving than the spectral spread of 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:
- The identity term gives the gradient a direct path from output to input — multiplicative collapse is broken.
- Initialising makes the deep network start as a shallow one; layers grow non-trivial as training proceeds.
- Combined with norms (3.8), residuals turn a Transformer into a residual stream — a shared -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.
(a) Write the Jacobian of one residual block in numerator layout. Then expand the Jacobian of two stacked residual blocks in terms of and . Identify the "identity path" term.
(b) Suppose every residual sub-layer has . Bound the spectral norm of the full -layer Jacobian above and below. Compare with the plain stack for .
(c) Argue that "identity initialisation" makes a -layer Transformer behave like a shallow network at . What does this imply about the rank of when training starts?
(d) A Transformer block applies two residual additions per layer (one around attention, one around FFN). For a stack of blocks, how many identity paths run from input to output? Compare to a ResNet with residual blocks.
(e) Veit, Wilber & Belongie (2016) argue that a residual network behaves like an "ensemble of shallow networks". Translate this into the expansion of §3.2: how does each subset correspond to one ensemble member?
References
- [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]
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]
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).
↩