Residual connection (Transformer)
A residual connection adds the input of a sub-layer to the output of that sub-layer, so that the sub-layer only has to learn a correction to its input. Both sub-layers of a Transformer block, self-attention and the FFN, are wrapped in a residual connection and a layer normalisation.[1]
Definition
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the input of a sub-layer, and let $\operatorname{SL}$ be the map that sub-layer computes. The residual connection replaces $\operatorname{SL}$ by
$$ \operatorname{Res}(\mathbf{X})=\mathbf{X}+\operatorname{SL}(\mathbf{X}). $$
The sum is defined only if $\operatorname{SL}$ returns a matrix of the same shape, which is why every sub-layer of a Transformer outputs width $d$.
With $\operatorname{SL}\equiv\mathbf{0}$ the connection is the identity, $\operatorname{Res}(\mathbf{X})=\mathbf{X}$, so a deeper stack can reproduce a shallower model exactly. That is the reason depth does not hurt in principle.[2]
Layer normalisation
Layer normalisation (LN) normalises the $d$ components of 1 position to zero mean and unit variance, then rescales and shifts them with 2 learned vectors,
$$ \operatorname{LN}(\vec x) =\vec\gamma\odot\frac{\vec x-\mu\,\mathbf{1}}{\sqrt{\sigma^{2}+\epsilon}}+\vec\beta, \qquad \mu=\frac{1}{d}\sum_{i=1}^{d}x_i, \qquad \sigma^{2}=\frac{1}{d}\sum_{i=1}^{d}\left(x_i-\mu\right)^{2}, $$
where $\vec\gamma,\vec\beta\in\mathbb{R}^{d}$ are parameters (a gain and a bias, $2d$ of each per LN), $\epsilon\approx10^{-5}$ guards the division, and $\odot$ is the elementwise product.[3] Applied to a sequence it acts row by row,
$$ \operatorname{LN}(\mathbf{X}) =\begin{pmatrix} \operatorname{LN}\left(\vec x_1\right)^{\top}\\ \vdots\\ \operatorname{LN}\left(\vec x_T\right)^{\top} \end{pmatrix}, $$
so the statistics are taken over the features of 1 position. They do not depend on the other examples of the batch, and are the same during training and at inference, which is not the case for batch normalisation.
Invariance to shift and scale
Setting $\epsilon=0$ for clarity, the mean and the variance of $\alpha\vec x+c\mathbf{1}$ are
$$ \frac{1}{d}\sum_{i=1}^{d}\left(\alpha x_i+c\right)=\alpha\mu+c, \qquad \frac{1}{d}\sum_{i=1}^{d}\left(\alpha x_i+c-\alpha\mu-c\right)^{2}=\alpha^{2}\sigma^{2}, $$
hence, for $\alpha>0$,
$$ \operatorname{LN}\left(\alpha\vec x+c\mathbf{1}\right) =\vec\gamma\odot\frac{\alpha\left(\vec x-\mu\mathbf{1}\right)}{\alpha\sqrt{\sigma^{2}}}+\vec\beta =\operatorname{LN}\left(\vec x\right). $$
So the scale $\alpha$ and the offset $c$ produced by the layer before LN are erased by it, and $\vec\gamma,\vec\beta$ are the only quantities that set the scale and the offset of the normalised output.
Gradient path
Write the representation after block $l$ as $\mathbf{Y}_l$, so that
$$ \mathbf{Y}_l=\mathbf{Y}_{l-1}+F_l\left(\mathbf{Y}_{l-1}\right), \qquad l=1,\cdots,L, \qquad \mathbf{Y}_0=\mathbf{X}, $$
where $F_l$ is the sub-layer of block $l$. Treating $\partial\mathbf{Y}_l/\partial\mathbf{Y}_{l-1}$ as the linear map it induces on the flattened matrix,
$$ \frac{\partial\mathbf{Y}_l}{\partial\mathbf{Y}_{l-1}} =\mathbf{I}+\frac{\partial F_l}{\partial\mathbf{Y}_{l-1}} =\mathbf{I}+\mathbf{J}_l, $$
so by the chain rule the gradient that reaches the input is
$$ \frac{\partial L}{\partial\mathbf{Y}_0} =\frac{\partial L}{\partial\mathbf{Y}_L}\prod_{l=1}^{L}\left(\mathbf{I}+\mathbf{J}_l\right). $$
For $L=2$ the product reads
$$ \left(\mathbf{I}+\mathbf{J}_2\right)\left(\mathbf{I}+\mathbf{J}_1\right) =\mathbf{I}+\mathbf{J}_1+\mathbf{J}_2+\mathbf{J}_2\mathbf{J}_1, $$
and the same holds for general $L$: every product of $k$ Jacobians appears once. In particular the term with $k=0$ is $\mathbf{I}$, so a gradient can travel from block $L$ to block $0$ through the identity alone, without being multiplied by any $\mathbf{J}_l$.
A plain stack has no such term. With $\mathbf{Y}_l=F_l(\mathbf{Y}_{l-1})$,
$$ \frac{\partial\mathbf{Y}_L}{\partial\mathbf{Y}_0}=\prod_{l=1}^{L}\mathbf{J}_l, \qquad \left\|\prod_{l=1}^{L}\mathbf{J}_l\right\|\le\kappa^{L}\ \text{ if }\ \left\|\mathbf{J}_l\right\|\le\kappa<1, $$
which is the vanishing-gradient problem: the factor decays geometrically with depth.
Placement
2 placements are in use. They differ in whether LN sits on the residual path or inside the branch,
| Post-LN | Pre-LN |
|---|---|
| $\mathbf{X}^{(l+1)}=\operatorname{LN}\left(\mathbf{X}^{(l)}+\operatorname{SL}\left(\mathbf{X}^{(l)}\right)\right)$ | $\mathbf{X}^{(l+1)}=\mathbf{X}^{(l)}+\operatorname{SL}\left(\operatorname{LN}\left(\mathbf{X}^{(l)}\right)\right)$ |
With $\mathbf{D}_l$ the Jacobian of the LN, the 1-step Jacobians are
$$ \text{Post-LN: }\ \frac{\partial\mathbf{X}^{(l+1)}}{\partial\mathbf{X}^{(l)}}=\mathbf{D}_l\left(\mathbf{I}+\mathbf{J}_l\right), \qquad \text{Pre-LN: }\ \frac{\partial\mathbf{X}^{(l+1)}}{\partial\mathbf{X}^{(l)}}=\mathbf{I}+\mathbf{J}_l\mathbf{D}_l . $$
In Post-LN the term of the product that carries no $\mathbf{J}$ is $\prod_{l=1}^{L}\mathbf{D}_l$, so the identity path is rescaled at every layer. In Pre-LN that term is $\mathbf{I}$ itself, and LN acts only inside the branch: the residual stream keeps its scale, and a final LN is applied before the output head, since no block normalises it.
Consequence: at initialisation Post-LN produces large expected gradients for the parameters near the output, so a large learning rate there is unstable and a warm-up phase is needed; Pre-LN keeps those gradients bounded and trains without 1.[4] The original Transformer is Post-LN, trained with a warm-up phase.[5]
Example
- ↑ Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. 5998–6008. https://doi.org/10.48550/arXiv.1706.03762He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep Residual Learning for Image Recognition. 770–778. https://doi.org/10.1109/CVPR.2016.90
- ↑ He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep Residual Learning for Image Recognition. 770–778. https://doi.org/10.1109/CVPR.2016.90
- ↑ Ba, J. L., Kiros, J., & Hinton, G. E. (2016). Layer Normalization. https://arxiv.org/abs/1607.06450
- ↑ Xiong, R., Yang, Y., He, D., Zheng, K., Zheng, S., Xing, C., Zhang, H., Lan, Y., Wang, L., & Liu, T.-Y. (2020). On Layer Normalization in the Transformer Architecture. 3532–3541. https://proceedings.mlr.press/v119/xiong20b.html
- ↑ Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. 5998–6008. https://doi.org/10.48550/arXiv.1706.03762