{"id":53752,"date":"2025-02-16T10:23:26","date_gmt":"2025-02-16T02:23:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53752\/"},"modified":"2025-02-16T10:23:26","modified_gmt":"2025-02-16T02:23:26","slug":"deepseek-grpo-trainer%e7%ae%80%e6%98%8e%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53752\/","title":{"rendered":"DeepSeek GRPO Trainer\u7b80\u660e\u6559\u7a0b"},"content":{"rendered":"<p>TRL \u652f\u6301\u4f7f\u7528 GRPO Trainer \u6765\u8bad\u7ec3\u8bed\u8a00\u6a21\u578b\uff0c\u5982\u8bba\u6587\u300a\u4e2d\u6240\u8ff0\u3002<\/p>\n<p>\u8bba\u6587\u6458\u8981\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n  \u6570\u5b66\u63a8\u7406\u56e0\u5176\u590d\u6742\u6027\u548c\u7ed3\u6784\u6027\u800c\u5bf9\u8bed\u8a00\u6a21\u578b\u6784\u6210\u4e86\u91cd\u5927\u6311\u6218\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 DeepSeekMath 7B\uff0c\u5b83\u7ee7\u7eed\u4f7f\u7528\u6765\u81ea Common Crawl \u7684 120B \u4e2a\u6570\u5b66\u76f8\u5173\u6807\u8bb0\u4ee5\u53ca\u81ea\u7136\u8bed\u8a00\u548c\u4ee3\u7801\u6570\u636e\u5bf9 DeepSeek-Coder-Base-v1.5 7B \u8fdb\u884c\u9884\u8bad\u7ec3\u3002DeepSeekMath 7B \u5728\u4e0d\u4f9d\u8d56\u5916\u90e8\u5de5\u5177\u5305\u548c\u6295\u7968\u6280\u672f\u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u7ade\u8d5b\u7ea7 MATH \u57fa\u51c6\u4e0a\u53d6\u5f97\u4e86\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684 51.7% \u7684\u6210\u7ee9\uff0c\u63a5\u8fd1 Gemini-Ultra \u548c GPT-4 \u7684\u6027\u80fd\u6c34\u5e73\u3002 DeepSeekMath 7B \u7684 64 \u4e2a\u6837\u672c\u7684\u81ea\u6d3d\u6027\u5728 MATH \u4e0a\u8fbe\u5230 60.9%\u3002DeepSeekMath \u7684\u6570\u5b66\u63a8\u7406\u80fd\u529b\u5f52\u56e0\u4e8e\u4e24\u4e2a\u5173\u952e\u56e0\u7d20\uff1a\u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u6570\u636e\u9009\u62e9\u7ba1\u9053\u5229\u7528\u4e86\u516c\u5f00\u53ef\u7528\u7684\u7f51\u7edc\u6570\u636e\u7684\u5de8\u5927\u6f5c\u529b\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f15\u5165\u4e86\u7ec4\u76f8\u5bf9\u7b56\u7565\u4f18\u5316 (GRPO)\uff0c\u8fd9\u662f\u8fd1\u7aef\u7b56\u7565\u4f18\u5316 (PPO) \u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u5b83\u53ef\u4ee5\u589e\u5f3a\u6570\u5b66\u63a8\u7406\u80fd\u529b\uff0c\u540c\u65f6\u4f18\u5316 PPO \u7684\u5185\u5b58\u4f7f\u7528\u91cf\u3002\n<\/p><\/blockquote>\n<h2>1\u3001\u5feb\u901f\u5165\u95e8<\/h2>\n<p>\u6b64\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 GRPO \u65b9\u6cd5\u8bad\u7ec3\u6a21\u578b\u3002\u6211\u4eec\u4f7f\u7528\u4e2d\u7684\u63d0\u793a\u8bad\u7ec3  \u6a21\u578b\uff08\u5ffd\u7565\u5b8c\u6210\u5217\uff01\uff09\u3002\u4f60\u53ef\u4ee5\u5728\u67e5\u770b\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\u7684\u811a\u672c\u3002\u8bf7\u6ce8\u610f\uff0c\u524d\u5411\u4f20\u9012\u7684\u8f93\u5165\u5f20\u91cf\u7684\u5927\u5c0f\u4e3a <code>num_generations * per_device_train_batch_size<\/code>\uff0c\u56e0\u4e3a GRPO \u4e3a\u6279\u6b21\u4e2d\u7684\u6bcf\u4e2a\u63d0\u793a\u751f\u6210 <code>num_generations<\/code> \u4e2a\u5b8c\u6210\u3002\u9002\u5f53\u8c03\u6574\u8fd9\u4e9b\u503c\u6709\u52a9\u4e8e\u9632\u6b62 OOM \u9519\u8bef\u3002\u56e0\u6b64\uff0c\u6709\u6548\u7684\u8bad\u7ec3\u6279\u6b21\u5927\u5c0f\u4e3a <code>num_generations * per_device_train_batch_size * gradient_accumulation_steps<\/code>\u3002<\/p>\n<pre><code># train_grpo.py\nfrom datasets import load_dataset\nfrom trl import GRPOConfig, GRPOTrainer\n\ndataset = load_dataset(\"trl-lib\/tldr\", split=\"train\")\n\n# Define the reward function, which rewards completions that are close to 20 characters\ndef reward_len(completions, **kwargs):\n    return [abs(20 - len(completion)) for completion in completions]\n\ntraining_args = GRPOConfig(output_dir=\"Qwen2-0.5B-GRPO\", logging_steps=10)\ntrainer = GRPOTrainer(\n    model=\"Qwen\/Qwen2-0.5B-Instruct\",\n    reward_funcs=reward_len,\n    args=training_args,\n    train_dataset=dataset,\n)\ntrainer.train()<\/code><\/pre>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u811a\u672c\uff1a<\/p>\n<pre><code>accelerate launch train_grpo.py\n<\/code><\/pre>\n<p>\u8bad\u7ec3\u5206\u5e03\u5728 8 \u4e2a GPU \u4e0a\uff0c\u5927\u7ea6\u9700\u8981 1 \u5929\u3002<\/p>\n<h2>2\u3001\u6df1\u5165\u4e86\u89e3 GRPO \u65b9\u6cd5<\/h2>\n<p>GRPO \u662f\u4e00\u79cd\u5728\u7ebf\u5b66\u4e60\u7b97\u6cd5\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u901a\u8fc7\u5728\u8bad\u7ec3\u671f\u95f4\u4f7f\u7528\u8bad\u7ec3\u6a21\u578b\u672c\u8eab\u751f\u6210\u7684\u6570\u636e\u6765\u8fed\u4ee3\u6539\u8fdb\u3002GRPO \u76ee\u6807\u80cc\u540e\u7684\u76f4\u89c9\u662f\u6700\u5927\u5316\u751f\u6210\u7684\u5b8c\u6210\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u786e\u4fdd\u6a21\u578b\u63a5\u8fd1\u53c2\u8003\u7b56\u7565\u3002\u8981\u4e86\u89e3 GRPO \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u53ef\u4ee5\u5c06\u5176\u5206\u89e3\u4e3a\u56db\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a\u751f\u6210\u5b8c\u6210\u3001\u8ba1\u7b97\u4f18\u52bf\u3001\u4f30\u8ba1 KL \u6563\u5ea6\u548c\u8ba1\u7b97\u635f\u5931\u3002<\/p>\n<blockquote><p>\n  \u751f\u6210\u5b8c\u6210\n<\/p><\/blockquote>\n<p>\u5728\u6bcf\u4e2a\u8bad\u7ec3\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u90fd\u4f1a\u62bd\u6837\u4e00\u6279\u63d0\u793a\u5e76\u4e3a\u6bcf\u4e2a\u63d0\u793a\u751f\u6210\u4e00\u7ec4G \u5b8c\u6210\uff08\u8868\u793a\u4e3a<br \/>oi\uff09\u3002<\/p>\n<blockquote><p>\n  \u8ba1\u7b97\u4f18\u52bf\n<\/p><\/blockquote>\n<p>\u5bf9\u4e8e\u6bcf\u4e2aG \u5e8f\u5217\uff0c\u6211\u4eec\u4f7f\u7528\u5956\u52b1\u6a21\u578b\u8ba1\u7b97\u5956\u52b1\u3002\u4e3a\u4e86\u4e0e\u5956\u52b1\u6a21\u578b\u7684\u6bd4\u8f83\u6027\u8d28\u4fdd\u6301\u4e00\u81f4\uff08\u901a\u5e38\u5728\u9488\u5bf9\u540c\u4e00\u95ee\u9898\u7684\u8f93\u51fa\u6bd4\u8f83\u6570\u636e\u96c6\u4e0a\u8fdb\u884c\u8bad\u7ec3\uff09\uff0c\u4f18\u52bf\u7684\u8ba1\u7b97\u53cd\u6620\u4e86\u8fd9\u4e9b\u76f8\u5bf9\u6bd4\u8f83\u3002\u5b83\u6309\u5982\u4e0b\u65b9\u5f0f\u8fdb\u884c\u89c4\u8303\u5316\uff1a<\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u8ba9\u8be5\u65b9\u6cd5\u5f97\u540d\uff1a\u7ec4\u76f8\u5bf9\u7b56\u7565\u4f18\u5316 (GRPO)\u3002<\/p>\n<blockquote><p>\n  \u4f30\u8ba1 KL \u6563\u5ea6\n<\/p><\/blockquote>\n<p>KL \u6563\u5ea6\u662f\u4f7f\u7528 Schulman \u7b49\u4eba (2020) \u5f15\u5165\u7684\u8fd1\u4f3c\u5668\u6765\u4f30\u8ba1\u7684\u3002\u8fd1\u4f3c\u5668\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n  \u8ba1\u7b97\u635f\u5931\n<\/p><\/blockquote>\n<p>\u76ee\u6807\u662f\u6700\u5927\u5316\u4f18\u52bf\uff0c\u540c\u65f6\u786e\u4fdd\u6a21\u578b\u63a5\u8fd1\u53c2\u8003\u7b56\u7565\u3002\u56e0\u6b64\uff0c\u635f\u5931\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>\u5176\u4e2d\u7b2c\u4e00\u9879\u8868\u793a\u7f29\u653e\u4f18\u52bf\uff0c\u7b2c\u4e8c\u9879\u901a\u8fc7 KL \u6563\u5ea6\u60e9\u7f5a\u504f\u79bb\u53c2\u8003\u7b56\u7565\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u5728\u539f\u59cb\u8bba\u6587\u4e2d\uff0c\u8be5\u516c\u5f0f\u88ab\u63a8\u5e7f\u4e3a\u901a\u8fc7\u5229\u7528\u88c1\u526a\u66ff\u4ee3\u76ee\u6807\u6765\u8003\u8651\u6bcf\u4e00\u4ee3\u4e4b\u540e\u7684\u591a\u6b21\u66f4\u65b0\uff1a<\/p>\n<p>\u5176\u4e2d clip(\u22c5,1\u2212\u03f5,1+\u03f5) \u901a\u8fc7\u5c06\u7b56\u7565\u6bd4\u7387\u9650\u5236\u5728 1\u2212\u03f5 \u548c 1+\u03f5 \u4e4b\u95f4\u6765\u786e\u4fdd\u66f4\u65b0\u4e0d\u4f1a\u8fc7\u5ea6\u504f\u79bb\u53c2\u8003\u7b56\u7565\u3002\u7136\u800c\uff0c\u5728 TRL \u4e2d\uff0c\u4e0e\u539f\u59cb\u8bba\u6587\u4e00\u6837\uff0c\u6211\u4eec\u6bcf\u4ee3\u53ea\u8fdb\u884c\u4e00\u6b21\u66f4\u65b0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u635f\u5931\u7b80\u5316\u4e3a\u7b2c\u4e00\u79cd\u5f62\u5f0f\u3002<\/p>\n<h2>3\u3001\u8bb0\u5f55\u6307\u6807<\/h2>\n<p>GRPO Trainer \u8bb0\u5f55\u4ee5\u4e0b\u6307\u6807\uff1a<\/p>\n<ul>\n<li><code>completion_length<\/code>\uff1a\u5e73\u5747\u5b8c\u6210\u957f\u5ea6\u3002<\/li>\n<li><code>reward\/{reward_func_name}<\/code>\uff1a\u6bcf\u4e2a\u5956\u52b1\u51fd\u6570\u8ba1\u7b97\u7684\u5956\u52b1\u3002<\/li>\n<li><code>reward<\/code>\uff1a\u5e73\u5747\u5956\u52b1\u3002<\/li>\n<li><code>reward_std<\/code>\uff1a\u5956\u52b1\u7ec4\u5185\u7684\u5e73\u5747\u6807\u51c6\u5dee\u3002<\/li>\n<li><code>kl<\/code>\uff1a\u6839\u636e\u5b8c\u6210\u60c5\u51b5\u8ba1\u7b97\u7684\u6a21\u578b\u4e0e\u53c2\u8003\u6a21\u578b\u4e4b\u95f4\u7684\u5e73\u5747 KL \u6563\u5ea6\u3002<\/li>\n<\/ul>\n<h2>4\u3001\u5b9a\u5236<\/h2>\n<h3>4.1 \u52a0\u901f<\/h3>\n<p>\u751f\u6210\u901a\u5e38\u662f\u5bfc\u81f4\u5728\u7ebf\u65b9\u6cd5\u8bad\u7ec3\u7f13\u6162\u7684\u4e3b\u8981\u74f6\u9888\u3002\u4e3a\u4e86\u52a0\u901f\u751f\u6210\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 vLLM\uff0c\u8fd9\u662f\u4e00\u4e2a\u652f\u6301\u5feb\u901f\u751f\u6210\u7684\u5e93\u3002\u8981\u542f\u7528\u5b83\uff0c\u8bf7\u5728\u8bad\u7ec3\u53c2\u6570\u4e2d\u4f20\u9012 <code>use_vllm=True<\/code>\u3002<\/p>\n<pre><code>from trl import GRPOConfig\n\ntraining_args = GRPOConfig(..., use_vllm=True)<\/code><\/pre>\n<p>\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u3002<\/p>\n<h3>4.2 \u4f7f\u7528\u81ea\u5b9a\u4e49\u5956\u52b1\u51fd\u6570<\/h3>\n<p>\u652f\u6301\u4f7f\u7528\u81ea\u5b9a\u4e49\u5956\u52b1\u51fd\u6570\u4ee3\u66ff\u5bc6\u96c6\u5956\u52b1\u6a21\u578b\u3002\u4e3a\u786e\u4fdd\u517c\u5bb9\u6027\uff0c\u4f60\u7684\u5956\u52b1\u51fd\u6570\u5fc5\u987b\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\uff1a<\/p>\n<blockquote><p>\n  \u8f93\u5165\u53c2\u6570\uff1a\n<\/p><\/blockquote>\n<p>\u8be5\u51fd\u6570\u5fc5\u987b\u63a5\u53d7\u4ee5\u4e0b\u5185\u5bb9\u4f5c\u4e3a\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><code>prompts<\/code>\uff08\u5305\u542b\u63d0\u793a\uff09\u3001<\/li>\n<li><code>completions<\/code>\uff08\u5305\u542b\u751f\u6210\u7684\u5b8c\u6210\uff09\u3001<\/li>\n<li>\u6570\u636e\u96c6\u53ef\u80fd\u5177\u6709\u7684\u6240\u6709\u5217\u540d\uff08\u4f46\u63d0\u793a\u9664\u5916\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u636e\u96c6\u5305\u542b\u540d\u4e3a <code>ground_truth<\/code> \u7684\u5217\uff0c\u5219\u5c06\u4f7f\u7528 <code>ground_truth<\/code> \u4f5c\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u6ee1\u8db3\u6b64\u8981\u6c42\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u4f7f\u7528 <code>**kwargs<\/code>\u3002<\/p>\n<p>\u6839\u636e\u6570\u636e\u96c6\u683c\u5f0f\uff0c\u8f93\u5165\u4f1a\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u6807\u51c6\u683c\u5f0f\uff0c\u63d0\u793a\u548c\u5b8c\u6210\u5c06\u662f\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/li>\n<li>\u5bf9\u4e8e\u5bf9\u8bdd\u683c\u5f0f\uff0c\u63d0\u793a\u548c\u5b8c\u6210\u5c06\u662f\u6d88\u606f\u5b57\u5178\u5217\u8868\u3002<\/li>\n<\/ul>\n<p>\u8fd4\u56de\u503c\uff1a\u8be5\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u6d6e\u70b9\u6570\u5217\u8868\u3002\u6bcf\u4e2a\u6d6e\u70b9\u6570\u4ee3\u8868\u4e0e\u5355\u6b21\u5b8c\u6210\u76f8\u5bf9\u5e94\u7684\u5956\u52b1\u3002<\/p>\n<h3>4.3 \u793a\u4f8b 1\uff1a\u5956\u52b1\u8f83\u957f\u7684\u5b8c\u6210\u6b21\u6570<\/h3>\n<p>\u4ee5\u4e0b\u662f\u6807\u51c6\u683c\u5f0f\u7684\u5956\u52b1\u51fd\u6570\u793a\u4f8b\uff0c\u7528\u4e8e\u5956\u52b1\u8f83\u957f\u7684\u5b8c\u6210\u6b21\u6570\uff1a<\/p>\n<pre><code>def reward_func(completions, **kwargs):\n    \"\"\"Reward function that gives higher scores to longer completions.\"\"\"\n    return [float(len(completion)) for completion in completions]<\/code><\/pre>\n<p>\u53ef\u4ee5\u6309\u5982\u4e0b\u65b9\u5f0f\u6d4b\u8bd5\u5b83\uff1a<\/p>\n<pre><code>prompts = [\"The sky is\", \"The sun is\"]\ncompletions = [\" blue.\", \" in the sky.\"]\nprint(reward_func(prompts=prompts, completions=completions))<\/code><\/pre>\n<h3>4.4 \u793a\u4f8b 2\uff1a\u4ee5\u7279\u5b9a\u683c\u5f0f\u5956\u52b1\u5b8c\u6210<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5956\u52b1\u51fd\u6570\u7684\u793a\u4f8b\uff0c\u7528\u4e8e\u68c0\u67e5\u5b8c\u6210\u662f\u5426\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u3002\u6b64\u793a\u4f8b\u7684\u7075\u611f\u6765\u81ea\u8bba\u6587 DeepSeek-R1\uff1a\u901a\u8fc7\u5f3a\u5316\u5b66\u4e60\u6fc0\u52b1\u6cd5\u5b66\u7855\u58eb\u4e2d\u7684\u63a8\u7406\u80fd\u529b\u4e2d\u4f7f\u7528\u7684\u683c\u5f0f\u5956\u52b1\u51fd\u6570\u3002\u5b83\u4e13\u4e3a\u5bf9\u8bdd\u683c\u5f0f\u800c\u8bbe\u8ba1\uff0c\u5176\u4e2d\u63d0\u793a\u548c\u5b8c\u6210\u7531\u7ed3\u6784\u5316\u6d88\u606f\u7ec4\u6210\u3002<\/p>\n<pre><code>import re\n\ndef format_reward_func(completions, **kwargs):\n    \"\"\"Reward function that checks if the completion has a specific format.\"\"\"\n    pattern = r\"^&lt;think&gt;.*?&lt;\/think&gt;&lt;answer&gt;.*?&lt;\/answer&gt;$\"\n    completion_contents = [completion[0][\"content\"] for completion in completions]\n    matches = [re.match(pattern, content) for content in completion_contents]\n    return [1.0 if match else 0.0 for match in matches]<\/code><\/pre>\n<p>\u53ef\u4ee5\u6309\u5982\u4e0b\u65b9\u5f0f\u6d4b\u8bd5\u6b64\u529f\u80fd\uff1a<\/p>\n<pre><code>prompts = [\n    [{\"role\": \"assistant\", \"content\": \"What is the result of (1 + 2) * 4?\"}],\n    [{\"role\": \"assistant\", \"content\": \"What is the result of (3 + 1) * 2?\"}],\n]\ncompletions = [\n    [{\"role\": \"assistant\", \"content\": \"&lt;think&gt;The sum of 1 and 2 is 3, which we multiply by 4 to get 12.&lt;\/think&gt;&lt;answer&gt;(1 + 2) * 4 = 12&lt;\/answer&gt;\"}],\n    [{\"role\": \"assistant\", \"content\": \"The sum of 3 and 1 is 4, which we multiply by 2 to get 8. So (3 + 1) * 2 = 8.\"}],\n]\nformat_reward_func(prompts=prompts, completions=completions)<\/code><\/pre>\n<h3>4.5 \u793a\u4f8b 3\uff1a\u57fa\u4e8e\u53c2\u8003\u7684\u5956\u52b1\u5b8c\u6210<\/h3>\n<p>\u4e0b\u9762\u662f\u68c0\u67e5\u662f\u5426\u6b63\u786e\u7684\u5956\u52b1\u51fd\u6570\u7684\u793a\u4f8b\u3002\u6b64\u793a\u4f8b\u7684\u7075\u611f\u6765\u81ea\u8bba\u6587 DeepSeek-R1\uff1a\u901a\u8fc7\u5f3a\u5316\u5b66\u4e60\u6fc0\u52b1 LLM \u4e2d\u7684\u63a8\u7406\u80fd\u529b\u4e2d\u4f7f\u7528\u7684\u51c6\u786e\u5ea6\u5956\u52b1\u51fd\u6570\u3002\u6b64\u793a\u4f8b\u4e13\u4e3a\u6807\u51c6\u683c\u5f0f\u8bbe\u8ba1\uff0c\u5176\u4e2d\u6570\u636e\u96c6\u5305\u542b\u4e00\u4e2a\u540d\u4e3a ground_truth \u7684\u5217\u3002<\/p>\n<pre><code>import re\n\ndef reward_func(completions, ground_truth, **kwargs):\n    # Regular expression to capture content inside \\boxed{}\n    matches = [re.search(r\"\\\\boxed\\{(.*?)\\}\", completion) for completion in completions]\n    contents = [match.group(1) if match else \"\" for match in matches]\n    # Reward 1 if the content is the same as the ground truth, 0 otherwise\n    return [1.0 if c == gt else 0.0 for c, gt in zip(contents, ground_truth)]<\/code><\/pre>\n<p>\u53ef\u4ee5\u6309\u5982\u4e0b\u65b9\u5f0f\u6d4b\u8bd5\u6b64\u529f\u80fd\uff1a<\/p>\n<pre><code>prompts = [\"Problem: Solve the equation $2x + 3 = 7$. Solution:\", \"Problem: Solve the equation $3x - 5 = 10$.\"]\ncompletions = [r\" The solution is \\boxed{2}.\", r\" The solution is \\boxed{6}.\"]\nground_truth = [\"2\", \"5\"]\nreward_func(prompts=prompts, completions=completions, ground_truth=ground_truth)<\/code><\/pre>\n<h3>4.6 \u5c06\u5956\u52b1\u51fd\u6570\u4f20\u9012\u7ed9\u8bad\u7ec3\u5668<\/h3>\n<p>\u8981\u4f7f\u7528\u4f60\u7684\u81ea\u5b9a\u4e49\u5956\u52b1\u51fd\u6570\uff0c\u8bf7\u6309\u5982\u4e0b\u65b9\u5f0f\u5c06\u5176\u4f20\u9012\u7ed9 GRPOTrainer\uff1a<\/p>\n<pre><code>from trl import GRPOTrainer\n\ntrainer = GRPOTrainer(\n    reward_funcs=reward_func,\n    ...,\n)<\/code><\/pre>\n<p>\u5982\u679c\u6709\u591a\u4e2a\u5956\u52b1\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5217\u8868\u4f20\u9012\uff1a<\/p>\n<pre><code>from trl import GRPOTrainer\n\ntrainer = GRPOTrainer(\n    reward_funcs=[reward_func1, reward_func2],\n    ...,\n)<\/code><\/pre>\n<p>\u5956\u52b1\u5c06\u8ba1\u7b97\u4e3a\u6bcf\u4e2a\u51fd\u6570\u5956\u52b1\u7684\u603b\u548c\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0cGRPOTrainer \u652f\u6301\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5956\u52b1\u51fd\u6570\u3002\u6709\u5173\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u53c2\u6570\u6587\u6863\u3002<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>TRL \u652f\u6301\u4f7f\u7528 GRPO Trainer \u6765\u8bad\u7ec3\u8bed\u8a00\u6a21\u578b\uff0c\u5982\u8bba\u6587\u300a\u4e2d\u6240\u8ff0\u3002 \u8bba\u6587\u6458\u8981\u5982\u4e0b\uff1a \u6570\u5b66\u63a8\u7406\u56e0\u5176\u590d\u6742\u6027\u548c\u7ed3\u6784\u6027\u800c\u5bf9\u8bed\u8a00\u6a21\u578b\u6784\u6210\u4e86\u91cd\u5927\u6311\u6218\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 DeepSeekMath 7B\uff0c\u5b83\u7ee7\u7eed\u4f7f\u7528\u6765\u81ea Common Crawl \u7684 120B \u4e2a\u6570\u5b66\u76f8\u5173\u6807\u8bb0\u4ee5\u53ca\u81ea\u7136\u8bed\u8a00\u548c\u4ee3\u7801\u6570\u636e\u5bf9 DeepSeek-Coder-Base-v1.5 7B \u8fdb\u884c\u9884\u8bad\u7ec3\u3002DeepSeekMath 7B \u5728\u4e0d\u4f9d\u8d56\u5916\u90e8\u5de5\u5177\u5305\u548c\u6295\u7968\u6280\u672f\u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u7ade\u8d5b\u7ea7 MATH \u57fa\u51c6\u4e0a\u53d6\u5f97\u4e86\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684 51.7% \u7684\u6210\u7ee9\uff0c\u63a5\u8fd1 Gemini-Ultra \u548c GPT-4 \u7684\u6027\u80fd\u6c34\u5e73\u3002 DeepSeekMath 7B \u7684 64 \u4e2a\u6837\u672c\u7684\u81ea\u6d3d\u6027\u5728 MATH \u4e0a\u8fbe\u5230 60.9%\u3002DeepSeekMath \u7684\u6570\u5b66\u63a8\u7406\u80fd\u529b\u5f52\u56e0\u4e8e\u4e24\u4e2a\u5173\u952e\u56e0\u7d20\uff1a\u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u6570\u636e\u9009\u62e9\u7ba1\u9053\u5229\u7528\u4e86\u516c\u5f00\u53ef\u7528\u7684\u7f51\u7edc\u6570\u636e\u7684\u5de8\u5927\u6f5c\u529b\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f15\u5165\u4e86\u7ec4\u76f8\u5bf9\u7b56\u7565\u4f18\u5316 (GRPO)\uff0c\u8fd9\u662f\u8fd1\u7aef\u7b56\u7565\u4f18\u5316 (PPO) \u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u5b83\u53ef\u4ee5\u589e\u5f3a\u6570\u5b66\u63a8\u7406\u80fd\u529b\uff0c\u540c\u65f6\u4f18\u5316 PPO \u7684\u5185\u5b58\u4f7f\u7528\u91cf\u3002 1\u3001\u5feb\u901f\u5165\u95e8 \u6b64\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 GRPO \u65b9\u6cd5\u8bad\u7ec3\u6a21\u578b\u3002\u6211\u4eec\u4f7f\u7528\u4e2d\u7684\u63d0\u793a\u8bad\u7ec3 \u6a21\u578b\uff08\u5ffd\u7565\u5b8c\u6210\u5217\uff01\uff09\u3002\u4f60\u53ef\u4ee5\u5728\u67e5\u770b\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u3002 \u4ee5\u4e0b\u662f\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\u7684\u811a\u672c\u3002\u8bf7\u6ce8\u610f\uff0c\u524d\u5411\u4f20\u9012\u7684\u8f93\u5165\u5f20\u91cf\u7684\u5927\u5c0f\u4e3a num_generations * per_device_train_batch_size\uff0c\u56e0\u4e3a GRPO \u4e3a\u6279\u6b21\u4e2d\u7684\u6bcf\u4e2a\u63d0\u793a\u751f\u6210 num_generations \u4e2a\u5b8c\u6210\u3002\u9002\u5f53\u8c03\u6574\u8fd9\u4e9b\u503c\u6709\u52a9\u4e8e\u9632\u6b62 OOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-53752","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53752"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53752\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}