reasoning-from-scratch

rasbt/reasoning-from-scratch

Implement a reasoning LLM in PyTorch from scratch, step by step

4.7k
Stars
699
Forks
2
Open issues
61
Watchers
Jupyter Notebook Apache-2.0Last pushed Jul 6, 2026

Implement a reasoning LLM in PyTorch from scratch, step by step

Categories

Tags

Similar tools

Install

git clone https://github.com/rasbt/reasoning-from-scratch

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.



To download a copy of this repository, click on the Download ZIP button or execute the following command in your terminal:

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 (In Progress)

  • Troubleshooting Guide
Chapter TitleMain Code
Ch 1: Understanding reasoning ModelsNo 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]