DeepSeek-R1的开放复现

Hacker News Top 工具

摘要

Hugging Face的Open R1项目为DeepSeek-R1提供了完全开放的复现流程,包括蒸馏数据集、训练脚本和评估工具,旨在让任何人都能基于R1的推理能力进行复现和进一步开发。

暂无内容
查看原文
查看缓存全文

缓存时间: 2026/06/11 16:36

huggingface/open-r1 来源:https://github.com/huggingface/open-r1

Open R1

DeepSeek-R1 的完全开源复现。本仓库仍在开发中,让我们一起构建!

目录

  1. 概述
  2. 行动计划
  3. 安装
  4. 训练模型
  5. 评估模型
  6. 复现 DeepSeek 的评估结果
  7. 数据生成
  8. 贡献

概述

本仓库的目标是补全 R1 流程中缺失的环节,使每个人都能复现并在此基础上继续构建。项目设计简洁,主要由以下部分组成:

  • src/open_r1:包含训练模型和生成合成数据的脚本:
    • grpo.py:在给定数据集上使用 GRPO 训练模型。
    • sft.py:在数据集上对模型执行简单的 SFT。
    • generate.py:使用 Distilabel(https://github.com/argilla-io/distilabel)从模型生成合成数据。
  • Makefile:包含针对 R1 流程每个步骤的易运行命令,充分利用上述脚本。

行动计划

我们将以 DeepSeek-R1 技术报告(https://github.com/deepseek-ai/DeepSeek-R1)为指导,大致可分为三个主要步骤:

  • 步骤 1:通过从 DeepSeek-R1 蒸馏高质量语料库,复现 R1-Distill 模型。
  • 步骤 2:复现 DeepSeek 用于创建 R1-Zero 的纯强化学习流程。这可能涉及策划新的、大规模的数学、推理和代码数据集。
  • 步骤 3:展示如何通过多阶段训练从基础模型过渡到强化学习微调模型。

新闻 🗞️

  • 🧑🍳 [2025/05/26](步骤 1 完成!) 我们发布 Mixture-of-Thoughts(https://huggingface.co/datasets/open-r1/Mixture-of-Thoughts)——一个精选的推理数据集,包含从 R1 蒸馏的 35 万条经过验证的推理轨迹。数据集涵盖数学、编程和科学任务,旨在教语言模型逐步推理。我们还提供了训练 OpenR1-Distill-7B(https://huggingface.co/open-r1/OpenR1-Distill-7B)的配方,该模型复现了 deepseek-ai/DeepSeek-R1-Distill-Qwen-7B(https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B)的推理能力,标志着 Open R1 项目步骤 1 的完成。
  • ⚡️ [2025/03/11](更新 #3)(https://huggingface.co/blog/open-r1/update-3): 我们发布 CodeForces-CoTs(https://huggingface.co/datasets/open-r1/codeforces-cots)数据集,包含从 R1 蒸馏的 1 万个竞赛编程问题和 10 万个解题方案。我们还发布了 IOI24:一个来自国际奥林匹克竞赛的极难问题的新基准。在 CodeForces-CoTs 上训练的 7B Qwen 模型在 IOI24 上可以超越 Claude 3.7 Sonnet,而 32B 模型可以超越 R1 本身。
  • ∞ [2025/02/10](更新 #2)(https://huggingface.co/blog/open-r1/update-2): 我们发布 OpenR1-Math-220k(https://huggingface.co/datasets/open-r1/OpenR1-Math-220k)数据集,包含从 R1 在 NuminaMath 新版本上蒸馏的 22 万条推理轨迹。在此数据集上训练的模型性能与 DeepSeek 蒸馏模型相当。
  • 🔥 [2025/02/02](更新 #1)(https://huggingface.co/blog/open-r1/update-1): 我们实现了训练(https://github.com/huggingface/open-r1?tab=readme-ov-file#training-models)、推理(https://github.com/huggingface/open-r1?tab=readme-ov-file#data-generation)和评估(https://github.com/huggingface/open-r1?tab=readme-ov-file#reproducing-deepseeks-evaluation-results)流程的第一部分。冲鸭!

安装

库依赖于 CUDA 12.4。如果遇到与段错误相关的错误,请使用 nvcc --version 双重检查系统运行的 CUDA 版本。

要运行本项目的代码,首先使用 uv 创建 Python 虚拟环境。要安装 uv,请遵循 UV 安装指南(https://docs.astral.sh/uv/getting-started/installation/)。

快捷方式:运行 make install 来设置开发库(具体如下文所述)。之后,如果一切设置正确,您可以尝试使用 Open-R1 模型。

uv venv openr1 --python 3.11 && source openr1/bin/activate && uv pip install --upgrade pip

对于 Hugging Face 集群用户,请在 .bashrc 中添加 export UV_LINK_MODE=copy 以抑制 uv 的缓存警告。

接下来,安装 vLLM 和 FlashAttention:

uv pip install vllm==0.8.5.post1
uv pip install setuptools && uv pip install flash-attn --no-build-isolation

这也会安装 PyTorch v2.6.0,非常重要的是要使用此版本,因为 vLLM 二进制文件是为它编译的。

然后,您可以通过 pip install -e .[LIST OF MODES] 安装特定用例的其余依赖项。对于大多数贡献者,我们建议:

GIT_LFS_SKIP_SMUDGE=1 uv pip install -e ".[dev]"

接下来,登录您的 Hugging Face 和 Weights and Biases 账户,如下所示:

huggingface-cli login
wandb login

最后,检查您的系统是否安装了 Git LFS,以便可以加载和推送模型/数据集到 Hugging Face Hub:

git-lfs --version

如果未安装,请运行:

sudo apt-get install git-lfs

训练模型

下面的训练命令配置为 8 x H100(80GB)节点。如果硬件和拓扑不同,您可能需要调整批量大小和梯度累积步数。

我们支持使用 DDP 或 DeepSpeed(ZeRO-2 和 ZeRO-3)训练模型。例如,要在从 DeepSeek-R1 蒸馏的包含推理轨迹的数据集(如 open-r1/Mixture-of-Thoughts(https://huggingface.co/datasets/open-r1/Mixture-of-Thoughts))上执行 SFT,请运行:

# 通过命令行训练
accelerate launch --config_file=recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --model_name_or_path open-r1/Qwen2.5-Math-7B-RoPE-300k \
    --dataset_name open-r1/Mixture-of-Thoughts \
    --dataset_config all \
    --eos_token '<|im_end|>' \
    --learning_rate 4.0e-5 \
    --num_train_epochs 5 \
    --max_seq_length 32768 \
    --per_device_train_batch_size 2 \
    --gradient_checkpointing \
    --bf16 \
    --use_liger_kernel \
    --output_dir data/OpenR1-Distill-7B

# 通过 YAML 配置训练
accelerate launch --config_file recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --config recipes/OpenR1-Distill-7B/sft/config_distill.yaml

目前,支持以下任务:

  • 监督微调 sft
  • 组相对策略优化 grpo

如果您增加/减少 GPU 数量,我们建议同时调整每个设备的批量大小或梯度累积步数,以保持全局批量大小不变。

默认情况下,这些脚本会将每个模型推送到您的 Hugging Face Hub 用户名下,即 {username}/{model_name}-{task}。您可以通过将参数附加到命令来覆盖每个 YAML 配置中的参数,如下所示:

# 将基础模型更改为更小的变体
accelerate launch --config_file recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --config recipes/OpenR1-Distill-7B/sft/config_distill.yaml \
    --model_name_or_path Qwen/Qwen3-0.6B-Base \
    --hub_model_id OpenR1-Distill-0.6B \
    --output_dir data/OpenR1-Distill-0.6B

如果您还想覆盖 Weights and Biases 的默认设置,可以这样做:

accelerate launch --config_file recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --config recipes/OpenR1-Distill-7B/sft/config_distill.yaml --wandb_entity huggingface --wandb_project open-r1 --run_name Qwen2.5-1.5B-GRPO

🚨 警告 🚨 大多数基础模型(如 meta-llama/Llama-3.2-1B)没有聊天模板,因此我们在训练期间将 ChatML 设置为默认。但是,对于 Qwen 基础模型(如 Qwen/Qwen2.5-1.5B),分词器中预定义了聊天模板,因此必须相应设置 EOS 令牌,例如:

# 为 Qwen 基础模型对齐 EOS 令牌与聊天模板
accelerate launch --config_file=recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --model_name_or_path Qwen/Qwen2.5-1.5B \
+   --eos_token '<|im_end|>' \
    --dataset_name open-r1/Mixture-of-Thoughts \
    --dataset_config all \
    --learning_rate 4.0e-5 \
    --num_train_epochs 1 \
    --max_seq_length 32768 \
    --per_device_train_batch_size 16 \
    --gradient_checkpointing \
    --bf16 \
    --use_liger_kernel \
    --output_dir data/Qwen2.5-1.5B-Open-R1-Distill

如果您希望使用自定义聊天模板(例如 Llama 或 Gemma),则必须提供聊天模板和相关的 EOS 令牌:

# 使用自定义聊天模板对齐 EOS 令牌
accelerate launch --config_file=recipes/accelerate_configs/zero3.yaml src/open_r1/sft.py \
    --model_name_or_path meta-llama/Llama-3.2-1B \
+   --chat_template "$(cat llama_chat_template.jinja)" \
+   --eos_token '<|eot_id|>' \
    --dataset_name open-r1/Mixture-of-Thoughts \
    --dataset_config all \
    --learning_rate 4.0e-5 \
    --num_train_epochs 1 \
    --max_seq_length 32768 \
    --per_device_train_batch_size 16 \
    --gradient_checkpointing \
    --bf16 \
    --use_liger_kernel \
    --output_dir data/Llama-3.2-1B-Open-R1-Distill

SFT 蒸馏

我们提供了一个配方,从相同的基础模型开始,复现 deepseek-ai/DeepSeek-R1-Distill-Qwen-7B(https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B)的推理能力。为此,请运行:

ACCELERATE_LOG_LEVEL=info accelerate launch --config_file recipes/accelerate_configs/zero3.yaml \
    src/open_r1/sft.py \
    --config recipes/OpenR1-Distill-7B/sft/config_distill.yaml

结果将是一个类似 open-r1/OpenR1-Distill-7B(https://huggingface.co/open-r1/OpenR1-Distill-7B)的模型,具有以下下游性能:

模型AIME 2024MATH-500GPQA DiamondLiveCodeBench v5
OpenR1-Distill-7B52.789.052.839.4
DeepSeek-R1-Distill-Qwen-7B51.393.552.437.4

您可以调整 YAML 配置以在不同的基础模型或数据集上训练。

GRPO

我们使用 TRL 的 vLLM 后端(https://huggingface.co/docs/trl/speeding_up_training?vllm+examples=GRPO#vllm-for-fast-generation-in-online-methods)将训练扩展到跨多个节点的大型模型。对于在 8 个 GPU 上单节点训练小型模型,使用 vllm_mode="colocate" 将 vLLM 与训练脚本在同一进程中运行:

ACCELERATE_LOG_LEVEL=info \
accelerate launch --config_file recipes/accelerate_configs/zero3.yaml \
    src/open_r1/grpo.py --config recipes/DeepSeek-R1-Distill-Qwen-1.5B/grpo/config_demo.yaml \
    --vllm_mode colocate

蒸馏 DeepSeek 模型中使用的聊天模板省略了 和 标签内的推理块内容。它还会预填充助手响应中的 ``,这会干扰格式奖励函数。为此,重要的是按照例如 recipes/DeepSeek-R1-Distill-Qwen-1.5B/grpo/config_demo.yaml 中的方式覆盖聊天模板。

对于 N+1 节点的多节点训练(1 个节点运行 vLLM 服务器,N 个节点运行训练),我们提供了一个示例 Slurm 脚本。例如,要在 1+1 个节点上以数据并行方式运行上述示例,请运行:

sbatch --nodes=2 slurm/train.slurm --model Qwen2.5-1.5B-Instruct --task grpo --config demo --accelerator zero2 --dp 8 --tp 1

有关更多详细信息,请参阅 在 Slurm 集群上启动作业 部分。

GRPO 数据集过滤

我们支持通过生成并计算可验证任务的通过率来过滤数据集,请参见此 README。

👨💻 使用代码解释器进行训练

我们提供了一个 code 奖励函数,用于在训练期间执行策略生成的代码。目前,此奖励函数针对像 Codeforces(https://codeforces.com)这样的代码竞赛,其中针对一组测试用例执行解决方案,并将总体成功率作为最终奖励返回。

为确保安全执行,我们支持多个沙箱提供者:

  1. E2B (https://e2b.dev) - 快速的基于云的沙箱,专注于 Python 执行
  2. Morph (https://cloud.morph.so/web/) - 基于云的沙箱,支持更广泛的语言 - Python/JS/C++/Rust

要使用代码奖励函数,首先安装必要的依赖项:

uv pip install -e '.[code]'
E2B 提供者

要使用 E2B 沙箱,创建一个 .env 文件并添加您的 E2B API 令牌:

E2B_API_KEY="e2b_xxx"
Morph 提供者

要使用 Morph,首先安装 morphcloud 包:

pip install morphcloud

然后将您的 Morph API 令牌添加到 .env 文件中:

MORPH_API_KEY="YOUR_MORPH_API_KEY"

要指定使用哪个提供者,请在配置中添加 provider_type 参数:

# 对于 E2B
provider_type: e2b
# 对于 Morph
provider_type: morph
数据集要求

确保您的数据集包含一个 verification_info 列,具有以下模式(采用 PrimeIntellect 优秀数据集(https://huggingface.co/collections/PrimeIntellect/synthetic-1-67a2c399cfdd6c9f7fae0c37)的可验证问题模式):

{
    "language": "python",   # Morph 支持更多语言,包括 C++、Java 等
    "test_cases": [
        {
            "input": "4\n4\n0001\n1000\n0011\n0111\n3\n010\n101\n0\n2\n00000\n00001\n4\n01\n001\n0001\n00001\n",
            "output": "1\n3 \n-1\n0\n\n2\n1 2 \n",
            "type": "stdin_stdout",
        }
    ],
}

例如,要在 Python 问题上训练一个小型模型,启动 vLLM 服务器:

CUDA_VISIBLE_DEVICES=0 trl vllm-serve --model Qwen/Qwen2.5-1.5B-Instruct

然后使用以下命令运行训练:

CUDA_VISIBLE_DEVICES=1,2,3,4,5,6,7 ACCELERATE_LOG_LEVEL=info \
accelerate launch --config_file recipes/accelerate_configs/zero2.yaml --num_processes=7 \
    src/open_r1/grpo.py --config recipes/Qwen2.5-1.5B-Instruct/grpo/config_demo_code.yaml
使用路由服务

当太多脚本在沙箱服务上执行时,可能会受到速率限制。对于两个提供者,我们提供了可以在 CPU 节点上启动的路由脚本:

对于 E2B:

sbatch slurm/e2b_router.slurm

对于 Morph:

sbatch slurm/morph_router.slurm

然后在您的训练 YAML 配置中添加路由 URL:

# 对于 E2B
e2b_router_url: 1.2.3.4:8000
# 对于 Morph
morph_router_url: 1.2.3.4:8000

端口应与启动路由时使用的端口匹配。所有训练作业可以共享同一个路由 IP,这将确保并行执行得到适当管理。

竞赛编程问题:IOI 和 CodeForces

我们提供了 ioi_code_reward 和 cf_code_reward 奖励函数,用于分别执行来自 IOI(https://hf.co/datasets/open-r1/ioi)和 CodeForces(https://huggingface.co/datasets/open-r1/codeforces)的问题。您可以使用 piston (https://github.com/engineer-man/piston) 或 Morph(目前仅限 IOI)作为执行提供者。

Piston

要使用 Piston:

  1. 获取 Piston workers 运行,请参阅 slurm/piston/README.md
  2. 将环境变量 PISTON_ENDPOINTS 设置为 slurm 或 Piston worker 端点列表

对于 IOI: 3. 在您的配置中,使用 ioi_provider: "piston"

对于 CodeForces: 3. 下载生成的(困难)测试用例:

# 更改 PATH_TO_SAVE_TESTCASES。根据您的机器容量增加 --max-workers
huggingface-cli download open-r1/codeforces --repo-type=dataset --include='generated_tests/*.parquet' --max-workers=8 --local-dir PATH_TO_SAVE_TESTCASES
  1. 将路径保存在 .env 中:
CF_TESTS_FOLDER=PATH_TO_SAVE_TESTCASES
Morph

Morph 是一个基于云的解决方案,提供沙箱化环境用于运行代码。要使用它:

  1. 安装 Morph 客户端:pip install morphcloud
  2. 将您的 Morph API 密钥添加到 .env 文件:MORPH_API_KEY="your_key_here"
  3. 在您的配置中

相似文章