liger-kernel 0.6.2__py3-none-any.whl → 0.6.3__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.
- liger_kernel/chunked_loss/fused_linear_ppo.py +4 -0
- liger_kernel/chunked_loss/grpo_loss.py +38 -4
- liger_kernel/chunked_loss/jsd_loss.py +5 -2
- liger_kernel/ops/cross_entropy.py +59 -53
- liger_kernel/ops/fused_linear_cross_entropy.py +68 -10
- liger_kernel/ops/layer_norm.py +4 -6
- liger_kernel/ops/poly_norm.py +386 -0
- liger_kernel/transformers/__init__.py +17 -0
- liger_kernel/transformers/functional.py +7 -0
- liger_kernel/transformers/fused_linear_cross_entropy.py +5 -1
- liger_kernel/transformers/model/falcon_h1.py +108 -0
- liger_kernel/transformers/model/gemma.py +2 -1
- liger_kernel/transformers/model/gemma2.py +8 -2
- liger_kernel/transformers/model/gemma3.py +27 -2
- liger_kernel/transformers/model/glm4.py +2 -1
- liger_kernel/transformers/model/glm4v.py +3 -2
- liger_kernel/transformers/model/glm4v_moe.py +153 -0
- liger_kernel/transformers/model/internvl.py +150 -0
- liger_kernel/transformers/model/llama.py +2 -1
- liger_kernel/transformers/model/llama4.py +2 -1
- liger_kernel/transformers/model/llava.py +6 -2
- liger_kernel/transformers/model/loss_utils.py +1 -0
- liger_kernel/transformers/model/mistral.py +2 -1
- liger_kernel/transformers/model/mixtral.py +8 -2
- liger_kernel/transformers/model/mllama.py +2 -1
- liger_kernel/transformers/model/olmo2.py +2 -1
- liger_kernel/transformers/model/paligemma.py +19 -0
- liger_kernel/transformers/model/phi3.py +2 -1
- liger_kernel/transformers/model/qwen2.py +2 -1
- liger_kernel/transformers/model/qwen2_5_vl.py +7 -2
- liger_kernel/transformers/model/qwen2_vl.py +7 -2
- liger_kernel/transformers/model/qwen3.py +2 -1
- liger_kernel/transformers/model/qwen3_moe.py +8 -2
- liger_kernel/transformers/model/qwen3_next.py +134 -0
- liger_kernel/transformers/model/smollm3.py +2 -1
- liger_kernel/transformers/model/smolvlm.py +158 -0
- liger_kernel/transformers/monkey_patch.py +452 -3
- liger_kernel/transformers/multi_token_attention.py +1 -1
- liger_kernel/transformers/poly_norm.py +42 -0
- liger_kernel/transformers/rms_norm.py +7 -0
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/METADATA +13 -10
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/RECORD +46 -39
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/WHEEL +0 -0
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/licenses/LICENSE +0 -0
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/licenses/NOTICE +0 -0
- {liger_kernel-0.6.2.dist-info → liger_kernel-0.6.3.dist-info}/top_level.txt +0 -0
|
@@ -119,8 +119,14 @@ def causal_forward(
|
|
|
119
119
|
logits = logits / self.config.final_logit_softcapping
|
|
120
120
|
logits = torch.tanh(logits)
|
|
121
121
|
logits = logits * self.config.final_logit_softcapping
|
|
122
|
-
if labels is not None:
|
|
123
|
-
loss = self.loss_function(
|
|
122
|
+
if labels is not None or shift_labels is not None:
|
|
123
|
+
loss = self.loss_function(
|
|
124
|
+
logits=logits,
|
|
125
|
+
labels=labels,
|
|
126
|
+
shift_labels=shift_labels,
|
|
127
|
+
vocab_size=self.vocab_size,
|
|
128
|
+
**loss_kwargs,
|
|
129
|
+
)
|
|
124
130
|
|
|
125
131
|
if not return_dict:
|
|
126
132
|
output = (logits,) + outputs[1:]
|
|
@@ -275,6 +281,25 @@ def multimodal_forward(
|
|
|
275
281
|
# Flatten the tokens
|
|
276
282
|
loss_fct = nn.CrossEntropyLoss()
|
|
277
283
|
|
|
284
|
+
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
285
|
+
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
286
|
+
loss = loss_fct(flat_logits, flat_labels)
|
|
287
|
+
elif shift_labels is not None:
|
|
288
|
+
# Upcast to float if we need to compute the loss to avoid potential precision issues
|
|
289
|
+
logits = logits.float()
|
|
290
|
+
shift_logits = logits[..., :-1, :]
|
|
291
|
+
if attention_mask is not None:
|
|
292
|
+
# we use the input attention mask to shift the logits and labels, because it is 2D.
|
|
293
|
+
# we also crop attn mask in case it is longer, which happens in PrefixTuning with peft
|
|
294
|
+
shift_attention_mask = attention_mask[:, -shift_logits.shape[1] :].to(logits.device)
|
|
295
|
+
shift_logits = shift_logits[shift_attention_mask.to(logits.device) != 0].contiguous()
|
|
296
|
+
shift_labels = shift_labels[shift_attention_mask.to(shift_labels.device) != 0].contiguous()
|
|
297
|
+
else:
|
|
298
|
+
shift_logits = shift_logits.contiguous()
|
|
299
|
+
shift_labels = shift_labels.contiguous()
|
|
300
|
+
# Flatten the tokens
|
|
301
|
+
loss_fct = nn.CrossEntropyLoss()
|
|
302
|
+
|
|
278
303
|
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
279
304
|
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
280
305
|
loss = loss_fct(flat_logits, flat_labels)
|
|
@@ -111,10 +111,11 @@ def lce_forward(
|
|
|
111
111
|
|
|
112
112
|
else:
|
|
113
113
|
logits = self.lm_head(kept_hidden_states)
|
|
114
|
-
if labels is not None:
|
|
114
|
+
if labels is not None or shift_labels is not None:
|
|
115
115
|
loss = self.loss_function(
|
|
116
116
|
logits=logits,
|
|
117
117
|
labels=labels,
|
|
118
|
+
shift_labels=shift_labels,
|
|
118
119
|
vocab_size=self.config.vocab_size,
|
|
119
120
|
**kwargs,
|
|
120
121
|
)
|
|
@@ -70,7 +70,7 @@ def lce_forward(
|
|
|
70
70
|
>>> processor = AutoProcessor.from_pretrained(MODEL_PATH, use_fast=True)
|
|
71
71
|
>>> model = Glm4vForConditionalGeneration.from_pretrained(
|
|
72
72
|
pretrained_model_name_or_path=MODEL_PATH,
|
|
73
|
-
|
|
73
|
+
dtype=torch.bfloat16,
|
|
74
74
|
device_map="auto",
|
|
75
75
|
)
|
|
76
76
|
>>> inputs = processor.apply_chat_template(
|
|
@@ -133,10 +133,11 @@ def lce_forward(
|
|
|
133
133
|
|
|
134
134
|
else:
|
|
135
135
|
logits = self.lm_head(kept_hidden_states)
|
|
136
|
-
if labels is not None:
|
|
136
|
+
if labels is not None or shift_labels is not None:
|
|
137
137
|
loss = self.loss_function(
|
|
138
138
|
logits=logits,
|
|
139
139
|
labels=labels,
|
|
140
|
+
shift_labels=shift_labels,
|
|
140
141
|
vocab_size=self.config.vocab_size,
|
|
141
142
|
**kwargs,
|
|
142
143
|
)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from typing import Tuple
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
|
|
7
|
+
from transformers.models.glm4v_moe.modeling_glm4v_moe import Glm4vMoeCausalLMOutputWithPast
|
|
8
|
+
from transformers.utils.deprecation import deprecate_kwarg
|
|
9
|
+
|
|
10
|
+
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
14
|
+
def lce_forward(
|
|
15
|
+
self,
|
|
16
|
+
input_ids: torch.LongTensor = None,
|
|
17
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
18
|
+
position_ids: Optional[torch.LongTensor] = None,
|
|
19
|
+
past_key_values: Optional[list[torch.FloatTensor]] = None,
|
|
20
|
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
|
21
|
+
labels: Optional[torch.LongTensor] = None,
|
|
22
|
+
pixel_values: Optional[torch.Tensor] = None,
|
|
23
|
+
pixel_values_videos: Optional[torch.FloatTensor] = None,
|
|
24
|
+
image_grid_thw: Optional[torch.LongTensor] = None,
|
|
25
|
+
video_grid_thw: Optional[torch.LongTensor] = None,
|
|
26
|
+
rope_deltas: Optional[torch.LongTensor] = None,
|
|
27
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
28
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
29
|
+
skip_logits: Optional[bool] = None,
|
|
30
|
+
**kwargs,
|
|
31
|
+
) -> Union[Tuple, Glm4vMoeCausalLMOutputWithPast]:
|
|
32
|
+
r"""
|
|
33
|
+
Args:
|
|
34
|
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
35
|
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
36
|
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
|
37
|
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
|
38
|
+
image_grid_thw (`torch.LongTensor` of shape `(num_images, 3)`, *optional*):
|
|
39
|
+
The temporal, height and width of feature shape of each image in LLM.
|
|
40
|
+
video_grid_thw (`torch.LongTensor` of shape `(num_videos, 3)`, *optional*):
|
|
41
|
+
The temporal, height and width of feature shape of each video in LLM.
|
|
42
|
+
rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*):
|
|
43
|
+
The rope index difference between sequence length and multimodal rope.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
logits_to_keep (`int` or `torch.Tensor`, *optional*):
|
|
47
|
+
If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
|
|
48
|
+
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
|
|
49
|
+
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
|
|
50
|
+
If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
|
|
51
|
+
This is useful when using packed tensor format (single dimension for batch and sequence length).
|
|
52
|
+
|
|
53
|
+
Example:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
>>> from transformers import AutoProcessor, Glm4vMoeForConditionalGeneration
|
|
57
|
+
>>> import torch
|
|
58
|
+
|
|
59
|
+
>>> MODEL_PATH = "zai-org/GLM-4.5V"
|
|
60
|
+
>>> messages = [
|
|
61
|
+
{
|
|
62
|
+
"role": "user",
|
|
63
|
+
"content": [
|
|
64
|
+
{
|
|
65
|
+
"type": "image",
|
|
66
|
+
"url": "https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"type": "text",
|
|
70
|
+
"text": "describe this image"
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
>>> processor = AutoProcessor.from_pretrained(MODEL_PATH)
|
|
76
|
+
>>> model = Glm4vMoeForConditionalGeneration.from_pretrained(
|
|
77
|
+
pretrained_model_name_or_path=MODEL_PATH,
|
|
78
|
+
dtype="auto",
|
|
79
|
+
device_map="auto",
|
|
80
|
+
)
|
|
81
|
+
>>> inputs = processor.apply_chat_template(
|
|
82
|
+
messages,
|
|
83
|
+
tokenize=True,
|
|
84
|
+
add_generation_prompt=True,
|
|
85
|
+
return_dict=True,
|
|
86
|
+
return_tensors="pt"
|
|
87
|
+
).to(model.device)
|
|
88
|
+
>>> inputs.pop("token_type_ids", None)
|
|
89
|
+
>>> generated_ids = model.generate(**inputs, max_new_tokens=8192)
|
|
90
|
+
>>> output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
|
|
91
|
+
```
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
95
|
+
outputs = self.model(
|
|
96
|
+
input_ids=input_ids,
|
|
97
|
+
pixel_values=pixel_values,
|
|
98
|
+
pixel_values_videos=pixel_values_videos,
|
|
99
|
+
image_grid_thw=image_grid_thw,
|
|
100
|
+
video_grid_thw=video_grid_thw,
|
|
101
|
+
position_ids=position_ids,
|
|
102
|
+
attention_mask=attention_mask,
|
|
103
|
+
past_key_values=past_key_values,
|
|
104
|
+
inputs_embeds=inputs_embeds,
|
|
105
|
+
cache_position=cache_position,
|
|
106
|
+
**kwargs,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
hidden_states = outputs[0]
|
|
110
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
111
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
112
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
113
|
+
|
|
114
|
+
shift_labels = kwargs.pop("shift_labels", None)
|
|
115
|
+
logits = None
|
|
116
|
+
loss = None
|
|
117
|
+
|
|
118
|
+
if skip_logits and labels is None and shift_labels is None:
|
|
119
|
+
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
120
|
+
|
|
121
|
+
if skip_logits is None:
|
|
122
|
+
# By default, if in training mode, don't materialize logits
|
|
123
|
+
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
124
|
+
|
|
125
|
+
if skip_logits:
|
|
126
|
+
loss = LigerForCausalLMLoss(
|
|
127
|
+
hidden_states=kept_hidden_states,
|
|
128
|
+
lm_head_weight=self.lm_head.weight,
|
|
129
|
+
labels=labels,
|
|
130
|
+
shift_labels=shift_labels,
|
|
131
|
+
hidden_size=self.config.hidden_size,
|
|
132
|
+
**kwargs,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
else:
|
|
136
|
+
logits = self.lm_head(kept_hidden_states)
|
|
137
|
+
if labels is not None or shift_labels is not None:
|
|
138
|
+
loss = self.loss_function(
|
|
139
|
+
logits=logits,
|
|
140
|
+
labels=labels,
|
|
141
|
+
shift_labels=shift_labels,
|
|
142
|
+
vocab_size=self.config.vocab_size,
|
|
143
|
+
**kwargs,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
return Glm4vMoeCausalLMOutputWithPast(
|
|
147
|
+
loss=loss,
|
|
148
|
+
logits=logits,
|
|
149
|
+
past_key_values=outputs.past_key_values,
|
|
150
|
+
hidden_states=outputs.hidden_states,
|
|
151
|
+
attentions=outputs.attentions,
|
|
152
|
+
rope_deltas=outputs.rope_deltas,
|
|
153
|
+
)
|
|
@@ -0,0 +1,150 @@
|
|
|
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.models.internvl.modeling_internvl import InternVLCausalLMOutputWithPast
|
|
9
|
+
from transformers.utils import can_return_tuple
|
|
10
|
+
|
|
11
|
+
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Copied from https://github.com/huggingface/transformers/blob/d888bd435d0c0eaabaabad5b33d52af518c7187c/src/transformers/models/internvl/modeling_internvl.py#L862
|
|
15
|
+
@can_return_tuple
|
|
16
|
+
def lce_forward(
|
|
17
|
+
self,
|
|
18
|
+
input_ids: torch.LongTensor = None,
|
|
19
|
+
pixel_values: Optional[torch.FloatTensor] = 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
|
+
vision_feature_layer: Optional[Union[int, List[int]]] = None,
|
|
25
|
+
vision_feature_select_strategy: Optional[str] = None,
|
|
26
|
+
labels: Optional[torch.LongTensor] = None,
|
|
27
|
+
use_cache: Optional[bool] = None,
|
|
28
|
+
output_attentions: Optional[bool] = None,
|
|
29
|
+
output_hidden_states: Optional[bool] = None,
|
|
30
|
+
return_dict: Optional[bool] = None,
|
|
31
|
+
cache_position: Optional[torch.LongTensor] = None,
|
|
32
|
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
33
|
+
image_sizes: Optional[torch.Tensor] = None,
|
|
34
|
+
skip_logits: Optional[bool] = None, # Added argument for liger-kernel
|
|
35
|
+
**lm_kwargs, # renamed from kwargs
|
|
36
|
+
) -> Union[Tuple, InternVLCausalLMOutputWithPast]:
|
|
37
|
+
r"""
|
|
38
|
+
Example:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
>>> import torch
|
|
42
|
+
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
43
|
+
|
|
44
|
+
>>> torch_device = "cuda"
|
|
45
|
+
>>> processor = AutoProcessor.from_pretrained("OpenGVLab/InternVL3-1B-hf")
|
|
46
|
+
>>> model = AutoModelForImageTextToText.from_pretrained(
|
|
47
|
+
... "OpenGVLab/InternVL3-1B-hf", dtype=torch.bfloat16, device_map=torch_device
|
|
48
|
+
... )
|
|
49
|
+
|
|
50
|
+
>>> messages = [
|
|
51
|
+
... {
|
|
52
|
+
... "role": "user",
|
|
53
|
+
... "content": [
|
|
54
|
+
... {
|
|
55
|
+
... "type": "image",
|
|
56
|
+
... "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
|
|
57
|
+
... },
|
|
58
|
+
... {
|
|
59
|
+
... "type": "image",
|
|
60
|
+
... "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg",
|
|
61
|
+
... },
|
|
62
|
+
... {"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
|
|
63
|
+
... ],
|
|
64
|
+
... },
|
|
65
|
+
... ]
|
|
66
|
+
|
|
67
|
+
>>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(torch_device)
|
|
68
|
+
>>> generate_ids = model.generate(**inputs, max_new_tokens=200)
|
|
69
|
+
>>> print(processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True))
|
|
70
|
+
The images depict the Statue of Liberty and the Golden Gate Bridge.
|
|
71
|
+
```"""
|
|
72
|
+
|
|
73
|
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
|
74
|
+
output_hidden_states = (
|
|
75
|
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
76
|
+
)
|
|
77
|
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
|
78
|
+
vision_feature_layer = (
|
|
79
|
+
vision_feature_layer if vision_feature_layer is not None else self.config.vision_feature_layer
|
|
80
|
+
)
|
|
81
|
+
vision_feature_select_strategy = (
|
|
82
|
+
vision_feature_select_strategy
|
|
83
|
+
if vision_feature_select_strategy is not None
|
|
84
|
+
else self.config.vision_feature_select_strategy
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
outputs = self.model(
|
|
88
|
+
input_ids=input_ids,
|
|
89
|
+
pixel_values=pixel_values,
|
|
90
|
+
attention_mask=attention_mask,
|
|
91
|
+
position_ids=position_ids,
|
|
92
|
+
past_key_values=past_key_values,
|
|
93
|
+
inputs_embeds=inputs_embeds,
|
|
94
|
+
vision_feature_layer=vision_feature_layer,
|
|
95
|
+
vision_feature_select_strategy=vision_feature_select_strategy,
|
|
96
|
+
use_cache=use_cache,
|
|
97
|
+
output_attentions=output_attentions,
|
|
98
|
+
output_hidden_states=output_hidden_states,
|
|
99
|
+
return_dict=return_dict,
|
|
100
|
+
cache_position=cache_position,
|
|
101
|
+
image_sizes=image_sizes,
|
|
102
|
+
**lm_kwargs,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Copied from llava.py
|
|
106
|
+
hidden_states = outputs[0]
|
|
107
|
+
# Only compute necessary logits, and do not upcast them to float if we are not computing the loss
|
|
108
|
+
slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
|
|
109
|
+
kept_hidden_states = hidden_states[:, slice_indices, :]
|
|
110
|
+
|
|
111
|
+
shift_labels = lm_kwargs.pop("shift_labels", None)
|
|
112
|
+
logits = None
|
|
113
|
+
loss = None
|
|
114
|
+
|
|
115
|
+
if skip_logits and labels is None and shift_labels is None:
|
|
116
|
+
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
117
|
+
|
|
118
|
+
if skip_logits is None:
|
|
119
|
+
# By default, if in training mode, don't materialize logits
|
|
120
|
+
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
121
|
+
|
|
122
|
+
if skip_logits:
|
|
123
|
+
loss = LigerForCausalLMLoss(
|
|
124
|
+
hidden_states=kept_hidden_states,
|
|
125
|
+
lm_head_weight=self.lm_head.weight,
|
|
126
|
+
labels=labels,
|
|
127
|
+
shift_labels=shift_labels,
|
|
128
|
+
hidden_size=self.config.text_config.hidden_size,
|
|
129
|
+
**lm_kwargs,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
else:
|
|
133
|
+
logits = self.lm_head(kept_hidden_states)
|
|
134
|
+
if labels is not None:
|
|
135
|
+
loss = self.loss_function(
|
|
136
|
+
logits=logits, labels=labels, vocab_size=self.config.text_config.vocab_size, **lm_kwargs
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
if not return_dict:
|
|
140
|
+
output = (logits,) + outputs[1:]
|
|
141
|
+
return (loss,) + output if loss is not None else output
|
|
142
|
+
|
|
143
|
+
return InternVLCausalLMOutputWithPast(
|
|
144
|
+
loss=loss,
|
|
145
|
+
logits=logits,
|
|
146
|
+
past_key_values=outputs.past_key_values,
|
|
147
|
+
hidden_states=outputs.hidden_states,
|
|
148
|
+
attentions=outputs.attentions,
|
|
149
|
+
image_hidden_states=outputs.image_hidden_states,
|
|
150
|
+
)
|
|
@@ -248,10 +248,11 @@ def lce_forward(
|
|
|
248
248
|
|
|
249
249
|
else:
|
|
250
250
|
logits = self.lm_head(kept_hidden_states)
|
|
251
|
-
if labels is not None:
|
|
251
|
+
if labels is not None or shift_labels is not None:
|
|
252
252
|
loss = self.loss_function(
|
|
253
253
|
logits=logits,
|
|
254
254
|
labels=labels,
|
|
255
|
+
shift_labels=shift_labels,
|
|
255
256
|
vocab_size=self.config.vocab_size,
|
|
256
257
|
**kwargs,
|
|
257
258
|
)
|
|
@@ -91,10 +91,11 @@ def lce_forward(
|
|
|
91
91
|
|
|
92
92
|
else: # if in inference mode materialize logits
|
|
93
93
|
logits = self.lm_head(kept_hidden_states)
|
|
94
|
-
if labels is not None:
|
|
94
|
+
if labels is not None or shift_labels is not None:
|
|
95
95
|
loss = self.loss_function(
|
|
96
96
|
logits=logits,
|
|
97
97
|
labels=labels,
|
|
98
|
+
shift_labels=shift_labels,
|
|
98
99
|
vocab_size=self.config.vocab_size,
|
|
99
100
|
**kwargs,
|
|
100
101
|
)
|
|
@@ -313,9 +313,13 @@ def lce_forward(
|
|
|
313
313
|
|
|
314
314
|
else:
|
|
315
315
|
logits = self.lm_head(kept_hidden_states)
|
|
316
|
-
if labels is not None:
|
|
316
|
+
if labels is not None or shift_labels is not None:
|
|
317
317
|
loss = self.loss_function(
|
|
318
|
-
logits=logits,
|
|
318
|
+
logits=logits,
|
|
319
|
+
labels=labels,
|
|
320
|
+
shift_labels=shift_labels,
|
|
321
|
+
vocab_size=self.config.text_config.vocab_size,
|
|
322
|
+
**lm_kwargs,
|
|
319
323
|
)
|
|
320
324
|
|
|
321
325
|
if not return_dict:
|
|
@@ -115,10 +115,11 @@ def lce_forward(
|
|
|
115
115
|
logits = self.lm_head(kept_hidden_states)
|
|
116
116
|
|
|
117
117
|
loss = None
|
|
118
|
-
if labels is not None:
|
|
118
|
+
if labels is not None or shift_labels is not None:
|
|
119
119
|
loss = self.loss_function(
|
|
120
120
|
logits=logits,
|
|
121
121
|
labels=labels,
|
|
122
|
+
shift_labels=shift_labels,
|
|
122
123
|
vocab_size=self.config.vocab_size,
|
|
123
124
|
**kwargs,
|
|
124
125
|
)
|
|
@@ -248,8 +248,14 @@ def lce_forward(
|
|
|
248
248
|
logits = self.lm_head(kept_hidden_states)
|
|
249
249
|
|
|
250
250
|
loss = None
|
|
251
|
-
if labels is not None:
|
|
252
|
-
loss = self.loss_function(
|
|
251
|
+
if labels is not None or shift_labels is not None:
|
|
252
|
+
loss = self.loss_function(
|
|
253
|
+
logits=logits,
|
|
254
|
+
labels=labels,
|
|
255
|
+
shift_labels=shift_labels,
|
|
256
|
+
vocab_size=self.vocab_size,
|
|
257
|
+
**kwargs,
|
|
258
|
+
)
|
|
253
259
|
aux_loss = None
|
|
254
260
|
if output_router_logits:
|
|
255
261
|
aux_loss = load_balancing_loss_func(
|
|
@@ -239,10 +239,11 @@ def lce_forward(
|
|
|
239
239
|
|
|
240
240
|
else:
|
|
241
241
|
logits = self.lm_head(kept_hidden_states)
|
|
242
|
-
if labels is not None:
|
|
242
|
+
if labels is not None or shift_labels is not None:
|
|
243
243
|
loss = self.loss_function(
|
|
244
244
|
logits=logits,
|
|
245
245
|
labels=labels,
|
|
246
|
+
shift_labels=shift_labels,
|
|
246
247
|
vocab_size=self.config.vocab_size,
|
|
247
248
|
**kwargs,
|
|
248
249
|
)
|
|
@@ -111,10 +111,11 @@ def lce_forward(
|
|
|
111
111
|
|
|
112
112
|
else:
|
|
113
113
|
logits = self.lm_head(kept_hidden_states)
|
|
114
|
-
if labels is not None:
|
|
114
|
+
if labels is not None or shift_labels is not None:
|
|
115
115
|
loss = self.loss_function(
|
|
116
116
|
logits=logits,
|
|
117
117
|
labels=labels,
|
|
118
|
+
shift_labels=shift_labels,
|
|
118
119
|
vocab_size=self.config.vocab_size,
|
|
119
120
|
**kwargs,
|
|
120
121
|
)
|
|
@@ -379,6 +379,25 @@ def lce_forward(
|
|
|
379
379
|
# Flatten the tokens
|
|
380
380
|
loss_fct = CrossEntropyLoss()
|
|
381
381
|
|
|
382
|
+
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
383
|
+
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
384
|
+
loss = loss_fct(flat_logits, flat_labels)
|
|
385
|
+
elif shift_labels is not None:
|
|
386
|
+
# Upcast to float if we need to compute the loss to avoid potential precision issues
|
|
387
|
+
logits = logits.float()
|
|
388
|
+
shift_logits = logits[..., :-1, :]
|
|
389
|
+
if attention_mask is not None:
|
|
390
|
+
# we use the input attention mask to shift the logits and labels, because it is 2D.
|
|
391
|
+
# we also crop attn mask in case it is longer, which happens in PrefixTuning with peft
|
|
392
|
+
shift_attention_mask = attention_mask[:, -shift_logits.shape[1] :].to(logits.device)
|
|
393
|
+
shift_logits = shift_logits[shift_attention_mask.to(logits.device) != 0].contiguous()
|
|
394
|
+
shift_labels = shift_labels[shift_attention_mask.to(shift_labels.device) != 0].contiguous()
|
|
395
|
+
else:
|
|
396
|
+
shift_logits = shift_logits.contiguous()
|
|
397
|
+
shift_labels = shift_labels.contiguous()
|
|
398
|
+
# Flatten the tokens
|
|
399
|
+
loss_fct = CrossEntropyLoss()
|
|
400
|
+
|
|
382
401
|
flat_logits = shift_logits.view(-1, self.config.text_config.vocab_size)
|
|
383
402
|
flat_labels = shift_labels.view(-1).to(shift_logits.device)
|
|
384
403
|
loss = loss_fct(flat_logits, flat_labels)
|
|
@@ -91,10 +91,11 @@ def lce_forward(
|
|
|
91
91
|
|
|
92
92
|
else:
|
|
93
93
|
logits = self.lm_head(kept_hidden_states)
|
|
94
|
-
if labels is not None:
|
|
94
|
+
if labels is not None or shift_labels is not None:
|
|
95
95
|
loss = self.loss_function(
|
|
96
96
|
logits=logits,
|
|
97
97
|
labels=labels,
|
|
98
|
+
shift_labels=shift_labels,
|
|
98
99
|
vocab_size=self.config.vocab_size,
|
|
99
100
|
**kwargs,
|
|
100
101
|
)
|
|
@@ -228,10 +228,11 @@ def lce_forward(
|
|
|
228
228
|
|
|
229
229
|
else:
|
|
230
230
|
logits = self.lm_head(kept_hidden_states)
|
|
231
|
-
if labels is not None:
|
|
231
|
+
if labels is not None or shift_labels is not None:
|
|
232
232
|
loss = self.loss_function(
|
|
233
233
|
logits=logits,
|
|
234
234
|
labels=labels,
|
|
235
|
+
shift_labels=shift_labels,
|
|
235
236
|
vocab_size=self.config.vocab_size,
|
|
236
237
|
**kwargs,
|
|
237
238
|
)
|
|
@@ -133,8 +133,13 @@ def lce_forward(
|
|
|
133
133
|
logits = self.lm_head(hidden_states)
|
|
134
134
|
|
|
135
135
|
loss = None
|
|
136
|
-
if labels is not None:
|
|
137
|
-
loss = self.loss_function(
|
|
136
|
+
if labels is not None or shift_labels is not None:
|
|
137
|
+
loss = self.loss_function(
|
|
138
|
+
logits=logits,
|
|
139
|
+
labels=labels,
|
|
140
|
+
shift_labels=shift_labels,
|
|
141
|
+
vocab_size=self.config.vocab_size,
|
|
142
|
+
)
|
|
138
143
|
|
|
139
144
|
if not return_dict:
|
|
140
145
|
output = (logits,) + outputs[1:]
|
|
@@ -129,8 +129,13 @@ def lce_forward(
|
|
|
129
129
|
logits = self.lm_head(hidden_states)
|
|
130
130
|
|
|
131
131
|
loss = None
|
|
132
|
-
if labels is not None:
|
|
133
|
-
loss = self.loss_function(
|
|
132
|
+
if labels is not None or shift_labels is not None:
|
|
133
|
+
loss = self.loss_function(
|
|
134
|
+
logits=logits,
|
|
135
|
+
labels=labels,
|
|
136
|
+
shift_labels=shift_labels,
|
|
137
|
+
vocab_size=self.config.vocab_size,
|
|
138
|
+
)
|
|
134
139
|
|
|
135
140
|
return Qwen2VLCausalLMOutputWithPast(
|
|
136
141
|
loss=loss,
|
|
@@ -103,10 +103,11 @@ def lce_forward(
|
|
|
103
103
|
|
|
104
104
|
else:
|
|
105
105
|
logits = self.lm_head(kept_hidden_states)
|
|
106
|
-
if labels is not None:
|
|
106
|
+
if labels is not None or shift_labels is not None:
|
|
107
107
|
loss = self.loss_function(
|
|
108
108
|
logits=logits,
|
|
109
109
|
labels=labels,
|
|
110
|
+
shift_labels=shift_labels,
|
|
110
111
|
vocab_size=self.config.vocab_size,
|
|
111
112
|
**kwargs,
|
|
112
113
|
)
|
|
@@ -107,8 +107,14 @@ def lce_forward(
|
|
|
107
107
|
)
|
|
108
108
|
else: # if in inference model materialize logits
|
|
109
109
|
logits = self.lm_head(kept_hidden_states)
|
|
110
|
-
if labels is not None:
|
|
111
|
-
loss = self.loss_function(
|
|
110
|
+
if labels is not None or shift_labels is not None:
|
|
111
|
+
loss = self.loss_function(
|
|
112
|
+
logits=logits,
|
|
113
|
+
labels=labels,
|
|
114
|
+
shift_labels=shift_labels,
|
|
115
|
+
vocab_size=self.vocab_size,
|
|
116
|
+
**kwargs,
|
|
117
|
+
)
|
|
112
118
|
|
|
113
119
|
aux_loss = None
|
|
114
120
|
if output_router_logits:
|