liger-kernel-nightly 0.6.1.dev20250728225847__py3-none-any.whl → 0.6.1.dev20250730201750__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/dpo_loss.py +54 -3
- liger_kernel/ops/fused_linear_cross_entropy.py +21 -13
- liger_kernel/transformers/functional.py +2 -0
- liger_kernel/transformers/fused_linear_cross_entropy.py +3 -0
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/METADATA +1 -1
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/RECORD +10 -10
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.6.1.dev20250728225847.dist-info → liger_kernel_nightly-0.6.1.dev20250730201750.dist-info}/top_level.txt +0 -0
@@ -13,6 +13,7 @@ class LigerFusedLinearDPOFunction(LigerFusedLinearPreferenceBase):
|
|
13
13
|
ref_chosen_logps=None,
|
14
14
|
ref_rejected_logps=None,
|
15
15
|
beta=0.1,
|
16
|
+
loss_type="sigmoid",
|
16
17
|
):
|
17
18
|
"""
|
18
19
|
Paper: https://arxiv.org/pdf/2305.18290
|
@@ -48,8 +49,50 @@ class LigerFusedLinearDPOFunction(LigerFusedLinearPreferenceBase):
|
|
48
49
|
chosen_rewards = beta * chosen_logratios
|
49
50
|
rejected_rewards = beta * rejected_logratios
|
50
51
|
|
51
|
-
|
52
|
-
|
52
|
+
if loss_type == "sigmoid":
|
53
|
+
logits_diff = beta * (chosen_logratios - rejected_logratios)
|
54
|
+
loss = -F.logsigmoid(logits_diff).sum() / (full_target.shape[0] // 2)
|
55
|
+
|
56
|
+
elif loss_type == "apo_zero":
|
57
|
+
# Eqn (7) of the APO paper (https://huggingface.co/papers/2408.06266)
|
58
|
+
# Use this loss when you believe the chosen outputs are better than your model's default output
|
59
|
+
losses_chosen = 1 - F.sigmoid(beta * chosen_logratios) # Increase chosen likelihood
|
60
|
+
losses_rejected = F.sigmoid(beta * rejected_logratios)
|
61
|
+
losses = losses_chosen + losses_rejected
|
62
|
+
loss = losses.sum() / (full_target.shape[0] // 2)
|
63
|
+
|
64
|
+
elif loss_type == "apo_down":
|
65
|
+
# Eqn (8) of the APO paper (https://huggingface.co/papers/2408.06266)
|
66
|
+
# Use this loss when you believe the chosen outputs are worse than your model's default output.
|
67
|
+
# Decrease chosen likelihood and decrease rejected likelihood more
|
68
|
+
losses_chosen = F.sigmoid(beta * chosen_logratios)
|
69
|
+
losses_rejected = 1 - F.sigmoid(beta * (chosen_logratios - rejected_logratios))
|
70
|
+
losses = losses_chosen + losses_rejected
|
71
|
+
loss = losses.sum() / (full_target.shape[0] // 2)
|
72
|
+
|
73
|
+
elif loss_type == "sppo_hard":
|
74
|
+
# In the paper (https://huggingface.co/papers/2405.00675), SPPO employs a soft probability approach,
|
75
|
+
# estimated using the PairRM score. The probability calculation is conducted outside of the trainer class.
|
76
|
+
# The version described here is the hard probability version, where P in Equation (4.7) of Algorithm 1 is
|
77
|
+
# set to 1 for the winner and 0 for the loser.
|
78
|
+
a = chosen_logps - ref_chosen_logps
|
79
|
+
b = rejected_logps - ref_rejected_logps
|
80
|
+
losses = (a - 0.5 / beta) ** 2 + (b + 0.5 / beta) ** 2
|
81
|
+
loss = losses.sum() / (full_target.shape[0] // 2)
|
82
|
+
|
83
|
+
elif loss_type == "nca_pair":
|
84
|
+
losses = (
|
85
|
+
-F.logsigmoid(chosen_rewards)
|
86
|
+
- 0.5 * F.logsigmoid(-chosen_rewards)
|
87
|
+
- 0.5 * F.logsigmoid(-rejected_rewards)
|
88
|
+
)
|
89
|
+
loss = losses.sum() / (full_target.shape[0] // 2)
|
90
|
+
|
91
|
+
else:
|
92
|
+
raise ValueError(
|
93
|
+
f"Unsupported loss_type: {loss_type}. Supported types are: sigmoid, apo_zero, apo_down, sppo_hard, nca_pair"
|
94
|
+
)
|
95
|
+
|
53
96
|
return loss, chosen_rewards, rejected_rewards
|
54
97
|
|
55
98
|
@classmethod
|
@@ -70,6 +113,7 @@ class LigerFusedLinearDPOFunction(LigerFusedLinearPreferenceBase):
|
|
70
113
|
use_ref_model=True,
|
71
114
|
average_log_prob=False,
|
72
115
|
chunk_size=1,
|
116
|
+
loss_type="sigmoid",
|
73
117
|
):
|
74
118
|
"""
|
75
119
|
Fused linear layer with DPO loss.
|
@@ -108,12 +152,13 @@ class LigerFusedLinearDPOFunction(LigerFusedLinearPreferenceBase):
|
|
108
152
|
ref_bias=ref_bias,
|
109
153
|
average_log_prob=average_log_prob,
|
110
154
|
chunk_size=chunk_size,
|
155
|
+
loss_type=loss_type,
|
111
156
|
)
|
112
157
|
|
113
158
|
@staticmethod
|
114
159
|
def backward(ctx, *grad_output):
|
115
160
|
grads = LigerFusedLinearPreferenceBase.backward(ctx, grad_output)[:4]
|
116
|
-
return *grads, None, None, None, None, None, None, None, None, None, None
|
161
|
+
return *grads, None, None, None, None, None, None, None, None, None, None, None
|
117
162
|
|
118
163
|
|
119
164
|
class LigerFusedLinearDPOLoss(torch.nn.Module):
|
@@ -130,6 +175,7 @@ class LigerFusedLinearDPOLoss(torch.nn.Module):
|
|
130
175
|
use_ref_model: bool = True,
|
131
176
|
average_log_prob: bool = False,
|
132
177
|
chunk_size: int = 1,
|
178
|
+
loss_type: str = "sigmoid",
|
133
179
|
):
|
134
180
|
"""
|
135
181
|
Args:
|
@@ -149,6 +195,10 @@ class LigerFusedLinearDPOLoss(torch.nn.Module):
|
|
149
195
|
self.use_ref_model = use_ref_model
|
150
196
|
self.average_log_prob = average_log_prob
|
151
197
|
self.chunk_size = chunk_size
|
198
|
+
self.loss_type = loss_type
|
199
|
+
supported_loss_types = {"sigmoid", "apo_zero", "apo_down", "sppo_hard", "nca_pair"}
|
200
|
+
if self.loss_type not in supported_loss_types:
|
201
|
+
raise ValueError(f"Unsupported loss_type: {self.loss_type}. Supported types are: {supported_loss_types}")
|
152
202
|
|
153
203
|
def forward(
|
154
204
|
self,
|
@@ -175,4 +225,5 @@ class LigerFusedLinearDPOLoss(torch.nn.Module):
|
|
175
225
|
self.use_ref_model,
|
176
226
|
self.average_log_prob,
|
177
227
|
self.chunk_size,
|
228
|
+
self.loss_type,
|
178
229
|
)
|
@@ -25,6 +25,7 @@ def fused_linear_cross_entropy_forward(
|
|
25
25
|
reduction="mean",
|
26
26
|
softcap=None,
|
27
27
|
return_z_loss=False,
|
28
|
+
accum_dtype=None,
|
28
29
|
):
|
29
30
|
assert isinstance(return_z_loss, bool), f"return_z_loss must be True or False. Got: {return_z_loss}"
|
30
31
|
device = _input.device
|
@@ -44,10 +45,16 @@ def fused_linear_cross_entropy_forward(
|
|
44
45
|
chunk_size = triton.next_power_of_2(triton.cdiv(BT, inc_factor)) # (BT + inc_factor - 1) // inc_factor
|
45
46
|
num_chunks = triton.cdiv(BT, chunk_size) # (BT + chunk_size - 1) // chunk_size
|
46
47
|
|
47
|
-
grad_weight = torch.zeros_like(weight, device=device) if weight.requires_grad else None
|
48
48
|
grad_input = torch.zeros_like(_input, device=device)
|
49
|
-
|
50
|
-
# we use fp32 for loss accumulator
|
49
|
+
|
50
|
+
# we use fp32 for loss and gradients accumulator
|
51
|
+
if accum_dtype is None:
|
52
|
+
grad_weight = torch.zeros_like(weight, device=device) if weight.requires_grad else None
|
53
|
+
grad_bias = torch.zeros_like(bias, device=device) if bias is not None else None
|
54
|
+
else:
|
55
|
+
grad_weight = torch.zeros_like(weight, dtype=accum_dtype, device=device) if weight.requires_grad else None
|
56
|
+
grad_bias = torch.zeros_like(bias, dtype=accum_dtype, device=device) if bias is not None else None
|
57
|
+
|
51
58
|
loss_1d = torch.zeros(BT, dtype=torch.float32, device=device)
|
52
59
|
z_loss_1d = torch.zeros(BT, dtype=_input.dtype, device=_input.device) if return_z_loss else None
|
53
60
|
|
@@ -124,16 +131,7 @@ def fused_linear_cross_entropy_forward(
|
|
124
131
|
grad_input[start_idx:end_idx] = grad_logits_chunk @ weight
|
125
132
|
|
126
133
|
if grad_weight is not None:
|
127
|
-
torch.
|
128
|
-
input=grad_weight,
|
129
|
-
mat1=logits_chunk.t().to(
|
130
|
-
_input_chunk.dtype
|
131
|
-
), # In an autocast scenario without bias, differing logits_chunk data types will cause an addmm operation error.
|
132
|
-
mat2=_input_chunk,
|
133
|
-
out=grad_weight,
|
134
|
-
alpha=1.0,
|
135
|
-
beta=1.0,
|
136
|
-
)
|
134
|
+
grad_weight += torch.mm(grad_logits_chunk.t(), _input_chunk).float()
|
137
135
|
|
138
136
|
if bias is not None:
|
139
137
|
torch.add(
|
@@ -151,6 +149,11 @@ def fused_linear_cross_entropy_forward(
|
|
151
149
|
else:
|
152
150
|
loss = torch.sum(loss_1d)
|
153
151
|
z_loss = torch.sum(z_loss_1d) if return_z_loss else None
|
152
|
+
|
153
|
+
# Cast back to original dtype
|
154
|
+
grad_weight = grad_weight.to(weight.dtype) if grad_weight is not None else None
|
155
|
+
grad_bias = grad_bias.to(bias.dtype) if grad_bias is not None else None
|
156
|
+
|
154
157
|
return loss, z_loss, grad_input, grad_weight, grad_bias
|
155
158
|
|
156
159
|
|
@@ -217,6 +220,7 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
|
|
217
220
|
reduction="mean",
|
218
221
|
softcap=None,
|
219
222
|
return_z_loss: bool = False,
|
223
|
+
accum_dtype=None,
|
220
224
|
):
|
221
225
|
"""
|
222
226
|
Fusing the last linear layer with cross-entropy loss
|
@@ -235,6 +239,8 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
|
|
235
239
|
ignore_index: the index to ignore in the target
|
236
240
|
label_smoothing (float): The amount of smoothing when computing the loss, where 0.0 means no smoothing.
|
237
241
|
reduction: reduction to apply
|
242
|
+
accum_dtype (torch.dtype): the dtype of intermediate result buffers for weight and bias gradient accumulations.
|
243
|
+
Recommended to set `accum_dtype` to higher precision, e.g. `torch.float32`, if the training is unstable with original dtype. Default: `None`, performing accumulations in original dtype
|
238
244
|
"""
|
239
245
|
|
240
246
|
loss, z_loss, grad_input, grad_weight, grad_bias = fused_linear_cross_entropy_forward(
|
@@ -249,6 +255,7 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
|
|
249
255
|
reduction=reduction,
|
250
256
|
softcap=softcap,
|
251
257
|
return_z_loss=return_z_loss,
|
258
|
+
accum_dtype=accum_dtype,
|
252
259
|
)
|
253
260
|
# downcast to dtype and store for backward
|
254
261
|
ctx.save_for_backward(
|
@@ -280,4 +287,5 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
|
|
280
287
|
None,
|
281
288
|
None,
|
282
289
|
None,
|
290
|
+
None,
|
283
291
|
)
|
@@ -64,6 +64,7 @@ def liger_fused_linear_cross_entropy(
|
|
64
64
|
reduction: str = "mean",
|
65
65
|
softcap: Optional[float] = None,
|
66
66
|
return_z_loss: bool = False,
|
67
|
+
accum_dtype=None,
|
67
68
|
):
|
68
69
|
loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
|
69
70
|
input,
|
@@ -77,6 +78,7 @@ def liger_fused_linear_cross_entropy(
|
|
77
78
|
reduction,
|
78
79
|
softcap,
|
79
80
|
return_z_loss,
|
81
|
+
accum_dtype,
|
80
82
|
)
|
81
83
|
if not return_z_loss:
|
82
84
|
return loss
|
@@ -15,6 +15,7 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
15
15
|
reduction: str = "mean",
|
16
16
|
softcap: Optional[float] = None,
|
17
17
|
return_z_loss: bool = False,
|
18
|
+
accum_dtype: Optional[torch.dtype] = None,
|
18
19
|
):
|
19
20
|
super().__init__()
|
20
21
|
assert (label_smoothing >= 0) and (label_smoothing <= 1), (
|
@@ -32,6 +33,7 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
32
33
|
self.reduction = reduction
|
33
34
|
self.softcap = softcap
|
34
35
|
self.return_z_loss = return_z_loss
|
36
|
+
self.accum_dtype = accum_dtype
|
35
37
|
|
36
38
|
def forward(self, lin_weight, _input, target, bias=None):
|
37
39
|
loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
|
@@ -46,6 +48,7 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
46
48
|
self.reduction,
|
47
49
|
self.softcap,
|
48
50
|
self.return_z_loss,
|
51
|
+
self.accum_dtype,
|
49
52
|
)
|
50
53
|
if not self.return_z_loss:
|
51
54
|
return loss
|
@@ -5,7 +5,7 @@ liger_kernel/chunked_loss/README.md,sha256=0FmkFC3hKBqyoDT5uTlIYmrvRkF-EOCR1y-EB
|
|
5
5
|
liger_kernel/chunked_loss/__init__.py,sha256=J5_jNnzZ4gZmA38W5f_4oab7xMoNk1Xy-yh3X_Xlf-s,714
|
6
6
|
liger_kernel/chunked_loss/cosine_similarity_loss.py,sha256=pZ07OQ6RI-c8uk96tDRlUXdt31-da7yWhfwircZlKRw,4198
|
7
7
|
liger_kernel/chunked_loss/cpo_loss.py,sha256=Gzz1eU4kgcbdubFVRy55e8A1Cr-r45UgNicXwZIjmBU,5454
|
8
|
-
liger_kernel/chunked_loss/dpo_loss.py,sha256=
|
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=ooR-qnZCyWJN935oHCSWLaKKKyaYERyhNczRGi1VOiw,11935
|
11
11
|
liger_kernel/chunked_loss/fused_linear_ppo.py,sha256=AA19cpv6D8mo5RbSK5GRCcZoOSnpxV_Z1eJlAsC5eic,13434
|
@@ -20,7 +20,7 @@ liger_kernel/ops/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,
|
|
20
20
|
liger_kernel/ops/cross_entropy.py,sha256=e8THGnhOcy_0SbOLABx67HEM7-B8a8pG7nDKbCRpQKM,19123
|
21
21
|
liger_kernel/ops/dyt.py,sha256=gCLz4S8aul8SY9nvIGaoK67aGb7U9MJRQdo3ONqmQYs,5417
|
22
22
|
liger_kernel/ops/fused_add_rms_norm.py,sha256=UBqmlqFCmhSAIpkNKd8rrfXatX7Z4J9bp2dX9A0lrJQ,14017
|
23
|
-
liger_kernel/ops/fused_linear_cross_entropy.py,sha256=
|
23
|
+
liger_kernel/ops/fused_linear_cross_entropy.py,sha256=YFPXUOIZpM_4r7AlfjkwOgDhAE_0H2mFjdKtx8cv-T4,11594
|
24
24
|
liger_kernel/ops/fused_linear_jsd.py,sha256=CSoprxb-YcJy-YUKiTcYkxN8sb9h2kdk_iHuncvSV5c,9683
|
25
25
|
liger_kernel/ops/fused_neighborhood_attention.py,sha256=vPi5xbnh6wxyZehaqo6Tuilqo2fN5SGDiONjnNmIKqs,35556
|
26
26
|
liger_kernel/ops/geglu.py,sha256=r0WSq9E93zzynL44Wh8femzOWK07_SseBM_pJUyxT3s,4144
|
@@ -45,9 +45,9 @@ liger_kernel/transformers/auto_model.py,sha256=0qCTRZt280Bj_LcFdzo9hlaR-BWNazawX
|
|
45
45
|
liger_kernel/transformers/cross_entropy.py,sha256=z3KTWQnFxr_IZaVjtYt0ZNEWQdDdYThN35xWkHlDGH0,1683
|
46
46
|
liger_kernel/transformers/dyt.py,sha256=i-4GPaMrl-jab9TVI5qN0-H9qycn_mCbV82ozU4nbmU,723
|
47
47
|
liger_kernel/transformers/fsdp.py,sha256=CUiyjTmjkjY7pLXQv8ly9rnzgXw6529csd9pvtJNMYc,3096
|
48
|
-
liger_kernel/transformers/functional.py,sha256=
|
48
|
+
liger_kernel/transformers/functional.py,sha256=XkYk_zb8xsRMtZtouYmlX_Tyyr-QA3WigSPF36DECYk,7777
|
49
49
|
liger_kernel/transformers/fused_add_rms_norm.py,sha256=7_Bzg-x6lLe6W1qG2DtjDALhEpNZlC6N5GppEs9cTYY,1199
|
50
|
-
liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=
|
50
|
+
liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=_5AaQT2mcUEO2T7JGJYQafz6A1Efn9d3-Z3xFO_Xe0o,1862
|
51
51
|
liger_kernel/transformers/fused_linear_jsd.py,sha256=bZ4otCvWBuOnA5XdQL-FzZVItJlDt-ht9e_pG7PG93E,3999
|
52
52
|
liger_kernel/transformers/fused_neighborhood_attention.py,sha256=TxYDUAt9B6WSP14aJP66C_2Mbds2sSIPGnamhUSTrC8,7957
|
53
53
|
liger_kernel/transformers/geglu.py,sha256=mrgqzIUVd6lN7fkDKLkw5YaESDxDtFgbot430WwPVOQ,1107
|
@@ -92,9 +92,9 @@ liger_kernel/transformers/trainer/__init__.py,sha256=p7yQfklV8-467qSz_ZMimkbDF7H
|
|
92
92
|
liger_kernel/transformers/trainer/orpo_trainer.py,sha256=tX0h63aOFe3rNqTmk6JpMf75UPo981yzEa6TghnjS0Q,5370
|
93
93
|
liger_kernel/triton/__init__.py,sha256=qCiCamzCRv6lpV8IqpAc9YMdNKC7GKurClWceQPnlis,92
|
94
94
|
liger_kernel/triton/monkey_patch.py,sha256=Rd0hUHAzDkFfHvnX7-PBaNK5EKnZhtfM_h-fgQH9HPY,1568
|
95
|
-
liger_kernel_nightly-0.6.1.
|
96
|
-
liger_kernel_nightly-0.6.1.
|
97
|
-
liger_kernel_nightly-0.6.1.
|
98
|
-
liger_kernel_nightly-0.6.1.
|
99
|
-
liger_kernel_nightly-0.6.1.
|
100
|
-
liger_kernel_nightly-0.6.1.
|
95
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
|
96
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/METADATA,sha256=th9i8KliNiFeyOFSAfZ0ZosaGiEtaJNFaQZvxwZqh_4,24502
|
97
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
|
98
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
|
99
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
|
100
|
+
liger_kernel_nightly-0.6.1.dev20250730201750.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|