liveBook · Manning

GPTModel

As explained in Build a Large Language Model (From Scratch)

The concrete GPT architecture class assembled in this section from embeddings, transformer blocks, normalization, and an output head.

GPTModel is the concrete GPT architecture assembled from token and positional embeddings, repeated TransformerBlock modules, final layer normalization, and a vocabulary-sized linear output head. Its forward pass converts token IDs into dense representations, adds positional information, processes the sequence through the transformer stack, normalizes the result, and produces logits for next-token prediction. section 4.6

The implementation follows this pipeline:

  • Embed input token IDs with tok_emb and create positional embeddings for the current sequence length. section 4.6
  • Add the token and positional tensors, then apply embedding dropout. section 4.6
  • Pass the result through n_layers repeated TransformerBlock modules containing masked multi-head attention and feed-forward networks. section 4.6 section 4.8
  • Apply final normalization and map each hidden vector through out_head to the 50,257-token vocabulary. section 4.6

Shape flow and generation

With a batch shaped [2, 4], GPTModel produces logits shaped [2, 4, 50257]: one vocabulary vector for every token position. Text generation selects the final position, applies softmax, chooses the highest-scoring token ID, appends it to the context, and repeats. Starting from “Hello, I am” and generating six tokens yields a ten-token sequence that can be decoded back into text. section 4.7 section 4.6

Figure 4.15 An overview of the GPT model architecture showing the flow of data through the GPT model. Starting from the bottom, tokenized text is first converted into token embeddings, which are then augmented with positional embeddings. This combined information forms a tensor that is passed through a series of transformer blocks shown in the center (each containing multi-head attention and feed forward neural network layers with dropout and layer normalization), which are stacked on top of each other and repeated 12 times.
figure

Use the architecture diagram to trace token embeddings and positional embeddings through the repeated transformer blocks to the final output. section 4.6

The same class supports a 124-million-parameter GPT-2 configuration with 12 transformer blocks and can be resized through configuration dictionaries. It can be instantiated for generation, trained with a PyTorch loop, saved through state_dict, restored with optimizer state, or populated with OpenAI GPT-2 weights. section 5.2 section 5.4 section 5.5

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Defines the class, tensor flow, output shapes, training loop, and weight loading, while deferring advanced optimization techniques.
Examples
Uses 124M GPT-2 configurations, [2, 4, 50257] logits, “Hello, I am” generation, training-loss traces, and OpenAI weight loading.
What it assumes
Assumes token IDs and PyTorch basics while teaching the architecture by composing embeddings, transformer blocks, normalization, and output projection.
What you can do after
Build a GPTModel in PyTorch, generate token sequences, pretrain it, save checkpoints, and load compatible GPT-2 weights.
How it connects
Connects tokenization, positional embeddings, transformer blocks, layer normalization, dropout, logits, softmax, pretraining, and pretrained weights.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.