-
Notifications
You must be signed in to change notification settings - Fork 26
/
train_single.py
370 lines (324 loc) · 11.5 KB
/
train_single.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
import os
import fastNLP
from fastNLP import (
Trainer,
Tester,
Callback,
LRScheduler,
LossInForward,
AccuracyMetric,
SpanFPreRecMetric,
GradientClipCallback,
logger,
)
from src.metric import YangJieSpanMetric
from src import utils
from src.utils import MetricInForward, get_optim
from src.models import get_model
from torch.optim.lr_scheduler import LambdaLR
import torch
from tensorboardX import SummaryWriter
from src.prune import Pruning
class LogCallback(Callback):
def __init__(self, path, print_every=50):
super().__init__()
self._log = utils.get_logger(__name__)
self.avg_loss = 0.0
self.print_every = print_every
self.writer = SummaryWriter(log_dir=path)
def on_backward_begin(self, loss):
self.avg_loss += loss.item()
if self.print_every > 0 and self.step % self.print_every == 0:
self.writer.add_scalar("loss", self.avg_loss, self.step)
self.avg_loss = 0.0
def on_valid_end(self, eval_result, metric_key, optimizer, is_better_eval):
scalars = {}
for res in eval_result.values():
if "acc" in res:
scalars.update(res)
elif "f" in res:
scalars.update(res)
self.writer.add_scalars("dev acc", scalars, self.epoch)
class LRStep(Callback):
def __init__(self, scheduler):
super().__init__()
self.scheduler = scheduler
def on_batch_end(self):
self.scheduler.step()
SEQ_LABEL_TASK = {"pos", "chunk", "ner"}
def get_metric(res):
if "acc" in res:
return "acc", res["acc"]
elif "f" in res:
return "f", res["f"]
for n, v in res.items():
if isinstance(v, dict):
ans = get_metric(v)
if ans is not None:
return ans
return None
def train_mlt_single(args):
global logger
logger.info(args)
task_lst, vocabs = utils.get_data(args.data_path)
task_db = task_lst[args.task_id]
train_data = task_db.train_set
dev_data = task_db.dev_set
test_data = task_db.test_set
task_name = task_db.task_name
if args.debug:
train_data = train_data[:200]
dev_data = dev_data[:200]
test_data = test_data[:200]
args.epochs = 3
args.pruning_iter = 3
summary_writer = SummaryWriter(
log_dir=os.path.join(args.tb_path, "global/%s" % task_name)
)
logger.info("task name: {}, task id: {}".format(task_db.task_name, task_db.task_id))
logger.info(
"train len {}, dev len {}, test len {}".format(
len(train_data), len(dev_data), len(test_data)
)
)
# init model
model = get_model(args, task_lst, vocabs)
logger.info("model: \n{}".format(model))
if args.init_weights is not None:
utils.load_model(model, args.init_weights)
if utils.need_acc(task_name):
metrics = [AccuracyMetric(target="y"), MetricInForward(val_name="loss")]
metric_key = "acc"
else:
metrics = [
YangJieSpanMetric(
tag_vocab=vocabs[task_name],
pred="pred",
target="y",
seq_len="seq_len",
encoding_type="bioes" if task_name == "ner" else "bio",
),
MetricInForward(val_name="loss"),
]
metric_key = "f"
logger.info(metrics)
need_cut_names = list(set([s.strip() for s in args.need_cut.split(",")]))
prune_names = []
for name, p in model.named_parameters():
if not p.requires_grad or "bias" in name:
continue
for n in need_cut_names:
if n in name:
prune_names.append(name)
break
# get Pruning class
pruner = Pruning(
model, prune_names, final_rate=args.final_rate, pruning_iter=args.pruning_iter
)
if args.init_masks is not None:
pruner.load(args.init_masks)
pruner.apply_mask(pruner.remain_mask, pruner._model)
# save checkpoint
os.makedirs(args.save_path, exist_ok=True)
logger.info('Saving init-weights to {}'.format(args.save_path))
torch.save(
model.cpu().state_dict(), os.path.join(args.save_path, "init_weights.th")
)
torch.save(args, os.path.join(args.save_path, "args.th"))
# start training and pruning
summary_writer.add_scalar("remain_rate", 100.0, 0)
summary_writer.add_scalar("cutoff", 0.0, 0)
if args.init_weights is not None:
init_tester = Tester(
test_data,
model,
metrics=metrics,
batch_size=args.batch_size,
num_workers=4,
device="cuda",
use_tqdm=False,
)
res = init_tester.test()
logger.info("No init testing, Result: {}".format(res))
del res, init_tester
for prune_step in range(pruner.pruning_iter + 1):
# reset optimizer every time
optim_params = [p for p in model.parameters() if p.requires_grad]
# utils.get_logger(__name__).debug(optim_params)
utils.get_logger(__name__).debug(len(optim_params))
optimizer = get_optim(args.optim, optim_params)
# optimizer = TriOptim(optimizer, args.n_filters, args.warmup, args.decay)
factor = pruner.cur_rate / 100.0
factor = 1.0
# print(factor, pruner.cur_rate)
for pg in optimizer.param_groups:
pg["lr"] = factor * pg["lr"]
utils.get_logger(__name__).info(optimizer)
trainer = Trainer(
train_data,
model,
loss=LossInForward(),
optimizer=optimizer,
metric_key=metric_key,
metrics=metrics,
print_every=200,
batch_size=args.batch_size,
num_workers=4,
n_epochs=args.epochs,
dev_data=dev_data,
save_path=None,
sampler=fastNLP.BucketSampler(batch_size=args.batch_size),
callbacks=[
pruner,
# LRStep(lstm.WarmupLinearSchedule(optimizer, args.warmup, int(len(train_data)/args.batch_size*args.epochs)))
GradientClipCallback(clip_type="norm", clip_value=5),
LRScheduler(
lr_scheduler=LambdaLR(optimizer, lambda ep: 1 / (1 + 0.05 * ep))
),
LogCallback(path=os.path.join(args.tb_path, "No", str(prune_step))),
],
use_tqdm=False,
device="cuda",
check_code_level=-1,
)
res = trainer.train()
logger.info("No #{} training, Result: {}".format(pruner.prune_times, res))
name, val = get_metric(res)
summary_writer.add_scalar("prunning_dev_acc", val, prune_step)
tester = Tester(
test_data,
model,
metrics=metrics,
batch_size=args.batch_size,
num_workers=4,
device="cuda",
use_tqdm=False,
)
res = tester.test()
logger.info("No #{} testing, Result: {}".format(pruner.prune_times, res))
name, val = get_metric(res)
summary_writer.add_scalar("pruning_test_acc", val, prune_step)
# prune and save
torch.save(
model.state_dict(),
os.path.join(
args.save_path,
"best_{}_{}.th".format(pruner.prune_times, pruner.cur_rate),
),
)
pruner.pruning_model()
summary_writer.add_scalar("remain_rate", pruner.cur_rate, prune_step + 1)
summary_writer.add_scalar("cutoff", pruner.last_cutoff, prune_step + 1)
pruner.save(
os.path.join(
args.save_path, "{}_{}.th".format(pruner.prune_times, pruner.cur_rate)
)
)
def eval_mtl_single(args):
global logger
# import ipdb; ipdb.set_trace()
args = torch.load(os.path.join(args.save_path, "args"))
print(args)
logger.info(args)
task_lst, vocabs = utils.get_data(args.data_path)
task_db = task_lst[args.task_id]
train_data = task_db.train_set
dev_data = task_db.dev_set
test_data = task_db.test_set
task_name = task_db.task_name
# text classification
for ds in [train_data, dev_data, test_data]:
ds.rename_field("words_idx", "x")
ds.rename_field("label", "y")
ds.set_input("x", "y", "task_id")
ds.set_target("y")
# seq label
if task_name in SEQ_LABEL_TASK:
for ds in [train_data, dev_data, test_data]:
ds.set_input("seq_len")
ds.set_target("seq_len")
logger = utils.get_logger(__name__)
logger.info("task name: {}, task id: {}".format(task_db.task_name, task_db.task_id))
logger.info(
"train len {}, dev len {}, test len {}".format(
len(train_data), len(dev_data), len(test_data)
)
)
# init model
model = get_model(args, task_lst, vocabs)
# logger.info('model: \n{}'.format(model))
if task_name not in SEQ_LABEL_TASK or task_name == "pos":
metrics = [
AccuracyMetric(target="y"),
# MetricInForward(val_name='loss')
]
else:
metrics = [
SpanFPreRecMetric(
tag_vocab=vocabs[task_name],
pred="pred",
target="y",
seq_len="seq_len",
encoding_type="bioes" if task_name == "ner" else "chunk",
),
AccuracyMetric(target="y")
# MetricInForward(val_name='loss')
]
cur_best = 0.0
init_best = None
eval_time = 0
paths = [path for path in os.listdir(args.save_path) if path.startswith("best")]
paths = sorted(paths, key=lambda x: int(x.split("_")[1]))
for path in paths:
path = os.path.join(args.save_path, path)
state = torch.load(path, map_location="cpu")
model.load_state_dict(state)
tester = Tester(
test_data,
model,
metrics=metrics,
batch_size=args.batch_size,
num_workers=4,
device="cuda",
use_tqdm=False,
)
res = tester.test()
val = 0.0
for metric_name, metric_dict in res.items():
if task_name == "pos" and "acc" in metric_dict:
val = metric_dict["acc"]
break
elif "f" in metric_dict:
val = metric_dict["f"]
break
if init_best is None:
init_best = val
logger.info(
"No #%d: best %f, %s, path: %s, is better: %s",
eval_time,
val,
tester._format_eval_results(res),
path,
val > init_best,
)
eval_time += 1
def main():
parser = utils.get_default_parser()
# fmt: off
parser.add_argument("--final_rate", dest='final_rate', type=float, default=0.1, help='percent of params to remain not to pruning')
parser.add_argument("--pruning_iter", dest='pruning_iter', type=int, default=10, help='max times to pruning')
parser.add_argument('--init_masks', dest='init_masks', type=str, default=None, help='initial masks for late reseting pruning')
parser.add_argument('--need_cut', default='lstm,conv', type=str, dest='need_cut', help='parameters names that not cut')
parser.add_argument("--task_id", dest='task_id', type=int, default=0, help='the task to use')
# fmt: on
args, unk = parser.parse_known_args()
print(args)
print("unknown args ", unk)
utils.init_prog(args)
if args.evaluate:
eval_mtl_single(args)
else:
train_mlt_single(args)
# print(args)
if __name__ == "__main__":
main()