forked from showlab/Show-o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllava_data_vq_unified.py
288 lines (233 loc) · 9.61 KB
/
llava_data_vq_unified.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
import copy
import json
import os
from functools import partial
import torch
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
from PIL import Image
from torch.utils.data import Dataset
from torch.utils.data.distributed import DistributedSampler
from training.utils import image_transform
from llava.llava import conversation as conversation_lib
DEFAULT_IMAGE_TOKEN = "<image>"
IGNORE_INDEX = -100
conversation_lib.default_conversation = conversation_lib.conv_templates["phi1.5"]
SYSTEM_PROMPT = "A chat between a curious user and an artificial intelligence assistant. " \
"The assistant gives helpful, detailed, and polite answers to the user's questions."
def preprocess_multimodal(sources):
for source in sources:
for sentence in source:
if DEFAULT_IMAGE_TOKEN in sentence['value']:
sentence['value'] = sentence['value'].replace(DEFAULT_IMAGE_TOKEN, '').strip()
sentence['value'] = DEFAULT_IMAGE_TOKEN + '\n' + sentence['value']
sentence['value'] = sentence['value'].strip()
# Customized operation, get rid of <image> special token. Edited by Zechen
sentence["value"] = sentence["value"].replace(DEFAULT_IMAGE_TOKEN, "")
sentence['value'] = sentence['value'].strip()
return sources
def preprocess_v0(
sources,
tokenizer,
):
# Let's assume has_image is false, since we will process the image token separately
has_image = False
# Adapted from llava-phi/mipha/train/train.py
conv = conversation_lib.default_conversation.copy()
roles = {"human": conv.roles[0], "gpt": conv.roles[1]}
# Apply prompt templates
conversations = []
for i, source in enumerate(sources):
if roles[source[0]["from"]] != conv.roles[0]:
# Skip the first one if it is not from human
source = source[1:]
conv.messages = []
for j, sentence in enumerate(source):
role = roles[sentence["from"]]
assert role == conv.roles[j % 2]
conv.append_message(role, sentence["value"])
conversation_str = str(conv.get_prompt()).strip()
conversations.append(conversation_str)
input_ids = tokenizer(
conversations,
return_tensors="pt",
padding="longest",
max_length=tokenizer.model_max_length,
truncation=True,
).input_ids
targets = input_ids.clone()
assert conv.sep_style == conversation_lib.SeparatorStyle.TWO
# Mask targets
sep = conv.sep + conv.roles[1] + ": " # ' ASSISTANT: '
for conversation, target in zip(conversations, targets): # loop for instances in a batch
# total_len = int(target.ne(tokenizer.pad_token_id).sum()) + conversation.count(conv.sep2) # in phi-2, pad_token_id == eos_token_id
total_len = int(target.ne(tokenizer.pad_token_id).sum())
rounds = conversation.split(conv.sep2) # handle multi-round conversation regarding one image
cur_len = 0 # no bos token in phi, so set the initial len to 0
if cur_len > 0:
target[:cur_len] = IGNORE_INDEX
for i, rou in enumerate(rounds):
if rou == "":
break
parts = rou.split(sep)
if len(parts) != 2:
break
parts[0] += sep
round_len = len(tokenizer(rou).input_ids) + 1 # +1 for <|endoftext|>
instruction_len = len(tokenizer(parts[0]).input_ids) - 1
target[cur_len: cur_len + instruction_len] = IGNORE_INDEX
cur_len += round_len
target[cur_len:] = IGNORE_INDEX
if cur_len < tokenizer.model_max_length:
if cur_len != total_len:
target[:] = IGNORE_INDEX
print(conversation)
print(
f"WARNING: tokenization mismatch: {cur_len} vs. {total_len}."
f" (ignored)"
)
input_ids_system = tokenizer(
[SYSTEM_PROMPT for _ in range(len(conversations))],
return_tensors="pt",
padding="longest",
max_length=tokenizer.model_max_length,
truncation=True,
).input_ids
return dict(
input_ids=input_ids,
labels=targets,
input_ids_system=input_ids_system
)
class LLaVADataset(Dataset):
def __init__(self,
tokenizer,
phase,
):
super(LLaVADataset, self).__init__()
self.tokenizer = tokenizer
if phase == "pretrain":
data_file_path = "/mnt/bn/vgfm2/test_dit/blip_laion_cc_sbu_558k.json"
self.image_root = "/mnt/bn/vgfm2/test_dit/pretraining_data"
else:
data_file_path = "/mnt/bn/vgfm2/test_dit/llava_v1_5_mix665k.json"
self.image_root = "/mnt/bn/vgfm2/test_dit/tuning_data"
with open(data_file_path, 'r') as f:
data = json.load(f)
self.list_data_dict = []
for item in data:
if 'image' in item.keys():
self.list_data_dict.append(item)
print("Formatting llava instruction data")
def __len__(self):
return len(self.list_data_dict)
def __getitem__(self, i):
sources = self.list_data_dict[i]
if isinstance(i, int):
sources = [sources]
assert len(sources) == 1, "Don't know why it is wrapped to a list" # FIXME
assert 'image' in sources[0]
image_file = self.list_data_dict[i]['image']
image_folder = self.image_root
try:
image = Image.open(os.path.join(image_folder, image_file)).convert('RGB')
image = image_transform(image)
except:
print("Read image error. Use dummy data.")
crop_size = 256
image = torch.zeros(3, crop_size, crop_size)
sources = preprocess_multimodal(copy.deepcopy([e["conversations"] for e in sources]))
data_dict = preprocess_v0(sources, self.tokenizer)
if isinstance(i, int):
data_dict = dict(input_ids=data_dict["input_ids"][0],
labels=data_dict["labels"][0],
input_ids_system=data_dict["input_ids_system"][0])
# image exist in the data
if 'image' in self.list_data_dict[i]:
data_dict['image'] = image
else:
# image does not exist in the data, but the model is multimodal
crop_size = 256
data_dict['image'] = torch.zeros(3, crop_size, crop_size)
return data_dict
def collate_fn(
instances,
tokenizer=None,
max_length=77,
):
input_ids, labels, input_ids_system = tuple([instance[key] for instance in instances]
for key in ("input_ids", "labels", "input_ids_system"))
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids,
batch_first=True,
padding_value=tokenizer.pad_token_id)
labels = torch.nn.utils.rnn.pad_sequence(labels,
batch_first=True,
padding_value=IGNORE_INDEX)
input_ids_system = torch.stack(input_ids_system, dim=0)
offset = max_length - input_ids.shape[-1] - input_ids_system.shape[-1]
if input_ids.shape[-1] < max_length - input_ids_system.shape[-1]:
pad_tube = torch.ones(size=(input_ids.shape[0], offset), dtype=input_ids.dtype) * tokenizer.pad_token_id
input_ids = torch.cat([input_ids, pad_tube], dim=1)
pad_tube = torch.ones(size=(labels.shape[0], offset), dtype=labels.dtype) * IGNORE_INDEX
labels = torch.cat([labels, pad_tube], dim=1)
min_max_len = min(
max_length - input_ids_system.shape[-1],
tokenizer.model_max_length - input_ids_system.shape[-1],
)
input_ids = input_ids[:, :min_max_len]
labels = labels[:, :min_max_len]
batch = dict(
input_ids=input_ids,
labels=labels,
attention_mask=input_ids.ne(tokenizer.pad_token_id),
input_ids_system=input_ids_system,
)
if 'image' in instances[0]:
images = [instance['image'] for instance in instances]
if all(x is not None and x.shape == images[0].shape for x in images):
batch['images'] = torch.stack(images)
else:
batch['images'] = images
return batch
def get_instruct_data_loader(
tokenizer,
batch_size,
num_workers,
world_size,
local_rank,
max_length,
phase,
):
train_dataset = LLaVADataset(
tokenizer,
phase,
)
datasampler = DistributedSampler(train_dataset, num_replicas=world_size, rank=local_rank)
dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
collate_fn=partial(
collate_fn,
tokenizer=tokenizer,
max_length=max_length,
),
sampler=datasampler
)
return dataloader
if __name__ == '__main__':
import transformers
pretrained_model_path = '/mnt/bn/vgfm2/test_mlx/xavier/pretrained_weights/phi-1_5'
tokenizer = transformers.AutoTokenizer.from_pretrained(pretrained_model_path,
padding_side="left")
special_tokens = ("soi", "eoi", "sovi", "eovi", "t2i", "mmu", "t2v", "v2v", "lvg")
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
tokenizer.add_tokens(list(special_tokens))
dataset = LLaVADataset(
tokenizer,
"tuning"
)
item = dataset.__getitem__(0)
import pdb
pdb.set_trace()