-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
287 lines (237 loc) · 10.4 KB
/
worker.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
import argparse
import os
import random
from typing import Union
import numpy as np
import torch
from UniTok import Vocab
from tqdm import tqdm
from transformers import get_linear_schedule_with_warmup
# from loader.bert_aggregator import BertAggregator
from loader.data import Data
from loader.task_depot.pretrain_task import PretrainTask
from model.auto_bert import AutoBert
from utils.config_init import ConfigInit
# from utils.config_initializer import init_config
from utils.gpu import GPU
from utils.monitor import Monitor
from utils.random_seed import seeding
from utils.time_printer import printer as print
from utils.logger import Logger
class Worker:
def __init__(self, project_args, project_exp, cuda=None):
self.args = project_args
self.exp = project_exp
self.logging = Logger(self.args.store.log_path, print)
self.device = self.get_device(cuda)
self.data = Data(
project_args=self.args,
project_exp=self.exp,
device=self.device,
)
self.model = AutoBert(
device=self.device,
bert_init=self.data.bert_init,
pretrain_depot=self.data.pretrain_depot,
)
self.model.to(self.device)
self.logging(self.model.bert.config)
self.save_model = self.model
if self.exp.mode == 'export':
self.m_optimizer = self.m_scheduler = None
else:
self.m_optimizer = torch.optim.Adam(
params=filter(lambda p: p.requires_grad, self.model.parameters()),
lr=self.exp.policy.lr
)
self.m_scheduler = get_linear_schedule_with_warmup(
self.m_optimizer,
num_warmup_steps=self.exp.policy.n_warmup,
num_training_steps=len(self.data.t_set) // self.exp.policy.batch_size * self.exp.policy.epoch,
)
print('training params')
for name, p in self.model.named_parameters():
if p.requires_grad:
print(name, p.data.shape)
self.attempt_loading()
@staticmethod
def get_device(cuda):
if cuda == -1:
return 'cpu'
if not cuda:
return GPU.auto_choose(torch_format=True)
return "cuda:{}".format(cuda)
def attempt_loading(self):
if self.exp.load.load_ckpt is not None:
load_path = os.path.join(self.args.store.save_dir, self.exp.load.load_ckpt)
self.logging("load model from exp {}".format(load_path))
state_dict = torch.load(load_path, map_location=self.device)
if '__rec__' in state_dict:
model_ckpt = state_dict['model']
else:
model_ckpt = state_dict
self.save_model.load_state_dict(model_ckpt, strict=not self.exp.load.relax_load)
load_status = False
if '__rec__' in state_dict:
if self.exp.mode != 'export' and not self.exp.load.load_model_only:
load_status = True
self.m_optimizer.load_state_dict(state_dict['optimizer'])
self.m_scheduler.load_state_dict(state_dict['scheduler'])
print('Load optimizer and scheduler:', load_status)
def log_interval(self, epoch, step, task: PretrainTask, loss):
self.logging(
"epoch {}, step {}, "
"task {}, "
"loss {:.4f}".format(
epoch,
step + 1,
task.name,
loss.item()
))
def train(self):
monitor = Monitor(
save_dir=self.args.store.save_dir,
top=1,
early_stop=self.exp.policy.early_stop,
)
self.logging('Start Training')
tasks = self.exp.tasks
tasks = [self.data.pretrain_depot[task] if isinstance(task, str) else task for task in tasks]
train_steps = len(self.data.t_set) // self.exp.policy.batch_size
accumulate_step = 0
assert self.exp.policy.accumulate_batch >= 1
self.m_optimizer.zero_grad()
for epoch in range(self.exp.policy.epoch_start, self.exp.policy.epoch + self.exp.policy.epoch_start):
self.model.train()
task = random.choice(tasks)
t_loader = self.data.get_t_loader(task)
for step, batch in enumerate(tqdm(t_loader)):
task_output = self.model(
batch=batch,
task=task,
)
loss = task.calculate_loss(batch, task_output)
loss.backward()
accumulate_step += 1
if accumulate_step >= self.exp.policy.accumulate_batch:
self.m_optimizer.step()
self.m_scheduler.step()
self.m_optimizer.zero_grad()
accumulate_step = 0
if self.exp.policy.check_interval:
if self.exp.policy.check_interval < 0: # step part
if (step + 1) % max(train_steps // (-self.exp.policy.check_interval), 1) == 0:
self.log_interval(epoch, step, task, loss.loss)
else:
if (step + 1) % self.exp.policy.check_interval == 0:
self.log_interval(epoch, step, task, loss.loss)
print('end epoch')
avg_loss = self.dev(task=task)
self.logging("epoch {} finished,"
"task {}, "
"loss {:.4f}".format(epoch, task.name, avg_loss))
try:
monitor.push(
epoch=epoch,
loss=avg_loss,
state_dict=dict(
model=self.model.state_dict(),
optimizer=self.m_optimizer.state_dict(),
scheduler=self.m_scheduler.state_dict(),
__rec__=True
)
)
except:
break
self.logging('Training Ended')
def dev(self, task: Union[str, PretrainTask]):
if isinstance(task, str):
task = self.data.pretrain_depot[task]
avg_loss = torch.tensor(.0).to(self.device)
self.model.eval()
d_loader = self.data.get_d_loader(task)
for step, batch in enumerate(tqdm(d_loader)):
with torch.no_grad():
task_output = self.model(
batch=batch,
task=task,
)
loss = task.calculate_loss(batch, task_output)
avg_loss += loss.loss
avg_loss /= len(self.data.d_set) / self.exp.policy.batch_size
return avg_loss.item()
def export(self):
# bert_aggregator = BertAggregator(
# layers=self.exp.save.layers,
# layer_strategy=self.exp.save.layer_strategy,
# union_strategy=self.exp.save.union_strategy,
# )
features = torch.zeros(
self.args.bert_config.num_hidden_layers,
self.data.depot.sample_size,
self.args.bert_config.hidden_size,
dtype=torch.float
).to(self.device)
print(self.exp.save.layer_strategy)
for loader in [self.data.get_t_loader(self.data.non_task), self.data.get_d_loader(self.data.non_task)]:
for batch in tqdm(loader):
with torch.no_grad():
task_output = self.model(batch=batch, task=self.data.non_task)
hidden_states = task_output.hidden_states
for index, hidden_state in enumerate(hidden_states[1:]):
if self.exp.save.layer_strategy == 'mean':
attention_sum = batch['attention_mask'].to(
self.device).sum(-1).unsqueeze(-1).repeat(1, 1, self.args.bert_config.hidden_size)
attention_mask = batch['attention_mask'].to(
self.device).unsqueeze(-1).repeat(1, 1, self.args.bert_config.hidden_size)
features[index][batch['append_info'][self.exp.save.key]] = (hidden_state * attention_mask).sum(
1) / attention_sum
elif self.exp.save.layer_strategy == 'cls':
features[index][batch['append_info'][self.exp.save.key]] = hidden_state[:, 0, :]
save_path = os.path.join(self.args.store.save_dir, self.exp.save.feature_path)
features = features.cpu()
if self.exp.save.translate_vocab:
print('translate vocab')
original_vocab = self.data.depot.vocab_depot(name='nid')
translate_vocab = Vocab(name='nid')
translate_vocab.load(self.exp.save.translate_vocab, as_path=True)
assert original_vocab.get_size() == translate_vocab.get_size()
new_features = torch.zeros(
self.args.bert_config.num_hidden_layers,
self.data.depot.sample_size,
self.args.bert_config.hidden_size,
dtype=torch.float
)
for index in translate_vocab.index2obj:
new_features[:, index, :] = features[:, original_vocab.obj2index[translate_vocab.index2obj[index]], :]
features = new_features
if self.exp.save.union_strategy == 'mean':
features = features.mean(0)
elif self.exp.save.union_strategy == 'last':
features = features[-1]
else:
assert self.exp.save.union_strategy == 'raw'
features = features.transpose(0, 1)
np.save(save_path, features.numpy(), allow_pickle=False)
def run(self):
if self.exp.mode == 'train':
self.train()
elif self.exp.mode == 'dev':
loss = self.dev(task='mlm')
print("dev loss {:.4f}".format(loss))
elif self.exp.mode == 'export':
self.export()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str)
parser.add_argument('--exp', type=str)
parser.add_argument('--cuda', type=int, default=None)
args = parser.parse_args()
# config, exp = init_config(args.config, args.exp)
config = ConfigInit(makedirs=[
'config.store.save_dir',
]).parse(args)
config, exp = config.config, config.exp
seeding(2021)
worker = Worker(project_args=config, project_exp=exp, cuda=args.cuda)
worker.run()