liger-kernel-nightly 0.6.2.dev20250919191028__py3-none-any.whl → 0.6.4.dev20251202054858__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/cosine_similarity_loss.py +13 -4
- liger_kernel/chunked_loss/fused_linear_distillation.py +13 -2
- liger_kernel/chunked_loss/fused_linear_ppo.py +21 -5
- liger_kernel/chunked_loss/grpo_loss.py +8 -5
- liger_kernel/chunked_loss/jsd_loss.py +18 -5
- liger_kernel/ops/cross_entropy.py +120 -63
- liger_kernel/ops/dyt.py +5 -2
- liger_kernel/ops/fused_add_rms_norm.py +5 -1
- liger_kernel/ops/fused_linear_cross_entropy.py +43 -12
- liger_kernel/ops/geglu.py +2 -1
- liger_kernel/ops/group_norm.py +2 -1
- liger_kernel/ops/grpo_loss.py +3 -1
- liger_kernel/ops/layer_norm.py +88 -70
- liger_kernel/ops/poly_norm.py +390 -0
- liger_kernel/ops/rms_norm.py +7 -2
- liger_kernel/ops/tiled_mlp.py +136 -0
- liger_kernel/ops/utils.py +2 -0
- liger_kernel/transformers/__init__.py +33 -0
- liger_kernel/transformers/cross_entropy.py +8 -3
- liger_kernel/transformers/functional.py +29 -6
- liger_kernel/transformers/fused_linear_cross_entropy.py +8 -3
- liger_kernel/transformers/grpo_loss.py +56 -1
- liger_kernel/transformers/model/falcon_h1.py +122 -0
- liger_kernel/transformers/model/gemma.py +19 -7
- liger_kernel/transformers/model/gemma2.py +22 -7
- liger_kernel/transformers/model/gemma3.py +52 -14
- liger_kernel/transformers/model/glm4.py +18 -5
- liger_kernel/transformers/model/glm4v.py +18 -5
- liger_kernel/transformers/model/glm4v_moe.py +25 -5
- liger_kernel/transformers/model/hunyuan_v1.py +134 -0
- liger_kernel/transformers/model/internvl.py +157 -0
- liger_kernel/transformers/model/llama.py +16 -6
- liger_kernel/transformers/model/llama4.py +18 -5
- liger_kernel/transformers/model/llava.py +18 -6
- liger_kernel/transformers/model/loss_utils.py +31 -3
- liger_kernel/transformers/model/mistral.py +17 -7
- liger_kernel/transformers/model/mixtral.py +24 -9
- liger_kernel/transformers/model/mllama.py +14 -5
- liger_kernel/transformers/model/olmo2.py +18 -5
- liger_kernel/transformers/model/olmo3.py +142 -0
- liger_kernel/transformers/model/output_classes.py +147 -0
- liger_kernel/transformers/model/paligemma.py +41 -5
- liger_kernel/transformers/model/phi3.py +16 -8
- liger_kernel/transformers/model/qwen2.py +18 -4
- liger_kernel/transformers/model/qwen2_5_vl.py +21 -8
- liger_kernel/transformers/model/qwen2_vl.py +24 -7
- liger_kernel/transformers/model/qwen3.py +22 -6
- liger_kernel/transformers/model/qwen3_moe.py +27 -7
- 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 +17 -7
- liger_kernel/transformers/model/smolvlm.py +158 -0
- liger_kernel/transformers/monkey_patch.py +729 -4
- liger_kernel/transformers/poly_norm.py +42 -0
- liger_kernel/transformers/rms_norm.py +7 -0
- liger_kernel/transformers/rope.py +43 -0
- liger_kernel/transformers/swiglu.py +17 -0
- liger_kernel/transformers/tiled_mlp.py +133 -0
- liger_kernel/utils.py +25 -0
- {liger_kernel_nightly-0.6.2.dev20250919191028.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/METADATA +13 -6
- liger_kernel_nightly-0.6.4.dev20251202054858.dist-info/RECORD +118 -0
- liger_kernel_nightly-0.6.2.dev20250919191028.dist-info/RECORD +0 -105
- {liger_kernel_nightly-0.6.2.dev20250919191028.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.6.2.dev20250919191028.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.6.2.dev20250919191028.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.6.2.dev20250919191028.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
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.utils import can_return_tuple
|
|
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 LigerInternVLCausalLMOutputWithPast
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Copied from https://github.com/huggingface/transformers/blob/d888bd435d0c0eaabaabad5b33d52af518c7187c/src/transformers/models/internvl/modeling_internvl.py#L862
|
|
16
|
+
@can_return_tuple
|
|
17
|
+
def lce_forward(
|
|
18
|
+
self,
|
|
19
|
+
input_ids: torch.LongTensor = None,
|
|
20
|
+
pixel_values: Optional[torch.FloatTensor] = None,
|
|
21
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
22
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
23
|
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
|
24
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
25
|
+
vision_feature_layer: Optional[Union[int, List[int]]] = None,
|
|
26
|
+
vision_feature_select_strategy: Optional[str] = None,
|
|
27
|
+
labels: Optional[torch.LongTensor] = None,
|
|
28
|
+
use_cache: Optional[bool] = None,
|
|
29
|
+
output_attentions: Optional[bool] = None,
|
|
30
|
+
output_hidden_states: Optional[bool] = None,
|
|
31
|
+
return_dict: Optional[bool] = None,
|
|
32
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
33
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
34
|
+
image_sizes: Optional[torch.Tensor] = None,
|
|
35
|
+
skip_logits: Optional[bool] = None, # Added argument for liger-kernel
|
|
36
|
+
**lm_kwargs, # renamed from kwargs
|
|
37
|
+
) -> Union[Tuple, LigerInternVLCausalLMOutputWithPast]:
|
|
38
|
+
r"""
|
|
39
|
+
Example:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
>>> import torch
|
|
43
|
+
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
44
|
+
|
|
45
|
+
>>> torch_device = "cuda"
|
|
46
|
+
>>> processor = AutoProcessor.from_pretrained("OpenGVLab/InternVL3-1B-hf")
|
|
47
|
+
>>> model = AutoModelForImageTextToText.from_pretrained(
|
|
48
|
+
... "OpenGVLab/InternVL3-1B-hf", dtype=torch.bfloat16, device_map=torch_device
|
|
49
|
+
... )
|
|
50
|
+
|
|
51
|
+
>>> messages = [
|
|
52
|
+
... {
|
|
53
|
+
... "role": "user",
|
|
54
|
+
... "content": [
|
|
55
|
+
... {
|
|
56
|
+
... "type": "image",
|
|
57
|
+
... "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
|
|
58
|
+
... },
|
|
59
|
+
... {
|
|
60
|
+
... "type": "image",
|
|
61
|
+
... "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg",
|
|
62
|
+
... },
|
|
63
|
+
... {"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
|
|
64
|
+
... ],
|
|
65
|
+
... },
|
|
66
|
+
... ]
|
|
67
|
+
|
|
68
|
+
>>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(torch_device)
|
|
69
|
+
>>> generate_ids = model.generate(**inputs, max_new_tokens=200)
|
|
70
|
+
>>> print(processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True))
|
|
71
|
+
The images depict the Statue of Liberty and the Golden Gate Bridge.
|
|
72
|
+
```"""
|
|
73
|
+
|
|
74
|
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
|
75
|
+
output_hidden_states = (
|
|
76
|
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
77
|
+
)
|
|
78
|
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
|
79
|
+
vision_feature_layer = (
|
|
80
|
+
vision_feature_layer if vision_feature_layer is not None else self.config.vision_feature_layer
|
|
81
|
+
)
|
|
82
|
+
vision_feature_select_strategy = (
|
|
83
|
+
vision_feature_select_strategy
|
|
84
|
+
if vision_feature_select_strategy is not None
|
|
85
|
+
else self.config.vision_feature_select_strategy
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
outputs = self.model(
|
|
89
|
+
input_ids=input_ids,
|
|
90
|
+
pixel_values=pixel_values,
|
|
91
|
+
attention_mask=attention_mask,
|
|
92
|
+
position_ids=position_ids,
|
|
93
|
+
past_key_values=past_key_values,
|
|
94
|
+
inputs_embeds=inputs_embeds,
|
|
95
|
+
vision_feature_layer=vision_feature_layer,
|
|
96
|
+
vision_feature_select_strategy=vision_feature_select_strategy,
|
|
97
|
+
use_cache=use_cache,
|
|
98
|
+
output_attentions=output_attentions,
|
|
99
|
+
output_hidden_states=output_hidden_states,
|
|
100
|
+
return_dict=return_dict,
|
|
101
|
+
cache_position=cache_position,
|
|
102
|
+
image_sizes=image_sizes,
|
|
103
|
+
**lm_kwargs,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Copied from llava.py
|
|
107
|
+
hidden_states = outputs[0]
|
|
108
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
109
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
110
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
111
|
+
|
|
112
|
+
shift_labels = lm_kwargs.pop("shift_labels", None)
|
|
113
|
+
logits = None
|
|
114
|
+
loss = None
|
|
115
|
+
token_accuracy = None
|
|
116
|
+
|
|
117
|
+
if skip_logits and labels is None and shift_labels is None:
|
|
118
|
+
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
119
|
+
|
|
120
|
+
if skip_logits is None:
|
|
121
|
+
# By default, if in training mode, don't materialize logits
|
|
122
|
+
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
123
|
+
|
|
124
|
+
if skip_logits:
|
|
125
|
+
result = LigerForCausalLMLoss(
|
|
126
|
+
hidden_states=kept_hidden_states,
|
|
127
|
+
lm_head_weight=self.lm_head.weight,
|
|
128
|
+
labels=labels,
|
|
129
|
+
shift_labels=shift_labels,
|
|
130
|
+
hidden_size=self.config.text_config.hidden_size,
|
|
131
|
+
**lm_kwargs,
|
|
132
|
+
)
|
|
133
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
134
|
+
|
|
135
|
+
else:
|
|
136
|
+
logits = self.lm_head(kept_hidden_states)
|
|
137
|
+
if labels is not None:
|
|
138
|
+
loss = self.loss_function(
|
|
139
|
+
logits=logits, labels=labels, vocab_size=self.config.text_config.vocab_size, **lm_kwargs
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
if not return_dict:
|
|
143
|
+
output = (logits,) + outputs[1:]
|
|
144
|
+
output = (loss,) + output if loss is not None else output
|
|
145
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
146
|
+
return output
|
|
147
|
+
|
|
148
|
+
# Return custom output class with token_accuracy field
|
|
149
|
+
return LigerInternVLCausalLMOutputWithPast(
|
|
150
|
+
loss=loss,
|
|
151
|
+
logits=logits,
|
|
152
|
+
past_key_values=outputs.past_key_values,
|
|
153
|
+
hidden_states=outputs.hidden_states,
|
|
154
|
+
attentions=outputs.attentions,
|
|
155
|
+
image_hidden_states=outputs.image_hidden_states,
|
|
156
|
+
token_accuracy=token_accuracy,
|
|
157
|
+
)
|
|
@@ -15,6 +15,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
15
15
|
from liger_kernel.transformers.fsdp import _FSDPForwardRedirection
|
|
16
16
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
17
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
|
|
18
20
|
from liger_kernel.utils import PEFT_AVAILABLE
|
|
19
21
|
|
|
20
22
|
if TYPE_CHECKING:
|
|
@@ -162,7 +164,7 @@ def lce_forward(
|
|
|
162
164
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
163
165
|
skip_logits: Optional[bool] = None,
|
|
164
166
|
**kwargs,
|
|
165
|
-
) -> Union[Tuple,
|
|
167
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
166
168
|
r"""
|
|
167
169
|
Args:
|
|
168
170
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -228,6 +230,8 @@ def lce_forward(
|
|
|
228
230
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
229
231
|
logits = None
|
|
230
232
|
loss = None
|
|
233
|
+
token_accuracy = None
|
|
234
|
+
|
|
231
235
|
# if in training mode, don't materialize logits
|
|
232
236
|
if skip_logits and labels is None and shift_labels is None:
|
|
233
237
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -236,8 +240,9 @@ def lce_forward(
|
|
|
236
240
|
# By default, if in training mode, don't materialize logits
|
|
237
241
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
238
242
|
|
|
243
|
+
# Compute loss
|
|
239
244
|
if skip_logits:
|
|
240
|
-
|
|
245
|
+
result = lce_maybe_trainable_lm_head(
|
|
241
246
|
self,
|
|
242
247
|
hidden_states=kept_hidden_states,
|
|
243
248
|
hidden_size=self.config.hidden_size,
|
|
@@ -245,27 +250,32 @@ def lce_forward(
|
|
|
245
250
|
shift_labels=shift_labels,
|
|
246
251
|
**kwargs,
|
|
247
252
|
)
|
|
248
|
-
|
|
253
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
249
254
|
else:
|
|
250
255
|
logits = self.lm_head(kept_hidden_states)
|
|
251
|
-
if labels is not None:
|
|
256
|
+
if labels is not None or shift_labels is not None:
|
|
252
257
|
loss = self.loss_function(
|
|
253
258
|
logits=logits,
|
|
254
259
|
labels=labels,
|
|
260
|
+
shift_labels=shift_labels,
|
|
255
261
|
vocab_size=self.config.vocab_size,
|
|
256
262
|
**kwargs,
|
|
257
263
|
)
|
|
258
264
|
|
|
259
265
|
if not return_dict:
|
|
260
266
|
output = (logits,) + outputs[1:]
|
|
261
|
-
|
|
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
|
|
262
270
|
|
|
263
|
-
|
|
271
|
+
# Return custom output class with token_accuracy field
|
|
272
|
+
return LigerCausalLMOutputWithPast(
|
|
264
273
|
loss=loss,
|
|
265
274
|
logits=logits,
|
|
266
275
|
past_key_values=outputs.past_key_values,
|
|
267
276
|
hidden_states=outputs.hidden_states,
|
|
268
277
|
attentions=outputs.attentions,
|
|
278
|
+
token_accuracy=token_accuracy,
|
|
269
279
|
)
|
|
270
280
|
|
|
271
281
|
|
|
@@ -6,9 +6,10 @@ from typing import Union
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
8
|
from transformers.cache_utils import Cache
|
|
9
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
10
9
|
|
|
11
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
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
def lce_forward(
|
|
@@ -26,7 +27,7 @@ def lce_forward(
|
|
|
26
27
|
cache_position: Optional[torch.LongTensor] = None,
|
|
27
28
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
28
29
|
**kwargs,
|
|
29
|
-
) -> Union[Tuple,
|
|
30
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
30
31
|
r"""
|
|
31
32
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
32
33
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
@@ -78,9 +79,11 @@ def lce_forward(
|
|
|
78
79
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
79
80
|
logits = None
|
|
80
81
|
loss = None
|
|
82
|
+
token_accuracy = None
|
|
81
83
|
|
|
84
|
+
# Compute loss
|
|
82
85
|
if self.training and (labels is not None or shift_labels is not None):
|
|
83
|
-
|
|
86
|
+
result = LigerForCausalLMLoss(
|
|
84
87
|
hidden_states=kept_hidden_states,
|
|
85
88
|
lm_head_weight=self.lm_head.weight,
|
|
86
89
|
labels=labels,
|
|
@@ -88,21 +91,31 @@ def lce_forward(
|
|
|
88
91
|
hidden_size=self.config.hidden_size,
|
|
89
92
|
**kwargs,
|
|
90
93
|
)
|
|
94
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
91
95
|
|
|
92
96
|
else: # if in inference mode materialize logits
|
|
93
97
|
logits = self.lm_head(kept_hidden_states)
|
|
94
|
-
if labels is not None:
|
|
98
|
+
if labels is not None or shift_labels is not None:
|
|
95
99
|
loss = self.loss_function(
|
|
96
100
|
logits=logits,
|
|
97
101
|
labels=labels,
|
|
102
|
+
shift_labels=shift_labels,
|
|
98
103
|
vocab_size=self.config.vocab_size,
|
|
99
104
|
**kwargs,
|
|
100
105
|
)
|
|
101
106
|
|
|
102
|
-
|
|
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(
|
|
103
115
|
loss=loss,
|
|
104
116
|
logits=logits,
|
|
105
117
|
past_key_values=outputs.past_key_values,
|
|
106
118
|
hidden_states=outputs.hidden_states,
|
|
107
119
|
attentions=outputs.attentions,
|
|
120
|
+
token_accuracy=token_accuracy,
|
|
108
121
|
)
|
|
@@ -11,6 +11,8 @@ from transformers.utils import is_torchdynamo_compiling
|
|
|
11
11
|
|
|
12
12
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
13
13
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
14
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
15
|
+
from liger_kernel.transformers.model.output_classes import LigerLlavaCausalLMOutputWithPast
|
|
14
16
|
|
|
15
17
|
|
|
16
18
|
def lce_forward_deprecated(
|
|
@@ -215,7 +217,7 @@ def lce_forward(
|
|
|
215
217
|
image_sizes: torch.Tensor = None,
|
|
216
218
|
skip_logits: Optional[bool] = None,
|
|
217
219
|
**lm_kwargs,
|
|
218
|
-
) -> Union[Tuple,
|
|
220
|
+
) -> Union[Tuple, LigerLlavaCausalLMOutputWithPast]:
|
|
219
221
|
r"""
|
|
220
222
|
Args:
|
|
221
223
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -293,6 +295,7 @@ def lce_forward(
|
|
|
293
295
|
shift_labels = lm_kwargs.pop("shift_labels", None)
|
|
294
296
|
logits = None
|
|
295
297
|
loss = None
|
|
298
|
+
token_accuracy = None
|
|
296
299
|
|
|
297
300
|
if skip_logits and labels is None and shift_labels is None:
|
|
298
301
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -302,7 +305,7 @@ def lce_forward(
|
|
|
302
305
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
303
306
|
|
|
304
307
|
if skip_logits:
|
|
305
|
-
|
|
308
|
+
result = LigerForCausalLMLoss(
|
|
306
309
|
hidden_states=kept_hidden_states,
|
|
307
310
|
lm_head_weight=self.lm_head.weight,
|
|
308
311
|
labels=labels,
|
|
@@ -310,23 +313,32 @@ def lce_forward(
|
|
|
310
313
|
hidden_size=self.config.text_config.hidden_size,
|
|
311
314
|
**lm_kwargs,
|
|
312
315
|
)
|
|
316
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
313
317
|
|
|
314
318
|
else:
|
|
315
319
|
logits = self.lm_head(kept_hidden_states)
|
|
316
|
-
if labels is not None:
|
|
320
|
+
if labels is not None or shift_labels is not None:
|
|
317
321
|
loss = self.loss_function(
|
|
318
|
-
logits=logits,
|
|
322
|
+
logits=logits,
|
|
323
|
+
labels=labels,
|
|
324
|
+
shift_labels=shift_labels,
|
|
325
|
+
vocab_size=self.config.text_config.vocab_size,
|
|
326
|
+
**lm_kwargs,
|
|
319
327
|
)
|
|
320
328
|
|
|
321
329
|
if not return_dict:
|
|
322
330
|
output = (logits,) + outputs[1:]
|
|
323
|
-
|
|
331
|
+
output = (loss,) + output if loss is not None else output
|
|
332
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
333
|
+
return output
|
|
324
334
|
|
|
325
|
-
|
|
335
|
+
# Return custom output class with token_accuracy field
|
|
336
|
+
return LigerLlavaCausalLMOutputWithPast(
|
|
326
337
|
loss=loss,
|
|
327
338
|
logits=logits,
|
|
328
339
|
past_key_values=outputs.past_key_values,
|
|
329
340
|
hidden_states=outputs.hidden_states,
|
|
330
341
|
attentions=outputs.attentions,
|
|
331
342
|
image_hidden_states=outputs.image_hidden_states,
|
|
343
|
+
token_accuracy=token_accuracy,
|
|
332
344
|
)
|
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
from typing import Optional
|
|
2
|
+
from typing import Tuple
|
|
2
3
|
|
|
3
4
|
import torch
|
|
4
5
|
import torch.nn as nn
|
|
5
6
|
|
|
6
7
|
import liger_kernel.transformers.functional as F
|
|
7
8
|
|
|
9
|
+
from liger_kernel.transformers.functional import CrossEntropyOutput
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def unpack_cross_entropy_result(
|
|
13
|
+
result,
|
|
14
|
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[torch.Tensor]]:
|
|
15
|
+
if isinstance(result, CrossEntropyOutput):
|
|
16
|
+
return result.loss, result.z_loss, result.token_accuracy
|
|
17
|
+
|
|
18
|
+
if isinstance(result, tuple):
|
|
19
|
+
loss = result[0]
|
|
20
|
+
z_loss = result[1] if len(result) > 1 else None
|
|
21
|
+
token_accuracy = result[2] if len(result) > 2 else None
|
|
22
|
+
return loss, z_loss, token_accuracy
|
|
23
|
+
|
|
24
|
+
return result, None, None
|
|
25
|
+
|
|
8
26
|
|
|
9
27
|
def fixed_fused_linear_cross_entropy(
|
|
10
28
|
hidden_states: torch.Tensor,
|
|
@@ -14,10 +32,11 @@ def fixed_fused_linear_cross_entropy(
|
|
|
14
32
|
ignore_index: int = -100,
|
|
15
33
|
final_logit_softcapping: Optional[float] = None,
|
|
16
34
|
accum_dtype: Optional[torch.dtype] = None,
|
|
35
|
+
return_token_accuracy: bool = False,
|
|
17
36
|
**kwargs,
|
|
18
37
|
):
|
|
19
38
|
reduction = "sum" if num_items_in_batch is not None else "mean"
|
|
20
|
-
|
|
39
|
+
result = F.liger_fused_linear_cross_entropy(
|
|
21
40
|
hidden_states,
|
|
22
41
|
lm_head_weight,
|
|
23
42
|
target,
|
|
@@ -25,11 +44,18 @@ def fixed_fused_linear_cross_entropy(
|
|
|
25
44
|
ignore_index=ignore_index,
|
|
26
45
|
softcap=final_logit_softcapping,
|
|
27
46
|
accum_dtype=accum_dtype,
|
|
47
|
+
return_token_accuracy=return_token_accuracy,
|
|
28
48
|
**kwargs,
|
|
29
49
|
)
|
|
50
|
+
|
|
51
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
52
|
+
|
|
30
53
|
if reduction == "sum":
|
|
31
54
|
loss = loss / num_items_in_batch
|
|
32
55
|
|
|
56
|
+
if return_token_accuracy:
|
|
57
|
+
return CrossEntropyOutput(loss=loss, token_accuracy=token_accuracy)
|
|
58
|
+
|
|
33
59
|
return loss
|
|
34
60
|
|
|
35
61
|
|
|
@@ -42,6 +68,7 @@ def LigerForCausalLMLoss(
|
|
|
42
68
|
ignore_index: int = -100,
|
|
43
69
|
shift_labels: Optional[torch.Tensor] = None,
|
|
44
70
|
final_logit_softcapping: Optional[float] = None,
|
|
71
|
+
return_token_accuracy: bool = False,
|
|
45
72
|
**kwargs,
|
|
46
73
|
):
|
|
47
74
|
# Skip upcast since intermediate values for the loss are all fp32 in kernel
|
|
@@ -55,13 +82,14 @@ def LigerForCausalLMLoss(
|
|
|
55
82
|
shift_labels = shift_labels.view(-1)
|
|
56
83
|
# Enable model parallelism
|
|
57
84
|
shift_labels = shift_labels.to(hidden_states.device)
|
|
58
|
-
|
|
85
|
+
result = fixed_fused_linear_cross_entropy(
|
|
59
86
|
hidden_states,
|
|
60
87
|
lm_head_weight,
|
|
61
88
|
shift_labels,
|
|
62
89
|
num_items_in_batch,
|
|
63
90
|
ignore_index,
|
|
64
91
|
final_logit_softcapping,
|
|
92
|
+
return_token_accuracy=return_token_accuracy,
|
|
65
93
|
**kwargs,
|
|
66
94
|
)
|
|
67
|
-
return
|
|
95
|
+
return result
|
|
@@ -6,10 +6,11 @@ from typing import Union
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
8
|
from transformers.cache_utils import Cache
|
|
9
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
10
9
|
from transformers.utils.deprecation import deprecate_kwarg
|
|
11
10
|
|
|
12
11
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
12
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
13
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
@@ -29,7 +30,7 @@ def lce_forward(
|
|
|
29
30
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
30
31
|
skip_logits: Optional[bool] = None,
|
|
31
32
|
**kwargs,
|
|
32
|
-
) -> Union[Tuple,
|
|
33
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
33
34
|
r"""
|
|
34
35
|
Copy paste Mistral's forward but replace torch cross entropy with liger fused linear cross entropy
|
|
35
36
|
|
|
@@ -94,6 +95,7 @@ def lce_forward(
|
|
|
94
95
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
95
96
|
loss = None
|
|
96
97
|
logits = None
|
|
98
|
+
token_accuracy = None
|
|
97
99
|
|
|
98
100
|
if skip_logits and labels is None and shift_labels is None:
|
|
99
101
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -101,8 +103,9 @@ def lce_forward(
|
|
|
101
103
|
if skip_logits is None:
|
|
102
104
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
103
105
|
|
|
106
|
+
# Compute loss
|
|
104
107
|
if skip_logits:
|
|
105
|
-
|
|
108
|
+
result = LigerForCausalLMLoss(
|
|
106
109
|
hidden_states=kept_hidden_states,
|
|
107
110
|
lm_head_weight=self.lm_head.weight,
|
|
108
111
|
labels=labels,
|
|
@@ -110,26 +113,33 @@ def lce_forward(
|
|
|
110
113
|
hidden_size=self.config.hidden_size,
|
|
111
114
|
**kwargs,
|
|
112
115
|
)
|
|
116
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
113
117
|
|
|
114
118
|
else:
|
|
115
119
|
logits = self.lm_head(kept_hidden_states)
|
|
116
120
|
|
|
117
121
|
loss = None
|
|
118
|
-
if labels is not None:
|
|
122
|
+
if labels is not None or shift_labels is not None:
|
|
119
123
|
loss = self.loss_function(
|
|
120
124
|
logits=logits,
|
|
121
125
|
labels=labels,
|
|
126
|
+
shift_labels=shift_labels,
|
|
122
127
|
vocab_size=self.config.vocab_size,
|
|
123
128
|
**kwargs,
|
|
124
129
|
)
|
|
130
|
+
|
|
125
131
|
if not return_dict:
|
|
126
|
-
|
|
127
|
-
|
|
132
|
+
output_tuple = (logits,) + outputs[1:]
|
|
133
|
+
output = (loss,) + output_tuple if loss is not None else output_tuple
|
|
134
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
135
|
+
return output
|
|
128
136
|
|
|
129
|
-
|
|
137
|
+
# Return custom output class with token_accuracy field
|
|
138
|
+
return LigerCausalLMOutputWithPast(
|
|
130
139
|
loss=loss,
|
|
131
140
|
logits=logits,
|
|
132
141
|
past_key_values=outputs.past_key_values,
|
|
133
142
|
hidden_states=outputs.hidden_states,
|
|
134
143
|
attentions=outputs.attentions,
|
|
144
|
+
token_accuracy=token_accuracy,
|
|
135
145
|
)
|
|
@@ -12,6 +12,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
12
12
|
|
|
13
13
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
14
14
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
15
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
16
|
+
from liger_kernel.transformers.model.output_classes import LigerMoeCausalLMOutputWithPast
|
|
15
17
|
|
|
16
18
|
|
|
17
19
|
def lce_forward_deprecated(
|
|
@@ -158,7 +160,7 @@ def lce_forward(
|
|
|
158
160
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
159
161
|
skip_logits: Optional[bool] = None,
|
|
160
162
|
**kwargs,
|
|
161
|
-
) -> Union[Tuple,
|
|
163
|
+
) -> Union[Tuple, LigerMoeCausalLMOutputWithPast]:
|
|
162
164
|
r"""
|
|
163
165
|
Args:
|
|
164
166
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -226,6 +228,7 @@ def lce_forward(
|
|
|
226
228
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
227
229
|
logits = None
|
|
228
230
|
loss = None
|
|
231
|
+
token_accuracy = None
|
|
229
232
|
|
|
230
233
|
if skip_logits and labels is None and shift_labels is None:
|
|
231
234
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -234,8 +237,9 @@ def lce_forward(
|
|
|
234
237
|
# By default, if in training mode, don't materialize logits
|
|
235
238
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
236
239
|
|
|
240
|
+
# Compute loss
|
|
237
241
|
if skip_logits:
|
|
238
|
-
|
|
242
|
+
result = LigerForCausalLMLoss(
|
|
239
243
|
hidden_states=kept_hidden_states,
|
|
240
244
|
lm_head_weight=self.lm_head.weight,
|
|
241
245
|
labels=labels,
|
|
@@ -243,13 +247,20 @@ def lce_forward(
|
|
|
243
247
|
hidden_size=self.config.hidden_size,
|
|
244
248
|
**kwargs,
|
|
245
249
|
)
|
|
250
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
246
251
|
|
|
247
252
|
else:
|
|
248
253
|
logits = self.lm_head(kept_hidden_states)
|
|
249
254
|
|
|
250
255
|
loss = None
|
|
251
|
-
if labels is not None:
|
|
252
|
-
loss = self.loss_function(
|
|
256
|
+
if labels is not None or shift_labels is not None:
|
|
257
|
+
loss = self.loss_function(
|
|
258
|
+
logits=logits,
|
|
259
|
+
labels=labels,
|
|
260
|
+
shift_labels=shift_labels,
|
|
261
|
+
vocab_size=self.vocab_size,
|
|
262
|
+
**kwargs,
|
|
263
|
+
)
|
|
253
264
|
aux_loss = None
|
|
254
265
|
if output_router_logits:
|
|
255
266
|
aux_loss = load_balancing_loss_func(
|
|
@@ -262,17 +273,21 @@ def lce_forward(
|
|
|
262
273
|
loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device
|
|
263
274
|
|
|
264
275
|
if not return_dict:
|
|
265
|
-
|
|
276
|
+
output_tuple = (logits,) + outputs[1:]
|
|
266
277
|
if output_router_logits:
|
|
267
|
-
|
|
268
|
-
|
|
278
|
+
output_tuple = (aux_loss,) + output_tuple
|
|
279
|
+
if token_accuracy is not None:
|
|
280
|
+
output_tuple = output_tuple + (token_accuracy,)
|
|
281
|
+
return (loss,) + output_tuple if loss is not None else output_tuple
|
|
269
282
|
|
|
270
|
-
|
|
283
|
+
# Return custom output class with token_accuracy field
|
|
284
|
+
return LigerMoeCausalLMOutputWithPast(
|
|
271
285
|
loss=loss,
|
|
272
286
|
aux_loss=aux_loss,
|
|
273
287
|
logits=logits,
|
|
274
288
|
past_key_values=outputs.past_key_values,
|
|
275
289
|
hidden_states=outputs.hidden_states,
|
|
276
290
|
attentions=outputs.attentions,
|
|
277
|
-
router_logits=outputs.router_logits,
|
|
291
|
+
router_logits=outputs.router_logits if return_dict else outputs[-1],
|
|
292
|
+
token_accuracy=token_accuracy,
|
|
278
293
|
)
|
|
@@ -12,6 +12,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
12
12
|
|
|
13
13
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
14
14
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
15
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
16
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
15
17
|
|
|
16
18
|
|
|
17
19
|
def lce_forward_deprecated(
|
|
@@ -149,7 +151,7 @@ def lce_forward(
|
|
|
149
151
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
150
152
|
skip_logits: Optional[bool] = None,
|
|
151
153
|
**kwargs,
|
|
152
|
-
) -> Union[Tuple,
|
|
154
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
153
155
|
r"""
|
|
154
156
|
Args:
|
|
155
157
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -219,6 +221,7 @@ def lce_forward(
|
|
|
219
221
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
220
222
|
logits = None
|
|
221
223
|
loss = None
|
|
224
|
+
token_accuracy = None
|
|
222
225
|
|
|
223
226
|
if skip_logits and labels is None and shift_labels is None:
|
|
224
227
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -228,7 +231,7 @@ def lce_forward(
|
|
|
228
231
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
229
232
|
|
|
230
233
|
if skip_logits:
|
|
231
|
-
|
|
234
|
+
result = LigerForCausalLMLoss(
|
|
232
235
|
hidden_states=kept_hidden_states,
|
|
233
236
|
lm_head_weight=self.lm_head.weight,
|
|
234
237
|
labels=labels,
|
|
@@ -236,25 +239,31 @@ def lce_forward(
|
|
|
236
239
|
hidden_size=self.config.hidden_size,
|
|
237
240
|
**kwargs,
|
|
238
241
|
)
|
|
242
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
239
243
|
|
|
240
244
|
else:
|
|
241
245
|
logits = self.lm_head(kept_hidden_states)
|
|
242
|
-
if labels is not None:
|
|
246
|
+
if labels is not None or shift_labels is not None:
|
|
243
247
|
loss = self.loss_function(
|
|
244
248
|
logits=logits,
|
|
245
249
|
labels=labels,
|
|
250
|
+
shift_labels=shift_labels,
|
|
246
251
|
vocab_size=self.config.vocab_size,
|
|
247
252
|
**kwargs,
|
|
248
253
|
)
|
|
249
254
|
|
|
250
255
|
if not return_dict:
|
|
251
256
|
output = (logits,) + outputs[1:]
|
|
252
|
-
|
|
257
|
+
output = (loss,) + output if loss is not None else output
|
|
258
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
259
|
+
return output
|
|
253
260
|
|
|
254
|
-
|
|
261
|
+
# Return custom output class with token_accuracy field
|
|
262
|
+
return LigerCausalLMOutputWithPast(
|
|
255
263
|
loss=loss,
|
|
256
264
|
logits=logits,
|
|
257
265
|
past_key_values=outputs.past_key_values,
|
|
258
266
|
hidden_states=outputs.hidden_states,
|
|
259
267
|
attentions=outputs.attentions,
|
|
268
|
+
token_accuracy=token_accuracy,
|
|
260
269
|
)
|