Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Normalization

In the previous chapter, we added residual connections to the two sublayers of the Transformer block, which gave the gradient a fast channel. But the residual connection also brings a side effect: the output of each layer is “input + transformation”, so as the number of layers grows, the values keep accumulating and getting larger, and training becomes unstable.

Layer normalization

The solution is layer normalization: pull the output of each layer back to a standard distribution with mean 0 and variance 1.

Compute the mean and variance:

μ=1d∑i=1dxi,σ2=1d∑i=1d(xi−μ)2\mu = \frac{1}{d} \sum_{i=1}^{d} x_i, \quad \sigma^2 = \frac{1}{d} \sum_{i=1}^{d} (x_i - \mu)^2

Standardize:

x^=x−μσ2+ϵ\hat{x} = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}}

Scale and shift:

output=γ⊙x^+β\text{output} = \gamma \odot \hat{x} + \beta

Here:

  • dd: the embedding size;

  • ϵ\epsilon: a very small number (for example 10-5) that keeps the divisor from being 0;

  • γ\gamma (weight): the learnable scaling factor (weight);

  • β\beta (bias): the learnable shifting factor (bias).

No matter how large the values are, layer normalization can pull them back to the standard range and prevent them from exploding. It also keeps the distribution of the values stable, makes gradient changes smoother, and speeds up convergence.

The learnable scaling and shifting factors let the magnitude of the normalization adjust itself automatically, so that the one-size-fits-all normalization formula fits the dataset better.

GPT basic modules with normalization: the inputs of the attention layer, the feed-forward network layer and the output layer all pass through a normalization layer first
Figure: GPT basic modules with normalization

Dataset

Character-encoding dataset

Lower triangular matrix layer

Normalization layer

We add a normalization layer (the LayerNorm layer):

  • Forward pass: compute the mean and variance over the last dimension (the feature dimension). After standardizing, scale and shift;

  • Backward pass: the gradients of the input xx, the scaling factor γ\gamma and the shifting factor β\beta need to be computed separately.

Activation functions

Gaussian error linear unit (GELU)

Loss function

GPT cross-entropy loss function

Model

GPT embedding layer

GPT attention layer

In the GPT attention layer, we first normalize the input, then compute attention, and finally add the residual connection.

GPT feed-forward network layer

Likewise, in the GPT feed-forward network layer we first normalize the input, then apply the feed-forward network, and finally add the residual connection.

GPT Transformer block

GPT output layer

The output layer likewise first normalizes its input, to make sure that the data entering the linear layer has a stable distribution.

GPT model

GPT trainer

Hyperparameters

Learning rate

Batch size

Number of epochs

Context size

Embedding size

Number of heads

Number of Transformer blocks

Modeling

Training

Evaluation

prediction:	1742 steps, each (4, 32, 65)
loss:	1.7808665809757263

Inference

ROMEO:
Well.

SABELLA:
Therefore thee go by the tranted is is fastely, and is but more him, where's for you wife?

HORFOMIO:
I am as oncove whomostion on holve all me there and when yalk?

DUKE VINCENTIO:
You speech that be, being suppy,
That try love.

SAVINCO:

In this chapter, we added layer normalization to the modules of the GPT model, to make sure that after the residual connection the data entering the next layer is back in the standard range, which keeps training stable.

With this, we have basically solved the problem of how to stack a GPT model deeper while keeping training stable, clearing the obstacles to training large-scale GPT models. In the next chapter, we will look at a problem that large-scale training easily runs into: overfitting. The model performs well on the training set but may perform poorly on the test set. It “memorizes” too many details of the training data instead of truly “learning” the patterns of language.

Exercises

  1. Root mean square normalization (RMSNorm) is an effective simplified version of layer normalization. RMSNorm directly assumes that the mean is 0 and computes only the root mean square. It also keeps only the learnable scaling factor γ\gamma and drops the shifting factor β\beta. Try replacing the normalization layer with RMSNorm and see how the training results change.