Transformers from the ground up
Residual streams and layer norm
Every sublayer writes into a shared residual stream. Layer norm keeps that stream at a workable scale, and the order of the two operations decides how deep a model can be trained.
The residual stream
Each block reads the current vector for a token. It computes an update and adds that update back to the vector it read. The path from input to output is therefore a sum of many small contributions. It is not a chain of replacements. The identity connection gives gradients a short route to every earlier layer. That is what makes deep stacks trainable.
The image to keep is a shared bus. Attention and the feed-forward block both read from it and write onto it. Neither owns it. Because the stream is a sum, a later layer can still recover the original token signal. Many blocks may have added to it by then. The stream’s scale also tends to grow with depth unless something controls it.
Layer norm, not batch norm
Layer norm rescales each token’s vector across its features. It subtracts the mean and divides by the standard deviation. It then applies a learned scale and shift. Every token is normalised using only its own values. The result does not depend on the other examples in the batch or on the sequence length. That independence is why it works for text of any length and for small batches.
Batch norm, by contrast, computes statistics across the batch. It works well for images with a fixed shape and a large batch. It also couples examples together and behaves differently in training and inference. Layer norm has no running averages to maintain. Its behaviour is the same in both modes. The learned scale and shift restore the network’s ability to represent unusual distributions.
Without those two vectors, every token would sit on a sphere of the same radius. The next matrix would have to work with directions alone. The scale lets a feature grow again. The shift lets it sit away from zero. Normalisation is therefore a change of variables, not a constraint. The cost is small: two vectors per layer, not a full matrix.
A pre-norm block
from torch import nn
norm = nn.LayerNorm(512, eps=1e-5)
# normalize, transform, then add to the stream
x = x + attention(norm(x))
x = x + feed_forward(norm(x))Normalise one token
Run the normalisation, then change the learned scale to [1, 2, 3, 4] and confirm the output mean is no longer zero.
Pre-norm and post-norm
The original transformer normalised after the residual addition: x = norm(x + sublayer(x)). That post-norm arrangement needs a careful learning-rate warmup. Very deep stacks also become hard to train, because the residual path is rescaled at every block. Many later models moved the norm inside the branch, before the sublayer. They kept a final norm before the output.
Pre-norm is easier to train and is the common choice for deep models. It is not strictly better in every respect. The residual stream is never normalised between blocks, so its scale can grow with depth. Some work also finds that post-norm or hybrid arrangements give better quality per weight at shallow depths. The choice is empirical, not a law.
The placement also changes what the residual stream carries. In pre-norm, the stream is a running sum of unnormalised updates. Its norm can grow with depth. In post-norm, every block resets the scale before the next one reads it. Neither is free. One asks for a final norm and careful starting weights. The other asks for a warmup schedule.
Layer norm normalises across features, not across tokens. If you accidentally normalise the other axis, every token in a sequence becomes coupled and the model stops behaving like a language model at all.
What normalization does to the stream
Normalisation removes the overall scale of a vector and keeps its direction. The next matrix product is then less sensitive to how large the stream has become. The activation also stays in a range where gradients are useful. The learned scale can put some of the size back. It does so per feature, not per token.
RMSNorm is a simpler version. It skips the mean subtraction and divides by the root mean square. It is cheaper and works about as well in many models. The epsilon term matters when the variance is near zero. Without it, a constant vector would divide by zero and produce an infinite update. The value is small, but it is not decorative.
A final layer norm before the output head is common in pre-norm models. The residual stream is never normalised inside the blocks. Without the final norm, the logits scale with the depth and the softmax can saturate. The final norm is cheap and easy to forget. Its absence shows up as unstable generation, not as a shape error.
A stream that survives depth
Run five blocks with and without the residual addition, then make the block shrink its input and compare the two norms again.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does a residual connection add?
