-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_level_matrix_tune.py
555 lines (485 loc) · 27.3 KB
/
instance_level_matrix_tune.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# coding=utf-8
# Copyright (c) Microsoft. All rights reserved.
import argparse
import json
import time
import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '0' # TODO: be careful if use to debug
import random
from datetime import datetime
from pprint import pprint
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader, BatchSampler, SequentialSampler
from pretrained_models import *
from tensorboardX import SummaryWriter
# from torch.utils.tensorboard import SummaryWriter
from experiments.exp_def import TaskDefs
from mt_dnn.inference import eval_model, extract_encoding, get_head_matrix, get_head_matrix_ins
from data_utils.log_wrapper import create_logger
from data_utils.task_def import EncoderModelType
from data_utils.utils import set_environment
from mt_dnn.batcher import SingleTaskDataset, MultiTaskDataset, Collater, MultiTaskBatchSampler, \
DistMultiTaskBatchSampler, DistSingleTaskBatchSampler
from mt_dnn.batcher import DistTaskDataset
from mt_dnn.model import MTDNNModel
from typing import Optional
def model_config(parser):
parser.add_argument('--update_bert_opt', default=0, type=int)
parser.add_argument('--multi_gpu_on', type=bool, default=False)
parser.add_argument('--mem_cum_type', type=str, default='simple',
help='bilinear/simple/defualt')
parser.add_argument('--answer_num_turn', type=int, default=5)
parser.add_argument('--answer_mem_drop_p', type=float, default=0.1)
parser.add_argument('--answer_att_hidden_size', type=int, default=128)
parser.add_argument('--answer_att_type', type=str, default='bilinear',
help='bilinear/simple/defualt')
parser.add_argument('--answer_rnn_type', type=str, default='gru',
help='rnn/gru/lstm')
parser.add_argument('--answer_sum_att_type', type=str, default='bilinear',
help='bilinear/simple/defualt')
parser.add_argument('--answer_merge_opt', type=int, default=1)
parser.add_argument('--answer_mem_type', type=int, default=1)
parser.add_argument('--max_answer_len', type=int, default=10)
parser.add_argument('--answer_dropout_p', type=float, default=0.1)
parser.add_argument('--answer_weight_norm_on', action='store_true')
parser.add_argument('--dump_state_on', action='store_true')
parser.add_argument('--answer_opt', type=int, default=1, help='0,1')
parser.add_argument('--pooler_actf', type=str, default='tanh',
help='tanh/relu/gelu')
parser.add_argument('--mtl_opt', type=int, default=0)
parser.add_argument('--ratio', type=float, default=0)
parser.add_argument('--mix_opt', type=int, default=0)
parser.add_argument('--max_seq_len', type=int, default=512)
parser.add_argument('--init_ratio', type=float, default=1)
parser.add_argument('--encoder_type', type=int, default=1)
parser.add_argument('--num_hidden_layers', type=int, default=-1)
# BERT pre-training
parser.add_argument('--bert_model_type', type=str, default='bert-base-cased')
parser.add_argument('--do_lower_case', action='store_true')
parser.add_argument('--masked_lm_prob', type=float, default=0.15)
parser.add_argument('--short_seq_prob', type=float, default=0.2)
parser.add_argument('--max_predictions_per_seq', type=int, default=128)
# bin samples
parser.add_argument('--bin_on', action='store_true')
parser.add_argument('--bin_size', type=int, default=64)
parser.add_argument('--bin_grow_ratio', type=int, default=0.5)
# dist training
parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank")
parser.add_argument("--world_size", type=int, default=1, help="For distributed training: world size")
parser.add_argument("--master_addr", type=str, default="localhost")
parser.add_argument("--master_port", type=str, default="6600")
parser.add_argument("--backend", type=str, default="nccl")
return parser
def data_config(parser):
parser.add_argument('--log_file', default='mt-dnn-train.log', help='path for log file.')
parser.add_argument('--tensorboard', action='store_true')
parser.add_argument('--tensorboard_logdir', default='tensorboard_logdir')
parser.add_argument("--init_checkpoint", default='bert-base-cased', type=str)
parser.add_argument('--data_dir', default='data/canonical_data/bert-base-cased')
parser.add_argument('--data_sort_on', action='store_true')
parser.add_argument('--name', default='farmer')
parser.add_argument('--task_def', type=str, default="experiments/glue/glue_task_def.yml")
parser.add_argument('--train_datasets', default='cola')
parser.add_argument('--test_datasets', default='cola')
parser.add_argument('--glue_format_on', action='store_true')
parser.add_argument('--mkd-opt', type=int, default=0,
help=">0 to turn on knowledge distillation, requires 'softlabel' column in input data")
parser.add_argument('--do_padding', action='store_true')
parser.add_argument('--save_path', type=str, default="gradient_files")
parser.add_argument("--force", action='store_true')
return parser
def train_config(parser):
parser.add_argument('--cuda', type=bool, default=torch.cuda.is_available(),
help='whether to use GPU acceleration.')
parser.add_argument('--log_per_updates', type=int, default=500)
parser.add_argument('--save_per_updates', type=int, default=10000)
parser.add_argument('--save_per_updates_on', action='store_true')
parser.add_argument('--epochs', type=int, default=3) # default tune 3 epochs
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--batch_size_eval', type=int, default=1)
parser.add_argument('--optimizer', default='adamax',
help='supported optimizer: adamax, sgd, adadelta, adam')
parser.add_argument('--grad_clipping', type=float, default=0)
parser.add_argument('--global_grad_clipping', type=float, default=1.0)
parser.add_argument('--weight_decay', type=float, default=0)
parser.add_argument('--learning_rate', type=float, default=5e-5)
parser.add_argument('--momentum', type=float, default=0)
parser.add_argument('--warmup', type=float, default=0.1)
parser.add_argument('--warmup_schedule', type=str, default='warmup_linear')
parser.add_argument('--adam_eps', type=float, default=1e-6)
parser.add_argument('--vb_dropout', action='store_false')
parser.add_argument('--dropout_p', type=float, default=0.1)
parser.add_argument('--dropout_w', type=float, default=0.000)
parser.add_argument('--bert_dropout_p', type=float, default=0.1)
# loading
parser.add_argument("--model_ckpt", default='checkpoints/model_0.pt', type=str)
parser.add_argument("--resume", action='store_true')
# scheduler
parser.add_argument('--have_lr_scheduler', dest='have_lr_scheduler', action='store_false')
parser.add_argument('--multi_step_lr', type=str, default='10,20,30')
# parser.add_argument('--feature_based_on', action='store_true')
parser.add_argument('--lr_gamma', type=float, default=0.5)
parser.add_argument('--scheduler_type', type=str, default='ms', help='ms/rop/exp')
parser.add_argument('--output_dir', default='checkpoint/8_bert-base-cased')
parser.add_argument('--seed', type=int, default=2018,
help='random seed for data shuffling, embedding init, etc.')
parser.add_argument('--grad_accumulation_step', type=int, default=1)
# fp 16
parser.add_argument('--fp16', action='store_true',
help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit")
parser.add_argument('--fp16_opt_level', type=str, default='O1',
help="For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
"See details at https://nvidia.github.io/apex/amp.html")
# adv training
parser.add_argument('--adv_train', action='store_true')
# the current release only includes smart perturbation
parser.add_argument('--adv_opt', default=0, type=int)
parser.add_argument('--adv_norm_level', default=0, type=int)
parser.add_argument('--adv_p_norm', default='inf', type=str)
parser.add_argument('--adv_alpha', default=1, type=float)
parser.add_argument('--adv_k', default=1, type=int)
parser.add_argument('--adv_step_size', default=1e-5, type=float)
parser.add_argument('--adv_noise_var', default=1e-5, type=float)
parser.add_argument('--adv_epsilon', default=1e-6, type=float)
parser.add_argument('--encode_mode', action='store_true', help="only encode test data")
parser.add_argument('--debug', action='store_true', help="print debug info")
return parser
parser = argparse.ArgumentParser()
parser = data_config(parser)
parser = model_config(parser)
parser = train_config(parser)
args = parser.parse_args()
output_dir = args.output_dir
data_dir = args.data_dir
args.train_datasets = args.train_datasets.split(',') ###########
args.test_datasets = args.train_datasets ##########
assert len(args.train_datasets) == 1
assert args.grad_accumulation_step == 1, "get task level head importance matrix, so there is no need to accumulate"
assert args.batch_size_eval == 1, "we should tune every instance separately"
os.makedirs(output_dir, exist_ok=True)
output_dir = os.path.abspath(output_dir)
set_environment(args.seed, args.cuda)
log_path = args.log_file
log_path = args.output_dir + "/log.log"
logger = create_logger(__name__, to_disk=True, log_file=log_path)
task_defs = TaskDefs(args.task_def)
encoder_type = args.encoder_type
def dump(path, data):
with open(path, 'w') as f:
json.dump(data, f)
def evaluation(model, datasets, data_list, task_defs, output_dir='checkpoints', epoch=0, n_updates=-1, with_label=False,
tensorboard=None, glue_format_on=False, test_on=False, device=None, logger=None):
# eval on rank 1
print_message(logger, "Evaluation")
test_prefix = "Test" if test_on else "Dev"
if n_updates > 0:
updates_str = "updates"
else:
updates_str = "epoch"
updates = model.updates if n_updates > 0 else epoch
for idx, dataset in enumerate(datasets):
prefix = dataset.split('_')[0]
task_def = task_defs.get_task_def(prefix)
label_dict = task_def.label_vocab
test_data = data_list[idx]
if test_data is not None:
with torch.no_grad():
test_metrics, test_predictions, test_scores, test_golds, test_ids = eval_model(model,
test_data,
metric_meta=task_def.metric_meta,
device=device,
with_label=with_label,
label_mapper=label_dict,
task_type=task_def.task_type)
for key, val in test_metrics.items():
if tensorboard:
tensorboard.add_scalar('{}/{}/{}'.format(test_prefix, dataset, key), val, global_step=updates)
if isinstance(val, str):
print_message(logger, 'Task {0} -- {1} {2} -- {3} {4}: {5}'.format(dataset, updates_str, updates,
test_prefix, key, val), level=1)
elif isinstance(val, float):
print_message(logger,
'Task {0} -- {1} {2} -- {3} {4}: {5:.3f}'.format(dataset, updates_str, updates,
test_prefix, key, val), level=1)
else:
test_metrics[key] = str(val)
print_message(logger, 'Task {0} -- {1} {2} -- {3} {4}: \n{5}'.format(dataset, updates_str, updates,
test_prefix, key, val),
level=1)
if args.local_rank in [-1, 0]:
score_file = os.path.join(output_dir,
'{}_{}_scores_{}_{}.json'.format(dataset, test_prefix.lower(), updates_str,
updates))
results = {'metrics': test_metrics, 'predictions': test_predictions, 'uids': test_ids,
'scores': test_scores}
dump(score_file, results)
if glue_format_on:
from experiments.glue.glue_utils import submit
official_score_file = os.path.join(output_dir,
'{}_{}_scores_{}.tsv'.format(dataset, test_prefix.lower(),
updates_str))
submit(official_score_file, results, label_dict)
# def tune_get_head_matrix(model, datasets, data_list, task_defs, save_path, device=None, logger=None):
# print_message(logger, "get head importance on training set")
# # test_prefix = "Test" if test_on else "Dev"
# # if n_updates > 0:
# # updates_str = "updates"
# # else:
# # updates_str = "epoch"
# # updates = model.updates if n_updates > 0 else epoch
# for idx, dataset in enumerate(datasets):
# prefix = dataset.split('_')[0]
# task_def = task_defs.get_task_def(prefix)
# label_dict = task_def.label_vocab
# test_data = data_list[idx]
# assert test_data is not None
# get_head_matrix(model, test_data, device=device, save_path=save_path)
def tune_get_instance_head(model, datasets, data_list, task_defs, save_path, device=None, logger=None):
''' tune on the training set to get instance-level head importance matrix '''
print_message(logger, "get instance-level head importance matrix on the training set")
for idx, dataset in enumerate(datasets):
prefix = dataset.split('_')[0]
task_def = task_defs.get_task_def(prefix)
label_dict = task_def.label_vocab
test_data = data_list[idx]
assert test_data is not None
get_head_matrix_ins(model, test_data, device=device, save_path=save_path)
def initialize_distributed(args):
"""Initialize torch.distributed."""
args.rank = int(os.getenv('RANK', '0'))
args.world_size = int(os.getenv("WORLD_SIZE", '1'))
if os.getenv('OMPI_COMM_WORLD_LOCAL_RANK'):
# We are using (OpenMPI) mpirun for launching distributed data parallel processes
local_rank = int(os.getenv('OMPI_COMM_WORLD_LOCAL_RANK'))
local_size = int(os.getenv('OMPI_COMM_WORLD_LOCAL_SIZE'))
args.local_rank = local_rank
args.rank = nodeid * local_size + local_rank
args.world_size = num_nodes * local_size
# args.batch_size = args.batch_size * args.world_size
device = args.rank % torch.cuda.device_count()
if args.local_rank is not None:
device = args.local_rank
torch.cuda.set_device(device)
device = torch.device('cuda', args.local_rank)
# Call the init process
init_method = 'tcp://'
master_ip = os.getenv('MASTER_ADDR', 'localhost')
master_port = os.getenv('MASTER_PORT', '6600')
init_method += master_ip + ':' + master_port
torch.distributed.init_process_group(
backend=args.backend,
world_size=args.world_size, rank=args.rank,
init_method=init_method)
return device
def print_message(logger, message, level=0):
if torch.distributed.is_initialized():
if torch.distributed.get_rank() == 0:
do_logging = True
else:
do_logging = False
else:
do_logging = True
if do_logging:
if level == 1:
logger.warning(message)
else:
logger.info(message)
def main():
# set up dist
begin = time.time()
device = torch.device("cuda")
if args.local_rank > -1:
device = initialize_distributed(args)
elif torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
# =============== check the gradient file ==============
save_path = args.save_path
if not os.path.exists(save_path):
os.mkdir(save_path)
save_path = os.path.join(save_path, args.bert_model_type)
if not os.path.exists(save_path):
os.mkdir(save_path)
save_path = os.path.join(save_path, args.test_datasets[0])
if not os.path.exists(save_path):
os.mkdir(save_path)
logger.info("the gradient file save path is {}".format(save_path))
save_name = os.path.join(save_path, "gradient_layer_normal_ins.pkl")
if os.path.isfile(save_name) and not args.force:
raise RuntimeError(
"there is already has gradient file {}\nPlease use '--force' to overcome".format(save_name))
# ======================================================
opt = vars(args)
# update data dir
opt['data_dir'] = data_dir
batch_size = args.batch_size
print_message(logger, 'Launching the MT-DNN training')
# return
tasks = {}
task_def_list = []
dropout_list = []
printable = args.local_rank in [-1, 0]
train_datasets = []
# training! tune on the specific task
for dataset in args.train_datasets:
prefix = dataset.split('_')[0]
if prefix in tasks:
continue
task_id = len(tasks)
tasks[prefix] = task_id
task_def = task_defs.get_task_def(prefix)
task_def_list.append(task_def)
train_path = os.path.join(data_dir, '{}_train.json'.format(dataset))
print_message(logger, 'Loading {} as task {}'.format(train_path, task_id))
train_data_set = SingleTaskDataset(train_path, True, maxlen=args.max_seq_len, task_id=task_id,
task_def=task_def, printable=printable)
train_datasets.append(train_data_set)
train_collater = Collater(dropout_w=args.dropout_w, encoder_type=encoder_type, soft_label=args.mkd_opt > 0,
max_seq_len=args.max_seq_len, do_padding=args.do_padding)
multi_task_train_dataset = MultiTaskDataset(train_datasets)
if args.local_rank != -1:
multi_task_batch_sampler = DistMultiTaskBatchSampler(train_datasets, args.batch_size, args.mix_opt, args.ratio,
rank=args.local_rank, world_size=args.world_size)
else:
multi_task_batch_sampler = MultiTaskBatchSampler(train_datasets, args.batch_size, args.mix_opt, args.ratio,
bin_on=args.bin_on, bin_size=args.bin_size,
bin_grow_ratio=args.bin_grow_ratio)
multi_task_train_data = DataLoader(multi_task_train_dataset, batch_sampler=multi_task_batch_sampler,
collate_fn=train_collater.collate_fn, pin_memory=args.cuda)
opt['task_def_list'] = task_def_list
dev_data_list = []
test_data_list = []
test_collater = Collater(is_train=False, encoder_type=encoder_type, max_seq_len=args.max_seq_len,
do_padding=args.do_padding)
## no grad tune on train set
for dataset in args.test_datasets:
prefix = dataset.split('_')[0]
task_def = task_defs.get_task_def(prefix)
task_id = tasks[prefix]
task_type = task_def.task_type
data_type = task_def.data_type
# dev_path = os.path.join(data_dir, '{}_dev.json'.format(dataset))
''' here the eval batch size is 1'''
dev_path = os.path.join(data_dir, '{}_train.json'.format(dataset)) # change it train set
dev_data = None
if os.path.exists(dev_path):
dev_data_set = SingleTaskDataset(dev_path, False, maxlen=args.max_seq_len, task_id=task_id,
task_def=task_def, printable=printable)
# use sequential sampler
sampler = SequentialSampler(dev_data_set)
dev_data = DataLoader(dev_data_set, batch_size=args.batch_size_eval,
sampler=sampler, collate_fn=test_collater.collate_fn, pin_memory=args.cuda)
else:
raise RuntimeError("no this data here:{}".format(dev_path))
test_data_list.append(dev_data)
print_message(logger, '#' * 20)
print_message(logger, opt)
print_message(logger, '#' * 20)
# div number of grad accumulation.
num_all_batches = args.epochs * len(multi_task_train_data) // args.grad_accumulation_step
print_message(logger, '############# Gradient Accumulation Info #############')
print_message(logger, 'number of step: {}'.format(args.epochs * len(multi_task_train_data)))
print_message(logger, 'number of grad grad_accumulation step: {}'.format(args.grad_accumulation_step))
print_message(logger, 'adjusted number of step: {}'.format(num_all_batches))
print_message(logger, '############# Gradient Accumulation Info #############')
init_model = args.init_checkpoint
state_dict = None
if os.path.exists(init_model):
if encoder_type == EncoderModelType.BERT or \
encoder_type == EncoderModelType.DEBERTA or \
encoder_type == EncoderModelType.ELECTRA:
state_dict = torch.load(init_model, map_location=device)
config = state_dict['config']
elif encoder_type == EncoderModelType.ROBERTA or encoder_type == EncoderModelType.XLM:
model_path = '{}/model.pt'.format(init_model)
state_dict = torch.load(model_path, map_location=device)
arch = state_dict['args'].arch
arch = arch.replace('_', '-')
if encoder_type == EncoderModelType.XLM:
arch = "xlm-{}".format(arch)
# convert model arch
from data_utils.roberta_utils import update_roberta_keys
from data_utils.roberta_utils import patch_name_dict
state = update_roberta_keys(state_dict['model'], nlayer=state_dict['args'].encoder_layers)
state = patch_name_dict(state)
literal_encoder_type = EncoderModelType(opt['encoder_type']).name.lower()
config_class, model_class, tokenizer_class = MODEL_CLASSES[literal_encoder_type]
config = config_class.from_pretrained(arch).to_dict()
state_dict = {'state': state}
else:
if opt['encoder_type'] not in EncoderModelType._value2member_map_:
raise ValueError("encoder_type is out of pre-defined types")
literal_encoder_type = EncoderModelType(opt['encoder_type']).name.lower()
config_class, model_class, tokenizer_class = MODEL_CLASSES[literal_encoder_type]
config = config_class.from_pretrained(init_model).to_dict()
config['attention_probs_dropout_prob'] = args.bert_dropout_p
config['hidden_dropout_prob'] = args.bert_dropout_p
config['multi_gpu_on'] = opt["multi_gpu_on"]
if args.num_hidden_layers > 0:
config['num_hidden_layers'] = args.num_hidden_layers
opt.update(config)
model = MTDNNModel(opt, device=device, state_dict=state_dict, num_train_step=num_all_batches)
if args.resume and args.model_ckpt:
print_message(logger, 'loading model from {}'.format(args.model_ckpt))
model.load(args.model_ckpt)
#### model meta str
headline = '############# Model Arch of MT-DNN #############'
### print network
print_message(logger, '\n{}\n{}\n'.format(headline, model.network))
# dump config
config_file = os.path.join(output_dir, 'config.json')
with open(config_file, 'w', encoding='utf-8') as writer:
writer.write('{}\n'.format(json.dumps(opt)))
writer.write('\n{}\n{}\n'.format(headline, model.network))
print_message(logger, "Total number of params: {}".format(model.total_param))
# don't use tensorboard
tensorboard = None
args.tensorboard = None
if args.tensorboard:
args.tensorboard_logdir = os.path.join(args.output_dir, args.tensorboard_logdir)
tensorboard = SummaryWriter(log_dir=args.tensorboard_logdir)
if args.encode_mode:
for idx, dataset in enumerate(args.test_datasets):
prefix = dataset.split('_')[0]
test_data = test_data_list[idx]
with torch.no_grad():
encoding = extract_encoding(model, test_data, use_cuda=args.cuda)
torch.save(encoding, os.path.join(output_dir, '{}_encoding.pt'.format(dataset)))
return
for epoch in range(0, args.epochs):
print_message(logger, 'At epoch {}'.format(epoch), level=1)
start = datetime.now()
for i, (batch_meta, batch_data) in enumerate(multi_task_train_data):
batch_meta, batch_data = Collater.patch_data(device, batch_meta, batch_data)
task_id = batch_meta['task_id']
model.update(batch_meta, batch_data)
if (model.updates) % (args.log_per_updates) == 0 or model.updates == 1:
ramaining_time = \
str((datetime.now() - start) / (i + 1) * (len(multi_task_train_data) - i - 1)).split('.')[0]
if args.adv_train and args.debug:
debug_info = ' adv loss[%.5f] emb val[%.8f] eff_perturb[%.8f] ' % (
model.adv_loss.avg,
model.emb_val.avg,
model.eff_perturb.avg
)
else:
debug_info = ' '
print_message(logger, 'Task [{0:2}] updates[{1:6}] train loss[{2:.5f}]{3}remaining[{4}]'.format(task_id,
model.updates,
model.train_loss.avg,
debug_info,
ramaining_time))
if args.tensorboard:
tensorboard.add_scalar('train/loss', model.train_loss.avg, global_step=model.updates)
tune_get_instance_head(model, args.test_datasets, test_data_list, task_defs, save_path=save_name, device=device,
logger=logger)
end = time.time()
run_time = (end - begin) / 60.
with open(os.path.join(save_path,"instace_time.txt"),"w") as f:
f.write("instance time consumption: %.3f minutes" % run_time)
if args.tensorboard:
tensorboard.close()
if __name__ == '__main__':
main()