liger-kernel-nightly 0.5.5.dev20250402185702__py3-none-any.whl → 0.6.4.dev20260112233432__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/chunked_loss/__init__.py +1 -0
- liger_kernel/chunked_loss/cosine_similarity_loss.py +142 -0
- liger_kernel/chunked_loss/dpo_loss.py +61 -3
- liger_kernel/chunked_loss/functional.py +2 -0
- liger_kernel/chunked_loss/fused_linear_distillation.py +23 -5
- liger_kernel/chunked_loss/fused_linear_ppo.py +36 -0
- liger_kernel/chunked_loss/fused_linear_preference.py +0 -1
- liger_kernel/chunked_loss/grpo_loss.py +76 -5
- liger_kernel/chunked_loss/jsd_loss.py +46 -15
- liger_kernel/ops/__init__.py +141 -0
- liger_kernel/ops/backends/README.md +151 -0
- liger_kernel/ops/backends/__init__.py +13 -0
- liger_kernel/ops/backends/_ascend/__init__.py +5 -0
- liger_kernel/ops/backends/_ascend/ascend-ub-manager-design.md +485 -0
- liger_kernel/ops/backends/_ascend/ops/__init__.py +49 -0
- liger_kernel/ops/backends/_ascend/ops/geglu.py +266 -0
- liger_kernel/ops/backends/_ascend/ops/qwen2vl_mrope.py +285 -0
- liger_kernel/ops/backends/_ascend/ops/rope.py +290 -0
- liger_kernel/ops/backends/_ascend/ops/swiglu.py +142 -0
- liger_kernel/ops/backends/_ascend/ops/tvd.py +221 -0
- liger_kernel/ops/backends/_ascend/ub_manager.py +349 -0
- liger_kernel/ops/backends/registry.py +61 -0
- liger_kernel/ops/cross_entropy.py +134 -65
- liger_kernel/ops/dyt.py +115 -180
- liger_kernel/ops/fused_add_rms_norm.py +416 -0
- liger_kernel/ops/fused_linear_cross_entropy.py +117 -23
- liger_kernel/ops/fused_neighborhood_attention.py +1022 -0
- liger_kernel/ops/geglu.py +6 -4
- liger_kernel/ops/group_norm.py +7 -7
- liger_kernel/ops/grpo_loss.py +312 -0
- liger_kernel/ops/jsd.py +2 -1
- liger_kernel/ops/kl_div.py +9 -5
- liger_kernel/ops/layer_norm.py +146 -78
- liger_kernel/ops/llama4_rope.py +225 -0
- liger_kernel/ops/multi_token_attention.py +207 -0
- liger_kernel/ops/poly_norm.py +390 -0
- liger_kernel/ops/rms_norm.py +398 -99
- liger_kernel/ops/rope.py +1 -1
- liger_kernel/ops/softmax.py +201 -0
- liger_kernel/ops/sparsemax.py +179 -0
- liger_kernel/ops/swiglu.py +1 -1
- liger_kernel/ops/tiled_mlp.py +136 -0
- liger_kernel/ops/utils.py +14 -0
- liger_kernel/transformers/__init__.py +208 -17
- liger_kernel/transformers/auto_model.py +21 -0
- liger_kernel/transformers/cross_entropy.py +9 -4
- liger_kernel/transformers/dyt.py +6 -4
- liger_kernel/transformers/experimental/__init__.py +5 -0
- liger_kernel/transformers/experimental/embedding.py +1 -1
- liger_kernel/transformers/fsdp.py +55 -0
- liger_kernel/transformers/functional.py +122 -20
- liger_kernel/transformers/fused_add_rms_norm.py +39 -0
- liger_kernel/transformers/fused_linear_cross_entropy.py +16 -5
- liger_kernel/transformers/fused_linear_jsd.py +1 -1
- liger_kernel/transformers/fused_neighborhood_attention.py +234 -0
- liger_kernel/transformers/geglu.py +1 -1
- liger_kernel/transformers/group_norm.py +1 -1
- liger_kernel/transformers/grpo_loss.py +153 -0
- liger_kernel/transformers/jsd.py +1 -1
- liger_kernel/transformers/kl_div.py +1 -1
- liger_kernel/transformers/layer_norm.py +1 -1
- liger_kernel/transformers/llama4_rope.py +93 -0
- liger_kernel/transformers/model/exaone4.py +136 -0
- liger_kernel/transformers/model/falcon_h1.py +122 -0
- liger_kernel/transformers/model/gemma.py +57 -27
- liger_kernel/transformers/model/gemma2.py +65 -28
- liger_kernel/transformers/model/gemma3.py +331 -0
- liger_kernel/transformers/model/glm4.py +141 -0
- liger_kernel/transformers/model/glm4v.py +163 -0
- liger_kernel/transformers/model/glm4v_moe.py +172 -0
- liger_kernel/transformers/model/gpt_oss.py +211 -0
- liger_kernel/transformers/model/hunyuan_v1.py +134 -0
- liger_kernel/transformers/model/internvl.py +157 -0
- liger_kernel/transformers/model/llama.py +109 -27
- liger_kernel/transformers/model/llama4.py +121 -0
- liger_kernel/transformers/model/llava.py +111 -136
- liger_kernel/transformers/model/loss_utils.py +50 -12
- liger_kernel/transformers/model/mistral.py +51 -34
- liger_kernel/transformers/model/mixtral.py +50 -29
- liger_kernel/transformers/model/mllama.py +46 -24
- liger_kernel/transformers/model/olmo2.py +47 -22
- liger_kernel/transformers/model/olmo3.py +142 -0
- liger_kernel/transformers/model/output_classes.py +147 -0
- liger_kernel/transformers/model/paligemma.py +50 -14
- liger_kernel/transformers/model/phi3.py +47 -172
- liger_kernel/transformers/model/qwen2.py +55 -23
- liger_kernel/transformers/model/qwen2_5_vl.py +62 -103
- liger_kernel/transformers/model/qwen2_vl.py +59 -108
- liger_kernel/transformers/model/qwen3.py +136 -0
- liger_kernel/transformers/model/qwen3_moe.py +152 -0
- liger_kernel/transformers/model/qwen3_next.py +146 -0
- liger_kernel/transformers/model/qwen3_vl.py +150 -0
- liger_kernel/transformers/model/qwen3_vl_moe.py +126 -0
- liger_kernel/transformers/model/smollm3.py +199 -0
- liger_kernel/transformers/model/smolvlm.py +158 -0
- liger_kernel/transformers/monkey_patch.py +2018 -244
- liger_kernel/transformers/multi_token_attention.py +64 -0
- liger_kernel/transformers/poly_norm.py +42 -0
- liger_kernel/transformers/qwen2vl_mrope.py +1 -1
- liger_kernel/transformers/rms_norm.py +54 -6
- liger_kernel/transformers/rope.py +45 -1
- liger_kernel/transformers/softmax.py +12 -0
- liger_kernel/transformers/sparsemax.py +16 -0
- liger_kernel/transformers/swiglu.py +39 -1
- liger_kernel/transformers/tiled_mlp.py +125 -0
- liger_kernel/transformers/trainer/orpo_trainer.py +1 -53
- liger_kernel/transformers/tvd.py +1 -1
- liger_kernel/utils.py +63 -0
- {liger_kernel_nightly-0.5.5.dev20250402185702.dist-info → liger_kernel_nightly-0.6.4.dev20260112233432.dist-info}/METADATA +73 -39
- liger_kernel_nightly-0.6.4.dev20260112233432.dist-info/RECORD +132 -0
- liger_kernel_nightly-0.5.5.dev20250402185702.dist-info/RECORD +0 -80
- {liger_kernel_nightly-0.5.5.dev20250402185702.dist-info → liger_kernel_nightly-0.6.4.dev20260112233432.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.5.5.dev20250402185702.dist-info → liger_kernel_nightly-0.6.4.dev20260112233432.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.5.5.dev20250402185702.dist-info → liger_kernel_nightly-0.6.4.dev20260112233432.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.5.5.dev20250402185702.dist-info → liger_kernel_nightly-0.6.4.dev20260112233432.dist-info}/top_level.txt +0 -0
|
@@ -7,22 +7,25 @@ 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
|
-
from transformers.
|
|
13
|
-
from transformers.models.llama.modeling_llama import LLAMA_INPUTS_DOCSTRING
|
|
14
|
-
from transformers.utils import add_start_docstrings_to_model_forward
|
|
15
|
-
from transformers.utils import replace_return_docstrings
|
|
13
|
+
from transformers.utils.deprecation import deprecate_kwarg
|
|
16
14
|
|
|
15
|
+
from liger_kernel.transformers.fsdp import _FSDPForwardRedirection
|
|
17
16
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
18
17
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
18
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
19
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
20
|
+
from liger_kernel.utils import PEFT_AVAILABLE
|
|
19
21
|
|
|
20
22
|
if TYPE_CHECKING:
|
|
21
23
|
from transformers.cache_utils import Cache
|
|
22
24
|
|
|
25
|
+
if PEFT_AVAILABLE:
|
|
26
|
+
from peft.utils.other import ModulesToSaveWrapper
|
|
27
|
+
|
|
23
28
|
|
|
24
|
-
@add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
|
|
25
|
-
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
|
26
29
|
def lce_forward_deprecated(
|
|
27
30
|
self,
|
|
28
31
|
input_ids: torch.LongTensor = None,
|
|
@@ -36,6 +39,7 @@ def lce_forward_deprecated(
|
|
|
36
39
|
output_hidden_states: Optional[bool] = None,
|
|
37
40
|
return_dict: Optional[bool] = None,
|
|
38
41
|
cache_position: Optional[torch.LongTensor] = None,
|
|
42
|
+
skip_logits: Optional[bool] = None,
|
|
39
43
|
) -> Union[Tuple, CausalLMOutputWithPast]:
|
|
40
44
|
r"""
|
|
41
45
|
Copy paste llama forward but replace torch cross entropy with liger fused linear cross entropy
|
|
@@ -90,7 +94,15 @@ def lce_forward_deprecated(
|
|
|
90
94
|
loss = None
|
|
91
95
|
logits = None
|
|
92
96
|
|
|
93
|
-
if
|
|
97
|
+
# if in training mode, don't materialize logits
|
|
98
|
+
if skip_logits and labels is None:
|
|
99
|
+
raise ValueError("skip_logits is True, but labels is None")
|
|
100
|
+
|
|
101
|
+
if skip_logits is None:
|
|
102
|
+
# By default, if in training mode, don't materialize logits
|
|
103
|
+
skip_logits = self.training and labels is not None
|
|
104
|
+
|
|
105
|
+
if skip_logits:
|
|
94
106
|
shift_hidden_states = hidden_states[..., :-1, :].contiguous()
|
|
95
107
|
shift_labels = labels[..., 1:].contiguous()
|
|
96
108
|
|
|
@@ -135,8 +147,7 @@ def lce_forward_deprecated(
|
|
|
135
147
|
)
|
|
136
148
|
|
|
137
149
|
|
|
138
|
-
@
|
|
139
|
-
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
|
150
|
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
140
151
|
def lce_forward(
|
|
141
152
|
self,
|
|
142
153
|
input_ids: torch.LongTensor = None,
|
|
@@ -150,9 +161,10 @@ def lce_forward(
|
|
|
150
161
|
output_hidden_states: Optional[bool] = None,
|
|
151
162
|
return_dict: Optional[bool] = None,
|
|
152
163
|
cache_position: Optional[torch.LongTensor] = None,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
165
|
+
skip_logits: Optional[bool] = None,
|
|
166
|
+
**kwargs,
|
|
167
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
156
168
|
r"""
|
|
157
169
|
Args:
|
|
158
170
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -160,10 +172,12 @@ def lce_forward(
|
|
|
160
172
|
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
161
173
|
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
162
174
|
|
|
163
|
-
|
|
164
|
-
|
|
175
|
+
logits_to_keep (`int` or `torch.Tensor`, *optional*):
|
|
176
|
+
If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
|
|
165
177
|
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
166
178
|
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
179
|
+
If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
|
|
180
|
+
This is useful when using packed tensor format (single dimension for batch and sequence length).
|
|
167
181
|
|
|
168
182
|
Returns:
|
|
169
183
|
|
|
@@ -202,43 +216,111 @@ def lce_forward(
|
|
|
202
216
|
output_hidden_states=output_hidden_states,
|
|
203
217
|
return_dict=return_dict,
|
|
204
218
|
cache_position=cache_position,
|
|
219
|
+
**kwargs,
|
|
205
220
|
)
|
|
206
221
|
|
|
207
222
|
hidden_states = outputs[0]
|
|
223
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
224
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
225
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
208
226
|
|
|
209
227
|
if self.config.pretraining_tp > 1:
|
|
210
228
|
raise Exception("Liger Kernel does not support pretraining_tp!!")
|
|
211
229
|
|
|
230
|
+
shift_labels = kwargs.pop("shift_labels", None)
|
|
212
231
|
logits = None
|
|
213
232
|
loss = None
|
|
233
|
+
token_accuracy = None
|
|
234
|
+
|
|
214
235
|
# if in training mode, don't materialize logits
|
|
215
|
-
if
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
236
|
+
if skip_logits and labels is None and shift_labels is None:
|
|
237
|
+
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
238
|
+
|
|
239
|
+
if skip_logits is None:
|
|
240
|
+
# By default, if in training mode, don't materialize logits
|
|
241
|
+
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
242
|
+
|
|
243
|
+
# Compute loss
|
|
244
|
+
if skip_logits:
|
|
245
|
+
result = lce_maybe_trainable_lm_head(
|
|
246
|
+
self,
|
|
247
|
+
hidden_states=kept_hidden_states,
|
|
220
248
|
hidden_size=self.config.hidden_size,
|
|
221
|
-
|
|
249
|
+
labels=labels,
|
|
250
|
+
shift_labels=shift_labels,
|
|
251
|
+
**kwargs,
|
|
222
252
|
)
|
|
223
|
-
|
|
224
|
-
else:
|
|
225
|
-
logits = self.lm_head(
|
|
226
|
-
if labels is not None:
|
|
253
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
254
|
+
else:
|
|
255
|
+
logits = self.lm_head(kept_hidden_states)
|
|
256
|
+
if labels is not None or shift_labels is not None:
|
|
227
257
|
loss = self.loss_function(
|
|
228
258
|
logits=logits,
|
|
229
259
|
labels=labels,
|
|
260
|
+
shift_labels=shift_labels,
|
|
230
261
|
vocab_size=self.config.vocab_size,
|
|
231
|
-
**
|
|
262
|
+
**kwargs,
|
|
232
263
|
)
|
|
233
264
|
|
|
234
265
|
if not return_dict:
|
|
235
266
|
output = (logits,) + outputs[1:]
|
|
236
|
-
|
|
267
|
+
output = ((loss,) + output) if loss is not None else output
|
|
268
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
269
|
+
return output
|
|
237
270
|
|
|
238
|
-
|
|
271
|
+
# Return custom output class with token_accuracy field
|
|
272
|
+
return LigerCausalLMOutputWithPast(
|
|
239
273
|
loss=loss,
|
|
240
274
|
logits=logits,
|
|
241
275
|
past_key_values=outputs.past_key_values,
|
|
242
276
|
hidden_states=outputs.hidden_states,
|
|
243
277
|
attentions=outputs.attentions,
|
|
278
|
+
token_accuracy=token_accuracy,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def lce_maybe_trainable_lm_head(self, hidden_states, hidden_size, labels, shift_labels, **loss_kwargs):
|
|
283
|
+
lm_head = self.lm_head
|
|
284
|
+
|
|
285
|
+
# Unwrap the module if lm_head has been added as trainable module in PEFT LoRA configuration,
|
|
286
|
+
# i.e. listed in the modules_to_save field of LoraConfig, so the lm_head weights are read
|
|
287
|
+
# from the unwrapped module.
|
|
288
|
+
# See https://huggingface.co/docs/peft/package_reference/lora for reference.
|
|
289
|
+
if PEFT_AVAILABLE and isinstance(lm_head, ModulesToSaveWrapper):
|
|
290
|
+
lm_head = lm_head.modules_to_save.default
|
|
291
|
+
|
|
292
|
+
# If FSDP is used and lm_head is trainable, e.g., during full fine-tuning or with LoRA,
|
|
293
|
+
# reading the lm_head module weights and calling the kernel must be done within FSDP forward pass
|
|
294
|
+
# so the module entire parameters are summoned and kept in memory during the kernel execution.
|
|
295
|
+
if isinstance(lm_head, FullyShardedDataParallel):
|
|
296
|
+
return _FSDPForwardRedirection()(
|
|
297
|
+
lm_head,
|
|
298
|
+
_liger_for_causal_lm_loss,
|
|
299
|
+
lm_head.module,
|
|
300
|
+
hidden_states,
|
|
301
|
+
hidden_size,
|
|
302
|
+
labels,
|
|
303
|
+
shift_labels,
|
|
304
|
+
**loss_kwargs,
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
# FSDP is not used so we can read the lm_head weights and call the kernel directly
|
|
308
|
+
return _liger_for_causal_lm_loss(
|
|
309
|
+
lm_head=self.lm_head,
|
|
310
|
+
hidden_states=hidden_states,
|
|
311
|
+
hidden_size=hidden_size,
|
|
312
|
+
labels=labels,
|
|
313
|
+
shift_labels=shift_labels,
|
|
314
|
+
**loss_kwargs,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _liger_for_causal_lm_loss(lm_head, hidden_states, hidden_size, labels, shift_labels, **loss_kwargs):
|
|
319
|
+
return LigerForCausalLMLoss(
|
|
320
|
+
hidden_states=hidden_states,
|
|
321
|
+
lm_head_weight=lm_head.weight,
|
|
322
|
+
labels=labels,
|
|
323
|
+
hidden_size=hidden_size,
|
|
324
|
+
shift_labels=shift_labels,
|
|
325
|
+
**loss_kwargs,
|
|
244
326
|
)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
from typing import Optional
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
from typing import Union
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from transformers.cache_utils import Cache
|
|
9
|
+
|
|
10
|
+
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
11
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
12
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def lce_forward(
|
|
16
|
+
self,
|
|
17
|
+
input_ids: torch.LongTensor = None,
|
|
18
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
19
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
20
|
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
|
21
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
22
|
+
labels: Optional[torch.LongTensor] = None,
|
|
23
|
+
use_cache: Optional[bool] = None,
|
|
24
|
+
output_attentions: Optional[bool] = None,
|
|
25
|
+
output_hidden_states: Optional[bool] = None,
|
|
26
|
+
return_dict: Optional[bool] = None,
|
|
27
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
28
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
29
|
+
**kwargs,
|
|
30
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
31
|
+
r"""
|
|
32
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
33
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
34
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
35
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
36
|
+
|
|
37
|
+
Example:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
>>> from transformers import AutoTokenizer, Llama4ForCausalLM
|
|
41
|
+
|
|
42
|
+
>>> model = Llama4ForCausalLM.from_pretrained("meta-llama4/Llama4-2-7b-hf")
|
|
43
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("meta-llama4/Llama4-2-7b-hf")
|
|
44
|
+
|
|
45
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
|
46
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
47
|
+
|
|
48
|
+
>>> # Generate
|
|
49
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
50
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
51
|
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
|
52
|
+
```"""
|
|
53
|
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
|
54
|
+
output_hidden_states = (
|
|
55
|
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
56
|
+
)
|
|
57
|
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
|
58
|
+
|
|
59
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
60
|
+
outputs = self.model(
|
|
61
|
+
input_ids=input_ids,
|
|
62
|
+
attention_mask=attention_mask,
|
|
63
|
+
position_ids=position_ids,
|
|
64
|
+
past_key_values=past_key_values,
|
|
65
|
+
inputs_embeds=inputs_embeds,
|
|
66
|
+
use_cache=use_cache,
|
|
67
|
+
output_attentions=output_attentions,
|
|
68
|
+
output_hidden_states=output_hidden_states,
|
|
69
|
+
return_dict=True,
|
|
70
|
+
cache_position=cache_position,
|
|
71
|
+
**kwargs,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
hidden_states = outputs[0]
|
|
75
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
76
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
77
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
78
|
+
|
|
79
|
+
shift_labels = kwargs.pop("shift_labels", None)
|
|
80
|
+
logits = None
|
|
81
|
+
loss = None
|
|
82
|
+
token_accuracy = None
|
|
83
|
+
|
|
84
|
+
# Compute loss
|
|
85
|
+
if self.training and (labels is not None or shift_labels is not None):
|
|
86
|
+
result = LigerForCausalLMLoss(
|
|
87
|
+
hidden_states=kept_hidden_states,
|
|
88
|
+
lm_head_weight=self.lm_head.weight,
|
|
89
|
+
labels=labels,
|
|
90
|
+
shift_labels=shift_labels,
|
|
91
|
+
hidden_size=self.config.hidden_size,
|
|
92
|
+
**kwargs,
|
|
93
|
+
)
|
|
94
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
95
|
+
|
|
96
|
+
else: # if in inference mode materialize logits
|
|
97
|
+
logits = self.lm_head(kept_hidden_states)
|
|
98
|
+
if labels is not None or shift_labels is not None:
|
|
99
|
+
loss = self.loss_function(
|
|
100
|
+
logits=logits,
|
|
101
|
+
labels=labels,
|
|
102
|
+
shift_labels=shift_labels,
|
|
103
|
+
vocab_size=self.config.vocab_size,
|
|
104
|
+
**kwargs,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
if not return_dict:
|
|
108
|
+
output = (logits,) + outputs[1:]
|
|
109
|
+
output = ((loss,) + output) if loss is not None else output
|
|
110
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
111
|
+
return output
|
|
112
|
+
|
|
113
|
+
# Return custom output class with token_accuracy field
|
|
114
|
+
return LigerCausalLMOutputWithPast(
|
|
115
|
+
loss=loss,
|
|
116
|
+
logits=logits,
|
|
117
|
+
past_key_values=outputs.past_key_values,
|
|
118
|
+
hidden_states=outputs.hidden_states,
|
|
119
|
+
attentions=outputs.attentions,
|
|
120
|
+
token_accuracy=token_accuracy,
|
|
121
|
+
)
|