A transformer block can be written post-LN, xl+1=LN(xl+Fl(xl)), or pre-LN, xl+1=xl+Fl(LN(xl)). How do gradients flow back through L layers in the two, and why does post-LN usually need learning-rate warm-up?

Sequence models and transformers, Transformers
Level L4 Advanced. Length Long. Nature Th+Mat Both.

What's being asked

Whether you can see that pre-LN keeps an identity path for the gradient while post-LN passes it through a layer norm at every block.

Key idea

Pre-LN: ∂xL∂xl=I+∑(terms through F), so there is always an identity path. Post-LN: ∂xL∂xl=∏k=lL−1JkLN (I+∂Fk/∂xk), with a layer-norm Jacobian at every layer (later layers multiply on the left).

Steps

  1. Pre-LN: write xl+1=xl+Fl(LN(xl)). Then ∂xl+1/∂xl=I+∂Fl(LN(xl))/∂xl.
  2. Multiplying over layers and expanding, ∂xL/∂xl=I+∑(… ). The identity term carries the loss gradient from the output to any layer with no attenuation. The LN sits inside the branch, not on the residual path.
  3. Post-LN: ∂xl+1/∂xl=JlLN (I+∂Fl/∂xl), where JLN is the layer-norm Jacobian. For input y it contains 1/σ(y) and projects out the mean and scale directions.
  4. The residual stream is rescaled at each layer, so there is no clean identity path. The product of L Jacobians can shrink or grow.
  5. Xiong et al. (2020) analyse both at initialisation. For post-LN, the gradient of the last layer's parameters is of order dln⁡d whatever L is, and it is large near the output and decays towards lower layers. For pre-LN it is of order dln⁡d/L and about the same size in every layer. A large early learning rate on the big post-LN gradients near the output makes training unstable, so warm-up is needed. For pre-LN the gradients are well-behaved and warm-up can often be dropped.
  6. Trade-off: in pre-LN the residual stream norm grows with depth, so deep layers add relatively little to it, and a final layer norm is needed before the output. Post-LN can give slightly better final quality when it trains.

Common mistakes

How long your answer should be

Half a page to a page; 6–8 minutes.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: How can softmax attention be written as a kernel smoother? Why does replacing exp⁡(q⊤k) by a feature-map kernel ϕ(q)⊤ϕ(k) reduce the cost from O(n2d) to O(nd2)? How can causal linear attention be computed as an RNN, and what is lost?
Next in Sequence models and transformers: What is an induction head, and how can a two-layer attention-only transformer use a pair of heads to complete a pattern such as [A][B]…[A]→[B]? What evidence ties induction heads to in-context learning?