109.2K ·GitHub
What is this?
This is the official code repository for the book 'Build a Large Language Model (From Scratch)', which teaches you to implement a GPT-like LLM in PyTorch step by step, from tokenization through pretraining and finetuning.
Is this a library I import, or a set of tutorial notebooks?
It's a collection of Jupyter notebooks and Python scripts organized by book chapter, meant to be read and run alongside the book rather than installed as a library.
How do I start working through the code?
Download or clone the repo, set up Python using the instructions in the setup directory, then work through the chapter folders in order (ch02 through ch07, plus appendices).
Do I need prior deep learning experience to follow along?
The main prerequisite is solid Python knowledge; prior experience with deep neural networks and PyTorch basics helps but isn't required, since Appendix A introduces PyTorch from scratch.
What topics does the book cover chapter by chapter?
It covers working with text data and tokenization, coding attention mechanisms, building a GPT model from scratch, pretraining on unlabeled data, finetuning for text classification, and finetuning to follow instructions, with appendices on PyTorch, exercise solutions, and LoRA finetuning.
Is there a video version of the book?
Yes, there's a companion video course that is 17 hours and 15 minutes long, organized to mirror the book's chapter structure as a standalone or complementary resource.
Is there a follow-up book?
Yes, 'Build A Reasoning Model (From Scratch)' is a standalone sequel that starts from a pretrained model and implements reasoning techniques like inference-time scaling, reinforcement learning, and distillation.
What extra bonus material is included beyond the core chapters?
Bonus folders cover topics like BPE tokenizer implementations, KV cache, Grouped-Query and Multi-Head Latent Attention, Mixture-of-Experts, converting GPT to Llama, Direct Preference Optimization, and from-scratch implementations of Llama 3.2, Qwen3, and Gemma 3.
What are similar projects?
how-to-train-your-gpt, train-llm-from-scratch, minGPT, nn-zero-to-hero, OpenArch, llm-architecture-from-scratch, nlp_course, ai-engineering-from-scratch
Which topics is it in?
finetuning, instruction-tuning, tokenizer, pretraining, from-scratch, language-model, attention-mechanism, natural-language-processing, lora, transformers, gpt, jupyter notebook