-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_train.py
418 lines (352 loc) · 17.8 KB
/
adapter_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
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
416
417
418
from functools import cache
from adapter_args_helper import (
DataArguments,
ModelArguments,
TrainingArguments
)
from datasets import load_from_disk, load_metric, set_caching_enabled, DatasetDict
from data_utils import load_dataset
from itertools import chain
from models import ClipCaptionModel
from torch.nn.functional import cross_entropy
from tqdm import tqdm
from transformers import (
default_data_collator,
get_linear_schedule_with_warmup,
set_seed,
AdamW,
DataCollatorForLanguageModeling,
GPT2Config,
GPT2Tokenizer,
GPT2Model,
EarlyStoppingCallback,
HfArgumentParser,
Trainer
)
from transformers.trainer_utils import get_last_checkpoint, is_main_process
import logging
import math
import numpy as np
import os
import sys
import torch
import transformers
from torch.utils.data import Dataset
from ppcm_models.pytorch_pretrained_bert.modeling_adapter import GPT2LMHeadModel, GPT2Config
from utils.helper import load_model_recursive
set_caching_enabled(True)
logger = logging.getLogger(__name__)
class BookcorpusopenGenreAdapterDataset(Dataset):
def __init__(self, data_args, split, tokenizer, genre=None, adapter_id=-1,
sample_row=100, match_up_to_n_genres=None, truncate=True,
max_seq_len=512, add_special_tokens=True,
*args, **kwargs):
super(BookcorpusopenGenreAdapterDataset, self).__init__(*args, **kwargs)
"""
Args:
adapter_id: int, adapter_id for the genre we want the adapter to be trained with
"""
self.data_args = data_args
self.tokenizer = tokenizer
self.add_special_tokens = add_special_tokens
self.truncate = truncate
self.max_seq_len = max_seq_len
self.adapter_id = adapter_id
self.preprocessing_num_workers = data_args.preprocessing_num_workers
self.dataset = self.load_bookcorpusopen(split, genre,
match_up_to_n_genres,
sample_row)
def load_bookcorpusopen(self, split, genre='Fiction',
match_up_to_n_genres=None, sample_row=None):
"""
Load bookcorpusopen from pyarrow file.
Further improvement:
Group, concat, and truncate entries based on the adapter_id after tokenization
Args:
split: string, {train, valid, test}
genre: string, genre that we want the adapter to be trained with, e.g. 'Fiction'
match_up_to_n_genres: int, how many of the firsts bookcorpusopen genres entries
is considered as a genre to match with the genre input.
None defaults to use all bookcorpusopen genres to match.
sample_row: int, set the int number to sample the dataset,
None means using all the datasets samples available
match_up_to_n_genres
Returns:
dataset: tokenized huggingface dataset format from one of the bookcorpusopen split,
with the adapter_id attached, and without any adapter_id = -1
"""
def genre_match(entry_genres_string_list, genre, match_up_to_n_genres):
"""
True to the genre that match to match_up_to_n_genres genres from the entry_genres
else false
"""
story_genre_list = [genre[1:-1] for genre in entry_genres_string_list[1:-1].split(', ')]
story_genre_stringlist = ", ".join(story_genre_list[:match_up_to_n_genres])
return genre.lower() in story_genre_stringlist.lower()
def map_tokenization(batch):
self.tokenizer.pad_token = self.tokenizer.eos_token
tokenized = self.tokenizer(batch[self.data_args.bookcorpusopen_story_column_name],
truncation=self.truncate,
max_length=self.max_seq_len,
add_special_tokens=self.add_special_tokens)
return tokenized
# Main data processing function that will concatenate all texts
# from our dataset and generate chunks of max_seq_len.
def group_texts(examples):
# Concatenate all texts.
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder, we could add padding if the model supported
# it instead of this drop, you can customize this part to your needs.
if total_length >= self.max_seq_len:
total_length = (total_length // self.max_seq_len) * self.max_seq_len
# Split by chunks of max_len.
result = {
k: [t[i : i + self.max_seq_len] \
for i in range(0, total_length, self.max_seq_len)]
for k, t in concatenated_examples.items()
}
return result
def remove_remainder(input_ids):
return len(input_ids) == self.max_seq_len
# load bookcorpusopen from arrow file
datasets = DatasetDict()
print('Loading train, validation, test dataset...')
datasets = load_from_disk(self.data_args.dataset_path)
print('Loaded')
# Select rows sampled and filter for the matching genres
sample_row = len(datasets[split]) if sample_row == None else sample_row
dataset = datasets[split].select(np.arange(0,sample_row,1))\
.filter(lambda x: genre_match(x['genre'], genre, match_up_to_n_genres)\
, num_proc=self.preprocessing_num_workers)
# Tokenize with huggingface datasets mapping function
tokenized_dataset = dataset.map(
map_tokenization,
remove_columns=self.data_args.bookcorpusopen_story_column_name,
num_proc=self.preprocessing_num_workers,
load_from_cache_file=True
)
print(split, 'split tokenized')
group_concatted_dataset = tokenized_dataset.map(
group_texts,
batched=True,
num_proc=self.preprocessing_num_workers,
load_from_cache_file=True,
desc=f"Grouping texts in chunks of {self.max_seq_len}",
)
dataset = group_concatted_dataset.filter(lambda x: remove_remainder(x['input_ids'])\
, num_proc=self.preprocessing_num_workers)
return dataset
def __getitem__(self, index):
forward_inputs = {}
forward_inputs['task_id'] = self.adapter_id
forward_inputs['input_ids'] = [self.dataset[index]['input_ids']]
forward_inputs["labels"] = forward_inputs["input_ids"].copy()
return forward_inputs
def __len__(self):
return self.dataset.num_rows
def run(model_args, data_args, training_args):
model_args.model_path = f'ppcm_models/dialoGPT/{model_args.model_size}/'
config = GPT2Config.from_json_file(os.path.join(model_args.model_path, 'config.json'))
tokenizer = GPT2Tokenizer.from_pretrained(model_args.model_path)
## Load either Adapters' checkpoint, or just finetuned DialoGPT
if(model_args.load_checkpoint_adapter != ""):
print("Loading ADAPTERS")
model = load_model_recursive(GPT2LMHeadModel(config), model_args.load_checkpoint_adapter, \
model_args, verbose=True)
else:
model = load_model_recursive(GPT2LMHeadModel(config), \
model_args.model_path+f"{model_args.model_size}_ft.pkl", \
model_args, verbose=True)
## Load GPT2 instead of DialoGPT
print('Load pretrained GPT2')
if model_args.model_size == 'small':
pt_gpt2_model = GPT2Model.from_pretrained('gpt2')
elif model_args.model_size == 'medium':
pt_gpt2_model = GPT2Model.from_pretrained('gpt2-medium')
elif model_args.model_size == 'large':
pt_gpt2_model = GPT2Model.from_pretrained('gpt2-large')
model.transformer.wte.weight = pt_gpt2_model.wte.weight
model.transformer.wpe.weight = pt_gpt2_model.wpe.weight
layers = np.arange(0,len(pt_gpt2_model.h),1)
for layer in layers:
model.transformer.h[layer].ln_1.weight = pt_gpt2_model.h[layer].ln_1.weight
model.transformer.h[layer].attn.c_attn.weight = pt_gpt2_model.h[layer].attn.c_attn.weight
model.transformer.h[layer].attn.c_proj.weight = pt_gpt2_model.h[layer].attn.c_proj.weight
model.transformer.h[layer].ln_2.weight = pt_gpt2_model.h[layer].ln_2.weight
model.transformer.h[layer].mlp.c_fc.weight = pt_gpt2_model.h[layer].mlp.c_fc.weight
model.transformer.h[layer].mlp.c_proj.weight = pt_gpt2_model.h[layer].mlp.c_proj.weight
print('GPT2 pretrained params loaded to previous DialoGPT adapter')
for n, p in model.named_parameters():
if "adapter" not in str(n):
p.requires_grad = False
parameters_to_update = [p for n, p in model.named_parameters() if "adapter" in str(n)]
print('GPT2 param frozen, Adapter is trainable and initialized with AdamW')
# Load the preprocessed dataset splits
dataset_dict = {}
for split in ['train', 'valid', 'test']:
dataset_dict[split] = BookcorpusopenGenreAdapterDataset(
data_args, split, tokenizer, genre=data_args.genre,
adapter_id=data_args.adapter_id, sample_row=data_args.sample_row,
match_up_to_n_genres=data_args.match_up_to_n_genres,
max_seq_len=model_args.max_seq_len)
for i in range(len(dataset_dict[split])):
input_ids_len = len(dataset_dict[split][i]['input_ids'][0])
if input_ids_len < model_args.max_seq_len:
print(split, i, input_ids_len)
def preprocess_logits_for_metrics(logits, labels):
if isinstance(logits, tuple):
# Depending on the model and config, logits may contain extra tensors,
# like past_key_values, but logits always come first
logits = logits[0]
return logits.argmax(dim=-1)
metric = load_metric("accuracy")
def compute_metrics(eval_preds):
preds, labels = eval_preds
# preds have the same shape as the labels, after the argmax(-1) has been calculated
# by preprocess_logits_for_metrics but we need to shift the labels
labels = labels[:, 1:].reshape(-1)
preds = preds[:, :-1].reshape(-1)
return metric.compute(predictions=preds, references=labels)
print('Preparing Trainer...')
# Initialize Trainer
trainer = Trainer(
train_dataset=dataset_dict['train'],
eval_dataset=dataset_dict['valid'],
model=model,
data_collator=default_data_collator,
args=training_args,
compute_metrics=compute_metrics if training_args.do_eval else None,
preprocess_logits_for_metrics=preprocess_logits_for_metrics if training_args.do_eval else None,
callbacks=[EarlyStoppingCallback(early_stopping_patience=training_args.early_stopping_patience)]
)
### Save path
if not os.path.exists(training_args.output_dir):
os.makedirs(training_args.output_dir, exist_ok=True)
run_name = 'GPT2{}_adapterid{}_genre{}_matched{}_sample{}_maxseqlen{}_bs{}_lr{}_{}epoch_wd{}_ws{}'\
.format(model_args.model_size,
data_args.adapter_id,
data_args.genre,
data_args.match_up_to_n_genres,
data_args.sample_row,
model_args.max_seq_len,
training_args.per_device_train_batch_size,
training_args.learning_rate,
training_args.num_train_epochs,
training_args.weight_decay,
training_args.warmup_steps)
training_args.output_dir = training_args.output_dir + run_name
###
# Training Phase
###
if training_args.do_train:
print('*** Training Phase ***')
checkpoint = None
# Detecting last checkpoint.
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
if training_args.resume_from_checkpoint is not None:
checkpoint = training_args.resume_from_checkpoint
elif last_checkpoint is not None:
checkpoint = last_checkpoint
train_result = trainer.train(resume_from_checkpoint=checkpoint)
### Saving
trainer.save_model() # Saves the tokenizer too for easy upload
metrics = train_result.metrics
metrics["train_samples"] = len(dataset_dict["train"])
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
###
# Evaluation Phase
###
if training_args.do_eval:
print("*** Evaluation Phase ***")
metrics = trainer.evaluate()
metrics["eval_samples"] = len(dataset_dict["valid"])
try:
perplexity = math.exp(metrics["eval_loss"])
except OverflowError:
perplexity = float("inf")
metrics["perplexity"] = perplexity
trainer.log_metrics("eval", metrics)
trainer.save_metrics("eval", metrics)
kwargs = {"finetuned_from": "GPT2"+model_args.model_size, "tasks": "text-generation"}
data_args.dataset_name = "Bookcorpusopen"
data_args.dataset_config_name = "Chunked"
if data_args.dataset_name is not None:
kwargs["dataset_tags"] = data_args.dataset_name
if data_args.dataset_config_name is not None:
kwargs["dataset_args"] = data_args.dataset_config_name
kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
else:
kwargs["dataset"] = data_args.dataset_name
if training_args.push_to_hub:
trainer.push_to_hub(**kwargs)
else:
trainer.create_model_card(**kwargs)
#####
# Entry Point
#####
def main():
###
# Parsing & Initialization
###
# Parse argument
parser = HfArgumentParser((ModelArguments, DataArguments, TrainingArguments))
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
else:
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
model_args.model_name_or_path = 'GPT2-'+model_args.model_size
# Set random seed
set_seed(training_args.seed)
# Detect last checkpoint
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
###
# Prepare logger
###
# Init logging
os.makedirs("./log", exist_ok=True)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout), logging.FileHandler(
"./log/log__{}".format(model_args.model_name_or_path.replace("/", "_")), mode="w")],
)
logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN)
# Log on each process the small summary:
logger.warning(
f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
)
# Set the verbosity to warn of the Transformers logger (on main process only):
if is_main_process(training_args.local_rank):
transformers.utils.logging.set_verbosity(transformers.logging.WARNING)
logger.info("Training/evaluation parameters %s", training_args)
run(model_args, data_args, training_args)
if __name__ == '__main__':
main()