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.

Residual connection

Over 4 chapters, we completed the most central part of the GPT model: the self-attention mechanism.

Next, we will focus on the engineering implementation of GPT pre-training.

A distinctive feature of GPT models is that “scale works wonders” (a Chinese saying, literally “great force produces miracles”). It shows not only in the massive data used for training, but also in the huge parameter count and in deep network structures of dozens or even hundreds of layers. Specifically, the GPT-3 model has 96 Transformer blocks, and deeper models have more layers. The more Transformer blocks are stacked, the harder training gets. During backpropagation the gradient has to pass through dozens or hundreds of layers, and each layer “dilutes” it a little more, until it almost vanishes in the last few layers. This is the so-called vanishing gradient problem.

Residual connection

In 2015, Kaiming He and colleagues at Microsoft Research proposed the concept of the residual connection, which has greatly alleviated the challenges associated with training deep neural networks. This paper (ResNet) is one of the most cited papers in the history of deep learning.

The core idea of the residual connection is extremely simple: let the output of each layer = input + transformation.

output=x+f(x)\text{output} = x + f(x)

instead of the traditional output=f(x)\text{output} = f(x).

Why does the residual connection work

  • Forward pass: even if f(x)f(x) is learned poorly, the output still contains xx at least, so no information is lost;

  • Backward pass: the gradient can flow back through xx and does not depend entirely on the gradient of f(x)f(x), so the vanishing gradient problem is greatly eased.

A residual connection is like building an extra highway for the gradient: however winding the f(x)f(x) in between may be, the gradient can always flow back to the earlier layers unobstructed.

Where to add residual connections

In the GPT model, the depth of the network consists mainly of Transformer blocks. A Transformer block contains two sublayers: self-attention and the feed-forward network. Each sublayer needs a residual connection:

  • self-attention: x=x+attention(x)x = x + \text{attention}(x);

  • feed-forward network: x=x+feedforward(x)x = x + \text{feedforward}(x).

In this way, no matter how many Transformer blocks are stacked, the gradient can reach the first layer smoothly, avoiding vanishing gradients.

GPT basic modules with residual connections: the attention layer and the feed-forward network layer each have a residual connection that bypasses the layer
Figure: GPT basic modules with residual connections

Dataset

Character-encoding dataset

Lower triangular matrix layer

Activation functions

Gaussian error linear unit (GELU)

Loss function

GPT cross-entropy loss function

Model

GPT embedding layer

GPT attention layer

We add a residual connection at the end of the GPT attention layer, changing the output from self.output(...) to x + self.output(...).

GPT feed-forward network layer

Likewise, we add a residual connection at the end of the GPT feed-forward network layer, changing the output from self.output(...) to x + self.output(...).

GPT Transformer block

GPT output layer

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.8085668785700706

Inference

ROMEO:
Well, I'll poisen, swearn thee, haprite,
Will me is is fatherior offenretcher rangershed is in tet Catie, there name the king,
Lets Comiou, who no: well I delieve what bow,
And
Berancionr her; and love stime and in him esceasured,
That try love.

SIY:
HtU

In this chapter, we added residual connections to both sublayers of the Transformer block. The gradient now has a direct channel, and one of the training difficulties of deep neural networks, the vanishing gradient, is solved with ease.

But the residual connection also has a side effect: the output of every layer is “input + transformation”, so as the number of layers grows, the values keep accumulating and getting larger, which makes training unstable. In the next chapter, we will set out to solve this problem.

Exercises

  1. Both sublayers of the Transformer block now have residual connections. Try removing one of them and see how the training results change.