-
Notifications
You must be signed in to change notification settings - Fork 3
/
pretrain.py
1170 lines (1062 loc) · 48.1 KB
/
pretrain.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
# coding=utf-8
# copied from hugginface github
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc.
# team.
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""UNITER pre-training runner."""
import argparse
from collections import defaultdict
import json
import math
import os
from os.path import exists, join
from time import time
import torch
from torch.utils.data import DataLoader
from torch.nn import functional as F
from torch.nn.utils import clip_grad_norm_
from apex import amp
from horovod import torch as hvd
from tqdm import tqdm
from data import (TokenBucketSampler, TokenBucketSamplerForItm,
MetaLoader, PrefetchLoader,
TxtTokLmdb, Img_SoftLabel_Lmdb, ImageLmdbGroup, ConcatDatasetWithLens,
MlmDataset, MlmDataset_VLXLMR, MlmDataset_Dmasking,
MmxlmDataset, VmlmDataset, Vmlm_Softlabel_Dataset,
Mmxlm_Softlabel_Dataset, MlmEvalDataset,
BlindMlmDataset, BlindMlmEvalDataset,
MrfrDataset, OnlyImgMrfrDataset,
MrcDataset, OnlyImgMrcDataset,
xlmr_mlm_dmasking_collate,xlmr_tlm_ni_dmasking_collate,
mlm_collate, xlmr_mlm_collate, xlmr_mmxlm_collate,
xlmr_mmxlm_softlabel_collate, mlm_eval_collate,
mlm_blind_collate, mlm_blind_eval_collate,
mrfr_collate, xlmr_mrfr_collate, mrfr_only_img_collate,
mrc_collate, xlmr_mrc_collate, mrc_only_img_collate,
ItmDataset, ItmDataset_HardNeg, itm_collate, itm_ot_collate, xlmr_itm_collate, xlmr_itm_ot_collate)
from data.mrm_nce import NegativeImageSampler, MrmNceDataset, mrm_nce_collate
from model import UniterForPretraining, VLXLMRForPretraining
from optim import get_lr_sched
from optim.misc import build_optimizer
from utils.logger import LOGGER, TB_LOGGER, RunningMeter, add_log_to_file
from utils.distributed import (all_reduce_and_rescale_tensors, all_gather_list,
broadcast_tensors)
from utils.save import ModelSaver, save_training_meta, TrainingRestorer
from utils.misc import NoOp, parse_with_config, set_dropout, set_random_seed
from utils.const import IMG_DIM, IMG_LABEL_DIM, BUCKET_SIZE
from model.const_variable import LABEL2TOKEN_MATRIX, VALID_XLMR_TOKEN_IDS
#import h5py
# with torch.no_grad():
# LABEL2TOKEN_MATRIX = torch.cuda.FloatTensor(LABEL2TOKEN_MATRIX)
WARM_STEP = 500
def rename_checkpoint(checkpoint, add_prefix="bert."):
old_keys = []
new_keys = []
for key in checkpoint.keys():
old_keys.append(key)
new_keys.append(add_prefix+key)
for new_key, old_key in zip(new_keys, old_keys):
checkpoint[new_key] = checkpoint.pop(old_key)
def build_dataloader(dataset, collate_fn, is_train, opts):
if is_train:
batch_size = opts.train_batch_size
else:
batch_size = opts.val_batch_size
sampler = TokenBucketSampler(dataset.lens, bucket_size=BUCKET_SIZE,
batch_size=batch_size, droplast=is_train)
loader = DataLoader(dataset, batch_sampler=sampler,
num_workers=opts.n_workers, pin_memory=opts.pin_mem,
collate_fn=collate_fn)
return loader
def build_dataloader_itm(dataset, collate_fn, is_train, opts):
if is_train:
batch_size = opts.train_batch_size
else:
batch_size = opts.val_batch_size
sampler = TokenBucketSamplerForItm(
dataset, bucket_size=BUCKET_SIZE,
batch_size=batch_size, droplast=is_train)
loader = DataLoader(dataset, batch_sampler=sampler,
num_workers=opts.n_workers, pin_memory=opts.pin_mem,
collate_fn=collate_fn)
return loader
#Modified by Mingyang to adapt to vlxlmr
def build_mlm_dataset(txt_db, img_db, blind, is_train, opts):
if is_train:
if blind:
#To Change if we come to use blind
collate_fn = mlm_blind_collate
datasets = [BlindMlmDataset(t) for t in txt_db]
else:
collate_fn = xlmr_mlm_collate
datasets = [MlmDataset(t, i) for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if blind:
#To Change if we come to use blind
collate_fn = mlm_blind_collate
dataset = BlindMlmDataset(txt_db)
else:
collate_fn = xlmr_mlm_collate
dataset = MlmDataset(txt_db, img_db)
return dataset, collate_fn
def build_tlm_dataset(txt_db, img_db, blind, is_train, opts, text_only=False):
if is_train:
if blind:
#To Change if we come to use blind
collate_fn = mlm_blind_collate
datasets = [BlindMlmDataset(t) for t in txt_db]
elif opts.co_masking:
if not text_only:
collate_fn = xlmr_mlm_dmasking_collate
else:
collate_fn = xlmr_tlm_ni_dmasking_collate
datasets = [MlmDataset_Dmasking(t, i, opts.co_masking_mode, text_only=text_only) for t, i in zip(txt_db, img_db)]
else:
collate_fn = xlmr_mlm_collate
datasets = [MlmDataset(t, i) for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if blind:
#To Change if we come to use blind
collate_fn = mlm_blind_collate
dataset = BlindMlmDataset(txt_db)
elif opts.co_masking:
collate_fn = xlmr_mlm_collate
dataset = MlmDataset_Dmasking(txt_db, img_db, opts.co_masking_mode, text_only=text_only)
else:
collate_fn = xlmr_mlm_collate
dataset = MlmDataset(txt_db, img_db)
return dataset, collate_fn
def build_mmxlm_dataset(txt_db, img_db, is_train, opts, soft=False):
if is_train:
if soft:
collate_fn = xlmr_mmxlm_softlabel_collate
datasets = [Mmxlm_Softlabel_Dataset(t, i, opts.mrm_prob) for t, i in zip(txt_db, img_db)]
else:
collate_fn = xlmr_mmxlm_collate
datasets = [MmxlmDataset(t, i, opts.mrm_prob) for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if soft:
collate_fn = xlmr_mmxlm_softlabel_collate
dataset = Mmxlm_Softlabel_Dataset(txt_db, img_db, opts.mrm_prob)
else:
collate_fn = xlmr_mmxlm_collate
dataset = MmxlmDataset(txt_db, img_db, opts.mrm_prob)
return dataset, collate_fn
def build_vmlm_dataset(txt_db, img_db, img_token_sl_db, is_train, opts, soft=False, language_list=None):
if is_train:
if soft:
collate_fn = xlmr_mmxlm_softlabel_collate
datasets = [Vmlm_Softlabel_Dataset(t, i, opts.mrm_prob, i_sl) for t, i, i_sl in zip(txt_db, img_db, img_token_sl_db)]
else:
collate_fn = xlmr_mmxlm_collate
#datasets = [VmlmDataset(t, i, opts.mrm_prob) for t, i in zip(txt_db, img_db)]
if language_list:
datasets = []
for t,i,lan in zip(txt_db, img_db, language_list):
#Get the languag
datasets.append(VmlmDataset(t,i, opts.mrm_prob, language=lan))
else:
datasets = [VmlmDataset(t, i, opts.mrm_prob) for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if soft:
collate_fn = xlmr_mmxlm_softlabel_collate
dataset = Vmlm_Softlabel_Dataset(txt_db, img_db, opts.mrm_prob, img_token_sl_db)
else:
collate_fn = xlmr_mmxlm_collate
if language_list:
dataset = VmlmDataset(txt_db, img_db, opts.mrm_prob, language=language_list[0])
else:
dataset = VmlmDataset(txt_db, img_db, opts.mrm_prob)
return dataset, collate_fn
def build_mrfr_dataset(txt_db, img_db, only_i, is_train, opts):
collate_fn = (mrfr_only_img_collate if only_i
else xlmr_mrfr_collate)
if is_train:
if only_i:
datasets = [OnlyImgMrfrDataset(opts.mrm_prob, i) for i in img_db]
else:
datasets = [MrfrDataset(opts.mrm_prob, t, i)
for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if only_i:
dataset = OnlyImgMrfrDataset(opts.mrm_prob, img_db)
else:
dataset = MrfrDataset(opts.mrm_prob, txt_db, img_db)
return dataset, collate_fn
def build_mrm_nce_dataset(txt_db, img_db, only_i, is_train, opts):
assert not only_i
neg_sampler = NegativeImageSampler(img_db, opts.neg_size)
collate_fn = mrm_nce_collate(neg_sampler)
if is_train:
datasets = [MrmNceDataset(opts.mrm_prob, t, i)
for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
dataset = MrmNceDataset(opts.mrm_prob, txt_db, img_db)
return dataset, collate_fn
def build_mrc_dataset(txt_db, img_db, only_i, is_train, opts):
collate_fn = (mrc_only_img_collate if only_i
else xlmr_mrc_collate)
if is_train:
if only_i:
datasets = [OnlyImgMrcDataset(opts.mrm_prob, i) for i in img_db]
else:
datasets = [MrcDataset(opts.mrm_prob, t, i)
for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if only_i:
dataset = OnlyImgMrcDataset(opts.mrm_prob, img_db)
else:
dataset = MrcDataset(opts.mrm_prob, txt_db, img_db)
return dataset, collate_fn
def build_itm_dataset(txt_db, img_db, is_train, opts):
if is_train:
if not opts.itm_hard_neg:
datasets = [ItmDataset(t, i, opts.itm_neg_prob)
for t, i in zip(txt_db, img_db)]
else:
datasets = [ItmDataset_HardNeg(t, i, opts.itm_neg_prob)
for t, i in zip(txt_db, img_db)]
dataset = ConcatDatasetWithLens(datasets)
else:
if not opts.itm_hard_neg:
dataset = ItmDataset(txt_db, img_db, opts.itm_neg_prob)
else:
dataset = ItmDataset_HardNeg(txt_db, img_db, opts.itm_neg_prob)
collate_fn = xlmr_itm_ot_collate if opts.itm_ot_lambda > 0 else xlmr_itm_collate
return dataset, collate_fn
def create_dataloaders(datasets, is_train, opts, all_img_dbs=None):
if all_img_dbs is None:
all_img_dbs = ImageLmdbGroup(opts.conf_th, opts.max_bb, opts.min_bb,
opts.num_bb, opts.compressed_db)
dataloaders = {}
for dset in datasets:
if is_train:
assert len(dset['db']) == len(dset['img'])
assert len(dset['tasks']) == len(dset['mix_ratio'])
img_db = [all_img_dbs[path] for path in dset['img']]
else:
assert len(dset['db']) == len(dset['img']) == 1
img_db = all_img_dbs[dset['img'][0]]
for i, t in enumerate(dset['tasks']):
task = f'{t}_{dset["name"]}'
if is_train:
LOGGER.info(f"Loading {task} train dataset "
f"{dset['db']}, {[img.img_dir for img in img_db]}")
txt_db = [TxtTokLmdb(path, opts.max_txt_len)
for path in dset['db']]
language_list = []
#only get the language_list from 'cc'
if dset['name'] == 'cc' and opts.multilingual_vmlm and task.startswith('vmlm'):
for path in dset['db']:
language = path.split('_')[-2] #Hacky Way to get the language, Need a better mechanism
language_list.append(language)
else:
LOGGER.info(f"Loading {task} validation dataset, "
f"{dset['db']}, {img_db.img_dir}")
txt_db = TxtTokLmdb(dset['db'][0], -1)
language_list = []
if opts.multilingual_vmlm and task.startswith('vmlm'):
lan = dset["name"].split('_')[-1]
language_list.append(lan)
if task.startswith('mlm'):
blind = 'blind' in task
dataset = build_mlm_dataset(txt_db, img_db,
blind, is_train, opts)
elif task.startswith('tlm'):
blind = 'blind' in task
text_only = "ni" in task
dataset = build_tlm_dataset(txt_db, img_db,
blind, is_train, opts, text_only)
elif task.startswith('mmxlm'):
if 'soft' in task:
soft = True
else:
soft = False
dataset = build_mmxlm_dataset(txt_db, img_db, is_train, opts, soft)
elif task.startswith('vmlm'):
if 'soft' in task:
soft = True
#load the img_soft_label
assert dset.get('img_token_soft_label', None) is not None
else:
soft = False
if is_train:
if soft:
assert len(dset['db']) == len(dset['img_token_soft_label'])
img_token_sl_db = [Img_SoftLabel_Lmdb(path) for path in dset['img_token_soft_label']]
else:
img_token_sl_db = None
else:
if soft:
assert len(dset['db']) == len(dset['img_token_soft_label']) == 1
img_token_sl_db = Img_SoftLabel_Lmdb(dset['img_token_soft_label'][0])
else:
img_token_sl_db = None
#print(language_list)
dataset = build_vmlm_dataset(txt_db, img_db, img_token_sl_db, is_train, opts, soft, language_list=language_list)
elif task.startswith('mrfr'):
only_i = 'only_i' in task
dataset = build_mrfr_dataset(txt_db, img_db,
only_i, is_train, opts)
elif task.startswith('mrm-nce'):
only_i = 'only_i' in task
dataset = build_mrm_nce_dataset(txt_db, img_db,
only_i, is_train, opts)
elif task.startswith('mrc'):
only_i = 'only_i' in task
dataset = build_mrc_dataset(txt_db, img_db,
only_i, is_train, opts)
elif task.startswith('itm'):
dataset = build_itm_dataset(txt_db, img_db, is_train, opts)
else:
raise ValueError(f'Undefined task {task}')
LOGGER.info(f"{len(dataset[0])*hvd.size()} samples loaded")
if task.startswith('itm'):
# itm handles distributed training in dset not sampler
loader = build_dataloader_itm(*dataset, is_train, opts)
else:
loader = build_dataloader(*dataset, is_train, opts)
if is_train:
ratio = dset['mix_ratio'][i]
dataloaders[task] = (loader, ratio)
else:
dataloaders[task] = PrefetchLoader(loader)
return dataloaders, all_img_dbs
def main(opts):
hvd.init()
n_gpu = hvd.size()
device = torch.device("cuda", hvd.local_rank())
torch.cuda.set_device(hvd.local_rank())
rank = hvd.rank()
opts.rank = rank
LOGGER.info("device: {} n_gpu: {}, rank: {}, "
"16-bits training: {}".format(
device, n_gpu, hvd.rank(), opts.fp16))
if opts.gradient_accumulation_steps < 1:
raise ValueError("Invalid gradient_accumulation_steps parameter: {}, "
"should be >= 1".format(
opts.gradient_accumulation_steps))
set_random_seed(opts.seed)
if rank == 0:
save_training_meta(opts)
TB_LOGGER.create(join(opts.output_dir, 'log'))
pbar = tqdm(total=opts.num_train_steps)
model_saver = ModelSaver(join(opts.output_dir, 'ckpt'))
add_log_to_file(join(opts.output_dir, 'log', 'log.txt'))
else:
LOGGER.disabled = True
pbar = NoOp()
model_saver = NoOp()
all_dbs = [db for datasets in [opts.train_datasets, opts.val_datasets]
for dset in datasets for db in dset['db']]
tokenizer = json.load(open(f'{all_dbs[0]}/meta.json'))['bert']
#print(tokenizer)
# assert all(tokenizer == json.load(open(f'{db}/meta.json'))['bert']
# for db in all_dbs)
# build data loaders
train_dataloaders, all_img_dbs = create_dataloaders(
opts.train_datasets, True, opts)
val_dataloaders, _ = create_dataloaders(
opts.val_datasets, False, opts, all_img_dbs)
meta_loader = MetaLoader(train_dataloaders,
accum_steps=opts.gradient_accumulation_steps,
distributed=n_gpu > 1)
meta_loader = PrefetchLoader(meta_loader)
# Prepare model
if opts.checkpoint:
checkpoint = torch.load(opts.checkpoint)
else:
checkpoint = {}
if opts.rename_checkpoints:
rename_checkpoint(checkpoint)
#Include early_adaptation
if opts.early_adaptation:
early_adaptation_checkpoint = torch.load(opts.early_adaptation_checkpoint)
checkpoint['roberta.img_embeddings.img_linear.weight'] = early_adaptation_checkpoint['v2w_linear.weight']
checkpoint['roberta.img_embeddings.img_linear.bias'] = early_adaptation_checkpoint['v2w_linear.bias']
model = VLXLMRForPretraining.from_pretrained(
opts.model_config, checkpoint,
img_dim=IMG_DIM, img_label_dim=IMG_LABEL_DIM,
nce_temp=opts.nce_temp, ot_pos_only=opts.ot_pos_only)
# model = UniterForPretraining.from_pretrained(
# opts.model_config, checkpoint,
# img_dim=IMG_DIM, img_label_dim=IMG_LABEL_DIM,
# nce_temp=opts.nce_temp, ot_pos_only=opts.ot_pos_only)
model.pad_vocab() # tensor core padding for vocabulary
model.to(device)
model.train()
# make sure every process has same model parameters in the beginning
broadcast_tensors([p.data for p in model.parameters()], 0)
set_dropout(model, opts.dropout)
# Prepare optimizer
optimizer = build_optimizer(model, opts)
task2scaler = {t: i for i, t in enumerate(train_dataloaders.keys())}
model, optimizer = amp.initialize(model, optimizer,
num_losses=len(task2scaler),
enabled=opts.fp16, opt_level='O2')
#global_step = 0
#Initialize the TrainingRestorer
restorer = TrainingRestorer(opts, model, optimizer)
global_step = restorer.global_step
TB_LOGGER._global_step = global_step
if hvd.rank() !=0:
restorer = NoOp() #Added for Restoring the Checkpoints
if global_step > 0:
pbar.update(global_step)
LOGGER.info(f"***** Running training with {n_gpu} GPUs *****")
LOGGER.info(" Batch size = %d", opts.train_batch_size)
LOGGER.info(" Accumulate steps = %d", opts.gradient_accumulation_steps)
LOGGER.info(" Num steps = %d", opts.num_train_steps)
# to compute training statistics
task2loss = {task: RunningMeter(f'loss/{task}')
for task in train_dataloaders.keys()}
# ITM w/ OT
if opts.itm_ot_lambda > 0:
for task in train_dataloaders.keys():
if task.startswith('itm'):
task2loss[f'{task}_xe'] = RunningMeter(f'loss/{task}_xe')
task2loss[f'{task}_ot'] = RunningMeter(f'loss/{task}_ot')
if not opts.ot_pos_only:
task2loss[f'{task}_ot_pos'] = RunningMeter(
f'loss/{task}_ot_pos')
task2loss[f'{task}_ot_neg'] = RunningMeter(
f'loss/{task}_ot_neg')
n_examples = defaultdict(int)
n_in_units = defaultdict(int)
n_loss_units = defaultdict(int)
n_neg_nce = defaultdict(int)
grad_norm = 0
start = time()
#Added by Mingyang to debug the training procedure
# debug_start = torch.cuda.Event(enable_timing=True)
# debug_end = torch.cuda.Event(enable_timing=True)
# quick hack for amp delay_unscale bug
optimizer.zero_grad()
optimizer.step()
#Added by Mingyang Zhou
# debug_start.record()
for step, (name, batch) in enumerate(meta_loader):
# forward pass
assert all(name == n for n in all_gather_list(name))
n_examples[name] += batch['input_ids'].size(0)
n_in_units[name] += (batch['attn_masks'] == 1).sum().item()
if 'nce' in name:
n_neg_nce[name] += batch['neg_feats'].size(0)
task = name.split('_')[0]
loss = model(batch, task=task, compute_loss=True)
if task.startswith('itm'):
# OT
itm_loss, ot_loss = loss
n_loss_units[name] += itm_loss.size(0)
itm_loss = itm_loss.mean()
if ot_loss is not None:
if not opts.ot_pos_only:
ot_pos, ot_neg = ot_loss
ot_loss = (ot_pos.sum() - ot_neg.sum()
) / (ot_pos.size(0) + ot_neg.size(0))
# NOTE: be ware of empty tensor
ot_pos = ot_pos.mean().item()
if not math.isnan(ot_pos):
task2loss[f'{name}_ot_pos'](ot_pos)
ot_neg = ot_neg.mean().item()
if not math.isnan(ot_neg):
task2loss[f'{name}_ot_neg'](ot_neg)
else:
ot_loss = ot_loss.mean()
loss = itm_loss + opts.itm_ot_lambda * ot_loss
task2loss[f'{name}_xe'](itm_loss.item())
task2loss[f'{name}_ot'](ot_loss.item())
else:
loss = itm_loss
elif task.startswith('vmlm-soft'):
loss = 1000*loss.mean()
else:
n_loss_units[name] += loss.size(0)
loss = loss.mean() # loss is not normalized in model
# backward pass
delay_unscale = (step+1) % opts.gradient_accumulation_steps != 0
with amp.scale_loss(loss, optimizer, delay_unscale=delay_unscale,
loss_id=task2scaler[name]) as scaled_loss:
scaled_loss.backward()
if not delay_unscale:
# gather gradients from every processes
# do this before unscaling to make sure every process uses
# the same gradient scale
grads = [p.grad.data for p in model.parameters()
if p.requires_grad and p.grad is not None]
all_reduce_and_rescale_tensors(grads, float(1))
task2loss[name](loss.item())
# optimizer update and logging
if (step + 1) % opts.gradient_accumulation_steps == 0:
global_step += 1
# learning rate scheduling
lr_this_step = get_lr_sched(global_step, opts)
for param_group in optimizer.param_groups:
param_group['lr'] = lr_this_step
TB_LOGGER.add_scalar('lr', lr_this_step, global_step)
# log loss
# for t, l in task2loss.items():
# loss = sum(v for v in all_gather_list(l.val)
# if v is not None) / hvd.size()
# task2loss[t] = RunningMeter(f'loss/{t}', loss)
TB_LOGGER.log_scaler_dict({l.name: l.val
for l in task2loss.values()
if l.val is not None})
TB_LOGGER.step()
# update model params
if opts.grad_norm != -1:
'''
if global_step % 10 == 0 and not opts.fp16:
bias = model.bert.img_embeddings.img_linear.bias
weight = model.bert.img_embeddings.img_linear.weight
print(f"bnorm: {bias.norm()}")
print(f"wnorm: {weight.norm()}")
print(f"bgnorm: {bias.grad.norm()}")
print(f"wgnorm: {weight.grad.norm()}")
mask = model.bert.img_embeddings.mask_embedding.weight
print(f"mnorm: {mask.norm()}")
print(f"mgnorm: {mask.grad.norm()}")
print([(n, p.grad.norm().item())
for n, p in model.named_parameters()
if p.grad is not None
and p.grad.norm().item() > grad_norm/10])
'''
grad_norm = clip_grad_norm_(amp.master_params(optimizer),
opts.grad_norm)
TB_LOGGER.add_scalar('grad_norm', grad_norm, global_step)
optimizer.step()
optimizer.zero_grad()
pbar.update(1)
if global_step % 100 == 0:
# monitor training throughput
LOGGER.info(f'==============Step {global_step}===============')
for t in train_dataloaders.keys():
assert all(tt == t for tt in all_gather_list(t))
tot_ex = sum(all_gather_list(n_examples[t]))
ex_per_sec = int(tot_ex / (time()-start))
tot_in = sum(all_gather_list(n_in_units[t]))
in_per_sec = int(tot_in / (time()-start))
tot_l = sum(all_gather_list(n_loss_units[t]))
l_per_sec = int(tot_l / (time()-start))
LOGGER.info(f'{t}: {tot_ex} examples trained at '
f'{ex_per_sec} ex/s')
TB_LOGGER.add_scalar(f'perf/{t}_ex_per_s', ex_per_sec,
global_step)
TB_LOGGER.add_scalar(f'perf/{t}_in_per_s', in_per_sec,
global_step)
TB_LOGGER.add_scalar(f'perf/{t}_loss_per_s', l_per_sec,
global_step)
if 'nce' in t:
avg_neg = sum(all_gather_list(n_neg_nce[t])
) / hvd.size() // step
LOGGER.info(f'{t}: averaging '
f'{avg_neg} negative samples')
LOGGER.info(f'===============================================')
if global_step % opts.valid_steps == 0:
LOGGER.info(f'Step {global_step}: start validation')
validate(model, val_dataloaders)
#os.makedir('/'.join([opts.output_dir, "ckpt")
model_saver.save(model, global_step, optimizer)
restorer.step()
if global_step >= opts.num_train_steps:
break
if global_step % opts.valid_steps != 0:
LOGGER.info(f'Step {global_step}: start validation')
validate(model, val_dataloaders)
model_saver.save(model, global_step)
def validate(model, val_dataloaders):
model.eval()
for task, loader in val_dataloaders.items():
LOGGER.info(f"validate on {task} task")
if task.startswith('mlm'):
val_log = validate_mlm(model, loader)
elif task.startswith('mmxlm-soft'):
val_log = validate_mmxlm_soft(model, loader)
elif task.startswith('mmxlm'):
val_log = validate_mmxlm(model, loader)
elif task.startswith('vmlm-soft'):
val_log = validate_vmlm_soft(model, loader)
elif task.startswith('vmlm'):
val_log = validate_vmlm(model, loader)
elif task.startswith('mrfr'):
val_log = validate_mrfr(model, loader)
elif task.startswith('mrm-nce'):
val_log = validate_mrm_nce(model, loader)
elif task.startswith('mrc'):
val_log = validate_mrc(model, loader, task)
elif task.startswith('itm'):
val_log = validate_itm(model, loader)
else:
raise ValueError(f'Undefined task {task}')
val_log = {f'{task}_{k}': v for k, v in val_log.items()}
TB_LOGGER.log_scaler_dict(
{f'valid_{task}/{k}': v for k, v in val_log.items()})
model.train()
@torch.no_grad()
def validate_mmxlm_soft(model, val_loader):
LOGGER.info("start running MMXLM_SOFT validation...")
val_loss = 0
n_feat = 0
st = time()
tot_score = 0
for i, batch in enumerate(val_loader):
prediction_soft_label = model(
batch, task="mmxlm-soft", compute_loss=False)
#if "kl" in task:
prediction_soft_label = F.log_softmax(
prediction_soft_label, dim=-1)
label_targets = batch['label_targets']
loss = F.kl_div(
prediction_soft_label, label_targets, reduction='sum')
tot_score += compute_accuracy_for_soft_targets(
prediction_soft_label, label_targets)
val_loss += loss.item()
n_feat += batch['tgt_masks'].sum().item()
val_loss = sum(all_gather_list(val_loss))
tot_score = sum(all_gather_list(tot_score))
n_feat = sum(all_gather_list(n_feat))
tot_time = time()-st
val_loss /= n_feat
val_acc = tot_score / n_feat
val_log = {'loss': val_loss,
'acc': val_acc,
'feat_per_s': n_feat/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"score: {val_acc*100:.2f}")
return val_log
@torch.no_grad()
def validate_mmxlm(model, val_loader):
LOGGER.info("start running MMXLM validation...")
val_loss = 0
n_correct = 0
n_word = 0
st = time()
for i, batch in enumerate(val_loader):
scores = model(batch, task='mmxlm', compute_loss=False)
labels = batch['txt_labels']
labels = labels[labels != -1]
loss = F.cross_entropy(scores, labels, reduction='sum')
val_loss += loss.item()
n_correct += (scores.max(dim=-1)[1] == labels).sum().item()
n_word += labels.numel()
val_loss = sum(all_gather_list(val_loss))
n_correct = sum(all_gather_list(n_correct))
n_word = sum(all_gather_list(n_word))
tot_time = time()-st
val_loss /= n_word
acc = n_correct / n_word
val_log = {'loss': val_loss,
'acc': acc,
'tok_per_s': n_word/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"acc: {acc*100:.2f}")
return val_log
@torch.no_grad()
def validate_vmlm(model, val_loader):
LOGGER.info("start running VMLM validation...")
val_loss = 0
n_correct = 0
n_word = 0
st = time()
for i, batch in enumerate(val_loader):
scores = model(batch, task='vmlm', compute_loss=False)
labels = batch['txt_labels']
labels = labels[labels != -1]
loss = F.cross_entropy(scores, labels, reduction='sum')
val_loss += loss.item()
n_correct += (scores.max(dim=-1)[1] == labels).sum().item()
n_word += labels.numel()
val_loss = sum(all_gather_list(val_loss))
n_correct = sum(all_gather_list(n_correct))
n_word = sum(all_gather_list(n_word))
tot_time = time()-st
val_loss /= n_word
acc = n_correct / n_word
val_log = {'loss': val_loss,
'acc': acc,
'tok_per_s': n_word/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"acc: {acc*100:.2f}")
return val_log
@torch.no_grad()
def validate_vmlm_soft(model, val_loader):
LOGGER.info("start running VMLM-SOFT validation...")
val_loss = 0
n_feat = 0
st = time()
tot_score = 0
#label2token_matrix = torch.float(LABEL2TOKEN_MATRIX)
for i, batch in enumerate(val_loader):
prediction_soft_label = model(
batch, task='vmlm-soft', compute_loss=False)
prediction_soft_label = F.log_softmax(
prediction_soft_label, dim=-1)
label_targets = batch['label_targets']
#convert label_targets to a new dimension
# label_targets = torch.matmul(label_targets, torch.cuda.FloatTensor(LABEL2TOKEN_MATRIX))
# label_targets = label_targets[:, VALID_XLMR_TOKEN_IDS]
# label_targets = label_targets / torch.sum(label_targets, dim=1, keepdim=True)
tot_score += compute_accuracy_for_soft_targets(
prediction_soft_label, label_targets)
loss = F.kl_div(
prediction_soft_label, label_targets, reduction='sum')
val_loss += loss.item()
n_feat += batch['tgt_masks'].sum().item()
val_loss = sum(all_gather_list(val_loss))
tot_score = sum(all_gather_list(tot_score))
n_feat = sum(all_gather_list(n_feat))
tot_time = time()-st
val_loss /= n_feat
val_acc = tot_score / n_feat
val_log = {'loss': val_loss,
'acc': val_acc,
'feat_per_s': n_feat/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"score: {val_acc*100:.2f}")
return val_log
@torch.no_grad()
def validate_mlm(model, val_loader):
LOGGER.info("start running MLM validation...")
val_loss = 0
n_correct = 0
n_word = 0
st = time()
for i, batch in enumerate(val_loader):
scores = model(batch, task='mlm', compute_loss=False)
labels = batch['txt_labels']
labels = labels[labels != -1]
loss = F.cross_entropy(scores, labels, reduction='sum')
val_loss += loss.item()
n_correct += (scores.max(dim=-1)[1] == labels).sum().item()
n_word += labels.numel()
val_loss = sum(all_gather_list(val_loss))
n_correct = sum(all_gather_list(n_correct))
n_word = sum(all_gather_list(n_word))
tot_time = time()-st
val_loss /= n_word
acc = n_correct / n_word
val_log = {'loss': val_loss,
'acc': acc,
'tok_per_s': n_word/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"acc: {acc*100:.2f}")
return val_log
@torch.no_grad()
def validate_mlm_old(model, val_loader):
LOGGER.info("start running MLM validation...")
val_loss = 0
n_correct = 0
n_word = 0
st = time()
for i, batch in enumerate(val_loader):
scores = model.forward(batch, task='mlm', compute_loss=False)
loss_fct = torch.nn.CrossEntropyLoss(ignore_index=-1,
reduction='sum')
scores = scores.contiguous().view(-1, model.config.vocab_size)
labels = batch['txt_labels'].contiguous().view(-1)
loss = loss_fct(scores, labels)
val_loss += loss.item()
n_correct += accuracy_count(scores, labels)
n_word += batch['txt_labels'].numel()
val_loss = sum(all_gather_list(val_loss))
n_correct = sum(all_gather_list(n_correct))
n_word = sum(all_gather_list(n_word))
tot_time = time()-st
val_loss /= n_word
acc = n_correct / n_word
val_log = {'loss': val_loss,
'acc': acc,
'tok_per_s': n_word/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"acc: {acc*100:.2f}")
return val_log
def accuracy_count(out, labels):
outputs = out.max(dim=-1)[1]
mask = labels != -1
n_correct = (outputs == labels).masked_select(mask).sum().item()
return n_correct
@torch.no_grad()
def validate_mrfr(model, val_loader):
LOGGER.info("start running MRFR validation...")
val_loss = 0
n_feat = 0
st = time()
for i, batch in enumerate(val_loader):
loss = model(batch, task='mrfr', compute_loss=True)
val_loss += loss.sum().item() / IMG_DIM
n_feat += batch['img_mask_tgt'].sum().item()
val_loss = sum(all_gather_list(val_loss))
n_feat = sum(all_gather_list(n_feat))
tot_time = time()-st
val_loss /= n_feat
val_log = {'loss': val_loss,
'feat_per_s': n_feat/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"loss: {val_loss:.2f}")
return val_log
@torch.no_grad()
def validate_mrm_nce(model, val_loader):
LOGGER.info("start running MRM-NCE validation...")
val_loss = 0
val_l2 = 0
n_correct = 0
cosine = 0
n_feat = 0
n_neg = 0
st = time()
for i, batch in enumerate(val_loader):
feats, pos_feats, neg_feats = model(batch, task='mrm-nce',
compute_loss=False)
logits = model.mrm_nce(feats, pos_feats, neg_feats,
compute_loss=False)
targets = torch.arange(0, logits.size(0),
dtype=torch.long, device=logits.device)
val_loss += F.cross_entropy(logits, targets, reduction='sum').item()
val_l2 += F.mse_loss(feats, pos_feats, reduction='sum'
).item() / feats.size(-1)
n_correct += (logits.max(dim=-1)[1] == targets).sum().item()
cosine += F.cosine_similarity(feats, pos_feats, dim=-1).sum().item()
nf = batch['img_mask_tgt'].sum().item()
n_feat += nf
n_neg += neg_feats.size(0) * nf
val_loss = sum(all_gather_list(val_loss))
val_l2 = sum(all_gather_list(val_l2))
n_correct = sum(all_gather_list(n_correct))
cosine = sum(all_gather_list(cosine))
n_feat = sum(all_gather_list(n_feat))
n_neg = sum(all_gather_list(n_neg))
tot_time = time()-st
val_loss /= n_feat
val_acc = n_correct / n_feat
val_log = {'loss': val_loss,
'acc': val_acc,
'l2': val_l2 / n_feat,
'cosine': cosine / n_feat,
'feat_per_s': n_feat/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"loss: {val_loss:.2f}, acc: {val_acc*100:.2f} "
f"(average {n_neg/n_feat:.0f} negatives)")
return val_log
@torch.no_grad()
def validate_mrc(model, val_loader, task):
LOGGER.info("start running MRC validation...")
val_loss = 0
n_feat = 0
st = time()
tot_score = 0
for i, batch in enumerate(val_loader):
prediction_soft_label = model(
batch, task=task, compute_loss=False)
if "kl" in task:
prediction_soft_label = F.log_softmax(
prediction_soft_label, dim=-1)
label_targets = batch['label_targets']
loss = F.kl_div(
prediction_soft_label, label_targets, reduction='sum')
tot_score += compute_accuracy_for_soft_targets(
prediction_soft_label, label_targets)
else:
# background class should not be the target
label_targets = batch['label_targets']
cls_label_targets = label_targets[:, 1:].max(dim=-1)[1] + 1
loss = F.cross_entropy(
prediction_soft_label, cls_label_targets,
ignore_index=0, reduction='sum')
tot_score += compute_accuracy_for_soft_targets(
prediction_soft_label[:, 1:], label_targets[:, 1:])
val_loss += loss.item()
n_feat += batch['img_mask_tgt'].sum().item()
val_loss = sum(all_gather_list(val_loss))
tot_score = sum(all_gather_list(tot_score))
n_feat = sum(all_gather_list(n_feat))
tot_time = time()-st
val_loss /= n_feat
val_acc = tot_score / n_feat
val_log = {'loss': val_loss,
'acc': val_acc,
'feat_per_s': n_feat/tot_time}
LOGGER.info(f"validation finished in {int(tot_time)} seconds, "
f"score: {val_acc*100:.2f}")
return val_log
def compute_accuracy_for_soft_targets(out, labels):
outputs = out.max(dim=-1)[1]
labels = labels.max(dim=-1)[1] # argmax
# print(outputs)
# print(labels)
n_correct = (outputs == labels).sum().item()
return n_correct
def compute_accuracy_for_soft_targets_vmlm(out, labels):
outputs = out.max(dim=-1)[1]
labels = labels.max(dim=-1)[1] # argmax
n_correct = (outputs == labels).sum().item()