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