role_maker.py 6.8 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2019 PaddlePaddle Authors. 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.
14
import sys
D
dongdaxiang 已提交
15 16 17


class RoleMakerBase(object):
18 19 20 21 22 23 24
    """
    RoleMakerBase is a base class for assigning a role to current process
    in distributed training.
    A paddle developer can implement RoleMakerBase to design a role maker
    for worker or pserver assignment.
    """

D
dongdaxiang 已提交
25
    def __init__(self):
D
dongdaxiang 已提交
26 27 28 29
        self._role_maker_name = ""
        self._trainer_endpoints = []
        self._pserver_endpoints = []
        self._role_is_generated = False
D
dongdaxiang 已提交
30

31
    def _is_worker(self):
32 33 34
        """
        return is_worker() of current process
        """
D
dongdaxiang 已提交
35 36
        raise NotImplementedError("Please implement this method in child class")

37
    def _is_server(self):
38 39 40
        """
        return is_server() of current process
        """
D
dongdaxiang 已提交
41 42
        raise NotImplementedError("Please implement this method in child class")

43
    def _get_local_ip(self):
44 45 46
        """
        return get local ip
        """
D
dongdaxiang 已提交
47
        import socket
D
dongdaxiang 已提交
48 49
        self._ip = socket.gethostbyname(socket.gethostname())
        return self._ip
D
dongdaxiang 已提交
50

51
    def _get_trainer_endpoints(self):
52 53 54
        """
        return trainer endpoints
        """
D
dongdaxiang 已提交
55
        return self._trainer_endpoints
D
dongdaxiang 已提交
56

57
    def _get_pserver_endpoints(self):
58 59 60
        """
        return pserver endpoints
        """
D
dongdaxiang 已提交
61
        return self._pserver_endpoints
D
dongdaxiang 已提交
62

63
    def _generate_role(self):
64 65 66
        """
        generate_role() should be called to identify current process's role
        """
D
dongdaxiang 已提交
67 68 69 70
        raise NotImplementedError("Please implement this method in child class")


class MPIRoleMaker(RoleMakerBase):
71 72 73 74 75
    """
    MPIRoleMaker is a MPI-API based role maker which is a counter-part of K8SRoleMaker
    mpi4py will be used if a developer inherits MPIRoleMaker
    """

D
dongdaxiang 已提交
76
    def __init__(self):
X
xujiaqi01 已提交
77
        super(MPIRoleMaker, self).__init__()
D
dongdaxiang 已提交
78
        from mpi4py import MPI
D
dongdaxiang 已提交
79
        self._comm = MPI.COMM_WORLD
D
dongdaxiang 已提交
80
        self.MPI = MPI
D
dongdaxiang 已提交
81
        self._ips = None
D
dongdaxiang 已提交
82

83
    def _get_rank(self):
84 85 86
        """
        return rank
        """
D
dongdaxiang 已提交
87 88
        self._rank = self._comm.Get_rank()
        return self._rank
D
dongdaxiang 已提交
89

90
    def _get_size(self):
91 92 93
        """
        return size
        """
D
dongdaxiang 已提交
94 95
        self._size = self._comm.Get_size()
        return self._size
D
dongdaxiang 已提交
96

97
    def _all_gather(self, obj):
98 99 100
        """
        all_gather(obj) will call MPI's allgather function
        """
X
xjqbest 已提交
101
        self._barrier_all()
D
dongdaxiang 已提交
102
        return self._comm.allgather(obj)
D
dongdaxiang 已提交
103

X
xjqbest 已提交
104 105 106 107 108
    def _worker_gather(self, obj):
        """
        worker_gather(obj) will call MPI's allgather function
        """
        if self._is_worker():
D
dongdaxiang 已提交
109 110
            self._node_type_comm.barrier()
            return self._node_type_comm.allgather(obj)
X
xjqbest 已提交
111 112
        return None

113
    def _barrier_all(self):
114 115 116
        """
        barrier_all() will call MPI's barrier_all function
        """
D
dongdaxiang 已提交
117
        self._comm.barrier()
D
dongdaxiang 已提交
118

119
    def _get_ips(self):
120 121 122
        """
        collect current distributed job's ip list
        """
D
dongdaxiang 已提交
123 124 125
        if self._ips == None:
            self._ips = self._comm.allgather(self._get_local_ip())
        return self._ips
D
dongdaxiang 已提交
126

127
    def _finalize(self):
128 129 130
        """
        finalize the current MPI instance.
        """
D
dongdaxiang 已提交
131
        self._comm.finalize()
D
dongdaxiang 已提交
132 133 134


class MPISymetricRoleMaker(MPIRoleMaker):
135 136 137 138 139 140
    """
    MPISymetricRoleMaker is designed for worker and server assignment
    under MPI. Typically, a worker and a server node will be appointed
    on each physical node. This role maker can be only used under MPI.
    """

D
dongdaxiang 已提交
141 142
    def __init__(self):
        super(MPISymetricRoleMaker, self).__init__()
D
dongdaxiang 已提交
143 144
        self._node_type = None
        self._proc_per_node = 2
D
dongdaxiang 已提交
145

146
    def _check_role_generation(self):
D
dongdaxiang 已提交
147
        if not self._role_is_generated:
148 149 150 151 152
            sys.stderr.write("generate_role() should be called first")
            sys.exit(-1)
            return False
        return True

153
    def _is_first_worker(self):
154 155 156 157
        """
        return whether current process is the first worker assigned by role maker
        """
        if self._check_role_generation():
X
xjqbest 已提交
158
            return self._is_worker() and 0 == self._worker_index()
159
        return False
D
dongdaxiang 已提交
160

161
    def _is_worker(self):
162 163 164 165
        """
        return whether current process is worker assigned by role maker
        """
        if self._check_role_generation():
D
dongdaxiang 已提交
166
            return self._node_type == 1
167
        return False
D
dongdaxiang 已提交
168

169
    def _is_server(self):
170 171 172 173
        """
        return whether current process is server assigned by role maker
        """
        if self._check_role_generation():
D
dongdaxiang 已提交
174
            return self._node_type == 0
175
        return False
D
dongdaxiang 已提交
176

177
    def _worker_num(self):
178 179 180 181
        """
        return the current number of worker
        """
        if self._check_role_generation():
X
xjqbest 已提交
182 183
            if self._is_worker():
                return self._get_size() / 2
184
        return 0
D
dongdaxiang 已提交
185

186
    def _server_num(self):
187 188 189 190
        """
        return the current number of server
        """
        if self._check_role_generation():
X
xjqbest 已提交
191 192
            if self._is_server():
                return self._get_size() / 2
193
        return 0
D
dongdaxiang 已提交
194

195
    def _worker_index(self):
196 197 198 199
        """
        return the index of worker
        """
        if self._check_role_generation():
D
dongdaxiang 已提交
200
            return self._rank / self._proc_per_node
201
        return 0
D
dongdaxiang 已提交
202

203
    def _server_index(self):
204 205 206 207
        """
        return the index of server
        """
        if self._check_role_generation():
D
dongdaxiang 已提交
208
            return self._rank / self._proc_per_node
209
        return 0
D
dongdaxiang 已提交
210

211
    def _barrier_worker(self):
212 213 214 215
        """
        barrier all workers in current distributed job
        """
        if self._check_role_generation():
X
xjqbest 已提交
216
            if self._is_worker():
D
dongdaxiang 已提交
217
                self._node_type_comm.barrier()
D
dongdaxiang 已提交
218

219
    def _barrier_server(self):
220 221 222 223
        """
        barrier all servers in current distributed job
        """
        if self._check_role_generation():
X
xjqbest 已提交
224
            if self._is_server():
D
dongdaxiang 已提交
225
                self._node_type_comm.barrier()
D
dongdaxiang 已提交
226

227
    def _generate_role(self):
228 229 230
        """
        generate currently process's role
        """
D
dongdaxiang 已提交
231
        if not self._role_is_generated:
232
            # TODO(guru4elephant): only allow to be called once
D
dongdaxiang 已提交
233 234
            self._trainer_endpoints = self._get_ips()
            self._pserver_endpoints = self._get_ips()
235

D
dongdaxiang 已提交
236 237
            if 0 == self._get_rank() % self._proc_per_node % 2:
                self._node_type = 0
238
            else:
D
dongdaxiang 已提交
239 240 241
                self._node_type = 1
            self._node_type_comm = self._comm.Split(self._node_type)
            self._role_is_generated = True