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 26 27 28
    def __init__(self):
        self.role_maker_name_ = ""
        self.trainer_endpoints_ = []
        self.pserver_endpoints_ = []
29
        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 48 49 50
        import socket
        self.ip_ = socket.gethostbyname(socket.gethostname())
        return self.ip_

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

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

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 79 80
        from mpi4py import MPI
        self.comm_ = MPI.COMM_WORLD
        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 89
        self.rank_ = self.comm_.Get_rank()
        return self.rank_

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

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 103
        return self.comm_.allgather(obj)

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

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

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

127
    def _finalize(self):
128 129 130
        """
        finalize the current MPI instance.
        """
131
        pass
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 143 144 145
    def __init__(self):
        super(MPISymetricRoleMaker, self).__init__()
        self.node_type_ = None
        self.proc_per_node_ = 2

146 147 148 149 150 151 152
    def _check_role_generation(self):
        if not self.role_is_generated_:
            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 166 167
        """
        return whether current process is worker assigned by role maker
        """
        if self._check_role_generation():
            return self.node_type_ == 1
        return False
D
dongdaxiang 已提交
168

169
    def _is_server(self):
170 171 172 173 174 175
        """
        return whether current process is server assigned by role maker
        """
        if self._check_role_generation():
            return self.node_type_ == 0
        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 200 201
        """
        return the index of worker
        """
        if self._check_role_generation():
            return self.rank_ / self.proc_per_node_
        return 0
D
dongdaxiang 已提交
202

203
    def _server_index(self):
204 205 206 207 208 209
        """
        return the index of server
        """
        if self._check_role_generation():
            return self.rank_ / self.proc_per_node_
        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():
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():
225
                self.node_type_comm_.barrier()
D
dongdaxiang 已提交
226

227
    def _generate_role(self):
228 229 230 231 232
        """
        generate currently process's role
        """
        if not self.role_is_generated_:
            # TODO(guru4elephant): only allow to be called once
X
xjqbest 已提交
233 234
            self.trainer_endpoints_ = self._get_ips()
            self.pserver_endpoints_ = self._get_ips()
235

X
xjqbest 已提交
236
            if 0 == self._get_rank() % self.proc_per_node_ % 2:
237 238 239 240 241
                self.node_type_ = 0
            else:
                self.node_type_ = 1
            self.node_type_comm_ = self.comm_.Split(self.node_type_)
            self.role_is_generated_ = True