-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
337 lines (294 loc) · 11.5 KB
/
evaluate.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
# check continue training script
"""
example for finetuning Phi-3-V on the NLVR2 dataset using the Hugging Face Trainer API
Modified from Idefics-2 finetuning notebook:
https://colab.research.google.com/drive/1rm3AGquGEYXfeeizE40bbDtcWh5S4Nlq?usp=sharing
Install dependencies:
pip install transformers==4.38.1 \
datasets \
accelerate==0.30.1 \
peft \
Levenshtein \
deepspeed==0.13.1
minimal run:
torchrun --nproc_per_node=4 finetune_hf_trainer_nlvr2.py
"""
import argparse
import json
import os
from pathlib import Path
import glob
import torch
from accelerate import Accelerator, DistributedDataParallelKwargs
from accelerate.utils import gather_object
from datasets import load_dataset
from tqdm import tqdm
from peft import LoraConfig
from prepare_dataset import create_dataset
from rouge_score import rouge_scorer
from transformers import (
AutoModelForCausalLM,
AutoProcessor,
BitsAndBytesConfig,
Trainer,
TrainingArguments,
)
from transformers.trainer_utils import get_last_checkpoint
from phi3v_dataset import Phi3VDataCollator, Phi3VEvalDataCollator
from .utils import cal_score_eval
# suggested deepspeed config
DS_CONFIG_DICT = {
'zero_optimization': {
'stage': 2,
'allgather_partitions': True,
'allgather_bucket_size': 5e8,
'overlap_comm': True,
'reduce_scatter': True,
'reduce_bucket_size': 5e8,
'contiguous_gradients': True,
'round_robin_gradients': True,
},
'fp16': {
'enabled': 'auto',
'loss_scale': 0,
'loss_scale_window': 1000,
'initial_scale_power': 16,
'hysteresis': 2,
'min_loss_scale': 1,
},
'bf16': {'enabled': 'auto'},
'train_micro_batch_size_per_gpu': 'auto',
'train_batch_size': 'auto',
'gradient_accumulation_steps': 'auto',
'gradient_clipping': 'auto',
}
IGNORE_INDEX = -100
def create_lora_config(rank, alpha_to_rank_ratio=2.0, dropout=0.0, freeze_vision_model=False):
linear_modules = [
# Phi language modules
'qkv_proj', # attention
'o_proj',
'down_proj', # MLP
'gate_up_proj',
'lm_head',
]
if not freeze_vision_model:
vision_linear_modules = [
# CLIP modules
'q_proj', # attention
'k_proj',
'v_proj',
'out_proj',
'fc1', # MLP
'fc2',
# image projection
'img_projection.0',
'img_projection.2',
]
linear_modules.extend(vision_linear_modules)
lora_config = LoraConfig(
r=rank,
lora_alpha=round(rank * alpha_to_rank_ratio),
lora_dropout=dropout,
target_modules=linear_modules,
init_lora_weights='gaussian',
)
return lora_config
def create_model(model_name_or_path, use_flash_attention=False, use_qlora=False, load_previous_lora=False):
bnb_config = (
BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type='nf4',
bnb_4bit_compute_dtype=torch.bfloat16 if use_flash_attention else torch.float16,
)
if use_qlora
else None
)
if load_previous_lora:
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
# Phi-3-V is originally trained in bf16 + flash attn
# For fp16 mixed precision training, load in f32 to avoid hf accelerate error
torch_dtype=torch.bfloat16 if use_flash_attention else torch.float32,
trust_remote_code=True,
_attn_implementation='flash_attention_2' if use_flash_attention else 'eager',
cache_dir="/scratch/09697/luosong/cache",
)
else:
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
# Phi-3-V is originally trained in bf16 + flash attn
# For fp16 mixed precision training, load in f32 to avoid hf accelerate error
torch_dtype=torch.bfloat16 if use_flash_attention else torch.float32,
trust_remote_code=True,
_attn_implementation='flash_attention_2' if use_flash_attention else 'eager',
quantization_config=bnb_config,
cache_dir="/scratch/09697/luosong/cache",
)
return model
@torch.no_grad()
def evaluate(
model, processor, eval_dataset, save_path=None, disable_tqdm=False, eval_batch_size=1
):
rank = int(os.environ.get('RANK', 0))
local_rank = int(os.environ.get('LOCAL_RANK', 0))
world_size = int(os.environ.get('WORLD_SIZE', 1))
model.eval()
answers_unique = []
generated_texts_unique = []
eval_dataset_shard = eval_dataset.shard(world_size, rank)
eval_dataloader = torch.utils.data.DataLoader(
eval_dataset_shard,
batch_size=eval_batch_size,
collate_fn=Phi3VEvalDataCollator(processor.tokenizer.pad_token_id),
shuffle=False,
drop_last=False,
num_workers=4,
prefetch_factor=2,
pin_memory=True,
)
for batch in tqdm(eval_dataloader, disable=(rank != 0) or disable_tqdm):
unique_ids = batch.pop('unique_ids')
answers = batch.pop('answers')
answers_unique.extend(
{'id': i, 'answer': a.strip().strip('.').lower()} for i, a in zip(unique_ids, answers)
)
inputs = {k: v.to(f'cuda:{local_rank}') for k, v in batch.items()}
generated_ids = model.generate(
**inputs, eos_token_id=processor.tokenizer.eos_token_id, max_new_tokens=64
)
input_len = inputs['input_ids'].size(1)
generated_texts = processor.batch_decode(
generated_ids[:, input_len:],
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
generated_texts_unique.extend(
{'id': i, 'generated_text': g.strip().strip('.').lower()}
for i, g in zip(unique_ids, generated_texts)
)
# gather outputs from all ranks
answers_unique = gather_object(answers_unique)
generated_texts_unique = gather_object(generated_texts_unique)
if rank == 0:
assert len(answers_unique) == len(generated_texts_unique)
eval_results = evaluate(answers_unique, generated_texts_unique)
if save_path:
with open(save_path, 'w') as f:
save_dict = {
'answers_unique': answers_unique,
'generated_texts_unique': generated_texts_unique,
'eval_result': eval_results,
}
json.dump(save_dict, f)
return eval_results
return None
def patch_clip_for_lora(model):
# remove unused parameters and then monkey patch
def get_img_features(self, img_embeds):
clip_vision_model = self.img_processor.vision_model
hidden_states = clip_vision_model.embeddings(img_embeds)
hidden_states = clip_vision_model.pre_layrnorm(hidden_states)
patch_feature = clip_vision_model.encoder(
inputs_embeds=hidden_states, output_hidden_states=True
).hidden_states[-1][:, 1:]
return patch_feature
image_embedder = model.model.vision_embed_tokens
layer_index = image_embedder.layer_idx
clip_layers = image_embedder.img_processor.vision_model.encoder.layers
if layer_index < 0:
layer_index = len(clip_layers) + layer_index
del clip_layers[layer_index + 1 :]
del image_embedder.img_processor.vision_model.post_layernorm
image_embedder.get_img_features = get_img_features.__get__(image_embedder)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_name_or_path',
type=str,
default='microsoft/Phi-3.5-vision-instruct',
help='Model name or path to load from',
)
parser.add_argument('--data_dir', type=str, required=True, help='Path to UCF-101 dataset')
parser.add_argument('--use_flash_attention', action='store_true', help='Use Flash Attention')
parser.add_argument('--bf16', action='store_true', help='Use BF16')
parser.add_argument('--use_lora', action='store_true', help='Use LoRA')
parser.add_argument('--use_qlora', action='store_true', help='Use QLora')
parser.add_argument('--output_dir', type=str, default='./output/', help='Output directory')
parser.add_argument('--batch_size', type=int, default=16, help='Batch size')
parser.add_argument(
'--batch_size_per_gpu',
type=int,
default=1,
help='Batch size per GPU (adjust this to fit in GPU memory)',
)
parser.add_argument('--num_crops', type=int, default=16, help='Number of maximum image crops')
parser.add_argument(
'--num_train_epochs', type=int, default=1, help='Number of training epochs'
)
parser.add_argument('--learning_rate', type=float, default=4.0e-5, help='Learning rate')
parser.add_argument('--wd', type=float, default=0.01, help='Weight decay')
parser.add_argument('--no-tqdm', dest='tqdm', action='store_false', help='Disable tqdm')
parser.add_argument('--lora_rank', type=int, default=64, help='LoRA rank')
parser.add_argument(
'--lora_alpha_ratio', type=float, default=2, help='LoRA alpha to rank ratio'
)
parser.add_argument('--lora_dropout', type=float, default=0.0, help='LoRA dropout')
parser.add_argument('--freeze_vision_model', action='store_true', help='Freeze vision model')
args = parser.parse_args()
assert args.num_crops <= 16, 'num_crops must be less than or equal to 16'
if args.use_qlora:
args.use_lora = True
if args.use_flash_attention:
args.bf16 = True
accelerator = Accelerator(kwargs_handlers=[DistributedDataParallelKwargs(find_unused_parameters=True)])
with accelerator.local_main_process_first():
processor = AutoProcessor.from_pretrained(
args.model_name_or_path,
trust_remote_code=True,
num_crops=args.num_crops,
cache_dir="/scratch/09697/luosong/cache",
)
last_checkpoint_dir = get_last_checkpoint(args.output_dir)
if last_checkpoint_dir is None:
raise(ValueError("No previous trained model exist. Trained model is needed for Evaluation"))
model = create_model(
last_checkpoint_dir,
use_flash_attention=args.use_flash_attention,
use_qlora=args.use_qlora,
load_previous_lora=True,
)
local_rank = int(os.environ.get('LOCAL_RANK', 0))
model = model.to(f'cuda:{local_rank}')
_, eval_dataset = create_dataset(args.data_dir, processor)
num_gpus = accelerator.num_processes
print(f'training on {num_gpus} GPUs')
assert (
args.batch_size % (num_gpus * args.batch_size_per_gpu) == 0
), 'Batch size must be divisible by the number of GPUs'
gradient_accumulation_steps = args.batch_size // (num_gpus * args.batch_size_per_gpu)
if args.bf16:
fp16 = False
bf16 = True
else:
fp16 = True
bf16 = False
data_collator = Phi3VDataCollator(pad_token_id=processor.tokenizer.pad_token_id)
# eval before fine-tuning
out_path = Path(args.output_dir)
out_path.mkdir(parents=True, exist_ok=True)
if not args.use_qlora:
local_rank = int(os.environ.get('LOCAL_RANK', 0))
model = model.to(f'cuda:{local_rank}')
acc = evaluate(
model,
processor,
eval_dataset,
save_path=out_path / 'eval_before_last_save.json',
disable_tqdm=not args.tqdm,
eval_batch_size = args.batch_size
)
if accelerator.is_main_process:
print(f'Accuracy before finetuning: {acc}')
if __name__ == '__main__':
main()