-
Notifications
You must be signed in to change notification settings - Fork 1
/
Manager.py
1345 lines (1079 loc) · 49 KB
/
Manager.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import re
import os
import logging
import argparse
import json
import pickle
import time
import random
import smtplib
import pandas as pd
import numpy as np
import scipy.stats as ss
import torch.nn as nn
import torch.optim as optim
import torch.distributed as dist
from datetime import datetime, timedelta
from tqdm.auto import tqdm
from typing import OrderedDict
from collections import defaultdict
from transformers import get_linear_schedule_with_warmup
# from torch.utils.tensorboard import SummaryWriter
from sklearn.metrics import roc_auc_score, log_loss, mean_squared_error, accuracy_score, f1_score
from torch.utils.data.dataloader import DataLoader
from torch.utils.data.distributed import DistributedSampler
from .utils import _group_lists
logger = logging.getLogger(__name__)
hparam_list = ["epochs", "device", "path", "title_length", "step", "checkpoint", "smoothing", "num_workers", "pin_memory", "interval", "npratio", "metrics", "aggregator", "head_num", "rank", "unilm_path", "unilm_config_path"]
class Manager():
"""
wrap training/evaluating processes
"""
def __init__(self, config=None):
"""
customize hyper parameters in command line
"""
if config is None:
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--scale", dest="scale", help="data scale", choices=["demo", "small", "large", "whole"], default="large")
parser.add_argument("-m", "--mode", dest="mode", help="train or test", choices=["train", "dev", "test", "encode", "inspect", "analyse", "recall"], default="train")
parser.add_argument("-e", "--epochs", dest="epochs", help="epochs to train the model", type=int, default=10)
parser.add_argument("-d","--device", dest="device", help="device to run on, -1 means cpu", choices=[i for i in range(-1,10)], type=int, default=0)
parser.add_argument("-p", "--path", dest="path", type=str, default="../../Data/", help="root path for large-scale reusable data")
parser.add_argument("-f", "--fast", dest="fast", help="enable fast evaluation/test", default=True)
parser.add_argument("-n", "--news", dest="news", help="which news to inspect", type=str, default=None)
parser.add_argument("-c", "--case", dest="case", help="whether to return the sample for case study", action="store_true", default=False)
parser.add_argument("-rt", "--recall_type", dest="recall_type", help="recall type", choices=["s","d","sd"], default=None)
parser.add_argument("-it", "--inspect_type", dest="inspect_type", help="the dataset to inspect", choices=["dev","test","all"], default=None)
parser.add_argument("-bs", "--batch_size", dest="batch_size", help="batch size in training", type=int, default=32)
parser.add_argument("-bsn", "--batch_size_news", dest="batch_size_news", help="batch size of loader_news", type=int, default=500)
parser.add_argument("-hs", "--his_size", dest="his_size",help="history size", type=int, default=50)
parser.add_argument("-is", "--impr_size", dest="impr_size", help="impression size for evaluating", type=int, default=2000)
parser.add_argument("-sl", "--signal_length", dest="signal_length", help="length of the bert tokenized tokens", type=int, default=30)
parser.add_argument("-hd", "--hidden_dim", dest="hidden_dim", help="number of hidden states", type=int, default=150)
parser.add_argument("-ed", "--embedding_dim", dest="embedding_dim", help="number of embedding states", type=int, default=768)
parser.add_argument("-bd", "--bert_dim", dest="bert_dim", help="number of hidden states in pre-trained language models", type=int, default=768)
parser.add_argument("-dp", "--dropout_p", dest="dropout_p", help="dropout probability", type=float, default=0.2)
parser.add_argument("-hn", "--head_num", dest="head_num", help="number of multi-heads", type=int, default=12)
parser.add_argument("-st","--step", dest="step", help="save/evaluate the model every step", type=int, default=10000)
parser.add_argument("-ck","--checkpoint", dest="checkpoint", help="load the model from checkpoint before training/evaluating", type=int, default=0)
parser.add_argument("-hst","--hold_step", dest="hold_step", help="keep training until step > hold_step", type=int, default=50000)
parser.add_argument("-se", "--save_epoch", help="save after each epoch if declared", action="store_true", default=False)
parser.add_argument("-lr", dest="lr", help="learning rate of non-bert modules", type=float, default=1e-4)
parser.add_argument("-blr", "--bert_lr", dest="bert_lr", help="learning rate of bert based modules", type=float, default=6e-6)
parser.add_argument("-vb", "--verbose", help="tailing name for tesrec", type=str, default="norm")
parser.add_argument("--descend_history", dest="descend_history", help="whether to order history by time in descending", action="store_true", default=False)
parser.add_argument("--debias", dest="debias", help="whether to add a learnable bias to each candidate news's score", type=bool, default=True)
parser.add_argument("-sm", "--smoothing", dest="smoothing", help="smoothing factor of tqdm", type=float, default=0.3)
parser.add_argument("--num_workers", dest="num_workers", help="worker number of a dataloader", type=int, default=0)
parser.add_argument("--shuffle", dest="shuffle", help="whether to shuffle the indices", action="store_true", default=False)
parser.add_argument("--shuffle_pos", dest="shuffle_pos", help="whether to shuffle the candidate news and its negtive samples", action="store_true", default=False)
parser.add_argument("--pin_memory", dest="pin_memory", help="whether to pin memory to speed up tensor transfer", action="store_true", default=False)
parser.add_argument("--anomaly", dest="anomaly", help="whether to detect abnormal parameters", action="store_true", default=False)
parser.add_argument("--scheduler", dest="scheduler", help="choose schedule scheme for optimizer", choices=["linear","none"], default="none")
parser.add_argument("--warmup", dest="warmup", help="warmup steps of scheduler", type=int, default=10000)
parser.add_argument("--interval", dest="interval", help="the step interval to update processing bar", default=10, type=int)
parser.add_argument("--no_email", dest="no_email", help="whether to email the result", action='store_true', default=False)
parser.add_argument("--npratio", dest="npratio", help="the number of unclicked news to sample when training", type=int, default=4)
parser.add_argument("--metrics", dest="metrics", help="metrics for evaluating the model", type=str, default="")
parser.add_argument("-emb", "--embedding", dest="embedding", help="choose embedding", choices=["bert","random","deberta"], default="bert")
parser.add_argument("-encn", "--encoderN", dest="encoderN", help="choose news encoder", choices=["cnn","rnn","npa","fim","mha","bert"], default="cnn")
parser.add_argument("-encu", "--encoderU", dest="encoderU", help="choose user encoder", choices=["avg","attn","cnn","lstm","gru","lstur","mha"], default="lstm")
parser.add_argument("-pl", "--pooler", dest="pooler", help="choose bert pooler", choices=["avg","attn","cls"], default="attn")
parser.add_argument("-rk", "--ranker", dest="ranker", help="choose ranker", choices=["onepass","original","cnn","knrm"], default="original")
parser.add_argument("-b", "--bert", dest="bert", help="choose bert model", choices=["bert", "deberta", "unilm", "longformer", "bigbird", "reformer", "funnel", "synthesizer", "distill", "newsbert"], default="bert")
parser.add_argument("-sd","--seed", dest="seed", default=42, type=int)
parser.add_argument("-ws", "--world_size", dest="world_size", help="total number of gpus", default=0, type=int)
args = parser.parse_args()
args.cdd_size = args.npratio + 1
args.metrics = "auc,mean_mrr,ndcg@5,ndcg@10".split(",") + [i for i in args.metrics.split(",") if i]
# -1 means cpu
if args.device == -1:
args.device = "cpu"
args.pin_memory = False
if args.bert == 'unilm':
args.unilm_path = args.path + 'bert_cache/UniLM/unilm2-base-uncased.bin'
args.unilm_config_path = args.path + 'bert_cache/UniLM/unilm2-base-uncased-config.json'
if args.recall_type is not None:
# set to recall mode
args.mode = "recall"
if args.inspect_type is not None:
args.mode = "inspect"
# 1 for debug, 0 for evaluating only at the end of an epoch
if args.step in [0,1]:
args.hold_step = 0
if args.scale == 'demo':
args.no_email = True
else:
args = config
if args.seed is not None:
seed = args.seed
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
for k,v in vars(args).items():
if not k.startswith("__"):
setattr(self, k, v)
def __str__(self):
return str({k:v for k,v in vars(self).items() if k not in hparam_list})
def setup(self, rank):
"""
set up distributed training and fix seeds
"""
if self.world_size > 1:
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"
# prevent warning of transformers
os.environ["TOKENIZERS_PARALLELISM"] = "True"
os.environ["TORCH_DISTRIBUTED_DEBUG"] = "DETAIL"
# initialize the process group
# set timeout to inf to prevent timeout error
dist.init_process_group("nccl", rank=rank, world_size=self.world_size, timeout=timedelta(0, 1000000))
# manager.rank will be invoked in creating DistributedSampler
self.rank = rank
# manager.device will be invoked in the model
self.device = rank
else:
# one-gpu
self.rank = -1
if rank != "cpu":
torch.cuda.set_device(rank)
os.environ["CUDA_VISIBLE_DEVICES"] = str(rank)
def prepare(self):
""" prepare dataloader and several paths
Returns:
loaders(list of dataloaders): 0-loader_train/test/dev, 1-loader_dev
"""
from .MIND import MIND, MIND_news, MIND_history
from .utils import Partition_Sampler
if self.rank in [-1, 0]:
logger.info("Hyper Parameters are {}".format(self))
logger.info("preparing dataset...")
shuffle = self.shuffle
pin_memory = self.pin_memory
num_workers = self.num_workers
if self.mode == "train":
file_directory_train = self.path + "MIND/MIND{}_train/".format(self.scale)
file_directory_dev = self.path + "MIND/MIND{}_dev/".format(self.scale)
# construct whole dataset if needed
if self.scale == 'whole' and not os.path.exists(file_directory_train):
self.construct_whole_dataset()
dataset_train = MIND(self, file_directory_train)
dataset_dev = MIND(self, file_directory_dev)
if self.world_size > 0:
sampler_train = DistributedSampler(dataset_train, num_replicas=self.world_size, rank=self.rank, shuffle=shuffle)
sampler_dev = Partition_Sampler(dataset_dev, num_replicas=self.world_size, rank=self.rank)
else:
sampler_train = None
sampler_dev = None
loader_train = DataLoader(dataset_train, batch_size=self.batch_size, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, shuffle=shuffle, sampler=sampler_train)
loader_dev = DataLoader(dataset_dev, batch_size=1, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, shuffle=False, sampler=sampler_dev)
if self.fast:
dataset_news = MIND_news(self, file_directory_dev)
loader_news = DataLoader(dataset_news, batch_size=self.batch_size_news, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False)
return loader_train, loader_dev, loader_news
else:
loader_dev = DataLoader(dataset_dev, batch_size=1, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, sampler=sampler_dev)
return loader_train, loader_dev
elif self.mode == "dev":
file_directory_dev = self.path + "MIND/MIND{}_dev/".format(self.scale)
dataset_dev = MIND(self, file_directory_dev)
if self.world_size > 0:
sampler_dev = Partition_Sampler(dataset_dev, num_replicas=self.world_size, rank=self.rank)
else:
sampler_dev = None
loader_dev = DataLoader(dataset_dev, batch_size=1, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, sampler=sampler_dev)
if self.fast:
dataset_news = MIND_news(self, file_directory_dev)
loader_news = DataLoader(dataset_news, batch_size=self.batch_size_news, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False)
return loader_dev, loader_news
return loader_dev,
elif self.mode == "test":
file_directory_test = self.path + "MIND/MINDlarge_test/"
dataset_test = MIND(self, file_directory_test)
if self.world_size > 0:
sampler_test = Partition_Sampler(dataset_test, num_replicas=self.world_size, rank=self.rank)
else:
sampler_test = None
loader_test = DataLoader(dataset_test, batch_size=1, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, sampler=sampler_test)
if self.fast:
dataset_news = MIND_news(self, file_directory_test)
loader_news = DataLoader(dataset_news, batch_size=self.batch_size_news, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False)
return loader_test, loader_news
return loader_test,
elif self.mode == "encode":
file_directory = self.path + "MIND/MIND{}_dev/".format(self.scale)
dataset = MIND_history(self, file_directory)
sampler = None
loader = DataLoader(dataset, batch_size=self.batch_size, pin_memory=pin_memory,
num_workers=num_workers, drop_last=False, shuffle=shuffle, sampler=sampler)
return loader,
def save(self, model, step, optimizer=None, best=False):
"""
shortcut for saving the model and optimizer
"""
if best:
save_path = "data/model_params/{}/best.model".format(self.name)
else:
save_path = "data/model_params/{}/{}_step{}.model".format(self.name, self.scale, step)
logger.info("saving model at {}...".format(save_path))
model_dict = model.state_dict()
save_dict = {}
save_dict["model"] = model_dict
save_dict["optimizer"] = optimizer.state_dict()
torch.save(save_dict, save_path)
def load(self, model, checkpoint, optimizer=None, strict=True):
"""
shortcut for loading model and optimizer parameters
"""
if checkpoint == 0:
save_path = "data/model_params/{}/best.model".format(self.name)
if not os.path.exists(save_path):
logger.warning("not loading any checkpoints!")
return
else:
save_path = "data/model_params/{}/{}_step{}.model".format(self.name, self.scale, checkpoint)
logger.info("loading model from {}...".format(save_path))
state_dict = torch.load(save_path, map_location=torch.device(model.device))
if self.world_size <= 1:
# in case we load a DDP model checkpoint to a non-DDP model
model_dict = OrderedDict()
pattern = re.compile("module.")
for k,v in state_dict["model"].items():
if re.search("module", k):
model_dict[re.sub(pattern, "", k)] = v
else:
model_dict = state_dict["model"]
else:
model_dict = state_dict["model"]
if not re.search("module", list(model_dict.keys())[0]):
logger.warning("Loading a non-distributed model to a distributed one!")
model = model.module
model.load_state_dict(model_dict, strict=strict)
if optimizer:
optimizer.load_state_dict(state_dict["optimizer"])
def _log(self, res):
"""
wrap logging, skip logging results on MINDdemo
"""
# logger.info("evaluation results:{}".format(res))
with open("performance.log", "a+") as f:
d = {"name": self.name}
for k, v in vars(self).items():
if k not in hparam_list:
d[k] = v
name = str(d) + "\n"
content = str(res) + "\n\n"
f.write(name)
f.write(content)
if not self.no_email:
try:
from data.configs.email import email,password
subject = "[Performance Report] {} : {}".format(d["name"], res["auc"])
email_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
email_server.login(email, password)
message = "Subject: {}\n\n{}".format(subject,name + content)
email_server.sendmail(email, email, message)
email_server.close()
except:
logger.info("error in connecting SMTP")
def _get_loss(self, model):
"""
get loss function for model
"""
if model.training:
loss = nn.NLLLoss()
else:
loss = nn.BCELoss()
return loss
def _get_optim(self, model, loader_train_length):
"""
get optimizer and scheduler
"""
if self.world_size > 1:
model = model.module
base_params = []
bert_params = []
for name, param in model.named_parameters():
if re.search("bert", name):
bert_params.append(param)
else:
base_params.append(param)
optimizer = optim.Adam([
{
"params": base_params,
"lr": self.lr #lr_schedule(args.lr, 1, args)
},
{
"params": bert_params,
"lr": self.bert_lr #lr_schedule(args.pretrain_lr, 1, args)
}
])
scheduler = None
if self.scheduler == "linear":
total_steps = loader_train_length * self.epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = self.warmup,
num_training_steps = total_steps)
return optimizer, scheduler
@torch.no_grad()
def _eval(self, model, loader_dev):
""" making prediction and gather results into groups according to impression_id
Args:
loader_dev(torch.utils.data.DataLoader): provide data
Returns:
impr_indexes: impression ids
labels: labels
preds: preds
"""
if self.rank in [-1, 0]:
logger.info("evaluating...")
impr_indexes = []
labels = []
preds = []
for x in tqdm(loader_dev, smoothing=self.smoothing, ncols=120, leave=True):
impr_indexes.extend(x["impr_index"].tolist())
preds.extend(model(x)[0].tolist())
labels.extend(x["label"].tolist())
# collect result across gpus when distributed evaluating
if self.world_size > 1:
dist.barrier()
outputs = [None for i in range(self.world_size)]
dist.all_gather_object(outputs, (impr_indexes, labels, preds))
if self.rank == 0:
impr_indexes = []
labels = []
preds = []
for output in outputs:
impr_indexes.extend(output[0])
labels.extend(output[1])
preds.extend(output[2])
labels, preds = _group_lists(impr_indexes, labels, preds)
else:
labels, preds = _group_lists(impr_indexes, labels, preds)
return labels, preds
@torch.no_grad()
def _eval_fast(self, model, loaders):
"""
1. encode and save news
2. compute scores by look-up tables and dot product
Args:
model
loaders
0: loader_test
1: loader_news
"""
# if the model is an instance of DDP, then we have to access its module attribute
if self.world_size > 1:
model = model.module
# encode and save news representations only on the master node
if self.rank in [-1, 0]:
cache_directory = "data/cache/tensors/{}/{}/dev/".format(self.name, self.scale)
os.makedirs(cache_directory, exist_ok=True)
logger.info("fast evaluate, encoding news...")
model.init_encoding()
news_reprs = torch.zeros(self.get_news_num() + 1, model.hidden_dim, device=model.device)
for x in tqdm(loaders[1], smoothing=self.smoothing, ncols=120, leave=True):
news_repr = model.encode_news(x).squeeze(-2)
news_reprs[x['cdd_id']] = news_repr
# must save for other processes to load
torch.save(news_reprs, cache_directory + "news.pt")
del news_reprs
model.destroy_encoding()
if self.world_size > 1:
dist.barrier()
model.init_embedding()
impr_indexes = []
labels = []
preds = []
for x in tqdm(loaders[0], smoothing=self.smoothing, ncols=120, leave=True):
impr_indexes.extend(x["impr_index"].tolist())
preds.extend(model.predict_fast(x).tolist())
labels.extend(x["label"].tolist())
model.destroy_embedding()
# collect result across gpus when distributed evaluating
if self.world_size > 1:
dist.barrier()
outputs = [None for i in range(self.world_size)]
dist.all_gather_object(outputs, (impr_indexes, labels, preds))
if self.rank == 0:
impr_indexes = []
labels = []
preds = []
for output in outputs:
impr_indexes.extend(output[0])
labels.extend(output[1])
preds.extend(output[2])
labels, preds = _group_lists(impr_indexes, labels, preds)
else:
labels, preds = _group_lists(impr_indexes, labels, preds)
return labels, preds
def evaluate(self, model, loaders, load=False, log=True, optimizer=None, scheduler=None):
"""Evaluate the given file and returns some evaluation metrics.
Args:
self(dict)
model
loaders(torch.utils.data.DataLoader):
0: loader_dev
1: loader_news
loading(bool): whether to load model
log(bool): whether to log
Returns:
res(dict): A dictionary contains evaluation metrics.
"""
model.eval()
# load saved model
if load:
self.load(model, self.checkpoint, optimizer)
# protect non-master node to return an object
res = None
if self.fast:
# fast evaluate
labels, preds = self._eval_fast(model, loaders)
else:
# slow evaluate
labels, preds = self._eval(model, loaders[0])
# compute metrics only on the master node
if self.rank in [0, -1]:
res = cal_metric(labels, preds, self.metrics)
logger.info("\nevaluation result of {} is {}".format(self.name, res))
if log and self.rank in [0, -1]:
res["step"] = self.checkpoint
self._log(res)
return res
def _train(self, model, loaders, optimizer, loss_func, scheduler=None):
""" train model and evaluate on validation set once every save_step
Args:
dataloader(torch.utils.data.DataLoader): provide data
optimizer(list of torch.nn.optim): optimizer for training
loss_func(torch.nn.Loss): loss function for training
schedulers
writer(torch.utils.tensorboard.SummaryWriter): tensorboard writer
interval(int): within each epoch, the interval of training steps to display loss
save_step(int): how many steps to save the model
Returns:
model: trained model
"""
steps = 0
interval = self.interval
save_epoch = self.save_epoch
if self.scale == "demo":
save_step = len(loaders[0]) - 1
self.fast = False
# save_step = 1
else:
if self.step == 0:
save_step = len(loaders[0]) - 1
else:
save_step = self.step
print(save_step)
distributed = self.world_size > 1
# if self.tb:
# writer = SummaryWriter("data/tb/{}/{}/{}/".format(
# self.name, self.scale, datetime.now().strftime("%Y%m%d-%H")))
best_res = {"auc":0}
if self.rank in [0, -1]:
logger.info("training {}...".format(self.name))
# logger.info("total training step: {}".format(self.epochs * len(loaders[0])))
for epoch in range(self.epochs):
epoch_loss = 0
if distributed:
loaders[0].sampler.set_epoch(epoch)
tqdm_ = tqdm(loaders[0], smoothing=self.smoothing, ncols=120, leave=True)
for step, x in enumerate(tqdm_):
optimizer.zero_grad(set_to_none=True)
pred = model(x)[0]
label = x["label"].to(model.device)
loss = loss_func(pred, label)
epoch_loss += float(loss)
loss.backward()
# nn.utils.clip_grad_norm_(model.parameters(), max_norm=2.0, norm_type=2)
optimizer.step()
if scheduler:
scheduler.step()
if step % interval == 0 and step > 0:
tqdm_.set_description(
"epoch: {:d}, step: {:d}, total_step: {:d}, loss: {:.4f}".format(epoch + 1, step, steps, epoch_loss / step))
# if writer:
# for name, param in model.named_parameters():
# writer.add_histogram(name, param, step)
# writer.add_scalar("data_loss",
# total_loss/total_steps)
if steps % save_step == 0 and (steps in [140000,150000,180000,200000,210000,220000,230000,240000] and self.scale == "whole" or steps > self.hold_step and self.scale == "large" or steps > 0 and self.scale == "demo"):
print("\n")
with torch.no_grad():
result = self.evaluate(model, loaders[1:], log=False)
if self.rank in [0,-1]:
if save_epoch:
self.save(model, steps, optimizer)
result["step"] = steps
if result["auc"] > best_res["auc"]:
best_res = result
self.save(model, steps, optimizer, best=True)
self._log(result)
# prevent SIGABRT
if distributed:
dist.barrier()
# continue training
model.train()
steps += 1
return best_res
def train(self, model, loaders):
""" train and evaluate sequentially
Args:
model(torch.nn.Module): the model to be trained
loaders(list): list of torch.utils.data.DataLoader
en: shell parameter
"""
model.train()
if self.rank in [-1, 0]:
# in case the folder does not exists, create one
os.makedirs("data/model_params/{}".format(self.name), exist_ok=True)
loss_func = self._get_loss(model)
optimizer, scheduler = self._get_optim(model, len(loaders[0]))
if self.checkpoint:
self.load(model, self.checkpoint, optimizer)
if self.anomaly:
with torch.autograd.set_detect_anomaly(True):
res = self._train(model, loaders, optimizer, loss_func, scheduler=scheduler)
else:
res = self._train(model, loaders, optimizer, loss_func, scheduler=scheduler)
if self.rank in [-1,0]:
logger.info("Best result: {}".format(res))
self._log(res)
@torch.no_grad()
def _test(self, model, loader_test):
"""
regular test without pre-encoding
"""
impr_indexes = []
preds = []
for x in tqdm(loader_test, smoothing=self.smoothing, ncols=120, leave=True):
impr_indexes.extend(x["impr_index"].tolist())
preds.extend(model(x)[0].tolist())
if self.world_size > 1:
dist.barrier()
outputs = [None for i in range(self.world_size)]
dist.all_gather_object(outputs, (impr_indexes, preds))
if self.rank == 0:
impr_indexes = []
preds = []
for output in outputs:
impr_indexes.extend(output[0])
preds.extend(output[1])
preds = _group_lists(impr_indexes, preds)[0]
else:
preds = _group_lists(impr_indexes, preds)[0]
return preds
@torch.no_grad()
def _test_fast(self, model, loaders):
"""
1. encode and save news
2. compute scores by look-up tables and dot product
Args:
model
loaders
0: loader_test
1: loader_news
"""
if self.world_size > 1:
model = model.module
if self.rank in [-1, 0]:
cache_directory = "data/cache/tensors/{}/{}/test/".format(self.name, self.scale)
os.makedirs(cache_directory, exist_ok=True)
logger.info("encoding news...")
model.init_encoding()
news_reprs = torch.zeros(self.get_news_num() + 1, model.hidden_dim, device=model.device)
for x in tqdm(loaders[1], smoothing=self.smoothing, ncols=120, leave=True):
news_repr = model.encode_news(x).squeeze(-2)
news_reprs[x['cdd_id']] = news_repr
torch.save(news_reprs, cache_directory + "news.pt")
del news_reprs
model.destroy_encoding()
logger.info("inferring...")
if self.world_size > 1:
dist.barrier()
model.init_embedding()
impr_indexes = []
preds = []
for x in tqdm(loaders[0], smoothing=self.smoothing, ncols=120, leave=True):
impr_indexes.extend(x["impr_index"].tolist())
preds.extend(model.predict_fast(x).tolist())
model.destroy_embedding()
if self.world_size > 1:
dist.barrier()
outputs = [None for i in range(self.world_size)]
dist.all_gather_object(outputs, (impr_indexes, preds))
if self.rank == 0:
impr_indexes = []
preds = []
for output in outputs:
impr_indexes.extend(output[0])
preds.extend(output[1])
preds = _group_lists(impr_indexes, preds)[0]
else:
preds = _group_lists(impr_indexes, preds)[0]
return preds
def test(self, model, loaders):
""" test the model on test dataset of MINDlarge
Args:
model
loader_test: DataLoader of MINDlarge_test
"""
model.eval()
self.load(model, self.checkpoint)
if self.rank in [0,-1]:
logger.info("testing...")
if self.fast:
# fast evaluate
preds = self._test_fast(model, loaders)
else:
# slow evaluate
preds = self._test(model, loaders[0])
if self.rank in [0, -1]:
save_directory = "data/results/{}".format(self.name + "/{}_step{}".format(self.scale, self.checkpoint))
os.makedirs(save_directory, exist_ok=True)
save_path = save_directory + "/prediction.txt"
index = 1
with open(save_path, "w") as f:
for pred in preds:
array = np.asarray(pred)
rank_list = ss.rankdata(1 - array, method="ordinal")
line = str(index) + " [" + ",".join([str(i)
for i in rank_list]) + "]" + "\n"
f.write(line)
index += 1
logger.info("written to prediction at {}!".format(save_path))
@torch.no_grad()
def encode(self, model, loaders):
"""
encode user
"""
model.eval()
logger.info("encoding users...")
self.load(model, self.checkpoint)
# user_reprs = []
start_time = time.time()
for x in tqdm(loaders[0], smoothing=self.smoothing, ncols=120, leave=True):
user_repr = model.encode_user(x)[0].squeeze(-2)
end_time = time.time()
logger.info("total encoding time: {}".format(end_time - start_time))
def get_user_num(self):
user_num_map = {
"demo": 2146,
"small": 94057,
"large": 876956,
"whole": 876956
}
return user_num_map[self.scale]
def get_news_num(self):
"""
get the news number of validation/test set
"""
news_num_map = {
"demo":{
"train": 42416,
"dev": 42416,
"inspect": 42416,
"test": 120961
},
"small":{
"train": 42416,
"dev": 42416,
"inspect": 42416,
"test": 120961
},
"large":{
"train": 72023,
"dev": 72023,
"inspect": 72023,
"test": 120961
},
"whole":{
"train": 72023,
"dev": 72023,
"inspect": 72023,
"test": 120961
}
}
return news_num_map[self.scale][self.mode]
def get_bert_for_load(self):
"""
transfer unilm to bert
"""
bert_map = {
"bert": "bert-base-uncased",
"deberta": "microsoft/deberta-base",
"longformer": "allenai/longformer-base-4096",
"bigbird": "google/bigbird-roberta-base",
"reformer": "google/reformer-crime-and-punishment",
"funnel": "funnel-transformer/small-base",
"synthesizer": "bert-base-uncased",
"distill": "distilbert-base-uncased",
"newsbert": "bert-base-uncased"
}
return bert_map[self.bert]
def get_bert_for_cache(self):
"""
path to save cached tokenization file, transfer unilm to bert
"""
bert_map = {
"bert": "bert",
"deberta": "deberta",
"longformer": "longformer",
"bigbird": "bigbird",
"reformer": "reformer",
"funnel": "bert",
"synthesizer": "bert",
"distill": "bert",
"newsbert": "bert"
}
return bert_map[self.bert]
def get_special_token_id(self, token):
special_token_map = {
"bert":{
"[PAD]": 0,
"[CLS]": 101,
"[SEP]": 102,
},
"deberta":{
"[PAD]": 0,
"[CLS]": 1,
"[SEP]": 2,
},
"longformer":{
"[PAD]": 1,
"[CLS]": 0,
"[SEP]": 2,
},
"bigbird":{
"[PAD]": 0,
"[CLS]": 65,
"[SEP]": 66
},
"reformer":{
"[PAD]": 2,
"[CLS]": 1,
"[SEP]": 2
},
"funnel":{
"[PAD]": 0,
"[CLS]": 101,
"[SEP]": 102,
},
"synthesizer":{
"[PAD]": 0,
"[CLS]": 101,
"[SEP]": 102,
},
"distill":{
"[PAD]": 0,
"[CLS]": 101,
"[SEP]": 102,
},
"newsbert":{
"[PAD]": 0,
"[CLS]": 101,
"[SEP]": 102,
},
}