liger-kernel-nightly 0.3.1.dev20241105004109__py3-none-any.whl → 0.3.1.dev20241105220546__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/gemma.py +124 -1
- liger_kernel/transformers/model/mistral.py +3 -0
- liger_kernel/transformers/model/mixtral.py +151 -1
- liger_kernel/transformers/model/mllama.py +133 -1
- liger_kernel/transformers/model/phi3.py +138 -1
- liger_kernel/transformers/model/qwen2.py +121 -1
- liger_kernel/transformers/model/qwen2_vl.py +1 -0
- liger_kernel/transformers/monkey_patch.py +50 -13
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.dist-info}/METADATA +1 -1
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.dist-info}/RECORD +14 -14
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.3.1.dev20241105004109.dist-info → liger_kernel_nightly-0.3.1.dev20241105220546.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,
|
|
@@ -136,3 +136,126 @@ def lce_forward(
|
|
|
136
136
|
hidden_states=outputs.hidden_states,
|
|
137
137
|
attentions=outputs.attentions,
|
|
138
138
|
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@add_start_docstrings_to_model_forward(GEMMA_INPUTS_DOCSTRING)
|
|
142
|
+
@replace_return_docstrings(
|
|
143
|
+
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
144
|
+
)
|
|
145
|
+
def lce_forward(
|
|
146
|
+
self,
|
|
147
|
+
input_ids: torch.LongTensor = None,
|
|
148
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
149
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
150
|
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
|
151
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
152
|
+
labels: Optional[torch.LongTensor] = None,
|
|
153
|
+
use_cache: Optional[bool] = None,
|
|
154
|
+
output_attentions: Optional[bool] = None,
|
|
155
|
+
output_hidden_states: Optional[bool] = None,
|
|
156
|
+
return_dict: Optional[bool] = None,
|
|
157
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
158
|
+
num_logits_to_keep: int = 0,
|
|
159
|
+
**loss_kwargs,
|
|
160
|
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
161
|
+
r"""
|
|
162
|
+
Args:
|
|
163
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
164
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
165
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
166
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
167
|
+
|
|
168
|
+
num_logits_to_keep (`int`, *optional*):
|
|
169
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
170
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
171
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
|
|
175
|
+
Example:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
>>> from transformers import AutoTokenizer, GemmaForCausalLM
|
|
179
|
+
|
|
180
|
+
>>> model = GemmaForCausalLM.from_pretrained("google/gemma-7b")
|
|
181
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
|
|
182
|
+
|
|
183
|
+
>>> prompt = "What is your favorite condiment?"
|
|
184
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
185
|
+
|
|
186
|
+
>>> # Generate
|
|
187
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
188
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
189
|
+
"What is your favorite condiment?"
|
|
190
|
+
```"""
|
|
191
|
+
output_attentions = (
|
|
192
|
+
output_attentions
|
|
193
|
+
if output_attentions is not None
|
|
194
|
+
else self.config.output_attentions
|
|
195
|
+
)
|
|
196
|
+
output_hidden_states = (
|
|
197
|
+
output_hidden_states
|
|
198
|
+
if output_hidden_states is not None
|
|
199
|
+
else self.config.output_hidden_states
|
|
200
|
+
)
|
|
201
|
+
return_dict = (
|
|
202
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
206
|
+
outputs = self.model(
|
|
207
|
+
input_ids=input_ids,
|
|
208
|
+
attention_mask=attention_mask,
|
|
209
|
+
position_ids=position_ids,
|
|
210
|
+
past_key_values=past_key_values,
|
|
211
|
+
inputs_embeds=inputs_embeds,
|
|
212
|
+
use_cache=use_cache,
|
|
213
|
+
output_attentions=output_attentions,
|
|
214
|
+
output_hidden_states=output_hidden_states,
|
|
215
|
+
return_dict=return_dict,
|
|
216
|
+
cache_position=cache_position,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
hidden_states = outputs[0]
|
|
220
|
+
|
|
221
|
+
logits = None
|
|
222
|
+
loss = None
|
|
223
|
+
# if in training mode, don't materialize logits
|
|
224
|
+
if self.training and (labels is not None):
|
|
225
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
226
|
+
|
|
227
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
228
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
229
|
+
|
|
230
|
+
# flatten tokens
|
|
231
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
232
|
+
shift_labels = shift_labels.view(-1)
|
|
233
|
+
|
|
234
|
+
reduction = "sum" if "num_items_in_batch" in loss_kwargs else "mean"
|
|
235
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
236
|
+
|
|
237
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
238
|
+
if reduction == "sum":
|
|
239
|
+
loss /= loss_kwargs["num_items_in_batch"]
|
|
240
|
+
|
|
241
|
+
else: # if in inference mode materialize logits
|
|
242
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
243
|
+
if labels is not None:
|
|
244
|
+
loss = self.loss_function(
|
|
245
|
+
logits=logits,
|
|
246
|
+
labels=labels,
|
|
247
|
+
vocab_size=self.config.vocab_size,
|
|
248
|
+
**loss_kwargs,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
if not return_dict:
|
|
252
|
+
output = (logits,) + outputs[1:]
|
|
253
|
+
return (loss,) + output if loss is not None else output
|
|
254
|
+
|
|
255
|
+
return CausalLMOutputWithPast(
|
|
256
|
+
loss=loss,
|
|
257
|
+
logits=logits,
|
|
258
|
+
past_key_values=outputs.past_key_values,
|
|
259
|
+
hidden_states=outputs.hidden_states,
|
|
260
|
+
attentions=outputs.attentions,
|
|
261
|
+
)
|
|
@@ -22,7 +22,7 @@ from liger_kernel.transformers.fused_linear_cross_entropy import (
|
|
|
22
22
|
@replace_return_docstrings(
|
|
23
23
|
output_type=MoeCausalLMOutputWithPast, 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,
|
|
@@ -157,3 +157,153 @@ def lce_forward(
|
|
|
157
157
|
attentions=outputs.attentions,
|
|
158
158
|
router_logits=outputs.router_logits,
|
|
159
159
|
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING)
|
|
163
|
+
@replace_return_docstrings(
|
|
164
|
+
output_type=MoeCausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
165
|
+
)
|
|
166
|
+
# Ignore copy
|
|
167
|
+
def lce_forward(
|
|
168
|
+
self,
|
|
169
|
+
input_ids: torch.LongTensor = None,
|
|
170
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
171
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
172
|
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
|
173
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
174
|
+
labels: Optional[torch.LongTensor] = None,
|
|
175
|
+
use_cache: Optional[bool] = None,
|
|
176
|
+
output_attentions: Optional[bool] = None,
|
|
177
|
+
output_hidden_states: Optional[bool] = None,
|
|
178
|
+
output_router_logits: Optional[bool] = None,
|
|
179
|
+
return_dict: Optional[bool] = None,
|
|
180
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
181
|
+
num_logits_to_keep: int = 0,
|
|
182
|
+
**loss_kwargs,
|
|
183
|
+
) -> Union[Tuple, MoeCausalLMOutputWithPast]:
|
|
184
|
+
r"""
|
|
185
|
+
Args:
|
|
186
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
187
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
188
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
189
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
190
|
+
|
|
191
|
+
num_logits_to_keep (`int`, *optional*):
|
|
192
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
193
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
194
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
|
|
198
|
+
Example:
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
>>> from transformers import AutoTokenizer, MixtralForCausalLM
|
|
202
|
+
|
|
203
|
+
>>> model = MixtralForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
|
|
204
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
|
|
205
|
+
|
|
206
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
|
207
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
208
|
+
|
|
209
|
+
>>> # Generate
|
|
210
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
211
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
212
|
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
|
213
|
+
```"""
|
|
214
|
+
|
|
215
|
+
output_attentions = (
|
|
216
|
+
output_attentions
|
|
217
|
+
if output_attentions is not None
|
|
218
|
+
else self.config.output_attentions
|
|
219
|
+
)
|
|
220
|
+
output_router_logits = (
|
|
221
|
+
output_router_logits
|
|
222
|
+
if output_router_logits is not None
|
|
223
|
+
else self.config.output_router_logits
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
output_hidden_states = (
|
|
227
|
+
output_hidden_states
|
|
228
|
+
if output_hidden_states is not None
|
|
229
|
+
else self.config.output_hidden_states
|
|
230
|
+
)
|
|
231
|
+
return_dict = (
|
|
232
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
236
|
+
outputs = self.model(
|
|
237
|
+
input_ids=input_ids,
|
|
238
|
+
attention_mask=attention_mask,
|
|
239
|
+
position_ids=position_ids,
|
|
240
|
+
past_key_values=past_key_values,
|
|
241
|
+
inputs_embeds=inputs_embeds,
|
|
242
|
+
use_cache=use_cache,
|
|
243
|
+
output_attentions=output_attentions,
|
|
244
|
+
output_hidden_states=output_hidden_states,
|
|
245
|
+
output_router_logits=output_router_logits,
|
|
246
|
+
return_dict=return_dict,
|
|
247
|
+
cache_position=cache_position,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
hidden_states = outputs[0]
|
|
251
|
+
|
|
252
|
+
logits = None
|
|
253
|
+
loss = None
|
|
254
|
+
# if in training mode, don't materialize logits
|
|
255
|
+
if self.training and (labels is not None):
|
|
256
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
257
|
+
|
|
258
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
259
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
260
|
+
|
|
261
|
+
# flatten tokens
|
|
262
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
263
|
+
shift_labels = shift_labels.view(-1)
|
|
264
|
+
|
|
265
|
+
reduction = "sum" if "num_items_in_batch" in loss_kwargs else "mean"
|
|
266
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
267
|
+
|
|
268
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
269
|
+
if reduction == "sum":
|
|
270
|
+
loss /= loss_kwargs["num_items_in_batch"]
|
|
271
|
+
|
|
272
|
+
else: # if in inference mode materialize logits
|
|
273
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
274
|
+
if labels is not None:
|
|
275
|
+
loss = self.loss_function(
|
|
276
|
+
logits=logits,
|
|
277
|
+
labels=labels,
|
|
278
|
+
vocab_size=self.config.vocab_size,
|
|
279
|
+
**loss_kwargs,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
aux_loss = None
|
|
283
|
+
if output_router_logits:
|
|
284
|
+
aux_loss = load_balancing_loss_func(
|
|
285
|
+
outputs.router_logits if return_dict else outputs[-1],
|
|
286
|
+
self.num_experts,
|
|
287
|
+
self.num_experts_per_tok,
|
|
288
|
+
attention_mask,
|
|
289
|
+
)
|
|
290
|
+
if labels is not None:
|
|
291
|
+
loss += self.router_aux_loss_coef * aux_loss.to(
|
|
292
|
+
loss.device
|
|
293
|
+
) # make sure to reside in the same device
|
|
294
|
+
|
|
295
|
+
if not return_dict:
|
|
296
|
+
output = (logits,) + outputs[1:]
|
|
297
|
+
if output_router_logits:
|
|
298
|
+
output = (aux_loss,) + output
|
|
299
|
+
return (loss,) + output if loss is not None else output
|
|
300
|
+
|
|
301
|
+
return MoeCausalLMOutputWithPast(
|
|
302
|
+
loss=loss,
|
|
303
|
+
aux_loss=aux_loss,
|
|
304
|
+
logits=logits,
|
|
305
|
+
past_key_values=outputs.past_key_values,
|
|
306
|
+
hidden_states=outputs.hidden_states,
|
|
307
|
+
attentions=outputs.attentions,
|
|
308
|
+
router_logits=outputs.router_logits,
|
|
309
|
+
)
|
|
@@ -19,7 +19,7 @@ from liger_kernel.transformers.fused_linear_cross_entropy import (
|
|
|
19
19
|
@replace_return_docstrings(
|
|
20
20
|
output_type=CausalLMOutputWithPast, config_class="MllamaTextConfig"
|
|
21
21
|
)
|
|
22
|
-
def
|
|
22
|
+
def lce_forward_deprecated(
|
|
23
23
|
self,
|
|
24
24
|
input_ids: torch.LongTensor = None,
|
|
25
25
|
attention_mask: Optional[torch.Tensor] = None,
|
|
@@ -140,3 +140,135 @@ def lce_forward(
|
|
|
140
140
|
hidden_states=outputs.hidden_states,
|
|
141
141
|
attentions=outputs.attentions,
|
|
142
142
|
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@add_start_docstrings_to_model_forward(MLLAMA_INPUTS_DOCSTRING)
|
|
146
|
+
@replace_return_docstrings(
|
|
147
|
+
output_type=CausalLMOutputWithPast, config_class="MllamaTextConfig"
|
|
148
|
+
)
|
|
149
|
+
def lce_forward(
|
|
150
|
+
self,
|
|
151
|
+
input_ids: torch.LongTensor = None,
|
|
152
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
153
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
154
|
+
cross_attention_states: Optional[torch.LongTensor] = None,
|
|
155
|
+
cross_attention_mask: Optional[torch.LongTensor] = None,
|
|
156
|
+
full_text_row_masked_out_mask: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
|
157
|
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
|
158
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
159
|
+
labels: Optional[torch.LongTensor] = None,
|
|
160
|
+
use_cache: Optional[bool] = None,
|
|
161
|
+
output_attentions: Optional[bool] = None,
|
|
162
|
+
output_hidden_states: Optional[bool] = None,
|
|
163
|
+
return_dict: Optional[bool] = None,
|
|
164
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
165
|
+
num_logits_to_keep: int = 0,
|
|
166
|
+
**loss_kwargs,
|
|
167
|
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
168
|
+
r"""
|
|
169
|
+
Args:
|
|
170
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
171
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
172
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
173
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
174
|
+
|
|
175
|
+
num_logits_to_keep (`int`, *optional*):
|
|
176
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
177
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
178
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
|
|
182
|
+
Example:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
>>> from transformers import AutoTokenizer, MllamaForCausalLM
|
|
186
|
+
|
|
187
|
+
>>> model = MllamaForCausalLM.from_pretrained("Llama-3.2-11B-Vision")
|
|
188
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("Llama-3.2-11B-Vision")
|
|
189
|
+
|
|
190
|
+
>>> prompt = "If I had to write a haiku, it would be:"
|
|
191
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
192
|
+
|
|
193
|
+
>>> # Generate
|
|
194
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=40, do_sample=True, temperature=0.6)
|
|
195
|
+
>>> result = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
196
|
+
>>> print(result)
|
|
197
|
+
If I had to write a haiku, it would be: "Snowflakes gently fall" - simple, yet peaceful.
|
|
198
|
+
I love the idea of snowflakes gently falling, each one
|
|
199
|
+
```
|
|
200
|
+
"""
|
|
201
|
+
output_attentions = (
|
|
202
|
+
output_attentions
|
|
203
|
+
if output_attentions is not None
|
|
204
|
+
else self.config.output_attentions
|
|
205
|
+
)
|
|
206
|
+
output_hidden_states = (
|
|
207
|
+
output_hidden_states
|
|
208
|
+
if output_hidden_states is not None
|
|
209
|
+
else self.config.output_hidden_states
|
|
210
|
+
)
|
|
211
|
+
return_dict = (
|
|
212
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
216
|
+
outputs = self.model(
|
|
217
|
+
input_ids=input_ids,
|
|
218
|
+
cross_attention_states=cross_attention_states,
|
|
219
|
+
attention_mask=attention_mask,
|
|
220
|
+
position_ids=position_ids,
|
|
221
|
+
cross_attention_mask=cross_attention_mask,
|
|
222
|
+
full_text_row_masked_out_mask=full_text_row_masked_out_mask,
|
|
223
|
+
past_key_values=past_key_values,
|
|
224
|
+
inputs_embeds=inputs_embeds,
|
|
225
|
+
use_cache=use_cache,
|
|
226
|
+
output_attentions=output_attentions,
|
|
227
|
+
output_hidden_states=output_hidden_states,
|
|
228
|
+
return_dict=return_dict,
|
|
229
|
+
cache_position=cache_position,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
hidden_states = outputs[0]
|
|
233
|
+
|
|
234
|
+
logits = None
|
|
235
|
+
loss = None
|
|
236
|
+
# if in training mode, don't materialize logits
|
|
237
|
+
if self.training and (labels is not None):
|
|
238
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
239
|
+
|
|
240
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
241
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
242
|
+
|
|
243
|
+
# flatten tokens
|
|
244
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
245
|
+
shift_labels = shift_labels.view(-1)
|
|
246
|
+
|
|
247
|
+
reduction = "sum" if "num_items_in_batch" in loss_kwargs else "mean"
|
|
248
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
249
|
+
|
|
250
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
251
|
+
if reduction == "sum":
|
|
252
|
+
loss /= loss_kwargs["num_items_in_batch"]
|
|
253
|
+
|
|
254
|
+
else: # if in inference mode materialize logits
|
|
255
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
256
|
+
if labels is not None:
|
|
257
|
+
loss = self.loss_function(
|
|
258
|
+
logits=logits,
|
|
259
|
+
labels=labels,
|
|
260
|
+
vocab_size=self.config.vocab_size,
|
|
261
|
+
**loss_kwargs,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
if not return_dict:
|
|
265
|
+
output = (logits,) + outputs[1:]
|
|
266
|
+
return (loss,) + output if loss is not None else output
|
|
267
|
+
|
|
268
|
+
return CausalLMOutputWithPast(
|
|
269
|
+
loss=loss,
|
|
270
|
+
logits=logits,
|
|
271
|
+
past_key_values=outputs.past_key_values,
|
|
272
|
+
hidden_states=outputs.hidden_states,
|
|
273
|
+
attentions=outputs.attentions,
|
|
274
|
+
)
|
|
@@ -21,7 +21,7 @@ from liger_kernel.transformers.fused_linear_cross_entropy import (
|
|
|
21
21
|
@replace_return_docstrings(
|
|
22
22
|
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
23
23
|
)
|
|
24
|
-
def
|
|
24
|
+
def lce_forward_deprecated(
|
|
25
25
|
self,
|
|
26
26
|
input_ids: torch.LongTensor = None,
|
|
27
27
|
attention_mask: Optional[torch.Tensor] = None,
|
|
@@ -135,3 +135,140 @@ def lce_forward(
|
|
|
135
135
|
hidden_states=outputs.hidden_states,
|
|
136
136
|
attentions=outputs.attentions,
|
|
137
137
|
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@add_start_docstrings_to_model_forward(PHI3_INPUTS_DOCSTRING)
|
|
141
|
+
@replace_return_docstrings(
|
|
142
|
+
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
143
|
+
)
|
|
144
|
+
def lce_forward(
|
|
145
|
+
self,
|
|
146
|
+
input_ids: torch.LongTensor = None,
|
|
147
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
148
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
149
|
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
|
150
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
151
|
+
labels: Optional[torch.LongTensor] = None,
|
|
152
|
+
use_cache: Optional[bool] = None,
|
|
153
|
+
output_attentions: Optional[bool] = None,
|
|
154
|
+
output_hidden_states: Optional[bool] = None,
|
|
155
|
+
return_dict: Optional[bool] = None,
|
|
156
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
157
|
+
num_logits_to_keep: int = 0,
|
|
158
|
+
**loss_kwargs,
|
|
159
|
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
160
|
+
r"""
|
|
161
|
+
Args:
|
|
162
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
163
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
164
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
165
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
166
|
+
|
|
167
|
+
num_logits_to_keep (`int`, *optional*):
|
|
168
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
169
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
170
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
|
|
174
|
+
Example:
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
>>> from transformers import AutoTokenizer, Phi3ForCausalLM
|
|
178
|
+
|
|
179
|
+
>>> model = Phi3ForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")
|
|
180
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
|
|
181
|
+
|
|
182
|
+
>>> prompt = "This is an example script ."
|
|
183
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
184
|
+
|
|
185
|
+
>>> # Generate
|
|
186
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
187
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
188
|
+
'This is an example script .\n Certainly! Below is a sample script that demonstrates a simple task, such as calculating the sum'
|
|
189
|
+
```"""
|
|
190
|
+
|
|
191
|
+
from transformers.models.phi3.modeling_phi3 import logging
|
|
192
|
+
|
|
193
|
+
logger = logging.get_logger(__name__)
|
|
194
|
+
|
|
195
|
+
if (
|
|
196
|
+
use_cache
|
|
197
|
+
and self.config.rope_scaling
|
|
198
|
+
and cache_position is not None
|
|
199
|
+
and cache_position[0] == self.config.original_max_position_embeddings
|
|
200
|
+
):
|
|
201
|
+
logger.warning(
|
|
202
|
+
f"If you are not using the generate method, you may encounter nonsensical outputs after the {self.config.original_max_position_embeddings}th token, as the KV cache needs to be recomputed."
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
output_attentions = (
|
|
206
|
+
output_attentions
|
|
207
|
+
if output_attentions is not None
|
|
208
|
+
else self.config.output_attentions
|
|
209
|
+
)
|
|
210
|
+
output_hidden_states = (
|
|
211
|
+
output_hidden_states
|
|
212
|
+
if output_hidden_states is not None
|
|
213
|
+
else self.config.output_hidden_states
|
|
214
|
+
)
|
|
215
|
+
return_dict = (
|
|
216
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
220
|
+
outputs = self.model(
|
|
221
|
+
input_ids=input_ids,
|
|
222
|
+
attention_mask=attention_mask,
|
|
223
|
+
position_ids=position_ids,
|
|
224
|
+
past_key_values=past_key_values,
|
|
225
|
+
inputs_embeds=inputs_embeds,
|
|
226
|
+
use_cache=use_cache,
|
|
227
|
+
output_attentions=output_attentions,
|
|
228
|
+
output_hidden_states=output_hidden_states,
|
|
229
|
+
return_dict=return_dict,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
hidden_states = outputs[0]
|
|
233
|
+
|
|
234
|
+
logits = None
|
|
235
|
+
loss = None
|
|
236
|
+
# if in training mode, don't materialize logits
|
|
237
|
+
if self.training and (labels is not None):
|
|
238
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
239
|
+
|
|
240
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
241
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
242
|
+
|
|
243
|
+
# flatten tokens
|
|
244
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
245
|
+
shift_labels = shift_labels.view(-1)
|
|
246
|
+
|
|
247
|
+
reduction = "sum" if "num_items_in_batch" in loss_kwargs else "mean"
|
|
248
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
249
|
+
|
|
250
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
251
|
+
if reduction == "sum":
|
|
252
|
+
loss /= loss_kwargs["num_items_in_batch"]
|
|
253
|
+
|
|
254
|
+
else: # if in inference mode materialize logits
|
|
255
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
256
|
+
if labels is not None:
|
|
257
|
+
loss = self.loss_function(
|
|
258
|
+
logits=logits,
|
|
259
|
+
labels=labels,
|
|
260
|
+
vocab_size=self.config.vocab_size,
|
|
261
|
+
**loss_kwargs,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
if not return_dict:
|
|
265
|
+
output = (logits,) + outputs[1:]
|
|
266
|
+
return (loss,) + output if loss is not None else output
|
|
267
|
+
|
|
268
|
+
return CausalLMOutputWithPast(
|
|
269
|
+
loss=loss,
|
|
270
|
+
logits=logits,
|
|
271
|
+
past_key_values=outputs.past_key_values,
|
|
272
|
+
hidden_states=outputs.hidden_states,
|
|
273
|
+
attentions=outputs.attentions,
|
|
274
|
+
)
|
|
@@ -21,7 +21,7 @@ from liger_kernel.transformers.fused_linear_cross_entropy import (
|
|
|
21
21
|
@replace_return_docstrings(
|
|
22
22
|
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
23
23
|
)
|
|
24
|
-
def
|
|
24
|
+
def lce_forward_deprecated(
|
|
25
25
|
self,
|
|
26
26
|
input_ids: torch.LongTensor = None,
|
|
27
27
|
attention_mask: Optional[torch.Tensor] = None,
|
|
@@ -134,3 +134,123 @@ def lce_forward(
|
|
|
134
134
|
hidden_states=outputs.hidden_states,
|
|
135
135
|
attentions=outputs.attentions,
|
|
136
136
|
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
|
|
140
|
+
@replace_return_docstrings(
|
|
141
|
+
output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
|
|
142
|
+
)
|
|
143
|
+
def lce_forward(
|
|
144
|
+
self,
|
|
145
|
+
input_ids: torch.LongTensor = None,
|
|
146
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
147
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
148
|
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
|
149
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
150
|
+
labels: Optional[torch.LongTensor] = None,
|
|
151
|
+
use_cache: Optional[bool] = None,
|
|
152
|
+
output_attentions: Optional[bool] = None,
|
|
153
|
+
output_hidden_states: Optional[bool] = None,
|
|
154
|
+
return_dict: Optional[bool] = None,
|
|
155
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
156
|
+
num_logits_to_keep: int = 0,
|
|
157
|
+
**loss_kwargs,
|
|
158
|
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
159
|
+
r"""
|
|
160
|
+
Args:
|
|
161
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
162
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
163
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
164
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
165
|
+
|
|
166
|
+
num_logits_to_keep (`int`, *optional*):
|
|
167
|
+
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
|
|
168
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
169
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
|
|
173
|
+
Example:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
>>> from transformers import AutoTokenizer, Qwen2ForCausalLM
|
|
177
|
+
|
|
178
|
+
>>> model = Qwen2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
|
179
|
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
|
180
|
+
|
|
181
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
|
182
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
183
|
+
|
|
184
|
+
>>> # Generate
|
|
185
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
186
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
187
|
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
|
188
|
+
```"""
|
|
189
|
+
|
|
190
|
+
output_attentions = (
|
|
191
|
+
output_attentions
|
|
192
|
+
if output_attentions is not None
|
|
193
|
+
else self.config.output_attentions
|
|
194
|
+
)
|
|
195
|
+
output_hidden_states = (
|
|
196
|
+
output_hidden_states
|
|
197
|
+
if output_hidden_states is not None
|
|
198
|
+
else self.config.output_hidden_states
|
|
199
|
+
)
|
|
200
|
+
return_dict = (
|
|
201
|
+
return_dict if return_dict is not None else self.config.use_return_dict
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
205
|
+
outputs = self.model(
|
|
206
|
+
input_ids=input_ids,
|
|
207
|
+
attention_mask=attention_mask,
|
|
208
|
+
position_ids=position_ids,
|
|
209
|
+
past_key_values=past_key_values,
|
|
210
|
+
inputs_embeds=inputs_embeds,
|
|
211
|
+
use_cache=use_cache,
|
|
212
|
+
output_attentions=output_attentions,
|
|
213
|
+
output_hidden_states=output_hidden_states,
|
|
214
|
+
return_dict=return_dict,
|
|
215
|
+
cache_position=cache_position,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
hidden_states = outputs[0]
|
|
219
|
+
|
|
220
|
+
logits = None
|
|
221
|
+
loss = None
|
|
222
|
+
# if in training mode, don't materialize logits
|
|
223
|
+
if self.training and (labels is not None):
|
|
224
|
+
# We do the same thing as ForCausalLMLoss but using Liger FLCE
|
|
225
|
+
|
|
226
|
+
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
227
|
+
shift_labels = labels[..., 1:].contiguous()
|
|
228
|
+
|
|
229
|
+
# flatten tokens
|
|
230
|
+
shift_hidden_states = shift_hidden_states.view(-1, self.config.hidden_size)
|
|
231
|
+
shift_labels = shift_labels.view(-1)
|
|
232
|
+
|
|
233
|
+
reduction = "sum" if "num_items_in_batch" in loss_kwargs else "mean"
|
|
234
|
+
lce = LigerFusedLinearCrossEntropyLoss(reduction=reduction)
|
|
235
|
+
|
|
236
|
+
loss = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
237
|
+
if reduction == "sum":
|
|
238
|
+
loss /= loss_kwargs["num_items_in_batch"]
|
|
239
|
+
|
|
240
|
+
else: # if in inference mode materialize logits
|
|
241
|
+
logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])
|
|
242
|
+
if labels is not None:
|
|
243
|
+
loss = self.loss_function(
|
|
244
|
+
logits=logits,
|
|
245
|
+
labels=labels,
|
|
246
|
+
vocab_size=self.config.vocab_size,
|
|
247
|
+
**loss_kwargs,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
return CausalLMOutputWithPast(
|
|
251
|
+
loss=loss,
|
|
252
|
+
logits=logits,
|
|
253
|
+
past_key_values=outputs.past_key_values,
|
|
254
|
+
hidden_states=outputs.hidden_states,
|
|
255
|
+
attentions=outputs.attentions,
|
|
256
|
+
)
|
|
@@ -80,6 +80,7 @@ def lce_forward(
|
|
|
80
80
|
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
81
81
|
"The image shows a street scene with a red stop sign in the foreground. In the background, there is a large red gate with Chinese characters ..."
|
|
82
82
|
```"""
|
|
83
|
+
# FIXME: The code is outdated and not compatible with transformer >= 4.46.1
|
|
83
84
|
|
|
84
85
|
output_attentions = (
|
|
85
86
|
output_attentions
|
|
@@ -11,14 +11,26 @@ from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss
|
|
|
11
11
|
from liger_kernel.transformers.geglu import LigerGEGLUMLP
|
|
12
12
|
from liger_kernel.transformers.layer_norm import LigerLayerNorm
|
|
13
13
|
from liger_kernel.transformers.model.gemma import lce_forward as gemma_lce_forward
|
|
14
|
+
from liger_kernel.transformers.model.gemma import (
|
|
15
|
+
lce_forward_deprecated as gemma_lce_forward_deprecated,
|
|
16
|
+
)
|
|
14
17
|
from liger_kernel.transformers.model.llama import lce_forward as llama_lce_forward
|
|
15
18
|
from liger_kernel.transformers.model.llama import (
|
|
16
19
|
lce_forward_deprecated as llama_lce_forward_deprecated,
|
|
17
20
|
)
|
|
18
21
|
from liger_kernel.transformers.model.mistral import lce_forward as mistral_lce_forward
|
|
19
22
|
from liger_kernel.transformers.model.mixtral import lce_forward as mixtral_lce_forward
|
|
23
|
+
from liger_kernel.transformers.model.mixtral import (
|
|
24
|
+
lce_forward_deprecated as mixtral_lce_forward_deprecated,
|
|
25
|
+
)
|
|
20
26
|
from liger_kernel.transformers.model.phi3 import lce_forward as phi3_lce_forward
|
|
27
|
+
from liger_kernel.transformers.model.phi3 import (
|
|
28
|
+
lce_forward_deprecated as phi3_lce_forward_deprecated,
|
|
29
|
+
)
|
|
21
30
|
from liger_kernel.transformers.model.qwen2 import lce_forward as qwen2_lce_forward
|
|
31
|
+
from liger_kernel.transformers.model.qwen2 import (
|
|
32
|
+
lce_forward_deprecated as qwen2_lce_forward_deprecated,
|
|
33
|
+
)
|
|
22
34
|
from liger_kernel.transformers.rms_norm import LigerRMSNorm
|
|
23
35
|
from liger_kernel.transformers.rope import liger_rotary_pos_emb
|
|
24
36
|
from liger_kernel.transformers.swiglu import (
|
|
@@ -30,6 +42,8 @@ from liger_kernel.transformers.swiglu import (
|
|
|
30
42
|
transformer_version = version.parse(transformers.__version__)
|
|
31
43
|
|
|
32
44
|
logger = logging.getLogger(__name__)
|
|
45
|
+
SUPPORTED_TRANSFORMER_VERSION = "4.46.1"
|
|
46
|
+
TRANSFORMER_DEPRECATION_WARNING = "Support for transformers versions < 4.46.1 will soon be discontinued due to issues with incorrect gradient accumulation. \n Please consider upgrading to avoid potential issues. See details: https://github.com/huggingface/transformers/pull/34191"
|
|
33
47
|
|
|
34
48
|
|
|
35
49
|
def _bind_method_to_module(module, method_name: str, new_method: Callable):
|
|
@@ -95,13 +109,10 @@ def apply_liger_kernel_to_llama(
|
|
|
95
109
|
if cross_entropy:
|
|
96
110
|
modeling_llama.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
97
111
|
if fused_linear_cross_entropy:
|
|
98
|
-
if transformer_version >= version.parse(
|
|
112
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
99
113
|
modeling_llama.LlamaForCausalLM.forward = llama_lce_forward
|
|
100
|
-
else: # if version < 4.46.
|
|
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
|
-
)
|
|
114
|
+
else: # if version < 4.46.1
|
|
115
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
105
116
|
modeling_llama.LlamaForCausalLM.forward = llama_lce_forward_deprecated
|
|
106
117
|
|
|
107
118
|
if model is not None:
|
|
@@ -170,6 +181,9 @@ def apply_liger_kernel_to_mllama(
|
|
|
170
181
|
)
|
|
171
182
|
|
|
172
183
|
from liger_kernel.transformers.model.mllama import lce_forward as mllama_lce_forward
|
|
184
|
+
from liger_kernel.transformers.model.mllama import (
|
|
185
|
+
lce_forward_deprecated as mllama_lce_forward_deprecated,
|
|
186
|
+
)
|
|
173
187
|
|
|
174
188
|
if rope:
|
|
175
189
|
modeling_mllama.apply_rotary_pos_emb = liger_rotary_pos_emb
|
|
@@ -182,9 +196,11 @@ def apply_liger_kernel_to_mllama(
|
|
|
182
196
|
if cross_entropy:
|
|
183
197
|
modeling_mllama.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
184
198
|
if fused_linear_cross_entropy:
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
199
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
200
|
+
modeling_mllama.MllamaForCausalLM.forward = mllama_lce_forward
|
|
201
|
+
else: # if version < 4.46.1
|
|
202
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
203
|
+
modeling_mllama.MllamaForCausalLM.forward = mllama_lce_forward_deprecated
|
|
188
204
|
|
|
189
205
|
if model is not None:
|
|
190
206
|
# The model instance already exists, so we need to additionally patch the
|
|
@@ -332,7 +348,11 @@ def apply_liger_kernel_to_mixtral(
|
|
|
332
348
|
if cross_entropy:
|
|
333
349
|
modeling_mixtral.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
334
350
|
if fused_linear_cross_entropy:
|
|
335
|
-
|
|
351
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
352
|
+
modeling_mixtral.MixtralForCausalLM.forward = mixtral_lce_forward
|
|
353
|
+
else: # if version < 4.46.1
|
|
354
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
355
|
+
modeling_mixtral.MixtralForCausalLM.forward = mixtral_lce_forward_deprecated
|
|
336
356
|
if swiglu:
|
|
337
357
|
modeling_mixtral.MixtralBlockSparseTop2MLP = LigerBlockSparseTop2MLP
|
|
338
358
|
|
|
@@ -408,7 +428,11 @@ def apply_liger_kernel_to_gemma(
|
|
|
408
428
|
if geglu:
|
|
409
429
|
modeling_gemma.GemmaMLP = LigerGEGLUMLP
|
|
410
430
|
if fused_linear_cross_entropy:
|
|
411
|
-
|
|
431
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
432
|
+
modeling_gemma.GemmaForCausalLM.forward = gemma_lce_forward
|
|
433
|
+
else: # if version < 4.46.1
|
|
434
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
435
|
+
modeling_gemma.GemmaForCausalLM.forward = gemma_lce_forward_deprecated
|
|
412
436
|
|
|
413
437
|
if model is not None:
|
|
414
438
|
# The model instance already exists, so we need to additionally patch the
|
|
@@ -539,8 +563,16 @@ def apply_liger_kernel_to_qwen2(
|
|
|
539
563
|
modeling_qwen2.Qwen2RMSNorm = LigerRMSNorm
|
|
540
564
|
if cross_entropy:
|
|
541
565
|
modeling_qwen2.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
566
|
+
|
|
567
|
+
# import pdb; pdb.set_trace()
|
|
542
568
|
if fused_linear_cross_entropy:
|
|
543
|
-
|
|
569
|
+
|
|
570
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
571
|
+
modeling_qwen2.Qwen2ForCausalLM.forward = qwen2_lce_forward
|
|
572
|
+
else: # if version < 4.46.1
|
|
573
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
574
|
+
modeling_qwen2.Qwen2ForCausalLM.forward = qwen2_lce_forward_deprecated
|
|
575
|
+
|
|
544
576
|
if swiglu:
|
|
545
577
|
modeling_qwen2.Qwen2MLP = LigerSwiGLUMLP
|
|
546
578
|
|
|
@@ -566,6 +598,7 @@ def apply_liger_kernel_to_qwen2(
|
|
|
566
598
|
if rms_norm:
|
|
567
599
|
_patch_rms_norm_module(decoder_layer.input_layernorm)
|
|
568
600
|
_patch_rms_norm_module(decoder_layer.post_attention_layernorm)
|
|
601
|
+
print("Applied Liger kernels to Qwen2")
|
|
569
602
|
|
|
570
603
|
|
|
571
604
|
def apply_liger_kernel_to_qwen2_vl(
|
|
@@ -684,7 +717,11 @@ def apply_liger_kernel_to_phi3(
|
|
|
684
717
|
if cross_entropy:
|
|
685
718
|
modeling_phi3.CrossEntropyLoss = LigerCrossEntropyLoss
|
|
686
719
|
if fused_linear_cross_entropy:
|
|
687
|
-
|
|
720
|
+
if transformer_version >= version.parse(SUPPORTED_TRANSFORMER_VERSION):
|
|
721
|
+
modeling_phi3.Phi3ForCausalLM.forward = phi3_lce_forward
|
|
722
|
+
else: # if version < 4.46.1
|
|
723
|
+
logger.warning(TRANSFORMER_DEPRECATION_WARNING)
|
|
724
|
+
modeling_phi3.Phi3ForCausalLM.forward = phi3_lce_forward_deprecated
|
|
688
725
|
|
|
689
726
|
if model is not None:
|
|
690
727
|
# The model instance already exists, so we need to additionally patch the
|
|
@@ -23,26 +23,26 @@ 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=qetRIZmdHIDxE0TtWP5-rWS91NuGgRYRZBTqzJUojkI,35507
|
|
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
|
|
30
30
|
liger_kernel/transformers/trainer_integration.py,sha256=W3ON51O5GkyzNJsItz0y5rKx-uy2f2cFfveZpqbUdhw,123
|
|
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
|
-
liger_kernel/transformers/model/gemma.py,sha256=
|
|
33
|
+
liger_kernel/transformers/model/gemma.py,sha256=R4huxuR48gkLrdT8KqV7As2v9dZtEmcGVz6YG1ZmuJE,9692
|
|
34
34
|
liger_kernel/transformers/model/llama.py,sha256=RinsgC_eR-YNvZd2SHPQxZ4eyR3uViaTFCM3SvI5nks,10426
|
|
35
|
-
liger_kernel/transformers/model/mistral.py,sha256=
|
|
36
|
-
liger_kernel/transformers/model/mixtral.py,sha256=
|
|
37
|
-
liger_kernel/transformers/model/mllama.py,sha256=
|
|
38
|
-
liger_kernel/transformers/model/phi3.py,sha256=
|
|
39
|
-
liger_kernel/transformers/model/qwen2.py,sha256=
|
|
40
|
-
liger_kernel/transformers/model/qwen2_vl.py,sha256=
|
|
35
|
+
liger_kernel/transformers/model/mistral.py,sha256=XpL1rlWg_llvW3z_Hf_d8WQs7uQaH4ds7EZ2SxjQHsU,5144
|
|
36
|
+
liger_kernel/transformers/model/mixtral.py,sha256=nyDS1dBpsOXYC2DuW59Hgu7ZrGftrHuWPfNqjcNPIxs,11503
|
|
37
|
+
liger_kernel/transformers/model/mllama.py,sha256=mesNCgj0Ea1O-fqRD4LVxDJ1CR2abY_zAzK_bfVzkiU,11222
|
|
38
|
+
liger_kernel/transformers/model/phi3.py,sha256=xUZPlaPKwknLjHc3uUW3EPodm1h0vD3G7Qnhh51v-Io,10332
|
|
39
|
+
liger_kernel/transformers/model/qwen2.py,sha256=EyhSSzQOskGjSnCsKMZpd1s5IAIlHd5PBO3q0MoCs00,9619
|
|
40
|
+
liger_kernel/transformers/model/qwen2_vl.py,sha256=j6xAhp9AG195dsZK5f8dFYVM9uKtWApZrggT5Y08jn4,7055
|
|
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.
|
|
43
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/LICENSE,sha256=OhzLDHJ0to4a8sodVLELZiCFylZ1NAAYLs-HrjPy0ag,1312
|
|
44
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/METADATA,sha256=vjm2XnEHcuQZqnTLDZWT6_wIWnV1kKGlRkvbCF2roTY,27720
|
|
45
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/NOTICE,sha256=njwnoPZLh9AN8SJQzxvCGLHi-8X__AvWRze6joNXIY8,2066
|
|
46
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
47
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/top_level.txt,sha256=2eghu4hA3LnkM7ElW92tQ8zegWKgSbeo-k-aGe1YnvY,13
|
|
48
|
+
liger_kernel_nightly-0.3.1.dev20241105220546.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|