-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathhvd_strategy.py
More file actions
1450 lines (1274 loc) · 56.1 KB
/
hvd_strategy.py
File metadata and controls
1450 lines (1274 loc) · 56.1 KB
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
#!/usr/bin/env python
# Copyright 2023 Alibaba Group Holding Limited. 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.
# =============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import uuid
import re
import abc
import os
import contextlib
import threading
import random as rn
import json
import collections
import time
import six
import numpy as np
import horovod.tensorflow as hvd
from tensorflow._api.v1 import train as train_v1
from tensorflow.core.protobuf import config_pb2
from tensorflow.core.framework import summary_pb2
from tensorflow.python.client import device_lib
from tensorflow.python.distribute import device_util, estimator_training, \
multi_worker_util, device_util, \
estimator_training
from tensorflow.python.eager import context as _context
from tensorflow.python.estimator.model_fn import ModeKeys
from tensorflow.python.framework import ops, constant_op, dtypes, ops, random_seed, \
tensor_util
from tensorflow.python.util import nest, compat
from tensorflow.python.training import saver, server_lib, training, checkpoint_utils, \
checkpoint_management, optimizer, \
session_run_hook, basic_session_run_hooks, \
training_util
from tensorflow.python.training import monitored_session as _monitored_session
from tensorflow.python.platform import gfile
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.ops import array_ops, data_flow_ops, embedding_ops, \
gen_io_ops, math_ops, \
string_ops, control_flow_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.summary import summary as core_summary
from tensorflow.python.saved_model import utils_impl as saved_model_utils
from tensorflow.python.saved_model import builder, constants
from tensorflow.python.saved_model.model_utils.export_utils import \
(EXPORT_TAG_MAP, get_timestamped_export_dir, SIGNATURE_KEY_MAP)
try:
from tensorflow.python.training.saving.saveable_object_util import \
op_list_to_dict
except ImportError:
op_list_to_dict = saver.BaseSaverBuilder.OpListToDict
from tensorflow_estimator.python.estimator import run_config as run_config_lib
from tensorflow_estimator.python.estimator import estimator as _estimator_lib
from tensorflow_estimator.python.estimator.training import \
_is_google_env, _MAX_DELAY_SECS, _TrainingExecutor
from tensorflow_estimator.python.estimator.export import export_lib
from tensorflow.python.keras.backend import reset_uids as reset_keras_uids
##################### HVDSTRATEGY COMMON CODE ##########################
class HvdContext(object):
_instance = None
@classmethod
def get(cls):
r'''Get singleton.
'''
if cls._instance is None:
cls._instance = cls()
return cls._instance
@classmethod
@contextlib.contextmanager
def scope(cls, **kwargs):
r'''Update params in context.
'''
prev_kwargs = {}
try:
c = cls.get()
yield c
finally:
del prev_kwargs
@classmethod
def get_tf_config(cls):
r'''Get configuration from TF_CONFIG environment variable.
'''
tf_config = json.loads(os.getenv('TF_CONFIG', '{}'))
if not tf_config:
return None
task = tf_config['task']
cluster = tf_config['cluster']
task_type = task['type']
task_id = int(task['index'])
tf_config_type = collections.namedtuple(
'TfConfig', ['task_type', 'task_id', 'cluster'])
return tf_config_type(task_type, task_id, cluster)
@property
def cluster_spec(self):
r'''cluster spec.
'''
return self._cluster_spec
@property
def task_type(self):
r'''job name of current server. `localhost` by default.
'''
return self._task_type
@property
def task_id(self):
r'''task index of current server. 0 by default.
'''
return self._task_id
@property
def is_chief(self):
r'''True if current server is chief worker.
'''
return self._is_chief
@property
def has_gpu(self):
r'''True if current server has GPU.
'''
return self._num_gpus > 0
@property
def world_size(self):
r'''Number of devices.
'''
return self._world_size
@property
def rank(self):
r'''Global index of default local device.
'''
return self._rank
@property
def num_gpus(self):
r'''Number of GPUs.
'''
return self._num_gpus
def _update(self, task_type=None, task_id=None, cluster_spec=None,
num_gpus=None):
r'''Update parameters from cluster_spec.
If task_type, task_id or cluster_spec is None, these arguments will not be
changed.
Args:
task_type: (Optional.) name of current job. `localhost` by default.
task_id: (Optional.) index of current task. 0 by default.
cluster_spec: (Optional.) ClusterSpec object.
'''
tf_config = None
try:
tf_config = self.get_tf_config()
except: # pylint: disable=bare-except
pass
if tf_config:
self._task_type = tf_config.task_type
self._task_id = tf_config.task_id
self._cluster_spec = server_lib.ClusterSpec(tf_config.cluster)
else:
self._task_type = 'localhost'
self._task_id = 0
self._cluster_spec = None
if task_type:
self._task_type = task_type
if self._task_type not in ('localhost', 'chief', 'worker'):
logging.info('No valid configuration for non-worker roles')
return
if task_id:
self._task_id = task_id
if cluster_spec:
self._cluster_spec = cluster_spec
if self._cluster_spec:
self._cluster_spec = multi_worker_util.normalize_cluster_spec(
self._cluster_spec)
self._is_chief = False
try:
self._is_chief = multi_worker_util.is_chief(
self._cluster_spec, self._task_type, self._task_id)
except: # pylint: disable=bare-except
pass
if num_gpus:
self._num_gpus = num_gpus
elif not self._num_gpus:
num_gpus = 0
num_gpus_config = config_pb2.ConfigProto()
num_gpus_config.inter_op_parallelism_threads = 1
num_gpus_config.intra_op_parallelism_threads = 1
num_gpus_config.gpu_options.allow_growth = True
for device in device_lib.list_local_devices(num_gpus_config):
if device.device_type == 'GPU':
num_gpus += 1
self._num_gpus = num_gpus
self._default_device = (
f'/job:{self._task_type}/replica:0/task:{self._task_id}')
self._local_cpu_device = device_util.canonicalize(
'/device:CPU:0', default=self._default_device)
if self._num_gpus == 0:
self._local_devices = [self._local_cpu_device]
else:
self._local_devices = [
device_util.canonicalize(
f'/device:GPU:{d}', default=self._default_device)
for d in range(self._num_gpus)]
local_world_size_str = os.getenv('LOCAL_WORLD_SIZE', '')
if not local_world_size_str:
self._local_world_size = len(
self._local_devices) # pylint: disable=protected-access
else:
self._local_world_size = int(local_world_size_str)
if not self._cluster_spec:
self._devices = list(self._local_devices)
return
task_indices = []
try:
task_defs = dict(
enumerate(self._cluster_spec.job_tasks(self._task_type)))
task_indices = sorted(task_defs)
except: # pylint: disable=bare-except
pass
worker_indices = []
try:
worker_defs = dict(
enumerate(self._cluster_spec.job_tasks('worker')))
worker_indices = sorted(worker_defs)
except: # pylint: disable=bare-except
pass
chief_indices = []
try:
chief_defs = dict(enumerate(self._cluster_spec.job_tasks('chief')))
chief_indices = sorted(chief_defs)
except: # pylint: disable=bare-except
pass
self._cpu_devices = [
device_util.resolve(
f'/job:{self._task_type}/task:{t}/device:CPU:0')
for t in task_indices]
if self._num_gpus == 0:
self._devices = self._cpu_devices
if self._task_type == 'worker':
chief_devices = [
device_util.resolve(f'/job:chief/task:{t}/device:CPU:0')
for t in chief_indices]
self._devices = chief_devices + self._devices
elif self._task_type == 'chief':
self._devices += [
device_util.resolve(f'/job:worker/task:{t}/device:CPU:0')
for t in worker_indices]
return
self._devices = [
device_util.resolve(
f'/job:{self._task_type}/task:{t}/device:GPU:{g}')
for t in task_indices for g in range(self._num_gpus)]
if self._task_type == 'worker':
chief_devices = [
device_util.resolve(f'/job:chief/task:{t}/device:GPU:{g}')
for t in chief_indices for g in range(self._num_gpus)]
self._devices = chief_devices + self._devices
elif self._task_type == 'chief':
self._devices += [
device_util.resolve(f'/job:worker/task:{t}/device:GPU:{g}')
for t in worker_indices for g in range(self._num_gpus)]
def __init__(self):
r'''Construct a server specification.
'''
self._task_type = 'localhost'
self._rank = hvd.local_rank()
self._world_size = hvd.size()
self._task_id = 0
self._cluster_spec = None
self._is_chief = True
visible_devices = os.getenv('CUDA_VISIBLE_DEVICES', '')
if visible_devices:
self._num_gpus = len(visible_devices.split(','))
else:
self._num_gpus = 1
self._update()
self._saving_listener_registry = {}
class GraphRewriting(object): # pylint: disable=useless-object-inheritance
r'''Python API rewriting.
'''
_lock = threading.Lock()
_stack_depth = 0
_registry = {}
_registry_keys = []
@classmethod
def register(cls, rewriting):
r'''Register implementation.
Args:
rewriting: Implementation class to register.
'''
if not issubclass(rewriting, cls):
raise ValueError(
f'{rewriting} must be a subclass of GraphRewriting')
if rewriting.__name__ not in cls._registry:
cls._registry_keys.append(rewriting.__name__)
cls._registry[rewriting.__name__] = rewriting()
@classmethod
@contextlib.contextmanager
def scope(cls, **kwargs):
r'''Context manager that patches Python APIs.
'''
seed = kwargs.pop('seed', None)
if seed is not None:
rn.seed(seed)
np.random.seed(seed)
random_seed.set_random_seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
with HvdContext.scope() as ctx:
try:
with cls._lock:
cls._stack_depth += 1
if cls._stack_depth <= 1:
for name in cls._registry_keys:
cls._registry[name].begin()
yield ctx
finally:
with cls._lock:
if cls._stack_depth <= 1:
for name in reversed(cls._registry_keys):
cls._registry[name].end()
cls._stack_depth -= 1
@abc.abstractmethod
def begin(self):
r'''Rewrites API.
'''
@abc.abstractmethod
def end(self):
r'''Revert API rewriting.
'''
##################### Optimizer ##########################
def wraps_optimizer(cls):
r'''Decorator to create horovod optimizer class.
Args:
optimizer_type: The actual optimizer type that will be used to compute and
apply the gradients. Must be one of the Optimizer classes.
aggregation: Aggregate gradients inside `compute_gradients` or
`apply_gradients`.
Returns:
HvdOptimizer
'''
class HvdOptimizer(cls, optimizer.Optimizer):
def __init__(self, learning_rate=0.001, *args, **kwargs):
learning_rate = learning_rate * HvdContext.get().world_size
super(HvdOptimizer, self).__init__(learning_rate, *args, **kwargs)
if isinstance(cls, HvdOptimizer):
return cls
else:
def horovod_optimizer(*args, **kwargs):
from horovod.tensorflow import DistributedOptimizer
horovod_args = DistributedOptimizer.__code__.co_varnames
horovod_real_kargs = {}
candidate_keys = list(kwargs.keys())
for kwarg in candidate_keys:
if kwarg in horovod_args:
value = kwargs[kwarg]
del kwargs[kwarg]
horovod_real_kargs[kwarg] = value
return DistributedOptimizer(HvdOptimizer(*args, **kwargs), **horovod_real_kargs)
return horovod_optimizer
class OptimizerRewriting(GraphRewriting):
r'''Rewriting optimizers.
'''
def __init__(self):
super().__init__()
self._prev_optimizers = {}
def begin(self):
r'''Rewrites API.
'''
for k, c in training.__dict__.items():
if (isinstance(c, type)
and issubclass(c, training.Optimizer)
and c not in (
training.Optimizer,
training.SyncReplicasOptimizer)):
self._prev_optimizers[k] = c
wrapped = wraps_optimizer(c)
setattr(training, k, wrapped)
setattr(train_v1, k, wrapped)
def end(self):
r'''Revert API rewriting.
'''
for c, opt in self._prev_optimizers.items():
setattr(training, c, opt)
setattr(train_v1, c, opt)
GraphRewriting.register(OptimizerRewriting)
##################### MonitoredTrainingSession ##########################
def wraps_session_config(session_config, *args, **kwargs):
r'''Wraps ConfigProto for distributed training.
'''
if not session_config:
kwargs.setdefault('allow_soft_placement', True)
session_config = config_pb2.ConfigProto(*args, **kwargs)
session_config.gpu_options.allow_growth = True
session_config.gpu_options.force_gpu_compatible = True
# Horovod: pin GPU to be used to process local rank (one GPU per process)
session_config.gpu_options.visible_device_list = str(HvdContext.get().rank)
return session_config
def wraps_monitored_training_session(fn):
r'''Decorator to create wrapped monitored training session.
'''
if hasattr(fn, 'wrapped_fn'):
return fn
def HorovodMonitoredTrainingSession(*args, **kwargs): # pylint: disable=invalid-name
r'''Creates a `MonitoredSession` for training.
'''
checkpoint_dir = kwargs.get('checkpoint_dir', None)
if HvdContext.get().rank != 0:
checkpoint_dir = None
summary_dir = kwargs.get('summary_dir', None)
summary_dir = summary_dir or checkpoint_dir
scaffold = kwargs.pop('scaffold', _monitored_session.Scaffold())
kwargs['scaffold'] = scaffold
hooks = kwargs.pop('hooks', [])
hooks.append(hvd.BroadcastGlobalVariablesHook(0))
chief_only_hooks = kwargs.pop('chief_only_hooks', [])
chief_only_hooks = list(chief_only_hooks)
kwargs['hooks'] = hooks
kwargs['chief_only_hooks'] = chief_only_hooks
kwargs['config'] = wraps_session_config(kwargs.pop('config', None))
kwargs['is_chief'] = True
args = list(args)
prev_monitored_session = _monitored_session.MonitoredSession
sess = fn(*args, **kwargs)
_monitored_session.MonitoredSession = prev_monitored_session
return sess
HorovodMonitoredTrainingSession.wrapped_fn = fn
return HorovodMonitoredTrainingSession
class SessionRewriting(GraphRewriting):
r'''Rewriting monitored training session.
'''
def __init__(self):
super().__init__()
self._prev_sess_fn = None
def begin(self):
r'''Rewrites API.
'''
self._prev_sess_fn = _monitored_session.MonitoredTrainingSession
_monitored_session.MonitoredTrainingSession = (
wraps_monitored_training_session(
_monitored_session.MonitoredTrainingSession))
training.MonitoredTrainingSession = (
_monitored_session.MonitoredTrainingSession)
train_v1.MonitoredTrainingSession = (
_monitored_session.MonitoredTrainingSession)
def end(self):
r'''Revert API rewriting.
'''
train_v1.MonitoredTrainingSession = self._prev_sess_fn
training.MonitoredTrainingSession = self._prev_sess_fn
_monitored_session.MonitoredTrainingSession = self._prev_sess_fn
GraphRewriting.register(SessionRewriting)
##################### Saver ##############################
class CollectiveSaverBase(object): # pylint: disable=useless-object-inheritance
r'''Base class of sharded savers.
'''
def wraps_saver(cls):
r'''Wraps a saver to support hybrid parallelism.
'''
if issubclass(cls, CollectiveSaverBase):
return cls
class CollectiveSaver(cls, CollectiveSaverBase):
r'''SaverBuilder with support for hybrid parallelism.
'''
def __init__(self, *args, **kwargs):
self._rank = HvdContext.get().rank
self._world_size = HvdContext.get().world_size
kwargs['sharded'] = True
kwargs['allow_empty'] = True
with ops.device('/cpu:0'):
super().__init__(*args, **kwargs)
@property
def rank(self):
return self._rank
@property
def world_size(self):
return self._world_size
def _build(self, *args, **kwargs):
r'''Builds saver_def.
'''
if self._world_size <= 1:
super()._build(*args, **kwargs)
return
if self._builder is None:
super()._build(*args, **kwargs)
def save(self, *args, **kwargs):
r'''Saves sharded variables.
'''
if self._world_size <= 1:
super().save(*args, **kwargs)
return
write_meta_graph = (
kwargs.pop('write_meta_graph', True)
and self._rank == 0)
kwargs['write_meta_graph'] = write_meta_graph
write_state = kwargs.pop('write_state', True) and self._rank == 0
kwargs['write_state'] = write_state
super().save(*args, **kwargs)
def export_meta_graph(self, filename=None, **kwargs):
if self._rank == 0:
return super().export_meta_graph(filename=filename, **kwargs)
return None
return CollectiveSaver
Saver = wraps_saver(saver.Saver)
def replace_default_saver():
rank = HvdContext.get().rank
savers = ops.get_collection_ref(ops.GraphKeys.SAVERS)
if not savers:
default_saver = Saver()
ops.add_to_collection(ops.GraphKeys.SAVERS, default_saver)
return
if len(savers) > 1:
raise ValueError(
f'Multiple items found in collection SAVERS: {savers}')
default_saver = savers[0]
if isinstance(default_saver, CollectiveSaverBase):
return
if not default_saver._sharded: # pylint: disable=protected-access
raise ValueError('Default saver must be sharded')
if default_saver._builder is None:
def _wraps_build(build_fn):
r'''Decorator to wrap saver build.
'''
def wrapped_build(self, *args, **kwargs):
r'''Builds saver_def.
'''
build_fn(self, *args, **kwargs)
return wrapped_build
default_saver._build = _wraps_build(
default_saver._build) # pylint: disable=protected-access
def _wraps_save(save_fn):
def wrapped_save(self, *args, **kwargs):
r'''Saves sharded variables.
'''
write_meta_graph = kwargs.pop(
'write_meta_graph', True) and rank == 0
kwargs['write_meta_graph'] = write_meta_graph
write_state = kwargs.pop('write_state', True) and rank == 0
kwargs['write_state'] = write_state
save_fn(self, *args, **kwargs)
return wrapped_save
default_saver.save = _wraps_save(default_saver.save)
class DefaultSaverRewriting(GraphRewriting):
r'''A SessionRunHook replaces default saver.
'''
def begin(self):
r''' initialize replica variables and enable synchronous dataset wrapper
'''
replace_default_saver()
GraphRewriting.register(DefaultSaverRewriting)
##################### Estimator ##########################
def export_all(
export_dir_base,
checkpoint_path,
signature_defs_and_main_op_fn,
assets_extra=None,
as_text=False,
clear_devices=True,
strip_default_attrs=True,
modes=None,
**kwargs):
r'''Build a SavedModel from variables in checkpoint.
Args:
export_dir_base: A string containing a directory to write the exported
graph and checkpoints.
checkpoint_path: A path to a checkpoint.
signature_defs_and_main_op_fn: Function returns signature defs and main_op.
assets_extra: A dict specifying how to populate the assets.extra directory
within the exported SavedModel. Each key should give the destination
path (including the filename) relative to the assets.extra directory.
The corresponding value gives the full path of the source file to be
copied. For example, the simple case of copying a single file without
renaming it is specified as
`{'my_asset_file.txt': '/path/to/my_asset_file.txt'}`.
as_text: Whether or not to write the SavedModel proto in text format.
clear_devices: Whether or not to clear the device field.
strip_default_attrs: Whether or not to remove default-valued attributes
from the NodeDefs.
modes: List contains PREDICT, TRAIN or TEST.
Returns:
Export directory if it's chief.
'''
if HvdContext.get().rank != 0:
return None
export_dir = get_timestamped_export_dir(export_dir_base)
with ops.Graph().as_default():
with HvdContext.scope():
# Build graph.
signature_def_map = signature_defs_and_main_op_fn()
main_op = None
if isinstance(signature_def_map, (tuple, list)):
if len(signature_def_map) > 1:
main_op = signature_def_map[1]
signature_def_map = signature_def_map[0]
if not main_op:
main_op = _monitored_session.Scaffold.default_local_init_op()
if modes is None:
modes = [ModeKeys.PREDICT, ModeKeys.TRAIN, ModeKeys.EVAL]
modes = [
m for m in modes
if SIGNATURE_KEY_MAP[m] in signature_def_map]
signature_def_map = {
k: signature_def_map[k] for k in signature_def_map
if k in [SIGNATURE_KEY_MAP[m] for m in modes]}
signature_tags = [EXPORT_TAG_MAP[m][0] for m in modes]
b = builder.SavedModelBuilder(export_dir, **kwargs)
b._has_saved_variables = True # pylint: disable=protected-access
# Copy variables.
saved_model_utils.get_or_create_variables_dir(export_dir)
export_checkpoint_path = saved_model_utils.get_variables_path(
export_dir)
checkpoint_files = [
*gfile.Glob(f'{checkpoint_path}.index'),
*gfile.Glob(f'{checkpoint_path}.data-?????-of-?????')]
for f in checkpoint_files:
export_ckpt = re.sub(
compat.as_text(checkpoint_path),
compat.as_text(export_checkpoint_path),
f)
gfile.Copy(f, export_ckpt)
# Add MetaGraph.
b.add_meta_graph(
tags=signature_tags,
signature_def_map=signature_def_map,
assets_collection=ops.get_collection(
ops.GraphKeys.ASSET_FILEPATHS),
clear_devices=clear_devices,
main_op=main_op,
strip_default_attrs=strip_default_attrs)
# Save model.
b.save(as_text=as_text)
# Save extras.
if assets_extra:
assets_extra_path = os.path.join(
export_dir, constants.EXTRA_ASSETS_DIRECTORY)
for dst, src in assets_extra.items():
target = os.path.join(
assets_extra_path, compat.as_bytes(dst))
gfile.MakeDirs(os.path.dirname(target))
gfile.Copy(src, target)
def wraps_model_fn(model_fn, model_dir, config):
r'''Decorator to set params in a model function.
'''
def wrapped_model_fn(features, labels, mode, params):
r'''Wrapped model function.
'''
with scope():
estimator_spec = model_fn(features, labels, mode, params)
if estimator_spec.scaffold.saver is None:
estimator_spec.scaffold._saver = Saver( # pylint: disable=protected-access
max_to_keep=config.keep_checkpoint_max,
keep_checkpoint_every_n_hours=config.keep_checkpoint_every_n_hours,
defer_build=True,
save_relative_paths=True)
training_hooks = list(estimator_spec.training_hooks) or []
training_chief_hooks = list(
estimator_spec.training_chief_hooks) or []
estimator_spec = estimator_spec._replace( # pylint: disable=protected-access
training_hooks=training_hooks,
training_chief_hooks=training_chief_hooks)
return estimator_spec
return wrapped_model_fn
def start_std_server(config):
r'''Creates, starts, and returns a server_lib.Server.
'''
logging.info('Start Tensorflow server.')
return server_lib.Server(config.cluster_spec,
job_name=config.task_type,
task_index=config.task_id,
config=wraps_session_config(config.session_config),
start=True,
protocol=config.protocol)
class ReuseVariables(object): # pylint: disable=useless-object-inheritance
r'''Variable reusing context.
'''
def __call__(self, reuse):
reset_keras_uids()
varscope = ops.get_default_graph().get_collection_ref(('__varscope',))
if varscope:
varscope[0].variable_scopes_count.clear()
vs.get_variable_scope()._reuse = reuse # pylint: disable=protected-access
@contextlib.contextmanager
def reuse_variables(reuse=None):
r'''Context manager that reuses variables.
'''
try:
fn = ReuseVariables()
prev_reuse = vs.get_variable_scope()._reuse # pylint: disable=protected-access
if reuse is not None:
fn(reuse)
yield fn
finally:
vs.get_variable_scope()._reuse = prev_reuse # pylint: disable=protected-access
EvaluationSpec = collections.namedtuple(
'EvaluationSpec', ['name', 'hooks', 'update_op', 'eval_dict'])
class EvaluationHook(session_run_hook.SessionRunHook):
r'''Hook to make evaluation along with training.
'''
def __init__(
self, fn,
steps=100,
every_n_iter=1000,
summary_dir=None,
history=None):
r'''Evaluates specific function.
Args:
fn: Function returns update_op, metric ops and hooks.
steps: Number of steps for which to evaluate model.
every_n_iter: `int`, runs the evaluator once every N training iteration.
summary_dir: Directory for summaries.
history: History of eval metrics. history should support `append` method.
Raises:
ValueError: if `every_n_iter` is non-positive or it's not a single machine
training
'''
if every_n_iter is None or every_n_iter <= 0:
raise ValueError(f'invalid every_n_iter={every_n_iter}.')
self._fn = fn
self._steps = steps
self._every_n_iter = every_n_iter
self._summary_dir = summary_dir
self._history = history
def begin(self):
r'''Preprocess global step and evaluation's hooks.
'''
self._evaluation_specs = ops.get_collection_ref(
EvaluationSpec.__name__)
if len(self._evaluation_specs) > 0:
raise ValueError('Only one evaluation spec allowed in a graph')
self._timer = None
self._iter_count = 0
self._hooks = []
self._timer = basic_session_run_hooks.SecondOrStepTimer(
every_steps=self._every_n_iter)
self._timer.reset()
with reuse_variables(vs.AUTO_REUSE):
with scope():
with ops.name_scope(ModeKeys.EVAL):
eval_spec = self._fn()
if isinstance(eval_spec, dict):
eval_dict = {}
update_ops = []
for metric_name, metric_val_and_update in eval_spec.items():
if not isinstance(metric_name, six.string_types):
raise ValueError(
f'Metric name {metric_name} should be a str')
if (not isinstance(metric_val_and_update, (tuple, list))
or len(metric_val_and_update) != 2):
raise ValueError(
f'{metric_val_and_update} should be a tuple '
'of (metric, update_op)')
eval_dict[metric_name] = metric_val_and_update[0]
update_ops.append(metric_val_and_update[1])
update_op = control_flow_ops.group(update_ops)
eval_spec = EvaluationSpec(
name=EvaluationSpec.__name__,
hooks=None,
update_op=update_op,
eval_dict=eval_dict)
if not isinstance(eval_spec, EvaluationSpec):
raise ValueError(
'eval_fn should return a dict or a EvaluationSpec')
self._evaluation_specs.append(eval_spec)
if eval_spec.hooks:
self._hooks.extend(eval_spec.hooks)
eval_dict = dict(eval_spec.eval_dict)
if ops.GraphKeys.GLOBAL_STEP not in eval_dict:
global_step_tensor = training_util.get_global_step(
ops.get_default_graph())
eval_dict[ops.GraphKeys.GLOBAL_STEP] = global_step_tensor
for h in self._hooks:
h.begin()
self._update_op = eval_spec.update_op
self._metrics = eval_dict
def after_create_session(self, session, coord): # pylint: disable=unused-argument
r'''Call evaluation's hooks.
'''
if ops.get_collection(ops.GraphKeys.SAVEABLE_OBJECTS):
raise ValueError(
'EvaluationHook does not support saveables other than global '
'variables.')
for h in self._hooks:
h.after_create_session(session, coord)
def after_run(self, run_context, run_values): # pylint: disable=unused-argument
r'''Runs evaluator after session run.
'''
self._iter_count += 1
if self._timer.should_trigger_for_step(self._iter_count):
ctx_stop_requested = run_context.stop_requested
run_context._stop_requested = False # pylint: disable=protected-access
self._evaluate(run_context)
run_context._stop_requested = ctx_stop_requested # pylint: disable=protected-access
def _call_before_run_hooks(
self, run_context, fetch_dict, user_feed_dict=None):
r'''Call hooks.before_run and handle requests from hooks.
'''
hook_feeds = {}
for hook in self._hooks:
request = hook.before_run(run_context)
if request is not None:
if request.fetches is not None:
fetch_dict[hook] = request.fetches
if request.feed_dict:
hook_feeds.update(request.feed_dict)
if not hook_feeds:
return user_feed_dict
if not user_feed_dict:
return hook_feeds
hook_feeds.update(user_feed_dict)
return hook_feeds
def _run(self, run_context, fetches):
r'''Run the evaluation.
'''
if isinstance(fetches, dict):
actual_fetches = fetches
else:
actual_fetches = {fetches: fetches}
eval_metrics = self._call_before_run_hooks(
run_context, actual_fetches)
eval_results = run_context.session.run(
actual_fetches, feed_dict=eval_metrics)
for hook in self._hooks:
hook.after_run(
run_context,
session_run_hook.SessionRunValues(
results=eval_results.pop(hook, None),
options=config_pb2.RunOptions(),
run_metadata=config_pb2.RunMetadata()))
return eval_results
def _write_dict_to_summary(self, dictionary):
r'''Write evaluation results to summary directory.
'''
current_global_step = dictionary[ops.GraphKeys.GLOBAL_STEP]
prev_np_printoptions = np.get_printoptions()
np.set_printoptions(suppress=True)
stats = ', '.join(
f'{k} = {v}'
for k, v in sorted(six.iteritems(dictionary))
if not (
isinstance(v, six.binary_type)
or k == ops.GraphKeys.GLOBAL_STEP))
np.set_printoptions(**prev_np_printoptions)
logging.info('Saving metrics for step %d: %s',
current_global_step, stats)
summary_dir = self._summary_dir
if HvdContext.get().world_size > 1:
summary_dir = os.path.join(summary_dir, f'{HvdContext.get().rank}')
summary_writer = core_summary.FileWriterCache.get(summary_dir)
summary_proto = summary_pb2.Summary()
for key in dictionary:
if dictionary[key] is None:
continue
if key == 'global_step':
continue
if isinstance(dictionary[key], (np.float32, float)):
summary_proto.value.add(
tag=key, simple_value=float(dictionary[key]))
elif isinstance(dictionary[key], (np.int64, np.int32, int)):
summary_proto.value.add(
tag=key, simple_value=int(dictionary[key]))
elif isinstance(dictionary[key], six.binary_type):
try:
summ = summary_pb2.Summary.FromString(dictionary[key])