Pretraining is the initial stage of LLM development: a model learns broad language patterns from a large, diverse corpus by predicting the next token. The resulting foundation model can then be fine-tuned for narrower tasks. section 1.3 section 1.8
GPT-like pretraining connects data preparation, a decoder-based transformer, attention, token prediction, and weight optimization. Large datasets expose the model to syntax, semantics, context, and general knowledge across many topics and languages. section 1.5 section 1.8
A practical pretraining workflow includes:
- Prepare tokenized input and target batches from unlabeled text. section 1.8 section 5.0
- Iterate over batches and epochs, reset gradients, calculate cross-entropy loss, backpropagate, and update weights with AdamW. section 5.2 section 5.6
- Evaluate training and validation losses, generate text samples, and monitor divergence as a signal of overfitting. section 5.2
- Save, resume, or load openly available weights when full-scale training is impractical. section 5.0 section 5.6
Small GPT-like training run
A PyTorch run initializes a GPTModel, moves it to a device, configures AdamW, and calls train_model_simple for ten epochs. With the prompt “Every effort moves you,” generated text changes from repeated punctuation and words to coherent, though sometimes memorized, passages. Training loss continues falling while validation loss stagnates, indicating overfitting.
Figure 1.3 Pretraining an LLM involves next-word prediction on large text datasets. A pretrained LLM can then be fine-tuned using a smaller labeled dataset.
Shows the two-stage flow from next-word prediction on large text datasets to fine-tuning on a smaller labeled dataset. section 1.3













