1. Can the gradient cross a deep network?
The previous lesson described stacks of Transformer blocks. During training, the loss must send a useful signal back through that stack to the earlier parameters.
The chain rule multiplies derivatives along a path. To see why depth matters, imagine a scalar network with 96 identical local derivatives:
One product becomes very small; the other grows substantially. Neither number by itself establishes overflow or a training failure, but they illustrate how repeated multiplication can change the scale of a learning signal.
Real networks multiply Jacobian matrices, not one average scalar per layer. Direction, singular values, nonlinearities and the current inputs matter. The example is a way to understand the chain rule, not a prediction of a particular model's gradient.
We encountered a related problem in backpropagation and recurrent networks. In an RNN, the repeated path runs through time; here it runs through depth.
2. Residual connections: learn an update
A plain layer computes . An identity residual layer computes:
The branch can learn an update to the existing representation rather than replacing it entirely. If a desired mapping is , the branch's target can be expressed as .
When leaving the representation nearly unchanged is useful, making small produces an approximately identity mapping. This can be easier to optimise than asking a complicated plain branch to reproduce its input.
The addition still changes the model. A learned branch does not automatically compensate perfectly, and adding a residual path does not guarantee that performance improves. The architecture is trained with that path included.
Input x
f(x)
x unchanged
↓ Add the two branches ↓
For vectors, differentiating gives:
The identity term provides a direct contribution to the gradient. But the total gradient includes the learned branch too. In a scalar example, cancels the identity term, while gives a derivative of , which can amplify across depth.
If every scalar branch has derivative , then:
This is a particular favourable example, not a rule that residual derivatives are always close to one.
Plain derivative product aⁿ: 1.0000e-192
Residual derivative product (1 + a)ⁿ: 2.5993e+0
Derivative of one residual layer: 1.01
All layers use the same fixed scalar slope, with no normalisation. Try a = −1 to see cancellation or a = 0.9 to see amplification. These products do not predict gradients in a real Transformer.
To add and directly, their shapes must match. Transformer sublayers normally return the model width, so each residual addition works row by row on the sequence representation. Other network designs can use a projected shortcut when dimensions differ; that shortcut's derivative is no longer exactly the identity.
3. LayerNorm: standardise one feature row
Repeated updates can change activation scales. Very large values can affect numerical precision and attention scores, while changing scales can complicate optimisation. But residual additions do not necessarily make the vector larger at every layer.
For a residual update:
its squared length satisfies:
The middle term can be negative, so the result depends on the direction of the update.
Start with x = [1, 0]
Aligned increment · norm increases
Add [0.5, 0] → [1.5, 0] · norm 1.500
Opposing increment · norm decreases
Add [-0.5, 0] → [0.5, 0] · norm 0.500
Perpendicular increment · direction changes
Add [0, 0.5] → [1, 0.5] · norm 1.118
In Transformer LayerNorm, statistics are normally computed across the feature coordinates of one token row, separately for each token and example. They are not a shared mean across the whole sequence or minibatch.
For :
A positive protects the denominator when the feature variance is zero or very small. The variance uses division by , not the sample-estimation correction .
The learned vectors and allow coordinate-wise scale and shift. They are parameters shared across token positions, while the statistics depend on each input row.
Work through four coordinates
For , the mean is and the variance is :
The centred vector has mean zero. With finite epsilon, its standardised variance is , slightly below one for positive variance. After the learned affine transformation, the output need not have zero mean or unit variance at all.
LayerNorm also does not turn every vector into a unit-length vector. Before the affine step and ignoring epsilon, a nonconstant standardised row has squared norm and length .
Mean μ: 4.0000
Variance σ²: 6.0000
Denominator √(σ² + ε): 2.4495
Standardised: [1.6330, -0.8165, -0.8165, 0.0000]
LayerNorm affine output: [1.6330, -0.8165, -0.8165, 0.0000]
RMSNorm output (same γ; no β): [1.7056, 0.4264, 0.4264, 0.8528]
ε = 0.00001. This uses one shared γ and β to make the example easy to inspect; real LayerNorm normally learns a separate scale and shift for each feature. RMSNorm does not subtract the mean and usually omits the shift.
Choose the constant input to see why epsilon matters: after centring, all coordinates are zero, and LayerNorm's output is its shift. RMSNorm behaves differently because it does not subtract the mean.
What normalisation helps with
Normalisation provides more controlled inputs to subsequent calculations. It is not clipping, does not make all coordinates equally important and cannot guarantee numerical safety by itself.
Large differences between attention scores can make Softmax highly concentrated; adding the same constant to every score does not change Softmax. Learned projections, scaling and normalisation interact to determine those scores.
Finite-precision addition can also lose a small increment beside a much larger value. The exact outcome depends on the number format and operations used. These are reasons to pay attention to numerical scale, rather than proof that any particular activation magnitude always causes a crash.
4. Put normalisation before or after the branch?
Two familiar arrangements use the same ingredients in different orders:
Post-norm · normalise after adding
Both contributions pass through LayerNorm after the sum.
Pre-norm · normalise the branch input
The identity path bypasses this sublayer's normalisation.
Ignoring dropout, a post-norm block with attention and feed-forward network can be written:
A pre-norm block instead uses:
The original Transformer used post-norm. In pre-norm, the main residual stream bypasses that sublayer's normalisation; the branch receives a normalised input. A final normalisation is commonly applied after the stack.
The gradient distinction is visible in the Jacobians:
The Jacobians are evaluated at their corresponding inputs. Pre-norm retains an explicit identity term; post-norm passes both contributions through a normalisation derivative.
Pre-norm often makes deep-network optimisation easier. But “post-norm cannot train” and “pre-norm never needs warm-up” would both be too strong. Initialisation, learning rates, residual scaling, depth and other design choices also matter. Both arrangements can train successfully.
A pre-norm residual stream itself is not reset to a fixed norm after each addition. It is the branch's input that is normalised, so claims that LayerNorm always forces every block output back to length one miss the placement distinction.
RMSNorm: normalise by root mean square
A common alternative omits mean subtraction and usually omits the learned shift:
This uses the mean of squared coordinates rather than centred variance. It can simplify computation, but it is not identical to LayerNorm and is not invariant to adding a constant offset. Comparable performance in some settings does not mean the two are interchangeable in every trained model.
5. What you have discovered
- Residual connections add a learned update to an existing representation and introduce an identity contribution to its Jacobian.
- That identity contribution can help gradient propagation, but total gradients are not guaranteed to stay near one.
- LayerNorm computes each token row's feature statistics, standardises with epsilon and applies learned scale and shift.
- Pre-norm and post-norm place normalisation on different computation paths; this changes their optimisation behaviour.
- RMSNorm controls scale without subtracting the mean, so its behaviour differs from LayerNorm.
We now have the architecture and its stabilising components. Next, we will follow pretraining, supervised fine-tuning and reinforcement learning, and see how training objectives turn a language model into an assistant.