-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
241 lines (203 loc) · 9.33 KB
/
trainer.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
import glob
import json
import torch
import shutil
import torch.nn as nn
import torch.utils.data
from typing import Dict
from transformers import get_linear_schedule_with_warmup, get_cosine_schedule_with_warmup
from transformers import AdamW
from doc import Dataset, collate
from utils import AverageMeter, ProgressMeter
from utils import save_checkpoint, delete_old_ckt, report_num_trainable_parameters, move_to_cuda, get_model_obj
from metric import accuracy
from models import build_model, ModelOutput
from dict_hub import build_tokenizer
import logging
from logger_config import logger
import ipdb
from collections import OrderedDict
class Trainer:
def __init__(self, args, ngpus_per_node):
self.args = args
self.ngpus_per_node = ngpus_per_node
build_tokenizer(args)
# save the log
fh = logging.FileHandler('{}/log.txt'.format(self.args.model_dir))
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# create model
logger.info("=> creating model")
self.model = build_model(self.args)
# continue training if ckpt given
if args.continue_training:
ckt_dict = torch.load(args.continue_training, map_location=lambda storage, loc: storage)
# DataParallel will introduce 'module.' prefix
state_dict = ckt_dict['state_dict']
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if k.startswith('module.'):
k = k[len('module.'):]
new_state_dict[k] = v
self.model.load_state_dict(new_state_dict, strict=True)
# patience epochs
self.patience = self.args.patience
logger.info(self.model)
self._setup_training()
# define loss function (criterion) and optimizer
self.criterion = nn.CrossEntropyLoss().cuda()
self.optimizer = AdamW([p for p in self.model.parameters() if p.requires_grad],
lr=args.lr,
weight_decay=args.weight_decay)
report_num_trainable_parameters(self.model)
train_dataset = Dataset(path=args.train_path, task=args.task)
valid_dataset = Dataset(path=args.valid_path, task=args.task) if args.valid_path else None
num_training_steps = args.epochs * len(train_dataset) // max(args.batch_size, 1)
args.warmup = min(args.warmup, num_training_steps // 10)
logger.info('Total training steps: {}, warmup steps: {}'.format(num_training_steps, args.warmup))
self.scheduler = self._create_lr_scheduler(num_training_steps)
self.best_metric = None
self.train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=True,
collate_fn=collate,
num_workers=args.workers,
pin_memory=True,
drop_last=True)
self.valid_loader = None
if valid_dataset:
self.valid_loader = torch.utils.data.DataLoader(
valid_dataset,
batch_size=args.batch_size * 2,
shuffle=True,
collate_fn=collate,
num_workers=args.workers,
pin_memory=True)
def train_loop(self):
if self.args.use_amp:
self.scaler = torch.cuda.amp.GradScaler()
best_epoch = None
for epoch in range(self.args.epochs):
# train for one epoch
self.train_epoch(epoch)
is_best = self._run_eval(epoch=epoch)
if is_best:
best_epoch = epoch
else:
if best_epoch and self.patience and (epoch - best_epoch + 1 >= self.patience):
logger.info('Lost patience at {} epoch. Best epoch at {}'.format(epoch, best_epoch))
break
@torch.no_grad()
def _run_eval(self, epoch, step=0):
metric_dict = self.eval_epoch(epoch)
is_best = self.valid_loader and (self.best_metric is None or metric_dict['Acc@1'] > self.best_metric['Acc@1'])
if is_best:
self.best_metric = metric_dict
filename = '{}/checkpoint_{}_{}.mdl'.format(self.args.model_dir, epoch, step)
if step == 0:
filename = '{}/checkpoint_epoch{}.mdl'.format(self.args.model_dir, epoch)
save_checkpoint({
'epoch': epoch,
'args': self.args.__dict__,
'state_dict': self.model.state_dict(),
}, is_best=is_best, filename=filename)
delete_old_ckt(path_pattern='{}/checkpoint_*.mdl'.format(self.args.model_dir),
keep=self.args.max_to_keep)
return is_best
@torch.no_grad()
def eval_epoch(self, epoch) -> Dict:
if not self.valid_loader:
return {}
losses = AverageMeter('Loss', ':.4')
top1 = AverageMeter('Acc@1', ':6.2f')
top3 = AverageMeter('Acc@3', ':6.2f')
for i, batch_dict in enumerate(self.valid_loader):
self.model.eval()
if torch.cuda.is_available():
batch_dict = move_to_cuda(batch_dict)
batch_size = len(batch_dict['batch_data'])
outputs = self.model(**batch_dict)
outputs = get_model_obj(self.model).compute_logits(output_dict=outputs, batch_dict=batch_dict)
outputs = ModelOutput(**outputs)
logits, labels = outputs.logits, outputs.labels
loss = self.criterion(logits, labels)
losses.update(loss.item(), batch_size)
acc1, acc3 = accuracy(logits, labels, topk=(1, 3))
top1.update(acc1.item(), batch_size)
top3.update(acc3.item(), batch_size)
metric_dict = {'Acc@1': round(top1.avg, 3),
'Acc@3': round(top3.avg, 3),
'loss': round(losses.avg, 3)}
logger.info('Epoch {}, valid metric: {}'.format(epoch, json.dumps(metric_dict)))
return metric_dict
def train_epoch(self, epoch):
losses = AverageMeter('Loss', ':.4')
top1 = AverageMeter('Acc@1', ':6.2f')
top3 = AverageMeter('Acc@3', ':6.2f')
inv_t = AverageMeter('InvT', ':6.2f')
progress = ProgressMeter(
len(self.train_loader),
[losses, inv_t, top1, top3],
prefix="Epoch: [{}]".format(epoch))
for i, batch_dict in enumerate(self.train_loader):
# switch to train mode
self.model.train()
if torch.cuda.is_available():
batch_dict = move_to_cuda(batch_dict)
batch_size = len(batch_dict['batch_data'])
# compute output
if self.args.use_amp:
with torch.cuda.amp.autocast():
outputs = self.model(**batch_dict)
else:
outputs = self.model(**batch_dict)
outputs = get_model_obj(self.model).compute_logits(output_dict=outputs, batch_dict=batch_dict)
outputs = ModelOutput(**outputs)
logits, labels = outputs.logits, outputs.labels
assert logits.size(0) == batch_size
# head + relation -> tail
loss = self.criterion(logits, labels)
# tail -> head + relation
loss += self.criterion(logits[:, :batch_size].t(), labels)
acc1, acc3 = accuracy(logits, labels, topk=(1, 3))
top1.update(acc1.item(), batch_size)
top3.update(acc3.item(), batch_size)
inv_t.update(outputs.inv_t, 1)
losses.update(loss.item(), batch_size)
# compute gradient and do SGD step
self.optimizer.zero_grad()
if self.args.use_amp:
self.scaler.scale(loss).backward()
self.scaler.unscale_(self.optimizer)
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.args.grad_clip)
self.scaler.step(self.optimizer)
self.scaler.update()
else:
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.args.grad_clip)
self.optimizer.step()
self.scheduler.step()
if i % self.args.print_freq == 0:
progress.display(i)
if (i + 1) % self.args.eval_every_n_step == 0:
self._run_eval(epoch=epoch, step=i + 1)
logger.info('Learning rate: {}'.format(self.scheduler.get_last_lr()[0]))
def _setup_training(self):
if torch.cuda.device_count() > 1:
self.model = torch.nn.DataParallel(self.model).cuda()
elif torch.cuda.is_available():
self.model.cuda()
else:
logger.info('No gpu will be used')
def _create_lr_scheduler(self, num_training_steps):
if self.args.lr_scheduler == 'linear':
return get_linear_schedule_with_warmup(optimizer=self.optimizer,
num_warmup_steps=self.args.warmup,
num_training_steps=num_training_steps)
elif self.args.lr_scheduler == 'cosine':
return get_cosine_schedule_with_warmup(optimizer=self.optimizer,
num_warmup_steps=self.args.warmup,
num_training_steps=num_training_steps)
else:
assert False, 'Unknown lr scheduler: {}'.format(self.args.scheduler)