role_maker.py 6.5 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 37
        raise NotImplementedError("Please implement this method in child class")

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

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

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

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

    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 77 78 79
    def __init__(self):
        from mpi4py import MPI
        self.comm_ = MPI.COMM_WORLD
        self.MPI = MPI
D
dongdaxiang 已提交
80
        self.ips_ = None
D
dongdaxiang 已提交
81 82

    def get_rank(self):
83 84 85
        """
        return rank
        """
D
dongdaxiang 已提交
86 87 88 89
        self.rank_ = self.comm_.Get_rank()
        return self.rank_

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

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

    def barrier_all(self):
104 105 106
        """
        barrier_all() will call MPI's barrier_all function
        """
D
dongdaxiang 已提交
107 108 109
        self.comm_.barrier()

    def get_ips(self):
110 111 112
        """
        collect current distributed job's ip list
        """
D
dongdaxiang 已提交
113 114 115 116 117
        if self.ips_ == None:
            self.ips_ = self.comm_.allgather(self.get_local_ip())
        return self.ips_

    def finalize(self):
118 119 120
        """
        finalize the current MPI instance.
        """
D
dongdaxiang 已提交
121 122 123 124
        self.comm_.finalize()


class MPISymetricRoleMaker(MPIRoleMaker):
125 126 127 128 129 130
    """
    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 已提交
131 132 133 134 135
    def __init__(self):
        super(MPISymetricRoleMaker, self).__init__()
        self.node_type_ = None
        self.proc_per_node_ = 2

136 137 138 139 140 141 142
    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

D
dongdaxiang 已提交
143
    def is_first_worker(self):
144 145 146 147 148 149
        """
        return whether current process is the first worker assigned by role maker
        """
        if self._check_role_generation():
            return self.is_worker() and 0 == self.worker_index()
        return False
D
dongdaxiang 已提交
150 151

    def is_worker(self):
152 153 154 155 156 157
        """
        return whether current process is worker assigned by role maker
        """
        if self._check_role_generation():
            return self.node_type_ == 1
        return False
D
dongdaxiang 已提交
158 159

    def is_server(self):
160 161 162 163 164 165
        """
        return whether current process is server assigned by role maker
        """
        if self._check_role_generation():
            return self.node_type_ == 0
        return False
D
dongdaxiang 已提交
166 167

    def worker_num(self):
168 169 170 171 172
        """
        return the current number of worker
        """
        if self._check_role_generation():
            if self.is_worker():
173
                return self.get_size() / 2;
174
        return 0
D
dongdaxiang 已提交
175 176

    def server_num(self):
177 178 179 180 181
        """
        return the current number of server
        """
        if self._check_role_generation():
            if self.is_server():
182
                return self.get_size() / 2;
183
        return 0
D
dongdaxiang 已提交
184 185

    def worker_index(self):
186 187 188 189 190 191
        """
        return the index of worker
        """
        if self._check_role_generation():
            return self.rank_ / self.proc_per_node_
        return 0
D
dongdaxiang 已提交
192 193

    def server_index(self):
194 195 196 197 198 199
        """
        return the index of server
        """
        if self._check_role_generation():
            return self.rank_ / self.proc_per_node_
        return 0
D
dongdaxiang 已提交
200 201

    def barrier_worker(self):
202 203 204 205 206 207
        """
        barrier all workers in current distributed job
        """
        if self._check_role_generation():
            if self.is_worker():
                self.node_type_comm_.barrier()
D
dongdaxiang 已提交
208 209

    def barrier_server(self):
210 211 212 213 214 215
        """
        barrier all servers in current distributed job
        """
        if self._check_role_generation():
            if self.is_server():
                self.node_type_comm_.barrier()
D
dongdaxiang 已提交
216 217

    def generate_role(self):
218 219 220 221 222 223 224 225 226 227 228 229 230 231
        """
        generate currently process's role
        """
        if not self.role_is_generated_:
            # TODO(guru4elephant): only allow to be called once
            self.trainer_endpoints_ = self.get_ips()
            self.pserver_endpoints_ = self.get_ips()

            if 0 == self.get_rank() % self.proc_per_node_ % 2:
                self.node_type_ = 0
            else:
                self.node_type_ = 1
            self.node_type_comm_ = self.comm_.Split(self.node_type_)
            self.role_is_generated_ = True