【GRPO】GRPO原理原文翻译
论文:DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models
注!这里我仅仅翻译GRPO部分供学习使用。其他部分请去看原文。
4. 强化学习(Reinforcement Learning)
4.1. 群组相对策略优化(Group Relative Policy Optimization, GRPO)
强化学习(RL)已被证明可以在监督微调(Supervised Fine-Tuning, SFT)阶段后进一步提升 LLM 的数学推理能力([WizardMath, Luo et al., 2023]; [Math-Shepherd, Wang et al., 2023b])。在本节中,我们介绍一种高效且有效的 RL 算法——群组相对策略优化(GRPO)。
4.1.1. 从 PPO 到 GRPO(From PPO to GRPO)
近端策略优化(Proximal Policy Optimization, PPO)(Schulman et al., 2017)是一种Actor-Critic 风格的强化学习算法,广泛应用于 LLM 的 RL 微调阶段(Ouyang et al., 2022)。具体而言,它通过最大化以下代理目标函数来优化 LLM:
J P P O ( θ ) = E [ q ∼ P ( Q ) , o ∼ π θ o l d ( O ∣ q ) ] 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ min [ π θ ( o t ∣ q , o < t ) π θ o l d ( o t ∣ q , o < t ) A t , clip ( π θ ( o t ∣ q , o < t ) π θ o l d ( o t ∣ q , o < t ) , 1 − ϵ , 1 + ϵ ) A t ] , ( 1 ) \mathcal{J}_{PPO}(\theta) = \mathbb{E}[q \sim P(Q), o \sim \pi_{\theta_{old}}(O|q)] \frac{1}{|o|} \sum_{t=1}^{|o|} \min \left[ \frac{\pi_{\theta}(o_t|q, o_{<t})}{\pi_{\theta_{old}}(o_t|q, o_{<t})} A_t, \text{clip} \left( \frac{\pi_{\theta}(o_t|q, o_{<t})}{\pi_{\theta_{old}}(o_t|q, o_{<t})}, 1-\epsilon, 1+\epsilon \right) A_t \right], (1) JPPO(θ)=E[q∼P(Q),o∼πθold(O∣q)]∣o∣1t=1∑∣o∣min[πθold(ot∣q,o<t)πθ(ot∣q,o<t)At,clip(πθold(ot∣q,o<t)πθ(ot∣q,o<t),1−ϵ,1+ϵ)At],(1)
其中, π θ \pi_{\theta} πθ 和 π θ o l d \pi_{\theta_{old}} πθold 分别表示当前和旧的策略模型, q , o q, o q,o 是从问题数据集中采样的问题和输出,而 π θ o l d \pi_{\theta_{old}} πθold 代表旧策略模型。 ϵ \epsilon ϵ 是一个裁剪相关的超参数,在 PPO 中引入以稳定训练过程。 A t A_t At 代表优势值(Advantage),通过广义优势估计(Generalized Advantage Estimation, GAE) 计算(Schulman et al., 2015),基于奖励 { r ≥ t } \{r_{\geq t}\} {r≥t} 和学习的值函数 V ψ V_{\psi} Vψ。
因此,在 PPO 中,价值函数(Value Function)需要与策略模型(Reference Model)一起训练。为了缓解对奖励模型的过度优化,标准方法是在每个token的奖励中添加一个来自参考模型的per-token KL 惩罚项(Ouyang et al., 2022),即:
r t = r φ ( q , o ≤ t ) − β log π θ ( o t ∣ q , o < t ) π r e f ( o t ∣ q , o < t ) , ( 2 ) r_t = r_{\varphi}(q, o_{\leq t}) - \beta \log \frac{\pi_{\theta}(o_t|q, o_{<t})}{\pi_{ref}(o_t|q, o_{<t})}, (2) rt=rφ(q,o≤t)−βlogπref(ot∣q,o<t)πθ(ot∣q,o<t),(2)
其中, r φ r_{\varphi} rφ 是奖励模型, π r e f \pi_{ref} πref 是参考模型,通常为初始 SFT 模型, β \beta β 是 KL 罚项的系数。
在 PPO 中使用的价值函数通常是与策略模型规模相当的另一个模型,这会带来大量的内存占用和计算负担。此外,在 RL 训练过程中,价值函数被用作基线来计算优势值,以降低方差。在大语言模型(LLM)上下文中,通常只有最后一个 token 会被奖励模型分配奖励分数,这可能会使得训练一个在每个 token 上都准确的价值函数变得更加复杂。
为了解决这个问题,正如 Figure 4 所示,我们提出了群组相对策略优化(Group Relative Policy Optimization, GRPO)。GRPO 不需要额外的价值函数近似计算(如 PPO 中所需),而是使用同一问题的多个采样输出的平均奖励作为基线。具体而言,对于每个问题 q q q,GRPO 从旧策略 π θ o l d \pi_{\theta_{old}} πθold 采样得到一组输出 { o 1 , o 2 , ⋯ , o G } \{o_1, o_2, \cdots , o_G\} {o1,o2,⋯,oG},然后优化策略模型,使其最大化以下目标函数:
J G R P O ( θ ) = E [ q ∼ P ( Q ) , { o i } i = 1 G ∼ π θ o l d ( O ∣ q ) ] \mathcal{J}_{GRPO}(\theta) = \mathbb{E}[q \sim P(Q), \{o_i\}_{i=1}^{G} \sim \pi_{\theta_{old}}(O|q)] JGRPO(θ)=E[q∼P(Q),{oi}i=1G∼πθold(O∣q)]
1 G ∑ i = 1 G 1 ∣ o i ∣ ∑ t = 1 ∣ o i ∣ ( min [ π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) A ^ i , t , clip ( π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) , 1 − ϵ , 1 + ϵ ) A ^ i , t ] − β D K L [ π θ ∣ ∣ π r e f ] ) , ( 3 ) \frac{1}{G} \sum_{i=1}^{G} \frac{1}{|o_i|} \sum_{t=1}^{|o_i|} \left( \min \left[ \frac{\pi_{\theta}(o_{i,t}|q, o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q, o_{i,<t})} \hat{A}_{i,t}, \text{clip} \left( \frac{\pi_{\theta}(o_{i,t}|q, o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q, o_{i,<t})}, 1-\epsilon, 1+\epsilon \right) \hat{A}_{i,t} \right] - \beta D_{KL} [\pi_{\theta}||\pi_{ref}] \right), (3) G1i=1∑G∣oi∣1t=1∑∣oi∣(min[πθold(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)A^i,t,clip(πθold(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t),1−ϵ,1+ϵ)A^i,t]−βDKL[πθ∣∣πref]),(3)
其中, ϵ \epsilon ϵ 和 β \beta β 是超参数, A ^ i , t \hat{A}_{i,t} A^i,t 是基于群组内部的相对奖励计算的优势值,后续小节将详细介绍其计算方式。GRPO 利用群组相对奖励的方式计算优势值,这一方式与奖励模型的相对评分特性很好地对齐,因为奖励模型通常是在比较相同问题的不同输出的数据集上进行训练的。
值得注意的是,与其在奖励中添加 KL 罚项,GRPO 直接在损失函数中正则化训练策略与参考策略的 KL 散度,避免了因 KL 计算而复杂化 A ^ i , t \hat{A}_{i,t} A^i,t 的问题。
不同于在方程 (2) 中使用的 KL 罚项,我们使用以下无偏估计器(Schulman, 2020)来估计 KL 散度:
D K L [ π θ ∣ ∣ π r e f ] = π r e f ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − log π r e f ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − 1 , ( 4 ) D_{KL} [\pi_{\theta}||\pi_{ref}] = \frac{\pi_{ref}(o_{i,t}|q, o_{i,<t})}{\pi_{\theta}(o_{i,t}|q, o_{i,<t})} - \log \frac{\pi_{ref}(o_{i,t}|q, o_{i,<t})}{\pi_{\theta}(o_{i,t}|q, o_{i,<t})} - 1, \space \space (4) DKL[πθ∣∣πref]=πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−logπθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−1, (4)
其中该估计器保证为正值。
此公式来自于 Schulman 的博客,blog地址.
我的另一篇文章 KL 散度的蒙特卡洛近似 是对这篇博客的翻译,感兴趣的同学可以跳转阅读。
------------------------------ 此段落内容为注释内容(非原论文内容)---------------------
无偏估计
无偏估计(Unbiased Estimation)是统计学中的一个重要概念,指的是 估计量的期望值等于其被估计的参数值,即估计量在多次抽样的情况下不会系统性地偏离真实值。
数学定义 设 θ ^ \hat{\theta} θ^ 是对某个参数 θ \theta θ 的估计量。如果满足: E [ θ ^ ] = θ E[\hat{\theta}] = \theta E[θ^]=θ 则称 θ ^ \hat{\theta} θ^ 是 θ \theta θ 的无偏估计量。
该公式是KL 散度的无偏估计器,我们可以从以下几个方面来解释其无偏性:
1. KL 散度的定义 KL 散度的标准定义如下: D K L ( π θ ∥ π ref ) = E o i , t ∼ π θ [ log π θ ( o i , t ∣ q , o i , < t ) π ref ( o i , t ∣ q , o i , < t ) ] D_{KL}(\pi_{\theta} \| \pi_{\text{ref}}) = \mathbb{E}_{o_{i,t} \sim \pi_{\theta}} \left[ \log \frac{\pi_{\theta} (o_{i,t} | q, o_{i,<t})}{\pi_{\text{ref}} (o_{i,t} | q, o_{i,<t})} \right] DKL(πθ∥πref)=Eoi,t∼πθ[logπref(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)] 即,它是一个关于 π θ \pi_{\theta} πθ采样分布的期望。
2. 公式的推导 观察该无偏估计器: D K L [ π θ ∥ π ref ] = π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − log π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − 1 D_{KL}[\pi_{\theta} \| \pi_{\text{ref}}] = \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - \log \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - 1 DKL[πθ∥πref]=πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−logπθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−1
我们可以对这个估计器求期望,并验证它是否等于真实 KL 散度:
E o i , t ∼ π θ [ π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − log π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − 1 ] \mathbb{E}_{o_{i,t} \sim \pi_{\theta}} \left[ \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - \log \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - 1 \right] Eoi,t∼πθ[πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−logπθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−1]
使用采样期望: ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) [ π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − log π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − 1 ] \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) \left[ \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - \log \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - 1 \right] oi,t∑πθ(oi,t∣q,oi,<t)[πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−logπθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−1]
拆开求和: ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) log π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) − ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) \log \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} - \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) oi,t∑πθ(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−oi,t∑πθ(oi,t∣q,oi,<t)logπθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)−oi,t∑πθ(oi,t∣q,oi,<t)
由于: ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) = 1 \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) = 1 oi,t∑πθ(oi,t∣q,oi,<t)=1 ∑ o i , t π θ ( o i , t ∣ q , o i , < t ) π ref ( o i , t ∣ q , o i , < t ) π θ ( o i , t ∣ q , o i , < t ) = ∑ o i , t π ref ( o i , t ∣ q , o i , < t ) = 1 \sum_{o_{i,t}} \pi_{\theta} (o_{i,t} | q, o_{i,<t}) \frac{\pi_{\text{ref}}(o_{i,t} | q, o_{i,<t})}{\pi_{\theta}(o_{i,t} | q, o_{i,<t})} = \sum_{o_{i,t}} \pi_{\text{ref}}(o_{i,t} | q, o_{i,<t}) = 1 oi,t∑πθ(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)πref(oi,t∣q,oi,<t)=oi,t∑πref(oi,t∣q,oi,<t)=1
所以: E [ D K L [ π θ ∥ π ref ] ] = 1 − D K L ( π θ ∥ π ref ) − 1 = D K L ( π θ ∥ π ref ) \mathbb{E}[D_{KL}[\pi_{\theta} \| \pi_{\text{ref}}]] = 1 - D_{KL}(\pi_{\theta} \| \pi_{\text{ref}}) - 1 = D_{KL}(\pi_{\theta} \| \pi_{\text{ref}}) E[DKL[πθ∥πref]]=1−DKL(πθ∥πref)−1=DKL(πθ∥πref)
这表明,该估计器的期望值正好等于真实 KL 散度,因此它是无偏的(Unbiased)。
直观解释
- 传统的 KL 估计通常需要从 π θ \pi_{\theta} πθ 采样多个数据点,并计算均值,但这种方法可能会有偏差。
- 这里的估计器通过重要性采样的思想,对 π ref \pi_{\text{ref}} πref 进行加权修正,从而确保了无偏性。
- 该估计器在采样过程中并不会系统性地高估或低估 KL 散度,而是保证其均值与真实 KL 相等。
4.1.2. 使用 GRPO 的结果监督强化学习(Outcome Supervision RL with GRPO)
形式化地讲,对于每个问题 q q q,从旧的策略模型 π θ o l d \pi_{\theta_{old}} πθold 采样得到一组输出 { o 1 , o 2 , ⋯ , o G } \{o_1, o_2, \cdots , o_G\} {o1,o2,⋯,oG}。然后,使用一个奖励模型对这些输出进行评分,得到 G G G 个奖励:
r = { r 1 , r 2 , ⋯ , r G } r = \{r_1, r_2, \cdots , r_G\} r={r1,r2,⋯,rG}
随后,这些奖励通过减去组平均值并除以组标准差进行归一化。结果监督仅在每个输出 o i o_i oi 结束时提供奖励,并将所有标记的优势值 A ^ i , t \hat{A}_{i,t} A^i,t 设为归一化奖励,即:
A ^ i , t = r ~ i = r i − mean ( r ) std ( r ) \hat{A}_{i,t} = \tilde{r}_i = \frac{r_i - \text{mean}(r)}{\text{std}(r)} A^i,t=r~i=std(r)ri−mean(r)
然后,通过最大化方程 (3) 中定义的目标来优化策略。
4.1.3. 使用 GRPO 的过程监督强化学习(Process Supervision RL with GRPO)
结果监督仅在每个输出结束时提供奖励,但在复杂的数学任务中,这可能不足以有效监督策略。遵循 Math-Shepherd: Verify and Reinforce LLMs Step-by-step without Human Annotations[Wang et al., 2023b] 的方法,我们进一步探索过程监督(Process Supervision),它在每个推理步骤结束时提供奖励。
具体而言,给定问题 q q q 和 G G G 个采样输出 { o 1 , o 2 , ⋯ , o G } \{o_1, o_2, \cdots , o_G\} {o1,o2,⋯,oG},过程奖励模型(Process Reward Model) 用于对每个步骤的输出进行评分,得到对应的奖励:
R = { { r 1 index ( 1 ) , ⋯ , r 1 index ( K 1 ) } , ⋯ , { r G index ( 1 ) , ⋯ , r G index ( K G ) } } R = \{\{r_1^{\text{index}(1)}, \cdots , r_1^{\text{index}(K_1)}\}, \cdots , \{r_G^{\text{index}(1)}, \cdots , r_G^{\text{index}(K_G)}\} \} R={{r1index(1),⋯,r1index(K1)},⋯,{rGindex(1),⋯,rGindex(KG)}}
其中, index ( j ) \text{index}(j) index(j) 表示第 j j j 步的终止标记索引, K i K_i Ki 是第 i i i 个输出中的总步数。
我们同样对这些奖励进行归一化,计算方式如下:
r ~ i index ( j ) = r i index ( j ) − mean ( R ) std ( R ) \tilde{r}_i^{\text{index}(j)} = \frac{r_i^{\text{index}(j)} - \text{mean}(R)}{\text{std}(R)} r~iindex(j)=std(R)riindex(j)−mean(R)
随后,过程监督计算每个标记的优势值,作为该标记后续所有奖励的归一化和,即:
A ^ i , t = ∑ index ( j ) ≥ t r ~ i index ( j ) \hat{A}_{i,t} = \sum_{\text{index}(j) \geq t} \tilde{r}_i^{\text{index}(j)} A^i,t=index(j)≥t∑r~iindex(j)
然后,通过最大化方程 (3) 中定义的目标来优化策略。
4.1.4. 迭代 GRPO 强化学习(Iterative RL with GRPO)
随着强化学习训练过程的推进,旧的奖励模型可能不足以有效监督当前的策略模型。因此,我们进一步探索迭代 GRPO 强化学习(Iterative RL with GRPO)。如算法 1 所示,在迭代 GRPO 中,我们根据策略模型的采样结果生成新的训练数据集,并通过重放机制(Replay Mechanism)结合 10% 的历史数据,持续训练旧的奖励模型。然后,我们将参考模型设置为当前策略模型,并使用更新后的奖励模型不断训练策略模型。
4.2. DeepSeekMath-RL 的训练与评估(Training and Evaluating DeepSeekMath-RL)
我们基于 DeepSeekMath-Instruct 7B 进行强化学习(RL)训练。RL 训练数据来自 SFT 数据集中与 GSM8K 和 MATH 相关的链式思维(Chain-of-Thought, CoT)格式问题,总计约 144K 个问题。为了研究 RL 在缺乏 RL 训练数据的基准测试上的影响,我们排除了其他 SFT 任务。我们依据 Math-Shepherd: Verify and Reinforce LLMs Step-by-step without Human Annotations[Wang et al., 2023b] 的方法构建奖励模型的训练数据集。
我们基于 DeepSeekMath-Base 7B 训练初始奖励模型,学习率设置为 2e-5。对于 GRPO,我们设定策略模型的学习率为 1e-6,KL 系数(KL coefficient)为 0.04。对于每个问题,我们采样 64 个输出,最大序列长度设定为 1024,训练批次大小(batch size)为 1024。在每个探索阶段后,策略模型仅进行一次更新。我们在 DeepSeekMath-Instruct 7B 相关基准测试上评估 DeepSeekMath-RL 7B 的性能。
在 DeepSeekMath-RL 7B 相关的评测中:
- DeepSeekMath-RL 7B, GSM8K 和 MATH 的链式思维推理任务被视为域内任务(In-domain tasks)。
- 其他基准测试被视为域外任务(Out-of-domain tasks)。
Table 5 展示了在英文和中文基准测试上的开放式和封闭式问题的链式思维推理(Chain-of-Thought)及工具整合推理(Tool-Integrated Reasoning)的表现。我们的研究发现:
- DeepSeekMath-RL 7B 在 GSM8K 和 MATH 上的准确率分别达到了 88.2% 和 51.7%,成功利用了链式思维推理。这一性能超越了所有开源模型(7B-70B 规模),并且在大多数封闭模型上表现优异。
- DeepSeekMath-RL 7B 仅在链式思维格式的 SFT 训练数据上进行强化学习,直接从 DeepSeekMath-Instruct 7B 开始训练。尽管 RL 训练数据量受限,DeepSeekMath-RL 7B 在所有评估指标上超越了 DeepSeekMath-Instruct 7B,展示了强化学习带来的持续改进能力。