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
|
@@ -5,10 +5,11 @@ from typing import Union
|
|
|
5
5
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
9
8
|
from transformers.utils.deprecation import deprecate_kwarg
|
|
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
|
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
@@ -28,7 +29,7 @@ def lce_forward(
|
|
|
28
29
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
29
30
|
skip_logits: Optional[bool] = None,
|
|
30
31
|
**kwargs,
|
|
31
|
-
) -> Union[Tuple,
|
|
32
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
32
33
|
r"""
|
|
33
34
|
Args:
|
|
34
35
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -91,6 +92,7 @@ def lce_forward(
|
|
|
91
92
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
92
93
|
logits = None
|
|
93
94
|
loss = None
|
|
95
|
+
token_accuracy = None
|
|
94
96
|
|
|
95
97
|
if skip_logits and labels is None and shift_labels is None:
|
|
96
98
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -99,8 +101,9 @@ def lce_forward(
|
|
|
99
101
|
# By default, if in training mode, don't materialize logits
|
|
100
102
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
101
103
|
|
|
104
|
+
# Compute loss
|
|
102
105
|
if skip_logits:
|
|
103
|
-
|
|
106
|
+
result = LigerForCausalLMLoss(
|
|
104
107
|
hidden_states=kept_hidden_states,
|
|
105
108
|
lm_head_weight=self.lm_head.weight,
|
|
106
109
|
labels=labels,
|
|
@@ -108,21 +111,31 @@ def lce_forward(
|
|
|
108
111
|
hidden_size=self.config.hidden_size,
|
|
109
112
|
**kwargs,
|
|
110
113
|
)
|
|
114
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
111
115
|
|
|
112
116
|
else:
|
|
113
117
|
logits = self.lm_head(kept_hidden_states)
|
|
114
|
-
if labels is not None:
|
|
118
|
+
if labels is not None or shift_labels is not None:
|
|
115
119
|
loss = self.loss_function(
|
|
116
120
|
logits=logits,
|
|
117
121
|
labels=labels,
|
|
122
|
+
shift_labels=shift_labels,
|
|
118
123
|
vocab_size=self.config.vocab_size,
|
|
119
124
|
**kwargs,
|
|
120
125
|
)
|
|
121
126
|
|
|
122
|
-
|
|
127
|
+
if not return_dict:
|
|
128
|
+
output = (logits,) + outputs[1:]
|
|
129
|
+
output = ((loss,) + output) if loss is not None else output
|
|
130
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
131
|
+
return output
|
|
132
|
+
|
|
133
|
+
# Return custom output class with token_accuracy field
|
|
134
|
+
return LigerCausalLMOutputWithPast(
|
|
123
135
|
loss=loss,
|
|
124
136
|
logits=logits,
|
|
125
137
|
past_key_values=outputs.past_key_values,
|
|
126
138
|
hidden_states=outputs.hidden_states,
|
|
127
139
|
attentions=outputs.attentions,
|
|
140
|
+
token_accuracy=token_accuracy,
|
|
128
141
|
)
|
|
@@ -0,0 +1,142 @@
|
|
|
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.modeling_outputs import BaseModelOutputWithPast
|
|
9
|
+
from transformers.utils.deprecation import deprecate_kwarg
|
|
10
|
+
|
|
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
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
17
|
+
def lce_forward(
|
|
18
|
+
self,
|
|
19
|
+
input_ids: torch.LongTensor = None,
|
|
20
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
21
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
22
|
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
|
23
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
24
|
+
labels: Optional[torch.LongTensor] = None,
|
|
25
|
+
use_cache: Optional[bool] = None,
|
|
26
|
+
output_attentions: Optional[bool] = None,
|
|
27
|
+
output_hidden_states: Optional[bool] = None,
|
|
28
|
+
return_dict: Optional[bool] = None,
|
|
29
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
30
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
31
|
+
skip_logits: Optional[bool] = None,
|
|
32
|
+
**kwargs,
|
|
33
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
34
|
+
r"""
|
|
35
|
+
Args:
|
|
36
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
37
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
38
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
39
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
40
|
+
|
|
41
|
+
logits_to_keep (`int` or `torch.Tensor`, *optional*):
|
|
42
|
+
If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
|
|
43
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
44
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
45
|
+
If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
|
|
46
|
+
This is useful when using packed tensor format (single dimension for batch and sequence length).
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
|
|
50
|
+
Example:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
>>> from transformers import AutoTokenizer, Olmo3ForCausalLM
|
|
54
|
+
|
|
55
|
+
>>> model = Olmo3ForCausalLM.from_pretrained("allenai/Olmo-3-7B-Instruct")
|
|
56
|
+
>>> tokenizer = AutoTokenizer.from_pretrained("allenai/Olmo-3-7B-Instruct")
|
|
57
|
+
|
|
58
|
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
|
59
|
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
|
60
|
+
|
|
61
|
+
>>> # Generate
|
|
62
|
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
63
|
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
64
|
+
'Hey, are you conscious? Can you talk to me?\nI’m not sure if you’re conscious of this, but I’m'
|
|
65
|
+
```
|
|
66
|
+
"""
|
|
67
|
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
|
68
|
+
output_hidden_states = (
|
|
69
|
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
70
|
+
)
|
|
71
|
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
|
72
|
+
|
|
73
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
74
|
+
outputs: BaseModelOutputWithPast = self.model(
|
|
75
|
+
input_ids=input_ids,
|
|
76
|
+
attention_mask=attention_mask,
|
|
77
|
+
position_ids=position_ids,
|
|
78
|
+
past_key_values=past_key_values,
|
|
79
|
+
inputs_embeds=inputs_embeds,
|
|
80
|
+
use_cache=use_cache,
|
|
81
|
+
output_attentions=output_attentions,
|
|
82
|
+
output_hidden_states=output_hidden_states,
|
|
83
|
+
return_dict=return_dict,
|
|
84
|
+
cache_position=cache_position,
|
|
85
|
+
**kwargs,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
hidden_states = outputs[0]
|
|
89
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
90
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
91
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
92
|
+
|
|
93
|
+
shift_labels = kwargs.pop("shift_labels", None)
|
|
94
|
+
logits = None
|
|
95
|
+
loss = None
|
|
96
|
+
token_accuracy = None
|
|
97
|
+
|
|
98
|
+
if skip_logits and labels is None and shift_labels is None:
|
|
99
|
+
raise ValueError("skip_logits is True, but labels and shift_labels are 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 or shift_labels is not None)
|
|
104
|
+
|
|
105
|
+
# Compute loss
|
|
106
|
+
if skip_logits:
|
|
107
|
+
result = LigerForCausalLMLoss(
|
|
108
|
+
hidden_states=kept_hidden_states,
|
|
109
|
+
lm_head_weight=self.lm_head.weight,
|
|
110
|
+
labels=labels,
|
|
111
|
+
shift_labels=shift_labels,
|
|
112
|
+
hidden_size=self.config.hidden_size,
|
|
113
|
+
**kwargs,
|
|
114
|
+
)
|
|
115
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
116
|
+
|
|
117
|
+
else:
|
|
118
|
+
logits = self.lm_head(kept_hidden_states)
|
|
119
|
+
if labels is not None or shift_labels is not None:
|
|
120
|
+
loss = self.loss_function(
|
|
121
|
+
logits=logits,
|
|
122
|
+
labels=labels,
|
|
123
|
+
shift_labels=shift_labels,
|
|
124
|
+
vocab_size=self.config.vocab_size,
|
|
125
|
+
**kwargs,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
if not return_dict:
|
|
129
|
+
output = (logits,) + outputs[1:]
|
|
130
|
+
output = ((loss,) + output) if loss is not None else output
|
|
131
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
132
|
+
return output
|
|
133
|
+
|
|
134
|
+
# Return custom output class with token_accuracy field
|
|
135
|
+
return LigerCausalLMOutputWithPast(
|
|
136
|
+
loss=loss,
|
|
137
|
+
logits=logits,
|
|
138
|
+
past_key_values=outputs.past_key_values,
|
|
139
|
+
hidden_states=outputs.hidden_states,
|
|
140
|
+
attentions=outputs.attentions,
|
|
141
|
+
token_accuracy=token_accuracy,
|
|
142
|
+
)
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Custom output classes for Liger-Kernel that extend transformers' ModelOutput classes
|
|
3
|
+
with optional token accuracy field.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
|
|
11
|
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
12
|
+
from transformers.modeling_outputs import MoeCausalLMOutputWithPast
|
|
13
|
+
|
|
14
|
+
# The following model-specific outputs are optional and depend on the installed
|
|
15
|
+
# transformers version. Guard their imports so our module remains importable
|
|
16
|
+
# even when those models are not available in the environment.
|
|
17
|
+
try:
|
|
18
|
+
from transformers.models.gemma3.modeling_gemma3 import Gemma3CausalLMOutputWithPast as _Gemma3CausalLMOutputWithPast
|
|
19
|
+
except Exception:
|
|
20
|
+
_Gemma3CausalLMOutputWithPast = None
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
from transformers.models.glm4v_moe.modeling_glm4v_moe import (
|
|
24
|
+
Glm4vMoeCausalLMOutputWithPast as _Glm4vMoeCausalLMOutputWithPast,
|
|
25
|
+
)
|
|
26
|
+
except Exception:
|
|
27
|
+
_Glm4vMoeCausalLMOutputWithPast = None
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
from transformers.models.internvl.modeling_internvl import (
|
|
31
|
+
InternVLCausalLMOutputWithPast as _InternVLCausalLMOutputWithPast,
|
|
32
|
+
)
|
|
33
|
+
except Exception:
|
|
34
|
+
_InternVLCausalLMOutputWithPast = None
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
from transformers.models.llava.modeling_llava import LlavaCausalLMOutputWithPast as _LlavaCausalLMOutputWithPast
|
|
38
|
+
except Exception:
|
|
39
|
+
_LlavaCausalLMOutputWithPast = None
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
from transformers.models.paligemma.modeling_paligemma import (
|
|
43
|
+
PaliGemmaCausalLMOutputWithPast as _PaliGemmaCausalLMOutputWithPast,
|
|
44
|
+
)
|
|
45
|
+
except Exception:
|
|
46
|
+
_PaliGemmaCausalLMOutputWithPast = None
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import (
|
|
50
|
+
Qwen2_5_VLCausalLMOutputWithPast as _Qwen2_5_VLCausalLMOutputWithPast,
|
|
51
|
+
)
|
|
52
|
+
except Exception:
|
|
53
|
+
_Qwen2_5_VLCausalLMOutputWithPast = None
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
from transformers.models.qwen2_vl.modeling_qwen2_vl import (
|
|
57
|
+
Qwen2VLCausalLMOutputWithPast as _Qwen2VLCausalLMOutputWithPast,
|
|
58
|
+
)
|
|
59
|
+
except Exception:
|
|
60
|
+
_Qwen2VLCausalLMOutputWithPast = None
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
|
|
64
|
+
Qwen3VLCausalLMOutputWithPast as _Qwen3VLCausalLMOutputWithPast,
|
|
65
|
+
)
|
|
66
|
+
except Exception:
|
|
67
|
+
_Qwen3VLCausalLMOutputWithPast = None
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
from transformers.models.qwen3_vl_moe.modeling_qwen3_vl_moe import (
|
|
71
|
+
Qwen3VLMoeCausalLMOutputWithPast as _Qwen3VLMoeCausalLMOutputWithPast,
|
|
72
|
+
)
|
|
73
|
+
except Exception:
|
|
74
|
+
_Qwen3VLMoeCausalLMOutputWithPast = None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class LigerCausalLMOutputWithPast(CausalLMOutputWithPast):
|
|
79
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass
|
|
83
|
+
class LigerMoeCausalLMOutputWithPast(MoeCausalLMOutputWithPast):
|
|
84
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
if _Gemma3CausalLMOutputWithPast is not None:
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class LigerGemma3CausalLMOutputWithPast(_Gemma3CausalLMOutputWithPast):
|
|
91
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if _Glm4vMoeCausalLMOutputWithPast is not None:
|
|
95
|
+
|
|
96
|
+
@dataclass
|
|
97
|
+
class LigerGlm4vMoeCausalLMOutputWithPast(_Glm4vMoeCausalLMOutputWithPast):
|
|
98
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
if _LlavaCausalLMOutputWithPast is not None:
|
|
102
|
+
|
|
103
|
+
@dataclass
|
|
104
|
+
class LigerLlavaCausalLMOutputWithPast(_LlavaCausalLMOutputWithPast):
|
|
105
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if _InternVLCausalLMOutputWithPast is not None:
|
|
109
|
+
|
|
110
|
+
@dataclass
|
|
111
|
+
class LigerInternVLCausalLMOutputWithPast(_InternVLCausalLMOutputWithPast):
|
|
112
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if _PaliGemmaCausalLMOutputWithPast is not None:
|
|
116
|
+
|
|
117
|
+
@dataclass
|
|
118
|
+
class LigerPaliGemmaCausalLMOutputWithPast(_PaliGemmaCausalLMOutputWithPast):
|
|
119
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if _Qwen2_5_VLCausalLMOutputWithPast is not None:
|
|
123
|
+
|
|
124
|
+
@dataclass
|
|
125
|
+
class LigerQwen2_5_VLCausalLMOutputWithPast(_Qwen2_5_VLCausalLMOutputWithPast):
|
|
126
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if _Qwen2VLCausalLMOutputWithPast is not None:
|
|
130
|
+
|
|
131
|
+
@dataclass
|
|
132
|
+
class LigerQwen2VLCausalLMOutputWithPast(_Qwen2VLCausalLMOutputWithPast):
|
|
133
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if _Qwen3VLCausalLMOutputWithPast is not None:
|
|
137
|
+
|
|
138
|
+
@dataclass
|
|
139
|
+
class LigerQwen3VLCausalLMOutputWithPast(_Qwen3VLCausalLMOutputWithPast):
|
|
140
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if _Qwen3VLMoeCausalLMOutputWithPast is not None:
|
|
144
|
+
|
|
145
|
+
@dataclass
|
|
146
|
+
class LigerQwen3VLMoeCausalLMOutputWithPast(_Qwen3VLMoeCausalLMOutputWithPast):
|
|
147
|
+
token_accuracy: Optional[torch.FloatTensor] = None
|
|
@@ -13,6 +13,9 @@ from transformers.utils import logging
|
|
|
13
13
|
from transformers.utils.deprecation import deprecate_kwarg
|
|
14
14
|
|
|
15
15
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
16
|
+
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
17
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
18
|
+
from liger_kernel.transformers.model.output_classes import LigerPaliGemmaCausalLMOutputWithPast
|
|
16
19
|
|
|
17
20
|
logger = logging.get_logger(__name__)
|
|
18
21
|
|
|
@@ -218,7 +221,7 @@ def lce_forward(
|
|
|
218
221
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
219
222
|
skip_logits: Optional[bool] = None,
|
|
220
223
|
**lm_kwargs,
|
|
221
|
-
) -> Union[Tuple,
|
|
224
|
+
) -> Union[Tuple, LigerPaliGemmaCausalLMOutputWithPast]:
|
|
222
225
|
r"""
|
|
223
226
|
Args:
|
|
224
227
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -331,6 +334,7 @@ def lce_forward(
|
|
|
331
334
|
|
|
332
335
|
loss = None
|
|
333
336
|
logits = None
|
|
337
|
+
token_accuracy = None
|
|
334
338
|
|
|
335
339
|
if skip_logits and labels is None:
|
|
336
340
|
raise ValueError("skip_logits is True, but labels is None")
|
|
@@ -358,8 +362,16 @@ def lce_forward(
|
|
|
358
362
|
shift_hidden_states = shift_hidden_states.view(-1, self.config.text_config.hidden_size)
|
|
359
363
|
shift_labels = shift_labels.view(-1).to(hidden_device)
|
|
360
364
|
|
|
361
|
-
|
|
362
|
-
|
|
365
|
+
# Use LigerForCausalLMLoss with accuracy support and pass already shifted labels
|
|
366
|
+
result = LigerForCausalLMLoss(
|
|
367
|
+
hidden_states=shift_hidden_states,
|
|
368
|
+
lm_head_weight=self.language_model.lm_head.weight,
|
|
369
|
+
labels=None,
|
|
370
|
+
shift_labels=shift_labels,
|
|
371
|
+
hidden_size=self.config.text_config.hidden_size,
|
|
372
|
+
**lm_kwargs,
|
|
373
|
+
)
|
|
374
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
363
375
|
else:
|
|
364
376
|
logits = self.language_model.lm_head(hidden_states)
|
|
365
377
|
if labels is not None:
|
|
@@ -382,15 +394,39 @@ def lce_forward(
|
|
|
382
394
|
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
383
395
|
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
384
396
|
loss = loss_fct(flat_logits, flat_labels)
|
|
397
|
+
elif shift_labels is not None:
|
|
398
|
+
# Upcast to float if we need to compute the loss to avoid potential precision issues
|
|
399
|
+
logits = logits.float()
|
|
400
|
+
shift_logits = logits[..., :-1, :]
|
|
401
|
+
if attention_mask is not None:
|
|
402
|
+
# we use the input attention mask to shift the logits and labels, because it is 2D.
|
|
403
|
+
# we also crop attn mask in case it is longer, which happens in PrefixTuning with peft
|
|
404
|
+
shift_attention_mask = attention_mask[:, -shift_logits.shape[1] :].to(logits.device)
|
|
405
|
+
shift_logits = shift_logits[shift_attention_mask.to(logits.device) != 0].contiguous()
|
|
406
|
+
shift_labels = shift_labels[shift_attention_mask.to(shift_labels.device) != 0].contiguous()
|
|
407
|
+
else:
|
|
408
|
+
shift_logits = shift_logits.contiguous()
|
|
409
|
+
shift_labels = shift_labels.contiguous()
|
|
410
|
+
# Flatten the tokens
|
|
411
|
+
loss_fct = CrossEntropyLoss()
|
|
412
|
+
|
|
413
|
+
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
414
|
+
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
415
|
+
loss = loss_fct(flat_logits, flat_labels)
|
|
416
|
+
|
|
385
417
|
if not return_dict:
|
|
386
418
|
output = (logits,) + outputs[1:]
|
|
387
|
-
|
|
419
|
+
output = (loss,) + output if loss is not None else output
|
|
420
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
421
|
+
return output
|
|
388
422
|
|
|
389
|
-
|
|
423
|
+
# Return PaliGemma output with token_accuracy field
|
|
424
|
+
return LigerPaliGemmaCausalLMOutputWithPast(
|
|
390
425
|
loss=loss,
|
|
391
426
|
logits=logits,
|
|
392
427
|
past_key_values=outputs.past_key_values,
|
|
393
428
|
hidden_states=outputs.hidden_states,
|
|
394
429
|
attentions=outputs.attentions,
|
|
395
430
|
image_hidden_states=image_features if pixel_values is not None else None,
|
|
431
|
+
token_accuracy=token_accuracy,
|
|
396
432
|
)
|
|
@@ -6,9 +6,10 @@ from typing import Union
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
8
|
from transformers.modeling_outputs import BaseModelOutputWithPast
|
|
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(
|
|
@@ -27,7 +28,7 @@ def lce_forward(
|
|
|
27
28
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
28
29
|
skip_logits: Optional[bool] = None,
|
|
29
30
|
**kwargs,
|
|
30
|
-
) -> Union[Tuple,
|
|
31
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
31
32
|
r"""
|
|
32
33
|
Example:
|
|
33
34
|
|
|
@@ -71,6 +72,7 @@ def lce_forward(
|
|
|
71
72
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
72
73
|
logits = None
|
|
73
74
|
loss = None
|
|
75
|
+
token_accuracy = None
|
|
74
76
|
|
|
75
77
|
if skip_logits and labels is None and shift_labels is None:
|
|
76
78
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -79,8 +81,9 @@ def lce_forward(
|
|
|
79
81
|
# By default, if in training mode, don't materialize logits
|
|
80
82
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
81
83
|
|
|
84
|
+
# Compute loss
|
|
82
85
|
if skip_logits:
|
|
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,25 +91,30 @@ def lce_forward(
|
|
|
88
91
|
hidden_size=self.config.hidden_size,
|
|
89
92
|
**kwargs,
|
|
90
93
|
)
|
|
91
|
-
|
|
94
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
92
95
|
else:
|
|
93
96
|
logits = self.lm_head(kept_hidden_states)
|
|
94
|
-
if labels is not None:
|
|
97
|
+
if labels is not None or shift_labels is not None:
|
|
95
98
|
loss = self.loss_function(
|
|
96
99
|
logits=logits,
|
|
97
100
|
labels=labels,
|
|
101
|
+
shift_labels=shift_labels,
|
|
98
102
|
vocab_size=self.config.vocab_size,
|
|
99
103
|
**kwargs,
|
|
100
104
|
)
|
|
101
105
|
|
|
102
106
|
if not return_dict:
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
output_tuple = (logits,) + outputs[1:]
|
|
108
|
+
output = (loss,) + output_tuple if loss is not None else output_tuple
|
|
109
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
110
|
+
return output
|
|
105
111
|
|
|
106
|
-
|
|
112
|
+
# Return custom output class with token_accuracy field
|
|
113
|
+
return LigerCausalLMOutputWithPast(
|
|
107
114
|
loss=loss,
|
|
108
115
|
logits=logits,
|
|
109
116
|
past_key_values=outputs.past_key_values,
|
|
110
117
|
hidden_states=outputs.hidden_states,
|
|
111
118
|
attentions=outputs.attentions,
|
|
119
|
+
token_accuracy=token_accuracy,
|
|
112
120
|
)
|
|
@@ -11,6 +11,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
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 LigerCausalLMOutputWithPast
|
|
14
16
|
|
|
15
17
|
|
|
16
18
|
def lce_forward_deprecated(
|
|
@@ -145,7 +147,7 @@ def lce_forward(
|
|
|
145
147
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
146
148
|
skip_logits: Optional[bool] = None,
|
|
147
149
|
**kwargs,
|
|
148
|
-
) -> Union[Tuple,
|
|
150
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
149
151
|
r"""
|
|
150
152
|
Args:
|
|
151
153
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -208,6 +210,7 @@ def lce_forward(
|
|
|
208
210
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
209
211
|
logits = None
|
|
210
212
|
loss = None
|
|
213
|
+
token_accuracy = None
|
|
211
214
|
|
|
212
215
|
if skip_logits and labels is None and shift_labels is None:
|
|
213
216
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -216,8 +219,9 @@ def lce_forward(
|
|
|
216
219
|
# By default, if in training mode, don't materialize logits
|
|
217
220
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
218
221
|
|
|
222
|
+
# Compute loss
|
|
219
223
|
if skip_logits:
|
|
220
|
-
|
|
224
|
+
result = LigerForCausalLMLoss(
|
|
221
225
|
hidden_states=kept_hidden_states,
|
|
222
226
|
lm_head_weight=self.lm_head.weight,
|
|
223
227
|
labels=labels,
|
|
@@ -225,21 +229,31 @@ def lce_forward(
|
|
|
225
229
|
hidden_size=self.config.hidden_size,
|
|
226
230
|
**kwargs,
|
|
227
231
|
)
|
|
232
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
228
233
|
|
|
229
234
|
else:
|
|
230
235
|
logits = self.lm_head(kept_hidden_states)
|
|
231
|
-
if labels is not None:
|
|
236
|
+
if labels is not None or shift_labels is not None:
|
|
232
237
|
loss = self.loss_function(
|
|
233
238
|
logits=logits,
|
|
234
239
|
labels=labels,
|
|
240
|
+
shift_labels=shift_labels,
|
|
235
241
|
vocab_size=self.config.vocab_size,
|
|
236
242
|
**kwargs,
|
|
237
243
|
)
|
|
238
244
|
|
|
239
|
-
|
|
245
|
+
if not return_dict:
|
|
246
|
+
output_tuple = (logits,) + outputs[1:]
|
|
247
|
+
output = (loss,) + output_tuple if loss is not None else output_tuple
|
|
248
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
249
|
+
return output
|
|
250
|
+
|
|
251
|
+
# Return custom output class with token accuracy field
|
|
252
|
+
return LigerCausalLMOutputWithPast(
|
|
240
253
|
loss=loss,
|
|
241
254
|
logits=logits,
|
|
242
255
|
past_key_values=outputs.past_key_values,
|
|
243
256
|
hidden_states=outputs.hidden_states,
|
|
244
257
|
attentions=outputs.attentions,
|
|
258
|
+
token_accuracy=token_accuracy,
|
|
245
259
|
)
|
|
@@ -5,10 +5,11 @@ from typing import Union
|
|
|
5
5
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
|
-
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import Qwen2_5_VLCausalLMOutputWithPast
|
|
9
8
|
from transformers.utils import can_return_tuple
|
|
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 LigerQwen2_5_VLCausalLMOutputWithPast
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
@can_return_tuple
|
|
@@ -33,7 +34,7 @@ def lce_forward(
|
|
|
33
34
|
second_per_grid_ts: Optional[torch.Tensor] = None,
|
|
34
35
|
skip_logits: Optional[bool] = None,
|
|
35
36
|
**kwargs,
|
|
36
|
-
) -> Union[Tuple,
|
|
37
|
+
) -> Union[Tuple, LigerQwen2_5_VLCausalLMOutputWithPast]:
|
|
37
38
|
r"""
|
|
38
39
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
39
40
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
@@ -113,6 +114,7 @@ def lce_forward(
|
|
|
113
114
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
114
115
|
loss = None
|
|
115
116
|
logits = None
|
|
117
|
+
token_accuracy = None
|
|
116
118
|
|
|
117
119
|
if skip_logits and labels is None and shift_labels is None:
|
|
118
120
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -120,8 +122,9 @@ def lce_forward(
|
|
|
120
122
|
if skip_logits is None:
|
|
121
123
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
122
124
|
|
|
125
|
+
# Compute loss
|
|
123
126
|
if skip_logits:
|
|
124
|
-
|
|
127
|
+
result = LigerForCausalLMLoss(
|
|
125
128
|
hidden_states=hidden_states,
|
|
126
129
|
lm_head_weight=self.lm_head.weight,
|
|
127
130
|
labels=labels,
|
|
@@ -129,22 +132,32 @@ def lce_forward(
|
|
|
129
132
|
hidden_size=self.config.hidden_size,
|
|
130
133
|
**kwargs,
|
|
131
134
|
)
|
|
135
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
132
136
|
else:
|
|
133
137
|
logits = self.lm_head(hidden_states)
|
|
134
138
|
|
|
135
139
|
loss = None
|
|
136
|
-
if labels is not None:
|
|
137
|
-
loss = self.loss_function(
|
|
140
|
+
if labels is not None or shift_labels is not None:
|
|
141
|
+
loss = self.loss_function(
|
|
142
|
+
logits=logits,
|
|
143
|
+
labels=labels,
|
|
144
|
+
shift_labels=shift_labels,
|
|
145
|
+
vocab_size=self.config.vocab_size,
|
|
146
|
+
)
|
|
138
147
|
|
|
139
148
|
if not return_dict:
|
|
140
|
-
|
|
141
|
-
|
|
149
|
+
output_tuple = (logits,) + outputs[1:]
|
|
150
|
+
output = (loss,) + output_tuple if loss is not None else output_tuple
|
|
151
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
152
|
+
return output
|
|
142
153
|
|
|
143
|
-
|
|
154
|
+
# Return Qwen2.5-VL output with token accuracy
|
|
155
|
+
return LigerQwen2_5_VLCausalLMOutputWithPast(
|
|
144
156
|
loss=loss,
|
|
145
157
|
logits=logits,
|
|
146
158
|
past_key_values=outputs.past_key_values,
|
|
147
159
|
hidden_states=outputs.hidden_states,
|
|
148
160
|
attentions=outputs.attentions,
|
|
149
161
|
rope_deltas=outputs.rope_deltas,
|
|
162
|
+
token_accuracy=token_accuracy,
|
|
150
163
|
)
|