liger-kernel-nightly 0.4.2.dev20241122052539__py3-none-any.whl → 0.4.2.dev20241123040418__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.
File without changes
@@ -92,8 +92,8 @@ def liger_cross_entropy_kernel(
92
92
  # 3. [Online softmax] first pass: find max + sum
93
93
  m = float("-inf") # m is the max value. use the notation from the paper
94
94
  d = 0.0 # d is the sum. use the notation from the paper
95
- ori_X_y = tl.load(
96
- X_ptr + y
95
+ ori_X_y = tl.load(X_ptr + y).cast(
96
+ tl.float32
97
97
  ) # we need to store the original value of X_y for the loss calculation
98
98
  if HAS_SOFTCAPPING:
99
99
  ori_X_y = softcap * tanh(ori_X_y / softcap)
@@ -106,8 +106,11 @@ def liger_cross_entropy_kernel(
106
106
  for i in range(0, n_cols, BLOCK_SIZE):
107
107
  X_offsets = i + tl.arange(0, BLOCK_SIZE)
108
108
  X_block = tl.load(
109
- X_ptr + X_offsets, mask=X_offsets < n_cols, other=float("-inf")
110
- )
109
+ X_ptr + X_offsets,
110
+ mask=X_offsets < n_cols,
111
+ other=float("-inf"),
112
+ # Ensure float32 precision for softmax calculation
113
+ ).cast(tl.float32)
111
114
  if HAS_SOFTCAPPING:
112
115
  X_block = softcap * tanh(X_block / softcap)
113
116
  block_max = tl.max(X_block)
@@ -141,8 +144,11 @@ def liger_cross_entropy_kernel(
141
144
  for i in range(0, n_cols, BLOCK_SIZE):
142
145
  X_offsets = i + tl.arange(0, BLOCK_SIZE)
143
146
  X_block = tl.load(
144
- X_ptr + X_offsets, mask=X_offsets < n_cols, other=float("-inf")
145
- )
147
+ X_ptr + X_offsets,
148
+ mask=X_offsets < n_cols,
149
+ other=float("-inf"),
150
+ # Ensure float32 precision for softmax calculation
151
+ ).cast(tl.float32)
146
152
  if HAS_SOFTCAPPING:
147
153
  intermediate = tanh(X_block / softcap)
148
154
  X_block = softcap * intermediate
@@ -26,7 +26,6 @@ def fused_linear_cross_entropy_forward(
26
26
  reduction="mean",
27
27
  softcap=None,
28
28
  ):
29
- dtype = _input.dtype
30
29
  device = _input.device
31
30
 
32
31
  # inputs have shape: BT x H
@@ -74,9 +73,6 @@ def fused_linear_cross_entropy_forward(
74
73
  loss_1d_slice = loss_1d[start_idx:end_idx] # chunk_size,
75
74
  n_non_ignore = (target_chunk != ignore_index).sum().item()
76
75
 
77
- # when doing CE, use the upcasted precision
78
- logits_chunk = logits_chunk.float()
79
-
80
76
  # ensure _input and target are contiguous
81
77
  logits_chunk = logits_chunk.contiguous()
82
78
  target_chunk = target_chunk.contiguous()
@@ -103,13 +99,6 @@ def fused_linear_cross_entropy_forward(
103
99
  num_warps=32 if not is_hip() else 16,
104
100
  )
105
101
 
106
- # gradient of logits_chunk is computed in-place by the above triton kernel.
107
- # Following HuggingFace model source code, we do the forward and backward
108
- # w.r.t. logits in fp32 for numerical stability especially as the num classes (vocab size) is huge.
109
- # (reference: https://github.com/huggingface/transformers/blob/v4.42.4/src/transformers/models/llama/modeling_llama.py#L1194)
110
- # Propagating to lm_head's backward, we'll switch back to the original dtype.
111
- logits_chunk = logits_chunk.to(dtype)
112
-
113
102
  # gradient of logits_chunk is computed in-place by the above triton kernel and is of shape: chunk_size x V
114
103
  # thus grad_input[start_idx: end_idx] should be of shape: chunk_size x H
115
104
  # additionally, since we are chunking the inputs, observe that the loss and gradients are calculated only
@@ -180,8 +180,13 @@ def layer_norm_backward(dY, X, W, B, Mean, RSTD):
180
180
  dY = dY.view(-1, dim)
181
181
  n_rows, n_cols = dY.shape
182
182
 
183
+ sm_count = 1
184
+ if X.device.type == "cuda":
185
+ sm_count = torch.cuda.get_device_properties(X.device).multi_processor_count
186
+ elif X.device.type == "xpu":
187
+ sm_count = torch.xpu.get_device_properties(X.device).gpu_subslice_count
188
+
183
189
  DX = torch.empty((n_rows, n_cols), dtype=X.dtype, device=X.device)
184
- sm_count = torch.cuda.get_device_properties(X.device).multi_processor_count
185
190
  _DW = torch.empty((sm_count, n_cols), dtype=W.dtype, device=W.device)
186
191
  _DB = torch.empty((sm_count, n_cols), dtype=W.dtype, device=W.device)
187
192
 
@@ -264,6 +264,7 @@ def rms_norm_backward(
264
264
  dY = dY.view(-1, dim)
265
265
  n_rows, n_cols = dY.shape
266
266
 
267
+ sm_count = 1
267
268
  if X.device.type == "cuda":
268
269
  sm_count = torch.cuda.get_device_properties(X.device).multi_processor_count
269
270
  elif X.device.type == "xpu":
liger_kernel/ops/utils.py CHANGED
@@ -20,6 +20,8 @@ import triton
20
20
  import triton.language as tl
21
21
  from packaging.version import Version
22
22
 
23
+ from liger_kernel.utils import infer_device
24
+
23
25
 
24
26
  def is_hip() -> bool:
25
27
  return torch.version.hip is not None
@@ -69,10 +71,11 @@ def compare_version(package: str, operator: Callable, target: str):
69
71
 
70
72
 
71
73
  def get_amp_custom_fwd_bwd() -> Callable:
74
+ device = infer_device()
72
75
  if compare_version("torch", operator.ge, "2.4.0"):
73
76
  return (
74
- functools.partial(torch.amp.custom_fwd, device_type="cuda"),
75
- functools.partial(torch.amp.custom_bwd, device_type="cuda"),
77
+ functools.partial(torch.amp.custom_fwd, device_type=device),
78
+ functools.partial(torch.amp.custom_bwd, device_type=device),
76
79
  )
77
80
  return torch.cuda.amp.custom_fwd, torch.cuda.amp.custom_bwd
78
81
 
liger_kernel/utils.py ADDED
@@ -0,0 +1,13 @@
1
+ import torch
2
+
3
+
4
+ def infer_device():
5
+ """
6
+ Get current device name based on available devices
7
+ """
8
+ if torch.cuda.is_available():
9
+ return "cuda"
10
+ elif torch.xpu.is_available():
11
+ return "xpu"
12
+ else:
13
+ return "cpu"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: liger_kernel_nightly
3
- Version: 0.4.2.dev20241122052539
3
+ Version: 0.4.2.dev20241123040418
4
4
  Summary: Efficient Triton kernels for LLM Training
5
5
  License: BSD 2-CLAUSE LICENSE
6
6
  Copyright 2024 LinkedIn Corporation
@@ -1,4 +1,6 @@
1
+ liger_kernel/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
1
2
  liger_kernel/env_report.py,sha256=jye8RvUkmhqaIshdeIpoUABoAu7FPKJUib4FnAfvkpw,1132
3
+ liger_kernel/utils.py,sha256=HJa-xVKOohDn6pLVIx-Fv0V9h0QAL3qZGQNRICI-OpI,249
2
4
  liger_kernel/chunked_loss/__init__.py,sha256=R2wCcz4Y0kTAve926DH3k182XKezpXeACMHj05g9Mm8,346
3
5
  liger_kernel/chunked_loss/cpo_loss.py,sha256=H2L6mNtU8RMJ17u4aMZ9FHEfBvg1Z_hliY5-jZxiDBM,3079
4
6
  liger_kernel/chunked_loss/dpo_loss.py,sha256=XcCGLVmTVdEX30q41XRXXK_c-MSumVJ-l4tQwobUv2w,4228
@@ -7,19 +9,19 @@ liger_kernel/chunked_loss/fused_linear_preference.py,sha256=nkEpNWTHh5GmlnHOnGx5
7
9
  liger_kernel/chunked_loss/orpo_loss.py,sha256=DZ-_hm1twllBWujEV4M4-VDBkxMDBvoGqMGe-aGP1hA,3147
8
10
  liger_kernel/chunked_loss/simpo_loss.py,sha256=Jpl_U6DfxlzyHnlKN2i05K0vwz-ouiTmxlLGb439FwY,3328
9
11
  liger_kernel/ops/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- liger_kernel/ops/cross_entropy.py,sha256=sfUb7-jIZp0EKXjg1DYy2Wdzw_Mg-mHmGoR5bpdm4tw,15526
11
- liger_kernel/ops/fused_linear_cross_entropy.py,sha256=ib7M3AjJE164yMfuS9R39k-5qnDgYOXptIT146lqYbg,9964
12
+ liger_kernel/ops/cross_entropy.py,sha256=VqaYB9Zirc51eZ28OmjEZRrrV9UysRjS_vhIftB9sKo,15753
13
+ liger_kernel/ops/fused_linear_cross_entropy.py,sha256=Tnw4gyAYVVdnCOqhOuLEzbUQ3goOTnoAfk3pqSIM5ac,9301
12
14
  liger_kernel/ops/fused_linear_jsd.py,sha256=nOv4zwfxHqqepKEmMsQuz-B3H-gRjyo8uClpmqSGLYA,9693
13
15
  liger_kernel/ops/geglu.py,sha256=MQL4zyzneZqZYUGPvb1QjI_EYT9_pKfSDgR25WD9jrI,4127
14
16
  liger_kernel/ops/group_norm.py,sha256=VaRErVJGR4JqgXXvuIjNGTn3E2egjLtU1y3ymwIf4d8,10961
15
17
  liger_kernel/ops/jsd.py,sha256=Ap2b0_geCl6fqBXLI1IS6Yn6GlO-8LgPmnOW3y47dus,6151
16
18
  liger_kernel/ops/kl_div.py,sha256=03FNXfvCb6M-56hhFepAFV9p6brArPR6KOKkdGD34mw,8374
17
- liger_kernel/ops/layer_norm.py,sha256=unGMYMOPqtkM9aTrokhcqgPmsV2AUN7Yzv86isVB9OI,7422
19
+ liger_kernel/ops/layer_norm.py,sha256=_CZggw3GNEIUx5weDzadFit5I-Lzosoo8prgeJzcViY,7589
18
20
  liger_kernel/ops/qwen2vl_mrope.py,sha256=xZvQnhkSTjU-k6KiiRn9e0SYO1ESs1jmuZFMICduLpc,8552
19
- liger_kernel/ops/rms_norm.py,sha256=GKs49wXmUngY7MJ5QDQxTp4P2HDVqzZBaCr0pGtyZyM,11733
21
+ liger_kernel/ops/rms_norm.py,sha256=g7OXwuYI8-LXudDwvXuiupVjjOsbu8c4wwv83VaHa54,11750
20
22
  liger_kernel/ops/rope.py,sha256=jrzaA9-6Orn44y_IIam9_YNPQxOFK2FrIRNfFea4EtU,8513
21
23
  liger_kernel/ops/swiglu.py,sha256=Fwxtd76rhHKT9ShQAGca9RsnASplAVxtYKHmiT73_yA,2994
22
- liger_kernel/ops/utils.py,sha256=3JSF--O7KT5Wa5BuO70M4h0XetxoZ_e9IoW9GRlxlBg,3777
24
+ liger_kernel/ops/utils.py,sha256=_VQvd1PX5JXm5xaiBrk2gANp3qr4kM7qYG3ypkBwkMs,3850
23
25
  liger_kernel/ops/experimental/embedding.py,sha256=LYR66dB-jhvhtUjeV4PnNro-n77J1mdlmpSLSxB3Y6U,4186
24
26
  liger_kernel/ops/experimental/mm_int8int2.py,sha256=JpGVZCgRC6T8XMUJ_QbZRS2XU1bh0urIZphs5DTc1mY,13358
25
27
  liger_kernel/transformers/__init__.py,sha256=gia-eBxr7TLxU0GdDf8AfCY4WgDlFLqIGSt7EoQGsBA,1336
@@ -52,9 +54,9 @@ liger_kernel/transformers/model/qwen2.py,sha256=EyhSSzQOskGjSnCsKMZpd1s5IAIlHd5P
52
54
  liger_kernel/transformers/model/qwen2_vl.py,sha256=bIQe2bWiY--G84FhCD29Gdi64_qHP6vbcGsK6vKysQE,8547
53
55
  liger_kernel/triton/__init__.py,sha256=yfRe0zMb47QnqjecZWG7LnanfCTzeku7SgWRAwNVmzU,101
54
56
  liger_kernel/triton/monkey_patch.py,sha256=5BcGKTtdqeYchypBIBopGIWPx1-cFALz7sOKoEsqXJ0,1584
55
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
56
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/METADATA,sha256=4fPpoJL_3aqEC-teWIauvl_VbNer1TYZ-7s8znCLSgs,21891
57
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
58
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
59
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
60
- liger_kernel_nightly-0.4.2.dev20241122052539.dist-info/RECORD,,
57
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
58
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/METADATA,sha256=lXW5-kGkMAutfiUrZflzYeW1bZo8efp65MDohQ3G1T0,21891
59
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
60
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
61
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
62
+ liger_kernel_nightly-0.4.2.dev20241123040418.dist-info/RECORD,,