-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
executable file
·451 lines (398 loc) · 17.8 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import os
import json
import math
import torch
import random
import logging
import numpy as np
from collections import OrderedDict
from tensorboardX import SummaryWriter
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from transformers import (
WEIGHTS_NAME,
AdamW,
get_linear_schedule_with_warmup
)
from processors import processor_dict
from models import load_model, save_model
from evaluate import evaluate_func_dict, eval_checkpoint
from torch.nn import CrossEntropyLoss
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def get_optimizer(model, weight_decay, learning_rate, adam_epsilon):
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": weight_decay},
{"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], "weight_decay": 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters, lr=learning_rate, eps=adam_epsilon)
return optimizer
def score_comp(score_a, score_b, metric_comp):
if metric_comp == "larger":
return score_a > score_b
elif metric_comp == "smaller":
return score_a < score_b
else:
raise NotImplementedError()
def evaluation_step(
args,
model, tokenizer, processor,
optimizer, scheduler,
dev_file_infos, test_file_infos,
eval_func,
global_step,
best_dev_score,
test_score_on_best_dev,
tb_writer=None,
tf_board_header="",
logger=None
):
if logger == None:
logger = logging.getLogger(__name__)
dev_dataset_results = eval_checkpoint(
args,
model, tokenizer, processor,
global_step=global_step,
file_infos=dev_file_infos,
eval_func=eval_func,
tb_writer=None,
tf_board_header=tf_board_header,
mode="dev",
logger=logger
)
logger.info("Dev Evauation : {}".format(json.dumps(dev_dataset_results, indent=4)))
tb_writer.add_scalar(
"{}_avg_dev".format(tf_board_header),
dev_dataset_results["avg_metric"],
global_step
)
if (score_comp(dev_dataset_results["avg_metric"], best_dev_score["avg_metric"], args.dev_metric_comp)):
best_dev_score = dev_dataset_results
save_model(
args.output_dir,
args, 0,
model, tokenizer,
optimizer, scheduler,
logger, prefix="best_dev"
)
logger.info("New BEST-DEV score found")
open(os.path.join(args.output_dir, "best_dev_score.json"), "w").write(json.dumps(best_dev_score, indent=4))
if args.evaluate_test_on_best_dev:
test_dataset_results = eval_checkpoint(
args,
model, tokenizer, processor,
global_step=global_step,
file_infos=test_file_infos,
eval_func=eval_func,
tb_writer=None,
tf_board_header=tf_board_header,
mode="test",
logger=logger
)
logger.info("Test Evauation : {}".format(json.dumps(test_dataset_results, indent=4)))
test_score_on_best_dev = test_dataset_results
open(os.path.join(args.output_dir, "test_score_on_best_dev.json"), "w").write(json.dumps(test_score_on_best_dev, indent=4))
return best_dev_score, test_score_on_best_dev
def training_loop(
args,
train_datasets,
model, tokenizer, processor,
optimizer, scheduler,
dev_dataset_results,
eval_func=None,
dev_file_infos=None,
test_file_infos=None,
tf_board_header="",
logger=None
):
if logger is None:
logger = logging.getLogger(__name__)
tb_writer = None
if args.local_rank in [-1, 0]:
# tensorboard setup
logger.info("Tensorboard summary creation.")
tb_path = os.path.join(args.output_dir, "tf_board")
if not os.path.exists(tb_path):
os.makedirs(tb_path)
tb_writer = SummaryWriter(tb_path)
# At first evaluate the result
test_score_on_best_dev = None,
if ( args.local_rank == -1 and args.evaluate_during_training):
dev_dataset_results, test_score_on_best_dev = evaluation_step(
args,
model, tokenizer, processor,
optimizer, scheduler,
dev_file_infos, test_file_infos,
eval_func,
global_step=0,
best_dev_score=dev_dataset_results,
test_score_on_best_dev=test_score_on_best_dev,
tb_writer=tb_writer,
tf_board_header=tf_board_header,
logger=logger
)
model.zero_grad()
# batch size setup
args.train_batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu)
logger.info("Iteratior Sampling rate calculation.")
# iteerator sampling rate
(dataset_len,
prob_index,
prob_list) = iterator_selection_prob(
args.sampling_penalty,
train_datasets,
logger
)
logger.info("Processing data loader.")
# Data loader creation
train_data_loader = []
total_num_of_samples = 0
for k in prob_index:
dataset = train_datasets[k]
total_num_of_samples += len(dataset)
train_sampler = RandomSampler(dataset) if args.local_rank == -1 else DistributedSampler(dataset)
data_loader = DataLoader(
dataset, sampler=train_sampler, batch_size=args.per_gpu_train_batch_size
)
train_data_loader.append((k, data_loader))
logger.info("Processing initial iterator.")
# Initial Iterator creation
train_iterators = []
for i in range(len(train_data_loader)):
assert train_data_loader[i][0] == prob_index[i]
train_iterators.append(iter(train_data_loader[i][1]))
tot_num_of_iterator = len(train_iterators)
num_of_batch_trained = [ 0 for i in range(tot_num_of_iterator) ]
# Number of steps calc
if args.max_steps > 0 :
total_training_steps = args.max_steps
else:
total_training_steps = int((total_num_of_samples * args.num_train_epochs) // (args.per_gpu_train_batch_size * args.gradient_accumulation_steps * torch.cuda.device_count()))
logger.info("Declare Optimizer and Scheduler if needed.")
# Declaring optimizer and scheduler
optimizer = get_optimizer(
model,
args.weight_decay,
args.learning_rate,
args.adam_epsilon
) if optimizer is None else optimizer
if args.fp16:
scaler = torch.cuda.amp.GradScaler()
if args.warmup_steps == -1:
assert args.warmup_percentage >=0 and args.warmup_percentage < 20
args.warmup_steps = int(max(0, (total_training_steps * args.warmup_percentage)//100))
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=total_training_steps
) if scheduler is None else scheduler
logger.info("Parallalize model and data.")
# Parallalize model and dataset
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[args.local_rank], output_device=args.local_rank, find_unused_parameters=True
)
if args.local_rank in [-1, 0]:
# Training log
logger.info("***** Running training *****")
logger.info(" Num examples = {}".format(total_num_of_samples))
logger.info(" Num Epochs = {}".format(args.num_train_epochs))
logger.info(" Instantaneous batch size per GPU = {}".format(args.per_gpu_train_batch_size))
logger.info(
" Total train batch size (w. parallel, distributed & accumulation) = {}".format(
args.train_batch_size
* args.gradient_accumulation_steps
* (torch.distributed.get_world_size() if args.local_rank != -1 else 1)
)
)
logger.info(" Gradient Accumulation steps = {}".format(args.gradient_accumulation_steps))
logger.info(" Total optimization steps = {} (forward steps : {})".format(
total_training_steps,
total_training_steps*args.gradient_accumulation_steps
)
)
logger.info(" Total warmup steps = {}".format(args.warmup_steps))
# Resume training
saved_global_step=-1
if os.path.exists(args.model_name_or_path) and args.resume_training:
saved_global_step = int(args.model_name_or_path.split("-")[-1].split("/")[0])
logger.info(" Continuing training from checkpoint, will skip to saved global_step : {}".format(saved_global_step))
# Training var declaration
tr_loss, logging_loss = 0.0, 0.0
steps, global_step = 0, 0
isUpdated = 0
# Precalculate iterator id for each steps
iterator_ids = []
total_number_of_batch = int(total_training_steps*args.gradient_accumulation_steps)
for _i in range(total_number_of_batch):
if _i % args.gradient_accumulation_steps == 0:
_iterator_id = np.random.choice(range(tot_num_of_iterator), p=prob_list)
iterator_ids.append(
_iterator_id
)
# Added here for reproductibility
set_seed(args.seed)
for iterator_ids_index in range(total_number_of_batch):
iterator_id = iterator_ids[iterator_ids_index]
model.train()
try:
batch = train_iterators[iterator_id].__next__()
except StopIteration:
train_iterators[iterator_id] = iter(train_data_loader[iterator_id][1])
batch = train_iterators[iterator_id].__next__()
num_of_batch_trained[ iterator_id ] += 1
# No need to train if current step is less than saved_global_step
if saved_global_step > global_step:
steps = steps + 1
if steps == args.gradient_accumulation_steps:
steps = 0
global_step = global_step + 1
continue
# Prepare batch
batch = tuple(t.to(args.device) for t in batch)
inputs = {
"input_ids": batch[0],
"attention_mask": batch[1],
"labels": batch[3],
"conf_penalty": args.conf_penalty if args.conf_penalty == 1 else None,
"marginal_entropy": args.marginal_entropy if args.marginal_entropy == 1 else None
}
if args.model_type != "distilbert":
# XLM and RoBERTa don't use segment_ids
inputs["token_type_ids"] = batch[2] if args.model_type in ["bert", "xlnet"] else None
# Inference
loss, _ = model(**inputs)
if args.n_gpu > 1:
loss = loss.mean()
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
# fp16 training
if args.fp16:
scaler.scale(loss).backward()
else:
loss.backward()
tr_loss += loss.item()
if (iterator_ids_index+1) % args.gradient_accumulation_steps == 0:
# fp16 training
if args.fp16:
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
# training step
if args.fp16:
scaler.step(optimizer)
scaler.update()
else:
optimizer.step()
scheduler.step()
model.zero_grad()
global_step += 1
if args.local_rank in [-1, 0] and args.logging_steps > 0 and global_step % args.logging_steps == 0:
# Log metrics
current_loss = (tr_loss - logging_loss) / args.logging_steps
tb_writer.add_scalar("lr", scheduler.get_lr()[0], global_step)
tb_writer.add_scalar("loss", current_loss , global_step)
logger.info("[GLOBAL_STEP] : {}/{}, [LOSS] :: {}".format(global_step, total_training_steps, current_loss))
logging_loss = tr_loss
# save checkpoint
if args.local_rank in [-1, 0] and args.save_steps > 0 and global_step % args.save_steps == 0:
save_model(args.output_dir, args, global_step, model, tokenizer, optimizer, scheduler, logger)
if ( args.local_rank in [-1, 0] and \
args.evaluate_during_training and \
args.eval_steps > 0 and \
global_step % args.eval_steps == 0):
dev_dataset_results, test_score_on_best_dev = evaluation_step(
args,
model, tokenizer, processor,
optimizer, scheduler,
dev_file_infos, test_file_infos,
eval_func,
global_step=global_step,
best_dev_score=dev_dataset_results,
test_score_on_best_dev=test_score_on_best_dev,
tb_writer=tb_writer,
tf_board_header=tf_board_header,
logger=logger
)
for enum_idx, (dataset_name, dataloader_obj) in enumerate(train_data_loader):
logger.info("TRAIN DATASET : {} : {} batch trained.".format(dataset_name, num_of_batch_trained[ enum_idx ]))
logger.info("Best-Dev : {}".format(json.dumps(dev_dataset_results, indent=4)))
if args.evaluate_test_on_best_dev:
logger.info("Test score on Best-Dev : {}".format(json.dumps(test_score_on_best_dev, indent=4)))
if args.local_rank in [-1, 0]:
tb_writer.close()
return global_step, tr_loss / global_step
def baseline_training(args, logger=None):
if logger is None:
logger = logging.getLogger(__name__)
if args.task_name not in processor_dict:
raise ValueError("Task not found in processor pool: {}".format((args.task_name)))
if args.task_name not in evaluate_func_dict:
raise ValueError("Task not found eval function pool: {}".format((args.task_name)))
processor = processor_dict[args.task_name](
src_lang=args.src_lang,
dev_lang=args.dev_lang,
tgt_lang=args.tgt_lang,
seed=args.seed,
percentage=args.train_data_percentage,
n_shot=None,
pad_token_label_id=CrossEntropyLoss().ignore_index
)
args.output_mode = processor.output_mode
label_list = processor.get_labels()
num_labels = len(label_list)
if args.local_rank not in [-1, 0]:
torch.distributed.barrier()
(config,
tokenizer,
model,
optimizer,
scheduler) = load_model(
task_name=args.task_name,
config_name=args.config_name,
tokenizer_name=args.tokenizer_name,
model_name_or_path=args.model_name_or_path,
num_labels=num_labels,
model_type=args.model_type,
logger=logger,
do_lower_case=args.do_lower_case,
cache_dir=args.cache_dir,
is_fp16=args.fp16
)
logger.info("Successfully loaded the config : {}, tokenizer : {}, model : {}".format(
args.config_name, args.tokenizer_name, args.model_name_or_path))
if args.local_rank == 0:
torch.distributed.barrier()
evaluate_func = evaluate_func_dict[args.task_name]
model.to(args.device)
train_datasets = {}
for data_file_info in args.train:
if data_file_info.split(";")[-1] in args.src_lang.split(";"):
logger.info("Process rank : {} , Loading : {}".format(args.local_rank, data_file_info))
train_dataset = processor.load_and_cache_examples(args, tokenizer, "train", data_file_info, logger)
train_datasets[data_file_info] = train_dataset
dev_file_infos, test_file_infos = [], []
for data_file_info in args.dev:
if data_file_info.split(";")[-1] in args.dev_lang.split(";"):
dev_file_infos.append(data_file_info)
for data_file_info in args.test:
if data_file_info.split(";")[-1] in args.tgt_lang.split(";"):
test_file_infos.append(data_file_info)
global_step, tr_loss = training_loop(
args, train_datasets,
model, tokenizer, processor,
optimizer, scheduler,
dev_dataset_results={"avg_metric":0},
eval_func=evaluate_func,
dev_file_infos=dev_file_infos if args.evaluate_during_training else None,
test_file_infos=test_file_infos if (args.evaluate_during_training and args.evaluate_test_on_best_dev) else None,
tf_board_header="{}_baseline".format(args.task_name),
logger=logger
)
logger.info(" global_step = %s, average loss = %s", global_step, tr_loss)