LLMs-from-scratch is the official code repository for the book "Build a Large Language Model (From Scratch)," authored by Sebastian Raschka, published by Manning, ISBN 9781633437166. The repository address is https://github.com/rasbt/LLMs-from-scratch .
Purpose: This project aims to help readers build, pretrain, and fine-tune a GPT-like large language model (LLM) from scratch step by step using PyTorch, and understand how large language models work. The methods in the book simulate the methods used to create large-scale foundation models such as ChatGPT, and also include code for loading larger pretrained model weights for fine-tuning.
Main capabilities: The repository organizes code by chapter, covering core aspects such as text data processing, attention mechanisms, GPT model implementation, pretraining on unlabeled data, classification fine-tuning, and instruction fine-tuning. Chapter 2 covers text data and byte pair encoding (BPE) tokenizers; Chapter 3 encodes attention mechanisms; Chapter 4 implements a GPT model from scratch and includes various attention alternatives and architectural variants, such as grouped-query attention, multi-head latent attention, sliding window attention, Gated DeltaNet, DeepSeek sparse attention, cross-layer KV sharing, mixture of experts (MoE), and more; Chapter 5 pretrains on unlabeled data and includes extensions such as converting GPT to Llama, extending the Tiktoken BPE tokenizer, and PyTorch performance optimization, and also provides bonus material for implementing multiple model architectures from scratch, including Llama 3.2, Qwen3 (dense and MoE), Gemma 3, Olmo 3, Tiny Aya, Qwen3.5, Gemma 4 E2B/E4B, and more; Chapter 6 performs classification fine-tuning; Chapter 7 performs instruction fine-tuning, including dataset generation, evaluating instruction responses using the OpenAI API and Ollama, generating preference datasets using Llama 3.1 70B and Ollama, and direct preference optimization (DPO) alignment, among others. In addition, the accompanying "Build A Reasoning Model (From Scratch)" repository also provides bonus material related to reasoning and evaluation, such as verifier evaluation (MATH-500), multiple-choice evaluation (MMLU), LLM leaderboard evaluation, LLM-as-a-Judge evaluation, self-consistency, self-refinement, from-scratch implementation of GRPO-based RLVR reinforcement learning, and more. Chapter 4 also includes topics such as FLOPs analysis and KV caching.
How to use: The repository code can be downloaded as a ZIP from GitHub or cloned using the git command. The book recommends using a Markdown editor or previewer to view README.md, or viewing it directly in the GitHub browserin, and the latter automatically renders Markdown. The README.md in the setup directory provides a guide to installing Python and packages and configuring the code environment. Each chapter contains multiple exercises, with solutions summarized in Appendix C; the corresponding code notebooks are located in each chapter's main folder, for example ./ch02/01_main-chapter-code/exercise-solutions.ipynb . The Manning website also provides a free 170-page PDF, "Test Yourself On Build a Large Language Model (From Scratch)," with about 30 quiz questions and answers per chapter to test understanding. The repository also includes a 17-hour 15-minute companion video course, organized by chapters and sections and corresponding to the book's structure, which can be used as a standalone alternative or supplementary resource.
Intended use: suitable for developers, students, and researchers with a solid foundation in Python programming who want to understand the internal mechanisms of LLMs in depth. If you have experience with deep neural networks, some concepts will be more familiar. The code uses PyTorch to implement everything from scratch and does not use any external LLM libraries; although proficiency in PyTorch is not required, familiarity with the basics will help, and Appendix A provides a brief introduction to PyTorch.
Limitations: the repository code corresponds to the printed book, and contributions that extend the main chapter code are currently not accepted, to avoid diverging from the physical book. The main chapter code is designed to run on an ordinary laptop in a reasonable amount of time without specialized hardware; if available, it will automatically use a GPU. The book uses PyTorch for implementation and does not depend on external LLM libraries. More details such as specific hardware configuration recommendations and full runtimes are not provided in the materials.