liger-kernel-nightly 0.3.1.dev20241031001941__py3-none-any.whl → 0.3.1.dev20241031214104__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.
Potentially problematic release.
This version of liger-kernel-nightly might be problematic. Click here for more details.
- liger_kernel/transformers/model/llama.py +135 -1
- liger_kernel/transformers/monkey_patch.py +15 -1
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/METADATA +2 -2
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/RECORD +13 -13
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/LICENSE-Apache-2.0 +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/LICENSE-MIT-AutoAWQ +0 -0
- /liger_kernel_nightly-0.3.1.dev20241031001941.dist-info/LICENSE-MIT-Efficient Cross Entropy → /liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-MIT-Efficient-Cross-Entropy +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/LICENSE-MIT-llmc +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/LICENSE-MIT-triton +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.3.1.dev20241031001941.dist-info → liger_kernel_nightly-0.3.1.dev20241031214104.dist-info}/top_level.txt +0 -0
|
@@ -22,7 +22,7 @@ from liger_kernel.transformers.fused_linear_cross_entropy import (
|
|
|
22
22
|
@replace_return_docstrings(
|
|
23
23
|
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
24
24
|
)
|
|
25
|
-
def
|
|
25
|
+
def lce_forward_deprecated(
|
|
26
26
|
self,
|
|
27
27
|
input_ids: torch.LongTensor = None,
|
|
28
28
|
attention_mask: Optional[torch.Tensor] = None,
|
|
@@ -145,3 +145,137 @@ def lce_forward(
|
|
|
145
145
|
hidden_states=outputs.hidden_states,
|
|
146
146
|
attentions=outputs.attentions,
|
|
147
147
|
)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
|
|
151
|
+
@replace_return_docstrings(
|
|
152
|
+
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
153
|
+
)
|
|
154
|
+
def lce_forward(
|
|
155
|
+
self,
|
|
156
|
+
input_ids=None,
|
|
157
|
+
attention_mask=None,
|
|
158
|
+
position_ids=None,
|
|
159
|
+
past_key_values=None,
|
|
160
|
+
inputs_embeds=None,
|
|
161
|
+
labels=None,
|
|
162
|
+
use_cache=None,
|
|
163
|
+
output_attentions=None,
|
|
164
|
+
output_hidden_states=None,
|
|
165
|
+
return_dict=None,
|
|
166
|
+
cache_position=None,
|
|
167
|
+
num_logits_to_keep=0,
|
|
168
|
+
**kwargs,
|
|
169
|
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
170
|
+
r"""
|
|
171
|
+
Args:
|
|
172
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
173
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
174
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
175
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
176
|
+
|
|
177
|
+
num_logits_to_keep (`int`, *optional*):
|
|
178
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
179
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
180
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
|
|
184
|
+
Example:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
>>> from transformers import AutoTokenizer, LlamaForCausalLM
|
|
188
|
+
|
|
189
|
+
>>> model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
|
|
190
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
|
|
191
|
+
|
|
192
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
|
193
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
194
|
+
|
|
195
|
+
>>> # Generate
|
|
196
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
197
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
198
|
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
|
199
|
+
```"""
|
|
200
|
+
|
|
201
|
+
"""
|
|
202
|
+
My TODO:
|
|
203
|
+
1. Run e2e example with hf < 4.46.0, GA on/GA off
|
|
204
|
+
2. Run e2e example with hf >= 4.46.0, GA on/GA off
|
|
205
|
+
"""
|
|
206
|
+
|
|
207
|
+
output_attentions = (
|
|
208
|
+
output_attentions
|
|
209
|
+
if output_attentions is not None
|
|
210
|
+
else self.config.output_attentions
|
|
211
|
+
)
|
|
212
|
+
output_hidden_states = (
|
|
213
|
+
output_hidden_states
|
|
214
|
+
if output_hidden_states is not None
|
|
215
|
+
else self.config.output_hidden_states
|
|
216
|
+
)
|
|
217
|
+
return_dict = (
|
|
218
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
222
|
+
outputs = self.model(
|
|
223
|
+
input_ids=input_ids,
|
|
224
|
+
attention_mask=attention_mask,
|
|
225
|
+
position_ids=position_ids,
|
|
226
|
+
past_key_values=past_key_values,
|
|
227
|
+
inputs_embeds=inputs_embeds,
|
|
228
|
+
use_cache=use_cache,
|
|
229
|
+
output_attentions=output_attentions,
|
|
230
|
+
output_hidden_states=output_hidden_states,
|
|
231
|
+
return_dict=return_dict,
|
|
232
|
+
cache_position=cache_position,
|
|
233
|
+
**kwargs,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
hidden_states = outputs[0]
|
|
237
|
+
|
|
238
|
+
if self.config.pretraining_tp > 1:
|
|
239
|
+
raise Exception("Liger Kernel does not support pretraining_tp!!")
|
|
240
|
+
|
|
241
|
+
logits = None
|
|
242
|
+
loss = None
|
|
243
|
+
# if in training mode, don't materialize logits
|
|
244
|
+
if self.training and (labels is not None):
|
|
245
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
246
|
+
|
|
247
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
248
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
249
|
+
|
|
250
|
+
# flatten tokens
|
|
251
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
252
|
+
shift_labels = shift_labels.view(-1)
|
|
253
|
+
|
|
254
|
+
reduction = "sum" if "num_items_in_batch" in kwargs else "mean"
|
|
255
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
256
|
+
|
|
257
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
258
|
+
if reduction == "sum":
|
|
259
|
+
loss /= kwargs["num_items_in_batch"]
|
|
260
|
+
|
|
261
|
+
else: # if in inference mode materialize logits
|
|
262
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
263
|
+
if labels is not None:
|
|
264
|
+
loss = self.loss_function(
|
|
265
|
+
logits=logits,
|
|
266
|
+
labels=labels,
|
|
267
|
+
vocab_size=self.config.vocab_size,
|
|
268
|
+
**kwargs,
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
if not return_dict:
|
|
272
|
+
output = (logits,) + outputs[1:]
|
|
273
|
+
return (loss,) + output if loss is not None else output
|
|
274
|
+
|
|
275
|
+
return CausalLMOutputWithPast(
|
|
276
|
+
loss=loss,
|
|
277
|
+
logits=logits,
|
|
278
|
+
past_key_values=outputs.past_key_values,
|
|
279
|
+
hidden_states=outputs.hidden_states,
|
|
280
|
+
attentions=outputs.attentions,
|
|
281
|
+
)
|
|
@@ -3,6 +3,8 @@ import logging
|
|
|
3
3
|
from functools import partial
|
|
4
4
|
from typing import Callable
|
|
5
5
|
|
|
6
|
+
import transformers
|
|
7
|
+
from packaging import version
|
|
6
8
|
from transformers import PreTrainedModel
|
|
7
9
|
|
|
8
10
|
from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss
|
|
@@ -10,6 +12,9 @@ from liger_kernel.transformers.geglu import LigerGEGLUMLP
|
|
|
10
12
|
from liger_kernel.transformers.layer_norm import LigerLayerNorm
|
|
11
13
|
from liger_kernel.transformers.model.gemma import lce_forward as gemma_lce_forward
|
|
12
14
|
from liger_kernel.transformers.model.llama import lce_forward as llama_lce_forward
|
|
15
|
+
from liger_kernel.transformers.model.llama import (
|
|
16
|
+
lce_forward_deprecated as llama_lce_forward_deprecated,
|
|
17
|
+
)
|
|
13
18
|
from liger_kernel.transformers.model.mistral import lce_forward as mistral_lce_forward
|
|
14
19
|
from liger_kernel.transformers.model.mixtral import lce_forward as mixtral_lce_forward
|
|
15
20
|
from liger_kernel.transformers.model.phi3 import lce_forward as phi3_lce_forward
|
|
@@ -22,6 +27,8 @@ from liger_kernel.transformers.swiglu import (
|
|
|
22
27
|
LigerSwiGLUMLP,
|
|
23
28
|
)
|
|
24
29
|
|
|
30
|
+
transformer_version = version.parse(transformers.__version__)
|
|
31
|
+
|
|
25
32
|
logger = logging.getLogger(__name__)
|
|
26
33
|
|
|
27
34
|
|
|
@@ -88,7 +95,14 @@ def apply_liger_kernel_to_llama(
|
|
|
88
95
|
if cross_entropy:
|
|
89
96
|
modeling_llama.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
90
97
|
if fused_linear_cross_entropy:
|
|
91
|
-
|
|
98
|
+
if transformer_version >= version.parse("4.46.0"):
|
|
99
|
+
modeling_llama.LlamaForCausalLM.forward = llama_lce_forward
|
|
100
|
+
else: # if version < 4.46.0
|
|
101
|
+
logger.warning(
|
|
102
|
+
"Support for transformers versions < 4.46.0 will soon be discontinued due to issues with incorrect gradient accumulation. "
|
|
103
|
+
"Please consider upgrading to avoid potential issues. See details: https://github.com/huggingface/transformers/pull/34191"
|
|
104
|
+
)
|
|
105
|
+
modeling_llama.LlamaForCausalLM.forward = llama_lce_forward_deprecated
|
|
92
106
|
|
|
93
107
|
if model is not None:
|
|
94
108
|
# The model instance already exists, so we need to additionally patch the
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: liger_kernel_nightly
|
|
3
|
-
Version: 0.3.1.
|
|
3
|
+
Version: 0.3.1.dev20241031214104
|
|
4
4
|
Summary: Efficient Triton kernels for LLM Training
|
|
5
5
|
License: BSD 2-CLAUSE LICENSE
|
|
6
6
|
Copyright 2024 LinkedIn Corporation
|
|
@@ -31,7 +31,7 @@ Description-Content-Type: text/markdown
|
|
|
31
31
|
License-File: LICENSE
|
|
32
32
|
License-File: LICENSE-Apache-2.0
|
|
33
33
|
License-File: LICENSE-MIT-AutoAWQ
|
|
34
|
-
License-File: LICENSE-MIT-Efficient
|
|
34
|
+
License-File: LICENSE-MIT-Efficient-Cross-Entropy
|
|
35
35
|
License-File: LICENSE-MIT-llmc
|
|
36
36
|
License-File: LICENSE-MIT-triton
|
|
37
37
|
License-File: NOTICE
|
|
@@ -23,7 +23,7 @@ liger_kernel/transformers/geglu.py,sha256=QcrME_8ooIn0xa59LaC0aoOdRrBIFd11Y0bAyF
|
|
|
23
23
|
liger_kernel/transformers/jsd.py,sha256=W-5CypO2mx4-bUWOxq1KScfCdoXlLoYbtt5xBnRzMs4,3056
|
|
24
24
|
liger_kernel/transformers/kl_div.py,sha256=qVhjBg6tjRyue5iZ3NFxo8uySY4JuIFJyv0IM_50F24,431
|
|
25
25
|
liger_kernel/transformers/layer_norm.py,sha256=fd6o4kSHJWolQMWxh-l1qObfgL08ruNbUoBiANKX1ow,972
|
|
26
|
-
liger_kernel/transformers/monkey_patch.py,sha256=
|
|
26
|
+
liger_kernel/transformers/monkey_patch.py,sha256=f8Mm3LaBB2NehbLyEK3kz1rE4u98QJna9OyM2XAy6NI,33607
|
|
27
27
|
liger_kernel/transformers/rms_norm.py,sha256=4XfMQI6dORF7s_5qUqVHKWv-3IUomaimU2dg-NwnpoM,1035
|
|
28
28
|
liger_kernel/transformers/rope.py,sha256=m-ah8vZBYW8tfplTXCiAPMHJWlB1tdp_JPXJeWE-Boo,943
|
|
29
29
|
liger_kernel/transformers/swiglu.py,sha256=0-tVJ8xEYfhxnduc16PflXFj8sZPxdx9sHUn3hfwCI4,2468
|
|
@@ -31,7 +31,7 @@ liger_kernel/transformers/trainer_integration.py,sha256=W3ON51O5GkyzNJsItz0y5rKx
|
|
|
31
31
|
liger_kernel/transformers/experimental/embedding.py,sha256=HpckiAMKM8-SRxKDcGTqortVxnjhwpZsfsp9lfjqfeM,895
|
|
32
32
|
liger_kernel/transformers/model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
33
33
|
liger_kernel/transformers/model/gemma.py,sha256=EcdkGbSj_qroTDFl0Sc_HLyDyY0xcDhwrgkM_wkXnw8,4987
|
|
34
|
-
liger_kernel/transformers/model/llama.py,sha256=
|
|
34
|
+
liger_kernel/transformers/model/llama.py,sha256=B_4mt7m2_nDGpa2bZymjshbF4sAVJWTn5pho86uGTZo,10167
|
|
35
35
|
liger_kernel/transformers/model/mistral.py,sha256=_MQJrDntlxBO5cJwgTjr2rk2nNd5FAXVnzcTg_PEekQ,5079
|
|
36
36
|
liger_kernel/transformers/model/mixtral.py,sha256=51FghRY8aGBWat7KSgTeFDqdStDiXY3dEJepByNhEOE,5847
|
|
37
37
|
liger_kernel/transformers/model/mllama.py,sha256=S00P0pJrGHOWBx170TPYZbQ0djv0__m8Dqv1FvKZUyE,5926
|
|
@@ -40,14 +40,14 @@ liger_kernel/transformers/model/qwen2.py,sha256=3inWFXGHYT7wA10OR6bq3mDUBrr10AS5
|
|
|
40
40
|
liger_kernel/transformers/model/qwen2_vl.py,sha256=ymsm9aQpSUiSU12GY8FO608p9dSHOz4TCnNI1htX5bk,6975
|
|
41
41
|
liger_kernel/triton/__init__.py,sha256=yfRe0zMb47QnqjecZWG7LnanfCTzeku7SgWRAwNVmzU,101
|
|
42
42
|
liger_kernel/triton/monkey_patch.py,sha256=5BcGKTtdqeYchypBIBopGIWPx1-cFALz7sOKoEsqXJ0,1584
|
|
43
|
-
liger_kernel_nightly-0.3.1.
|
|
44
|
-
liger_kernel_nightly-0.3.1.
|
|
45
|
-
liger_kernel_nightly-0.3.1.
|
|
46
|
-
liger_kernel_nightly-0.3.1.
|
|
47
|
-
liger_kernel_nightly-0.3.1.
|
|
48
|
-
liger_kernel_nightly-0.3.1.
|
|
49
|
-
liger_kernel_nightly-0.3.1.
|
|
50
|
-
liger_kernel_nightly-0.3.1.
|
|
51
|
-
liger_kernel_nightly-0.3.1.
|
|
52
|
-
liger_kernel_nightly-0.3.1.
|
|
53
|
-
liger_kernel_nightly-0.3.1.
|
|
43
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
|
|
44
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-Apache-2.0,sha256=NRaCIsL9eblGS35gk4WKTC0usNYnR_mgRHJTKqz2_UE,11348
|
|
45
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-MIT-AutoAWQ,sha256=pfiOyInrAPY3xQbvV1i-gOqNZK7QEyIepT1IbqOYYYo,1067
|
|
46
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-MIT-Efficient-Cross-Entropy,sha256=PaC9HqyFYTy-ClS0H8Zfa2motJuTppjECXmjHwJcaOk,1063
|
|
47
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-MIT-llmc,sha256=kyFLt_XUcXS88CuxQt5-PjOcLjpJP2m-T4gtqZf3GLc,1071
|
|
48
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/LICENSE-MIT-triton,sha256=wL6W8IwsKiyHtzXubg8TCXhRZuo8S83EPdqXffYtqWg,1131
|
|
49
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/METADATA,sha256=wnIQKAkgub7YGk78qo0YfkPlYoWGAcLUWPd36sBeHY8,27717
|
|
50
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
|
|
51
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
52
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
|
|
53
|
+
liger_kernel_nightly-0.3.1.dev20241031214104.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|