-
Notifications
You must be signed in to change notification settings - Fork 6
/
predict.py
executable file
·415 lines (361 loc) · 15.6 KB
/
predict.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import argparse
import os
from platform import processor
import sys
from tqdm import tqdm
import json
import torch
from torch.utils.data import DataLoader, Subset
from torch.utils.data.distributed import DistributedSampler
import deepspeed
from transformers import AutoTokenizer
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) + "/training")
from training.ppo_training.ppo_training_utils import sampling, sampling_llava, sampling_llama
from training.utils.data import build_dataset, DataCollatorPadToMaxLenForPrediction
from training.utils.utils import print_rank_0, to_device, set_random_seed, get_all_reduce_mean
from training.utils.ds_utils import get_train_ds_config
from training.utils.module.lora import convert_linear_layer_to_lora, only_optimize_lora_parameters, fuse_lora, unfuse_lora
from training.utils.model import create_dsvl_model_and_transforms, build_model
def parse_args():
parser = argparse.ArgumentParser(
description=
"Finetune a transformers model on a multi-modal task")
parser.add_argument('--data_path',
type=str,
default='./data/',
help='Where the training data are stored.')
parser.add_argument('--image_folder',
type=str,
default='./data/',
help='Where the image data are stored.')
parser.add_argument('--data_debug_path',
type=str,
default=None,
help='If provided, will save 10 training samples'
'to the path for debug purpose.')
parser.add_argument('--dataset_names',
nargs='*',
default=['minigpt4'],
help='Name of training dataset(s) to be used. Accepted format:'
'1) a single dataset name, 2) multiple dataset names in the'
'form: dataset1 dataset2 ...')
parser.add_argument('--dataset_samples',
nargs='*',
default=['all'],
help='How many samples do we use from each dataset.'
'Should be either a integer number or string all which'
'means use all samples. For example: all 512 means'
'using all samples form first data and 512 samples'
'from second data')
parser.add_argument('--dataset_concatenate_samples',
nargs='*',
default=[1],
help='How many samples do we concatenate from each dataset.'
'Should be either a integer number or string. 1 which'
'means use 1 sample for each datapoint')
parser.add_argument(
"--max_num_image_per_sample",
type=int,
default=1,
help="The maximum number of images per sample.",
)
parser.add_argument(
"--batch_size",
type=int,
default=1,
help="Batch size (per device) for the training dataloader.",
)
parser.add_argument(
"--per_device_train_batch_size",
type=int,
default=1,
help="Batch size (per device) for the training dataloader.",
)
parser.add_argument(
"--max_seq_len",
type=int,
default=4096,
help="The maximum sequence length, note that image tokens are included.",
)
parser.add_argument('--enable_tensorboard',
action='store_true',
help='Enable tensorboard logging')
parser.add_argument(
"--enable_mmca_attention",
action='store_true',
help="enable the new proposed attn, which is similar to cross attention",
)
parser.add_argument("--output_path",
type=str,
default=None,
help="Where to store the result")
parser.add_argument("--seed",
type=int,
default=1234,
help="A seed for reproducible training.")
parser.add_argument("--local_rank",
type=int,
default=-1,
help="local_rank for distributed training on gpus")
parser.add_argument('--gradient_checkpointing',
action='store_true',
help='Enable HF gradient checkpointing for model.')
parser.add_argument(
"--model_architecture",
type=str,
default="default",
help=
"Architecture of pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--lm_model_name_or_path",
type=str,
help=
"Path to pretrained model or model identifier from huggingface.co/models.",
required=True,
)
parser.add_argument("--vision_model_name_or_path",
default="openai/clip-vit-large-patch14", type=str)
parser.add_argument(
"--vis_proj",
type=str,
default='baseline',
help="[baseline, vit, or perceiver], used to projection vision feature to LLM embedding",
)
# deepspeed features
parser.add_argument(
"--precision",
type=str,
choices=["fp16", "bf16"],
default="fp16",
help=
"FP16 or BF16 precision. FP16 is recommended for typical use cases. BF16 is good for large models",
)
## LoRA for efficient training setting
parser.add_argument("--lang_lora_dim",
type=int,
default=0,
help="Use LoRA for fine-tuning language decoder (> 0).")
parser.add_argument("--lang_lora_module_name",
type=str,
default="model.layers.",
help="The scope name of the target LoRA parameters.")
parser.add_argument("--vis_lora_dim",
type=int,
default=0,
help="Use LoRA for fine-tuning visual encoder (> 0).")
parser.add_argument("--vis_lora_module_name",
type=str,
default="encoder.layers.",
help="The scope name of the target LoRA parameters.")
parser.add_argument('--only_optimize_lora',
action='store_true',
help='Only optimize the LoRA parameters.')
## from ppo training
parser.add_argument('--from_checkpoint',
type=str,
required=True,
help='Path to the trained SFT model.')
parser.add_argument(
"--vision_reward_model_name_or_path",
default="openai/clip-vit-large-patch14",
help=
"Path to the used vision model for training reward models.",
type=str)
parser.add_argument(
'--vis_encoder_update',
action='store_true',
help='Enable vision encoder update.')
parser.add_argument(
'--lang_decoder_update',
action='store_true',
help='Enable LLM update.')
parser.add_argument('--template',
type=str,
choices=
["default", "llama_2", "llama_3", "llama_3",
"vicuna", "llava", "llava_next", "llama-3.2-vision"],)
parser.add_argument(
"--max_new_tokens",
default=512,
type=int
)
parser.add_argument(
"--topk",
default=50,
type=int
)
parser.add_argument(
"--topp",
default=0.95,
type=float
)
parser.add_argument(
"--do_sample",
action="store_true"
)
parser.add_argument(
"--num_return_sequences",
default=1,
type=int
)
parser.add_argument(
"--temperature",
default=0.75,
type=float
)
parser = deepspeed.add_config_arguments(parser)
args = parser.parse_args()
if 'qwen' in args.vision_model_name_or_path.lower():
assert args.vis_proj == 'baseline', "qwen's model only support baseline vis_proj as it has the perceiver module inside"
assert args.batch_size == 1, "For the time being, we only support cases where batchsize is 1."
return args
def main():
args = parse_args()
if args.local_rank == -1:
device = torch.device("cuda")
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
deepspeed.init_distributed()
args.global_rank = torch.distributed.get_rank()
# If passed along, set the training seed now.
# set_random_seed(args.seed)
torch.distributed.barrier()
if args.model_architecture == "default":
tokenizer = AutoTokenizer.from_pretrained(args.lm_model_name_or_path,
fast_tokenizer=True)
tokenizer.padding_side = 'left'
else:
tokenizer = None
# RLHF engine is responsible for creating models, loading checkpoints, ds-initialize models/optims/lr-schedulers
ds_config = get_train_ds_config(
offload=False,
args=args,
stage=2)
print_rank_0("load model............")
model, image_processor, tokenizer = build_model(
text_tokenizer=tokenizer,
args=args,
ds_config=ds_config)
if args.model_architecture == "default":
model.load_state_dict(torch.load(os.path.join(args.from_checkpoint, 'pytorch_model.bin'), map_location='cpu'), strict=False)
model = model.to(torch.bfloat16)
model.to('cuda')
# Prepare the data
if len(args.dataset_samples) < len(args.dataset_names):
assert len(args.dataset_samples) == 1, "when args.dataset_samples is not the same length as args.dataset_names, it should be only one number"
args.dataset_samples = [args.dataset_samples[0]] * len(args.dataset_names)
if len(args.dataset_concatenate_samples) < len(args.dataset_names):
assert len(args.dataset_concatenate_samples) == 1, "when args.dataset_concatenate_samples is not the same length as args.dataset_names, it should be only one number"
args.dataset_concatenate_samples = [args.dataset_concatenate_samples[0]] * len(args.dataset_names)
# convert to int
args.dataset_concatenate_samples = [int(i) for i in args.dataset_concatenate_samples]
dataset = build_dataset(
args.data_path,
args.data_debug_path,
args.dataset_names,
args.dataset_samples,
args.dataset_concatenate_samples,
args.max_num_image_per_sample,
vis_root=args.image_folder,
vis_processor=image_processor,
tokenizer=tokenizer,
template=args.template
)
generation_kwargs={
"topk": args.topk,
"topp": args.topp,
"do_sample": (args.temperature>0.0),
"max_new_tokens": args.max_new_tokens,
"num_return_sequences": args.num_return_sequences,
"temperature": args.temperature
}
# split the dataset into train and evaluation
train_dataset = dataset
args.batch_size = 1
if args.model_architecture == "llama-3.2-vision":
image_size = image_processor.size
else:
image_size = image_processor.crop_size
train_dataloader = DataLoader(
train_dataset,
batch_size=args.batch_size,
sampler=DistributedSampler(train_dataset, shuffle=False, drop_last=False),
collate_fn=DataCollatorPadToMaxLenForPrediction(args.max_seq_len, tokenizer.pad_token_id, image_size),
)
reference = json.load(open(args.data_path, "r", encoding="utf-8"))
reference_dict = {}
for item in reference:
reference_dict[str(item['id'])] = item
print_rank_0("***** Running predicting *****", args.global_rank)
model.eval()
ref_count = 0
for idx, batch in enumerate(tqdm(train_dataloader)):
batch = to_device(batch, device) #torch.size(1, 3, 224, 224]) #torch.Size([1, 1, 3, 224, 224])
images = batch["image"].half()
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
image_num = batch["image_num"]
image_sizes = None
if args.model_architecture == "llava_next":
image_sizes = batch["image_sizes"]
image_sizes = image_sizes.reshape(len(input_ids), 2)
images = images.reshape(len(input_ids), -1, images.size(-3), images.size(-2), images.size(-1))
if args.model_architecture == 'llama-3.2-vision':
aspect_ratio_ids = batch["aspect_ratio_ids"]
aspect_ratio_mask = batch["aspect_ratio_mask"]
if image_num[0] == 0:
images = [None]
# import pdb; pdb.set_trace()
if args.model_architecture == 'default':
sampling_ans = sampling(model,
images, input_ids,
attention_mask=attention_mask,
pad_token_id=tokenizer.pad_token_id,
**generation_kwargs)
elif args.model_architecture in ["llava", "llava_next"]:
sampling_ans = sampling_llava(model,
images, input_ids,
image_sizes=image_sizes,
attention_mask=attention_mask,
pad_token_id=tokenizer.pad_token_id,
processor=tokenizer,
**generation_kwargs)
elif args.model_architecture in ["llama-3.2-vision"]:
sampling_ans = sampling_llama(model,
images, input_ids,
aspect_ratio_ids=aspect_ratio_ids,
aspect_ratio_mask=aspect_ratio_mask,
attention_mask=attention_mask,
pad_token_id=tokenizer.pad_token_id,
processor=tokenizer,
**generation_kwargs)
else:
raise NotImplementedError("Not support newly added model architecture")
with open(args.output_path, "a") as trg:
for i in range(args.batch_size):
id = str(batch['id'][0])
ref_image = reference_dict[id]['image'] if reference_dict[id]['image'] is not None else "None"
if 'label' in reference_dict[id].keys():
ref_label = reference_dict[id]['label']
elif len(reference_dict[id]['conversations']) == 2:
ref_label = reference_dict[id]['conversations'][1]['value'].replace("\n", "\\n")
else:
ref_label = "None"
line = " ||| ".join([id, ref_image.strip(),
tokenizer.decode(input_ids[i], skip_special_tokens=True, clean_up_tokenization_spaces=True).replace("\n", "\\n"),
sampling_ans[i][1].replace("\n", "\\n"),
ref_label.strip()]) + "\n"
print(line)
trg.write(line)
ref_count = ref_count + 1
if __name__ == "__main__":
main()