forked from graphcore/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert.py
1149 lines (944 loc) · 45.5 KB
/
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
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
# Copyright 2019 Graphcore Ltd.
import time
import os
import sys
import math
import ctypes
import random
import datetime
from functools import reduce
from collections import deque
from collections import defaultdict
from itertools import chain
import logging
import socket
import popart
import numpy as np
from torch.utils.tensorboard import SummaryWriter
from bert_model import ExecutionMode, get_model, BertConfig
from bert_data import get_pretraining_dataset, get_squad_dataset
from bert_tf_loader import load_initializers_from_tf
from bert_optimizer import ScheduledOptimizerFactory
import utils
logger = logging.getLogger('BERT')
so_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"custom_ops.so")
if os.path.exists(so_path):
ctypes.cdll.LoadLibrary(so_path)
else:
logger.warning("Could not find custom_ops.so. Execute `make` before running this script.")
def set_library_seeds(seed):
np.random.seed(seed)
random.seed(seed)
def bert_config_from_args(args):
return BertConfig(**{k: getattr(args, k)
for k in BertConfig._fields if hasattr(args, k)})
def bert_add_embedding_inputs(args, model, sequence_info):
if args.host_embedding == "NONE":
indices = model.builder.addInputTensor(sequence_info, "indices")
positions = model.builder.addInputTensor(sequence_info, "positions")
else: # "ALL", "WORD", "MERGE"
expanded_sequence_info = popart.TensorInfo(
"FLOAT16", [args.batch_size * args.sequence_length, args.hidden_size])
indices = model.builder.addInputTensor(
expanded_sequence_info, "indices_expanded")
if args.host_embedding == "ALL":
positions = model.builder.addInputTensor(
expanded_sequence_info, "pos_expanded")
else:
positions = model.builder.addInputTensor(sequence_info, "positions")
return indices, positions
def bert_add_inputs(args, model):
sequence_info = popart.TensorInfo(
"UINT32", [args.batch_size * args.sequence_length])
indices, positions = bert_add_embedding_inputs(args, model, sequence_info)
segments = model.builder.addInputTensor(sequence_info, "segments")
labels = []
masks = []
mask_info = popart.TensorInfo("UINT32", [args.batch_size, 1])
if args.task == "PRETRAINING":
masks.append(
model.builder.addInputTensor(mask_info, "mask_tokens_mask_idx"))
masks.append(
model.builder.addInputTensor(mask_info, "sequence_mask_idx"))
mlm_info = popart.TensorInfo(
"UINT32", [args.batch_size, args.mask_tokens])
labels.append(model.builder.addInputTensor(mlm_info, "mask_labels"))
nsp_info = popart.TensorInfo(
"UINT32", [args.batch_size])
labels.append(model.builder.addInputTensor(nsp_info, "nsp_labels"))
elif args.task == "SQUAD":
masks.append(model.builder.addInputTensor(mask_info, "seq_pad_idx"))
if not args.inference:
labels_info = popart.TensorInfo(
"UINT32", [args.batch_size])
labels.append(model.builder.addInputTensor(
labels_info, "start_labels"))
labels.append(model.builder.addInputTensor(
labels_info, "end_labels"))
return indices, positions, segments, masks, labels
def bert_logits_graph(model, indices, positions, segments, masks, mode):
if mode == ExecutionMode.PHASED:
logits = model(indices, positions, segments, masks)
else:
logits = model.build_graph(indices, positions, segments, masks)
return logits
def bert_perplexity_graph(model, logits, labels):
with model.mlm_scope:
mlm_probs = model.builder.aiOnnx.softmax(
[logits[0]], axis=2, debugPrefix="Softmax")
losses = bert_loss_graph(model, [mlm_probs], [labels[0]])
# Due to limitations on the way loss is handled with Popart, we'll anchor the loss and calculate
# the perplexity on the host in the stats function.
return losses
def bert_infer_graph(model, logits, include_probs=True):
# NOTE: include_probs added as we don't need to calculate the softmax if we only care about accuracy and not
# about loss
probs = None
if model.config.task == "SQUAD":
if model.config.execution_mode == ExecutionMode.PHASED:
with model.scope_provider(model.builder, model.squad_scope):
predictions = list(
model.builder.aiOnnx.argmax([logit],
axis=1,
keepdims=0,
debugPrefix=f"{logit}/ArgMax")
for logit in logits)
if include_probs:
probs = list(
model.builder.aiOnnx.softmax(
[logit], axis=1, debugPrefix=f"{logit}/Softmax")
for logit in logits)
for prob in probs:
model.builder.setInplacePreferences(
prob, {"SoftmaxInplace": -1})
else:
with model.squad_scope:
predictions = list(
model.builder.aiOnnx.argmax([logit],
axis=1,
keepdims=0,
debugPrefix=f"{logit}/ArgMax")
for logit in logits)
probs = list(
model.builder.aiOnnx.softmax(
[logit], axis=1, debugPrefix=f"{logit}/Softmax")
for logit in logits)
for prob in probs:
model.builder.setInplacePreferences(
prob, {"SoftmaxInplace": -1})
elif model.config.task == "PRETRAINING":
if model.config.execution_mode == ExecutionMode.PHASED:
with model.scope_provider(model.builder, model.nsp_scope):
nsp_predictions = model.builder.aiOnnx.argmax(
[logits[1]], axis=1, keepdims=0, debugPrefix="ArgMax")
if include_probs:
nsp_probs = model.builder.aiOnnx.softmax([logits[1]],
axis=1,
debugPrefix="Softmax")
with model.scope_provider(model.builder, model.mlm_scope):
mlm_predictions = model.builder.aiOnnx.argmax(
[logits[0]], axis=2, keepdims=0, debugPrefix="ArgMax")
if include_probs:
mlm_probs = model.builder.aiOnnx.softmax([logits[0]],
axis=2,
debugPrefix="Softmax")
else:
with model.nsp_scope:
nsp_predictions = model.builder.aiOnnx.argmax(
[logits[1]], axis=1, keepdims=0, debugPrefix="ArgMax")
if include_probs:
nsp_probs = model.builder.aiOnnx.softmax([logits[1]],
axis=1,
debugPrefix="Softmax")
with model.mlm_scope:
mlm_predictions = model.builder.aiOnnx.argmax(
[logits[0]], axis=2, keepdims=0, debugPrefix="ArgMax")
if include_probs:
mlm_probs = model.builder.aiOnnx.softmax([logits[0]],
axis=2,
debugPrefix="Softmax")
predictions = [mlm_predictions, nsp_predictions]
if include_probs:
probs = [mlm_probs, nsp_probs]
return predictions, probs
def bert_loss_graph(model, probs, labels):
def loss(prob, label):
if model.config.execution_mode == ExecutionMode.PHASED:
if model.config.task == "SQUAD":
with model.scope_provider(model.builder, model.squad_scope):
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
debugPrefix=f"{label}/loss")
elif 'nsp' in label:
with model.scope_provider(model.builder, model.nsp_scope):
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
ignoreIndex=2,
debugPrefix=f"{label}/loss")
else:
with model.scope_provider(model.builder, model.mlm_scope):
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
ignoreIndex=0,
debugPrefix=f"{label}/loss")
else:
if model.config.task == "SQUAD":
with model.squad_scope:
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
debugPrefix=f"{label}/loss")
elif 'nsp' in label:
with model.nsp_scope:
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
ignoreIndex=2,
debugPrefix=f"{label}/loss")
else:
with model.mlm_scope:
nllloss = model.builder.aiGraphcore.nllloss(
[prob, label],
reduction=popart.ReductionType.NoReduction,
ignoreIndex=0,
debugPrefix=f"{label}/loss")
return nllloss
return [loss(*p_l) for p_l in zip(probs, labels)]
def bert_add_logit_outputs(model, logits):
outputs = {}
for logit in logits:
outputs[logit] = popart.AnchorReturnType("ALL")
for out in outputs.keys():
model.builder.addOutputTensor(out)
return outputs
def bert_add_validation_outputs(model, predictions, losses):
outputs = {}
for pred in predictions:
outputs[pred] = popart.AnchorReturnType("ALL")
for loss in losses:
outputs[loss] = popart.AnchorReturnType("ALL")
for out in outputs.keys():
model.builder.addOutputTensor(out)
return outputs
def bert_session_options(args, model):
engine_options = {}
options = popart.SessionOptions()
options.virtualGraphMode = popart.VirtualGraphMode.Manual
options.enableFloatingPointChecks = args.floating_point_exceptions
options.enableStochasticRounding = args.stochastic_rounding
options.enableGroupedMatmuls = False
options.enablePrefetchDatastreams = not args.low_latency_inference
options.enableOutlining = not args.no_outlining
partials_type = "half" if args.enable_half_partials else "float"
options.partialsTypeMatMuls = partials_type
options.convolutionOptions = {'partialsType': partials_type}
# Increasing the outlineThreshold prevents creating subgraphs of cheap Ops
# such as add or reshapeInplace.
# Instead only reusing ops with a highSubgraphValue such as matmul or normalisation.
options.outlineThreshold = 10.0
if args.execution_mode == "PIPELINE":
options.enablePipelining = True
options.autoRecomputation = popart.RecomputationType.Pipeline
elif args.execution_mode == "PHASED":
options.virtualGraphMode = popart.VirtualGraphMode.ExecutionPhases
options.enableOutliningCopyCostPruning = False
options.outlineThreshold = -np.inf
options.executionPhaseSettings.phases = model.total_execution_phases
options.batchSerializationSettings.factor = args.batch_serialize
options.autoRecomputation = popart.RecomputationType.Standard
options.explicitRecomputation = True
options.aliasZeroCopy = True
options.activationTensorLocationSettings.location.storage = popart.TensorStorage.OffChip
varLocation = popart.TensorLocation()
varLocation.storage = popart.TensorStorage.OffChip
varLocation.loadTileSet = popart.TileSet.IO
varLocation.storageTileSet = popart.TileSet.IO
varLocation.replicatedTensorSharding = (popart.ReplicatedTensorSharding.On
if args.replicated_weight_sharding else
popart.ReplicatedTensorSharding.Off)
options.weightTensorLocationSettings.location = varLocation
options.optimizerStateTensorLocationSettings.location = varLocation
options.accumulatorTensorLocationSettings.location = varLocation
options.numIOTiles = args.num_io_tiles
options.timeLimitScheduler = -1
options.swapLimitScheduler = -1
engine_options["target.syncReplicasIndependently"] = "false"
if args.gradient_accumulation_factor > 1:
options.enableGradientAccumulation = True
options.accumulationFactor = args.gradient_accumulation_factor
if args.replication_factor > 1:
options.enableReplicatedGraphs = True
options.replicatedGraphCount = args.replication_factor
if args.engine_cache is not None:
options.enableEngineCaching = True
options.cachePath = args.engine_cache
if args.gc_profile:
options.enableEngineCaching = False
options.reportOptions = {
"showVarStorage": "true",
"showPerIpuMemoryUsage": "true",
"showExecutionSteps": "true"
}
options.instrumentWithHardwareCycleCounter = args.report_hw_cycle_count
options.disableGradAccumulationTensorStreams = True
if args.max_copy_merge_size == -1:
logger.debug("No copy merge size limit applied")
else:
logger.warning(
f"Copy merge size limit set to {args.max_copy_merge_size}")
engine_options["opt.maxCopyMergeSize"] = str(args.max_copy_merge_size)
# Adding {"fullyConnectedPass", "TRAINING_BWD"} to some matmuls causes large
# transposes before operations.
if args.disable_fully_connected_pass:
if args.task == "SQUAD" and args.sequence_length == 384:
logger.warning(
"Fully connected pass has been disabled. This may cause SQuAD 384 12-layer to go OOM.")
options.enableFullyConnectedPass = False
if args.inference and args.engine_cache is not None and not args.variable_weights_inference:
logger.warning("Using engine cache with constant weights. Checkpoint weights will be ignored. "
"Use the `--variable-weights-inference` flag if checkpoint weights should be used.")
if args.variable_weights_inference:
options.constantWeights = False
if args.group_host_syncs:
options.groupHostSync = True
if args.internal_exchange_optimisation_target is not None:
engine_options["opt.internalExchangeOptimisationTarget"] = str(args.internal_exchange_optimisation_target)
options.engineOptions = engine_options
# Set synthetic data mode (if active)
if args.synthetic_data:
if args.synthetic_data_initializer == "zeros":
options.syntheticDataMode = popart.SyntheticDataMode.Zeros
else:
options.syntheticDataMode = popart.SyntheticDataMode.RandomNormal
logger.info(
f"Running with Synthetic Data Type '{options.syntheticDataMode}'")
return options
def bert_session_patterns(args):
patterns = popart.Patterns()
if args.task != "SQUAD":
patterns.enablePattern("DisableAttnDropoutBwdPattern", False)
if args.execution_mode == ExecutionMode.PHASED:
patterns.enablePattern("TiedGatherPattern", False)
return patterns
def calc_required_ipus(args, model):
if args.execution_mode == "PHASED":
num_ipus = 2
else:
num_ipus = model.total_ipus
num_ipus *= args.replication_factor
request_ipus = pow(2, math.ceil(math.log2(num_ipus)))
logger.info(f"Need {num_ipus} IPUs. Requesting {request_ipus}")
return request_ipus, num_ipus
def compile_graph_checked(args, session):
try:
start_time = time.time()
session.prepareDevice()
end_time = time.time()
logger.info(f"Compiled. Duration {end_time - start_time} seconds")
except popart.OutOfMemoryException as e:
utils.fetch_reports(args, session=session, exception=e)
raise e
def bert_training_session(model, args, feed, losses, device,
optimizer_factory):
options = bert_session_options(args, model)
patterns = bert_session_patterns(args)
def final_loss(losses):
if model.config.execution_mode == ExecutionMode.PHASED:
with model.scope_provider(model.builder, model.final_loss_scope):
reduced_losses = []
for loss in losses:
reduced_losses.append(
model.builder.aiOnnx.reducesum([loss], [0, 1], 0, f"Reduce{loss}"))
loss_sum = model.builder.aiOnnx.sum(reduced_losses, "FinalLoss")
else:
with model.final_loss_scope:
reduced_losses = []
for loss in losses:
reduced_losses.append(
model.builder.aiOnnx.reducesum([loss], [0, 1], 0, f"Reduce{loss}"))
loss_sum = model.builder.aiOnnx.sum(reduced_losses, "FinalLoss")
return loss_sum
loss = final_loss(losses)
proto = model.builder.getModelProto()
optimizer = optimizer_factory.create()
logger.info("Creating Session")
session = popart.TrainingSession(fnModel=proto,
loss=loss,
deviceInfo=device,
optimizer=optimizer,
dataFlow=feed,
patterns=patterns,
userOptions=options)
logger.info("Compiling Training Graph")
compile_graph_checked(args, session)
utils.fetch_reports(args, session=session)
session.weightsFromHost()
session.setRandomSeed(args.seed)
anchors = session.initAnchorArrays()
return session, anchors
def bert_inference_session(model, args, feed, device):
options = bert_session_options(args, model)
patterns = bert_session_patterns(args)
proto = model.builder.getModelProto()
logger.info("Creating Session")
session = popart.InferenceSession(fnModel=proto,
deviceInfo=device,
dataFlow=feed,
patterns=patterns,
userOptions=options)
logger.info("Compiling Inference Graph")
compile_graph_checked(args, session)
utils.fetch_reports(args, session=session)
session.weightsFromHost()
session.setRandomSeed(args.seed)
anchors = session.initAnchorArrays()
return session, anchors
def bert_writer(args):
log_name = f"{os.path.basename(args.checkpoint_dir)}."\
f"{datetime.datetime.now().isoformat()}"
log_dir = os.path.join(
args.log_dir, log_name)
writer = SummaryWriter(log_dir=log_dir)
return writer
def get_bert_dataset(model, args, inputs, embedding_dict=None, positional_dict=None, merge_both_embeddings=False):
config = model.config
shapeOf = model.builder.getTensorShape
# The inputs after the first three (ind, pos, seg) are always lists
inputs = reduce(chain, inputs[3:], inputs[:3])
tensor_shapes = [(tensorId, shapeOf(tensorId)) for tensorId in inputs]
if config.task == "PRETRAINING":
return get_pretraining_dataset(
tensor_shapes,
input_files=args.input_files,
sequence_length=config.sequence_length,
mask_tokens=config.mask_tokens,
vocab_length=config.vocab_length,
batch_size=config.batch_size,
batches_per_step=args.batches_per_step,
accumulation_factor=args.gradient_accumulation_factor,
replication_factor=args.replication_factor,
duplication_factor=args.duplication_factor,
shuffle=args.shuffle,
generated_data=args.generated_data or args.synthetic_data,
epochs_to_cache=args.epochs_to_cache,
start_data_at_epoch=args.continue_training_from_epoch)
if config.task == "SQUAD":
ds = get_squad_dataset(
tensor_shapes,
input_file=args.input_files[0],
output_dir=args.squad_results_dir,
sequence_length=config.sequence_length,
vocab_file=args.vocab_file,
vocab_length=config.vocab_length,
batch_size=config.batch_size,
batches_per_step=args.batches_per_step,
embedding_dict=embedding_dict,
positional_dict=positional_dict,
merge_both_embeddings=merge_both_embeddings,
accumulation_factor=args.gradient_accumulation_factor,
replication_factor=args.replication_factor,
shuffle=args.shuffle,
is_training=not args.inference,
overwrite_cache=args.overwrite_cache,
no_drop_remainder=args.no_drop_remainder,
evaluate_script=args.squad_evaluate_script,
generated_data=args.generated_data or args.synthetic_data,
do_lower_case=args.do_lower_case,
max_pipeline_stage=model.total_pipeline_stages if args.execution_mode == "PIPELINE" else 1,
seed=args.seed,
mpi_size=args.mpi_size,
mpi_rank=args.mpi_rank,
is_distributed= args.mpi_size > 1)
return ds
def bert_step_loss(losses, anchors, num_unmasked, padding_masks):
master_mask = num_unmasked != 0
for loss, mask in zip(losses, padding_masks):
anchors[loss][np.logical_not(mask)] = 0
combined_loss = reduce(np.add, map(lambda loss: anchors[loss], losses))
# Mask both num_losses and combined_loss to remove entries where all labels are ignored
num_unmasked = num_unmasked[master_mask]
combined_loss = combined_loss[master_mask]
# Calculate mean loss for each token
combined_loss /= num_unmasked
# Calculate mean loss for step
step_loss = np.mean(combined_loss)
return step_loss
def bert_step_accuracy(labels, anchors, predictions, num_unmasked, padding_masks):
total_correct = 0
for pred, label, mask in zip(map(lambda p: anchors[p], predictions),
labels,
padding_masks):
equal = pred.reshape(label.shape) == label
total_correct += np.sum(equal[mask])
total_attempted = np.sum(num_unmasked)
step_accuracy = total_correct / total_attempted
return step_accuracy
def bert_output_stats(labels, anchors, losses, predictions, ignore_index=None):
if ignore_index is not None:
padding_masks = [label != ignore_index for label in labels]
else:
padding_masks = [np.ones(label.shape, np.bool) for label in labels]
num_unmasked = np.sum(np.array(padding_masks, dtype=np.int8), axis=0)
# In the case of inference w/ labels, we don't care about the loss, so it can be ignored
if losses[0] is None:
step_loss = None
else:
step_loss = bert_step_loss(losses, anchors, num_unmasked, padding_masks)
step_accuracy = bert_step_accuracy(labels, anchors, predictions, num_unmasked, padding_masks)
return step_loss, step_accuracy
def bert_pretraining_stats(labels, anchors, losses, predictions):
if losses is None:
losses = [None, None]
# For pretraining inference with perplexity, we'll only have a single loss
if len(losses) == 1:
losses.append(None)
mlm_loss, mlm_acc = bert_output_stats(
[labels[0]], anchors, [losses[0]], [predictions[0]], 0)
nsp_loss, nsp_acc = bert_output_stats(
[labels[1]], anchors, [losses[1]], [predictions[1]], 2)
return [mlm_loss, nsp_loss], [mlm_acc, nsp_acc]
def save_model_and_stats(args, session, writer, step, epoch=None, step_in_filename=False):
if not args.no_model_save:
save_file = "model"
if epoch is not None:
save_file += f"_{epoch}"
if step_in_filename:
save_file += f":{step}"
save_file += '.onnx'
save_path = os.path.join(args.checkpoint_dir, save_file)
logger.info(f"Saving model to: {save_path}")
session.modelToHost(save_path)
utils.save_model_statistics(save_path, writer, step)
class Iteration:
def __init__(self, args, batches_per_step, steps_per_epoch, writer, recording_steps=None):
self.start_epoch = args.continue_training_from_epoch
self.count = self.start_epoch * steps_per_epoch
self.epoch = 0
self.epochs = args.epochs
self.epochs_per_save = args.epochs_per_save
self.steps_per_log = args.steps_per_log
self.samples_per_step = batches_per_step * \
args.gradient_accumulation_factor * args.replication_factor * args.batch_size
self.steps_per_epoch = steps_per_epoch
self.total_steps = self.steps_per_epoch * self.epochs
self.writer = writer
self.task = args.task
self.calculate_perplexity = args.inference_lm_perplexity
# This should get overridden but will ensure we can always write a scalar to TB.
self.learning_rate = 0
if recording_steps is None:
recording_steps = self.steps_per_epoch
self.durations = deque(maxlen=recording_steps)
self.cycles = deque(maxlen=recording_steps)
if self.task == "PRETRAINING":
self.mlm_losses = deque(maxlen=recording_steps)
self.nsp_losses = deque(maxlen=recording_steps)
self.mlm_accuracies = deque(maxlen=recording_steps)
self.nsp_accuracies = deque(maxlen=recording_steps)
self.stats_fn = bert_pretraining_stats
else:
self.losses = deque(maxlen=recording_steps)
self.accuracies = deque(maxlen=recording_steps)
self.stats_fn = bert_output_stats
def add_stats(self, duration, hw_cycles, *args):
self.durations.append(duration)
if hw_cycles:
self.cycles.append(hw_cycles)
loss, accuracy = self.stats_fn(*args)
self.writer.add_scalar("defaultLearningRate",
self.learning_rate,
self.count)
if self.task == "PRETRAINING":
self.mlm_losses.append(loss[0])
self.nsp_losses.append(loss[1])
self.mlm_accuracies.append(accuracy[0])
self.nsp_accuracies.append(accuracy[1])
self.writer.add_scalar("loss/MLM",
np.average(self.mlm_losses),
self.count)
self.writer.add_scalar("loss/NSP",
np.average(self.nsp_losses),
self.count)
self.writer.add_scalar("accuracy/MLM",
np.average(self.mlm_accuracies),
self.count)
self.writer.add_scalar("accuracy/NSP",
np.average(self.nsp_accuracies),
self.count)
else:
self.losses.append(loss)
self.accuracies.append(accuracy)
self.writer.add_scalar("loss",
np.average(self.losses),
self.count)
self.writer.add_scalar("accuracy",
np.average(self.accuracies),
self.count)
def add_inference_stats(self, duration, hw_cycles, labels_data, anchors, predictions, losses):
self.durations.append(duration)
if hw_cycles:
self.cycles.append(hw_cycles)
if labels_data:
loss, accuracy = self.stats_fn(labels_data, anchors, losses, predictions)
if self.task == "PRETRAINING":
self.mlm_accuracies.append(accuracy[0])
self.nsp_accuracies.append(accuracy[1])
if self.calculate_perplexity:
self.mlm_losses.append(loss[0])
else:
self.accuracies.append(accuracy)
@property
def throughput(self):
return np.divide(self.samples_per_step, self.durations)
def report_stats(self):
avg = np.average
status_string = \
f"Iteration: {self.count:6} " \
f"Epoch: {self.count/self.steps_per_epoch:6.2f}/{self.epochs} "
if self.task == "PRETRAINING":
status_string += \
f"Loss (MLM NSP): {avg(self.mlm_losses):5.3f} {avg(self.nsp_losses):5.3f} " \
f"Accuracy (MLM NSP): {avg(self.mlm_accuracies):5.3f} {avg(self.nsp_accuracies):5.3f} "
else:
status_string += \
f"Loss: {avg(self.losses):5.3f} " \
f"Accuracy: {avg(self.accuracies):5.3f} "
status_string += \
f"Learning Rate: {self.learning_rate:.5f} "
status_string += \
f"Duration: {avg(self.durations):6.4f} s " \
f"Throughput: {avg(self.throughput):6.1f} samples/s"
if self.cycles:
status_string += f" Cycles: {int(avg(self.cycles))}"
logger.info(status_string)
def report_inference_stats(self, mean_latency, min_latency, max_latency, hw_cycles):
avg = np.average
status_string = \
f"Iteration: {self.count:6} " \
f"Duration: {avg(self.durations):6.4f} s " \
f"Throughput: {avg(self.throughput):6.1f} samples/s"
if self.task == "PRETRAINING":
status_string += \
f" Accuracy (MLM NSP): {avg(self.mlm_accuracies):5.3f} {avg(self.nsp_accuracies):5.3f}"
if self.calculate_perplexity:
status_string += \
f" LM Perplexity: {np.exp(avg(self.mlm_losses)):5.3f}"
if mean_latency is not None:
status_string += f" Per-sample Latency: {mean_latency} {min_latency} {max_latency} seconds (mean min max)"
if hw_cycles is not None:
status_string += f" Cycles: {hw_cycles}"
logger.info(status_string)
def bert_process_data(args, session, labels, data, anchors,
losses, predictions, iteration: Iteration,
optimizer_factory: ScheduledOptimizerFactory):
labels_data = [data[label] for label in labels]
if not np.any([np.any(label) for label in labels_data]):
# Label may be all padding due to args.vocab_length being smaller than when the data was generated
return
stepio = popart.PyStepIO(data, anchors)
start = time.time()
session.run(stepio)
duration = time.time() - start
hw_cycles = session.getCycleCount() if args.report_hw_cycle_count else None
iteration.add_stats(duration, hw_cycles, labels_data,
anchors, losses, predictions)
if (iteration.count % iteration.steps_per_log) == 0:
iteration.report_stats()
utils.fetch_reports(args, session=session, execution=True)
# The following will only be true if:
# Learning rate mode is STEP and the current total step counter is in the schedule
# Learning rate mode is EPOCH and the current epoch has just changed to one in the schedule
if optimizer_factory.should_update(iteration):
optimizer = optimizer_factory.update_and_create(iteration)
session.updateOptimizerFromHost(optimizer)
iteration.count += 1
def get_timing_start_anchor(start_times):
# Return the ID of the first input that is sent from the host.
# Order is repeateable so we can just check the time for one entry:
return min(start_times, key=lambda k: start_times[k][-1])
def get_timing_end_anchor(end_times):
# Return the ID of the last anchor that is returned to the host.
# Order is repeateable so we can just check the time for one entry:
return max(end_times, key=lambda k: end_times[k][-1])
def create_callback_stepio(data, anchors, start_times, end_times, batches_per_step):
micro_batch_indices = defaultdict(int)
# Input callback is called when the data is needed:
def input_callback(id, is_prefetch: bool):
input_time = time.perf_counter()
start_times[id].append(input_time)
return data[id][micro_batch_indices[id]]
# Called after the input buffer has been consumed by the device:
def input_complete_callback(id):
micro_batch_indices[id] = \
(micro_batch_indices[id] + 1) % batches_per_step
return
# Output callback is called when a buffer is needed for the result:
def output_callback(id):
return anchors[id][micro_batch_indices[id]]
# Complete callback is called when the output buffer has
# been filled (result is ready to be consumed by the host):
def output_complete_callback(id):
output_time = time.perf_counter()
end_times[id].append(output_time)
micro_batch_indices[id] = \
(micro_batch_indices[id] + 1) % batches_per_step
stepio = popart.PyStepIOCallback(input_callback,
input_complete_callback,
output_callback,
output_complete_callback)
return stepio
def compute_latency(args, start_times, end_times, durations):
if start_times and args.low_latency_inference and args.task == "SQUAD":
# Compute latency stats using time between the
# two anchors most separated in time:
start_id = get_timing_start_anchor(start_times)
end_id = get_timing_end_anchor(end_times)
rtts = list(
map(lambda v: v[1] - v[0], zip(start_times[start_id], end_times[end_id])))
if len(rtts) != args.batches_per_step:
raise RuntimeError(
"Number of timings doesn't match items in the batch. Something is wrong.")
mean_latency = (sum(rtts)) / args.batches_per_step
min_latency = min(rtts)
max_latency = max(rtts)
if (logging.getLogger().isEnabledFor(logging.DEBUG)):
for i, v in enumerate(rtts):
logging.debug(f"LATENCY: {i} {v}")
else:
mean_latency = np.average(durations)
min_latency = min(durations)
max_latency = max(durations)
return mean_latency, min_latency, max_latency
def bert_process_infer_data(args, session, data, anchors,
logits, iteration: Iteration,
start_times, end_times, stepio,
labels=None, predictions=None, losses=None):
if stepio is None:
stepio = popart.PyStepIO(data, anchors)
start = time.perf_counter()
session.run(stepio)
duration = time.perf_counter() - start
hw_cycles = session.getCycleCount() if args.report_hw_cycle_count else None
labels_data = None
if labels is not None:
labels_data = [data[label] for label in labels]
if not np.any([np.any(label) for label in labels_data]):
labels_data = None
iteration.add_inference_stats(duration, hw_cycles, labels_data, anchors, predictions, losses)
mean_latency, min_latency, max_latency = compute_latency(
args, start_times, end_times, iteration.durations)
if (iteration.count % iteration.steps_per_log) == 0:
iteration.report_inference_stats(mean_latency, min_latency, max_latency, hw_cycles)
utils.fetch_reports(args, session=session, execution=True)
iteration.count += 1
if args.task == "PRETRAINING":
return None
return [anchors[logit] for logit in logits]
def bert_train_loop(args, session, writer,
dataset, labels, predictions, losses, anchors,
iteration, optimizer_factory):
losses = [loss for loss in losses]
save_model_and_stats(args, session, writer,
iteration.count, iteration.epoch)
for iteration.epoch in range(iteration.start_epoch, args.epochs):
for data in dataset:
bert_process_data(args, session, labels, data, anchors,
losses, predictions, iteration, optimizer_factory)
if args.steps_per_save > 0 and (iteration.count % args.steps_per_save) == 0:
save_model_and_stats(args, session, writer,
iteration.count, iteration.epoch, True)
if args.epochs_per_save > 0 and ((iteration.epoch + 1) % iteration.epochs_per_save) == 0:
save_model_and_stats(args, session, writer,
iteration.count, iteration.epoch + 1)
save_model_and_stats(args, session, writer, iteration.count)
def enable_realtime_scheduling(args):
if args.realtime_scheduler:
# Use a system call to enable real-time scheduling
# for the whole process:
pid = os.getpid()
logger.info(f"Enabling real-time scheduler for process: PID {pid}")
os.system(f"sudo -n chrt --rr -p 99 {pid}")
def disable_realtime_scheduling(args):
if args.realtime_scheduler:
# Use a system call to reset to default scheduling
# for the whole process:
pid = os.getpid()
logger.info(f"Disabling real-time scheduler for process: PID {pid}")
os.system(f"sudo -n chrt --other -p 0 {pid}")
def bert_infer_loop(args, session,
dataset, inputs, logits, anchors,
labels, predictions, losses,
iteration):
save_results = args.task == "SQUAD" and not (args.synthetic_data or args.generated_data)
if not losses:
losses = None
# Create the stepio once outside of the inference loop:
static_data = {}
start_times = defaultdict(list)
end_times = defaultdict(list)
if args.low_latency_inference and args.task == "SQUAD":
stepio = create_callback_stepio(static_data, anchors, start_times, end_times,
dataset.batches_per_step)
else:
stepio = None
enable_realtime_scheduling(args)
for iteration.epoch in range(args.epochs_inference):
for data in dataset:
static_data.update({t: data[t] for t in inputs})
result = bert_process_infer_data(args, session, static_data, anchors,
logits, iteration,
start_times, end_times, stepio,
labels, predictions, losses)
if result is not None and save_results and iteration.epoch == args.epochs_inference - 1:
dataset.add_results(data, result)
start_times.clear()
end_times.clear()
disable_realtime_scheduling(args)
# If SQuAD save the predictions and run the evaulation script
if save_results:
dataset.write_predictions()
def acquire_device(args, request_ipus):
if args.use_ipu_model:
model_opts = {"numIPUs": request_ipus}
if args.ipu_model_version is not None:
model_opts["ipuVersion"] = args.ipu_model_version
device = popart.DeviceManager().createIpuModelDevice(model_opts)
else:
if args.execution_mode == "PINGPONG":
sync_pattern = popart.SyncPattern.ReplicaAndLadder
elif args.execution_mode == "PIPELINE" and args.replication_factor <= 1:
sync_pattern = popart.SyncPattern.SinglePipeline
else:
sync_pattern = popart.SyncPattern.Full
device = popart.DeviceManager().acquireAvailableDevice(
request_ipus,
pattern=sync_pattern)
if device is None:
raise OSError("Failed to acquire IPU.")
logger.info(f"Acquired device: {device}")
return device
def bert_pretrained_initialisers(config, args):
if args.synthetic_data:
logger.info("Initialising from synthetic_data")
return None