GPT-2 is a decoder-only transformer language model that maps token sequences to next-token logits. Its pipeline combines token and positional embeddings, repeated transformer blocks, final layer normalization, and a vocabulary-sized linear output layer. section 4.1 section 4.6
The 124M configuration specifies the model’s main data flow and dimensions:
- Accepts token-index batches shaped (batch size, sequence length), then produces context representations for every input position. section 4.1
- Adds token embeddings and positional embeddings with vocabulary size 50,257, context length 1,024, and embedding dimension 768. section 4.1 section 4.6
- Passes the combined tensor through 12 repeated transformer blocks containing masked multi-head attention, feed-forward layers, dropout, and layer normalization. section 4.6
- Applies final normalization and projects each position to 50,257 logits, yielding an output shape of (batch size, sequence length, 50,257). section 4.6
- Scales the same core architecture across 124M, 355M, 774M, and 1,558M variants by increasing embedding sizes and repeated components. section 5.5
A forward pass and pretrained generation
Tokenizing “Every effort moves you” and “Every day holds a” produces a two-row batch with four token IDs per row. A randomly initialized GPTModel returns logits shaped torch.Size([2, 4, 50257]). After loading OpenAI’s 124M weights, generation with top_k=50, temperature=1.5, and 25 new tokens produces text beginning “Every effort moves you toward finding an ideal new way to practice something!” section 4.6 section 5.5
Figure 4.2 A GPT model. In addition to the embedding layers, it consists of one or more transformer blocks containing the masked multi-head attention module we previously implemented.
Use the architecture figure to trace token embeddings and positional embeddings through repeated transformer blocks to vocabulary logits. section 4.1
OpenAI’s released weights can be downloaded and assigned to the implementation after matching configuration settings and checking tensor shapes. The weights were originally saved with TensorFlow, while the model implementation uses PyTorch. section 5.5





