PPO¶
PPO(Schulman et al., 2017)限制单次更新能把策略移动多远。它截断重要性比重 (\frac{p_\theta(x)}{q(x)}),使学习器不会偏离采样器 (q) 太远——当你对同一批 rollout 数据进行多次梯度更新时很有用。
截断和损失都逐 token 计算。截断后的目标为:
[ \mathcal{L}{\text{CLIP}}(\theta) = -\mathbb{E}\right) A(x)\right] ]}\left[\text{clip}\left(\frac{p_\theta(x)}{q(x)},\, 1-\epsilon_{\text{low}},\, 1+\epsilon_{\text{high}
最终损失取截断与未截断目标中更保守的那个:
[ \mathcal{L}{\text{PPO}}(\theta) = -\mathbb{E}\right) A(x)\right)\right] ]}\left[\min\left(\frac{p_\theta(x)}{q(x)} A(x),\ \text{clip}\left(\frac{p_\theta(x)}{q(x)},\, 1-\epsilon_{\text{low}},\, 1+\epsilon_{\text{high}
Mint Recursive 中截断范围固定为 (\epsilon_{\text{low}}=\epsilon_{\text{high}}=0.2)。
等价代码:
prob_ratio = torch.exp(target_logprobs - sampling_logprobs)
clipped_ratio = torch.clamp(prob_ratio, clip_low_threshold, clip_high_threshold)
unclipped = prob_ratio * advantages
clipped = clipped_ratio * advantages
ppo_objective = torch.min(unclipped, clipped) # most conservative
loss = -ppo_objective.sum()
Inputs
target_tokens: array[(N,), int]—— 由 (q) 采样的 token ID。logprobs: array[(N,), float]——sampling_logprobs。advantages: array[(N,), float]—— 逐 token 的 advantage。
Outputs
logprobs——target_logprobs。
Diagnostics
loss:sum—— 截断损失总和。
自定义截断范围¶
传入 loss_fn_config 覆盖默认值: