-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
285 lines (247 loc) · 11.5 KB
/
train.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
import os
import sys
from dataclasses import dataclass, field
from typing import Dict, Optional, Sequence
import argparse
import torch
import transformers
from accelerate.utils import set_seed
import llava.train.train as train
from llava.mm_utils import tokenizer_image_token
from prepare_data import prepare_instruct_tuning_with_policy_augmentation
from llava.constants import IGNORE_INDEX
from llavaguard.llavaguard_trainer import LlavaGuardTrainer
from trl import DataCollatorForCompletionOnlyLM
from llava import conversation as conversation_lib
import tokenizers
from packaging import version
IS_TOKENIZER_GREATER_THAN_0_14 = version.parse(tokenizers.__version__) >= version.parse('0.14')
@dataclass
class DataArguments:
data_path: str = field(default=None,
metadata={"help": "Path to the training data."})
data_path_eval: str = field(default=None,
metadata={"help": "Path to the validation data."})
lazy_preprocess: bool = False
is_multimodal: bool = True
image_folder: Optional[str] = field(default=None)
image_aspect_ratio: str = 'square'
def preprocess_mpt(
sources,
tokenizer: transformers.PreTrainedTokenizer,
has_image: bool = False
) -> Dict:
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], f"{i}"
conv.append_message(role, sentence["value"])
conversations.append(conv.get_prompt())
# Tokenize conversations
if has_image:
input_ids = torch.stack([tokenizer_image_token(prompt, tokenizer, return_tensors='pt') for prompt in conversations], dim=0)
else:
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.MPT
# Mask targets
sep = conv.sep + conv.roles[1]
for conversation, target in zip(conversations, targets):
total_len = int(target.ne(tokenizer.pad_token_id).sum())
rounds = conversation.split(conv.sep)
re_rounds = [conv.sep.join(rounds[:3])] # system + user + gpt
for conv_idx in range(3, len(rounds), 2):
re_rounds.append(conv.sep.join(rounds[conv_idx:conv_idx+2])) # user + gpt
cur_len = 0
target[:cur_len] = IGNORE_INDEX
for i, rou in enumerate(re_rounds):
if rou == "":
break
parts = rou.split(sep)
if len(parts) != 2:
break
parts[0] += sep
if has_image:
round_len = len(tokenizer_image_token(rou, tokenizer))
instruction_len = len(tokenizer_image_token(parts[0], tokenizer)) - 1
else:
round_len = len(tokenizer(rou).input_ids)
instruction_len = len(tokenizer(parts[0]).input_ids) - 1
if i != 0 and getattr(tokenizer, 'legacy', False) and IS_TOKENIZER_GREATER_THAN_0_14:
round_len += 1
instruction_len += 1
target[cur_len : cur_len + instruction_len] = IGNORE_INDEX
cur_len += round_len + 1
target[cur_len:] = IGNORE_INDEX
if cur_len < tokenizer.model_max_length:
if cur_len != total_len:
target[:] = IGNORE_INDEX
print(
f"WARNING: tokenization mismatch: {cur_len} vs. {total_len}."
f" (ignored)"
)
return dict(
input_ids=input_ids,
labels=targets,
)
@dataclass
class LlavaGuardDataCollator(object):
"""Collate examples for supervised fine-tuning."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels = tuple([instance[key] for instance in instances]
for key in ("input_ids", "labels"))
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids,
batch_first=True,
padding_value=self.tokenizer.pad_token_id)
labels = torch.nn.utils.rnn.pad_sequence(labels,
batch_first=True,
padding_value=IGNORE_INDEX)
input_ids = input_ids[:, :self.tokenizer.model_max_length]
labels = labels[:, :self.tokenizer.model_max_length]
batch = dict(
input_ids=input_ids,
labels=labels,
attention_mask=input_ids.ne(self.tokenizer.pad_token_id),
# attention_mask=~labels.ne(IGNORE_INDEX),
)
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
@dataclass
class LlavaGuardDataCollator2(DataCollatorForCompletionOnlyLM):
def __init__(self, tokenizer: transformers.PreTrainedTokenizer):
conv = conversation_lib.default_conversation.copy()
response_template = conv.roles[1]
super().__init__(tokenizer=tokenizer, response_template=response_template)
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
# instance
batch = super().__call__(instances)
input_ids = torch.nn.utils.rnn.pad_sequence(
batch['input_ids'],
batch_first=True,
padding_value=self.tokenizer.pad_token_id)
labels = torch.nn.utils.rnn.pad_sequence(batch['labels'],
batch_first=True,
padding_value=IGNORE_INDEX)
attention_mask = torch.nn.utils.rnn.pad_sequence(batch['attention_mask'],
batch_first=True,
padding_value=IGNORE_INDEX)
input_ids = input_ids[:, :self.tokenizer.model_max_length]
labels = labels[:, :self.tokenizer.model_max_length]
# print input_ids.shape, labels.shape, attention_mask.shape
print(input_ids.shape, labels.shape, attention_mask.shape)
attention_mask = attention_mask[:, :self.tokenizer.model_max_length]
batch = dict(
input_ids=input_ids,
labels=labels,
attention_mask=attention_mask,
)
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 make_supervised_data_module(tokenizer: transformers.PreTrainedTokenizer,
data_args) -> Dict:
"""Make dataset and collator for supervised fine-tuning."""
train_dataset = train.LazySupervisedDataset(tokenizer=tokenizer,
data_path=data_args.data_path,
data_args=data_args)
if hasattr(data_args, 'data_path_eval') and data_args.data_path_eval is not None:
val_dataset = train.LazySupervisedDataset(tokenizer=tokenizer,
data_path=data_args.data_path_eval,
data_args=data_args)
else:
val_dataset = None
# check whether train and test dataset are the have image tokens or not
sample = train_dataset[0]
# if 'image' in sample:
# print('Image found in the dataset')
# else:
# raise ValueError('Image not found in the dataset')
# # check for image token (-200) in input_ids
# if -200 in sample['input_ids']:
# print('Image token found in input_ids')
# else:
# raise ValueError('Image token not found in input_ids')
# data_collator = LlavaGuardDataCollator(tokenizer=tokenizer)
data_collator = train.DataCollatorForSupervisedDataset(tokenizer=tokenizer)
return dict(train_dataset=train_dataset,
eval_dataset=val_dataset,
data_collator=data_collator)
if __name__ == "__main__":
set_seed(42)
# if model already exists, skip training
args = sys.argv[1:]
parser1 = argparse.ArgumentParser()
parser1.add_argument('--output_dir', type=str, default=None)
parser1.add_argument('--data_path_eval', type=str, default=None)
parser1.add_argument('--model_name_or_path', type=str, default=None)
parser1.add_argument('--data_path', type=str, default=None)
name_space, a2 = parser1.parse_known_args(args)
output_dir = name_space.output_dir
data_path_train = name_space.data_path
model_name = name_space.model_name_or_path.split('/')[-1]
with_augmented_policies = 'augmented' in output_dir
llavaguard = output_dir.split('/')[-3]
template_version = output_dir.split('/')[-1]
ds_version = output_dir.split('/')[-2]
prepare_instruct_tuning_with_policy_augmentation(template_version, False, with_augmented_policies)
# if we find a trained model in the path then we skip training
if os.path.exists(output_dir + '/trainer_state.json'):
print(
f'Model {output_dir} already exists. Skipping training. If you want to retrain, delete the model directory.')
print('######################################################################################################')
sys.exit(0)
print(f'''
Start Training {llavaguard}
Base model: {model_name}, Dataset version: {ds_version}, Template version: {template_version}
Train data path: {data_path_train}
Output directory: {output_dir}
######################################################################################################
''')
# add the template version to the command line arguments
sys.argv.append('--run_name')
sys.argv.append(f'{llavaguard}_{template_version}')
# sys.argv.append('--tags')
# sys.argv.append(f'{ds_version}_{template_version}')
train.make_supervised_data_module = make_supervised_data_module
train.DataArguments = DataArguments
train.LLaVATrainer = LlavaGuardTrainer
train.preprocess_mpt = preprocess_mpt
os.makedirs(output_dir, exist_ok=True)
# safe all parser arguments in the output directory for future reference
with open(f'{output_dir}/params.txt', 'w+') as f:
params = ' '.join(sys.argv)
f.write(params.replace('--', '\n'))
train.train(attn_implementation="flash_attention_2")
#
# lora_dir = None if 'lora' not in output_dir else output_dir
# model_base = model_base if 'lora' not in output_dir else output_dir
# # eval LlavaGuard after training
# eval_llavaguard.evaluation(lora_dir=lora_dir, model_base=model_base,
# data_path_eval=data_path_eval,
# data_path_train=None,
# copy_images=False, replace_existing_output=True)