# rasbt/reasoning-from-scratch architecture

> Architecture diagram of the GitHub repository rasbt/reasoning-from-scratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rasbt/reasoning-from-scratch
- Repository: https://github.com/rasbt/reasoning-from-scratch

## Overview

This is the companion code for a hands-on book about building a small reasoning LLM from a pretrained Qwen3 model. The principal workflow is model inference and evaluation, followed by inference-time scaling or reinforcement-learning training; generated reasoning data can also support distillation, and trained models can be used through Hugging Face. The graph preserves these documented branches and the chat and evaluation examples. Some relationships are only described at the book level, so those links are dashed; unsampled implementations are represented by their exact enclosing directories.

Main entry points: [`reasoning_from_scratch/ch02.py`](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch02.py) (Text Generation), [`reasoning_from_scratch/ch04.py`](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch04.py) (Inference Scaling), [`reasoning_from_scratch/ch06.py`](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch06.py) (RLVR Methods), [`ch03/02_math500-verifier-scripts`](https://github.com/rasbt/reasoning-from-scratch/tree/main/ch03/02_math500-verifier-scripts) (MATH-500 Evaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Model Runtime"]
  node_qwen["Qwen3 Model<br/>[qwen3.py]"]
  node_qwen_batch["Batched Qwen3<br/>[qwen3_batched.py]"]
  node_qwen_opt["Optimized Qwen3<br/>[qwen3_optimized.py]"]
  node_generation["Text Generation<br/>[ch02.py]"]
end

subgraph group_reasoning["Reasoning Methods"]
  node_scaling["Inference Scaling<br/>[ch04.py]"]
  node_refinement["Self Refinement<br/>[ch05.py]"]
end

subgraph group_training["Training"]
  node_rl_methods["RLVR Methods<br/>[ch06.py]"]
  node_grpo["GRPO Training"]
  node_grpo_fsdp["Distributed GRPO"]
  node_grpo_advanced["Advanced GRPO"]
end

subgraph group_deployment["Data And Deployment"]
  node_data_generation["Reasoning Data Generation"]
  node_distillation["Distillation Training<br/>[distill_batched.py]"]
  node_hf_export["Hugging Face Export<br/>[hf_export.py]"]
  node_hf_wrapper["Hugging Face Wrapper<br/>[hf_wrapper.py]"]
end

subgraph group_interfaces["Evaluation And Interfaces"]
  node_eval_utils["Evaluation Utilities<br/>[ch03.py]"]
  node_parser["Advanced Parser<br/>[parser.py]"]
  node_math_eval["MATH-500 Evaluation"]
  node_benchmark_eval["LLM Benchmarks"]
  node_chat["Chat Interfaces"]
end

node_reader(("Reader"))
node_provider(("Model Providers"))

node_reader -.->|"runs inference"| node_generation
node_generation -->|"runs model"| node_qwen
node_generation -->|"supports batching"| node_qwen_batch
node_generation -->|"benchmarks variant"| node_qwen_opt
node_reader -.->|"evaluates answers"| node_math_eval
node_math_eval -->|"uses utilities"| node_eval_utils
node_math_eval -->|"uses parser"| node_parser
node_reader -.->|"scales inference"| node_scaling
node_scaling -->|"uses generation"| node_generation
node_scaling -->|"extracts answers"| node_eval_utils
node_reader -.->|"refines responses"| node_refinement
node_refinement -->|"uses scaling methods"| node_scaling
node_reader -.->|"trains model"| node_grpo
node_grpo -->|"uses RL methods"| node_rl_methods
node_grpo_fsdp -->|"uses RL methods"| node_rl_methods
node_grpo_advanced -->|"uses RL methods"| node_rl_methods
node_rl_methods -->|"grades answers"| node_eval_utils
node_provider -.->|"supplies completions"| node_data_generation
node_reader -.->|"generates data"| node_data_generation
node_reader -.->|"distills model"| node_distillation
node_distillation -->|"trains batched model"| node_qwen_batch
node_reader -.->|"exports model"| node_hf_export
node_reader -.->|"uses model"| node_hf_wrapper
node_reader -.->|"compares models"| node_benchmark_eval
node_reader -.->|"chats with model"| node_chat
node_chat -->|"uses model"| node_qwen
node_hf_wrapper -->|"wraps model"| node_qwen

click node_qwen "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3.py"
click node_qwen_batch "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3_batched.py"
click node_qwen_opt "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3_optimized.py"
click node_generation "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch02.py"
click node_eval_utils "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch03.py"
click node_parser "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/bonus/parser.py"
click node_scaling "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch04.py"
click node_refinement "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch05.py"
click node_rl_methods "https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch06.py"
click node_grpo "https://github.com/rasbt/reasoning-from-scratch/blob/main/ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl.py"
click node_grpo_fsdp "https://github.com/rasbt/reasoning-from-scratch/blob/main/ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl_batched_fsdp.py"
click node_grpo_advanced "https://github.com/rasbt/reasoning-from-scratch/tree/main/ch07/03_rlvr_grpo_scripts_advanced"
click node_data_generation "https://github.com/rasbt/reasoning-from-scratch/tree/main/ch08/02_generate_distillation_data"
click node_distillation "https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/04_train_with_distillation/distill_batched.py"
click node_hf_export "https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/06_use_via_huggingface/export_approach/hf_export.py"
click node_hf_wrapper "https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/06_use_via_huggingface/wrapper_approach/hf_wrapper.py"
click node_math_eval "https://github.com/rasbt/reasoning-from-scratch/tree/main/ch03/02_math500-verifier-scripts"
click node_benchmark_eval "https://github.com/rasbt/reasoning-from-scratch/tree/main/chF"
click node_chat "https://github.com/rasbt/reasoning-from-scratch/tree/main/chG/01_main-chapter-code"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_qwen,node_qwen_batch,node_qwen_opt,node_generation toneBlue
class node_scaling,node_refinement toneAmber
class node_rl_methods,node_grpo,node_grpo_fsdp,node_grpo_advanced toneMint
class node_data_generation,node_distillation,node_hf_export,node_hf_wrapper toneRose
class node_eval_utils,node_parser,node_math_eval,node_benchmark_eval,node_chat,node_reader,node_provider toneIndigo
```

## Components

### Model Runtime

- [Qwen3 Model](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3.py): `reasoning_from_scratch/qwen3.py`
- [Batched Qwen3](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3_batched.py): `reasoning_from_scratch/qwen3_batched.py`
- [Optimized Qwen3](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/qwen3_optimized.py): `reasoning_from_scratch/qwen3_optimized.py`
- [Text Generation](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch02.py): `reasoning_from_scratch/ch02.py`

### Reasoning Methods

- [Inference Scaling](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch04.py): `reasoning_from_scratch/ch04.py`
- [Self Refinement](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch05.py): `reasoning_from_scratch/ch05.py`

### Training

- [RLVR Methods](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch06.py): `reasoning_from_scratch/ch06.py`
- [GRPO Training](https://github.com/rasbt/reasoning-from-scratch/blob/main/ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl.py): `ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl.py`
- [Distributed GRPO](https://github.com/rasbt/reasoning-from-scratch/blob/main/ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl_batched_fsdp.py): `ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl_batched_fsdp.py`
- [Advanced GRPO](https://github.com/rasbt/reasoning-from-scratch/tree/main/ch07/03_rlvr_grpo_scripts_advanced): `ch07/03_rlvr_grpo_scripts_advanced`

### Data And Deployment

- [Reasoning Data Generation](https://github.com/rasbt/reasoning-from-scratch/tree/main/ch08/02_generate_distillation_data): `ch08/02_generate_distillation_data`
- [Distillation Training](https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/04_train_with_distillation/distill_batched.py): `ch08/04_train_with_distillation/distill_batched.py`
- [Hugging Face Export](https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/06_use_via_huggingface/export_approach/hf_export.py): `ch08/06_use_via_huggingface/export_approach/hf_export.py`
- [Hugging Face Wrapper](https://github.com/rasbt/reasoning-from-scratch/blob/main/ch08/06_use_via_huggingface/wrapper_approach/hf_wrapper.py): `ch08/06_use_via_huggingface/wrapper_approach/hf_wrapper.py`

### Evaluation And Interfaces

- [Evaluation Utilities](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/ch03.py): `reasoning_from_scratch/ch03.py`
- [Advanced Parser](https://github.com/rasbt/reasoning-from-scratch/blob/main/reasoning_from_scratch/bonus/parser.py): `reasoning_from_scratch/bonus/parser.py`
- [MATH-500 Evaluation](https://github.com/rasbt/reasoning-from-scratch/tree/main/ch03/02_math500-verifier-scripts): `ch03/02_math500-verifier-scripts`
- [LLM Benchmarks](https://github.com/rasbt/reasoning-from-scratch/tree/main/chF): `chF`
- [Chat Interfaces](https://github.com/rasbt/reasoning-from-scratch/tree/main/chG/01_main-chapter-code): `chG/01_main-chapter-code`

### Other components

- **Reader**
- **Model Providers**

## Connections

- Reader → Text Generation: runs inference (evidence: `README.md`)
- Text Generation → Qwen3 Model: runs model (evidence: `reasoning_from_scratch/ch02.py`)
- Text Generation → Batched Qwen3: supports batching (evidence: `ch03/02_math500-verifier-scripts/evaluate_math500_batched.py`)
- Text Generation → Optimized Qwen3: benchmarks variant (evidence: `ch02/03_optimized-LLM/compare_inference.py`)
- Reader → MATH-500 Evaluation: evaluates answers (evidence: `README.md`)
- MATH-500 Evaluation → Evaluation Utilities: uses utilities (evidence: `ch03/02_math500-verifier-scripts/evaluate_math500_batched.py`)
- MATH-500 Evaluation → Advanced Parser: uses parser
- Reader → Inference Scaling: scales inference (evidence: `README.md`)
- Inference Scaling → Text Generation: uses generation (evidence: `reasoning_from_scratch/ch04.py`)
- Inference Scaling → Evaluation Utilities: extracts answers (evidence: `ch04/02_math500-inference-scaling-scripts/self_consistency_math500.py`)
- Reader → Self Refinement: refines responses (evidence: `README.md`)
- Self Refinement → Inference Scaling: uses scaling methods (evidence: `reasoning_from_scratch/ch05.py`)
- Reader → GRPO Training: trains model (evidence: `README.md`)
- GRPO Training → RLVR Methods: uses RL methods (evidence: `ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl.py`)
- Distributed GRPO → RLVR Methods: uses RL methods (evidence: `ch06/02_rlvr_grpo_scripts_intro/rlvr_grpo_original_no_kl_batched_fsdp.py`)
- Advanced GRPO → RLVR Methods: uses RL methods (evidence: `ch07/03_rlvr_grpo_scripts_advanced/7_6_plus_format_reward.py`)
- RLVR Methods → Evaluation Utilities: grades answers (evidence: `reasoning_from_scratch/ch06.py`)
- Model Providers → Reasoning Data Generation: supplies completions
- Reader → Reasoning Data Generation: generates data (evidence: `README.md`)
- Reader → Distillation Training: distills model (evidence: `README.md`)
- Distillation Training → Batched Qwen3: trains batched model (evidence: `ch08/04_train_with_distillation/distill_batched.py`)
- Reader → Hugging Face Export: exports model (evidence: `README.md`)
- Reader → Hugging Face Wrapper: uses model (evidence: `README.md`)
- Reader → LLM Benchmarks: compares models (evidence: `README.md`)
- Reader → Chat Interfaces: chats with model (evidence: `README.md`)
- Chat Interfaces → Qwen3 Model: uses model (evidence: `chG/01_main-chapter-code/qwen3_chat_interface_multiturn.py`)
- Hugging Face Wrapper → Qwen3 Model: wraps model (evidence: `ch08/06_use_via_huggingface/wrapper_approach/hf_wrapper.py`)
