forked from NVIDIA/Megatron-LM
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pretrain_bert.py
executable file
·585 lines (472 loc) · 20.1 KB
/
pretrain_bert.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
# coding=utf-8
# Copyright (c) 2019, 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.
"""Pretrain BERT"""
import os
import random
import numpy as np
import torch
import time
from arguments import get_args
from configure_data import configure_data
from fp16 import FP16_Module
from fp16 import FP16_Optimizer
from learning_rates import AnnealingLR
from model import BertModel
from model import get_params_for_weight_decay_optimization
from model import DistributedDataParallel as DDP
from optim import Adam
from utils import Timers
from utils import save_checkpoint
from utils import load_checkpoint
# global variables
from collections import OrderedDict
global_timeit_dict = OrderedDict()
global_example_count = 0
global_token_count = 0
event_writer = None
logdir = None
from tensorboardX import SummaryWriter
def log_tb(tag, val):
"""Log value to tensorboard (relies on global_example_count rather than step count to give comparable graphs across
batch sizes)"""
global global_token_count, event_writer
event_writer.add_scalar(tag, val, global_token_count)
def current_timestamp() -> str:
# timestamp format like 2019-04-15_11-29-51
current_seconds = time.time()
# correct to local timezone (PDT) if running on AWS (which is UTC)
import datetime
from pytz import reference
localtime = reference.LocalTimezone()
today = datetime.datetime.now()
timezone = localtime.tzname(today)
# TODO(y): use pytz for proper timezone conversion instead of -=
if timezone == 'UTC':
current_seconds -= 7 * 3600
else:
assert timezone == 'PDT'
time_str = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(current_seconds))
return time_str
class timeit:
"""Decorator to measure length of time spent in the block in millis and log
it to TensorBoard."""
def __init__(self, tag=""):
self.tag = tag
def __enter__(self):
self.start = time.perf_counter()
return self
def __exit__(self, *args):
self.end = time.perf_counter()
interval_ms = 1000 * (self.end - self.start)
global_timeit_dict.setdefault(self.tag, []).append(interval_ms)
newtag = 'times/' + self.tag
log_tb(newtag, interval_ms)
def get_model(tokenizer, args):
"""Build the model."""
print('building BERT model ...')
model = BertModel(tokenizer, args)
num_of_params = sum([p.nelement() for p in model.parameters()])
print(' > number of parameters: {}'.format(
num_of_params), flush=True)
print(model)
log_tb('sizes/params', num_of_params)
# GPU allocation.
model.cuda(torch.cuda.current_device())
# Fp16 conversion.
if args.fp16:
model = FP16_Module(model)
if args.fp32_embedding:
model.module.model.bert.embeddings.word_embeddings.float()
model.module.model.bert.embeddings.position_embeddings.float()
model.module.model.bert.embeddings.token_type_embeddings.float()
if args.fp32_tokentypes:
model.module.model.bert.embeddings.token_type_embeddings.float()
if args.fp32_layernorm:
for name, _module in model.named_modules():
if 'LayerNorm' in name:
_module.float()
# Wrap model for distributed training.
if args.world_size > 1:
model = DDP(model)
return model
def get_optimizer(model, args):
"""Set up the optimizer."""
# Build parameter groups (weight decay and non-decay).
while isinstance(model, (DDP, FP16_Module)):
model = model.module
layers = model.model.bert.encoder.layer
pooler = model.model.bert.pooler
lmheads = model.model.cls.predictions
nspheads = model.model.cls.seq_relationship
embeddings = model.model.bert.embeddings
param_groups = []
param_groups += list(get_params_for_weight_decay_optimization(layers))
param_groups += list(get_params_for_weight_decay_optimization(pooler))
param_groups += list(get_params_for_weight_decay_optimization(nspheads))
param_groups += list(get_params_for_weight_decay_optimization(embeddings))
param_groups += list(get_params_for_weight_decay_optimization(
lmheads.transform))
param_groups[1]['params'].append(lmheads.bias)
# Use Adam.
optimizer = Adam(param_groups,
lr=args.lr, weight_decay=args.weight_decay)
# Wrap into fp16 optimizer.
if args.fp16:
optimizer = FP16_Optimizer(optimizer,
static_loss_scale=args.loss_scale,
dynamic_loss_scale=args.dynamic_loss_scale,
dynamic_loss_args={
'scale_window': args.loss_scale_window,
'min_scale':args.min_scale,
'delayed_shift': args.hysteresis})
return optimizer
def get_learning_rate_scheduler(optimizer, args):
"""Build the learning rate scheduler."""
# Add linear learning rate scheduler.
if args.lr_decay_iters is not None:
num_iters = args.lr_decay_iters
else:
num_iters = args.train_iters * args.epochs
init_step = -1
warmup_iter = args.warmup * num_iters
lr_scheduler = AnnealingLR(optimizer,
start_lr=args.lr,
warmup_iter=warmup_iter,
num_iters=num_iters,
decay_style=args.lr_decay_style,
last_iter=init_step)
return lr_scheduler
def setup_model_and_optimizer(args, tokenizer):
"""Setup model and optimizer."""
model = get_model(tokenizer, args)
optimizer = get_optimizer(model, args)
lr_scheduler = get_learning_rate_scheduler(optimizer, args)
criterion = torch.nn.CrossEntropyLoss(reduce=False, ignore_index=-1)
if args.load is not None:
epoch, i, total_iters = load_checkpoint(model, optimizer,
lr_scheduler, args)
if args.resume_dataloader:
args.epoch = epoch
args.mid_epoch_iters = i
args.total_iters = total_iters
return model, optimizer, lr_scheduler, criterion
def get_batch(data):
''' get_batch subdivides the source data into chunks of
length args.seq_length. If source is equal to the example
output of the data loading example, with a seq_length limit
of 2, we'd get the following two Variables for i = 0:
┌ a g m s ┐ ┌ b h n t ┐
└ b h n t ┘ └ c i o u ┘
Note that despite the name of the function, the subdivison of data is not
done along the batch dimension (i.e. dimension 1), since that was handled
by the data loader. The chunks are along dimension 0, corresponding
to the seq_len dimension in the LSTM. A Variable representing an appropriate
shard reset mask of the same dimensions is also returned.
'''
tokens = torch.autograd.Variable(data['text'].long())
types = torch.autograd.Variable(data['types'].long())
next_sentence = torch.autograd.Variable(data['is_random'].long())
loss_mask = torch.autograd.Variable(data['mask'].float())
lm_labels = torch.autograd.Variable(data['mask_labels'].long())
padding_mask = torch.autograd.Variable(data['pad_mask'].byte())
# Move to cuda
tokens = tokens.cuda()
types = types.cuda()
next_sentence = next_sentence.cuda()
loss_mask = loss_mask.cuda()
lm_labels = lm_labels.cuda()
padding_mask = padding_mask.cuda()
return tokens, types, next_sentence, loss_mask, lm_labels, padding_mask
def forward_step(data, model, criterion, args):
"""Forward step."""
# Get the batch.
tokens, types, next_sentence, loss_mask, lm_labels, \
padding_mask = get_batch(data)
# Forward model.
output, nsp = model(tokens, types, 1-padding_mask,
checkpoint_activations=args.checkpoint_activations)
nsp_loss = criterion(nsp.view(-1, 2).contiguous().float(),
next_sentence.view(-1).contiguous()).mean()
losses = criterion(output.view(-1, args.data_size).contiguous().float(),
lm_labels.contiguous().view(-1).contiguous())
loss_mask = loss_mask.contiguous()
loss_mask = loss_mask.view(-1)
lm_loss = torch.sum(
losses * loss_mask.view(-1).float()) / loss_mask.sum()
return lm_loss, nsp_loss
def backward_step(optimizer, model, lm_loss, nsp_loss, args):
"""Backward step."""
# Total loss.
loss = lm_loss + nsp_loss
# Backward pass.
optimizer.zero_grad()
if args.fp16:
optimizer.backward(loss, update_master_grads=False)
else:
loss.backward()
# Reduce across processes.
lm_loss_reduced = lm_loss
nsp_loss_reduced = nsp_loss
if args.world_size > 1:
reduced_losses = torch.cat((lm_loss.view(1), nsp_loss.view(1)))
torch.distributed.all_reduce(reduced_losses.data)
reduced_losses.data = reduced_losses.data / args.world_size
model.allreduce_params(reduce_after=False,
fp32_allreduce=args.fp32_allreduce)
lm_loss_reduced = reduced_losses[0]
nsp_loss_reduced = reduced_losses[1]
# Update master gradients.
if args.fp16:
optimizer.update_master_grads()
# Clipping gradients helps prevent the exploding gradient.
if args.clip_grad > 0:
if not args.fp16:
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip_grad)
else:
optimizer.clip_master_grads(args.clip_grad)
return lm_loss_reduced, nsp_loss_reduced
tokens_in_batch = 1
def train_step(input_data, model, criterion, optimizer, lr_scheduler, args):
"""Single training step."""
global tokens_in_batch
# Forward model for one step.
# import pdb; pdb.set_trace()
data_batch = input_data['text']
tokens_in_batch = data_batch.shape[0]*data_batch.shape[1]
lm_loss, nsp_loss = forward_step(input_data, model, criterion, args)
# Calculate gradients, reduce across processes, and clip.
lm_loss_reduced, nsp_loss_reduced = backward_step(optimizer, model, lm_loss,
nsp_loss, args)
# Update parameters.
optimizer.step()
# Update learning rate.
skipped_iter = 0
if not (args.fp16 and optimizer.overflow):
lr_scheduler.step()
else:
skipped_iter = 1
return lm_loss_reduced, nsp_loss_reduced, skipped_iter
def train_epoch(epoch, model, optimizer, train_data,
lr_scheduler, criterion, timers, args):
"""Train one full epoch."""
# Turn on training mode which enables dropout.
model.train()
global global_token_count
# Tracking loss.
total_lm_loss = 0.0
total_nsp_loss = 0.0
# Iterations.
max_iters = args.train_iters
iteration = 0
skipped_iters = 0
if args.resume_dataloader:
iteration = args.mid_epoch_iters
args.resume_dataloader = False
# Data iterator.
data_iterator = iter(train_data)
timers('interval time').start()
while iteration < max_iters:
start_time = time.time()
lm_loss, nsp_loss, skipped_iter = train_step(next(data_iterator),
model,
criterion,
optimizer,
lr_scheduler,
args)
end_time = time.time()
elapsed_time = (end_time - start_time)
log_tb('times/step', 1000*elapsed_time)
log_tb('times/tokens_per_sec', tokens_in_batch/elapsed_time)
log_tb('lr', optimizer.param_groups[0]['lr'])
# log_tb('loss_scale', optimizer.loss_scale)
global_token_count += tokens_in_batch
skipped_iters += skipped_iter
iteration += 1
# Update losses.
total_lm_loss += lm_loss.data.detach().float()
total_nsp_loss += nsp_loss.data.detach().float()
# Logging.
if iteration % args.log_interval == 0:
learning_rate = optimizer.param_groups[0]['lr']
avg_nsp_loss = total_nsp_loss.item() / args.log_interval
avg_lm_loss = total_lm_loss.item() / args.log_interval
elapsed_time = timers('interval time').elapsed()
log_string = ' epoch{:2d} |'.format(epoch)
log_tb('loss_lm', avg_lm_loss)
log_tb('loss_nsp', avg_nsp_loss)
log_tb('epoch', epoch)
log_string += ' iteration {:8d}/{:8d} |'.format(iteration,
max_iters)
log_string += ' elapsed time per iteration (ms): {:.1f} |'.format(
elapsed_time * 1000.0 / args.log_interval)
log_string += ' learning rate {:.3E} |'.format(learning_rate)
log_string += ' lm loss {:.3E} |'.format(avg_lm_loss)
log_string += ' nsp loss {:.3E} |'.format(avg_nsp_loss)
if args.fp16:
log_string += ' loss scale {:.1f} |'.format(
optimizer.loss_scale)
print(log_string, flush=True)
total_nsp_loss = 0.0
total_lm_loss = 0.0
# Checkpointing
if args.save and args.save_iters and iteration % args.save_iters == 0:
total_iters = args.train_iters * (epoch-1) + iteration
model_suffix = 'model/%d.pt' % (total_iters)
save_checkpoint(model_suffix, epoch, iteration, model, optimizer,
lr_scheduler, args)
return iteration, skipped_iters
def evaluate(data_source, model, criterion, args):
"""Evaluation."""
# Turn on evaluation mode which disables dropout.
model.eval()
total_lm_loss = 0
total_nsp_loss = 0
max_iters = args.eval_iters
with torch.no_grad():
data_iterator = iter(data_source)
iteration = 0
while iteration < max_iters:
# Forward evaluation.
lm_loss, nsp_loss = forward_step(next(data_iterator), model,
criterion, args)
# Reduce across processes.
if isinstance(model, DDP):
reduced_losses = torch.cat((lm_loss.view(1), nsp_loss.view(1)))
torch.distributed.all_reduce(reduced_losses.data)
reduced_losses.data = reduced_losses.data/args.world_size
lm_loss = reduced_losses[0]
nsp_loss = reduced_losses[1]
total_lm_loss += lm_loss.data.detach().float().item()
total_nsp_loss += nsp_loss.data.detach().float().item()
iteration += 1
# Move model back to the train mode.
model.train()
total_lm_loss /= max_iters
total_nsp_loss /= max_iters
return total_lm_loss, total_nsp_loss
def initialize_distributed(args):
"""Initialize torch.distributed."""
# Manually set the device ids.
device = args.rank % torch.cuda.device_count()
if args.local_rank is not None:
device = args.local_rank
torch.cuda.set_device(device)
# Call the init process
if args.world_size > 1:
init_method = 'tcp://'
master_ip = os.getenv('MASTER_ADDR', 'localhost')
master_port = os.getenv('MASTER_PORT', '6000')
init_method += master_ip + ':' + master_port
torch.distributed.init_process_group(
backend=args.distributed_backend,
world_size=args.world_size, rank=args.rank,
init_method=init_method)
def set_random_seed(seed):
"""Set random seed for reproducability."""
if seed is not None and seed > 0:
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
def main():
"""Main training program."""
global global_example_count, global_token_count, event_writer, logdir, train_step, train_loss, best_val_loss, eval_start_time, log_start_time, epoch
global_token_count = 0
# Arguments.
args = get_args()
# global global_example_count, global_token_count, event_writer, logdir
logdir = f'{args.logdir}'
os.system(f'mkdir -p {logdir}')
event_writer = SummaryWriter(logdir)
log_tb("first", time.time())
print('Pretrain BERT model')
# Disable CuDNN.
torch.backends.cudnn.enabled = False
# Timer.
timers = Timers()
# Pytorch distributed.
initialize_distributed(args)
# Random seeds for reproducability.
set_random_seed(args.seed)
# Data stuff.
data_config = configure_data()
data_config.set_defaults(data_set_type='BERT', transpose=False)
(train_data, val_data, test_data), tokenizer = data_config.apply(args)
args.data_size = tokenizer.num_tokens
# Model, optimizer, and learning rate.
model, optimizer, lr_scheduler, criterion = setup_model_and_optimizer(
args, tokenizer)
# At any point you can hit Ctrl + C to break out of training early.
try:
total_iters = 0
skipped_iters = 0
start_epoch = 1
best_val_loss = float('inf')
# Resume data loader if necessary.
if args.resume_dataloader:
start_epoch = args.epoch
total_iters = args.total_iters
train_data.batch_sampler.start_iter = total_iters % len(train_data)
# For all epochs.
for epoch in range(start_epoch, args.epochs+1):
timers('epoch time').start()
iteration, skipped = train_epoch(epoch, model, optimizer,
train_data, lr_scheduler,
criterion, timers, args)
elapsed_time = timers('epoch time').elapsed()
total_iters += iteration
skipped_iters += skipped
lm_loss, nsp_loss = evaluate(val_data, model, criterion, args)
val_loss = lm_loss + nsp_loss
print('-' * 100)
print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:.4E} | '
'valid LM Loss {:.4E} | valid NSP Loss {:.4E}'.format(
epoch, elapsed_time, val_loss, lm_loss, nsp_loss))
print('-' * 100)
if val_loss < best_val_loss:
best_val_loss = val_loss
if args.save:
best_path = 'best/model.pt'
print('saving best model to:',
os.path.join(args.save, best_path))
save_checkpoint(best_path, epoch+1, total_iters, model,
optimizer, lr_scheduler, args)
except KeyboardInterrupt:
print('-' * 100)
print('Exiting from training early')
if args.save:
cur_path = 'current/model.pt'
print('saving current model to:',
os.path.join(args.save, cur_path))
save_checkpoint(cur_path, epoch, total_iters, model, optimizer,
lr_scheduler, args)
exit()
if args.save:
final_path = 'final/model.pt'
print('saving final model to:', os.path.join(args.save, final_path))
save_checkpoint(final_path, args.epochs, total_iters, model, optimizer,
lr_scheduler, args)
if test_data is not None:
# Run on test data.
print('entering test')
lm_loss, nsp_loss = evaluate(test_data, model, criterion, args)
test_loss = lm_loss + nsp_loss
print('=' * 100)
print('| End of training | test loss {:5.4f} | valid LM Loss {:.4E} |'
' valid NSP Loss {:.4E}'.format(test_loss, lm_loss, nsp_loss))
print('=' * 100)
if __name__ == "__main__":
main()
# test