4 Implementing a GPT model from Scratch To Generate Text

 

This chapter covers

  • Coding a GPT-like large language model (LLM) that can be trained to generate human-like text
  • Normalizing layer activations to stabilize neural network training
  • Adding shortcut connections in deep neural networks to train models more effectively
  • Implementing transformer blocks to create GPT models of various sizes
  • Computing the number of parameters and storage requirements of GPT models

In the previous chapter, you learned and coded the multi-head attention mechanism, one of the core components of LLMs. In this chapter, we will now code the other building blocks of an LLM and assemble them into a GPT-like model that we will train in the next chapter to generate human-like text, as illustrated in Figure 4.1.

Figure 4.1 A mental model of the three main stages of coding an LLM, pretraining the LLM on a general text dataset, and finetuning it on a labeled dataset. This chapter focuses on implementing the LLM architecture, which we will train in the next chapter.

The LLM architecture, referenced in Figure 4.1, consists of several building blocks that we will implement throughout this chapter. We will begin with a top-down view of the model architecture in the next section before covering the individual components in more detail.

4.1 Coding an LLM architecture

4.2 Normalizing activations with layer normalization

4.3 Implementing a feed forward network with GELU activations

4.4 Adding shortcut connections

4.5 Connecting attention and linear layers in a transformer block

4.6 Coding the GPT model

4.7 Generating text

4.8 Summary

sitemap