/
pretrain_model.py
1239 lines (1115 loc) · 50.9 KB
/
pretrain_model.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
# Copyright (c) 2020, Hicham EL BOUKKOURI. 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.
r"""
Script for pre-training BERT / CharacterBERT.
NOTE: this is adapted from an older version of:
https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/BERT/run_pretraining.py
"""
import os
import csv
import math
import random
import logging
import argparse
import datetime
import warnings
from concurrent.futures import ProcessPoolExecutor
import h5py
import numpy as np
from tqdm import tqdm, trange
import torch
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import (
DataLoader, RandomSampler, SequentialSampler, Dataset
)
import amp_C
import apex_C
from apex import amp
from apex.optimizers import FusedLAMB
from apex.parallel import DistributedDataParallel
from apex.parallel.distributed import flat_dist_call
from apex.amp import _amp_state
from schedulers import LinearWarmUpScheduler, PolyWarmUpScheduler
from transformers import (
BertConfig, BertTokenizer, BertForPreTraining,
CharacterBertConfig, CharacterBertTokenizer, CharacterBertForPreTraining
)
from utils.distributed import is_main_process
warnings.filterwarnings("ignore")
WORKDIR = os.environ['WORKDIR']
LOGGING_FORMAT = "%(asctime)s | PID: %(process)d | %(filename)s | %(levelname)s - %(message)s"
logging.basicConfig(format=LOGGING_FORMAT, datefmt="%d/%m/%Y %H:%M:%S", level=logging.INFO)
IGNORE_INDEX = torch.nn.CrossEntropyLoss().ignore_index
def set_all_random_seeds(random_seed: int, verbose: bool = True):
r"""Sets the initial random seed to a specific value."""
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(random_seed)
if verbose:
logging.info("Setting random seed to: %d", random_seed)
class PretrainingDataset(Dataset):
r"""
PyTorch Dataset subclass that allows easy access to the pre-training
data previously stored in an .hdf5 file.
Args:
hdf5_fpath (:obj:`str`):
Path to an .hdf5 file contraining the pre-training data.
max_masked_tokens_per_input (:obj:`int`):
Hard limit on the number of masked tokens per input sequence.
This is therefore also a limit on the number of MLM predictions
per input sequence.
"""
def __init__(self,
hdf5_fpath: str,
max_masked_tokens_per_input
):
self.hdf5_fpath = hdf5_fpath
self.max_masked_tokens_per_input = max_masked_tokens_per_input
file_in = h5py.File(hdf5_fpath, "r")
keys = [
'input_ids',
'input_mask',
'segment_ids',
'masked_lm_positions',
'masked_lm_ids',
'next_sentence_labels'
]
self.inputs = [np.asarray(file_in[key][:]) for key in keys]
file_in.close()
def __len__(self):
"""Returns the total number of samples in the pre-training dataset."""
return len(self.inputs[0])
def __getitem__(self, index):
"""Returns the sample at the provided index."""
# Get elements at `index` as torch tensors
[
input_ids, input_mask, segment_ids,
masked_lm_positions, masked_lm_ids, next_sentence_labels
] = [
torch.from_numpy(element[index].astype(np.int64)) if i < 5
else torch.from_numpy(np.asarray(element[index].astype(np.int64)))
for i, element in enumerate(self.inputs)
]
# MLM labels is IGNORE_INDEX everywhere and `token_id` at masked positions
index = self.max_masked_tokens_per_input
masked_lm_labels = IGNORE_INDEX * torch.ones((input_ids.shape[0],), dtype=torch.long)
padded_mask_indices = (masked_lm_positions == 0).nonzero()
if len(padded_mask_indices) != 0:
index = padded_mask_indices[0].item()
masked_lm_labels[masked_lm_positions[:index]] = masked_lm_ids[:index]
return [
input_ids, segment_ids, input_mask,
masked_lm_labels, next_sentence_labels
]
def create_pretraining_dataloader(
hdf5_fpath: str,
max_masked_tokens_per_input: int,
batch_size: int
):
r"""
Makes a PyTorch DataLoader for producing random batches of pre-training
tensors using data stored in an .hdf5 file. This also returns the path
to the .hdf5 file for ... TODO: figure out why?
Args:
hdf5_fpath (:obj:`str`):
Path to an .hdf5 file contraining the pre-training data.
max_masked_tokens_per_input (:obj:`int`):
Hard limit on the number of masked tokens per input sequence.
This is therefore also a limit on the number of MLM predictions
per input sequence.
batch_size (:obj:`int`):
Batch size of tensors returned by the DataLoader.
"""
pretraining_data = PretrainingDataset(
hdf5_fpath=hdf5_fpath,
max_masked_tokens_per_input=max_masked_tokens_per_input
)
train_sampler = RandomSampler(pretraining_data)
train_dataloader = DataLoader(
pretraining_data,
sampler=train_sampler,
batch_size=batch_size,
num_workers=4,
pin_memory=True
)
return train_dataloader, hdf5_fpath
def parse_args():
r"""Parses a number of arguments to set as attributes for `ModelPretrainer`."""
parser = argparse.ArgumentParser()
##################################################################
# Required parameters:
# ---------------------------------------------------------------
# - input/output dirs
# - model config (BertConfig / CharacterBertConfig)
# - a flag for pre-training CharacterBERT instead of BERT
##################################################################
parser.add_argument(
"--hdf5_directory",
type=str, required=True,
help=\
"Path to a directory contraining hdf5 files: training.*.hdf5, "
"validation.*.hdf5 and test.*.hdf5 files."
)
parser.add_argument(
"--output_directory",
type=str, required=True,
help=\
"Path to a directory where model checkpoints and metrics "
"will be saved."
)
parser.add_argument(
'--is_character_bert',
action='store_true',
help="Pre-train CharacterBERT instead of BERT."
)
##################################################################
# Other parameters
##################################################################
# Parameters related to checkpoint handling
parser.add_argument(
'--random_seed',
required=False, type=int, default=42,
help=\
"An intial seed for controlling some of the randomness."
)
parser.add_argument(
"--local_rank",
type=int, default=-1,
help=\
"Identifier of the current process within the distributed process "
"group. This is always `-1` when distributed training is deactivated."
)
parser.add_argument(
'--phase1_end_step',
type=int, default=7038,
help=\
"Number of training steps (backprops) in pre-training phase n°1: "
"`max_input_length=128`and `max_masked_tokens_per_input=20`."
)
parser.add_argument(
'--phase2',
action='store_true',
help=\
"Whether it is pre-training phase n°2: "
"`max_input_length=512`and `max_masked_tokens_per_input=80`."
)
parser.add_argument(
"--init_checkpoint",
type=str, default=None,
help="An initial checkpoint to start pre-training from."
)
parser.add_argument(
"--resume_pretraining",
action='store_true',
help="Whether to resume pre-training from a checkpoint."
)
parser.add_argument(
'--resume_step',
type=int, default=-1,
help=\
"Step to resume pre-training from. By default, this is `-1` "
"which results in resuming from the latest checkpoint available."
)
##################################################################
# Training hyperparameters
parser.add_argument(
'--max_input_length',
required=False, type=int, default=128,
help=\
"Maximum sequence length for the model input. "
"Set this according to the input .hdf5 files contents."
)
parser.add_argument(
"--max_masked_tokens_per_input",
type=int, default=20,
help=\
"Hard limit on the number of tokens that can be masked. "
"Set this according to the input .hdf5 files contents."
)
parser.add_argument(
'--num_accumulation_steps',
type=int, default=512,
help=\
"Number of steps (forward passes) during which gradients are "
"accumulated before running a single model parameters update."
)
parser.add_argument(
"--target_batch_size",
type=int, default=8192,
help=\
"Target batch size post-accumulation (actual batch size is "
"derived from the number of accumulation steps). For example, if "
"`target_batch_size=32` and `num_accumulation_steps=4` then the "
"actual batch size will be `32/4 = 8`. This is useful for "
"achieving larger batch sizes while keeping an actual batch size "
"that is small enough to fit in memory."
)
parser.add_argument(
"--learning_rate",
type=float, default=6e-3,
help="The initial learning rate for the FusedLAMB optimizer."
)
parser.add_argument(
"--warmup_proportion",
type=float, default=0.2843,
help=\
"A value of X means that learning rate will increase during "
"(100*X)%% of pre-training steps before reaching the desired value "
"then decrease to 0 during the rest of pre-training steps."
)
parser.add_argument(
"--total_steps",
type=float, default=7038,
help="Total number of pre-training steps to perform."
)
##################################################################
# fp16 related parameters
parser.add_argument(
'--fp16',
action='store_true',
help="Whether to use 16-bit float precision instead of 32-bit"
)
parser.add_argument(
'--loss_scale',
type=float, default=0.0,
help='Loss scaling, positive power of 2 values can improve fp16 convergence.'
)
parser.add_argument(
'--allreduce_post_accumulation',
action='store_true',
help="Whether to do allreduces during gradient accumulation steps."
)
parser.add_argument(
'--allreduce_post_accumulation_fp16',
action='store_true',
help="Whether to do fp16 allreduce post accumulation.")
##################################################################
# Logging and checkpointing
parser.add_argument(
'--do_validation',
action='store_true',
help="Whether to run a validation step before checkpointing."
)
parser.add_argument(
'--checkpoint_interval',
type=int, default=200,
help=\
"Number of model updates before a model checkpoint is saved."
)
parser.add_argument(
'--num_checkpoints_to_keep',
type=int, default=3,
help=\
"Maximum number of checkpoints to keep."
)
parser.add_argument(
'--log_freq',
type=float, default=1.0,
help='Frequency of logging loss.'
)
parser.add_argument(
'--tensorboard_id',
type=str, default='default',
help="Name of the directory where Tensorboard logs will be saved."
)
args = parser.parse_args()
return args
class ModelPretrainer:
r"""A helper class for pre-training BERT and CharacterBERT models."""
def __init__(self, args):
self.start_datetime = datetime.datetime.now()
# Set attributes from parsed arguments
self.hdf5_directory = args.hdf5_directory
self.output_directory = args.output_directory
self.tensorboard_id = args.tensorboard_id
self.is_character_bert = args.is_character_bert
self.local_rank = args.local_rank
self.phase1_end_step = args.phase1_end_step
self.phase2 = args.phase2
self.init_checkpoint = args.init_checkpoint
self.resume_pretraining = args.resume_pretraining
self.resume_step = args.resume_step
self.max_input_length = args.max_input_length
self.max_masked_tokens_per_input = args.max_masked_tokens_per_input
self.target_batch_size = args.target_batch_size
self.learning_rate = args.learning_rate
self.total_steps = args.total_steps
self.warmup_proportion = args.warmup_proportion
self.num_accumulation_steps = args.num_accumulation_steps
self.allreduce_post_accumulation = args.allreduce_post_accumulation
self.fp16 = args.fp16
self.loss_scale = args.loss_scale
self.allreduce_post_accumulation_fp16 = args.allreduce_post_accumulation_fp16
self.log_freq = args.log_freq
self.do_validation = args.do_validation
self.checkpoint_interval = args.checkpoint_interval
self.num_checkpoints_to_keep = args.num_checkpoints_to_keep
self.random_seed = args.random_seed
self.is_main_process = (
self.local_rank in [-1, 0]) and is_main_process()
if self.is_main_process:
logging.info('Preparing to run pre-training using parameters:')
for argname, argvalue in vars(args).items():
logging.info(' * %s: %s', argname, argvalue)
# Set the random seed for reproducibility
set_all_random_seeds(self.random_seed, verbose=self.is_main_process)
# Make sure CUDA is available (it won't be if you're not using GPUs):
assert torch.cuda.is_available(), "CUDA is unavailable (are you using GPUs?)"
# Set CUDA-related attributes
self.training_is_distributed = (self.local_rank != -1)
if self.training_is_distributed:
torch.cuda.set_device(self.local_rank)
self.device = torch.device("cuda", self.local_rank)
# Initialize distributed backend (takes care of sychronizing nodes/GPUs)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
self.n_gpu = 1
else:
# TODO: test this
self.device = torch.device("cuda")
self.n_gpu = torch.cuda.device_count()
self.allreduce_post_accumulation = False
self.allreduce_post_accumulation_fp16 = False
if self.num_accumulation_steps == 1:
self.allreduce_post_accumulation = False
self.allreduce_post_accumulation_fp16 = False
logging.info(
"Distributed Training: %s, Number of GPUs: %d, Device: `%s`, Local Rank: `%s` (is_main: `%s`)",
self.training_is_distributed, self.n_gpu, self.device, self.local_rank, self.is_main_process,
)
# Derive actual batch size from target batch size and accumulation steps:
assert self.num_accumulation_steps >= 1, \
"`num_accumulation_steps` should be greater or equal to 1"
assert self.target_batch_size % self.num_accumulation_steps == 0, \
"`target_batch_size` should be divisible by `num_accumulation_steps`"
self.batch_size = self.target_batch_size // self.num_accumulation_steps
# Make sure self.output_directory is empty when starting a training from scratch:
if not self.resume_pretraining:
os.makedirs(self.output_directory, exist_ok=True)
assert not any([
fname.startswith('ckpt')
for fname in os.listdir(self.output_directory)]), \
"Output directory should be empty when not resuming from a previous checkpoint"
self.global_step = None # training step counter
self.checkpoint = None # checkpoint for resuming training
self.model = None # actual model we are pre-training
self.optimizer = None # the optimizer (FusedLAMB)
self.lr_scheduler = None # the scheduler (PolyWarmUpScheduler)
self.tensorboard_writer = None # helper for logging loss to Tensorboard
self.best_validation_loss = float(1e6) # best val. loss achieved so far
self.most_recent_ckpts_paths = [] # list of most recent ckpt paths
def prepare_model_optimizer_and_scheduler(self):
r"""Prepares the model, the optimizer and the learning rate scheduler."""
###################################################################
# MODEL PREPARATION
# -----------------
# - step 1: Initialize a random model from config
# - step 2: Load model weights from checkpoint if any
# - step 3: Move model to device (GPU)
###################################################################
# Initialize a random model according to a specific config:
# NOTE: here we load from a physical path instead of using a keyword
# as compute nodes may not allow downloading from online hubs
if self.is_character_bert:
model_config = CharacterBertConfig.from_pretrained(
os.path.join(WORKDIR, 'data', 'character-bert'))
model = CharacterBertForPreTraining(model_config)
else:
model_config = BertConfig.from_pretrained(
os.path.join(WORKDIR, 'data', 'bert-base-uncased'))
model = BertForPreTraining(model_config)
if self.is_main_process:
logging.info(
"Initialized %s using Config:\n%s",
"CharacterBERT" if self.is_character_bert else "BERT",
model_config
)
# Load checkpoint if any:
if not self.resume_pretraining:
# CASE: no checkpoint -> training from scratch
self.global_step = 0
if self.is_main_process:
logging.info("Pre-training from scratch (good luck!)")
else:
if self.init_checkpoint:
# CASE: load checkpoint from direct path
self.global_step = 0
init_checkpoint = self.init_checkpoint
if self.is_main_process:
logging.info(
"Resuming pre-training from specific checkpoint `%s`",
init_checkpoint
)
else:
# CASE: load checkpoint from resume_step
if self.is_main_process:
logging.info(
"Resuming pre-training from step `%s`. "
"Looking inside `output_directory` for checkpoints...",
self.resume_step
)
if self.resume_step == -1:
# CASE: resume_step == -1, load latest checkpoint
model_names = [
fname
for fname in os.listdir(self.output_directory)
if fname.endswith(".pt")]
assert model_names, "Could not find any checkpoints to resume from."
self.resume_step = max([
int(x.split('.pt')[0].split('_')[1].strip())
for x in model_names]) # TODO: find a better way for this
if self.is_main_process:
logging.info(
"Resuming from latest checkpoint: ckpt_%s.pt",
self.resume_step
)
else:
# CASE: resume_step == X, load checkpoint: `ckpt_X.pt`
if self.is_main_process:
logging.info(
"Resuming from checkpoint: ckpt_%s.pt",
self.resume_step
)
self.global_step = self.resume_step
init_checkpoint = os.path.join(
self.output_directory, f"ckpt_{self.resume_step}.pt")
# Load the actual checkpoint file
self.checkpoint = torch.load(
init_checkpoint, map_location="cpu"
)
# NOTE: Keeping these lines below as a reminder that re-training on
# a different domain with CharacterBERT requires changing the
# output layer with a topK tokens matrix from the new domain.
# # Case where we would retrain a general_domain CharacterBERT
# # on the medical domain. Don't use the general domain output layer:
# if self.is_medical_domain and self.is_character_bert and (not self.phase2):
# model.load_state_dict(
# {
# k: v for (k, v) in self.checkpoint['model'].items()
# # Don't load output matrix from general domain model
# if not k.startswith('cls.predictions') # ignoring the old output layer
# },
# strict=False)
# if self.is_main_process:
# logging.warning(
# "Loaded model weights from `%s`, "
# "but ignored the `cls.predictions` module.",
# init_checkpoint)
# # General case: load weights from checkpoint
# else:
# model.load_state_dict(self.checkpoint['model'], strict=True)
# if self.is_main_process:
# logging.info('Loaded model weights from `%s`',
# init_checkpoint)
# General case: load weights from checkpoint
model.load_state_dict(self.checkpoint['model'], strict=True)
if self.is_main_process:
logging.info('Loaded model weights from `%s`', init_checkpoint)
# Deduce previous steps from phase1 when in phase2
if self.phase2 and not self.init_checkpoint:
self.global_step -= self.phase1_end_step
if self.is_main_process:
logging.info("Training will start at global_step=%s", self.global_step)
# Move model to GPU:
model.to(self.device)
if self.is_main_process:
logging.info("Model was moved to device: %s", self.device)
###################################################################
# OPTIMIZER / SCHEDULER PREPARATION
# ---------------------------------
# - step 1: Define the optimizer (FusedLAMB w/ some weight decay)
# - step 2: Define the learning rate scheduler (PolyWarmUpScheduler)
###################################################################
# Initialize an optimizer:
no_decay = ['bias', 'gamma', 'beta', 'LayerNorm'] # no weight decay
optimizer_grouped_parameters = [
{
'params': [
param for name, param in model.named_parameters()
if not any((nd in name) for nd in no_decay)],
'weight_decay': 0.01
},
{
'params': [
param for name, param in model.named_parameters()
if any((nd in name) for nd in no_decay)],
'weight_decay': 0.0
}
]
optimizer = FusedLAMB(
optimizer_grouped_parameters, lr=self.learning_rate)
if self.is_main_process:
logging.info("Using optimizer: %s", optimizer)
# Initialize a learning rate scheduler:
self.lr_scheduler = PolyWarmUpScheduler(
optimizer,
warmup=self.warmup_proportion,
total_steps=self.total_steps
)
if self.is_main_process:
logging.info("Using scheduler: %s", self.lr_scheduler)
###################################################################
# OTHER PREPARATION STEPS
# -----------------------
# - step 1: Set up Mixed Precision training (fp16) if required
# - step 2: Load optimizer stat from checkpoint if any
# - step 2: Set up DataParallel
###################################################################
# Set up fp16:
if self.fp16:
if self.is_main_process:
logging.info("Setting up `Almost FP16` Mixed Precision...")
if self.loss_scale == 0:
model, optimizer = amp.initialize(
model, optimizer, opt_level="O2", loss_scale="dynamic")
else:
model, optimizer = amp.initialize(
model, optimizer, opt_level="O2", loss_scale=self.loss_scale)
amp._amp_state.loss_scalers[0]._loss_scale = 2**20
# Load optimizer state from checkpoint
if self.resume_pretraining:
if self.is_main_process:
logging.info("Loading optimizer state from checkpoint...")
if self.phase2 or self.init_checkpoint:
keys = list(self.checkpoint['optimizer']['state'].keys())
# Override hyperparameters from previous self.checkpoint
for key in keys:
self.checkpoint['optimizer']['state'][key]['step'] = self.global_step
for i, _ in enumerate(self.checkpoint['optimizer']['param_groups']):
self.checkpoint['optimizer']['param_groups'][i]['step'] = self.global_step
self.checkpoint['optimizer']['param_groups'][i]['t_total'] = self.total_steps
self.checkpoint['optimizer']['param_groups'][i]['warmup'] = self.warmup_proportion
self.checkpoint['optimizer']['param_groups'][i]['lr'] = self.learning_rate
if self.is_main_process:
logging.info("Overwrote the following parameters with new values:")
logging.info("* step: %s", self.global_step)
logging.info("* t_total: %s", self.total_steps)
logging.info("* warmup: %s", self.warmup_proportion)
logging.info("* lr: %s", self.learning_rate)
optimizer.load_state_dict(self.checkpoint['optimizer'])
# Restore AMP master parameters
if self.fp16:
if self.is_main_process:
logging.info("Restoring AMP master parameters (optimizer)...")
optimizer._lazy_init_maybe_master_weights()
optimizer._amp_stash.lazy_init_called = True
optimizer.load_state_dict(self.checkpoint['optimizer'])
for param, saved_param in zip(amp.master_params(optimizer), self.checkpoint['master params']):
param.data.copy_(saved_param.data)
# Distribute model
if self.training_is_distributed:
if not self.allreduce_post_accumulation:
model = DistributedDataParallel(
model,
message_size=250000000,
gradient_predivide_factor=\
torch.distributed.get_world_size()
)
else:
flat_dist_call(
[param.data for param in model.parameters()],
torch.distributed.broadcast,
(0,)
)
elif self.n_gpu > 1:
model = torch.nn.DataParallel(model)
# Set the values of self.model and self.optimizer
self.model = model
self.optimizer = optimizer
def run_validation(self):
r"""Runs a validation step and returns a boolean saying if the model has improved."""
# Build a list of validation .hdf5 file paths:
files = []
for fname in os.listdir(self.hdf5_directory):
fpath = os.path.join(self.hdf5_directory, fname)
if os.path.isfile(fpath) and fname.startswith('validation.') and fname.endswith('.hdf5'):
files.append(fpath)
f_start_id = 0
files.sort()
num_files = len(files)
# Select first .hdf5 file
if \
torch.distributed.is_initialized() \
and torch.distributed.get_world_size() > num_files:
remainder = torch.distributed.get_world_size() % num_files
hdf5_fpath = files[
(
f_start_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
+ remainder * f_start_id
) % num_files
]
else:
hdf5_fpath = files[
(
f_start_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
) % num_files
]
# Set previous_file variable for next iteration
previous_file = hdf5_fpath
# Load the pre-training data from the .hdf5 file
pretraining_data = PretrainingDataset(
hdf5_fpath=hdf5_fpath,
max_masked_tokens_per_input=self.max_masked_tokens_per_input
)
validation_sampler = RandomSampler(pretraining_data) # This could be SequentialSampler
validation_dataloader = DataLoader(
pretraining_data,
sampler=validation_sampler,
batch_size=self.batch_size * self.n_gpu,
num_workers=4, pin_memory=True
)
steps = 0
average_loss = 0.0 # averaged loss every self.log_freq steps
# Use model in `evaluation mode`
with torch.no_grad():
self.model.eval()
if self.is_main_process:
logging.info("*************************")
logging.info("** Evaluation step **")
logging.info("*************************")
# Loop over the rest of pre-training data files
pool = ProcessPoolExecutor(1)
if len(files) == 1:
f_start_id = -1
for f_id in range(f_start_id + 1, 1 + len(files)//torch.distributed.get_world_size()):
# Submit creation of next DataLoader
if torch.distributed.get_world_size() > num_files:
hdf5_fpath = files[
(
f_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
+ remainder * f_id
) % num_files
]
else:
hdf5_fpath = files[
(
f_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
) % num_files
]
if self.is_main_process:
logging.info(
"Local rank: %s | File n° %s: %s",
self.local_rank, f_id, os.path.basename(previous_file)
)
previous_file = hdf5_fpath
dataset_future = pool.submit(
create_pretraining_dataloader,
hdf5_fpath,
self.max_masked_tokens_per_input,
self.batch_size * self.n_gpu,
)
# Iterate over batches (w/ progress bar for main process)
validation_batches = tqdm(
validation_dataloader,
desc="Computing loss on the validation set..."
) if self.is_main_process else validation_dataloader
for batch in validation_batches:
steps += 1
(
input_ids,
segment_ids,
input_mask,
masked_lm_labels,
next_sentence_labels
) = [tensor.to(self.device) for tensor in batch]
# Forward Pass
model_output = self.model(
input_ids=input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
labels=masked_lm_labels,
next_sentence_label=next_sentence_labels)
loss = model_output['loss']
if self.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
divisor = self.num_accumulation_steps
if self.num_accumulation_steps > 1:
if not self.allreduce_post_accumulation:
# this division was merged into predivision
loss = loss / self.num_accumulation_steps
divisor = 1.0
# Update average
average_loss += loss.item()
# Move to next file after using up all batches of current file
del validation_dataloader
validation_dataloader, hdf5_fpath = \
dataset_future.result(timeout=None)
del validation_dataloader
num_steps = max(1, int(steps / self.num_accumulation_steps))
average_loss = torch.tensor(average_loss, dtype=torch.float32).cuda()
average_loss = average_loss / (num_steps * divisor)
if torch.distributed.is_initialized():
average_loss /= torch.distributed.get_world_size()
torch.distributed.all_reduce(average_loss)
# Check if model has improved
validation_loss = average_loss.item()
model_has_improved = False
if validation_loss < self.best_validation_loss:
model_has_improved = True
self.best_validation_loss = validation_loss
# Log
if self.is_main_process:
logging.info(
"\nTotal Validation Steps: %s | Validation Loss = %.3f",
num_steps, validation_loss
)
self.tensorboard_writer.add_scalar(
"Avg. validation loss", validation_loss,
global_step=self.global_step
)
# NOTE: /!\ Put model back in `training mode`
self.model.train()
return model_has_improved
def run_pretraining(self):
r"""Runs the pre-training process."""
if self.is_main_process:
logging.info("*********************************")
logging.info("*** Starting pre-training ***")
logging.info("*********************************")
logging.info("Training on GPU: %s", torch.cuda.get_device_name(0))
logging.info("Target batch size: %s", self.target_batch_size)
logging.info("Number of accumulation steps: %s", self.num_accumulation_steps)
logging.info("Actual batch size: %s", self.batch_size)
self.model.train()
self.most_recent_ckpts_paths = []
average_loss = 0.0 # averaged loss every self.log_freq steps
epoch = 0
training_steps = 0
pool = ProcessPoolExecutor(1)
if self.is_main_process:
tensorboard_log_fpath = os.path.join(
WORKDIR,
'.tensorboard_logs',
self.tensorboard_id,
self.start_datetime.strftime("%d-%m-%Y_%H-%M-%S")
)
logging.info(
"Writing TensorBoard logs in: %s",
tensorboard_log_fpath.replace(WORKDIR, '$WORKDIR'))
self.tensorboard_writer = SummaryWriter(tensorboard_log_fpath)
# NOTE: Infinite loop over epochs, termination is handled via iteration count
while True:
# If beginning of pre-training: read files from hdf5_directory and shuffle
if (not self.resume_pretraining) or (epoch > 0) \
or (self.phase2 and self.global_step < 1) or self.init_checkpoint:
files = []
for fname in os.listdir(self.hdf5_directory):
fpath = os.path.join(self.hdf5_directory, fname)
if os.path.isfile(fpath) and fname.startswith('training.') and fname.endswith('.hdf5'):
files.append(fpath)
f_start_id = 0
files.sort()
random.Random(self.random_seed + epoch).shuffle(files)
# Else: get id of next file
else:
f_start_id = self.checkpoint['files'][0]
files = self.checkpoint['files'][1:]
self.resume_pretraining = False
num_files = len(files)
# Get the current process hdf5 file
# and handle case where there are more processes than files left:
if \
torch.distributed.is_initialized() \
and torch.distributed.get_world_size() > num_files:
remainder = torch.distributed.get_world_size() % num_files
hdf5_fpath = files[
(
f_start_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
+ remainder * f_start_id
) % num_files
]
else:
hdf5_fpath = files[
(
f_start_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
) % num_files
]
# Set previous_file variable for next iteration
previous_file = hdf5_fpath
# Load the pre-training data from the .hdf5 file
pretraining_data = PretrainingDataset(
hdf5_fpath=hdf5_fpath,
max_masked_tokens_per_input=self.max_masked_tokens_per_input
)
train_sampler = RandomSampler(pretraining_data)
train_dataloader = DataLoader(
pretraining_data,
sampler=train_sampler,
batch_size=self.batch_size * self.n_gpu,
num_workers=4, pin_memory=True
)
overflow_buf = None
if self.allreduce_post_accumulation:
overflow_buf = torch.cuda.IntTensor([0])
# Loop over the rest of pre-training data files
if len(files) == 1:
f_start_id = -1
for f_id in range(f_start_id + 1, len(files)):
# Submit creation of next DataLoader
if torch.distributed.get_world_size() > num_files:
hdf5_fpath = files[
(
f_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
+ remainder * f_id
) % num_files
]
else:
hdf5_fpath = files[
(
f_id * torch.distributed.get_world_size()
+ torch.distributed.get_rank()
) % num_files
]
if self.is_main_process:
logging.info(
"Local rank: %s | File n° %s: %s",
self.local_rank, f_id, os.path.basename(previous_file)
)
previous_file = hdf5_fpath
dataset_future = pool.submit(
create_pretraining_dataloader,
hdf5_fpath,
self.max_masked_tokens_per_input,
self.batch_size * self.n_gpu,
)
# Iterate over batches (w/ progress bar for main process)
training_batches = tqdm(
train_dataloader,
desc="Pre-training..."