liger-kernel-nightly 0.5.2.dev20250115025212__py3-none-any.whl → 0.5.2.dev20250119025401__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,7 +24,9 @@ def fused_linear_cross_entropy_forward(
24
24
  label_smoothing=0.0,
25
25
  reduction="mean",
26
26
  softcap=None,
27
+ return_z_loss=False,
27
28
  ):
29
+ assert isinstance(return_z_loss, bool), f"return_z_loss must be True or False. Got: {return_z_loss}"
28
30
  device = _input.device
29
31
 
30
32
  # inputs have shape: BT x H
@@ -47,6 +49,7 @@ def fused_linear_cross_entropy_forward(
47
49
  grad_bias = torch.zeros_like(bias, device=device) if bias is not None else None
48
50
  # we use fp32 for loss accumulator
49
51
  loss_1d = torch.zeros(BT, dtype=torch.float32, device=device)
52
+ z_loss_1d = torch.zeros(BT, dtype=_input.dtype, device=_input.device) if return_z_loss else None
50
53
 
51
54
  # TODO: evaluate how CUDA synchronization caused by .item() affects the speed
52
55
  target_mask = target != ignore_index
@@ -81,6 +84,7 @@ def fused_linear_cross_entropy_forward(
81
84
 
82
85
  # unreduced loss
83
86
  loss_1d_slice = loss_1d[start_idx:end_idx] # chunk_size,
87
+ z_loss_1d_slice = z_loss_1d[start_idx:end_idx] if return_z_loss else None
84
88
 
85
89
  # ensure _input and target are contiguous
86
90
  logits_chunk = logits_chunk.contiguous()
@@ -94,7 +98,7 @@ def fused_linear_cross_entropy_forward(
94
98
  Y_stride=target_chunk.stride(-1), # always 1
95
99
  weight_ptr=ce_weight,
96
100
  loss_ptr=loss_1d_slice,
97
- z_loss_ptr=None,
101
+ z_loss_ptr=z_loss_1d_slice,
98
102
  loss_stride=loss_1d_slice.stride(-1), # always 1
99
103
  n_cols=V,
100
104
  n_non_ignore=total_n_non_ignore,
@@ -105,7 +109,7 @@ def fused_linear_cross_entropy_forward(
105
109
  label_smoothing=label_smoothing,
106
110
  reduction=reduction,
107
111
  softcap=softcap,
108
- RETURN_Z_LOSS=False,
112
+ RETURN_Z_LOSS=return_z_loss,
109
113
  HAS_WEIGHT=True if ce_weight is not None else False,
110
114
  HAS_SOFTCAPPING=True if softcap is not None else False,
111
115
  BLOCK_SIZE=BLOCK_SIZE,
@@ -113,6 +117,8 @@ def fused_linear_cross_entropy_forward(
113
117
  )
114
118
 
115
119
  loss_1d[start_idx:end_idx] = loss_1d_slice
120
+ if return_z_loss:
121
+ z_loss_1d[start_idx:end_idx] = z_loss_1d_slice
116
122
  grad_logits_chunk = logits_chunk # chunk_size x V
117
123
 
118
124
  grad_input[start_idx:end_idx] = grad_logits_chunk @ weight
@@ -139,9 +145,12 @@ def fused_linear_cross_entropy_forward(
139
145
 
140
146
  if reduction == "none":
141
147
  loss = loss_1d
148
+ z_loss = z_loss_1d if return_z_loss else None
149
+
142
150
  else:
143
151
  loss = torch.sum(loss_1d)
144
- return loss, grad_input, grad_weight, grad_bias
152
+ z_loss = torch.sum(z_loss_1d) if return_z_loss else None
153
+ return loss, z_loss, grad_input, grad_weight, grad_bias
145
154
 
146
155
 
147
156
  def fused_linear_cross_entropy_backward(grad_output, grad_input, grad_weight, grad_bias):
@@ -206,6 +215,7 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
206
215
  label_smoothing=0.0,
207
216
  reduction="mean",
208
217
  softcap=None,
218
+ return_z_loss: bool = False,
209
219
  ):
210
220
  """
211
221
  Fusing the last linear layer with cross-entropy loss
@@ -226,7 +236,7 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
226
236
  reduction: reduction to apply
227
237
  """
228
238
 
229
- loss, grad_input, grad_weight, grad_bias = fused_linear_cross_entropy_forward(
239
+ loss, z_loss, grad_input, grad_weight, grad_bias = fused_linear_cross_entropy_forward(
230
240
  _input=_input,
231
241
  weight=weight,
232
242
  target=target,
@@ -237,6 +247,7 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
237
247
  label_smoothing=label_smoothing,
238
248
  reduction=reduction,
239
249
  softcap=softcap,
250
+ return_z_loss=return_z_loss,
240
251
  )
241
252
  # downcast to dtype and store for backward
242
253
  ctx.save_for_backward(
@@ -244,11 +255,14 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
244
255
  grad_weight.detach() if grad_weight is not None else None,
245
256
  grad_bias.detach() if bias is not None else None,
246
257
  )
247
- return loss
258
+ ctx.return_z_loss = return_z_loss
259
+ return loss, z_loss
248
260
 
249
261
  @staticmethod
250
262
  @amp_custom_bwd
251
- def backward(ctx, grad_output):
263
+ def backward(ctx, grad_output, grad_output2):
264
+ if ctx.return_z_loss:
265
+ del grad_output2 # z_loss is only for logging
252
266
  (grad_input, grad_weight, grad_bias) = ctx.saved_tensors
253
267
  grad_input, grad_weight, grad_bias = fused_linear_cross_entropy_backward(
254
268
  grad_output, grad_input, grad_weight, grad_bias
@@ -264,4 +278,5 @@ class LigerFusedLinearCrossEntropyFunction(torch.autograd.Function):
264
278
  None,
265
279
  None,
266
280
  None,
281
+ None,
267
282
  )
@@ -56,8 +56,9 @@ def liger_fused_linear_cross_entropy(
56
56
  label_smoothing: float = 0.0,
57
57
  reduction: str = "mean",
58
58
  softcap: Optional[float] = None,
59
+ return_z_loss: bool = False,
59
60
  ):
60
- return LigerFusedLinearCrossEntropyFunction.apply(
61
+ loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
61
62
  input,
62
63
  weight,
63
64
  target,
@@ -68,7 +69,11 @@ def liger_fused_linear_cross_entropy(
68
69
  label_smoothing,
69
70
  reduction,
70
71
  softcap,
72
+ return_z_loss,
71
73
  )
74
+ if not return_z_loss:
75
+ return loss
76
+ return loss, z_loss
72
77
 
73
78
 
74
79
  def liger_fused_linear_jsd(
@@ -14,6 +14,7 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
14
14
  label_smoothing: float = 0.0,
15
15
  reduction: str = "mean",
16
16
  softcap: Optional[float] = None,
17
+ return_z_loss: bool = False,
17
18
  ):
18
19
  super().__init__()
19
20
  assert (label_smoothing >= 0) and (
@@ -31,9 +32,10 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
31
32
  self.label_smoothing = label_smoothing
32
33
  self.reduction = reduction
33
34
  self.softcap = softcap
35
+ self.return_z_loss = return_z_loss
34
36
 
35
37
  def forward(self, lin_weight, _input, target, bias=None):
36
- return LigerFusedLinearCrossEntropyFunction.apply(
38
+ loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
37
39
  _input,
38
40
  lin_weight,
39
41
  target,
@@ -44,4 +46,8 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
44
46
  self.label_smoothing,
45
47
  self.reduction,
46
48
  self.softcap,
49
+ self.return_z_loss,
47
50
  )
51
+ if not self.return_z_loss:
52
+ return loss
53
+ return loss, z_loss
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: liger_kernel_nightly
3
- Version: 0.5.2.dev20250115025212
3
+ Version: 0.5.2.dev20250119025401
4
4
  Summary: Efficient Triton kernels for LLM Training
5
5
  License: BSD 2-CLAUSE LICENSE
6
6
  Copyright 2024 LinkedIn Corporation
@@ -12,7 +12,7 @@ liger_kernel/chunked_loss/orpo_loss.py,sha256=yjcrrbVeemLYodoSKT-FMSnaPtyKAZ3aOr
12
12
  liger_kernel/chunked_loss/simpo_loss.py,sha256=3TTc7U79Orjgi-Wu81WZkWk5MgsdqKXIOBHgIvDazPw,3865
13
13
  liger_kernel/ops/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  liger_kernel/ops/cross_entropy.py,sha256=SRzAF9Ek84pBVFy3wqQZs7AhRoorKRIgQ-Td_rtl1Kk,18606
15
- liger_kernel/ops/fused_linear_cross_entropy.py,sha256=hezFRwbcPc-HNGZUFqUn5AYUqUpboPpFh4MNqEW4WgU,10108
15
+ liger_kernel/ops/fused_linear_cross_entropy.py,sha256=jm0bJLRp7UpE4MCa3yhvaSErP792Ze8nNgtUIbOw5sw,10910
16
16
  liger_kernel/ops/fused_linear_jsd.py,sha256=eKqaADj7LgWfoYqyH03tjrmhNTfJOF1Dhx_bWzBTnTU,9600
17
17
  liger_kernel/ops/geglu.py,sha256=axGvCIvlBzuluoAIrWTsp2iZM4BFKNInkPov8YVvH9E,4126
18
18
  liger_kernel/ops/group_norm.py,sha256=qD4D4lSjSgVtO52EBNLC2iTseALRgPgqXE50U2woggk,10837
@@ -29,8 +29,8 @@ liger_kernel/ops/experimental/mm_int8int2.py,sha256=TrS9lpwekrik_w5qE7AhMJD1bcq-
29
29
  liger_kernel/transformers/__init__.py,sha256=QPmYkL6hosBPpPqCUGqvIvAtD9XzLgvZqZxUyYMZeVk,2008
30
30
  liger_kernel/transformers/auto_model.py,sha256=0qCTRZt280Bj_LcFdzo9hlaR-BWNazawXOGgoCZjgEg,1545
31
31
  liger_kernel/transformers/cross_entropy.py,sha256=LtiHlj_tK2YFpilwvbG_NEVzbf82zKRpWCZMjaFUd4M,1681
32
- liger_kernel/transformers/functional.py,sha256=B1wkHWLx-YNhxvXBEXB4Ch1yEwF3mjwTPCeXA5aCV_c,4490
33
- liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=LAN8-pjUI2Erz_MnfMer-0ZmxJ0JlKxGzdZGJY-N65g,1569
32
+ liger_kernel/transformers/functional.py,sha256=lDOjch622dJIc78K3ePFK_H1DX00GC5kKjodjcbEgbM,4624
33
+ liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=ygU7cycCHWvSrrTgn2TseN8t-Qwfer4V7ldwhZ1E6_g,1776
34
34
  liger_kernel/transformers/fused_linear_jsd.py,sha256=bZ4otCvWBuOnA5XdQL-FzZVItJlDt-ht9e_pG7PG93E,3999
35
35
  liger_kernel/transformers/geglu.py,sha256=mrgqzIUVd6lN7fkDKLkw5YaESDxDtFgbot430WwPVOQ,1107
36
36
  liger_kernel/transformers/group_norm.py,sha256=URmjkQFsrbMffzcJiGpX7ckxWlpL95AiJS-80hwAWPk,2173
@@ -58,9 +58,9 @@ liger_kernel/transformers/trainer/__init__.py,sha256=p7yQfklV8-467qSz_ZMimkbDF7H
58
58
  liger_kernel/transformers/trainer/orpo_trainer.py,sha256=pdekW7l6Qg_aqa5SYKYlSWUF8m3lkOFvFLcIMEHrz9s,8338
59
59
  liger_kernel/triton/__init__.py,sha256=qCiCamzCRv6lpV8IqpAc9YMdNKC7GKurClWceQPnlis,92
60
60
  liger_kernel/triton/monkey_patch.py,sha256=Rd0hUHAzDkFfHvnX7-PBaNK5EKnZhtfM_h-fgQH9HPY,1568
61
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
62
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/METADATA,sha256=sDbWP1JYiBLWMiChKJMWMd5sVddlHdRk0Jw8oQnkEP8,21055
63
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
64
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
65
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
66
- liger_kernel_nightly-0.5.2.dev20250115025212.dist-info/RECORD,,
61
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
62
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/METADATA,sha256=u0kPURJPvpM73KiKZdLmhZmMH5Qyk-cb1gxj1pXnIJw,21055
63
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
64
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
65
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
66
+ liger_kernel_nightly-0.5.2.dev20250119025401.dist-info/RECORD,,