Repository navigation
Expand file tree
/
Copy pathcadrille.py
More file actions
348 lines (308 loc) · 14.1 KB
/
Copy pathcadrille.py
File metadata and controls
348 lines (308 loc) · 14.1 KB
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
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
from qwen_vl_utils import process_vision_info
from transformers import Qwen2VLForConditionalGeneration
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLCausalLMOutputWithPast
def collate(batch, processor, n_points, eval=False):
messages = []
is_pc = [0] * len(batch)
is_img = [0] * len(batch)
if not eval:
for i, m in enumerate(batch):
if 'video' in m.keys():
is_img[i] = 1
message = [{
'role': 'user',
'content': [
{'type': 'video', 'video': m['video'], 'fps': 1.0},
{'type': 'text', 'text': m['description']}
]
},
{
'role': 'assistant',
'content': [
{'type': 'text', 'text': m['answer']}
]
}]
else:
if 'point_cloud' in m.keys():
is_pc[i] = 1
message = [{
'role': 'user',
'content': [
{'type': 'text', 'text': m['description']}
]
},
{
'role': 'assistant',
'content': [
{'type': 'text', 'text': m['answer']}
]
}]
messages.append(message)
texts = [
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=False)
for msg in messages]
else:
for i, m in enumerate(batch):
if 'video' in m.keys():
is_img[i] = 1
message = [{
'role': 'user',
'content': [
{'type': 'video', 'video': m['video'], 'fps': 1.0},
{'type': 'text', 'text': m['description']}
]
}]
else:
if 'point_cloud' in m.keys():
is_pc[i] = 1
message = [{
'role': 'user',
'content': [
{'type': 'text', 'text': m['description']}
]
}]
messages.append(message)
texts = [
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
for msg in messages]
points_inputs = ''.join(n_points * [processor.tokenizer.pad_token])
for i in range(len(texts)):
if is_pc[i]:
texts[i] = points_inputs + texts[i]
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=texts,
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors='pt')
inputs['point_clouds'] = torch.stack([
torch.tensor(m['point_cloud']) if is_pc[i] else torch.zeros(n_points, 3)
for i, m in enumerate(batch)])
inputs['is_pc'] = torch.tensor(is_pc, dtype=torch.bool)
inputs['is_img'] = torch.tensor(is_img, dtype=torch.bool)
if 'pixel_values_videos' in inputs.keys():
pixel_values_videos = inputs['pixel_values_videos'].new_zeros((
len(batch), torch.prod(inputs['video_grid_thw'][0]),
inputs['pixel_values_videos'].shape[1]))
pixel_values_videos[inputs['is_img']] = torch.stack(
torch.chunk(inputs['pixel_values_videos'],
chunks=sum(inputs['is_img'])))
inputs['pixel_values_videos'] = pixel_values_videos
video_grid_thw = inputs['video_grid_thw'].new_zeros((len(batch), 3))
video_grid_thw[inputs['is_img']] = inputs['video_grid_thw']
inputs['video_grid_thw'] = video_grid_thw
if not eval:
input_ids_lists = inputs['input_ids'].tolist()
assert len(messages) == len(input_ids_lists)
labels_list = []
for ids_list in input_ids_lists:
label_ids = [-100] * len(ids_list)
for begin_end_indexs in find_assistant_content_sublist_indexes(ids_list):
label_ids[begin_end_indexs[0] + 2: begin_end_indexs[1] + 1] = \
ids_list[begin_end_indexs[0] + 2: begin_end_indexs[1] + 1]
labels_list.append(label_ids)
labels_ids = torch.tensor(labels_list, dtype=torch.int64)
inputs['labels'] = labels_ids
else:
inputs['file_name'] = [m['file_name'] for m in batch]
return inputs
def find_assistant_content_sublist_indexes(l):
start_indexes = []
end_indexes = []
# Iterate through the list to find starting points
for i in range(len(l) - 1):
# Check if the current and next element form the start sequence
if l[i] == 151644 and l[i + 1] == 77091:
start_indexes.append(i)
# Now look for the first 151645 after the start
for j in range(i + 2, len(l)):
if l[j] == 151645:
end_indexes.append(j)
break # Move to the next start after finding the end
return list(zip(start_indexes, end_indexes))
class FourierEmbedder(nn.Module):
def __init__(self,
num_freqs=6,
logspace=True,
include_input=True,
include_pi=True):
super().__init__()
if logspace:
frequencies = 2.0 ** torch.arange(num_freqs, dtype=torch.float32)
else:
frequencies = torch.linspace(
1.0,
2.0 ** (num_freqs - 1),
num_freqs,
dtype=torch.float32)
if include_pi:
frequencies *= torch.pi
self.register_buffer('frequencies', frequencies, persistent=False)
self.include_input = include_input
def forward(self, x):
embed = (x[..., None].contiguous() * self.frequencies).view(*x.shape[:-1], -1)
if self.include_input:
return torch.cat((x, embed.sin(), embed.cos()), dim=-1)
else:
return torch.cat((embed.sin(), embed.cos()), dim=-1)
class FourierPointEncoder(nn.Module):
def __init__(self, hidden_size):
super().__init__()
self.fourier_embedder = FourierEmbedder(num_freqs=8, include_pi=False)
self.projection = nn.Linear(51, hidden_size)
def forward(self, points):
x = self.fourier_embedder(points[..., :3])
x = self.projection(x)
return x
class Cadrille(Qwen2VLForConditionalGeneration):
def __init__(self, config):
super().__init__(config)
torch.set_default_dtype(torch.float32)
self.point_encoder = FourierPointEncoder(config.hidden_size)
torch.set_default_dtype(torch.bfloat16)
def forward(
self,
input_ids=None,
attention_mask=None,
position_ids=None,
past_key_values=None,
inputs_embeds=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
pixel_values=None,
pixel_values_videos=None,
image_grid_thw=None,
video_grid_thw=None,
rope_deltas=None,
cache_position=None,
point_clouds=None,
is_pc=None,
is_img=None):
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
if inputs_embeds is None:
inputs_embeds = self.model.embed_tokens(input_ids)
if pixel_values is not None:
pixel_values = pixel_values.type(self.visual.get_dtype())
image_embeds = self.visual(pixel_values, grid_thw=image_grid_thw)
n_image_tokens = (input_ids == self.config.image_token_id).sum().item()
n_image_features = image_embeds.shape[0]
if n_image_tokens != n_image_features:
raise ValueError(
f"Image features and image tokens do not match: tokens: {n_image_tokens}, features {n_image_features}"
)
image_mask = (
(input_ids == self.config.image_token_id)
.unsqueeze(-1)
.expand_as(inputs_embeds)
.to(inputs_embeds.device)
)
image_embeds = image_embeds.to(inputs_embeds.device, inputs_embeds.dtype)
inputs_embeds = inputs_embeds.masked_scatter(image_mask, image_embeds)
if is_img.sum() > 0 and pixel_values_videos is not None:
pixel_values_videos = pixel_values_videos[is_img]
pixel_values_videos = pixel_values_videos.view(-1, pixel_values_videos.shape[-1])
pixel_values_videos = pixel_values_videos.type(self.visual.get_dtype())
video_grid_thw = video_grid_thw[is_img]
video_embeds = self.visual(pixel_values_videos, grid_thw=video_grid_thw)
n_video_tokens = (input_ids == self.config.video_token_id).sum().item()
n_video_features = video_embeds.shape[0]
if n_video_tokens != n_video_features:
raise ValueError(
f"Video features and video tokens do not match: tokens: {n_video_tokens}, features {n_video_features}"
)
video_mask = (
(input_ids == self.config.video_token_id)
.unsqueeze(-1)
.expand_as(inputs_embeds)
.to(inputs_embeds.device)
)
video_embeds = video_embeds.to(inputs_embeds.device, inputs_embeds.dtype)
inputs_embeds = inputs_embeds.masked_scatter(video_mask, video_embeds)
# add point cloud embeddings, we add only next 6 lines
if is_pc.sum() > 0 and (past_key_values is None or past_key_values.get_seq_length() == 0):
point_embeds = self.point_encoder(point_clouds.float()).bfloat16()
start_idxs = attention_mask.shape[1] - attention_mask.sum(axis=1)
for i, start_idx in enumerate(start_idxs):
if is_pc[i]:
inputs_embeds[i, start_idx:start_idx + point_embeds.shape[1], :] = point_embeds[i]
if attention_mask is not None:
attention_mask = attention_mask.to(inputs_embeds.device)
# if we get 4D attention mask we cannot calculate rope deltas anymore. TODO @raushan fixme
if position_ids is None and (attention_mask is None or attention_mask.ndim == 2):
# calculate RoPE index once per generation in the pre-fill stage only
if (
(cache_position is not None and cache_position[0] == 0)
or self.rope_deltas is None
or (past_key_values is None or past_key_values.get_seq_length() == 0)
):
position_ids, rope_deltas = self.get_rope_index(
input_ids, image_grid_thw, video_grid_thw, attention_mask
)
self.rope_deltas = rope_deltas
# then use the prev pre-calculated rope-deltas to get the correct position ids
else:
batch_size, seq_length, _ = inputs_embeds.shape
delta = cache_position[0] + self.rope_deltas if cache_position is not None else 0
position_ids = torch.arange(seq_length, device=inputs_embeds.device)
position_ids = position_ids.view(1, -1).expand(batch_size, -1)
if cache_position is not None: # otherwise `deltas` is an int `0`
delta = delta.repeat_interleave(batch_size // delta.shape[0], dim=0)
delta = delta.to(position_ids.device)
position_ids = position_ids.add(delta)
position_ids = position_ids.unsqueeze(0).expand(3, -1, -1)
outputs = self.model(
input_ids=None,
position_ids=position_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,
cache_position=cache_position,
)
hidden_states = outputs[0]
logits = self.lm_head(hidden_states)
loss = None
if labels is not None:
# Upcast to float if we need to compute the loss to avoid potential precision issues
logits = logits.float()
# 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 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 Qwen2VLCausalLMOutputWithPast(
loss=loss,
logits=logits,
past_key_values=outputs.past_key_values,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
rope_deltas=self.rope_deltas,
)
def prepare_inputs_for_generation(self, *args, **kwargs):
model_inputs = super().prepare_inputs_for_generation(*args, **kwargs)
model_inputs['point_clouds'] = kwargs['point_clouds']
model_inputs['is_pc'] = kwargs['is_pc']
model_inputs['is_img'] = kwargs['is_img']
return model_inputs