forked from Tencent/PocketFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_gpu_wrapper.py
More file actions
98 lines (81 loc) · 2.82 KB
/
multi_gpu_wrapper.py
File metadata and controls
98 lines (81 loc) · 2.82 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
# Tencent is pleased to support the open source community by making PocketFlow available.
#
# Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# 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.
# ==============================================================================
"""Wrapper for multi-GPU training."""
# use Hovorod / TF-Plus for multi-GPU training
try:
import horovod.tensorflow as mgw
print('using Horovod for multi-GPU training')
except ImportError:
try:
import tfplus.tensorflow as mgw
print('using TF-Plus for multi-GPU training')
except ImportError:
print('[WARNING] TF-Plus & Horovod cannot be imported; multi-GPU training is unsupported')
class MultiGpuWrapper(object):
"""Wrapper for multi-GPU training."""
def __init__(self):
"""Constructor function."""
pass
@classmethod
def init(cls, *args):
"""Initialization."""
try:
return mgw.init(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def size(cls, *args):
"""Get the number of workers at all nodes."""
try:
return mgw.size(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def rank(cls, *args):
"""Get the rank of current worker at all nodes."""
try:
return mgw.rank(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def local_size(cls, *args):
"""Get the number of workers at the current node."""
try:
return mgw.local_size(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def local_rank(cls, *args):
"""Get the rank of current worker at the current node."""
try:
return mgw.local_rank(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def DistributedOptimizer(cls, *args):
"""Get a distributed optimizer from the base optimizer."""
try:
return mgw.DistributedOptimizer(*args)
except NameError:
raise NameError('module <mgw> not imported')
@classmethod
def broadcast_global_variables(cls, *args):
"""Get a TensorFlow operation to broadcast all the global variables."""
try:
return mgw.broadcast_global_variables(*args)
except NameError:
raise NameError('module <mgw> not imported')