-
Notifications
You must be signed in to change notification settings - Fork 93
/
comm.py
96 lines (77 loc) · 3.33 KB
/
comm.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
# Copyright 2020,2021 Sony Corporation.
# Copyright 2021 Sony Group Corporation.
#
# 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.
import nnabla as nn
from nnabla.logger import logger
def create_float_context(ctx):
from nnabla.ext_utils import get_extension_context
ctx_float = get_extension_context(ctx.backend[0].split(':')[
0], device_id=ctx.device_id)
return ctx_float
class CommunicatorWrapper(object):
def __init__(self, ctx):
try:
import nnabla.communicators as C
comm = C.MultiProcessDataParallelCommunicator(ctx)
comm.init()
self.n_procs = comm.size
self.rank = comm.rank
self.local_rank = comm.local_rank
self.comm = comm
except Exception as e:
print(e)
print('No communicator found. Running with a single process. If you run this with MPI processes,'
' all processes will perform totally same.')
self.n_procs = 1
self.rank = 0
self.local_rank = 0
self.comm = None
if len(ctx.device_id) > 0:
ctx.device_id = str(int(ctx.device_id) + int(self.local_rank))
self.ctx = ctx
self.ctx_float = create_float_context(ctx)
logger.info("[Communicator] Using gpu_id = {} as rank = {}".format(
self.ctx.device_id, self.rank))
def barrier(self):
if self.n_procs == 1:
# skip all reduce since no processes have to be all-reduced
return
self.comm.barrier()
def broadcast(self, x):
assert isinstance(x, nn.Variable)
if self.n_procs == 1:
# skip all reduce since no processes have to be all-reduced
return
self.comm.bcast([x.data], src=0, inplace=True)
def all_reduce(self, params, division, inplace):
if self.n_procs == 1:
# skip all reduce since no processes have to be all-reduced
return
self.comm.all_reduce(params, division=division, inplace=inplace)
def all_reduced_solver_update(self, solver, division=False, inplace=True):
if self.n_procs > 1:
params = [
x.grad for x in solver.get_parameters().values()]
self.all_reduce(params, division=division, inplace=inplace)
solver.update()
def all_reduced_solver_update_all(self, *solvers, division=False, inplace=True):
for solver in solvers:
self.all_reduced_solver_update(
solver, division=division, inplace=inplace)
def get_all_reduce_callback(self, params=None, packing_size=2 << 20):
if self.n_procs == 1:
return None
if params is None:
params = nn.get_parameters().values()
return self.comm.all_reduce_callback([x.grad for x in params], packing_size)