What it is
This is the official code repository for the book Build a Large Language Model (From Scratch). It walks through coding, pretraining and finetuning a GPT-like LLM step by step in PyTorch, without external LLM libraries. The book's method for a small educational model mirrors the approach used for large foundational models such as those behind ChatGPT, and the code also loads weights of larger pretrained models for finetuning.
Who it's for
- Python programmers who want to understand how LLMs work from the inside out by coding them
- Readers of the book who want the official chapter code, exercise solutions and bonus material
- Learners with some deep neural network experience, or PyTorch beginners who can use Appendix A as an introduction
Requirements
Requirements
- A strong foundation in Python programming
- Familiarity with PyTorch basics is useful but not required (Appendix A gives an introduction)
- Conventional laptop is sufficient for the main chapter code; GPUs are used automatically if available
- Git, to clone the repository (or download the ZIP)
Setup
Clone the repository
Download a copy of the repository with the following command, or use the Download ZIP button on GitHub.
bashgit clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.gitSet up Python and packages
The README points to the README.md in the setup directory for guidance on installing Python and Python packages and setting up the code environment. Bonus setup folders cover Python setup tips, installing the libraries used in the book, and a Docker environment guide.
Examples
Chapter 2: Working with text data
textch02/01_main-chapter-code/ch02.ipynb
ch02/01_main-chapter-code/dataloader.ipynbWhat it does: Main notebooks for Chapter 2, listed in the README's chapter table, plus a summary dataloader notebook.
Chapter 4: Implementing a GPT model
textch04/01_main-chapter-code/ch04.ipynb
ch04/01_main-chapter-code/gpt.pyWhat it does: The chapter notebook for building a GPT model from scratch, and gpt.py as the summary script.
Chapter 5: Pretraining and generation
textch05/01_main-chapter-code/ch05.ipynb
ch05/01_main-chapter-code/gpt_train.py
ch05/01_main-chapter-code/gpt_generate.pyWhat it does: Pretraining on unlabeled data: the chapter notebook plus summary scripts for training and generation.
Chapter 7: Instruction finetuning
textch07/01_main-chapter-code/ch07.ipynb
ch07/01_main-chapter-code/gpt_instruction_finetuning.py
ch07/01_main-chapter-code/ollama_evaluate.pyWhat it does: Finetuning to follow instructions, with scripts for finetuning and for evaluating responses via Ollama.
Pros & cons
Pros
- Pro:Official code for the book, covering the full path from text data and attention to pretraining and finetuning
- Pro:Main chapter code runs on conventional laptops and automatically uses GPUs if available
- Pro:Extensive bonus material (e.g., KV cache, GQA, MoE, Llama 3.2, Qwen3, Gemma 3, DPO, LoRA)
- Pro:Supporting resources include exercise solutions, a companion video course and a free quiz PDF
Cons
- Con:Contributions that extend the main chapter code are not accepted, since it must match the print book
- Con:Designed as companion code for a book, so it is best used alongside the text
- Con:Main-chapter model is small and educational in scale; larger models require loading pretrained weights
Images
