liger-kernel-nightly 0.6.1.dev20250809233505__py3-none-any.whl → 0.6.1.dev20250812205818__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/transformers/model/loss_utils.py +2 -0
- liger_kernel/transformers/model/phi3.py +8 -145
- liger_kernel/transformers/monkey_patch.py +6 -18
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/METADATA +1 -1
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/RECORD +9 -9
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.6.1.dev20250809233505.dist-info → liger_kernel_nightly-0.6.1.dev20250812205818.dist-info}/top_level.txt +0 -0
@@ -13,6 +13,7 @@ def fixed_fused_linear_cross_entropy(
|
|
13
13
|
num_items_in_batch: Optional[int] = None,
|
14
14
|
ignore_index: int = -100,
|
15
15
|
final_logit_softcapping: Optional[float] = None,
|
16
|
+
accum_dtype: Optional[torch.dtype] = None,
|
16
17
|
**kwargs,
|
17
18
|
):
|
18
19
|
reduction = "sum" if num_items_in_batch is not None else "mean"
|
@@ -23,6 +24,7 @@ def fixed_fused_linear_cross_entropy(
|
|
23
24
|
reduction=reduction,
|
24
25
|
ignore_index=ignore_index,
|
25
26
|
softcap=final_logit_softcapping,
|
27
|
+
accum_dtype=accum_dtype,
|
26
28
|
)
|
27
29
|
if reduction == "sum":
|
28
30
|
loss = loss / num_items_in_batch
|
@@ -5,131 +5,12 @@ from typing import Union
|
|
5
5
|
|
6
6
|
import torch
|
7
7
|
|
8
|
-
from
|
8
|
+
from transformers.modeling_outputs import BaseModelOutputWithPast
|
9
9
|
from transformers.modeling_outputs import CausalLMOutputWithPast
|
10
|
-
from transformers.utils.deprecation import deprecate_kwarg
|
11
10
|
|
12
|
-
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
13
11
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
14
12
|
|
15
13
|
|
16
|
-
def lce_forward_deprecated(
|
17
|
-
self,
|
18
|
-
input_ids: torch.LongTensor = None,
|
19
|
-
attention_mask: Optional[torch.Tensor] = None,
|
20
|
-
position_ids: Optional[torch.LongTensor] = None,
|
21
|
-
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
22
|
-
inputs_embeds: Optional[torch.FloatTensor] = None,
|
23
|
-
labels: Optional[torch.LongTensor] = None,
|
24
|
-
use_cache: Optional[bool] = None,
|
25
|
-
output_attentions: Optional[bool] = None,
|
26
|
-
output_hidden_states: Optional[bool] = None,
|
27
|
-
return_dict: Optional[bool] = None,
|
28
|
-
cache_position: Optional[torch.LongTensor] = None,
|
29
|
-
skip_logits: Optional[bool] = None,
|
30
|
-
) -> Union[Tuple, CausalLMOutputWithPast]:
|
31
|
-
r"""
|
32
|
-
Copy paste phi3 forward from transfomers v4.44.2 but replace torch cross entropy with liger fused linear cross entropy
|
33
|
-
|
34
|
-
|
35
|
-
Args:
|
36
|
-
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
37
|
-
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
38
|
-
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
39
|
-
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
40
|
-
|
41
|
-
Returns:
|
42
|
-
|
43
|
-
Example:
|
44
|
-
|
45
|
-
```python
|
46
|
-
>>> from transformers import AutoTokenizer, Phi3ForCausalLM
|
47
|
-
|
48
|
-
>>> model = Phi3ForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")
|
49
|
-
>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
|
50
|
-
|
51
|
-
>>> prompt = "This is an example script ."
|
52
|
-
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
53
|
-
|
54
|
-
>>> # Generate
|
55
|
-
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
56
|
-
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
57
|
-
'This is an example script .\n Certainly! Below is a sample script that demonstrates a simple task, such as calculating the sum'
|
58
|
-
```"""
|
59
|
-
|
60
|
-
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
61
|
-
output_hidden_states = (
|
62
|
-
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
63
|
-
)
|
64
|
-
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
65
|
-
|
66
|
-
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
67
|
-
outputs = self.model(
|
68
|
-
input_ids=input_ids,
|
69
|
-
attention_mask=attention_mask,
|
70
|
-
position_ids=position_ids,
|
71
|
-
past_key_values=past_key_values,
|
72
|
-
inputs_embeds=inputs_embeds,
|
73
|
-
use_cache=use_cache,
|
74
|
-
output_attentions=output_attentions,
|
75
|
-
output_hidden_states=output_hidden_states,
|
76
|
-
return_dict=return_dict,
|
77
|
-
)
|
78
|
-
|
79
|
-
hidden_states = outputs[0]
|
80
|
-
|
81
|
-
loss = None
|
82
|
-
logits = None
|
83
|
-
|
84
|
-
if skip_logits and labels is None:
|
85
|
-
raise ValueError("skip_logits is True, but labels is None")
|
86
|
-
|
87
|
-
if skip_logits is None:
|
88
|
-
# By default, if in training mode, don't materialize logits
|
89
|
-
skip_logits = self.training and labels is not None
|
90
|
-
|
91
|
-
if skip_logits:
|
92
|
-
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
93
|
-
shift_labels = labels[..., 1:].contiguous()
|
94
|
-
|
95
|
-
# flatten tokens
|
96
|
-
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
97
|
-
shift_labels = shift_labels.view(-1)
|
98
|
-
|
99
|
-
lce = LigerFusedLinearCrossEntropyLoss()
|
100
|
-
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
101
|
-
else:
|
102
|
-
logits = self.lm_head(hidden_states)
|
103
|
-
|
104
|
-
loss = None
|
105
|
-
if labels is not None:
|
106
|
-
# Upcast to float if we need to compute the loss to avoid potential precision issues
|
107
|
-
logits = logits.float()
|
108
|
-
# Shift so that tokens < n predict n
|
109
|
-
shift_logits = logits[..., :-1, :].contiguous()
|
110
|
-
shift_labels = labels[..., 1:].contiguous()
|
111
|
-
# Flatten the tokens
|
112
|
-
loss_fct = CrossEntropyLoss()
|
113
|
-
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
114
|
-
shift_labels = shift_labels.view(-1)
|
115
|
-
# Enable model parallelism
|
116
|
-
shift_labels = shift_labels.to(shift_logits.device)
|
117
|
-
loss = loss_fct(shift_logits, shift_labels)
|
118
|
-
|
119
|
-
if not return_dict:
|
120
|
-
output = (logits,) + outputs[1:]
|
121
|
-
return (loss,) + output if loss is not None else output
|
122
|
-
|
123
|
-
return CausalLMOutputWithPast(
|
124
|
-
loss=loss,
|
125
|
-
logits=logits,
|
126
|
-
past_key_values=outputs.past_key_values,
|
127
|
-
hidden_states=outputs.hidden_states,
|
128
|
-
attentions=outputs.attentions,
|
129
|
-
)
|
130
|
-
|
131
|
-
|
132
|
-
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
133
14
|
def lce_forward(
|
134
15
|
self,
|
135
16
|
input_ids: torch.LongTensor = None,
|
@@ -148,36 +29,21 @@ def lce_forward(
|
|
148
29
|
**kwargs,
|
149
30
|
) -> Union[Tuple, CausalLMOutputWithPast]:
|
150
31
|
r"""
|
151
|
-
Args:
|
152
|
-
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
153
|
-
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
154
|
-
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
155
|
-
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
156
|
-
|
157
|
-
logits_to_keep (`int` or `torch.Tensor`, *optional*):
|
158
|
-
If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
|
159
|
-
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
160
|
-
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
161
|
-
If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
|
162
|
-
This is useful when using packed tensor format (single dimension for batch and sequence length).
|
163
|
-
|
164
|
-
Returns:
|
165
|
-
|
166
32
|
Example:
|
167
33
|
|
168
34
|
```python
|
169
35
|
>>> from transformers import AutoTokenizer, Phi3ForCausalLM
|
170
36
|
|
171
|
-
>>> model = Phi3ForCausalLM.from_pretrained("
|
172
|
-
>>> tokenizer = AutoTokenizer.from_pretrained("
|
37
|
+
>>> model = Phi3ForCausalLM.from_pretrained("meta-phi3/Phi3-2-7b-hf")
|
38
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("meta-phi3/Phi3-2-7b-hf")
|
173
39
|
|
174
|
-
>>> prompt = "
|
40
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
175
41
|
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
176
42
|
|
177
43
|
>>> # Generate
|
178
44
|
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
179
45
|
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
180
|
-
|
46
|
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
181
47
|
```"""
|
182
48
|
|
183
49
|
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
@@ -186,21 +52,18 @@ def lce_forward(
|
|
186
52
|
)
|
187
53
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
188
54
|
|
189
|
-
|
190
|
-
outputs = self.model(
|
55
|
+
outputs: BaseModelOutputWithPast = self.model(
|
191
56
|
input_ids=input_ids,
|
192
57
|
attention_mask=attention_mask,
|
193
58
|
position_ids=position_ids,
|
194
59
|
past_key_values=past_key_values,
|
195
60
|
inputs_embeds=inputs_embeds,
|
196
61
|
use_cache=use_cache,
|
197
|
-
|
198
|
-
output_hidden_states=output_hidden_states,
|
199
|
-
return_dict=return_dict,
|
62
|
+
cache_position=cache_position,
|
200
63
|
**kwargs,
|
201
64
|
)
|
202
65
|
|
203
|
-
hidden_states = outputs
|
66
|
+
hidden_states = outputs.last_hidden_state
|
204
67
|
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
205
68
|
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
206
69
|
kept_hidden_states = hidden_states[:, slice_indices, :]
|
@@ -26,7 +26,6 @@ from liger_kernel.transformers.model.mistral import lce_forward as mistral_lce_f
|
|
26
26
|
from liger_kernel.transformers.model.mixtral import lce_forward as mixtral_lce_forward
|
27
27
|
from liger_kernel.transformers.model.mixtral import lce_forward_deprecated as mixtral_lce_forward_deprecated
|
28
28
|
from liger_kernel.transformers.model.phi3 import lce_forward as phi3_lce_forward
|
29
|
-
from liger_kernel.transformers.model.phi3 import lce_forward_deprecated as phi3_lce_forward_deprecated
|
30
29
|
from liger_kernel.transformers.model.qwen2 import lce_forward as qwen2_lce_forward
|
31
30
|
from liger_kernel.transformers.model.qwen2 import lce_forward_deprecated as qwen2_lce_forward_deprecated
|
32
31
|
from liger_kernel.transformers.model.smollm3 import lce_forward as smollm3_lce_forward
|
@@ -1677,25 +1676,14 @@ def apply_liger_kernel_to_phi3(
|
|
1677
1676
|
if swiglu:
|
1678
1677
|
modeling_phi3.Phi3MLP = LigerPhi3SwiGLUMLP
|
1679
1678
|
if cross_entropy:
|
1680
|
-
|
1681
|
-
from transformers.loss.loss_utils import nn
|
1679
|
+
from transformers.loss.loss_utils import nn
|
1682
1680
|
|
1683
|
-
|
1684
|
-
else:
|
1685
|
-
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
1686
|
-
modeling_phi3.CrossEntropyLoss = LigerCrossEntropyLoss
|
1681
|
+
nn.functional.cross_entropy = liger_cross_entropy
|
1687
1682
|
if fused_linear_cross_entropy:
|
1688
|
-
if
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
modeling_phi3.Phi3ForCausalLM.forward = phi3_lce_forward
|
1693
|
-
else: # if version < 4.46.1
|
1694
|
-
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
1695
|
-
if model is not None:
|
1696
|
-
model.forward = MethodType(phi3_lce_forward_deprecated, model)
|
1697
|
-
else:
|
1698
|
-
modeling_phi3.Phi3ForCausalLM.forward = phi3_lce_forward_deprecated
|
1683
|
+
if model is not None:
|
1684
|
+
model.forward = MethodType(phi3_lce_forward, model)
|
1685
|
+
else:
|
1686
|
+
modeling_phi3.Phi3ForCausalLM.forward = phi3_lce_forward
|
1699
1687
|
|
1700
1688
|
if model is not None:
|
1701
1689
|
# The model instance already exists, so we need to additionally patch the
|
@@ -58,7 +58,7 @@ liger_kernel/transformers/jsd.py,sha256=DGqRnxIZxsvxo0_tbbxX3b-sDbDjC_yKufyRIHCc
|
|
58
58
|
liger_kernel/transformers/kl_div.py,sha256=WLffFbh1EExD2Eb1F7lN11fo9JJC-0751WJjZAF1Fj8,409
|
59
59
|
liger_kernel/transformers/layer_norm.py,sha256=c9pk3PEasOKYR0rhe5e5nNrnYKVCEW4VC8S6LpCq9EQ,906
|
60
60
|
liger_kernel/transformers/llama4_rope.py,sha256=kS6PSHEwf3dS7hD7C7p8S0geugx2EMCiP0h0F7LsUoY,3639
|
61
|
-
liger_kernel/transformers/monkey_patch.py,sha256=
|
61
|
+
liger_kernel/transformers/monkey_patch.py,sha256=zeYj1XhI1at_gWdKnvZqazT53uBy_YOV36ZuQfnhf20,88545
|
62
62
|
liger_kernel/transformers/multi_token_attention.py,sha256=l9VDICK0dfmifUDW668hGscP8AHq2rYcM2oGUa3baRQ,1751
|
63
63
|
liger_kernel/transformers/qwen2vl_mrope.py,sha256=5EwSqrMdsL9MYspeBMXBsNJKvH0MOmRrtJXAJlnnlOI,1047
|
64
64
|
liger_kernel/transformers/rms_norm.py,sha256=vkekcvTeWY8vL4H6hg3t0XeY0Ew_3OFMPHuzqlxPPVw,2719
|
@@ -77,13 +77,13 @@ liger_kernel/transformers/model/glm4.py,sha256=GlnEhdGJuDIqp2R9qC54biY3HwV1tWmfp
|
|
77
77
|
liger_kernel/transformers/model/llama.py,sha256=i8jJgyZsMKWQ-zKloETLugtwFpUOdaWxLDceciFXKd4,12832
|
78
78
|
liger_kernel/transformers/model/llama4.py,sha256=IgbB8sTh3dlETQnaNNy1bZLuXy-Nt7qmeAjF27ydGpg,4210
|
79
79
|
liger_kernel/transformers/model/llava.py,sha256=bLCioday_SOm69ogMDBhy_4UsVkH2-BSl93-EXY6-7I,15076
|
80
|
-
liger_kernel/transformers/model/loss_utils.py,sha256=
|
80
|
+
liger_kernel/transformers/model/loss_utils.py,sha256=YiYsmRHIuoRnFjGpwyIM18DCsrPPmO32YWMWqkEm1UQ,1867
|
81
81
|
liger_kernel/transformers/model/mistral.py,sha256=syYNL8dLThX2-4uC13Lu0krEZ5zw3InviDUR3AJmc-I,5500
|
82
82
|
liger_kernel/transformers/model/mixtral.py,sha256=VY-y73IyjcCyWyI7ahxXLw0fJrhgjYfr1xwRYtsHX0o,11396
|
83
83
|
liger_kernel/transformers/model/mllama.py,sha256=my29NXk-p6ckQaP8qDIN8e318yI_9mQZHt38MV3SqLY,11280
|
84
84
|
liger_kernel/transformers/model/olmo2.py,sha256=6L_bo-ZUgO1lYppdJneOtYxNIylQKS6BiGp13g7Uq9E,5259
|
85
85
|
liger_kernel/transformers/model/paligemma.py,sha256=xuIx3oOwTgftU3jqLfWOxUxgCLBNJh0yNC21an9qDjo,18773
|
86
|
-
liger_kernel/transformers/model/phi3.py,sha256=
|
86
|
+
liger_kernel/transformers/model/phi3.py,sha256=AwScxUe3LjmHHyQg4gW9bMoUI7uA6fUEMXJ3YhBiHtQ,4046
|
87
87
|
liger_kernel/transformers/model/qwen2.py,sha256=3fpOTEOkniQmkCfN1KUa3KhseHJVzhj2Ht9FdYPUy-E,9962
|
88
88
|
liger_kernel/transformers/model/qwen2_5_vl.py,sha256=zEVVwotCXnAm3RRc8-1Nc8uitSWrwW4B9dYY2uOZDwg,6331
|
89
89
|
liger_kernel/transformers/model/qwen2_vl.py,sha256=5vK-vtCDpKZ2w33xYp2BS8kQYWUbKMqaiKvQcI27Mss,5884
|
@@ -94,9 +94,9 @@ liger_kernel/transformers/trainer/__init__.py,sha256=p7yQfklV8-467qSz_ZMimkbDF7H
|
|
94
94
|
liger_kernel/transformers/trainer/orpo_trainer.py,sha256=tX0h63aOFe3rNqTmk6JpMf75UPo981yzEa6TghnjS0Q,5370
|
95
95
|
liger_kernel/triton/__init__.py,sha256=qCiCamzCRv6lpV8IqpAc9YMdNKC7GKurClWceQPnlis,92
|
96
96
|
liger_kernel/triton/monkey_patch.py,sha256=Rd0hUHAzDkFfHvnX7-PBaNK5EKnZhtfM_h-fgQH9HPY,1568
|
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.
|
101
|
-
liger_kernel_nightly-0.6.1.
|
102
|
-
liger_kernel_nightly-0.6.1.
|
97
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
|
98
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/METADATA,sha256=1s-igyDBWH7I09Q1f-7-h5BtStzEN30M_ffGQk5ZE4M,24504
|
99
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
|
100
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
|
101
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
|
102
|
+
liger_kernel_nightly-0.6.1.dev20250812205818.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|