# huggingface/open-r1 architecture

> Architecture diagram of the GitHub repository huggingface/open-r1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/huggingface/open-r1
- Repository: https://github.com/huggingface/open-r1

## Overview

1. **What this repo is**
   Open R1 is an end-to-end training and data-generation pipeline for reproducing DeepSeek-R1-style reasoning models. The repository is organized around three core workflows: supervised fine-tuning, GRPO reinforcement learning, and synthetic data generation.

2. **Primary execution entrypoints**
   The main runtime lives in `src/open_r1/`:
   - `sft.py` for supervised fine-tuning
   - `grpo.py` for GRPO training
   - `generate.py` for synthetic trace generation  
   These are the scripts launched directly by Accelerate, Slurm, or local commands.

3. **Training pipeline boundary**
   Training code depends on model/dataset configuration plus distributed runtime setup. The `recipes/accelerate_configs/` files define the distributed backend shape: DDP, ZeRO-2, or ZeRO-3. The `recipes/<model>/<task>/config*.yaml` files define per-run training settings, dataset references, chat templates, and output locations.

4. **SFT flow**
   SFT is the simplest path: load a base model, load a Hugging Face dataset or dataset mixture, tokenize with the chosen chat template/EOS rules, train, then push the resulting checkpoint to the Hugging Face Hub. This path is used for distillation recipes like `OpenR1-Distill-7B` and the model-specific SFT configs in `recipes/`.

5. **GRPO flow**
   GRPO is the more complex training subsystem. It couples:
   - the training process in `grpo.py`
   - a generation backend, usually vLLM
   - reward functions in `src/open_r1/rewards.py`
   - optional sandboxed code execution for verifiable tasks  
   The policy generates candidate responses, rewards are computed, and the trainer updates the model. This is the main reinforcement-learning boundary in the repo.

6. **Code-verification subsystem**
   Competitive-programming and code-execution rewards live under `src/open_r1/utils/competitive_programming/`. This includes scoring logic for Codeforces/IOI-style tasks, code patching/utilities, and provider adapters for sandboxes. The repo supports multiple execution backends:
   - E2B
   - Morph
   - Piston  
   For larger deployments, router services in `scripts/` and `slurm/` centralize sandbox access so many training jobs can share the same execution pool.

7. **Data and dataset management**
   `src/open_r1/utils/data.py` and related helpers support dataset loading, filtering, and mixture construction. The repo also includes dedicated dataset-filtering recipes under `recipes/dataset_filtering/`, plus pass-rate filtering scripts in `scripts/pass_rate_filtering/`. There is also a decontamination script (`scripts/decontaminate.py`) that removes benchmark overlap before publishing datasets.

8. **Synthetic data generation**
   `generate.py` is used to produce reasoning traces from a model and push them back to the Hugging Face Hub. The documented generation path uses Distilabel with vLLM, often at larger scale via Ray/Slurm for DeepSeek-R1. Architecturally, this is a model-inference-to-dataset pipeline, not training.

9. **Evaluation subsystem**
   Model evaluation is externalized from training and runs through LightEval with vLLM. The repo’s `scripts/run_benchmarks.py` and `scripts/benchmark_e2b.py` orchestrate benchmark execution, while `scripts/upload_details.py` handles result publication. The evaluation path is meant to validate trained checkpoints against AIME, MATH, GPQA, and LiveCodeBench-style tasks.

10. **Cluster orchestration layer**
    Slurm scripts in `slurm/` wrap the major workflows: training, generation, evaluation, and sandbox router deployment. This is the repo’s deployment layer for multi-node execution, especially for GRPO where one node may host vLLM and the others run training.

11. **Configuration and reproducibility**
    The `recipes/` tree is the main reproducibility surface. It captures exact model/task recipes, accelerate settings, and specialized configs for code, math, and distillation runs. The README’s examples show that most behavior changes come from config swaps rather than code changes.

12. **External systems and persistence**
    The repo is built around a few important external boundaries:
    - Hugging Face Hub for datasets and model publishing
    - Weights & Biases for experiment logging
    - vLLM for fast generation during training/evaluation
    - Accelerate/TRL for distributed training orchestration
    - sandbox providers for code execution  
    Local artifacts are kept under `logs/` and `data/`-style output directories, while released models and datasets are pushed outward to the Hub.

13. **Testing and quality gates**
    `tests/` covers reward logic and data utilities, with slow tests for code rewards. CI is defined in `.github/workflows/tests.yml`. This suggests the highest-risk code paths are the reward functions and dataset plumbing rather than model training itself.

14. **How the pieces connect**
    The overall architecture is: curated datasets or synthetic traces enter training; SFT or GRPO produces checkpoints; evaluation validates them; generation and decontamination produce new datasets; and the resulting artifacts are published back to the Hub. The core boundary is between orchestration/configuration on one side and heavyweight distributed inference/training/sandbox execution on the other.

Main entry points: [`src/open_r1/grpo.py`](https://github.com/huggingface/open-r1/blob/main/src/open_r1/grpo.py) (GRPO), [`src/open_r1/sft.py`](https://github.com/huggingface/open-r1/blob/main/src/open_r1/sft.py) (SFT), [`src/open_r1/generate.py`](https://github.com/huggingface/open-r1/blob/main/src/open_r1/generate.py) (Generate), [`src/open_r1/utils/competitive_programming/utils.py`](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/utils.py) (Code rewards)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_sft["SFT<br/>training entrypoint<br/>[sft.py]"]
  node_grpo["GRPO<br/>rl training entrypoint<br/>[grpo.py]"]
  node_generate["Generate<br/>inference entrypoint<br/>[generate.py]"]
  node_rewards(("Rewards<br/>reward logic<br/>[rewards.py]"))
  node_data_utils["Data utils<br/>dataset plumbing<br/>[data.py]"]
  node_eval_utils["Eval utils<br/>evaluation helpers<br/>[evaluation.py]"]
  node_hub_utils["Hub utils<br/>publication helpers<br/>[hub.py]"]
  node_wandb["W&B logging<br/>logging adapter<br/>[wandb_logging.py]"]
  node_code_providers["Code providers<br/>sandbox adapters<br/>[code_providers.py]"]
  node_cp_utils[("Code rewards<br/>competitive programming<br/>[utils.py]")]
  node_morph_client["Morph client<br/>sandbox client<br/>[morph_client.py]"]
  node_piston_client["Piston client<br/>sandbox client<br/>[piston_client.py]"]
end

subgraph group_recipes["Recipes"]
  node_accelerate_cfgs["Accelerate cfgs<br/>distributed configs<br/>[ddp.yaml]"]
  node_model_recipes["Model recipes<br/>run configs<br/>[README.md]"]
  node_filter_recipes["Filter recipes<br/>data configs<br/>[config_demo.yaml]"]
end

subgraph group_ops["Operations"]
  node_train_ops["Train ops<br/>orchestration scripts<br/>[train.slurm]"]
  node_eval_ops["Eval ops<br/>benchmark scripts<br/>[run_benchmarks.py]"]
  node_generation_ops["Gen ops<br/>generation scripts"]
  node_router_ops["Router ops<br/>sandbox routing<br/>[e2b_router.py]"]
  node_filter_ops["Filter ops<br/>data filtering scripts"]
end

subgraph group_quality["Quality"]
  node_tests["Tests<br/>quality gates<br/>[test_rewards.py]"]
end

node_sft -->|"loads data"| node_data_utils
node_sft -->|"publishes checkpoint"| node_hub_utils
node_sft -->|"logs metrics"| node_wandb
node_sft -->|"configured by"| node_model_recipes
node_sft -->|"runs under"| node_accelerate_cfgs
node_grpo -->|"scores outputs"| node_rewards
node_grpo -->|"executes code"| node_code_providers
node_grpo -->|"uses"| node_cp_utils
node_grpo -->|"loads data"| node_data_utils
node_grpo -->|"publishes checkpoint"| node_hub_utils
node_grpo -->|"logs metrics"| node_wandb
node_grpo -->|"configured by"| node_model_recipes
node_grpo -->|"runs under"| node_accelerate_cfgs
node_generate -->|"publishes traces"| node_hub_utils
node_generate -->|"configured by"| node_model_recipes
node_generate -->|"logs metrics"| node_wandb
node_cp_utils -->|"adapts"| node_morph_client
node_cp_utils -->|"adapts"| node_piston_client
node_router_ops -->|"routes sandboxes"| node_code_providers
node_train_ops -->|"launches"| node_sft
node_train_ops -->|"launches"| node_grpo
node_generation_ops -->|"launches"| node_generate
node_eval_ops -->|"orchestrates"| node_eval_utils
node_eval_ops -->|"uploads results"| node_hub_utils
node_filter_ops -->|"filters data"| node_data_utils
node_filter_ops -->|"configured by"| node_filter_recipes
node_tests -->|"verifies"| node_rewards
node_tests -->|"verifies"| node_data_utils
node_tests -->|"covers"| node_cp_utils

click node_sft "https://github.com/huggingface/open-r1/blob/main/src/open_r1/sft.py"
click node_grpo "https://github.com/huggingface/open-r1/blob/main/src/open_r1/grpo.py"
click node_generate "https://github.com/huggingface/open-r1/blob/main/src/open_r1/generate.py"
click node_rewards "https://github.com/huggingface/open-r1/blob/main/src/open_r1/rewards.py"
click node_data_utils "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/data.py"
click node_eval_utils "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/evaluation.py"
click node_hub_utils "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/hub.py"
click node_wandb "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/wandb_logging.py"
click node_code_providers "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/code_providers.py"
click node_cp_utils "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/utils.py"
click node_morph_client "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/morph_client.py"
click node_piston_client "https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/piston_client.py"
click node_accelerate_cfgs "https://github.com/huggingface/open-r1/blob/main/recipes/accelerate_configs/ddp.yaml"
click node_model_recipes "https://github.com/huggingface/open-r1/blob/main/recipes/README.md"
click node_filter_recipes "https://github.com/huggingface/open-r1/blob/main/recipes/dataset_filtering/config_demo.yaml"
click node_train_ops "https://github.com/huggingface/open-r1/blob/main/slurm/train.slurm"
click node_eval_ops "https://github.com/huggingface/open-r1/blob/main/scripts/run_benchmarks.py"
click node_generation_ops "https://github.com/huggingface/open-r1/blob/main/scripts/generate_reasoning.py"
click node_router_ops "https://github.com/huggingface/open-r1/blob/main/scripts/e2b_router.py"
click node_filter_ops "https://github.com/huggingface/open-r1/blob/main/scripts/pass_rate_filtering/compute_pass_rate.py"
click node_tests "https://github.com/huggingface/open-r1/blob/main/tests/test_rewards.py"

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_sft,node_grpo,node_generate,node_rewards,node_data_utils,node_eval_utils,node_hub_utils,node_wandb,node_code_providers,node_cp_utils,node_morph_client,node_piston_client toneBlue
class node_accelerate_cfgs,node_model_recipes,node_filter_recipes toneAmber
class node_train_ops,node_eval_ops,node_generation_ops,node_router_ops,node_filter_ops toneMint
class node_tests toneRose
```

## Components

### Core runtime

Training, generation, rewards, and shared utilities under src/open_r1.

- [SFT](https://github.com/huggingface/open-r1/blob/main/src/open_r1/sft.py): training entrypoint · Supervised fine-tuning runtime launched by Accelerate or Slurm. · `src/open_r1/sft.py`
- [GRPO](https://github.com/huggingface/open-r1/blob/main/src/open_r1/grpo.py): rl training entrypoint · Reinforcement-learning trainer that couples policy updates with reward computation and generation. · `src/open_r1/grpo.py`
- [Generate](https://github.com/huggingface/open-r1/blob/main/src/open_r1/generate.py): inference entrypoint · Synthetic trace generation pipeline for producing reasoning data. · `src/open_r1/generate.py`
- [Rewards](https://github.com/huggingface/open-r1/blob/main/src/open_r1/rewards.py): reward logic · Reward functions used by GRPO, including verifiable-task scoring hooks. · `src/open_r1/rewards.py`
- [Data utils](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/data.py): dataset plumbing · Dataset loading, filtering, and mixture construction helpers. · `src/open_r1/utils/data.py`
- [Eval utils](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/evaluation.py): evaluation helpers · Shared evaluation helpers used by benchmarking and validation flows. · `src/open_r1/utils/evaluation.py`
- [Hub utils](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/hub.py): publication helpers · Helpers for pushing models and datasets to the Hugging Face Hub. · `src/open_r1/utils/hub.py`
- [W&B logging](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/wandb_logging.py): logging adapter · Experiment logging integration for training and evaluation runs. · `src/open_r1/utils/wandb_logging.py`
- [Code providers](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/code_providers.py): sandbox adapters · Abstraction layer for external code-execution backends. · `src/open_r1/utils/code_providers.py`
- [Code rewards](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/utils.py): competitive programming · Competitive-programming scoring, patching, and sandbox client utilities. · `src/open_r1/utils/competitive_programming/utils.py`
- [Morph client](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/morph_client.py): sandbox client · Client adapter for Morph-backed code execution. · `src/open_r1/utils/competitive_programming/morph_client.py`
- [Piston client](https://github.com/huggingface/open-r1/blob/main/src/open_r1/utils/competitive_programming/piston_client.py): sandbox client · Client adapter for Piston-backed code execution. · `src/open_r1/utils/competitive_programming/piston_client.py`

### Recipes

Reproducible run configurations for models, tasks, and distributed settings.

- [Accelerate cfgs](https://github.com/huggingface/open-r1/blob/main/recipes/accelerate_configs/ddp.yaml): distributed configs · DDP, FSDP, ZeRO-2, and ZeRO-3 backend shapes for training runs. · `recipes/accelerate_configs/ddp.yaml`
- [Model recipes](https://github.com/huggingface/open-r1/blob/main/recipes/README.md): run configs · Per-model SFT and GRPO YAML recipes defining datasets, templates, and outputs. · `recipes/README.md`
- [Filter recipes](https://github.com/huggingface/open-r1/blob/main/recipes/dataset_filtering/config_demo.yaml): data configs · Dataset filtering configurations for pass-rate and decontamination-oriented workflows. · `recipes/dataset_filtering/config_demo.yaml`

### Operations

Scripts and Slurm entrypoints for evaluation, filtering, routers, and deployment.

- [Train ops](https://github.com/huggingface/open-r1/blob/main/slurm/train.slurm): orchestration scripts · Slurm and shell entrypoints for training and clustered execution. · `slurm/train.slurm`
- [Eval ops](https://github.com/huggingface/open-r1/blob/main/scripts/run_benchmarks.py): benchmark scripts · Benchmark orchestration and result publishing scripts. · `scripts/run_benchmarks.py`
- [Gen ops](https://github.com/huggingface/open-r1/blob/main/scripts/generate_reasoning.py): generation scripts · Scripts for trace generation and related scaling workflows. · `scripts/generate_reasoning.py`
- [Router ops](https://github.com/huggingface/open-r1/blob/main/scripts/e2b_router.py): sandbox routing · Router and worker deployment for shared sandbox execution pools. · `scripts/e2b_router.py`
- [Filter ops](https://github.com/huggingface/open-r1/blob/main/scripts/pass_rate_filtering/compute_pass_rate.py): data filtering scripts · Pass-rate filtering and benchmark decontamination utilities. · `scripts/pass_rate_filtering/compute_pass_rate.py`

### Quality

Tests and CI coverage for reward and data paths.

- [Tests](https://github.com/huggingface/open-r1/blob/main/tests/test_rewards.py): quality gates · Reward and data utility tests, including slow code-reward coverage. · `tests/test_rewards.py`

## Connections

- SFT → Data utils: loads data · SFT consumes curated datasets and mixtures.
- SFT → Hub utils: publishes checkpoint · Finished checkpoints are pushed to the Hub.
- SFT → W&B logging: logs metrics · Training runs emit experiment metrics.
- SFT → Model recipes: configured by · Recipe YAMLs define model, dataset, and output settings.
- SFT → Accelerate cfgs: runs under · Distributed backend shape comes from Accelerate configs.
- GRPO → Rewards: scores outputs · GRPO uses reward functions to evaluate sampled responses.
- GRPO → Code providers: executes code · Verifiable tasks may call external sandbox providers.
- GRPO → Code rewards: uses · Competitive-programming scoring and patching support code rewards.
- GRPO → Data utils: loads data · GRPO training also consumes datasets and mixtures.
- GRPO → Hub utils: publishes checkpoint · Trained policies are published outward when configured.
- GRPO → W&B logging: logs metrics · Reward and training metrics are logged during RL runs.
- GRPO → Model recipes: configured by · Task-specific YAMLs define prompts, rewards, and output paths.
- GRPO → Accelerate cfgs: runs under · Distributed backend settings are selected via Accelerate configs.
- Generate → Hub utils: publishes traces · Generated reasoning traces are pushed to the Hub.
- Generate → Model recipes: configured by · Generation behavior is driven by recipe settings.
- Generate → W&B logging: logs metrics · Generation runs can be tracked in experiment logging.
- Code rewards → Morph client: adapts · Morph is one of the code-execution backends.
- Code rewards → Piston client: adapts · Piston is another code-execution backend.
- Router ops → Code providers: routes sandboxes · Shared router services centralize access to execution pools.
- Train ops → SFT: launches · Slurm training jobs invoke the SFT entrypoint.
- Train ops → GRPO: launches · Slurm training jobs invoke the GRPO entrypoint.
- Gen ops → Generate: launches · Generation scripts and Slurm jobs invoke synthetic trace generation.
- Eval ops → Eval utils: orchestrates · Benchmark scripts use shared evaluation helpers.
- Eval ops → Hub utils: uploads results · Evaluation details and outputs may be published.
- Filter ops → Data utils: filters data · Filtering scripts operate on dataset mixtures and pass rates.
- Filter ops → Filter recipes: configured by · Dataset filtering behavior comes from recipe files.
- Tests → Rewards: verifies · Tests cover reward logic and edge cases.
- Tests → Data utils: verifies · Tests cover dataset utilities and plumbing.
- Tests → Code rewards: covers · Slow tests exercise code-reward paths.
