liger-kernel-nightly 0.4.2.dev20241121054604__py3-none-any.whl → 0.4.2.dev20241121224158__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.
@@ -202,7 +202,7 @@ class LigerFusedLinearJSDFunction(torch.autograd.Function):
202
202
  teacher_input (torch.tensor): input of the last projection layer in teacher model, with shape (B*T, H), where B is batch size, T is sequence length, H is hidden dimension.
203
203
  teacher_weight (torch.tensor): the last projection layer in teacher model, with shape (V, H), where V is vocab size
204
204
  shift_labels (Optional[torch.LongTensor]): indicator of next predicted vocab with shape (BT) where each value is in [0, V-1].
205
- jsd_beta (float): coefficient beta of generalized JSD in the open interval (0, 1). Default: `0.5`
205
+ jsd_beta (float): coefficient beta of generalized JSD in the interval [0, 1]. It implements forward/reverse KL when beta equals 0 and 1 respectively. Default: `0.5`
206
206
  ignore_index (int): the index to ignore. Default: -100
207
207
  temperature (float): temperature in softmax function to control the output probability distribution. Default: `1.0`
208
208
 
liger_kernel/ops/jsd.py CHANGED
@@ -18,7 +18,7 @@ def _jsd_kernel(
18
18
  dX_ptr,
19
19
  dX_stride,
20
20
  label_ptr,
21
- beta,
21
+ beta: tl.constexpr,
22
22
  n_non_ignore: int,
23
23
  ignore_index: tl.constexpr,
24
24
  n_cols,
@@ -50,17 +50,26 @@ def _jsd_kernel(
50
50
  X = tl.load(X_ptr + offsets, mask=mask, other=float("-inf")).to(tl.float32)
51
51
  Y = tl.load(Y_ptr + offsets, mask=mask, other=float("-inf")).to(tl.float32)
52
52
 
53
- Q = tl.exp(X)
54
- P = tl.exp(Y)
55
- M = beta * P + (1 - beta) * Q
56
- log_M = tl.log(M)
53
+ if beta == 0.0: # forward KL
54
+ Y_prob = tl.exp(Y)
55
+ loss = Y_prob * (Y - X)
56
+ dX = -Y_prob
57
+ elif beta == 1.0:
58
+ X_prob = tl.exp(X)
59
+ loss = X_prob * (X - Y)
60
+ dX = loss + X_prob
61
+ else:
62
+ Q = tl.exp(X)
63
+ P = tl.exp(Y)
64
+ M = beta * P + (1 - beta) * Q
65
+ log_M = tl.log(M)
66
+
67
+ loss = beta * P * Y + (1 - beta) * Q * X - M * log_M
68
+ dX = (1 - beta) * Q * (X - log_M)
57
69
 
58
- loss = beta * P * Y + (1 - beta) * Q * X - M * log_M
59
- # reduction == "batchmean"
60
70
  loss = loss / n_non_ignore
71
+ dX = dX / n_non_ignore
61
72
  tl.store(loss_ptr + offsets, loss, mask=mask)
62
-
63
- dX = (1 - beta) * Q * (X - log_M) / n_non_ignore
64
73
  tl.store(dX_ptr + offsets, dX, mask=mask)
65
74
 
66
75
 
@@ -142,7 +151,7 @@ class LigerJSDFunction(torch.autograd.Function):
142
151
  _input (torch.Tensor): predict values with shape (BT, V) in logspace
143
152
  target (torch.Tensor): ground truth values with shape (BT, V) in logspace
144
153
  shift_labels (Optional[torch.LongTensor]): indicator of next predicted vocab with shape (BT) where each value is in [0, V-1].
145
- beta (float): coefficient beta of generalized JSD in the open interval (0, 1)
154
+ beta (float): coefficient beta of generalized JSD in the interval [0, 1]. It implements forward/reverse KL when beta equals 0 and 1 respectively. Default: `0.5`
146
155
  ignore_index (int): the index to ignore. Default: -100
147
156
 
148
157
  Returns:
@@ -12,7 +12,7 @@ class LigerFusedLinearJSD(torch.nn.Module):
12
12
  the materialization of the large logits tensor.
13
13
 
14
14
  Args:
15
- jsd_beta (float): coefficient beta of generalized JSD in the open interval (0, 1). Default: `0.5`
15
+ jsd_beta (float): coefficient beta of generalized JSD in the interval [0, 1]. It implements forward/reverse KL when beta equals 0 and 1 respectively. Default: `0.5`
16
16
  ignore_index (int): The index to ignore in the target. Default: `-100`
17
17
  temperature (float): temperature in softmax function to control the output probability distribution. Default: `1.0`
18
18
 
@@ -70,9 +70,6 @@ class LigerFusedLinearJSD(torch.nn.Module):
70
70
 
71
71
  def __init__(self, jsd_beta=0.5, ignore_index=-100, temperature=1.0):
72
72
  super().__init__()
73
- assert (
74
- jsd_beta > 0 and jsd_beta < 1
75
- ), f"beta must be greater than 0 and less than 1. Got: {jsd_beta}"
76
73
  assert temperature != 0, "temperature cannot be 0."
77
74
  self.jsd_beta = jsd_beta
78
75
  self.temperature = temperature
@@ -18,7 +18,7 @@ class LigerJSD(torch.nn.Module):
18
18
  :math:`P` denotes the teacher model and :math:`Q` denotes the student model.
19
19
 
20
20
  Args:
21
- beta (float): coefficient beta of generalized JSD in the open interval (0, 1). Default: `0.5`
21
+ beta (float): coefficient beta of generalized JSD in the interval [0, 1]. It implements forward/reverse KL when beta equals 0 and 1 respectively. Default: `0.5`
22
22
  ignore_index (int): The index to ignore in the target. Default: `-100`
23
23
 
24
24
  Shape:
@@ -58,9 +58,6 @@ class LigerJSD(torch.nn.Module):
58
58
 
59
59
  def __init__(self, beta: float = 0.5, ignore_index: int = -100):
60
60
  super().__init__()
61
- assert (
62
- beta > 0 and beta < 1
63
- ), f"beta must be greater than 0 and less than 1. Got: {beta}"
64
61
  self.beta = beta
65
62
  self.ignore_index = ignore_index
66
63
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: liger_kernel_nightly
3
- Version: 0.4.2.dev20241121054604
3
+ Version: 0.4.2.dev20241121224158
4
4
  Summary: Efficient Triton kernels for LLM Training
5
5
  License: BSD 2-CLAUSE LICENSE
6
6
  Copyright 2024 LinkedIn Corporation
@@ -303,8 +303,8 @@ $$\text{GeGLU}(x)=\text{GELU}(xW+b)\otimes(xV+c)$$
303
303
  <!-- TODO: verify vocab sizes are accurate -->
304
304
  - **FusedLinearCrossEntropy**: Peak memory usage of cross entropy loss is further improved by fusing the model head with the CE loss and chunking the input for block-wise loss and gradient calculation, a technique inspired by [Efficient Cross Entropy](https://github.com/mgmalek/efficient_cross_entropy). It achieves >4X memory reduction for 128k vocab size. **This is highly effective for large batch size, large sequence length, and large vocabulary sizes.** Please refer to the [Medusa example](https://github.com/linkedin/Liger-Kernel/tree/main/examples/medusa) for individual kernel usage.
305
305
  - **KLDivergence**: [KL Divergence](https://pytorch.org/docs/stable/generated/torch.nn.KLDivLoss.html) is implemented by fusing the forward into a single triton kernel, with reduction done outside the kernel. It achieves ~1.5X speed and ~15% memory reduction for 128K vocab size.
306
- - **JSD**: [Generalized JSD](https://arxiv.org/pdf/2306.13649) (Jensen-Shannon divergence), is implemented by computing both the loss and gradient in the forward pass. It achieves ~1.5X speed and ~54% memory reduction for 128k vocab size.
307
- - **FusedLinearJSD**: Peak memory usage of JSD loss is further improved by fusing the model head with the JSD and chunking the input for block-wise loss and gradient calculation. It achieves ~85% memory reduction for 128k vocab size where batch size $\times$ sequence length is 8192.
306
+ - **JSD**: [Generalized JSD](https://arxiv.org/pdf/2306.13649) (Jensen-Shannon divergence), is implemented by computing both the loss and gradient in the forward pass. It achieves ~1.5X speed and ~54% memory reduction for 128k vocab size. **NOTE**: It implements forward/reverse KL when `beta` equals 0 and 1 respectively.
307
+ - **FusedLinearJSD**: Peak memory usage of JSD loss is further improved by fusing the model head with the JSD and chunking the input for block-wise loss and gradient calculation. It achieves ~85% memory reduction for 128k vocab size where batch size $\times$ sequence length is 8192. **NOTE**: It implements forward/reverse KL when `beta` equals 0 and 1 respectively.
308
308
 
309
309
 
310
310
  ### Experimental Kernels
@@ -9,10 +9,10 @@ liger_kernel/chunked_loss/simpo_loss.py,sha256=Jpl_U6DfxlzyHnlKN2i05K0vwz-ouiTmx
9
9
  liger_kernel/ops/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  liger_kernel/ops/cross_entropy.py,sha256=sfUb7-jIZp0EKXjg1DYy2Wdzw_Mg-mHmGoR5bpdm4tw,15526
11
11
  liger_kernel/ops/fused_linear_cross_entropy.py,sha256=ib7M3AjJE164yMfuS9R39k-5qnDgYOXptIT146lqYbg,9964
12
- liger_kernel/ops/fused_linear_jsd.py,sha256=5D_obamh08lGGTMyh85kBJD_aNjPhOYf4-TmCZ6m4s4,9626
12
+ liger_kernel/ops/fused_linear_jsd.py,sha256=nOv4zwfxHqqepKEmMsQuz-B3H-gRjyo8uClpmqSGLYA,9693
13
13
  liger_kernel/ops/geglu.py,sha256=MQL4zyzneZqZYUGPvb1QjI_EYT9_pKfSDgR25WD9jrI,4127
14
14
  liger_kernel/ops/group_norm.py,sha256=VaRErVJGR4JqgXXvuIjNGTn3E2egjLtU1y3ymwIf4d8,10961
15
- liger_kernel/ops/jsd.py,sha256=anWfdioucxZy4JQfTvbHBR-IQrZKeH-gBF1MHwwTuTQ,5781
15
+ liger_kernel/ops/jsd.py,sha256=Ap2b0_geCl6fqBXLI1IS6Yn6GlO-8LgPmnOW3y47dus,6151
16
16
  liger_kernel/ops/kl_div.py,sha256=03FNXfvCb6M-56hhFepAFV9p6brArPR6KOKkdGD34mw,8374
17
17
  liger_kernel/ops/layer_norm.py,sha256=unGMYMOPqtkM9aTrokhcqgPmsV2AUN7Yzv86isVB9OI,7422
18
18
  liger_kernel/ops/qwen2vl_mrope.py,sha256=xZvQnhkSTjU-k6KiiRn9e0SYO1ESs1jmuZFMICduLpc,8552
@@ -27,10 +27,10 @@ liger_kernel/transformers/auto_model.py,sha256=RMIwQHSiXoksXFTIqFZ4PLBgoqkxJJAT3
27
27
  liger_kernel/transformers/cross_entropy.py,sha256=yEm_YQ7oa3_BzT3hdW6KrAslduhSqWcJQVNZZDcWCg4,1758
28
28
  liger_kernel/transformers/functional.py,sha256=jwTHmyjOVC1_I-6ztY1EbbRqPIfFHojcHrP2c4P6U4I,2123
29
29
  liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=_i0PXSp5iZ9pKXdEeZ4lvHCENJYjV4y74yz3ZRG5XQg,1484
30
- liger_kernel/transformers/fused_linear_jsd.py,sha256=MJ-KjmLZnakuoVpnbDGkd95DQgvESniyrRWYzollVZM,4066
30
+ liger_kernel/transformers/fused_linear_jsd.py,sha256=bZ4otCvWBuOnA5XdQL-FzZVItJlDt-ht9e_pG7PG93E,3999
31
31
  liger_kernel/transformers/geglu.py,sha256=QcrME_8ooIn0xa59LaC0aoOdRrBIFd11Y0bAyF0NfCw,1130
32
32
  liger_kernel/transformers/group_norm.py,sha256=FJ9R7mS9G1wO-GRIQ6QKSmIhnZ6nQ6GIkE4NnX_hnn0,2241
33
- liger_kernel/transformers/jsd.py,sha256=W-5CypO2mx4-bUWOxq1KScfCdoXlLoYbtt5xBnRzMs4,3056
33
+ liger_kernel/transformers/jsd.py,sha256=sbr8DnKSYZJH9pv2rpmboNijYGpZKbhb2-WSGp5_v6g,3001
34
34
  liger_kernel/transformers/kl_div.py,sha256=qVhjBg6tjRyue5iZ3NFxo8uySY4JuIFJyv0IM_50F24,431
35
35
  liger_kernel/transformers/layer_norm.py,sha256=fd6o4kSHJWolQMWxh-l1qObfgL08ruNbUoBiANKX1ow,972
36
36
  liger_kernel/transformers/monkey_patch.py,sha256=Fk2v4GZQDJzfh3Cpc6BHNJbs_tungDyWmqS9nuG9Lc4,38406
@@ -52,9 +52,9 @@ liger_kernel/transformers/model/qwen2.py,sha256=EyhSSzQOskGjSnCsKMZpd1s5IAIlHd5P
52
52
  liger_kernel/transformers/model/qwen2_vl.py,sha256=bIQe2bWiY--G84FhCD29Gdi64_qHP6vbcGsK6vKysQE,8547
53
53
  liger_kernel/triton/__init__.py,sha256=yfRe0zMb47QnqjecZWG7LnanfCTzeku7SgWRAwNVmzU,101
54
54
  liger_kernel/triton/monkey_patch.py,sha256=5BcGKTtdqeYchypBIBopGIWPx1-cFALz7sOKoEsqXJ0,1584
55
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
56
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/METADATA,sha256=JFMw-oIVSlNwaCh7Fi2x1Q-JdLAOK_s9qxkV4idrGEQ,21723
57
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
58
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
59
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
60
- liger_kernel_nightly-0.4.2.dev20241121054604.dist-info/RECORD,,
55
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
56
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/METADATA,sha256=3HyUur6qJmSMTQaxiLaiDaGUrvU3_ILHlvWdobywuso,21891
57
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
58
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
59
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
60
+ liger_kernel_nightly-0.4.2.dev20241121224158.dist-info/RECORD,,