Illustration of what reasoning-from-scratch does: Code for a book that adds reasoning to a small Qwen3 model in PyTorch

rasbt /

reasoning-from-scratch

Code for a book that adds reasoning to a small Qwen3 model in PyTorch

Reasoning models like DeepSeek R1 are easy to misread if you only read about them in theory. This repository is the official code for the book Build a Reasoning Model (From Scratch). You start with a pre-trained open base LLM, Qwen3, and add reasoning skills yourself in PyTorch.

The chapters go in order. You load and run the model, then build an evaluation setup on MATH-500. Next come inference-time methods such as chain-of-thought prompting, self-consistency, best-of-N and self-refinement. After that you train with reinforcement learning (GRPO), improve it, and distill reasoning from a larger teacher model.

The code is for learning, so the model is small and the main chapters are meant to run on consumer hardware. It stands alone, though the author's earlier book covers building a base LLM. The repo follows a printed book, so changes to the main chapter code are not accepted as contributions.

What it does

  • Chapter notebooks with solutions

    Chapters 2 to 8 each have a main Jupyter notebook and a notebook with exercise solutions.

  • Inference-time scaling

    Covers chain-of-thought prompting, self-consistency, best-of-N and self-refinement, with scripts that test them on MATH-500.

  • Reinforcement learning with GRPO

    Shows how to train a reasoning model with GRPO, then improve it. Bonus scripts include DeepSeek-V3.2-, Olmo3- and GDPO-style training.

  • Distillation

    Generates teacher outputs through Ollama or OpenRouter and trains a smaller model on them, with single-example and batched scripts.

  • Checkpoints and extras

    Includes downloadable training checkpoints, Qwen3 source code, Hugging Face usage, evaluation methods and a chat interface.

What you could build

  • Learn how GRPO reinforcement learning trains a model to reason by running it yourself.
  • Compare chain-of-thought, self-consistency and self-refinement on MATH-500 problems.
  • Distill a larger teacher model's reasoning into a smaller model using Ollama or OpenRouter outputs.
  • Study a from-scratch Qwen3 implementation in PyTorch.
  • Build a simple chat interface around a model you trained.

Get started

Clone the repository, then set up Python using the tips in Chapter 2. Open the ch02 notebook and work through the chapters in order.

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

Commands are copied from the project's README. Read it on GitHub for the full setup.

More in Learning & resource lists

    ↑↓ to move, Enter to open, Esc to close