Commit cc14537
Add CISPO and SAPO loss type support for Triton GRPO loss kernel (#1074)
## Summary
Add **CISPO** and **SAPO** loss type support to the Triton
`ops/grpo_loss.py` kernel.
This is a follow-up to:
- #1054 - Add CISPO loss type support for LigerFusedLinearGRPOLoss
(chunked loss path)
- #1073 - Add SAPO loss type support for LigerFusedLinearGRPOLoss
(chunked loss path)
> **Note**: This PR depends on #1073 (SAPO PR) being merged first, as it
builds on top of that branch.
### Background
PR #1054 and #1073 added CISPO and SAPO support to the `chunked_loss`
path, but the `ops` (Triton kernel) path was marked as a follow-up. This
PR implements that follow-up.
### Changes
**`src/liger_kernel/ops/grpo_loss.py`**
- Add loss type constants (`_LOSS_TYPE_GRPO`, `_LOSS_TYPE_CISPO`,
`_LOSS_TYPE_SAPO`) with `tl.constexpr` for compile-time branching
- Implement CISPO in forward/backward kernels:
- Upper-bound only clipping (no lower bound)
- Detached coefficient (gradient only flows through logp)
- Loss formula: `-coef_2 * advantage * logp`
- Implement SAPO in forward/backward kernels:
- Sigmoid-based soft gating instead of hard clipping
- Different temperatures for positive/negative advantages
- Loss formula: `-sigmoid(τ*(ρ-1)) * 4/τ * advantage`
- Update `GrpoLossFunction` with `loss_type`, `sapo_temperature_pos`,
`sapo_temperature_neg` parameters
**`src/liger_kernel/transformers/grpo_loss.py`**
- Remove error blocking for CISPO and SAPO loss types
- Add `sapo_temperature_pos` and `sapo_temperature_neg` parameters
- Update `_reduce_grpo_loss` to handle CISPO (DAPO normalization) and
SAPO (GRPO normalization)
**`test/transformers/test_grpo_loss.py`**
- Add reference PyTorch implementations (`torch_cispo_loss`,
`torch_sapo_loss`)
- Add `test_cispo_loss` and `test_sapo_loss` test functions
## Testing Done
- Hardware Type: NVIDIA GPU
- [x] run `make test` to ensure correctness
- [x] run `make checkstyle` to ensure code style
- [ ] run `make test-convergence` to ensure convergence
---------
Signed-off-by: Tcc0403 <76503978+Tcc0403@users.noreply.github.com>
Co-authored-by: Tcc0403 <76503978+Tcc0403@users.noreply.github.com>1 parent ad6f0a7 commit cc14537
File tree
4 files changed
+401
-57
lines changed- src/liger_kernel
- chunked_loss
- ops
- transformers
- test/transformers
4 files changed
+401
-57
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
342 | 347 | | |
343 | 348 | | |
344 | 349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
5 | 20 | | |
6 | 21 | | |
7 | 22 | | |
| |||
83 | 98 | | |
84 | 99 | | |
85 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
86 | 104 | | |
87 | 105 | | |
88 | 106 | | |
| |||
123 | 141 | | |
124 | 142 | | |
125 | 143 | | |
126 | | - | |
127 | 144 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
134 | 171 | | |
135 | 172 | | |
136 | 173 | | |
| |||
165 | 202 | | |
166 | 203 | | |
167 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
168 | 208 | | |
169 | 209 | | |
170 | 210 | | |
| |||
202 | 242 | | |
203 | 243 | | |
204 | 244 | | |
205 | | - | |
206 | 245 | | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | 246 | | |
211 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
212 | 274 | | |
213 | 275 | | |
214 | 276 | | |
| |||
239 | 301 | | |
240 | 302 | | |
241 | 303 | | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
242 | 307 | | |
243 | 308 | | |
244 | 309 | | |
245 | 310 | | |
246 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
247 | 326 | | |
248 | 327 | | |
249 | 328 | | |
| |||
270 | 349 | | |
271 | 350 | | |
272 | 351 | | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
273 | 355 | | |
274 | 356 | | |
275 | 357 | | |
276 | 358 | | |
277 | 359 | | |
278 | | - | |
279 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
280 | 370 | | |
281 | 371 | | |
282 | 372 | | |
283 | 373 | | |
284 | 374 | | |
285 | | - | |
286 | 375 | | |
287 | | - | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
288 | 379 | | |
289 | 380 | | |
290 | 381 | | |
| |||
303 | 394 | | |
304 | 395 | | |
305 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
306 | 400 | | |
307 | 401 | | |
308 | 402 | | |
309 | 403 | | |
310 | 404 | | |
311 | 405 | | |
312 | | - | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
| |||
45 | 43 | | |
46 | 44 | | |
47 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | | - | |
| 73 | + | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
82 | | - | |
| 84 | + | |
| 85 | + | |
83 | 86 | | |
84 | 87 | | |
85 | 88 | | |
| |||
0 commit comments