liveBook · Manning

Chapter 4: Implementing a GPT model from scratch to generate text

What you will learn in chapter 4 of Build a Large Language Model (From Scratch)

Introduces the plan to assemble previously coded multi-head attention with other building blocks into a GPT-like model.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the plan to assemble previously coded multi-head attention with other building blocks into a GPT-like model.

4.1 Coding an LLM architecture

This section introduces a GPT-2-sized model configuration, traces data through a placeholder GPT architecture, and explains the shape of its token-level outputs.

Key terms: parameters, trainable weights, GPT-2, GPT-3, pretrained weights, GPT_CONFIG_124M, vocab_size, context_length, emb_dim, n_heads

4.2 Normalizing activations with layer normalization

This section explains why GPT uses layer normalization, how it normalizes activations, and the implementation choices in its LayerNorm module.

Key terms: layer normalization, activations, vanishing or exploding gradients, unit variance, mean, variance, standard deviation, dim=-1, embedding dimension, LayerNorm

4.3 Implementing a feed forward network with GELU activations

This section explains why GPT uses GELU and how a feed-forward module expands and contracts token embeddings while preserving their input shape.

Key terms: GELU, standard Gaussian distribution, approximation, ReLU, smoothness, gradient, FeedForward, Linear layers, GELU activation, embedding dimension

4.4 Adding shortcut connections

The section explains how shortcut connections help prevent vanishing gradients and uses a small network to compare gradient flow with and without them.

Key terms: shortcut connections, skip connections, residual connections, vanishing gradient problem, mean absolute gradient, loss.backward(), print_gradients, layers.4, layers.0

4.5 Connecting attention and linear layers in a transformer block

This section connects attention, feed forward processing, normalization, dropout, and shortcuts into a transformer block while explaining why its output keeps the input shape but gains contextual information.

Key terms: masked multi-head attention, FeedForward, self-attention, TransformerBlock, Pre-LayerNorm, Post-LayerNorm, LayerNorm, dropout, shortcut connection, input dimensions

4.6 Coding the GPT model

This section assembles a GPT model from embeddings and previously built components, explains its output logits, and accounts for its parameter count and memory use.

Key terms: GPTModel, cfg, token embeddings, positional embeddings, TransformerBlock, LayerNorm, logits, batch_size, num_tokens, vocab_size

4.7 Generating text

This section explains how to turn GPT output scores into text by selecting and appending one token at a time, then shows why an untrained model produces gibberish.

Key terms: softmax, token ID, decode, generate_text_simple, input context, maximum context size, logits, torch.argmax, greedy decoding, .eval()

4.8 Summary

Summarizes the components that make up a GPT model, how it generates text, and why training matters.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Coding attention mechanisms Chapter 5: Pretraining on unlabeled data →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.