liger-kernel-nightly 0.6.3.dev20251121200119__py3-none-any.whl → 0.6.3.dev20251121202601__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- liger_kernel/chunked_loss/fused_linear_ppo.py +21 -5
- liger_kernel/chunked_loss/grpo_loss.py +8 -5
- liger_kernel/ops/grpo_loss.py +3 -1
- liger_kernel/transformers/grpo_loss.py +56 -1
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/METADATA +1 -1
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/RECORD +10 -10
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.6.3.dev20251121200119.dist-info → liger_kernel_nightly-0.6.3.dev20251121202601.dist-info}/top_level.txt +0 -0
|
@@ -32,7 +32,7 @@ class LigerFusedLinearPPOBase(torch.autograd.Function):
|
|
|
32
32
|
epsilon_low=0.2,
|
|
33
33
|
epsilon_high=0.2,
|
|
34
34
|
beta=0.04,
|
|
35
|
-
loss_type="
|
|
35
|
+
loss_type="dapo",
|
|
36
36
|
max_completion_length=None,
|
|
37
37
|
importance_sampling_level="token",
|
|
38
38
|
temperature=1.0,
|
|
@@ -60,7 +60,7 @@ class LigerFusedLinearPPOBase(torch.autograd.Function):
|
|
|
60
60
|
epsilon_low: Lower bound for clipping the importance sampling ratio
|
|
61
61
|
epsilon_high: Upper bound for clipping the importance sampling ratio
|
|
62
62
|
beta: Weight for the KL penalty
|
|
63
|
-
loss_type: Type of loss calculation ("grpo", "bnpo", "dr_grpo")
|
|
63
|
+
loss_type: Type of loss calculation ("grpo", "bnpo", "dr_grpo", "dapo")
|
|
64
64
|
max_completion_length: Maximum completion length required for "dr_grpo"
|
|
65
65
|
temperature: Temperature for the logits
|
|
66
66
|
compiled: Whether to use torch compile
|
|
@@ -244,6 +244,21 @@ class LigerFusedLinearPPOBase(torch.autograd.Function):
|
|
|
244
244
|
|
|
245
245
|
return loss_acc, tuple(final_metrics)
|
|
246
246
|
|
|
247
|
+
@staticmethod
|
|
248
|
+
def _compute_dapo_normalizer(attention_mask):
|
|
249
|
+
"""Global active tokens averaged per process."""
|
|
250
|
+
normalizer = attention_mask.to(torch.float32).sum()
|
|
251
|
+
world_size = 1
|
|
252
|
+
if torch.distributed.is_available() and torch.distributed.is_initialized():
|
|
253
|
+
import torch.distributed as dist
|
|
254
|
+
|
|
255
|
+
normalizer = normalizer.clone()
|
|
256
|
+
dist.all_reduce(normalizer, op=dist.ReduceOp.SUM)
|
|
257
|
+
world_size = dist.get_world_size()
|
|
258
|
+
|
|
259
|
+
normalizer = normalizer / world_size
|
|
260
|
+
return torch.clamp(normalizer, min=1.0)
|
|
261
|
+
|
|
247
262
|
@staticmethod
|
|
248
263
|
def _compute_chunk_loss(
|
|
249
264
|
input_chunk,
|
|
@@ -261,7 +276,7 @@ class LigerFusedLinearPPOBase(torch.autograd.Function):
|
|
|
261
276
|
epsilon_low=0.2,
|
|
262
277
|
epsilon_high=0.2,
|
|
263
278
|
beta=0.04,
|
|
264
|
-
loss_type="
|
|
279
|
+
loss_type="dapo",
|
|
265
280
|
max_completion_length=None,
|
|
266
281
|
importance_sampling_level="token",
|
|
267
282
|
temperature=1.0,
|
|
@@ -341,10 +356,11 @@ class LigerFusedLinearPPOBase(torch.autograd.Function):
|
|
|
341
356
|
None, # grad_epsilon_low
|
|
342
357
|
None, # grad_epsilon_high
|
|
343
358
|
None, # grad_beta
|
|
359
|
+
None, # grad_loss_type
|
|
360
|
+
None, # grad_max_completion_length
|
|
361
|
+
None, # grad_importance_sampling_level
|
|
344
362
|
None, # grad_temperature
|
|
345
363
|
None, # grad_compiled
|
|
346
364
|
None, # grad_use_ref_model
|
|
347
365
|
None, # grad_chunk_size
|
|
348
|
-
None, # grad_loss_type
|
|
349
|
-
None, # grad_max_completion_length
|
|
350
366
|
)
|
|
@@ -29,7 +29,7 @@ class LigerFusedLinearGRPOFunction(LigerFusedLinearPPOBase):
|
|
|
29
29
|
epsilon_low=0.2,
|
|
30
30
|
epsilon_high=0.2,
|
|
31
31
|
beta=0.04,
|
|
32
|
-
loss_type="
|
|
32
|
+
loss_type="dapo", # ["grpo", "bnpo", "dr_grpo", "dapo"]
|
|
33
33
|
max_completion_length=None, # Required for dr_grpo
|
|
34
34
|
importance_sampling_level="token", # ["token", "sequence"] - new parameter for GSPO
|
|
35
35
|
**kwargs,
|
|
@@ -94,6 +94,9 @@ class LigerFusedLinearGRPOFunction(LigerFusedLinearPPOBase):
|
|
|
94
94
|
if max_completion_length is None:
|
|
95
95
|
raise ValueError("max_completion_length must be provided for loss_type 'dr_grpo'")
|
|
96
96
|
loss = (per_token_loss * attention_mask).sum() / (full_attention_mask.shape[0] * max_completion_length)
|
|
97
|
+
elif loss_type == "dapo":
|
|
98
|
+
loss_normalizer = LigerFusedLinearPPOBase._compute_dapo_normalizer(full_attention_mask)
|
|
99
|
+
loss = (per_token_loss * attention_mask).sum() / loss_normalizer
|
|
97
100
|
else:
|
|
98
101
|
raise ValueError(f"Unknown loss type: {loss_type}")
|
|
99
102
|
|
|
@@ -135,7 +138,7 @@ class LigerFusedLinearGRPOFunction(LigerFusedLinearPPOBase):
|
|
|
135
138
|
beta=0.04,
|
|
136
139
|
epsilon_low=0.2,
|
|
137
140
|
epsilon_high=0.2,
|
|
138
|
-
loss_type="
|
|
141
|
+
loss_type="dapo",
|
|
139
142
|
max_completion_length=None,
|
|
140
143
|
importance_sampling_level="token",
|
|
141
144
|
temperature=1.0,
|
|
@@ -157,7 +160,7 @@ class LigerFusedLinearGRPOFunction(LigerFusedLinearPPOBase):
|
|
|
157
160
|
ref_weight (torch.Tensor, optional): Reference model weight tensor. Shape: (vocab_size, hidden_size)
|
|
158
161
|
ref_bias (torch.Tensor, optional): Reference model bias tensor. Shape: (vocab_size,)
|
|
159
162
|
beta (float): Weight for the KL penalty
|
|
160
|
-
loss_type (str): Type of loss calculation ("grpo", "bnpo", "dr_grpo"). Defaults to "
|
|
163
|
+
loss_type (str): Type of loss calculation ("grpo", "bnpo", "dr_grpo", "dapo"). Defaults to "dapo".
|
|
161
164
|
max_completion_length (int, optional): Maximum completion length, required for "dr_grpo". Defaults to None.
|
|
162
165
|
importance_sampling_level (str): Level of importance sampling ("token" or "sequence"). Defaults to "token".
|
|
163
166
|
temperature (float): Temperature for the logits
|
|
@@ -235,7 +238,7 @@ class LigerFusedLinearGRPOLoss(torch.nn.Module):
|
|
|
235
238
|
chunk_size: int = 1,
|
|
236
239
|
epsilon_low: float = 0.2,
|
|
237
240
|
epsilon_high: float = 0.2,
|
|
238
|
-
loss_type: str = "
|
|
241
|
+
loss_type: str = "dapo",
|
|
239
242
|
max_completion_length: Optional[int] = None,
|
|
240
243
|
importance_sampling_level: str = "token",
|
|
241
244
|
temperature: float = 1.0,
|
|
@@ -248,7 +251,7 @@ class LigerFusedLinearGRPOLoss(torch.nn.Module):
|
|
|
248
251
|
chunk_size (int): Size of chunks for processing.
|
|
249
252
|
epsilon_low (float): Lower bound for the importance sampling ratio.
|
|
250
253
|
epsilon_high (float): Upper bound for the importance sampling ratio.
|
|
251
|
-
loss_type (str): Type of loss calculation ("grpo", "bnpo", "dr_grpo"). Defaults to "
|
|
254
|
+
loss_type (str): Type of loss calculation ("grpo", "bnpo", "dr_grpo", "dapo"). Defaults to "dapo".
|
|
252
255
|
max_completion_length (int, optional): Maximum completion length, required for "dr_grpo". Defaults to None.
|
|
253
256
|
importance_sampling_level (str): Level of importance sampling ("token" or "sequence"). Defaults to "token".
|
|
254
257
|
temperature (float): Temperature for the logits.
|
liger_kernel/ops/grpo_loss.py
CHANGED
|
@@ -128,7 +128,9 @@ def _grpo_loss_fwd_kernel(
|
|
|
128
128
|
per_token_loss1 = coef_1 * advantage
|
|
129
129
|
per_token_loss2 = coef_2 * advantage
|
|
130
130
|
per_token_loss = -tl.minimum(per_token_loss1, per_token_loss2)
|
|
131
|
-
|
|
131
|
+
is_low_clipped = (coef_1 < 1 - EPS_LOW) & (advantage < 0)
|
|
132
|
+
is_high_clipped = (coef_1 > 1 + EPS_HIGH) & (advantage > 0)
|
|
133
|
+
is_clipped = is_low_clipped | is_high_clipped
|
|
132
134
|
|
|
133
135
|
if BETA != 0.0:
|
|
134
136
|
REF_LOGP += off_b * L + off_l
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from liger_kernel.chunked_loss.fused_linear_ppo import LigerFusedLinearPPOBase
|
|
1
4
|
from liger_kernel.ops.grpo_loss import GrpoLossFunction
|
|
2
5
|
|
|
3
6
|
|
|
@@ -13,12 +16,20 @@ def triton_grpo_loss(
|
|
|
13
16
|
eps_low=0.2,
|
|
14
17
|
eps_high=0.4,
|
|
15
18
|
inplace=True,
|
|
19
|
+
loss_type="dapo",
|
|
20
|
+
max_completion_length=None,
|
|
21
|
+
importance_sampling_level="token",
|
|
22
|
+
reduce=False,
|
|
16
23
|
):
|
|
17
24
|
assert logits is not None and completion_ids is not None and advantages is not None, (
|
|
18
25
|
"must provide logits、completion_ids and advantages"
|
|
19
26
|
)
|
|
27
|
+
if importance_sampling_level != "token":
|
|
28
|
+
raise ValueError(
|
|
29
|
+
f"Triton GRPO loss only supports token-level importance sampling. Got {importance_sampling_level}."
|
|
30
|
+
)
|
|
20
31
|
|
|
21
|
-
|
|
32
|
+
per_token_loss, per_token_kl, is_clipped = GrpoLossFunction.apply(
|
|
22
33
|
logits,
|
|
23
34
|
old_logp,
|
|
24
35
|
ref_logp,
|
|
@@ -31,6 +42,50 @@ def triton_grpo_loss(
|
|
|
31
42
|
eps_high,
|
|
32
43
|
inplace,
|
|
33
44
|
)
|
|
45
|
+
if not reduce:
|
|
46
|
+
return per_token_loss, per_token_kl, is_clipped
|
|
47
|
+
|
|
48
|
+
loss = _reduce_grpo_loss(
|
|
49
|
+
per_token_loss,
|
|
50
|
+
completion_mask,
|
|
51
|
+
loss_type=loss_type,
|
|
52
|
+
max_completion_length=max_completion_length,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
metrics = []
|
|
56
|
+
if beta != 0.0 and per_token_kl is not None:
|
|
57
|
+
metrics.append(_masked_mean(per_token_kl, completion_mask))
|
|
58
|
+
metrics.append(_masked_mean(is_clipped.float(), completion_mask))
|
|
59
|
+
return loss, metrics
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _reduce_grpo_loss(per_token_loss, completion_mask, loss_type, max_completion_length):
|
|
63
|
+
mask = completion_mask
|
|
64
|
+
if mask is None:
|
|
65
|
+
mask = torch.ones_like(per_token_loss, dtype=per_token_loss.dtype, device=per_token_loss.device)
|
|
66
|
+
mask = mask.to(per_token_loss.dtype)
|
|
67
|
+
|
|
68
|
+
if loss_type == "grpo":
|
|
69
|
+
per_seq = (per_token_loss * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)
|
|
70
|
+
return per_seq.mean()
|
|
71
|
+
if loss_type == "bnpo":
|
|
72
|
+
return (per_token_loss * mask).sum() / mask.sum().clamp(min=1.0)
|
|
73
|
+
if loss_type == "dr_grpo":
|
|
74
|
+
if max_completion_length is None:
|
|
75
|
+
raise ValueError("max_completion_length must be provided when using loss_type='dr_grpo'")
|
|
76
|
+
batch = per_token_loss.shape[0]
|
|
77
|
+
return (per_token_loss * mask).sum() / (batch * max_completion_length)
|
|
78
|
+
if loss_type == "dapo":
|
|
79
|
+
normalizer = LigerFusedLinearPPOBase._compute_dapo_normalizer(mask)
|
|
80
|
+
return (per_token_loss * mask).sum() / normalizer
|
|
81
|
+
raise ValueError(f"Unsupported loss_type '{loss_type}' for Triton GRPO loss.")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _masked_mean(values, mask):
|
|
85
|
+
if mask is None:
|
|
86
|
+
mask = torch.ones_like(values, dtype=values.dtype, device=values.device)
|
|
87
|
+
mask = mask.to(values.dtype)
|
|
88
|
+
return (values * mask).sum() / mask.sum().clamp(min=1.0)
|
|
34
89
|
|
|
35
90
|
|
|
36
91
|
# This is a demo how to use grpo_loss in GRPOTrainer. The Trl version must be 0.16
|
|
@@ -8,10 +8,10 @@ liger_kernel/chunked_loss/cpo_loss.py,sha256=Gzz1eU4kgcbdubFVRy55e8A1Cr-r45UgNic
|
|
|
8
8
|
liger_kernel/chunked_loss/dpo_loss.py,sha256=I83khNs3QQjuhr8U3NIOAACkbse6DNiBV-TulPZ0lXw,9006
|
|
9
9
|
liger_kernel/chunked_loss/functional.py,sha256=-XPDbLml9dHmvoSU2VNTUrBDFehuzvuAGPikVetBMtI,1132
|
|
10
10
|
liger_kernel/chunked_loss/fused_linear_distillation.py,sha256=yRtolfFGfKB-SxGQQyF68GYXd11Zlvh1InLdGeWNFIE,12652
|
|
11
|
-
liger_kernel/chunked_loss/fused_linear_ppo.py,sha256=
|
|
11
|
+
liger_kernel/chunked_loss/fused_linear_ppo.py,sha256=baU19PwqO1FTVxwlB-eyJv6gOLtL7baXGzSncYQ8Ktc,14296
|
|
12
12
|
liger_kernel/chunked_loss/fused_linear_preference.py,sha256=FIH85uUXAOgYx5Ax8MjFhJHVu-2pKtY7wSegd0zSyyY,18336
|
|
13
13
|
liger_kernel/chunked_loss/fused_linear_unpaired_preference.py,sha256=RiuK3UtRwH9T6jZ36sA8Urj-TVuOLOO2syLg_JOQapY,13437
|
|
14
|
-
liger_kernel/chunked_loss/grpo_loss.py,sha256=
|
|
14
|
+
liger_kernel/chunked_loss/grpo_loss.py,sha256=bmuZaNgqNbJ5pJGFDXWE-B4BGYF7xWVSN15UyCfuq_s,13079
|
|
15
15
|
liger_kernel/chunked_loss/jsd_loss.py,sha256=G0RghPYYelyZ6DOEiwS8we9TT5MY2iHpiFqzZ2Xy87g,8038
|
|
16
16
|
liger_kernel/chunked_loss/kto_loss.py,sha256=llVCe6DkcpCo57seGWoMikaQVFApx764jsmSbQyqwQY,7529
|
|
17
17
|
liger_kernel/chunked_loss/orpo_loss.py,sha256=nu9UYG16dcMw93lvHi4_hYs3Q0FK1KnlmMRj7OpYU8s,4872
|
|
@@ -25,7 +25,7 @@ liger_kernel/ops/fused_linear_jsd.py,sha256=CSoprxb-YcJy-YUKiTcYkxN8sb9h2kdk_iHu
|
|
|
25
25
|
liger_kernel/ops/fused_neighborhood_attention.py,sha256=vPi5xbnh6wxyZehaqo6Tuilqo2fN5SGDiONjnNmIKqs,35556
|
|
26
26
|
liger_kernel/ops/geglu.py,sha256=r0WSq9E93zzynL44Wh8femzOWK07_SseBM_pJUyxT3s,4144
|
|
27
27
|
liger_kernel/ops/group_norm.py,sha256=qD4D4lSjSgVtO52EBNLC2iTseALRgPgqXE50U2woggk,10837
|
|
28
|
-
liger_kernel/ops/grpo_loss.py,sha256=
|
|
28
|
+
liger_kernel/ops/grpo_loss.py,sha256=2SyOujtF9I3xiNo4wFf4s6MeiDotE_qeYfRWgj_bOBE,9573
|
|
29
29
|
liger_kernel/ops/jsd.py,sha256=onHp5T3MbvJaVz5Vup7Ww6EQp_HTaZeayTjJk6FgQMY,7042
|
|
30
30
|
liger_kernel/ops/kl_div.py,sha256=ZjGdDLKWksHT9dZ0xF_TDgAkj5cuMTwwT5tr9E-_24o,8734
|
|
31
31
|
liger_kernel/ops/layer_norm.py,sha256=WmiORsIyufOhazmYZTPjeSc5Z-xTAYwXAKqUcCv_dlY,9807
|
|
@@ -55,7 +55,7 @@ liger_kernel/transformers/fused_linear_jsd.py,sha256=bZ4otCvWBuOnA5XdQL-FzZVItJl
|
|
|
55
55
|
liger_kernel/transformers/fused_neighborhood_attention.py,sha256=TxYDUAt9B6WSP14aJP66C_2Mbds2sSIPGnamhUSTrC8,7957
|
|
56
56
|
liger_kernel/transformers/geglu.py,sha256=mrgqzIUVd6lN7fkDKLkw5YaESDxDtFgbot430WwPVOQ,1107
|
|
57
57
|
liger_kernel/transformers/group_norm.py,sha256=6qMAWOprr4SzP0YhNVNGQIBpM5aUHplUD2VuGJrMBz0,2173
|
|
58
|
-
liger_kernel/transformers/grpo_loss.py,sha256=
|
|
58
|
+
liger_kernel/transformers/grpo_loss.py,sha256=QS6Ycct1E2yMfqoHPBa2sUAu5cmweNPK_-Q_KJE8hb4,6098
|
|
59
59
|
liger_kernel/transformers/jsd.py,sha256=DGqRnxIZxsvxo0_tbbxX3b-sDbDjC_yKufyRIHCcScY,2979
|
|
60
60
|
liger_kernel/transformers/kl_div.py,sha256=WLffFbh1EExD2Eb1F7lN11fo9JJC-0751WJjZAF1Fj8,409
|
|
61
61
|
liger_kernel/transformers/layer_norm.py,sha256=c9pk3PEasOKYR0rhe5e5nNrnYKVCEW4VC8S6LpCq9EQ,906
|
|
@@ -110,9 +110,9 @@ liger_kernel/transformers/trainer/__init__.py,sha256=p7yQfklV8-467qSz_ZMimkbDF7H
|
|
|
110
110
|
liger_kernel/transformers/trainer/orpo_trainer.py,sha256=tX0h63aOFe3rNqTmk6JpMf75UPo981yzEa6TghnjS0Q,5370
|
|
111
111
|
liger_kernel/triton/__init__.py,sha256=qCiCamzCRv6lpV8IqpAc9YMdNKC7GKurClWceQPnlis,92
|
|
112
112
|
liger_kernel/triton/monkey_patch.py,sha256=Rd0hUHAzDkFfHvnX7-PBaNK5EKnZhtfM_h-fgQH9HPY,1568
|
|
113
|
-
liger_kernel_nightly-0.6.3.
|
|
114
|
-
liger_kernel_nightly-0.6.3.
|
|
115
|
-
liger_kernel_nightly-0.6.3.
|
|
116
|
-
liger_kernel_nightly-0.6.3.
|
|
117
|
-
liger_kernel_nightly-0.6.3.
|
|
118
|
-
liger_kernel_nightly-0.6.3.
|
|
113
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
|
|
114
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/METADATA,sha256=g6I_79r5KT_3WD4b8mKW-bb_Q_Gx1O8pSZ2LwUOu-OA,25238
|
|
115
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
|
|
116
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
|
|
117
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
|
|
118
|
+
liger_kernel_nightly-0.6.3.dev20251121202601.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|