rasbt /reasoning-from-scratch
Implement a reasoning LLM in PyTorch from scratch, step by step
★ 5,840⑂ 947👁 67⑃ 200● 58▤ 1
+9
日增长
5,840
stars
947
forks
67
watchers
200
prs
58
issues
1
releases
README
Build A Reasoning Model (From Scratch) This repository contains the code for developing an LLM reasoning model and is the official code repository for the book Build a Reasoning Model (From Scratch). (Printed in color.) In Build a Reasoning Model (From Scratch), you will learn and understand how a reasoning large language model (LLM) works. Reasoning is one of the most exciting and important recent advances in improving LLMs, but it’s also one of the easiest to misunderstand if you only hear the term reasoning and read about it in theory. This is why this book takes a hands-on approach. We will start with a pre-trained base LLM and then add reasoning capabilities ourselves, step by step in code, so you can see exactly how it works. The methods described in this book walk you through the process of developing your own small-but-functional reasoning model for educational purposes. It mirrors the approaches used in creating large-scale reasoning models such as DeepSeek R1, GPT-5 Thinking, and others. In addition, this book includes code for loading the weights of existing, pretrained models. - Link to the official source code repository - Link to the book at Manning (the publisher's website) - Link to the book page on Amazon.com - ISBN 9781633434677 To download a copy of this repository, click on the Download ZIP button or execute the following command in your terminal: bash git clone --depth 1 https://github.com/rasbt/reasoning-from-scratch.git Tip: Chapter 2 provides additional tips on installing Python, managing Python packages, and setting up your coding environment. Table of Contents - Troubleshooting Guide | Chapter Title | Main Code | | ----------------------------------------------------------- | ------------------------------------------------------------ | | Ch 1: Understanding reasoning Models | No code | | Ch 2: Generating Text with a Pre-trained LLM | - ch02_main.ipynb- ch02_exercise-solutions.ipynb | | Ch 3: Evaluating Reasoning Models | - ch03_main.ipynb- ch03_exercise-solutions.ipynb | | Ch 4: Improving Reasoning with Inference-Time Scaling | - ch04_main.ipynb- ch04_exercise-solutions.ipynb | | Ch 5: Inference-Time Scaling via Self-Refinement | - ch05_main.ipynb- ch05_exercise-solutions.ipynb | | Ch 6: Training Reasoning Models with Reinforcement Learning | - ch06_main.ipynb- ch06_exercise-solutions.ipynb | | Ch 7: Improving GRPO for Reinforcement Learning | - ch07_main.ipynb- ch07_exercise-solutions.ipynb | | Ch 8: Distilling Reasoning Models for Efficient Reasoning | - ch08_main.ipynb- ch08_exercise-solutions.ipynb | | Appendix A: References and Further Reading | No code | | Appendix
Top issues
#226Always crashed when running rlvr_grpo_original_no_kl_batched_fsdp.py6 comments · 2026-03-27#235CUDA out of memory - nV RTX 3060 laptop GPU - time to move to Colab?4 comments · 2026-04-29
Discussions
#278How to customize pyproject.toml torch marker string for windows?2 comments · 2026-10-26#276Possible inconsistency in Figure 4.150 comments · 2026-10-08#270Supervised post training in Chapter 6 (RLVR GPRO)1 comments · 2026-09-26#268Appendix C errata2 comments · 2026-09-22#266Looped / Latent Reasoning Chapter?1 comments · 2026-09-17#265Fig 7.18 errata1 comments · 2026-09-15#260reasoning-from-scratch and NVIDIA Thor GPU2 comments · 2026-08-26#258How to categorize all stages of building a LLM1 comments · 2026-08-13
Latest release
v1.0 v1.0 2026-05-18State of the book code as it goes to print. This release captures the final version of the code accompanying the print edition. (Future updates may include small fixes, compatibility updates, or clarifications, but this tag marks the stable reference point for readers following along with the book.)