forked from graphcore/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
1372 lines (1171 loc) · 66.6 KB
/
train.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 (c) 2019 Graphcore Ltd. 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.
"""
Training CNNs on Graphcore IPUs.
See the README and the --help option for more information.
"""
import tensorflow.compat.v1 as tf
import os
import re
import time
import math
import argparse
import datetime
import random
import warnings
from socket import gethostname
from collections import deque, OrderedDict, namedtuple
from contextlib import ExitStack
from functools import partial
import numpy as np
import sys
import importlib
from pathlib import Path
import validation
import log as logging
from tensorflow.python import ipu
from ipu_utils import get_config
from tensorflow.compiler.plugin.poplar.ops import gen_ipu_ops
from tensorflow.python.ipu import loops, ipu_infeed_queue, ipu_outfeed_queue, ipu_compiler
from tensorflow.python.ipu.utils import reset_ipu_seed
from tensorflow.python.ipu.ops import pipelining_ops
from tensorflow.python.ipu import horovod as hvd
from tensorflow.python.ipu.horovod import ipu_multi_replica_strategy
from ipu_optimizer import IPUOptimizer
from tensorflow.python.ipu.scopes import ipu_scope
import Datasets.data as dataset
from Datasets import imagenet_dataset
from weight_avg import average_ckpts, save_ckpt
from optimisers import make_fp32_optimiser
from Models.batch_norm import add_bn_moving_average_updates
from Models.proxy_norm import make_pn_optimiser
import popdist
import popdist.tensorflow
from Datasets import augmentations
import json
import configurations
from tensorflow.python.ipu.config import SchedulingAlgorithm
DATASET_CONSTANTS = dataset.DATASET_CONSTANTS
MLPERF_EVAL_TARGET = 75.9
GraphOps = namedtuple(
'graphOps', ['graph',
'session',
'init',
'ops',
'placeholders',
'iterator',
'outfeed',
'saver'])
pipeline_schedule_options = [str(p).split(".")[-1] for p in list(pipelining_ops.PipelineSchedule)]
scheduling_algorith_map = {
'choose-best': SchedulingAlgorithm.CHOOSE_BEST,
'clustering': SchedulingAlgorithm.CLUSTERING,
'post-order': SchedulingAlgorithm.POST_ORDER,
'look-ahead': SchedulingAlgorithm.LOOK_AHEAD,
'shortest-path': SchedulingAlgorithm.SHORTEST_PATH
}
def integer_labels_to_dense(data_dict, opts, num_classes):
"""
Function tranforms integer labels into their dense representation.
This takes into acount the data augmentations of label smoothing, mixup and cutmix
:param data_dict:
:param opts: global options
:param num_classes: number of classes for the classification problem
:return: tensor representing the target labels, of shape [batch_size, num_classes]
"""
smooth_negatives = opts["label_smoothing"] / (num_classes - 1)
smooth_positives = 1.0 - opts["label_smoothing"]
smoothed_one_hot_fn = partial(tf.one_hot, depth=num_classes, on_value=smooth_positives, off_value=smooth_negatives)
smoothed_labels = smoothed_one_hot_fn(data_dict['label'])
if opts["mixup_alpha"] > 0:
# linear mix of the one-hot labels
smoothed_mixup_labels = smoothed_one_hot_fn(data_dict['label_mixed_up'])
# mix must be broadcastable to [batch_size, n_labels]
mix = tf.cast(tf.squeeze(data_dict["mixup_coefficients"]), smoothed_labels.dtype)[:, None]
smoothed_labels = mix * smoothed_labels + (1. - mix) * smoothed_mixup_labels
if opts['cutmix_lambda'] < 1.:
smoothed_cutmix_labels = smoothed_one_hot_fn(data_dict['cutmix_label'])
cutmix_lambda = tf.cast(tf.squeeze(data_dict["cutmix_lambda"]), smoothed_labels.dtype)[:, None]
smoothed_labels = cutmix_lambda * smoothed_labels + (1. - cutmix_lambda) * smoothed_cutmix_labels
if opts['cutmix_lambda'] < 1. and opts['mixup_alpha'] > 0:
# we have 4 labels per one image, split into four parts:
# 1. the original label, multiplied by 'mix' and 'cutout_lambda'
one_hot_1 = mix * cutmix_lambda * smoothed_one_hot_fn(data_dict['label'])
# 2. the mixup label, 'label2', multiplied by 'mix' and '1 - cutout'
one_hot_2 = (1. - mix) * cutmix_lambda * smoothed_one_hot_fn(data_dict['label_mixed_up'])
# now, the image that will be pasted in with cutmix is a combination of two images itself -- with the
# mixing coefficient given in 'mix2'
mix2 = tf.cast(tf.squeeze(data_dict["mixup_coefficients_2"]), smoothed_labels.dtype)[:, None]
# 3. the foreground image of the cut-in patch
one_hot_3 = mix2 * (1. - cutmix_lambda) * smoothed_one_hot_fn(data_dict['cutmix_label'])
# 4. the background image of the cut-in patch
one_hot_4 = (1. - mix2) * (1. - cutmix_lambda) * smoothed_one_hot_fn(data_dict['cutmix_label2'])
smoothed_labels = one_hot_1 + one_hot_2 + one_hot_3 + one_hot_4
return smoothed_labels
def calculate_loss(logits, data_dict, opts):
predictions = tf.argmax(logits, 1, output_type=tf.int32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predictions, data_dict['label']), tf.float16))
if opts["label_smoothing"] > 0 or opts["mixup_alpha"] > 0 or opts['cutmix_lambda'] < 1.:
num_classes = int(logits.shape[1])
smoothed_labels = integer_labels_to_dense(data_dict, opts, num_classes=num_classes)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=smoothed_labels))
else:
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
labels=data_dict['label']))
tf.add_to_collection('losses', cross_entropy)
loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
return loss, cross_entropy, accuracy
def get_optimizer(opts, lr):
if not opts['offload_fp32_weight_copy'] and not opts.get('proxy_norm'):
# revert to basic optimizers when possible, while functionally they should be identical
# the basic optimizer functions are sometimes targeted directly for performance optimizations
SGD = tf.train.GradientDescentOptimizer
Momentum = tf.train.MomentumOptimizer
RMSProp = tf.train.RMSPropOptimizer
else:
from optimisers import SGD, Momentum, RMSProp
if opts['optimiser'] == 'LARS':
raise NotImplementedError('--offload-fp32-weight-copy and --proxy-norm are currently not compatible'
'with LARS optimizer.')
if opts['gradient_accumulation_count'] > 1 and not opts['pipeline']:
raise NotImplementedError('--offload-fp32-weight-copy and --proxy-norm are currently not compatible'
'with GradientAccumulatorV1.')
opt_kwargs = {}
if opts['optimiser'] == 'SGD':
optimizer = SGD
elif opts['optimiser'] == 'momentum':
optimizer = Momentum
opt_kwargs = {'momentum': opts['momentum']}
logging.mlperf_logging(key="OPT_NAME", value="sgd")
logging.mlperf_logging(key="SGD_OPT_MOMENTUM", value=opts['momentum'])
logging.mlperf_logging(key="SGD_OPT_WEIGHT_DECAY",
value=opts['weight_decay'])
logging.mlperf_logging(key="SGD_OPT_BASE_LEARNING_RATE",
value=opts.get("abs_learning_rate", 0))
logging.mlperf_logging(key="SGD_OPT_END_LEARNING_RATE",
value=opts.get("abs_end_learning_rate", 0))
logging.mlperf_logging(key="SGD_OPT_LEARNING_RATE_DECAY_POLY_POWER",
value=opts.get("poly_lr_decay_power", 2))
elif opts['optimiser'] == 'RMSprop':
optimizer = RMSProp
opt_kwargs = {'momentum': opts['momentum'],
'decay': opts['rmsprop_decay'],
'epsilon': opts['rmsprop_epsilon']}
elif opts['optimiser'] == 'LARS':
from lars_optimizer import LARSOptimizer
optimizer = LARSOptimizer
logging.mlperf_logging(key="OPT_NAME", value="lars")
opts['lars_skip_list'] += ['batch_norm/moving_']
opt_kwargs = {'weight_decay': opts['lars_weight_decay'],
'eeta': opts['lars_eeta'],
'epsilon': opts['lars_epsilon'],
'momentum': opts['momentum'],
'skip_list': opts['lars_skip_list']}
else:
raise ValueError("Optimizer {} not recognised".format(opts['optimiser']))
if opts.get('BN_decay'):
optimizer = add_bn_moving_average_updates(optimizer, momentum=opts.get('BN_decay'))
if opts.get('proxy_norm'):
if (opts['model'] == 'efficientnet' and not opts['use_relu']):
from tensorflow.python.ipu import nn_ops
try:
activation = nn_ops.swish
except AttributeError:
activation = tf.nn.swish
print("IPU nn_ops.swish operation not found. Falling back to tf.nn.swish .")
else:
activation = tf.nn.relu
optimizer = make_pn_optimiser(optimizer,
proxy_filter_fn=lambda name: ('proxy' in name),
activation=activation,
proxy_epsilon=opts['proxy_epsilon'],
pipeline_splits=opts['pipeline_splits'],
dtype=tf.float16 if opts["precision"].split('.')[0] == '16' else tf.float32,
weight_decay=opts['weight_decay'] * opts['lr_scale'])
if opts['offload_fp32_weight_copy']:
optimizer = make_fp32_optimiser(optimizer)
optimizer = optimizer(lr, **opt_kwargs)
wd_exclude = opts["wd_exclude"] if "wd_exclude" in opts.keys() else []
wd_exclude += ['batch_norm/moving_'] # always exclude moving averages from weight decay
# get variables to include in training
if opts["variable_filter"]:
var_list = [v for v in tf.trainable_variables() if any(s in v.name for s in opts["variable_filter"])]
else:
var_list = tf.trainable_variables()
def filter_fn(name):
return not any(s in name for s in wd_exclude)
return IPUOptimizer(optimizer,
sharded=opts["shards"] > 1 and not opts['pipeline'],
replicas=opts["total_replicas"],
gradient_accumulation_count=opts["gradient_accumulation_count"],
pipelining=opts['pipeline'],
grad_scale=opts["grad_scale"],
weight_decay=opts["weight_decay"] * opts['loss_scaling'],
weight_decay_filter_fn=filter_fn,
var_list=var_list)
def calculate_and_apply_gradients(loss, opts=None, learning_rate=None):
optimizer = get_optimizer(opts, learning_rate / opts['lr_scale'])
grads_and_vars = optimizer.compute_gradients(loss * opts['loss_scaling'])
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
return learning_rate, optimizer.apply_gradients(grads_and_vars)
def ipuside_preprocessing(data_dict, opts, training=True):
if opts['dataset'] == 'imagenet':
if opts['fused_preprocessing']:
data_dict['image'] = imagenet_dataset.fused_accelerator_side_preprocessing(
data_dict['image'], opts=opts)
else:
if opts['eight_bit_io']:
dtypes = opts['precision'].split('.')
input_dtype = tf.float16 if dtypes[0] == '16' else tf.float32
data_dict['image'] = tf.cast(
data_dict['image'], dtype=input_dtype)
if not opts['hostside_norm']:
data_dict['image'] = imagenet_dataset.accelerator_side_preprocessing(
data_dict['image'], opts=opts)
elif opts['eight_bit_io']:
dtypes = opts['precision'].split('.')
input_dtype = tf.float16 if dtypes[0] == '16' else tf.float32
data_dict['image'] = tf.cast(
data_dict['image'], dtype=input_dtype)
if not opts['hostside_image_mixing'] and training:
# apply augmentation on the accelerator
if opts['mixup_alpha'] > 0.:
data_dict = augmentations.mixup_image(data_dict)
if opts['cutmix_lambda'] < 1.:
data_dict = augmentations.cutmix(data_dict, cutmix_lambda=opts['cutmix_lambda'],
cutmix_version=opts['cutmix_version'])
def basic_training_step(data_dict, model, opts, learning_rate):
"""
A basic training step that will work on all hardware
"""
ipuside_preprocessing(data_dict, opts)
image, label = data_dict['image'], data_dict['label']
logits = model(opts, training=True, image=image)
loss, cross_entropy, accuracy = calculate_loss(logits, data_dict, opts)
learning_rate, train_op = calculate_and_apply_gradients(loss, opts, learning_rate=learning_rate)
return loss, cross_entropy, accuracy, learning_rate, train_op
def basic_pipelined_training_step(model, opts, learning_rate, infeed, outfeed, iterations_per_step=1):
def first_stage(learning_rate, data_dict, pipeline_stage=None):
ipuside_preprocessing(data_dict, opts)
image, label = data_dict['image'], data_dict['label']
outputs = [learning_rate, pipeline_stage(image), label]
# if both are applied, the ordering is important for unpacking these later
if opts['mixup_alpha'] > 0.:
outputs += [data_dict['label_mixed_up'], data_dict['mixup_coefficients']]
if opts['cutmix_lambda'] < 1.:
outputs += [data_dict['cutmix_label'], data_dict['cutmix_lambda']]
if opts['mixup_alpha'] > 0. and opts['cutmix_lambda'] < 1.:
outputs += [data_dict['cutmix_label2'], data_dict['mixup_coefficients_2']]
return outputs
def later_stage(learning_rate, x, label, *inputs, pipeline_stage=None, final_stage=False):
inputs = list(inputs)
x = pipeline_stage(x)
if not final_stage:
return [learning_rate, x, label] + inputs
else:
data_dict = {'label': label}
if opts['mixup_alpha'] > 0.:
data_dict.update({'label_mixed_up': inputs.pop(0), 'mixup_coefficients': inputs.pop(0)})
if opts['cutmix_lambda'] < 1.:
data_dict.update({'cutmix_label': inputs.pop(0), 'cutmix_lambda': inputs.pop(0)})
if opts['cutmix_lambda'] < 1. and opts['mixup_alpha'] > 0.:
data_dict.update({'cutmix_label2': inputs.pop(0), 'mixup_coefficients_2': inputs.pop(0)})
loss, cross_entropy, accuracy = calculate_loss(x, data_dict, opts)
# note: would ideally add in scaling in optimizer_function for learning_rate
return loss, cross_entropy, accuracy, learning_rate / opts["lr_scale"]
model_stages = model(opts)
computational_stages = [partial(first_stage, pipeline_stage=model_stages[0])]
computational_stages += [partial(later_stage, pipeline_stage=model_stages[idx+1],
final_stage=idx == len(model_stages)-2) for idx in range(len(model_stages) - 1)]
def optimizer_function(loss, _, __, lr):
optimizer = get_optimizer(opts, lr)
return pipelining_ops.OptimizerFunctionOutput(optimizer, loss * opts["loss_scaling"])
options = None
amps = opts['available_memory_proportion']
if amps and len(amps) > 1:
# Map values to the different pipeline stages
options = []
for i in range(len(amps) // 2):
options.append(pipelining_ops.PipelineStageOptions({"availableMemoryProportion": amps[2 * i]},
{"availableMemoryProportion": amps[2 * i + 1]}))
# Map all stages to the same device for a simple recomputation setup on a single IPU.
device_mapping = None
if opts["pipeline_schedule"] == "Sequential" and opts["shards"] == 1:
device_mapping = [0] * len(computational_stages)
return pipelining_ops.pipeline(computational_stages=computational_stages,
gradient_accumulation_count=int(opts['gradient_accumulation_count']),
repeat_count=iterations_per_step,
inputs=[learning_rate],
infeed_queue=infeed,
outfeed_queue=outfeed,
accumulate_outfeed=True,
optimizer_function=optimizer_function,
device_mapping=device_mapping,
forward_propagation_stages_poplar_options=options,
backward_propagation_stages_poplar_options=options,
pipeline_schedule=next(p for p in list(pipelining_ops.PipelineSchedule)
if opts["pipeline_schedule"] == str(p).split(".")[-1]),
offload_weight_update_variables=not opts['disable_variable_offloading'],
name="Pipeline")
def distributed_per_replica(function):
"""Run the function with the distribution strategy (if any) in a per-replica context."""
def wrapper(*arguments):
if tf.distribute.has_strategy():
strategy = tf.distribute.get_strategy()
return strategy.experimental_run_v2(function, args=arguments)
else:
return function(*arguments)
return wrapper
@distributed_per_replica
def training_step_with_infeeds_and_outfeeds(train_iterator, outfeed, model, opts, learning_rate, iterations_per_step=1):
"""
Training step that uses an infeed loop with outfeeds. This runs 'iterations_per_step' steps per session call. This leads to
significant speed ups on IPU. Not compatible with running on CPU or GPU.
"""
if opts['pipeline']:
training_step = partial(basic_pipelined_training_step,
model=model.staged_model,
opts=opts,
learning_rate=learning_rate,
infeed=train_iterator,
outfeed=outfeed,
iterations_per_step=iterations_per_step)
return ipu_compiler.compile(training_step, [])
else:
training_step = partial(basic_training_step,
model=model.Model,
opts=opts,
learning_rate=learning_rate)
def training_step_loop(data_dict, outfeed=None):
loss, cross_ent, accuracy, lr_out, apply_grads = training_step(data_dict)
outfeed = outfeed.enqueue((loss, cross_ent, accuracy, lr_out))
return outfeed, apply_grads
def compiled_fn():
return loops.repeat(iterations_per_step,
partial(training_step_loop, outfeed=outfeed),
[],
train_iterator)
return ipu_compiler.compile(compiled_fn, [])
def configure_distribution(opts, sess_config):
"""
Creates the distribution strategy, updates the given session configuration
accordingly, and starts a distributed server that allows the workers to connect.
Returns the strategy, the session target address and the updated session configuration.
"""
cluster = tf.distribute.cluster_resolver.SimpleClusterResolver(
cluster_spec=tf.train.ClusterSpec(opts['distributed_cluster']),
task_id=opts['distributed_worker_index'],
task_type="worker")
strategy = ipu.ipu_multi_worker_strategy.IPUMultiWorkerStrategy(cluster)
sess_config = strategy.update_config_proto(sess_config)
server = tf.distribute.Server(cluster.cluster_spec(),
job_name=cluster.task_type,
task_index=cluster.task_id,
protocol=cluster.rpc_layer,
config=sess_config)
return strategy, server.target, sess_config
def create_popdist_strategy():
"""
Creates a distribution strategy for use with popdist. We use the
Horovod-based IPUMultiReplicaStrategy. Horovod is used for the initial
broadcast of the weights and when reductions are requested on the host.
"""
# We add the IPU cross replica reductions explicitly in the IPUOptimizer,
# so disable them in the IPUMultiReplicaStrategy.
return ipu_multi_replica_strategy.IPUMultiReplicaStrategy(
add_ipu_cross_replica_reductions=False)
def training_graph(model, opts, iterations_per_step=1):
train_graph = tf.Graph()
sess_config = tf.ConfigProto()
sess_target = None
strategy = None
if opts['distributed_cluster']:
strategy, sess_target, sess_config = configure_distribution(opts, sess_config)
elif opts['use_popdist']:
strategy = create_popdist_strategy()
with train_graph.as_default(), ExitStack() as stack:
if strategy:
stack.enter_context(strategy.scope())
learning_rate_ph = tf.placeholder(tf.float32, shape=[])
# all data-consuming functions operate on a 'data_dict'
training_dataset = dataset.data(opts, is_training=True).map(lambda x: {'data_dict': x})
# datasets must be defined outside the ipu device scope
train_iterator = ipu_infeed_queue.IPUInfeedQueue(training_dataset,
prefetch_depth=opts['prefetch_depth'])
outfeed_queue = ipu_outfeed_queue.IPUOutfeedQueue()
with ipu_scope('/device:IPU:0'):
train = training_step_with_infeeds_and_outfeeds(train_iterator, outfeed_queue, model,
opts, learning_rate_ph, iterations_per_step)
outfeed = outfeed_queue.dequeue()
ga_factor = opts["gradient_accumulation_count"] if opts['pipeline'] else 1
if strategy:
# Take the mean of all the outputs across the distributed workers
outfeed = [strategy.reduce(tf.distribute.ReduceOp.MEAN, v)/ga_factor for v in
outfeed]
else:
outfeed = [v/ga_factor for v in outfeed]
logging.print_trainable_variables(opts)
train_saver = tf.train.Saver(max_to_keep=999999)
ipu.utils.move_variable_initialization_to_cpu(graph=None)
train_init = tf.global_variables_initializer()
if opts['use_popdist']:
broadcast_weights = []
for var in tf.global_variables():
broadcast_weights.append(var.assign(hvd.broadcast(var, root_rank=0)))
iteration_ph = tf.placeholder(dtype=tf.int32, shape=())
broadcast_iteration = hvd.broadcast(iteration_ph, root_rank=0)
else:
broadcast_weights = None
broadcast_iteration, iteration_ph = None, None
globalAMP = None
if opts["available_memory_proportion"] and len(opts["available_memory_proportion"]) == 1:
globalAMP = opts["available_memory_proportion"][0]
min_remote_tensor_size = 128
if not opts["disable_variable_offloading"]:
min_remote_tensor_size = opts["min_remote_tensor_size"]
ipu_options = get_config(ipu_id=opts["select_ipu"],
prng=not opts["no_stochastic_rounding"],
shards=opts["shards"],
number_of_replicas=opts['replicas'],
max_cross_replica_buffer_size=opts["max_cross_replica_buffer_size"],
fp_exceptions=opts["fp_exceptions"],
half_partials=opts["enable_half_partials"],
conv_dithering=opts["enable_conv_dithering"],
conv_output=opts["gather_conv_output"],
enable_recomputation=opts["enable_recomputation"],
seed=opts["seed"],
availableMemoryProportion=globalAMP,
stable_norm=opts["stable_norm"],
internalExchangeOptimisationTarget=opts[
"internal_exchange_optimisation_target"
],
num_io_tiles=opts["num_io_tiles"],
number_of_distributed_batch_norm_replicas=opts.get("BN_span", 1),
min_remote_tensor_size=min_remote_tensor_size,
nanoo=not opts["saturate_on_overflow"],
scheduling_algorithm=scheduling_algorith_map[opts['scheduling_algorithm']],
max_reduce_many_buffer_size=opts["max_reduce_many_buffer_size"],
compile_only=opts["compile_only"]
)
if opts['use_popdist']:
ipu_options = popdist.tensorflow.set_ipu_config(ipu_options, opts['shards'], configure_device=False)
if opts['on_demand']:
ipu_options.device_connection.enable_remote_buffers = True
ipu_options.device_connection.type = ipu.utils.DeviceConnectionType.ON_DEMAND
ipu_options.configure_ipu_system()
train_sess = tf.Session(graph=train_graph, config=sess_config, target=sess_target)
ops = {'train': train,
'broadcast_weights': broadcast_weights,
'broadcast_iteration': broadcast_iteration}
placeholders = {'learning_rate': learning_rate_ph,
'iteration': iteration_ph}
return GraphOps(train_graph, train_sess, train_init, ops, placeholders, train_iterator, outfeed, train_saver)
def training_step(train, e, learning_rate):
# Run Training
start = time.time()
_ = train.session.run(train.ops['train'], feed_dict={train.placeholders['learning_rate']: learning_rate})
batch_time = (time.time() - start)
if not os.environ.get('TF_POPLAR_FLAGS') or '--use_synthetic_data' not in os.environ.get('TF_POPLAR_FLAGS'):
loss, cross_ent, accuracy, lr_out = train.session.run(train.outfeed)
loss = np.mean(loss)
accuracy = 100.0 * np.mean(accuracy)
lr = lr_out.flatten()[-1]
else:
loss, accuracy, lr = 0, 0, 0
return loss, accuracy, batch_time, lr
def train_process(model, LR_Class, opts):
DATASET_CONSTANTS = dataset.reconfigure_dataset_constants(opts)
# --------------- OPTIONS --------------------
epochs = opts['epochs']
iterations_per_epoch = DATASET_CONSTANTS[opts['dataset']]['NUM_IMAGES'] // opts['total_batch_size']
logging.mlperf_logging(
key="TRAIN_SAMPLES",
value=DATASET_CONSTANTS[opts['dataset']]['NUM_IMAGES'])
logging.mlperf_logging(
key="EVAL_SAMPLES",
value=DATASET_CONSTANTS[opts['dataset']]['NUM_VALIDATION_IMAGES'])
if not opts['iterations']:
iterations = DATASET_CONSTANTS[opts['dataset']]['NUM_IMAGES']*epochs // opts['total_batch_size']
log_freq = iterations_per_epoch // opts['logs_per_epoch']
else:
iterations = opts['iterations']
log_freq = opts['log_freq']
if not opts['epochs']:
opts['epochs'] = 1.0 * iterations * opts['total_batch_size'] / DATASET_CONSTANTS[opts['dataset']]['NUM_IMAGES']
if log_freq < opts['batches_per_step']:
iterations_per_step = log_freq
else:
iterations_per_step = log_freq // int(round(log_freq / opts['batches_per_step']))
iterations_per_valid = iterations_per_epoch
if type(opts['ckpts_per_epoch']) is int:
iterations_per_ckpt = (iterations_per_epoch // opts['ckpts_per_epoch']
if opts['ckpts_per_epoch'] else np.inf)
else:
iterations_per_ckpt = (iterations_per_epoch * opts['epochs_per_ckpt']
if opts['epochs_per_ckpt'] else np.inf)
if iterations_per_ckpt == 0:
iterations_per_ckpt = 1
ckpt_offset = opts['ckpt_epochs_offset'] * iterations_per_epoch
if type(opts['syncs_per_epoch']) is int:
iterations_per_sync = (iterations_per_epoch // opts['syncs_per_epoch']
if opts['syncs_per_epoch'] else np.inf)
else:
iterations_per_sync = (iterations_per_epoch * opts['epochs_per_sync']
if opts['epochs_per_sync'] else np.inf)
LR = LR_Class(opts, iterations)
if opts['optimiser'] == 'momentum':
warmup_iterations = 0
if opts['warmup_epochs'] and int(round(opts['epochs'])):
warmup_iterations = iterations * opts['warmup_epochs'] // int(round(opts['epochs']))
decay_steps = iterations - warmup_iterations
logging.mlperf_logging(key="SGD_OPT_LEARNING_RATE_DECAY_STEPS",
value=decay_steps)
batch_accs = deque(maxlen=iterations_per_epoch // iterations_per_step)
batch_losses = deque(maxlen=iterations_per_epoch // iterations_per_step)
batch_times = deque(maxlen=iterations_per_epoch // iterations_per_step)
start_all = None
validation_points = []
ckpts = []
# -------------- BUILD TRAINING GRAPH ----------------
train_iterations = (iterations_per_step if opts['pipeline'] else
iterations_per_step * opts['gradient_accumulation_count'])
train = training_graph(model, opts, train_iterations)
logging.mlperf_logging(key="WEIGHTS_INITIALIZATION")
train.session.run(train.init)
train.session.run(train.iterator.initializer)
# -------------- SAVE AND RESTORE --------------
if opts.get('init_path'):
train.saver.restore(train.session, opts['init_path'])
if opts.get('restoring'):
if opts['distributed_worker_index'] == 0:
filename_pattern = re.compile('(.*ckpt-(\d+)).index')
patterns = map(lambda x: filename_pattern.match(x), os.listdir(opts['logs_path'])) # apply regex
filtered = filter(lambda x: x is not None, patterns) # remove patterns that don't match regex
tuples = list(map(lambda x: (int(x.group(2)), os.path.join(opts['logs_path'], x.group(1))),
filtered)) # create a tuple for easier sorting
filenames = sorted(tuples, key=lambda x: x[0]) # sort
latest_checkpoint = filenames[-1]
logging.print_to_file_and_screen(
"Restoring training from latest checkpoint: {}".format(latest_checkpoint[1]), opts)
i = int(latest_checkpoint[0])
train.saver.restore(train.session, latest_checkpoint[1])
# restore list of saved checkpoints
for j, f in filenames:
epoch = float(opts['total_batch_size'] * j) / DATASET_CONSTANTS[opts['dataset']]['NUM_IMAGES']
if j != 0:
ckpts.append((j, epoch, False, f))
_j = j - iterations_per_step
valid_this_step = (opts['validation'] and
((_j // iterations_per_valid) < ((_j + iterations_per_step) // iterations_per_valid) or
(_j == 0) or
((_j + (2 * iterations_per_step)) >= iterations)))
if valid_this_step:
validation_points.append((j, epoch, j == 0, f))
else:
i = 0
if opts['use_popdist']:
# only instance 0 accesses the disk to restore the checkpoints, so the value of the most recent iteration
# is only known to this instance. We use horovod to synchronise the iteration value across the other
# instances. The same happens to the variable values restored from the latest checkpoint.
i = train.session.run(train.ops['broadcast_iteration'],
feed_dict={train.placeholders['iteration']: i})
train.session.run(train.ops['broadcast_weights'])
else:
i = 0
if opts['ckpts_per_epoch'] and opts['distributed_worker_index'] == 0:
filepath = train.saver.save(train.session, opts['checkpoint_path'], global_step=0)
print("Saved initial checkpoint to {}".format(filepath))
# single warm up step without weight update or training
# Graph gets compiled in here
_, _, compilation_time, _ = training_step(train, 0, 0)
logging.print_to_file_and_screen(
"Compilation time: {}s.".format(compilation_time), opts)
# End to avoid any training if compile only mode
if opts['compile_only']:
print("Training graph successfully compiled")
sys.exit(0)
# ------------- TRAINING LOOP ----------------
print_format = (
"step: {step:6d}, iteration: {iteration:6d}, epoch: {epoch:6.2f}, lr: {lr:6.4g}, loss: {loss_avg:6.3f}, top-1 accuracy: {train_acc_avg:6.3f}%"
", img/sec: {img_per_sec:6.2f}, time: {it_time:8.6f}, total_time: {total_time:8.1f}")
step = 0
logging.mlperf_logging(key="INIT_STOP", log_type="stop")
start_all = time.time()
logging.mlperf_logging(key="RUN_START", log_type="start")
# Training block
logging.mlperf_logging(key="BLOCK_START", log_type="start",
metadata={"first_epoch_num": 1,
"epoch_count": opts['epochs']},
)
logging.mlperf_logging(key="EPOCH_START", log_type="start",
metadata={"epoch_num": 1})
log_epoch = 1
while i < iterations:
epoch = float(opts['total_batch_size'] * (i + iterations_per_step)) / DATASET_CONSTANTS[opts['dataset']][
'NUM_IMAGES']
if not opts['pipeline']:
step += opts['gradient_accumulation_count']
else:
step += 1
log_this_step = ((i // log_freq) < ((i + iterations_per_step) // log_freq) or
(i == 0) or
((i + (2 * iterations_per_step)) >= iterations))
ckpt_this_step = (opts['ckpts_per_epoch'] and i >= ckpt_offset - iterations_per_step and
(((i - ckpt_offset) // iterations_per_ckpt) < ((i + iterations_per_step - ckpt_offset) // iterations_per_ckpt) or
((i + iterations_per_step) >= iterations)))
# avoid early checkpointing
if ((opts['epochs_per_ckpt'] or opts['ckpts_per_epoch'] == 1) and
ckpt_this_step and round(epoch) == opts['epochs'] and
(i + iterations_per_step) < iterations):
ckpt_this_step = False
valid_this_step = (opts['validation'] and
((i // iterations_per_valid) < ((i + iterations_per_step) // iterations_per_valid) or
((i + iterations_per_step) >= iterations)))
sync_this_step = (opts['syncs_per_epoch'] and
((i // iterations_per_sync) < (
(i + iterations_per_step) // iterations_per_sync)))
# epoch transition logging
if math.floor(epoch) == log_epoch and int(round(epoch)) != int(round(opts['epochs'])):
logging.mlperf_logging(key="EPOCH_STOP", log_type="stop",
metadata={"epoch_num": log_epoch})
log_epoch = round(epoch) + 1
if i + iterations_per_step < iterations:
logging.mlperf_logging(key="EPOCH_START", log_type="start",
metadata={"epoch_num": log_epoch})
# Run Training
try:
batch_loss, batch_acc, batch_time, current_lr = training_step(train, i + 1, LR.feed_dict_lr(i))
if opts['pipeline']:
current_lr *= opts['lr_scale']
except tf.errors.OpError as e:
raise tf.errors.ResourceExhaustedError(e.node_def, e.op, e.message)
batch_time /= iterations_per_step
# Calculate Stats
batch_accs.append([batch_acc])
batch_losses.append([batch_loss])
if i != 0:
batch_times.append([batch_time])
# Print loss
if log_this_step:
train_acc = np.mean(batch_accs)
train_loss = np.mean(batch_losses)
if len(batch_times) != 0:
avg_batch_time = np.mean(batch_times)
else:
avg_batch_time = batch_time
# flush times every time it is reported
batch_times.clear()
total_time = time.time() - start_all
stats = OrderedDict([
('step', step),
('iteration', i + iterations_per_step),
('epoch', epoch),
('lr', current_lr),
('loss_batch', batch_loss),
('loss_avg', train_loss),
('train_acc_batch', batch_acc),
('train_acc_avg', train_acc),
('it_time', avg_batch_time),
('img_per_sec', opts['total_batch_size'] / avg_batch_time),
('total_time', total_time),
])
logging.print_to_file_and_screen(print_format.format(**stats), opts)
logging.write_to_csv(stats, i == 0, True, opts)
if opts['wandb'] and opts['distributed_worker_index'] == 0:
logging.log_to_wandb(stats)
# only instance 0 writes checkpoints to disk
if ckpt_this_step and (opts['distributed_worker_index'] == 0 or opts['ckpt_all_instances']):
ckpt_start = time.time()
filepath = train.saver.save(train.session, opts['checkpoint_path'], global_step=i + iterations_per_step)
ckpt_time = time.time() - ckpt_start
logging.print_to_file_and_screen(
"Saved checkpoint to {} in {}s".format(filepath, ckpt_time), opts)
ckpts.append((i + iterations_per_step, epoch, i == 0, filepath))
# synchronize popdist instances
if sync_this_step:
sync_start = time.time()
broadcast_ops = []
with train.graph.as_default():
for var in tf.global_variables():
broadcast_ops.append(
var.assign(hvd.broadcast(var, root_rank=0)))
train.session.run(broadcast_ops)
sync_time = time.time() - sync_start
logging.print_to_file_and_screen(
"Synced weights in {}s.".format(sync_time), opts)
# Eval
# only instance 0 loads checkpoints from disk during validation
if valid_this_step and opts['validation'] and opts['distributed_worker_index'] == 0:
validation_points.append((i + iterations_per_step, epoch, i <= iterations_per_valid, filepath))
i += iterations_per_step
if round(epoch) == opts['epochs'] and i >= iterations:
logging.mlperf_logging(key="EPOCH_STOP", log_type="stop",
metadata={"epoch_num": log_epoch})
logging.mlperf_logging(key="BLOCK_STOP", log_type="stop",
metadata={"first_epoch_num": 1}
)
# only instance 0 loads checkpoints from disk during weight averaging
if (opts['weight_avg_N'] or opts['weight_avg_exp']) and opts['distributed_worker_index'] == 0:
_ckpts = ckpts
final_iteration, final_epoch = _ckpts[-1][:2]
if opts['weight_avg_N']:
for N in opts['weight_avg_N']:
V = average_ckpts(
[c[3] for c in _ckpts if round(c[1], 1) >= round(final_epoch, 1) - N],
mode='mean')
filename = os.path.join(opts['checkpoint_path'], "weight_avg_N_{}".format(N))
save_ckpt(V, ckpts[-1][3], filename)
validation_points.append((final_iteration, final_epoch, False, filename))
if opts['weight_avg_exp']:
for d in opts['weight_avg_exp']:
V = average_ckpts(list(zip(*ckpts))[3], mode='exponential', decay=d)
filename = os.path.join(opts['checkpoint_path'], "weight_avg_exp_{}".format(d))
save_ckpt(V, ckpts[-1][3], filename)
validation_points.append((final_iteration, final_epoch, False, filename))
success = False
# ------------ VALIDATION ------------
if len(validation_points) > 0 and opts['validation']:
# Validation block
logging.mlperf_logging(key="BLOCK_START", log_type="start",
metadata={"first_epoch_num": 1,
"epoch_count": opts['epochs']}
)
# -------------- BUILD VALIDATION GRAPH ----------------
valid = validation.initialise_validation(model, opts)
total_samples = 0 # disable latency calculation
latency_thread = validation.LatencyThread(valid, total_samples)
# ------------ RUN VALIDATION ------------
for iteration, epoch, first_run, filepath in validation_points:
stats = validation.validation_run(valid, filepath, iteration, epoch, first_run, opts, latency_thread)
# Handle skipped case
if stats and "val_size" in stats and "val_acc" in stats:
if stats['val_acc'] > MLPERF_EVAL_TARGET:
success = True
logging.mlperf_logging(key="BLOCK_STOP", log_type="stop",
metadata={"first_epoch_num": 1}
)
logging.mlperf_logging(key="RUN_STOP",
value={"success": success},
metadata={"epoch_num": epoch,
"status": "success" if success else "aborted"})
# --------------- CLEANUP ----------------
train.session.close()
def add_main_arguments(parser, required=True):
group = parser.add_argument_group('Main')
group.add_argument('--model', type=str.lower, default='resnet', help="Choose model")
group.add_argument('--lr-schedule', default='stepped',
help="Learning rate schedule function. Default: stepped")
group.add_argument('--restore-path', type=str,
help='path to training log folder of run to restore')
group.add_argument('--help', action='store_true', help='Show help information')
return parser
def set_main_defaults(opts):
opts['training'] = True
if opts.get('restore_path'):
opts['restoring'] = True
opts['summary_str'] = "\n"
def add_training_arguments(parser):
tr_group = parser.add_argument_group('Training')
tr_group.add_argument('--batch-size', type=int,
help="Set batch-size for training graph")
tr_group.add_argument('--gradient-accumulation-count', type=int, default=1,
help="""Number of gradients to accumulate before doing a weight update.
When using pipelining this is the number of times each pipeline stage
will be executed.""")
tr_group.add_argument('--base-learning-rate-exponent', type=float,
help="Base learning rate exponent (2**N). blr = lr / bs")
tr_group.add_argument('--abs-learning-rate', type=float,
help="Absolute learning rate, if value not specified the base learning rate is used.")
tr_group.add_argument('--epochs', type=float,
help="Number of training epochs")
tr_group.add_argument('--iterations', type=int, default=None,
help="Force a fixed number of training iterations to be run rather than epochs.")
tr_group.add_argument('--weight-decay', type=float,
help="Value for weight decay bias, setting to 0 removes weight decay.")
tr_group.add_argument('--loss-scaling', type=float, default=128,
help="Loss scaling factor")
tr_group.add_argument('--label-smoothing', type=float, default=0,
help="Label smoothing factor (Default=0 => no smoothing)")
tr_group.add_argument('--ckpts-per-epoch', type=int, default=1,
help="Checkpoints per epoch")
tr_group.add_argument('--epochs-per-ckpt', type=int, default=0,
help="Epochs per checkpoint. Overwrites --ckpts-per-epoch")
tr_group.add_argument('--ckpt-epochs-offset', type=int, default=0,
help="Epoch offset when checkpointing starts.")
tr_group.add_argument('--ckpt-all-instances', type=bool, default=False,
help="""Allow all instances to create a checkpoint.
By default only instance 0 does checkpointing""")
tr_group.add_argument('--no-validation', action="store_false", dest='validation',
help="Do not do any validation runs.")
tr_group.set_defaults(validation=True)
tr_group.add_argument('--shards', type=int, default=1,
help="Number of IPU shards for training graph")
tr_group.add_argument('--replicas', type=int, default=1,
help="Replicate graph N times to increase batch to batch-size*N")
tr_group.add_argument('--max-cross-replica-buffer-size', type=int, default=10 * 1024 * 1024,
help="""The maximum number of bytes that can be waiting before a cross
replica sum op is scheduled. [Default=10*1024*1024]""")
tr_group.add_argument('--pipeline', action="store_true",
help="""Enables pipelining. Must also set --shards > 1
and --gradient-accumulation-count > --shards.""")
tr_group.add_argument('--pipeline-splits', nargs='+', type=str, default=None,
help="Strings for splitting pipelines. E.g. b2/0/relu b3/0/relu")
tr_group.add_argument('--pipeline-schedule', type=str, default="Interleaved",
choices=pipeline_schedule_options,
help="Pipelining schedule. Choose between 'Interleaved', 'Grouped' and 'Sequential'.")
tr_group.add_argument('--optimiser', type=str, default="SGD", choices=['SGD', 'RMSprop', 'momentum', 'LARS'],
help="Optimiser")
tr_group.add_argument('--momentum', type=float, default=0.9,
help="Momentum coefficient")
tr_group.add_argument('--rmsprop-decay', type=float,
help="RMSprop decay coefficient")
tr_group.add_argument('--rmsprop-base-decay-exp', type=float,
help="Linearly scale RMSprop decay coefficient as 1-(total_batch_size*2**rmsprop_base_decay_exp) ")
tr_group.add_argument('--rmsprop-epsilon', type=float, default=0.001,
help="RMSprop epsilon coefficient")
tr_group.add_argument('--offload-fp32-weight-copy', action="store_true",
help="Create an fp32 copy of fp16 weights which can be offloaded to remote memory")
tr_group.add_argument('--variable-filter', nargs='+', type=str, default=[],
help="Filter which variables to include in training")
tr_group.add_argument('--init-path', type=str,
help="Path to checkpoint to initialise from")
tr_group.add_argument('--distributed', action="store_true",
help="Use distributed multi-worker training")
tr_group.add_argument('--syncs-per-epoch', type=int, default=0,
help="Synchronize replicas when using poprun.")
tr_group.add_argument('--epochs-per-sync', type=float, default=0,
help="Synchronize replicas when using poprun after some epochs.")
tr_group.add_argument('--stable-norm', action="store_true",