liger-kernel-nightly 0.5.9.dev20250519011716__py3-none-any.whl → 0.5.9.dev20250519025610__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.
@@ -0,0 +1,310 @@
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+
5
+
6
+ @triton.jit
7
+ def _selective_log_softmax_kernel(
8
+ LOGITS,
9
+ INPUT_IDS,
10
+ LOG_P,
11
+ MASK,
12
+ TEMPERATURE,
13
+ stride_input_ids_b,
14
+ L: tl.constexpr,
15
+ N: tl.constexpr,
16
+ BLOCK_N: tl.constexpr = 4096,
17
+ ):
18
+ off_b = tl.program_id(0).cast(tl.int64)
19
+ off_l = tl.program_id(1).cast(tl.int64)
20
+
21
+ LOGITS += off_b * (L + 1) * N + off_l * N
22
+ INPUT_IDS += off_b * stride_input_ids_b + off_l
23
+ LOG_P += off_b * L + off_l
24
+
25
+ if MASK is not None:
26
+ MASK += off_b * stride_input_ids_b + off_l
27
+ not_skip = tl.load(MASK)
28
+ if not_skip == 0:
29
+ return
30
+
31
+ m_i = float("-inf")
32
+ l_i = 0.0
33
+ for start in range(0, N, BLOCK_N):
34
+ cols = start + tl.arange(0, BLOCK_N)
35
+ logits = tl.load(LOGITS + cols, mask=cols < N, other=float("-inf")).to(tl.float32) / TEMPERATURE
36
+ new_m_i = tl.maximum(m_i, tl.max(logits))
37
+ alpha = tl.exp(m_i - new_m_i)
38
+ l_i = l_i * alpha + tl.sum(tl.exp(logits - new_m_i))
39
+ m_i = new_m_i
40
+ lse = m_i + tl.log(l_i)
41
+
42
+ ids = tl.load(INPUT_IDS)
43
+ x = tl.load(LOGITS + ids).to(tl.float32) / TEMPERATURE
44
+ logp = x - lse
45
+ tl.store(LOG_P, logp)
46
+
47
+
48
+ # compue old_logp and ref_logp, it reduce 10G peak Memory. it does not requires grad
49
+ @torch.no_grad
50
+ def fused_selective_log_softmax(logits: torch.Tensor, input_ids: torch.Tensor, temperature: float = 0.9, mask=None):
51
+ assert logits.is_contiguous()
52
+ B, L_ADD_1, N = logits.shape
53
+ L = L_ADD_1 - 1
54
+ input_ids = input_ids[:, -L:]
55
+ if mask is not None:
56
+ mask = mask[:, -L:]
57
+ log_p = torch.zeros(B, L, dtype=torch.float32, device=logits.device)
58
+ kwargs = {"BLOCK_N": 2048, "num_stages": 4, "num_warps": 1}
59
+ _selective_log_softmax_kernel[(B, L)](
60
+ logits, input_ids, log_p, mask, temperature, input_ids.stride(0), L, N, **kwargs
61
+ )
62
+ return log_p
63
+
64
+
65
+ # @triton.autotune([triton.Config({"BLOCK_N":BLOCK_N}, num_stages=ns, num_warps=nw)
66
+ # for BLOCK_N in [2048, 4096, 8192]
67
+ # for ns in [1, 2, 4]
68
+ # for nw in [1, 2, 4, 8, 16]],
69
+ # key=['N'])
70
+ @triton.jit
71
+ def _grpo_loss_fwd_kernel(
72
+ LOGITS,
73
+ OLD_LOGP,
74
+ REF_LOGP,
75
+ INPUT_IDS,
76
+ COMPLETION_MASK,
77
+ ADVANTAGES,
78
+ LOSS,
79
+ LSE,
80
+ KL,
81
+ IS_CLIPPED,
82
+ TEMPERATURE,
83
+ BETA: tl.constexpr,
84
+ EPS_LOW,
85
+ EPS_HIGH,
86
+ L: tl.constexpr,
87
+ N: tl.constexpr,
88
+ BLOCK_N: tl.constexpr = 4096,
89
+ ):
90
+ off_b = tl.program_id(0).cast(tl.int64)
91
+ off_l = tl.program_id(1).cast(tl.int64)
92
+
93
+ if COMPLETION_MASK is not None:
94
+ COMPLETION_MASK += off_b * L + off_l
95
+ not_skip = tl.load(COMPLETION_MASK)
96
+ if not_skip == 0:
97
+ return
98
+
99
+ LOGITS += off_b * (L + 1) * N + off_l * N
100
+ INPUT_IDS += off_b * L + off_l
101
+ ADVANTAGES += off_b
102
+ LOSS += off_b * L + off_l
103
+ LSE += off_b * L + off_l
104
+ IS_CLIPPED += off_b * L + off_l
105
+
106
+ m_i = float("-inf")
107
+ l_i = 0.0
108
+ for start in range(0, N, BLOCK_N):
109
+ cols = start + tl.arange(0, BLOCK_N)
110
+ logits = tl.load(LOGITS + cols, mask=cols < N, other=float("-inf")).to(tl.float32) / TEMPERATURE
111
+ new_m_i = tl.maximum(m_i, tl.max(logits))
112
+ alpha = tl.exp(m_i - new_m_i)
113
+ l_i = l_i * alpha + tl.sum(tl.exp(logits - new_m_i))
114
+ m_i = new_m_i
115
+ lse = m_i + tl.log(l_i)
116
+
117
+ idx = tl.load(INPUT_IDS)
118
+ x = tl.load(LOGITS + idx).to(tl.float32) / TEMPERATURE
119
+ logp = x - lse
120
+ if OLD_LOGP is None:
121
+ old_logp = logp
122
+ else:
123
+ OLD_LOGP += off_b * L + off_l
124
+ old_logp = tl.load(OLD_LOGP).to(tl.float32)
125
+ coef_1 = tl.exp(logp - old_logp)
126
+ coef_2 = tl.clamp(coef_1, 1 - EPS_LOW, 1 + EPS_HIGH)
127
+ advantage = tl.load(ADVANTAGES).to(tl.float32)
128
+ per_token_loss1 = coef_1 * advantage
129
+ per_token_loss2 = coef_2 * advantage
130
+ per_token_loss = -tl.minimum(per_token_loss1, per_token_loss2)
131
+ is_clipped = per_token_loss1 < per_token_loss2
132
+
133
+ if BETA != 0.0:
134
+ REF_LOGP += off_b * L + off_l
135
+ KL += off_b * L + off_l
136
+ ref_logp = tl.load(REF_LOGP).to(tl.float32)
137
+ kl = tl.exp(ref_logp - logp) - (ref_logp - logp) - 1
138
+ per_token_loss += BETA * kl
139
+ tl.store(KL, kl)
140
+
141
+ tl.store(LOSS, per_token_loss)
142
+ tl.store(LSE, lse)
143
+ tl.store(IS_CLIPPED, is_clipped)
144
+
145
+
146
+ # @triton.autotune([triton.Config({"BLOCK_N":BLOCK_N}, num_stages=ns, num_warps=nw)
147
+ # for BLOCK_N in [2048, 4096, 8192]
148
+ # for ns in [1, 2, 4]
149
+ # for nw in [1, 2, 4, 8, 16]],
150
+ # key=['N'])
151
+ @triton.jit
152
+ def _grpo_loss_bwd_kernel(
153
+ DLOSS,
154
+ DLOGITS,
155
+ LOGITS,
156
+ OLD_LOGP,
157
+ REF_LOGP,
158
+ INPUT_IDS,
159
+ ADVANTAGES,
160
+ COMPLETION_MASK,
161
+ LSE,
162
+ TEMPERATURE,
163
+ BETA: tl.constexpr,
164
+ EPS_LOW,
165
+ EPS_HIGH,
166
+ loss_stride0,
167
+ loss_stride1,
168
+ L: tl.constexpr,
169
+ N: tl.constexpr,
170
+ BLOCK_N: tl.constexpr = 4096,
171
+ ):
172
+ off_b = tl.program_id(0).cast(tl.int64)
173
+ off_l = tl.program_id(1).cast(tl.int64)
174
+
175
+ DLOGITS += off_b * (L + 1) * N + off_l * N
176
+ if COMPLETION_MASK is not None:
177
+ COMPLETION_MASK += off_b * L + off_l
178
+ not_skip = tl.load(COMPLETION_MASK)
179
+ if not_skip == 0:
180
+ for start in range(0, N, BLOCK_N):
181
+ cols = tl.arange(0, BLOCK_N) + start
182
+ tl.store(DLOGITS + cols, 0.0, mask=cols < N)
183
+ return
184
+
185
+ LOGITS += off_b * (L + 1) * N + off_l * N
186
+ DLOSS += off_b * loss_stride0 + off_l * loss_stride1
187
+ INPUT_IDS += off_b * L + off_l
188
+ ADVANTAGES += off_b
189
+ LSE += off_b * L + off_l
190
+
191
+ dloss = tl.load(DLOSS).to(tl.float32)
192
+ lse = tl.load(LSE).to(tl.float32)
193
+
194
+ idx = tl.load(INPUT_IDS)
195
+ x = tl.load(LOGITS + idx).to(tl.float32) / TEMPERATURE
196
+ logp = x - lse
197
+ if OLD_LOGP is None:
198
+ old_logp = logp
199
+ else:
200
+ OLD_LOGP += off_b * L + off_l
201
+ old_logp = tl.load(OLD_LOGP).to(tl.float32)
202
+ coef_1 = tl.exp(logp - old_logp)
203
+ coef_2 = tl.clamp(coef_1, 1 - EPS_LOW, 1 + EPS_HIGH)
204
+ advantage = tl.load(ADVANTAGES).to(tl.float32)
205
+ per_token_loss1 = coef_1 * advantage
206
+ per_token_loss2 = coef_2 * advantage
207
+ mask = per_token_loss2 >= per_token_loss1
208
+
209
+ dlogp = -per_token_loss1 * mask
210
+ if BETA != 0.0:
211
+ REF_LOGP += off_b * L + off_l
212
+ ref_logp = tl.load(REF_LOGP).to(tl.float32)
213
+ dlogp += BETA * (1 - tl.exp(ref_logp - logp))
214
+
215
+ dlogp = dlogp * dloss / TEMPERATURE
216
+ tl.debug_barrier()
217
+ for start_n in tl.range(0, N, BLOCK_N):
218
+ cols = start_n + tl.arange(0, BLOCK_N)
219
+ logits = tl.load(LOGITS + cols, mask=cols < N, other=-float("inf")).to(tl.float32) / TEMPERATURE
220
+ probs = tl.exp(logits - lse)
221
+ dlogits = tl.where(cols == idx, 1 - probs, -probs) * dlogp
222
+ tl.store(DLOGITS + cols, dlogits, mask=cols < N)
223
+
224
+
225
+ class GrpoLossFunction(torch.autograd.Function):
226
+ @staticmethod
227
+ def forward(
228
+ ctx,
229
+ logits,
230
+ old_logp,
231
+ ref_logp,
232
+ completion_ids,
233
+ advantages,
234
+ completion_mask,
235
+ temperature,
236
+ beta,
237
+ eps_low,
238
+ eps_high,
239
+ inplace,
240
+ ):
241
+ assert logits.is_contiguous() and completion_ids.is_contiguous()
242
+ assert old_logp is None or old_logp.is_contiguous()
243
+ assert (ref_logp is not None and ref_logp.is_contiguous()) if beta != 0.0 else True
244
+
245
+ B, L_ADD_1, N = logits.shape
246
+ L = L_ADD_1 - 1
247
+
248
+ if completion_mask is not None:
249
+ assert completion_mask.is_contiguous()
250
+
251
+ loss = torch.zeros(B, L, device=logits.device, dtype=torch.float32)
252
+ lse = torch.zeros_like(loss)
253
+ is_clipped = torch.zeros_like(loss)
254
+ kl = torch.zeros_like(loss) if beta != 0.0 else None
255
+ kwargs = {"BLOCK_N": 2048, "num_stages": 2, "num_warps": 1}
256
+ _grpo_loss_fwd_kernel[(B, L)](
257
+ logits,
258
+ old_logp,
259
+ ref_logp,
260
+ completion_ids,
261
+ completion_mask,
262
+ advantages,
263
+ loss,
264
+ lse,
265
+ kl,
266
+ is_clipped,
267
+ temperature,
268
+ beta,
269
+ eps_low,
270
+ eps_high,
271
+ L,
272
+ N,
273
+ **kwargs,
274
+ )
275
+ ctx.save_for_backward(logits, old_logp, ref_logp, completion_ids, advantages, completion_mask, lse)
276
+ ctx.infos = (temperature, beta, eps_low, eps_high, inplace)
277
+ # return loss
278
+ return loss, kl, is_clipped
279
+
280
+ @staticmethod
281
+ def backward(ctx, *args):
282
+ dloss = args[0]
283
+ # print(dloss.shape)
284
+ logits, old_logp, ref_logp, completion_ids, advantages, completion_mask, lse = ctx.saved_tensors
285
+ temperature, beta, eps_low, eps_high, inplace = ctx.infos
286
+ B, L_ADD_1, N = logits.shape
287
+ L = L_ADD_1 - 1
288
+ dlogits = logits.data if inplace else torch.empty_like(logits)
289
+ kwargs = {"BLOCK_N": 4096, "num_stages": 1, "num_warps": 16}
290
+ _grpo_loss_bwd_kernel[(B, L)](
291
+ dloss,
292
+ dlogits,
293
+ logits,
294
+ old_logp,
295
+ ref_logp,
296
+ completion_ids,
297
+ advantages,
298
+ completion_mask,
299
+ lse,
300
+ temperature,
301
+ beta,
302
+ eps_low,
303
+ eps_high,
304
+ *dloss.stride(),
305
+ L,
306
+ N,
307
+ **kwargs,
308
+ )
309
+ dlogits[:, -1, :] = 0
310
+ return dlogits, None, None, None, None, None, None, None, None, None, None
@@ -0,0 +1,55 @@
1
+ from typing import Any
2
+ from typing import Callable
3
+
4
+ from torch.distributed.fsdp import FullyShardedDataParallel
5
+
6
+
7
+ class _FSDPForwardRedirection:
8
+ """
9
+ Modified based on
10
+ https://github.com/Lightning-AI/pytorch-lightning/blob/d3f9c83d6efa4f1def36aa6c199600946cdb9117/src/lightning/pytorch/strategies/strategy.py#L601-L648
11
+ Redirect a method call through FullyShardedDataParallel.forward so that the FSDP module's root pre-forward and
12
+ post-forward can be properly executed around the method call.
13
+ This is needed in cases where we call a submodule of a FSDP module. For instance, when we want to call only
14
+ the `LlamaModel` part out of a FSDP-wrapped `LlamaForCausalLM` to get the hidden states without involving
15
+ GPU-memory-heavy `lm_head` and cross entropy computation, doing this directly (i.e. `model.model.forward()`)
16
+ will not work because the first `nn.Embedding` layer is not independently wrapped as a FSDP module (because of
17
+ the transformer-based wrapping policy), and not calling it through FSDP root module forward will not all-gather
18
+ its parameter, thus resulting in "RuntimeError: 'weight' must be 2-D" error. Similarly, if we want to call just
19
+ the `lm_head` part of a model, we need this trick too to properly get its params all-gathered.
20
+ """
21
+
22
+ def __call__(
23
+ self,
24
+ wrapper_module: FullyShardedDataParallel,
25
+ method: Callable,
26
+ *args: Any,
27
+ **kwargs: Any,
28
+ ):
29
+ """Reroutes a method call through the `wrapper_module`'s `forward` method.
30
+ Args:
31
+ wrapper_module: The module that has `original_module` wrapped.
32
+ original_module: The module that was wrapped inside `wrapper_module`.
33
+ method_name: The name of the method that should be called on the `original_module` after inputs get
34
+ redirected through the `wrapper_module`'s `forward` method.
35
+ *args: The positional arguments to the method `method_name`. They will get passed to a patched
36
+ `forward` method instead.
37
+ **kwargs: The keyword arguments to the method `method_name`. They will get passed to a patched
38
+ `forward` method instead.
39
+ """
40
+ assert isinstance(wrapper_module, FullyShardedDataParallel)
41
+ original_module = wrapper_module._fsdp_wrapped_module
42
+ original_forward = original_module.forward
43
+
44
+ def wrapped_forward(*_args: Any, **_kwargs: Any) -> Any:
45
+ # Unpatch ourselves immediately before calling the method `method_name`
46
+ # because itself may want to call the real `forward`
47
+ original_module.forward = original_forward # type: ignore[method-assign]
48
+ # Call the actual method e.g. `.training_step(...)`
49
+ out = method(*_args, **_kwargs)
50
+ return out
51
+
52
+ # Patch the original_module's forward so we can redirect the arguments back to the real method
53
+ original_module.forward = wrapped_forward # type: ignore[method-assign]
54
+ wrapper_output = wrapper_module(*args, **kwargs)
55
+ return wrapper_output
@@ -0,0 +1,98 @@
1
+ from liger_kernel.ops.grpo_loss import GrpoLossFunction
2
+
3
+
4
+ def triton_grpo_loss(
5
+ logits,
6
+ old_logp,
7
+ ref_logp,
8
+ completion_ids,
9
+ advantages,
10
+ completion_mask=None,
11
+ temperature=0.9,
12
+ beta=0.04,
13
+ eps_low=0.2,
14
+ eps_high=0.4,
15
+ inplace=True,
16
+ ):
17
+ assert logits is not None and completion_ids is not None and advantages is not None, (
18
+ "must provide logits、completion_ids and advantages"
19
+ )
20
+
21
+ return GrpoLossFunction.apply(
22
+ logits,
23
+ old_logp,
24
+ ref_logp,
25
+ completion_ids,
26
+ advantages,
27
+ completion_mask,
28
+ temperature,
29
+ beta,
30
+ eps_low,
31
+ eps_high,
32
+ inplace,
33
+ )
34
+
35
+
36
+ # This is a demo how to use grpo_loss in GRPOTrainer. The Trl version must be 0.16
37
+ """
38
+ import torch
39
+ import trl
40
+ assert trl.__version__.startswith("0.16"), "please pip install trl==0.16"
41
+ from trl.extras.profiling import profiling_decorator
42
+
43
+ @profiling_decorator
44
+ def _get_per_token_logps(self, model, input_ids, attention_mask, logits_to_keep):
45
+ # We add 1 to `logits_to_keep` because the last logits of the sequence is later excluded
46
+ logits = model(input_ids=input_ids, attention_mask=attention_mask, logits_to_keep=logits_to_keep + 1).logits
47
+ return fused_selective_log_softmax(logits, input_ids, self.temperature, mask=attention_mask)
48
+
49
+ @profiling_decorator
50
+ def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
51
+ if return_outputs:
52
+ raise ValueError("The GRPOTrainer does not support returning outputs")
53
+ # Compute the per-token log probabilities for the model
54
+
55
+ prompt_ids, prompt_mask = inputs["prompt_ids"], inputs["prompt_mask"]
56
+ completion_ids, completion_mask = inputs["completion_ids"], inputs["completion_mask"]
57
+ input_ids = torch.cat([prompt_ids, completion_ids], dim=1)
58
+ attention_mask = torch.cat([prompt_mask, completion_mask], dim=1)
59
+ logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens
60
+ logits = model(input_ids=input_ids, attention_mask=attention_mask, logits_to_keep=logits_to_keep + 1).logits
61
+
62
+ ref_per_token_logps = inputs["ref_per_token_logps"]
63
+ advantages = inputs["advantages"]
64
+ old_per_token_logps = inputs["old_per_token_logps"]
65
+
66
+
67
+ per_token_loss, per_token_kl, is_clipped = triton_grpo_loss(logits,
68
+ old_per_token_logps,
69
+ ref_per_token_logps,
70
+ completion_ids,
71
+ advantages,
72
+ completion_mask,
73
+ self.temperature,
74
+ self.beta,
75
+ self.epsilon_low,
76
+ self.epsilon_high,)
77
+ loss = (per_token_loss * completion_mask).sum() / completion_mask.sum()
78
+
79
+ # Log the metrics
80
+ mode = "eval" if self.control.should_evaluate else "train"
81
+
82
+ if self.beta != 0.0:
83
+ mean_kl = (per_token_kl * completion_mask).sum() / completion_mask.sum()
84
+ self._metrics[mode]["kl"].append(self.accelerator.gather_for_metrics(mean_kl).mean().item())
85
+
86
+ clip_ratio = (is_clipped * completion_mask).sum() / completion_mask.sum()
87
+ self._metrics[mode]["clip_ratio"].append(self.accelerator.gather_for_metrics(clip_ratio).mean().item())
88
+ return loss
89
+
90
+ trl.GRPOTrainer._get_per_token_logps = _get_per_token_logps
91
+ trl.GRPOTrainer.compute_loss = compute_loss
92
+ trigger = None
93
+ """
94
+
95
+ # add this line at the first line of grpo.py in open-r1
96
+ """
97
+ from liger_kernel.transformers.grpo_loss import trigger
98
+ """
@@ -7,16 +7,22 @@ from typing import Union
7
7
  import torch
8
8
  import torch.nn.functional as F
9
9
 
10
+ from torch.distributed.fsdp import FullyShardedDataParallel
10
11
  from torch.nn import CrossEntropyLoss
11
12
  from transformers.modeling_outputs import CausalLMOutputWithPast
12
13
  from transformers.utils.deprecation import deprecate_kwarg
13
14
 
15
+ from liger_kernel.transformers.fsdp import _FSDPForwardRedirection
14
16
  from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
15
17
  from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
18
+ from liger_kernel.utils import PEFT_AVAILABLE
16
19
 
17
20
  if TYPE_CHECKING:
18
21
  from transformers.cache_utils import Cache
19
22
 
23
+ if PEFT_AVAILABLE:
24
+ from peft.utils.other import ModulesToSaveWrapper
25
+
20
26
 
21
27
  def lce_forward_deprecated(
22
28
  self,
@@ -213,12 +219,12 @@ def lce_forward(
213
219
  loss = None
214
220
  # if in training mode, don't materialize logits
215
221
  if self.training and (labels is not None or shift_labels is not None):
216
- loss = LigerForCausalLMLoss(
222
+ loss = lce_maybe_trainable_lm_head(
223
+ self,
217
224
  hidden_states=kept_hidden_states,
218
- lm_head_weight=self.lm_head.weight,
225
+ hidden_size=self.config.hidden_size,
219
226
  labels=labels,
220
227
  shift_labels=shift_labels,
221
- hidden_size=self.config.hidden_size,
222
228
  **loss_kwargs,
223
229
  )
224
230
 
@@ -243,3 +249,50 @@ def lce_forward(
243
249
  hidden_states=outputs.hidden_states,
244
250
  attentions=outputs.attentions,
245
251
  )
252
+
253
+
254
+ def lce_maybe_trainable_lm_head(self, hidden_states, hidden_size, labels, shift_labels, **loss_kwargs):
255
+ lm_head = self.lm_head
256
+
257
+ # Unwrap the module if lm_head has been added as trainable module in PEFT LoRA configuration,
258
+ # i.e. listed in the modules_to_save field of LoraConfig, so the lm_head weights are read
259
+ # from the unwrapped module.
260
+ # See https://huggingface.co/docs/peft/package_reference/lora for reference.
261
+ if PEFT_AVAILABLE and isinstance(lm_head, ModulesToSaveWrapper):
262
+ lm_head = lm_head.modules_to_save.default
263
+
264
+ # If FSDP is used and lm_head is trainable, e.g., during full fine-tuning or with LoRA,
265
+ # reading the lm_head module weights and calling the kernel must be done within FSDP forward pass
266
+ # so the module entire parameters are summoned and kept in memory during the kernel execution.
267
+ if isinstance(lm_head, FullyShardedDataParallel):
268
+ return _FSDPForwardRedirection()(
269
+ lm_head,
270
+ _liger_for_causal_lm_loss,
271
+ lm_head.module,
272
+ hidden_states,
273
+ hidden_size,
274
+ labels,
275
+ shift_labels,
276
+ **loss_kwargs,
277
+ )
278
+
279
+ # FSDP is not used so we can read the lm_head weights and call the kernel directly
280
+ return _liger_for_causal_lm_loss(
281
+ lm_head=self.lm_head,
282
+ hidden_states=hidden_states,
283
+ hidden_size=hidden_size,
284
+ labels=labels,
285
+ shift_labels=shift_labels,
286
+ **loss_kwargs,
287
+ )
288
+
289
+
290
+ def _liger_for_causal_lm_loss(lm_head, hidden_states, hidden_size, labels, shift_labels, **loss_kwargs):
291
+ return LigerForCausalLMLoss(
292
+ hidden_states=hidden_states,
293
+ lm_head_weight=lm_head.weight,
294
+ labels=labels,
295
+ hidden_size=hidden_size,
296
+ shift_labels=shift_labels,
297
+ **loss_kwargs,
298
+ )
@@ -1,5 +1,3 @@
1
- from typing import Any
2
- from typing import Callable
3
1
  from typing import Dict
4
2
  from typing import List
5
3
  from typing import Literal
@@ -13,57 +11,7 @@ from torch.distributed.fsdp import FullyShardedDataParallel
13
11
  from trl.trainer import ORPOTrainer
14
12
 
15
13
  from liger_kernel.chunked_loss import LigerFusedLinearORPOLoss
16
-
17
-
18
- class _FSDPForwardRedirection:
19
- """
20
- Modified based on
21
- https://github.com/Lightning-AI/pytorch-lightning/blob/d3f9c83d6efa4f1def36aa6c199600946cdb9117/src/lightning/pytorch/strategies/strategy.py#L601-L648
22
- Redirect a method call through FullyShardedDataParallel.forward so that the FSDP module's root pre-forward and
23
- post-forward can be properly executed around the method call.
24
- This is needed in cases where we call a submodule of a FSDP module. For instance, when we want to call only
25
- the `LlamaModel` part out of a FSDP-wrapped `LlamaForCausalLM` to get the hidden states without involving
26
- GPU-memory-heavy `lm_head` and cross entropy computation, doing this directly (i.e. `model.model.forward()`)
27
- will not work because the first `nn.Embedding` layer is not independently wrapped as a FSDP module (because of
28
- the transformer-based wrapping policy), and not calling it through FSDP root module forward will not all-gather
29
- its parameter, thus resulting in "RuntimeError: 'weight' must be 2-D" error. Similarly, if we want to call just
30
- the `lm_head` part of a model, we need this trick too to properly get its params all-gathered.
31
- """
32
-
33
- def __call__(
34
- self,
35
- wrapper_module: FullyShardedDataParallel,
36
- method: Callable,
37
- *args: Any,
38
- **kwargs: Any,
39
- ):
40
- """Reroutes a method call through the `wrapper_module`'s `forward` method.
41
- Args:
42
- wrapper_module: The module that has `original_module` wrapped.
43
- original_module: The module that was wrapped inside `wrapper_module`.
44
- method_name: The name of the method that should be called on the `original_module` after inputs get
45
- redirected through the `wrapper_module`'s `forward` method.
46
- *args: The positional arguments to the method `method_name`. They will get passed to a patched
47
- `forward` method instead.
48
- **kwargs: The keyword arguments to the method `method_name`. They will get passed to a patched
49
- `forward` method instead.
50
- """
51
- assert isinstance(wrapper_module, FullyShardedDataParallel)
52
- original_module = wrapper_module._fsdp_wrapped_module
53
- original_forward = original_module.forward
54
-
55
- def wrapped_forward(*_args: Any, **_kwargs: Any) -> Any:
56
- # Unpatch ourselves immediately before calling the method `method_name`
57
- # because itself may want to call the real `forward`
58
- original_module.forward = original_forward # type: ignore[method-assign]
59
- # Call the actual method e.g. `.training_step(...)`
60
- out = method(*_args, **_kwargs)
61
- return out
62
-
63
- # Patch the original_module's forward so we can redirect the arguments back to the real method
64
- original_module.forward = wrapped_forward # type: ignore[method-assign]
65
- wrapper_output = wrapper_module(*args, **kwargs)
66
- return wrapper_output
14
+ from liger_kernel.transformers.fsdp import _FSDPForwardRedirection
67
15
 
68
16
 
69
17
  class LigerORPOTrainer(ORPOTrainer):
liger_kernel/utils.py CHANGED
@@ -1,6 +1,17 @@
1
+ try:
2
+ import peft # noqa: F401
3
+
4
+ PEFT_AVAILABLE = True
5
+ except ImportError:
6
+ PEFT_AVAILABLE = False
7
+
1
8
  import torch
2
9
 
3
10
 
11
+ def is_peft_available():
12
+ return PEFT_AVAILABLE
13
+
14
+
4
15
  def infer_device():
5
16
  """
6
17
  Get current device name based on available devices
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: liger_kernel_nightly
3
- Version: 0.5.9.dev20250519011716
3
+ Version: 0.5.9.dev20250519025610
4
4
  Summary: Efficient Triton kernels for LLM Training
5
5
  License: BSD 2-CLAUSE LICENSE
6
6
  Copyright 2024 LinkedIn Corporation
@@ -1,6 +1,6 @@
1
1
  liger_kernel/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  liger_kernel/env_report.py,sha256=uhdEC8OydxoZlb7B6YYcAaBF3crGFdIck-4cxaW4NJY,1728
3
- liger_kernel/utils.py,sha256=178Hn8uD-VauDT6FjqMyXLbKLod8ObIpaTtapHwfEK0,1861
3
+ liger_kernel/utils.py,sha256=BQleeZWHSZPNuPcYcoZTOp1kcNEZONZilPP5-AmjgWI,2024
4
4
  liger_kernel/chunked_loss/README.md,sha256=0FmkFC3hKBqyoDT5uTlIYmrvRkF-EOCR1y-EBU1LpWU,2248
5
5
  liger_kernel/chunked_loss/__init__.py,sha256=ATu-xX5Fc49Cr6yBOGBRNTo593ZrU5ZCsIuvoIbJWw4,603
6
6
  liger_kernel/chunked_loss/cpo_loss.py,sha256=Gzz1eU4kgcbdubFVRy55e8A1Cr-r45UgNicXwZIjmBU,5454
@@ -22,6 +22,7 @@ liger_kernel/ops/fused_linear_cross_entropy.py,sha256=5fbGhN85n3zf0uIdJ7PYHWIRzT
22
22
  liger_kernel/ops/fused_linear_jsd.py,sha256=CSoprxb-YcJy-YUKiTcYkxN8sb9h2kdk_iHuncvSV5c,9683
23
23
  liger_kernel/ops/geglu.py,sha256=axGvCIvlBzuluoAIrWTsp2iZM4BFKNInkPov8YVvH9E,4126
24
24
  liger_kernel/ops/group_norm.py,sha256=qD4D4lSjSgVtO52EBNLC2iTseALRgPgqXE50U2woggk,10837
25
+ liger_kernel/ops/grpo_loss.py,sha256=anRnv7k1-AV3pCC6_TqP0GMg78YYUfRAJrbpx6PVhl0,9448
25
26
  liger_kernel/ops/jsd.py,sha256=onHp5T3MbvJaVz5Vup7Ww6EQp_HTaZeayTjJk6FgQMY,7042
26
27
  liger_kernel/ops/kl_div.py,sha256=ZjGdDLKWksHT9dZ0xF_TDgAkj5cuMTwwT5tr9E-_24o,8734
27
28
  liger_kernel/ops/layer_norm.py,sha256=vWCyOm-F2GMAilB-ozJcFeUQQLCJoTE_uiXq-_0uYuI,8356
@@ -38,12 +39,14 @@ liger_kernel/transformers/__init__.py,sha256=0KX0rxyy0E_uNWVE0PSTzEVzKqc5KdFHtvd
38
39
  liger_kernel/transformers/auto_model.py,sha256=0qCTRZt280Bj_LcFdzo9hlaR-BWNazawXOGgoCZjgEg,1545
39
40
  liger_kernel/transformers/cross_entropy.py,sha256=z3KTWQnFxr_IZaVjtYt0ZNEWQdDdYThN35xWkHlDGH0,1683
40
41
  liger_kernel/transformers/dyt.py,sha256=i-4GPaMrl-jab9TVI5qN0-H9qycn_mCbV82ozU4nbmU,723
42
+ liger_kernel/transformers/fsdp.py,sha256=CUiyjTmjkjY7pLXQv8ly9rnzgXw6529csd9pvtJNMYc,3096
41
43
  liger_kernel/transformers/functional.py,sha256=2YBfvtdU1GRZuRpJhHgJXeGYa1RvmO6-qQvrKQrLJK4,5259
42
44
  liger_kernel/transformers/fused_linear_cross_entropy.py,sha256=O8Sg5BT81nTaY9fSGoOY9dOD9ekibwwiuXhdUHaxntQ,1742
43
45
  liger_kernel/transformers/fused_linear_jsd.py,sha256=bZ4otCvWBuOnA5XdQL-FzZVItJlDt-ht9e_pG7PG93E,3999
44
46
  liger_kernel/transformers/geglu.py,sha256=mrgqzIUVd6lN7fkDKLkw5YaESDxDtFgbot430WwPVOQ,1107
45
47
  liger_kernel/transformers/gema3_rms.py,sha256=LTmZOXe6WEnv6ZroW-kU1TE2B36-z5v8OLmKr3XEVFo,353
46
48
  liger_kernel/transformers/group_norm.py,sha256=6qMAWOprr4SzP0YhNVNGQIBpM5aUHplUD2VuGJrMBz0,2173
49
+ liger_kernel/transformers/grpo_loss.py,sha256=uAkUNKSnUGEOqa82L9w2e6AI1kcmG8K45-QxyaT8zhM,3897
47
50
  liger_kernel/transformers/jsd.py,sha256=DGqRnxIZxsvxo0_tbbxX3b-sDbDjC_yKufyRIHCcScY,2979
48
51
  liger_kernel/transformers/kl_div.py,sha256=WLffFbh1EExD2Eb1F7lN11fo9JJC-0751WJjZAF1Fj8,409
49
52
  liger_kernel/transformers/layer_norm.py,sha256=c9pk3PEasOKYR0rhe5e5nNrnYKVCEW4VC8S6LpCq9EQ,906
@@ -61,7 +64,7 @@ liger_kernel/transformers/model/gemma.py,sha256=gi5fVeFPryoYy0_T3rzU2wm7v_xiJnLC
61
64
  liger_kernel/transformers/model/gemma2.py,sha256=61uH9JSZM6cPDoGHr2kNUVq2O4A3XIy2Qea36XhkkPQ,10761
62
65
  liger_kernel/transformers/model/gemma3.py,sha256=e-o7rcOJAJMZDJBB-blkLz5ildWjuDneSkakqwrADBc,15630
63
66
  liger_kernel/transformers/model/glm4.py,sha256=yYbQEcSrSTMleNTpwJosMhBf4VC9-79EyC__utmOSFg,5031
64
- liger_kernel/transformers/model/llama.py,sha256=pkkoKip94p3hNWA11cIVvTdNqCRB8FgR039pZWLqNeA,10181
67
+ liger_kernel/transformers/model/llama.py,sha256=ALVgzpD_YRYE7-6npb0KkjSBwrhCsgk_4lbaymOyRVw,12226
65
68
  liger_kernel/transformers/model/llava.py,sha256=RjLVnpHtOClc1jJkkPSqke7fcgWC3Jjh1rrGyvh5kb8,17008
66
69
  liger_kernel/transformers/model/loss_utils.py,sha256=WWAMdiONPaXpIvxyOim_0igLrYh0yyOok5Q9_L9xvZw,1787
67
70
  liger_kernel/transformers/model/mistral.py,sha256=0lt1Jq37zWjxLZF-Vuj9jUyIEnWlMuT7PB5xB42KXBs,5313
@@ -76,12 +79,12 @@ liger_kernel/transformers/model/qwen2_vl.py,sha256=q3AMpxFfwHjaMu9Q3jpwpMPRzrE-e
76
79
  liger_kernel/transformers/model/qwen3.py,sha256=u_0cCRwr1jcwMkSknbBVb9my1OepCGU718uxKhNUOVM,4657
77
80
  liger_kernel/transformers/model/qwen3_moe.py,sha256=lIWGunVtNP-d7VfRvEGY820howzecb10g6ZeWRgsfl8,5463
78
81
  liger_kernel/transformers/trainer/__init__.py,sha256=p7yQfklV8-467qSz_ZMimkbDF7HHWHwku25A-GYL0WU,193
79
- liger_kernel/transformers/trainer/orpo_trainer.py,sha256=pdekW7l6Qg_aqa5SYKYlSWUF8m3lkOFvFLcIMEHrz9s,8338
82
+ liger_kernel/transformers/trainer/orpo_trainer.py,sha256=tX0h63aOFe3rNqTmk6JpMf75UPo981yzEa6TghnjS0Q,5370
80
83
  liger_kernel/triton/__init__.py,sha256=qCiCamzCRv6lpV8IqpAc9YMdNKC7GKurClWceQPnlis,92
81
84
  liger_kernel/triton/monkey_patch.py,sha256=Rd0hUHAzDkFfHvnX7-PBaNK5EKnZhtfM_h-fgQH9HPY,1568
82
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
83
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/METADATA,sha256=JJ5XcqsRjwW1nB2hH580FLzHY9i3mC_aEZj9mDNX6Gg,23970
84
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
85
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
86
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
87
- liger_kernel_nightly-0.5.9.dev20250519011716.dist-info/RECORD,,
85
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
86
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/METADATA,sha256=y96ZmoWt54lwSvXqmZylo4V_wUHZ2dD2Xb29tV0jvLA,23970
87
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
88
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
89
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
90
+ liger_kernel_nightly-0.5.9.dev20250519025610.dist-info/RECORD,,