-
Notifications
You must be signed in to change notification settings - Fork 5
/
modeling_lmm.py
375 lines (322 loc) · 18.9 KB
/
modeling_lmm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# coding=utf-8
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
#
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
# and OPT implementations in this library. It has been modified from its
# original forms to accommodate minor architectural differences compared
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from transformers import PreTrainedModel, add_start_docstrings
from transformers.activations import ACT2FN
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, \
SequenceClassifierOutputWithPast
from transformers.utils import logging, add_start_docstrings_to_model_forward, replace_return_docstrings
from transformers import AutoConfig, AutoModelForCausalLM, LlamaConfig, LlamaModel, LlamaForCausalLM
import math
from typing import List, Optional, Tuple, Union
import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
logger = logging.get_logger(__name__)
DEFAULT_VIDEO_TOKEN = "<video>"
DEFAULT_VIDEO_PATCH_TOKEN = "<vid_patch>"
DEFAULT_VID_START_TOKEN = "<vid_start>"
DEFAULT_VID_END_TOKEN = "<vid_end>"
class VisionConfig:
def __init__(self):
self.frame_size = 224
self.patch_size = 14
self.hidden_size = 768
self.use_vid_start_end = True#None
self.vid_start_token = DEFAULT_VID_START_TOKEN#None
self.vid_end_token = DEFAULT_VID_END_TOKEN#None
self.vid_patch_token = DEFAULT_VIDEO_PATCH_TOKEN#None
class VideoCapConfig(LlamaConfig):
model_type = "VideoCap"
class VideoCapLlamaModel(LlamaModel):
config_class = VideoCapConfig
def __init__(self, config: LlamaConfig, mm_vision_tower=None, mm_hidden_size=None): # TODO: Remove unused params
super(VideoCapLlamaModel, self).__init__(config)
#if hasattr(config, "mm_vision_tower"):
self.vision_config = VisionConfig()
#video_projecter
#if hasattr(config, "use_mm_proj"):
self.mm_projector = nn.Linear(self.vision_config.hidden_size, config.hidden_size)
#initialize the video projecter
def initialize_vision_modules(self, pretrain_mm_mlp_adapter=None, tune_mm_mlp_adapter=False):
vision_config = self.vision_config
num_patches = (vision_config.frame_size // vision_config.patch_size) ** 2
position_size = 100
self.config.use_mm_proj = True
self.config.mm_hidden_size = vision_config.hidden_size
self.frame_position_embedding = nn.Embedding(position_size, 768)
if not hasattr(self, 'mm_projector'):
self.mm_projector = nn.Linear(vision_config.hidden_size, self.config.hidden_size)
if pretrain_mm_mlp_adapter is not None:
mm_projector_weights = torch.load(pretrain_mm_mlp_adapter, map_location='cpu')
self.mm_projector.load_state_dict({k.split('.')[-1]: v for k, v in mm_projector_weights.items()})
return dict(
video_token_len=num_patches,
vision_config=vision_config
)
def forward(
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
video_spatio_temporal_features: Optional[torch.FloatTensor] = None,
video_frame_position: Optional[torch.LongTensor] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, BaseModelOutputWithPast]:
orig_embeds_params = getattr(self, 'orig_embeds_params', None)
# if orig_embeds_params is not None:
# orig_embeds_params = orig_embeds_params[0]
# with torch.no_grad():
# self.get_input_embeddings().weight.data[:-2] = orig_embeds_params[:-2].data
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
#print("input_ids",input_ids.shape)
#print(input_ids.device)
#print("video_spatio_temporal_features",video_spatio_temporal_features.shape)
#encode the video features and get the input for llm
#print(len(video_spatio_temporal_features))
#print(input_ids.shape)
hidden_states = 768
if (input_ids.shape[1] != 1 or self.training) and video_spatio_temporal_features is not None:
#video_features = self.mm_projector(video_spatio_temporal_features)
dummy_video_features = torch.zeros(video_spatio_temporal_features[0].shape[-2], 4096, device=inputs_embeds.device,
dtype=inputs_embeds.dtype)
#dummy_video_features = self.mm_projector(dummy_video_features)
#video_size = [t_fea.shape for t_fea in video_spatio_temporal_features]
#print(video_size)
#video_feas = [t_fea[0] for t_fea in video_spatio_temporal_features]
#pad_video_features = torch.nn.utils.rnn.pad_sequence(video_feas,batch_first=True)
#video_size = [t_fea.shape[-2] for t_fea in video_spatio_temporal_features]
#project_video_feas = self.mm_projector(pad_video_features)
new_input_embeds = []
t_i = -1
for cur_input_ids, cur_input_embeds in zip(input_ids, inputs_embeds):
t_i+=1
video_features = self.mm_projector(video_spatio_temporal_features[t_i])
#video_features = project_video_feas[t_i][:video_size[t_i],:]
#video_frame_position = None
#获取当前的frame position embedding
#print("loc",video_frame_position)
if(video_frame_position is not None):
temp_video_position = video_frame_position[t_i]
temp_position_fea = self.mm_projector(self.frame_position_embedding(temp_video_position))
video_features += temp_position_fea.repeat(video_features.size(1),1)
#video_features = video_features.unsqueeze(0)
#print("video_features",video_features.shape,video_features)
#print("t_video_features",t_video_features.shape,t_video_features)
max_idx = len(video_features)
cur_video_idx = 0
if (cur_input_ids == self.vision_config.vid_patch_token).sum() == 0:
# Multimodal LLM, but the current sample is not multimodal
cur_input_embeds = cur_input_embeds + (0. * dummy_video_features).sum()
new_input_embeds.append(cur_input_embeds)
cur_video_idx += 1
continue
#检测video的所有起始token,将video feature插入进去
if self.vision_config.use_vid_start_end:
if (cur_input_ids == self.vision_config.vid_start_token).sum() != (
cur_input_ids == self.vision_config.vid_end_token).sum():
max_idx = (cur_input_ids == self.vision_config.vid_end_token).sum()
#raise ValueError("The number of video start tokens and video end tokens should be the same.")
video_start_tokens = torch.where(cur_input_ids == self.vision_config.vid_start_token)[0]
for video_start_token_pos in video_start_tokens:
cur_video_features = video_features[cur_video_idx].to(device=cur_input_embeds.device)
num_patches = cur_video_features.shape[0]
if cur_input_ids[video_start_token_pos + num_patches + 1] != self.vision_config.vid_end_token:
raise ValueError("The video end token should follow the video start token.")
if orig_embeds_params is not None:
cur_new_input_embeds = torch.cat((cur_input_embeds[:video_start_token_pos].detach(),
cur_input_embeds[
video_start_token_pos:video_start_token_pos + 1],
cur_video_features, cur_input_embeds[
video_start_token_pos + num_patches
+ 1:video_start_token_pos
+ num_patches + 2],
cur_input_embeds[
video_start_token_pos + num_patches + 2:].detach()),
dim=0)
else:
cur_new_input_embeds = torch.cat((cur_input_embeds[:video_start_token_pos + 1],
cur_video_features,
cur_input_embeds[video_start_token_pos
+ num_patches + 1:]), dim=0)
cur_video_idx += 1
if(cur_video_idx>=max_idx):break
new_input_embeds.append(cur_new_input_embeds)
else:
cur_video_features = video_features[cur_video_idx]
num_patches = cur_video_features.shape[0]
if (cur_input_ids == self.vision_config.vid_patch_token).sum() != num_patches:
raise ValueError(
"The number of video patch tokens should be the same as the number of video patches.")
masked_indices = torch.where(cur_input_ids == self.vision_config.vid_patch_token)[0]
mask_index_start = masked_indices[0]
if (masked_indices != torch.arange(mask_index_start, mask_index_start + num_patches,
device=masked_indices.device, dtype=masked_indices.dtype)).any():
raise ValueError("The video patch tokens should be consecutive.")
if orig_embeds_params is not None:
cur_new_input_embeds = torch.cat((cur_input_embeds[:mask_index_start].detach(),
cur_video_features,
cur_input_embeds[mask_index_start + num_patches:].detach()),
dim=0)
else:
cur_new_input_embeds = torch.cat((cur_input_embeds[:mask_index_start], cur_video_features,
cur_input_embeds[mask_index_start + num_patches:]), dim=0)
new_input_embeds.append(cur_new_input_embeds)
cur_video_idx += 1
inputs_embeds = torch.stack(new_input_embeds, dim=0)
return super(VideoCapLlamaModel, self).forward(
input_ids=None, attention_mask=attention_mask, past_key_values=past_key_values,
inputs_embeds=inputs_embeds, use_cache=use_cache,
output_attentions=output_attentions, output_hidden_states=output_hidden_states,
return_dict=return_dict
)
class VideoCapLlamaForCausalLM(LlamaForCausalLM):
config_class = VideoCapConfig
def __init__(self, config):
super(LlamaForCausalLM, self).__init__(config)
self.model = VideoCapLlamaModel(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
# Initialize weights and apply final processing
self.post_init()
def get_model(self):
return self.model
def forward(
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
video_spatio_temporal_features: Optional[torch.FloatTensor] = None,
video_frame_position: Optional[torch.LongTensor] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, CausalLMOutputWithPast]:
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
video_spatio_temporal_features=video_spatio_temporal_features,
video_frame_position=video_frame_position
)
hidden_states = outputs[0]
logits = self.lm_head(hidden_states)
loss = None
if labels is not None:
# Shift so that tokens < n predict n
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# Flatten the tokens
loss_fct = CrossEntropyLoss()
shift_logits = shift_logits.view(-1, self.config.vocab_size)
shift_labels = shift_labels.view(-1)
# Enable model/pipeline parallelism
shift_labels = shift_labels.to(shift_logits.device)
loss = loss_fct(shift_logits, shift_labels)
if not return_dict:
output = (logits,) + outputs[1:]
return (loss,) + output if loss is not None else output
return CausalLMOutputWithPast(
loss=loss,
logits=logits,
past_key_values=outputs.past_key_values,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
def prepare_inputs_for_generation(
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
):
if past_key_values:
input_ids = input_ids[:, -1:]
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
if inputs_embeds is not None and past_key_values is None:
model_inputs = {"inputs_embeds": inputs_embeds}
else:
model_inputs = {"input_ids": input_ids}
model_inputs.update(
{
"past_key_values": past_key_values,
"use_cache": kwargs.get("use_cache"),
"attention_mask": attention_mask,
"video_spatio_temporal_features": kwargs.get("video_spatio_temporal_features", None),
"video_frame_position": kwargs.get("video_frame_position", None),
}
)
return model_inputs
def initialize_vision_tokenizer(self, mm_use_vid_start_end, tokenizer, device,
tune_mm_mlp_adapter=False, pretrain_mm_mlp_adapter=None):
vision_config = self.get_model().vision_config
vision_config.use_vid_start_end = mm_use_vid_start_end
tokenizer.add_tokens([DEFAULT_VIDEO_PATCH_TOKEN], special_tokens=True)
self.resize_token_embeddings(len(tokenizer))
if mm_use_vid_start_end:
num_new_tokens = tokenizer.add_tokens([DEFAULT_VID_START_TOKEN, DEFAULT_VID_END_TOKEN], special_tokens=True)
self.resize_token_embeddings(len(tokenizer))
vision_config.vid_start_token, vision_config.vid_end_token = tokenizer.convert_tokens_to_ids(
[DEFAULT_VID_START_TOKEN, DEFAULT_VID_END_TOKEN])
if num_new_tokens > 0:
input_embeddings = self.get_input_embeddings().weight.data
output_embeddings = self.get_output_embeddings().weight.data
input_embeddings_avg = input_embeddings[:-num_new_tokens].mean(
dim=0, keepdim=True)
output_embeddings_avg = output_embeddings[:-num_new_tokens].mean(
dim=0, keepdim=True)
input_embeddings[-num_new_tokens:] = input_embeddings_avg
output_embeddings[-num_new_tokens:] = output_embeddings_avg
if tune_mm_mlp_adapter:
self.get_model().orig_embeds_params = [
self.get_input_embeddings().weight.data.clone().to(device=device)]
for p in self.get_input_embeddings().parameters():
p.requires_grad = True
for p in self.get_output_embeddings().parameters():
p.requires_grad = False
if pretrain_mm_mlp_adapter:
mm_projector_weights = torch.load(pretrain_mm_mlp_adapter, map_location='cpu')
embed_tokens_weight = mm_projector_weights['model.embed_tokens.weight']
assert num_new_tokens == 2
if input_embeddings.shape == embed_tokens_weight.shape:
input_embeddings[-num_new_tokens:] = embed_tokens_weight[-num_new_tokens:]
elif embed_tokens_weight.shape[0] == num_new_tokens:
input_embeddings[-num_new_tokens:] = embed_tokens_weight
else:
raise ValueError(
f"Unexpected embed_tokens_weight shape. Pretrained: {embed_tokens_weight.shape}. "
f"Current: {input_embeddings.shape}. Numer of new tokens: {num_new_tokens}.")
vision_config.vid_patch_token = tokenizer.convert_tokens_to_ids([DEFAULT_VIDEO_PATCH_TOKEN])[0]
AutoConfig.register("VideoCap", VideoCapConfig)
AutoModelForCausalLM.register(VideoCapConfig, VideoCapLlamaForCausalLM)