Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"پیادهسازی GRPO در TRL","local":"پیادهسازی-grpo-در-trl","sections":[{"title":"مؤلفههای اصلی","local":"مؤلفههای-اصلی","sections":[{"title":"۱. قالب مجموعهداده","local":"۱-قالب-مجموعهداده","sections":[],"depth":3},{"title":"۲. تابع پاداش","local":"۲-تابع-پاداش","sections":[],"depth":3},{"title":"۳. پیکربندی آموزش","local":"۳-پیکربندی-آموزش","sections":[],"depth":3}],"depth":2},{"title":"نکتههایی برای موفقیت","local":"نکتههایی-برای-موفقیت","sections":[],"depth":2},{"title":"طراحی تابع پاداش","local":"طراحی-تابع-پاداش","sections":[{"title":"۱. پاداشهای مبتنی بر طول","local":"۱-پاداشهای-مبتنی-بر-طول","sections":[],"depth":3}],"depth":2},{"title":"۲. پاداشهای مبتنی بر قانون برای کارهای قابلراستیآزمایی","local":"۲-پاداشهای-مبتنی-بر-قانون-برای-کارهای-قابلراستیآزمایی","sections":[],"depth":2},{"title":"۳. پاداشهای مبتنی بر قالب","local":"۳-پاداشهای-مبتنی-بر-قالب","sections":[],"depth":2},{"title":"همین بود!","local":"همین-بود","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/entry/start.CqZXmSrv.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/oyrVqMhC.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/B3UUDp28.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/entry/app.B4UvgX5H.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/BioDZtGL.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/NIl2lQjl.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/DN6uzRQe.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/nodes/0.DcFj1eGj.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/u5kFQ1Zb.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/nodes/32.Bpj1QFON.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/BjCHz3cj.js" rel="modulepreload"> | |
| <!--v383be--><meta name="hf:doc:metadata" content="{"title":"پیادهسازی GRPO در TRL","local":"پیادهسازی-grpo-در-trl","sections":[{"title":"مؤلفههای اصلی","local":"مؤلفههای-اصلی","sections":[{"title":"۱. قالب مجموعهداده","local":"۱-قالب-مجموعهداده","sections":[],"depth":3},{"title":"۲. تابع پاداش","local":"۲-تابع-پاداش","sections":[],"depth":3},{"title":"۳. پیکربندی آموزش","local":"۳-پیکربندی-آموزش","sections":[],"depth":3}],"depth":2},{"title":"نکتههایی برای موفقیت","local":"نکتههایی-برای-موفقیت","sections":[],"depth":2},{"title":"طراحی تابع پاداش","local":"طراحی-تابع-پاداش","sections":[{"title":"۱. پاداشهای مبتنی بر طول","local":"۱-پاداشهای-مبتنی-بر-طول","sections":[],"depth":3}],"depth":2},{"title":"۲. پاداشهای مبتنی بر قانون برای کارهای قابلراستیآزمایی","local":"۲-پاداشهای-مبتنی-بر-قانون-برای-کارهای-قابلراستیآزمایی","sections":[],"depth":2},{"title":"۳. پاداشهای مبتنی بر قالب","local":"۳-پاداشهای-مبتنی-بر-قالب","sections":[],"depth":2},{"title":"همین بود!","local":"همین-بود","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div dir="rtl"><div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="پیادهسازی-grpo-در-trl" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#پیادهسازی-grpo-در-trl"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>پیادهسازی GRPO در TRL</span></h1><!--]--><!----> <p>در این صفحه یاد میگیریم چگونه با استفاده از کتابخانهٔ Transformer Reinforcement Learning (TRL)، بهینهسازی خطمشی نسبی گروهی (GRPO) را پیادهسازی کنیم. تمرکز ما بر پیادهسازی عملی با حداقل کد است.</p> <p>مفاهیم اصلی GRPO را همانگونه که در GRPOTrainer مربوط به TRL پیادهسازی شدهاند بررسی میکنیم و برای راهنمایی از قطعهکدهای مستندات رسمی TRL کمک میگیریم.</p> <blockquote class="tip"><p>این فصل برای افرادی نوشته شده است که با TRL تازه آشنا شدهاند. اگر از قبل با TRL آشنایی دارید، شاید بخواهید <a href="https://github.com/huggingface/open-r1/blob/main/src/open_r1/grpo.py" rel="nofollow">پیادهسازی Open R1</a> از GRPO را هم ببینید.</p></blockquote> <p>ابتدا برخی از مفاهیم مهم الگوریتم GRPO را مرور کنیم:</p> <ul><li>تشکیل گروه: مدل برای هر پرامپت چندین تکمیل تولید میکند.</li> <li>یادگیری ترجیح: مدل از تابع پاداشی یاد میگیرد که گروههای تکمیل را با هم مقایسه میکند.</li> <li>پیکربندی آموزش: مدل از یک پیکربندی برای کنترل فرایند آموزش استفاده میکند.</li></ul> <p>برای پیادهسازی GRPO چه کارهایی لازم است؟</p> <ul><li>یک مجموعهداده از پرامپتها تعریف کنید.</li> <li>تابع پاداشی تعریف کنید که فهرستی از تکمیلها را میگیرد و فهرستی از پاداشها را برمیگرداند.</li> <li>فرایند آموزش را با GRPOConfig پیکربندی کنید.</li> <li>مدل را با GRPOTrainer آموزش دهید.</li></ul> <p>در ادامه یک مثال حداقلی برای شروع آموزش با GRPO میبینید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> trl <span class="hljs-keyword">import</span> GRPOTrainer, GRPOConfig | |
| <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset | |
| <span class="hljs-comment"># 1. Load your dataset</span> | |
| dataset = load_dataset(<span class="hljs-string">"your_dataset"</span>, split=<span class="hljs-string">"train"</span>) | |
| <span class="hljs-comment"># 2. Define a simple reward function</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">reward_func</span>(<span class="hljs-params">completions, **kwargs</span>): | |
| <span class="hljs-string">"""Example: Reward longer completions"""</span> | |
| <span class="hljs-keyword">return</span> [<span class="hljs-built_in">float</span>(<span class="hljs-built_in">len</span>(completion)) <span class="hljs-keyword">for</span> completion <span class="hljs-keyword">in</span> completions] | |
| <span class="hljs-comment"># 3. Configure training</span> | |
| training_args = GRPOConfig( | |
| output_dir=<span class="hljs-string">"output"</span>, | |
| num_train_epochs=<span class="hljs-number">3</span>, | |
| per_device_train_batch_size=<span class="hljs-number">4</span>, | |
| gradient_accumulation_steps=<span class="hljs-number">2</span>, | |
| logging_steps=<span class="hljs-number">10</span>, | |
| ) | |
| <span class="hljs-comment"># 4. Initialize and train</span> | |
| trainer = GRPOTrainer( | |
| model=<span class="hljs-string">"your_model"</span>, <span class="hljs-comment"># e.g. "Qwen/Qwen2-0.5B-Instruct"</span> | |
| args=training_args, | |
| train_dataset=dataset, | |
| reward_funcs=reward_func, | |
| ) | |
| trainer.train()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="مؤلفههای-اصلی" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#مؤلفههای-اصلی"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>مؤلفههای اصلی</span></h2><!--]--><!----> <!--[2--><h3 class="relative group"><a id="۱-قالب-مجموعهداده" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۱-قالب-مجموعهداده"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۱. قالب مجموعهداده</span></h3><!--]--><!----> <p>مجموعهدادهٔ شما باید شامل پرامپتهایی باشد که مدل به آنها پاسخ میدهد. آموزشدهندهٔ GRPO برای هر پرامپت چندین تکمیل تولید میکند و با استفاده از تابع پاداش آنها را مقایسه میکند.</p> <!--[2--><h3 class="relative group"><a id="۲-تابع-پاداش" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۲-تابع-پاداش"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۲. تابع پاداش</span></h3><!--]--><!----> <p>تابع پاداش اهمیت زیادی دارد؛ این تابع تعیین میکند مدل چگونه یاد بگیرد. در ادامه دو مثال عملی میبینید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-comment"># Example 1: Reward based on completion length</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">reward_length</span>(<span class="hljs-params">completions, **kwargs</span>): | |
| <span class="hljs-keyword">return</span> [<span class="hljs-built_in">float</span>(<span class="hljs-built_in">len</span>(completion)) <span class="hljs-keyword">for</span> completion <span class="hljs-keyword">in</span> completions] | |
| <span class="hljs-comment"># Example 2: Reward based on matching a pattern</span> | |
| <span class="hljs-keyword">import</span> re | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">reward_format</span>(<span class="hljs-params">completions, **kwargs</span>): | |
| pattern = <span class="hljs-string">r"^<think>.*?</think><answer>.*?</answer>$"</span> | |
| <span class="hljs-keyword">return</span> [<span class="hljs-number">1.0</span> <span class="hljs-keyword">if</span> re.<span class="hljs-keyword">match</span>(pattern, c) <span class="hljs-keyword">else</span> <span class="hljs-number">0.0</span> <span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> completions]<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="۳-پیکربندی-آموزش" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۳-پیکربندی-آموزش"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۳. پیکربندی آموزش</span></h3><!--]--><!----> <p>پارامترهای مهمی که باید در <code>GRPOConfig</code> در نظر بگیرید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->training_args = GRPOConfig( | |
| <span class="hljs-comment"># Essential parameters</span> | |
| output_dir=<span class="hljs-string">"output"</span>, | |
| num_train_epochs=<span class="hljs-number">3</span>, | |
| num_generation=<span class="hljs-number">4</span>, <span class="hljs-comment"># Number of completions to generate for each prompt</span> | |
| per_device_train_batch_size=<span class="hljs-number">4</span>, <span class="hljs-comment"># We want to get all generations in one device batch</span> | |
| <span class="hljs-comment"># Optional but useful</span> | |
| gradient_accumulation_steps=<span class="hljs-number">2</span>, | |
| learning_rate=<span class="hljs-number">1e-5</span>, | |
| logging_steps=<span class="hljs-number">10</span>, | |
| <span class="hljs-comment"># GRPO specific (optional)</span> | |
| use_vllm=<span class="hljs-literal">True</span>, <span class="hljs-comment"># Speed up generation</span> | |
| )<!----></pre></div><!----> <p>پارامتر <code>num_generation</code> برای GRPO اهمیت ویژهای دارد، چون اندازهٔ گروه، یعنی تعداد تکمیلهای متفاوتی را که مدل برای هر پرامپت تولید میکند، تعیین میکند. این ویژگی GRPO را از روشهای دیگر RL متمایز میسازد:</p> <ul><li>خیلی کوچک (مثلاً ۲ تا ۳): ممکن است تنوع کافی برای مقایسههای معنادار فراهم نکند.</li> <li>مقدار پیشنهادی (۴ تا ۱۶): تعادل خوبی بین تنوع و بهرهوری محاسباتی ایجاد میکند.</li> <li>مقادیر بزرگتر: ممکن است یادگیری را بهتر کنند، اما هزینهٔ محاسباتی را بهطور چشمگیری افزایش میدهند.</li></ul> <p>اندازهٔ گروه را باید بر اساس منابع محاسباتی و پیچیدگی کار خود انتخاب کنید. برای کارهای ساده، گروههای کوچکتر (۴ تا ۸) ممکن است کافی باشند، درحالیکه کارهای استدلالی پیچیدهتر شاید از گروههای بزرگتر (۸ تا ۱۶) سود ببرند.</p> <!--[1--><h2 class="relative group"><a id="نکتههایی-برای-موفقیت" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#نکتههایی-برای-موفقیت"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>نکتههایی برای موفقیت</span></h2><!--]--><!----> <ol><li><strong>مدیریت حافظه</strong>: <code>per_device_train_batch_size</code> و <code>gradient_accumulation_steps</code> را بر اساس حافظهٔ GPU خود تنظیم کنید.</li> <li><strong>سرعت</strong>: اگر مدل شما پشتیبانی میشود، برای تولید سریعتر <code>use_vllm=True</code> را فعال کنید.</li> <li><strong>پایش</strong>: در طول آموزش، معیارهای ثبتشده را بررسی کنید: <ul><li><code>reward</code>: میانگین پاداش در میان تکمیلها</li> <li><code>reward_std</code>: انحراف معیار در گروههای پاداش</li> <li><code>kl</code>: واگرایی KL از مدل مرجع</li></ul></li></ol> <!--[1--><h2 class="relative group"><a id="طراحی-تابع-پاداش" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#طراحی-تابع-پاداش"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>طراحی تابع پاداش</span></h2><!--]--><!----> <p>مقالهٔ DeepSeek R1 چند رویکرد مؤثر برای طراحی تابع پاداش نشان میدهد که میتوانید آنها را برای پیادهسازی GRPO خود سازگار کنید:</p> <!--[2--><h3 class="relative group"><a id="۱-پاداشهای-مبتنی-بر-طول" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۱-پاداشهای-مبتنی-بر-طول"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۱. پاداشهای مبتنی بر طول</span></h3><!--]--><!----> <p>یکی از سادهترین پاداشها برای پیادهسازی، پاداش مبتنی بر طول است. میتوانید به تکمیلهای طولانیتر پاداش بدهید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">reward_len</span>(<span class="hljs-params">completions, **kwargs</span>): | |
| ideal_length = <span class="hljs-number">20</span> | |
| <span class="hljs-keyword">return</span> [-<span class="hljs-built_in">abs</span>(ideal_length - <span class="hljs-built_in">len</span>(completion)) <span class="hljs-keyword">for</span> completion <span class="hljs-keyword">in</span> completions]<!----></pre></div><!----> <p>این تابع پاداش تکمیلهایی را که بیش از حد کوتاه یا بلند هستند جریمه میکند و مدل را تشویق میکند تکمیلهایی نزدیک به طول ایدئال ۲۰ توکن تولید کند.</p> <iframe src="https://marimo.app/gh/huggingface/notebooks/main/e?entrypoint=course%2Fen%2Fchapter13%2Fgrpo_length.py&embed=true&show-chrome=false" title="Marimo Notebook" width="100%" height="800px" frameborder="0" allow="clipboard-write"></iframe> <!--[1--><h2 class="relative group"><a id="۲-پاداشهای-مبتنی-بر-قانون-برای-کارهای-قابلراستیآزمایی" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۲-پاداشهای-مبتنی-بر-قانون-برای-کارهای-قابلراستیآزمایی"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۲. پاداشهای مبتنی بر قانون برای کارهای قابلراستیآزمایی</span></h2><!--]--><!----> <p>برای کارهایی با پاسخهای عینی و درست (مانند ریاضی یا کدنویسی)، میتوانید توابع پاداش مبتنی بر قانون پیادهسازی کنید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">problem_reward</span>(<span class="hljs-params">completions, answers, **kwargs</span>): | |
| <span class="hljs-string">"""Reward function for math problems with verifiable answers | |
| completions: list of completions to evaluate | |
| answers: list of answers to the problems from the dataset | |
| """</span> | |
| rewards = [] | |
| <span class="hljs-keyword">for</span> completion, correct_answer <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(completions, answers): | |
| <span class="hljs-comment"># Extract the answer from the completion</span> | |
| <span class="hljs-keyword">try</span>: | |
| <span class="hljs-comment"># This is a simplified example - you'd need proper parsing</span> | |
| answer = extract_final_answer(completion) | |
| <span class="hljs-comment"># Binary reward: 1 for correct, 0 for incorrect</span> | |
| reward = <span class="hljs-number">1.0</span> <span class="hljs-keyword">if</span> answer == correct_answer <span class="hljs-keyword">else</span> <span class="hljs-number">0.0</span> | |
| rewards.append(reward) | |
| <span class="hljs-keyword">except</span>: | |
| <span class="hljs-comment"># If we can't parse an answer, give a low reward</span> | |
| rewards.append(<span class="hljs-number">0.0</span>) | |
| <span class="hljs-keyword">return</span> rewards<!----></pre></div><!----> <iframe src="https://marimo.app/gh/huggingface/notebooks/main/e?entrypoint=course%2Fen%2Fchapter13%2Fgrpo_math.py&embed=true&show-chrome=false" title="Marimo Notebook" width="100%" height="800px" frameborder="0" allow="clipboard-write"></iframe> <!--[1--><h2 class="relative group"><a id="۳-پاداشهای-مبتنی-بر-قالب" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۳-پاداشهای-مبتنی-بر-قالب"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۳. پاداشهای مبتنی بر قالب</span></h2><!--]--><!----> <p>همچنین میتوانید به قالببندی درست پاداش بدهید؛ این موضوع در آموزش DeepSeek R1 اهمیت داشت:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">format_reward</span>(<span class="hljs-params">completions, **kwargs</span>): | |
| <span class="hljs-string">"""Reward completions that follow the desired format"""</span> | |
| <span class="hljs-comment"># Example: Check if the completion follows a think-then-answer format</span> | |
| pattern = <span class="hljs-string">r"<think>(.*?)</think>\s*<answer>(.*?)</answer>"</span> | |
| rewards = [] | |
| <span class="hljs-keyword">for</span> completion <span class="hljs-keyword">in</span> completions: | |
| <span class="hljs-keyword">match</span> = re.search(pattern, completion, re.DOTALL) | |
| <span class="hljs-keyword">if</span> <span class="hljs-keyword">match</span>: | |
| <span class="hljs-comment"># Check if there's substantial content in both sections</span> | |
| think_content = <span class="hljs-keyword">match</span>.group(<span class="hljs-number">1</span>).strip() | |
| answer_content = <span class="hljs-keyword">match</span>.group(<span class="hljs-number">2</span>).strip() | |
| <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(think_content) > <span class="hljs-number">20</span> <span class="hljs-keyword">and</span> <span class="hljs-built_in">len</span>(answer_content) > <span class="hljs-number">0</span>: | |
| rewards.append(<span class="hljs-number">1.0</span>) | |
| <span class="hljs-keyword">else</span>: | |
| rewards.append( | |
| <span class="hljs-number">0.5</span> | |
| ) <span class="hljs-comment"># Partial reward for correct format but limited content</span> | |
| <span class="hljs-keyword">else</span>: | |
| rewards.append(<span class="hljs-number">0.0</span>) <span class="hljs-comment"># No reward for incorrect format</span> | |
| <span class="hljs-keyword">return</span> rewards<!----></pre></div><!----> <iframe src="https://marimo.app/gh/huggingface/notebooks/main/e?entrypoint=course%2Fen%2Fchapter13%2Fgrpo_format.py&embed=true&show-chrome=false" title="Marimo Notebook" width="100%" height="800px" frameborder="0" allow="clipboard-write"></iframe> <p>این مثالها نشان میدهند چگونه میتوانید با الهام از فرایند آموزش DeepSeek R1، توابع پاداشی با تمرکز بر درستی، قالببندی و سیگنالهای ترکیبی پیادهسازی کنید.</p> <!--[1--><h2 class="relative group"><a id="همین-بود" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#همین-بود"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>همین بود!</span></h2><!--]--><!----> <p>در بخش بعدی، تمرینی را دنبال میکنید تا GRPO را در TRL پیادهسازی کنید.</p></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/course/blob/main/chapters/fa/chapter12/4.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_pfm8ny = { | |
| base: "/docs/course/pr_1364/fa", | |
| assets: "/docs/course/pr_1364/fa" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/course/pr_1364/fa/_app/immutable/entry/start.CqZXmSrv.js"), | |
| import("/docs/course/pr_1364/fa/_app/immutable/entry/app.B4UvgX5H.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 32], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 43.7 kB
- Xet hash:
- 2fd1b9d2706796affa66917b228e2d6426527d7d95c6794e6777e87eb6f53a49
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.