|
| 1 | +# Loss Types |
| 2 | + |
| 3 | +GRPO训练支持五种不同的loss类型,主要区别在于归一化的维度上有所不同。 |
| 4 | + |
| 5 | +## 损失函数 |
| 6 | + |
| 7 | +token 级别上,GRPO 训练使用以下损失函数 |
| 8 | + |
| 9 | +$$\mathcal{L}_{i,t} = -\min\left(\rho_{i,t} A_{i,t}, \text{clip}(\rho_{i,t}, 1-\epsilon, 1+\epsilon) A_{i,t}\right)$$ |
| 10 | + |
| 11 | +当设置`loss_type cispo`时,使用 cispo 损失 |
| 12 | + |
| 13 | +$$\mathcal{L}_{i,t}^{\text{CISPO}} = -\text{detach}\left(\min(\rho_{i,t}, \epsilon_{\text{high}})\right) \cdot A_{i,t} \cdot \log \pi_\theta(y_{i,t}|y_{i,<t})$$ |
| 14 | + |
| 15 | +其中: |
| 16 | +- $\rho_{i,t} = \frac{\pi_\theta(y_{i,t}|y_{i,<t})}{\pi_{\theta_{\text{old}}}(y_{i,t}|y_{i,<t})}$ 是重要性采样权重 |
| 17 | +- $A_{i,t}$ 是优势函数 |
| 18 | +- $\epsilon$ 和 $\epsilon_{\text{high}}$ 是clipping参数 |
| 19 | +- $\text{detach}(\cdot)$ 表示该项不参与梯度计算 |
| 20 | + |
| 21 | +## GRPO |
| 22 | + |
| 23 | +`--loss_type grpo` |
| 24 | + |
| 25 | +GRPO是标准的损失函数实现,对每个样本的token-level损失取平均,然后对所有样本取平均。 |
| 26 | + |
| 27 | +**公式:** |
| 28 | + |
| 29 | +$$\mathcal{L}_{\text{GRPO}} = \frac{1}{N} \sum_{i=1}^{N} \frac{1}{T_i} \sum_{t=1}^{T_i} \mathcal{L}_{i,t}$$ |
| 30 | + |
| 31 | +其中: |
| 32 | +- $N$ 是批次中的样本数量 |
| 33 | +- $T_i$ 是第$i$个样本的completion token数量 |
| 34 | + |
| 35 | +**归一化维度:** 样本维度(先对每个样本的所有token取平均,再对所有样本取平均) |
| 36 | + |
| 37 | +## BNPO (Batch Normalized Policy Optimization) |
| 38 | + |
| 39 | +`--loss_type bnpo` |
| 40 | + |
| 41 | +BNPO将所有样本的所有token的损失直接求和,然后除以所有completion token的总数量。 |
| 42 | + |
| 43 | +**公式:** |
| 44 | + |
| 45 | +$$\mathcal{L}_{\text{BNPO}} = \frac{\sum_{i=1}^{N} \sum_{t=1}^{T_i} \mathcal{L}_{i,t}}{\sum_{i=1}^{N} T_i}$$ |
| 46 | + |
| 47 | +其中: |
| 48 | +- $N$ 是批次中的样本数量 |
| 49 | +- $T_i$ 是第$i$个样本的completion token数量 |
| 50 | + |
| 51 | +**归一化维度:** Token维度(对所有completion token取平均) |
| 52 | + |
| 53 | +## DR-GRPO |
| 54 | + |
| 55 | +`--loss_type dr_grpo` |
| 56 | + |
| 57 | +DR-GRPO将所有样本的所有token的损失求和,然后除以批次大小乘以最大completion长度。 |
| 58 | + |
| 59 | +**公式:** |
| 60 | + |
| 61 | +$$\mathcal{L}_{\text{DR-GRPO}} = \frac{\sum_{i=1}^{N} \sum_{t=1}^{T_i} \mathcal{L}_{i,t}}{N \times L_{\text{max}}}$$ |
| 62 | + |
| 63 | +其中: |
| 64 | +- $N$ 是批次中的样本数量 |
| 65 | +- $T_i$ 是第$i$个样本的completion token数量 |
| 66 | +- $L_{\text{max}}$ 是最大completion长度 |
| 67 | + |
| 68 | +**归一化维度:** 固定维度(批次大小 × 最大completion长度) |
| 69 | + |
| 70 | +## CISPO |
| 71 | + |
| 72 | +`--loss_type cispo` |
| 73 | + |
| 74 | +CISPO损失按所有进程的completion token总数进行归一化。 |
| 75 | + |
| 76 | +**公式:** |
| 77 | + |
| 78 | +$$\mathcal{L}_{\text{CISPO}} = \frac{\sum_{i=1}^{N} \sum_{t=1}^{T_i} \mathcal{L}_{i,t}^{\text{CISPO}}}{\sum_{\text{all processes}} \sum_{i=1}^{N_p} T_{p,i}}$$ |
| 79 | + |
| 80 | +其中: |
| 81 | +- $N$ 是当前进程批次中的样本数量 |
| 82 | +- $T_i$ 是第$i$个样本的completion token数量 |
| 83 | +- $N_p$ 是第$p$个进程的样本数量 |
| 84 | + |
| 85 | +**归一化维度:** 全局token维度(跨所有进程的completion token总数) |
| 86 | + |
| 87 | +## DAPO |
| 88 | + |
| 89 | +`--loss_type dapo` |
| 90 | + |
| 91 | +DAPO与BNPO类似,使用token-level归一化,但基于全局数据(多进程)进行归一化。 |
| 92 | + |
| 93 | +**公式:** |
| 94 | + |
| 95 | +$$\mathcal{L}_{\text{DAPO}} = \frac{\sum_{i=1}^{N} \sum_{t=1}^{T_i} \mathcal{L}_{i,t}}{\sum_{\text{all processes}} \sum_{i=1}^{N_p} T_{p,i}}$$ |
| 96 | + |
| 97 | +其中: |
| 98 | +- $N$ 是当前进程批次中的样本数量 |
| 99 | +- $T_i$ 是第$i$个样本的completion token数量 |
| 100 | +- $N_p$ 是第$p$个进程的样本数量 |
| 101 | + |
| 102 | +**归一化维度:** 全局token维度(跨所有进程的completion token总数) |
0 commit comments