How huggingface/open-r1 works
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: grpo.py GRPO · sft.py SFT · generate.py Generate · utils.py Code rewards
Components 21
Core runtime
Training, generation, rewards, and shared utilities under src/open_r1.
- SFT · Supervised fine-tuning runtime launched by Accelerate or Slurm.
src/open_r1/sft.py - GRPO · Reinforcement-learning trainer that couples policy updates with reward computation and generation.
src/open_r1/grpo.py - Generate · Synthetic trace generation pipeline for producing reasoning data.
src/open_r1/generate.py - Rewards · Reward functions used by GRPO, including verifiable-task scoring hooks.
src/open_r1/rewards.py - Data utils · Dataset loading, filtering, and mixture construction helpers.
src/open_r1/utils/data.py - Eval utils · Shared evaluation helpers used by benchmarking and validation flows.
src/open_r1/utils/evaluation.py - Hub utils · Helpers for pushing models and datasets to the Hugging Face Hub.
src/open_r1/utils/hub.py - W&B logging · Experiment logging integration for training and evaluation runs.
src/open_r1/utils/wandb_logging.py - Code providers · Abstraction layer for external code-execution backends.
src/open_r1/utils/code_providers.py - Code rewards · Competitive-programming scoring, patching, and sandbox client utilities.
src/open_r1/utils/competitive_programming/utils.py - Morph client · Client adapter for Morph-backed code execution.
src/open_r1/utils/competitive_programming/morph_client.py - Piston 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 · DDP, FSDP, ZeRO-2, and ZeRO-3 backend shapes for training runs.
recipes/accelerate_configs/ddp.yaml - Model recipes · Per-model SFT and GRPO YAML recipes defining datasets, templates, and outputs.
recipes/README.md - Filter recipes · 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 · Slurm and shell entrypoints for training and clustered execution.
slurm/train.slurm - Eval ops · Benchmark orchestration and result publishing scripts.
scripts/run_benchmarks.py - Gen ops · Scripts for trace generation and related scaling workflows.
scripts/generate_reasoning.py - Router ops · Router and worker deployment for shared sandbox execution pools.
scripts/e2b_router.py - Filter ops · 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 · Reward and data utility tests, including slow code-reward coverage.
tests/test_rewards.py
Connections 29
- 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.