-
Notifications
You must be signed in to change notification settings - Fork 9
/
trainer_base.py
148 lines (117 loc) · 5.08 KB
/
trainer_base.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
import torch.backends.cudnn as cudnn
import torch.multiprocessing as mp
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
import os
import collections
from pathlib import Path
from packaging import version
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
import logging
from pprint import pprint
from utils import load_state_dict, set_global_logging_level
proj_dir = Path(__file__).resolve().parent.parent
class TrainerBase(object):
def __init__(self, args):
self.args = args
self.verbose = True
if self.args.distributed:
if self.args.gpu != 0:
self.verbose = False
if not self.verbose:
set_global_logging_level(logging.ERROR, ["transformers"])
def create_optimizer_and_scheduler(self):
if self.verbose:
print('Building Optimizer')
lr_scheduler = None
if 'adamw' in self.args.optim:
from transformers.optimization import get_linear_schedule_with_warmup
batch_per_epoch = len(self.train_loader)
t_total = batch_per_epoch // self.args.gradient_accumulation_steps * self.args.epochs
warmup_steps = self.args.warmup_steps
if warmup_steps < 1:
warmup_ratio = warmup_steps
warmup_steps = int(t_total * self.args.warmup_steps)
else:
warmup_ratio = warmup_steps / t_total
if self.verbose:
print("Batch per epoch: %d" % batch_per_epoch)
print("Total Iters: %d" % t_total)
print('Warmup ratio:', warmup_ratio)
print('Warmup steps %d:' % warmup_steps)
parameters = filter(lambda p: p.requires_grad, self.model.parameters())
optim = torch.optim.AdamW(
parameters,
lr=self.args.lr,
)
lr_scheduler = get_linear_schedule_with_warmup(
optim, warmup_steps, t_total)
else:
optim = self.args.optimizer(
list(self.model.parameters()), self.args.lr)
return optim, lr_scheduler
def load_checkpoint(self, ckpt_path):
state_dict = load_state_dict(ckpt_path, 'cpu')
original_keys = list(state_dict.keys())
for key in original_keys:
if key.startswith("vis_encoder."):
new_key = 'encoder.' + key[len("vis_encoder."):]
state_dict[new_key] = state_dict.pop(key)
if key.startswith("model.vis_encoder."):
new_key = 'model.encoder.' + key[len("model.vis_encoder."):]
state_dict[new_key] = state_dict.pop(key)
results = self.model.load_state_dict(state_dict, strict=False)
if self.verbose:
print('Model loaded from ', ckpt_path)
# pprint(results)
def init_weights(self):
def init_bert_weights(module):
""" Initialize the weights."""
if isinstance(module, (nn.Linear, nn.Embedding)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=1)
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
self.model.apply(init_bert_weights)
self.model.init_weights()
def predict(self):
pass
def evaluate(self):
pass
def save(self, name):
if not os.path.isdir(self.args.ckpt_dir):
os.makedirs(self.args.ckpt_dir, exist_ok=True)
state_dict = self.model.state_dict()
original_keys = list(state_dict.keys())
clip_keys = []
for key in original_keys:
if "clip_model." in key:
clip_keys.append(key)
for key in clip_keys:
del state_dict[key]
torch.save(state_dict, os.path.join(self.args.ckpt_dir, "%s.pth" % name))
print('Model saved at', os.path.join(self.args.ckpt_dir, "%s.pth" % name))
def load(self, path, loc=None):
if loc is None and hasattr(self.args, 'gpu'):
loc = f'cuda:{self.args.gpu}'
state_dict = torch.load("%s.pth" % path, map_location=loc)
original_keys = list(state_dict.keys())
for key in original_keys:
if key.startswith("module.vis_encoder."):
new_key = 'module.encoder.' + key[len("module.vis_encoder."):]
state_dict[new_key] = state_dict.pop(key)
if key.startswith("module.model.vis_encoder."):
new_key = 'module.model.encoder.' + \
key[len("module.model.vis_encoder."):]
state_dict[new_key] = state_dict.pop(key)
results = self.model.load_state_dict(state_dict, strict=False)
if self.verbose:
print('Model loaded from ', path)
# pprint(results)