欢迎关注我的CSDN:https://spike.blog.csdn.net/
本文地址:https://spike.blog.csdn.net/article/details/146533892
在强化学习算法中,DAPO (Decoupled Clip and Dynamic Sampling Policy Optimization),通过解耦裁剪和动态采样策略提升模型的推理能力。与 GRPO (Group Relative Policy Optimization) 相比,DRPO 移除 KL 散度惩罚项,允许模型在长推理任务中自由探索,同时,通过调整上下裁剪范围,增加低概率 Token 探索能力,有效缓解熵崩溃问题。DRPO 引入动态采样策略,过滤掉准确率为 0 或 1 的无效样本,确保每个批次中的样本,具有有效的梯度信号,从而提升训练效率和模型的收敛速度。Dr. GRPO (GRPO Done Right) 解决 GRPO 优化中的偏差问题,提出的改进方案,通过删除长度归一化项和标准差标准化项,解决 GRPO 可能导致错误响应逐渐变长的问题。同时,Dr. GRPO 在掩码均值函数中,将 mask.sum(axis=dim)
替换为固定值 MAX_TOKENS
,以确保优化目标的无偏性。能够有效缓解优化偏差,显著减少错误回答的长度,同时保持模型的推理性能。
DAPO,即 Decoupled Clip and Dynamic sAmpling Policy Optimization,解耦剪裁与动态采样策略优化
- DAPO: an Open-Source LLM Reinforcement Learning System at Scale
Dr. GRPO,即 GRPO Done Right
- Dr. GRPO: Understanding R1-Zero-Like Training: A Critical Perspective
GitHub:
- DAPO:https://dapo-sia.github.io/
- Dr. GRPO:https://github.com/sail-sg/understand-r1-zero
标准的 GRPO,如下:
1 G ∑ i = 1 G 1 ∣ o i ∣ ∑ t = 1 ∣ o i ∣ { m i n [ π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) A ^ i , t , c l i p ( π θ ( 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 ) } w h e r e A ^ i , t = R ( q , o i ) − m e a n ( { R ( q , o i ) , . . . , R ( q , o G ) } ) s t d ( { R ( q , o i ) , . . . , R ( q , o G ) } ) \frac{1}{G}\sum_{i=1}^{G} \frac{1}{|o_{i}|}\sum_{t=1}^{|o_{i}|} \big\{ min \big[ \frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}\hat{A}_{i,t}, clip(\frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}, 1-\epsilon, 1+\epsilon)\hat{A}_{i,t} \big] -\beta \mathbb{D}_{KL}(\pi_{\theta}||\pi_{ref}) \big\} \\ where \ \hat{A}_{i,t}=\frac{R(q,o_{i}) - mean(\{R(q,o_{i}),...,R(q,o_{G})\})}{std(\{R(q,o_{i}),...,R(q,o_{G})\})} 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)}where A^i,t=std({R(q,oi),...,R(q,oG)})R(q,oi)−mean({R(q,oi),...,R(q,oG)})
DAPO (Decoupled Clip and Dynamic sAmpling Policy Optimization),即 解耦剪裁与动态采样策略优化,即 (1) 增加 Clip 上界范围,(2) 去除 全正或全错的采样,(3) 修改 Token-Level 全平均 代替 Sample-Level 分组平均。
1 ∑ i = 1 G ∣ o i ∣ 1 G ∑ i = 1 G ∑ t = 1 ∣ o i ∣ { m i n [ π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) A ^ i , t , c l i p ( π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) , 1 − ϵ l o w , 1 + ϵ u p ) A ^ i , t ] } w h e r e A ^ i , t = R ( q , o i ) − m e a n ( { R ( q , o i ) , . . . , R ( q , o G ) } ) s t d ( { R ( q , o i ) , . . . , R ( q , o G ) } ) s . t . 0 < ∣ { o i ∣ i s _ e q u i v a l e n t ( a , o i ) } ∣ < G \frac{1}{\sum_{i=1}^{G}|o_{i}|} \frac{1}{G}\sum_{i=1}^{G} \sum_{t=1}^{|o_{i}|} \big\{ min \big[ \frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}\hat{A}_{i,t}, clip(\frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}, 1-\epsilon_{low}, 1+\epsilon_{up})\hat{A}_{i,t} \big] \big\} \\ where \ \hat{A}_{i,t}=\frac{R(q,o_{i}) - mean(\{R(q,o_{i}),...,R(q,o_{G})\})}{std(\{R(q,o_{i}),...,R(q,o_{G})\})} \\ s.t. \ 0 < \big| \{{o_{i}|is\_equivalent(a,o_{i})} \} \big| < G ∑i=1G∣oi∣1G1i=1∑Gt=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−ϵlow,1+ϵup)A^i,t]}where A^i,t=std({R(q,oi),...,R(q,oG)})R(q,oi)−mean({R(q,oi),...,R(q,oG)})s.t. 0< {oi∣is_equivalent(a,oi)} <G
Dr. GRPO (GRPO Done Right),即 运行正确的 GRPO,即 (1)去掉 序列长度 1 ∣ o i ∣ \frac{1}{|o_{i}|} ∣oi∣1,(2)去掉 优势方差 s t d std std,如下:
1 G ∑ i = 1 G ∑ t = 1 ∣ o i ∣ { m i n [ π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) A ^ i , t , c l i p ( π θ ( o i , t ∣ q , o i , < t ) π θ o l d ( o i , t ∣ q , o i , < t ) , 1 − ϵ , 1 + ϵ ) A ^ i , t ] ) } w h e r e A ^ i , t = R ( q , o i ) − m e a n ( { R ( q , o i ) , . . . , R ( q , o G ) } ) \frac{1}{G}\sum_{i=1}^{G} \sum_{t=1}^{|o_{i}|} \big\{ min \big[ \frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}\hat{A}_{i,t}, clip(\frac{\pi_{\theta}(o_{i,t}|q,o_{i,<t})}{\pi_{\theta_{old}}(o_{i,t}|q,o_{i,<t})}, 1-\epsilon, 1+\epsilon)\hat{A}_{i,t} \big]) \big\} \\ where \ \hat{A}_{i,t}=R(q,o_{i}) - mean(\{R(q,o_{i}),...,R(q,o_{G})\}) G1i=1∑Gt=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])}where A^i,t=R(q,oi)−mean({R(q,oi),...,R(q,oG)})
DAPO 移除 KL 散度 (Removing KL Divergence):KL 惩罚用于调节 在线策略 与 冻结参考策略 之间的散度。
- 在 强化学习人类反馈(RLHF) 场景中,强化学习的目标是,避免与初始模型偏离太远,对齐模型的行为。
- 训练 长思维链(long-CoT) 的模型,模型分布可能显著偏离初始模型,因此,KL 惩罚没有必要。
Dr. GRPO,同样,移除 KL 散度,即:
- KL 正则化项,通常用于从人类反馈中强化学习,其中 r r r 是从 π r e f \pi_{ref} πref 收集的数据中,学习得到的奖励模型。正则化有助于防止 π θ \pi_{\theta} πθ 过度偏离奖励模型准确的分布。
- 推理模型的强化学习微调,通常使用基于规则的验证器作为 r r r,消除分布偏移的担忧,因此,可以移除 KL 项
- 节省在训练过程中由 π r e f \pi_{ref} πref 所需的显存和计算资源,而且,可能为 RL 训练带来更好的性能。
- 因此,假设 β D K L \beta \mathbb{D}_{KL} βDKL 项的参数 β = 0 \beta=0 β=0。
在 TRL - GRPO 的 KL 散度参数中,即
- β = 0.04 \beta = 0.04 β=0.04,default,GRPOConfig
- 在 math 训练中, β = 0.001 \beta=0.001 β=0.001 ,也是降低 KL 散度权重。
1. DAPO 策略
DAPO 去除 KL 散度项、使用基于规则的奖励模型(Rule-based Reward Modeling)。
DAPO 核心包括:
- Clip-Higher (裁剪上界):促进系统多样性,避免熵坍塌(Entropy Collapse),在 策略梯度损失(Policy Gradient Loss) 中,通过增加重要性采样比率的 剪裁上界(Upper Clip) 缓解。
- 提升 Clip 上界,正例(A>0),低概率 Token 绝对提升能力增强,有效释放低概率路径的探索潜能,缓解策略熵的快速降低。
- 保持 Clip 下界,避免策略急剧收缩。
- 如 ϵ l o w = 0.2 , ϵ h i g h = 0.28 \epsilon_{low}=0.2,\epsilon_{high}=0.28 ϵlow=0.2,ϵhigh=0.28
- Dynamic Sampling (动态采样):提高训练效率和稳定性,提出动态采样策略,筛选出准确率为 1 和 0 的提示组(Prompt Groups),确保每个批次中,有效梯度的提示数量保持一致。
- Token-level Policy Gradient Loss (Token-Level 策略梯度损失):避免 长思维链的强化学习(long-CoT RL) 中,无意义的 Pattern 惩罚较低,效果显著。
- GRPO:先在 部分(Generation Level) 取平均,再在 整体(Group Level) 取平均。
- DAPO:在 Group 中,全部 Generation 全部 Tokens,一起取平均。
- Overlong Reward Shaping (过长奖励规范):使用 超长过滤(Overlong Filtering) 策略,隐藏(Mask) 截断样本(Truncated Samples) 的损失,显著稳定训练过程,提高性能,降低奖励噪声(Reward Noise)。
软过长惩罚(Soft Overlong Punishment),即:
R l e n g t h ( y ) = { 0 , ∣ y ∣ ≤ L m a x − L c a c h e L m a x − L c a c h e − ∣ y ∣ L c a c h e , L m a x − L c a c h e < ∣ y ∣ ≤ L m a x − 1 , L m a x < ∣ y ∣ R_{length}(y) = \begin{cases} 0, &|y|\le L_{max} - L_{cache} \\ \frac{L_{max} - L_{cache} - |y|}{L_{cache}}, &L_{max} - L_{cache} < |y| \le L_{max} \\ -1, &L_{max} < |y| \end{cases} Rlength(y)=⎩ ⎨ ⎧0,LcacheLmax−Lcache−∣y∣,−1,∣y∣≤Lmax−LcacheLmax−Lcache<∣y∣≤LmaxLmax<∣y∣
DAPO 流程:
2. Dr. GRPO 策略
Dr. GRPO 解决 GRPO 的 2 个偏差(Biases):
- 响应长度偏差(Response-level length bias):即 除以 ∣ o i ∣ |o_{i}| ∣oi∣:
- 正向优势 ( A ^ i , t > 0 \hat{A}_{i,t}>0 A^i,t>0) 回答正确,短回答的梯度更新大,长回答的梯度更新小。
- 负向优势 ( A ^ i , t < 0 \hat{A}_{i,t}<0 A^i,t<0) 回答错误,长回答的惩罚较小,短回答的惩罚较大。
- 去掉 除以 ∣ o i ∣ |o_{i}| ∣oi∣ 之后,避免回答长短的影响,只考虑奖励函数值。
- 问题难度偏差(Question-level difficulty bias):即 优势 A ^ i , t \hat{A}_{i,t} A^i,t 除以 s t d ( { R ( q , o i ) , . . . , R ( q , o G ) } ) std(\{R(q,o_{i}),...,R(q,o_{G})\}) std({R(q,oi),...,R(q,oG)})
- 标准差较低的问题,在策略更新过程中,赋予更高的权重。
- 批次归一化是合理的,但是,问题级别归一化,导致目标函数中不同问题的权重有所不同。
- 模型训练更偏向于,回答一致性较高的问题,降低探索能力。
3. TRL GRPO 实现
TRL 代码中,计算 GRPO 的逻辑:
# 1. advantages 优势的计算过程
# Gather the reward per function: this part is crucial, because the rewards are normalized per group and the
# completions may be distributed across processes
rewards_per_func = gather(rewards_per_func)# Apply weights to each reward function's output and sum
rewards = (rewards_per_func * self.reward_weights.to(device).unsqueeze(0)).nansum(dim=1)# Compute grouped-wise rewards
mean_grouped_rewards = rewards.view(-1, self.num_generations).mean(dim=1)
std_grouped_rewards = rewards.view(-1, self.num_generations).std(dim=1)# Normalize the rewards to compute the advantages
mean_grouped_rewards = mean_grouped_rewards.repeat_interleave(self.num_generations, dim=0)
std_grouped_rewards = std_grouped_rewards.repeat_interleave(self.num_generations, dim=0)
advantages = rewards - mean_grouped_rewards
if self.args.scale_rewards:advantages = advantages / (std_grouped_rewards + 1e-4)# KL 散度
per_token_kl = torch.exp(ref_per_token_logps - per_token_logps) - (ref_per_token_logps - per_token_logps) - 1# 2. Loss 的计算过程
# Compute the loss
advantages = inputs["advantages"]
# When using num_iterations == 1, old_per_token_logps == per_token_logps, so we can skip it's computation (see
# _generate_and_score_completions) and use per_token_logps.detach() instead.
old_per_token_logps = inputs["old_per_token_logps"] if self.num_iterations > 1 else per_token_logps.detach()
coef_1 = torch.exp(per_token_logps - old_per_token_logps)
coef_2 = torch.clamp(coef_1, 1 - self.epsilon_low, 1 + self.epsilon_high)
per_token_loss1 = coef_1 * advantages.unsqueeze(1)
per_token_loss2 = coef_2 * advantages.unsqueeze(1)
per_token_loss = -torch.min(per_token_loss1, per_token_loss2)
if self.beta != 0.0:per_token_loss = per_token_loss + self.beta * per_token_kl
loss = (per_token_loss * completion_mask).sum() / completion_mask.sum()
4. loss 是 0 不代表 gradient 是 0
loss = 0 也可以反向传播,更新梯度:
- loss 是 0,gradient 可能不是 0
l o s s ( w ) = ( w − 1 ) 2 − 1 ∂ l o s s ∂ w = 2 w − 2 \begin{align} loss(w) &= (w-1)^{2} - 1 \\ \frac{\partial{loss}}{\partial{w}} &= 2w - 2 \end{align} loss(w)∂w∂loss=(w−1)2−1=2w−2
- 当 loss 是 0 时,w=0,梯度 gradient 是 -2,学习 = 学习率 * 梯度,假设,学习率 η = 0.1 \eta=0.1 η=0.1 。
w n e w = w − η ⋅ g = 0 − ( 0.1 × ( − 2 ) ) = 0.2 w_{new} = w - \eta \cdot g = 0 - (0.1\times(-2)) = 0.2 wnew=w−η⋅g=0−(0.1×(−2))=0.2
- gradient 是 0,则不可优化
测试:
import torch
x = torch.tensor([3.0], requires_grad=True)
y1 = x-x
y1.backward()
print(f"Grad for x-x: {x.grad.item()}") # 0.0x.grad.zero_()
y2 = x - x.detach()
y2.backward()
print(f"Grad for x - x.detach(): {x.grad.item()}") # 1.0
参考:
- GitHub - GRPO questions
- GitHub - Add warning when negative KL
- 知乎 - DAPO中的CLIP trick