manager.py 20.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2021 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.

import time
import socket
import os
import six
19
import copy
20 21
import logging
import signal
K
kuizhiqing 已提交
22
import random
23 24 25 26
import threading
import traceback
from paddle.distributed.fleet import cloud_utils
from paddle.distributed.fleet import launch_utils
27 28

logger = logging.getLogger("ELASTIC")
29 30 31 32 33 34
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
    fmt='%(name)s %(levelname)s %(asctime)s %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)
35 36 37

ELASTIC_EXIT_CODE = 101

38 39 40 41 42 43 44 45 46 47 48 49
# wait for timeout, unit: seconds
ELASTIC_TIMEOUT = 2 * 60

# keepalived ttl, unit: seconds
ELASTIC_TTL = 60


# 1: Fault tolerance, 2: Elastic
class ElasticLevel:
    FAULT_TOLERANCE = 1
    ELASTIC = 2

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

class ElasticStatus:
    COMPLETED = "completed"
    ERROR = "error"
    HOLD = "hold"
    RESTART = "restart"
    EXIT = "exit"


class LauncherInterface(object):
    def __init__(self, args):
        self.args = args
        self.procs = []

    def _terminate_procs(self):
K
kuizhiqing 已提交
65
        # try to terminate process by group, this happend in multiprocess senario in user process
K
kuizhiqing 已提交
66 67 68 69 70 71 72 73
        if os.name != 'nt':
            for p in self.procs:
                if p.proc.poll() is None:
                    os.killpg(os.getpgid(p.proc.pid), signal.SIGTERM)
                    if p.log_fn:
                        p.log_fn.close()
                    logger.info("terminate process group gid:{}".format(
                        p.proc.pid))
K
kuizhiqing 已提交
74

K
kuizhiqing 已提交
75
            time.sleep(1)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        for p in self.procs:
            if p.proc.poll() is None:
                p.proc.terminate()
                if p.log_fn:
                    p.log_fn.close()
                logger.info("terminate process id:{}".format(p.proc.pid))

        for step in range(0, 50):
            alive = False
            for p in self.procs:
                if p.proc.poll() is None:  # not termniate
                    os.kill(p.proc.pid, signal.SIGKILL)
                    alive = True

            if not alive:
K
kuizhiqing 已提交
91
                logger.info("terminated all the procs")
92 93 94 95 96 97 98 99 100 101 102 103 104
                return True

            time.sleep(1)
        return False

    def _check_procs(self):
        alive = False
        result = None
        for p in self.procs:
            ret = p.proc.poll()
            if ret is None:
                alive = True
            elif ret != 0:
K
kuizhiqing 已提交
105 106 107 108
                logger.error("ABORT!!! ABORT!!! ABORT!!!")
                logger.error(
                    "ERROR rank {} error with exit code {}, check log for detail.".
                    format(p.rank, ret))
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                result = ret
        if not alive and result is None:
            return 0
        else:
            return result

    def launch(self):
        raise NotImplementedError

    def stop(self):
        raise NotImplementedError

    def watch(self):
        raise NotImplementedError


class ElasticManager(object):
126
    def __init__(self, args, etcd_client):
127 128 129 130

        self.args = args
        server = args.elastic_server or os.getenv('PADDLE_ELASTIC_SERVER')
        name = args.job_id or os.getenv('PADDLE_ELASTIC_JOB_ID')
131
        self.min_np, self.max_np = self._parse_np(args.np)
132 133 134 135
        host = args.host or os.getenv('POD_IP')
        scale = args.scale or int(os.getenv('PADDLE_ELASTIC_SCALE', 0))
        force = args.force or os.getenv('PADDLE_ELASTIC_FORCE')

136 137 138 139 140
        start_port = 6170
        if os.environ.get('FLAGS_START_PORT') is not None:
            start_port = int(os.environ.get('FLAGS_START_PORT'))
        if cloud_utils.use_paddlecloud():
            start_port = int(os.getenv("PADDLE_PORT", ""))
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        (self.device_mode,
         self.devices_per_proc) = launch_utils.get_device_proc_info(args)

        self.elastic_timeout = int(
            os.getenv('PADDLE_ELASTIC_TIMEOUT', ELASTIC_TIMEOUT))
        elastic_ttl = int(os.getenv('PADDLE_ELASTIC_TTL', ELASTIC_TTL))
        self.dist_endpoints = os.getenv('DISTRIBUTED_TRAINER_ENDPOINTS', '')
        self.trainers = os.getenv('PADDLE_TRAINERS', '')
        self.all_host_endpoints = os.getenv('PADDLE_TRAINER_ENDPOINTS',
                                            '').split(",")
        self.np = len(self.all_host_endpoints)
        logger.info(f'start job with np={self.np}')

        #[ "%s:%d" % (ip, start_port) for ip in self.trainers.split(",")]
        logger.info(
            f"trainers={self.trainers}, all_host_endpoints={self.all_host_endpoints}"
        )

        # auto correct the value of elastic_level
        # 1: Fault tolerant, 2: Elastic
162
        self.elastic_level = int(
163 164 165 166 167 168 169 170 171
            os.getenv('PADDLE_ELASTIC_FAULT_TOLERANC_LEVEL',
                      ElasticLevel.FAULT_TOLERANCE))
        if self.min_np == self.max_np or \
                (self.min_np > 0 and self.max_np == 0):
            self.elastic_level = ElasticLevel.FAULT_TOLERANCE
            logger.info(f'start job with ElasticLevel.FAULT_TOLERANCE')
        if self.min_np > 0 and self.max_np > self.min_np:
            self.elastic_level = ElasticLevel.ELASTIC
            logger.info(f'start job with ElasticLevel.ELASTIC')
172

K
kuizhiqing 已提交
173 174 175 176 177 178 179 180
        # compatible with kuberntes service discovery
        if not server and os.getenv(
                'PADDLE_ELASTIC_ETCD_SERVICE_HOST') and os.getenv(
                    'PADDLE_ELASTIC_ETCD_SERVICE_PORT'):
            server = '{}:{}'.format(
                os.getenv('PADDLE_ELASTIC_ETCD_SERVICE_HOST'),
                os.getenv('PADDLE_ELASTIC_ETCD_SERVICE_PORT'))

181 182 183 184 185 186
        logger.debug('init with server {} host {}'.format(server, host))

        self.hosts = []
        self.stopped = False

        self.sigint = 0
K
kuizhiqing 已提交
187
        self.need_sync = False
188

189 190 191
        self.elastic_startup_time = None

        if not server or ':' not in server or not name or not self.np:
192 193
            logger.info(
                'Elastic is not enabled with server {} name {} and np {}'.
194
                format(server, name, self.np))
195 196 197 198 199
            self.enable = False
            return
        else:
            self.enable = True

200
        self.etcd = etcd_client
201
        self.host = host if host else self._get_host()
202
        self.host_port = "%s:%d" % (self.host, start_port)
203 204 205

        # etcd data
        self.prefix = "/paddle/" + name
K
kuizhiqing 已提交
206
        self.node_prefix = self.prefix + '/nodes'
207 208
        self.np_path = self.prefix + '/np'
        self.endpoints_path = self.prefix + '/endpoints'
K
kuizhiqing 已提交
209 210 211 212 213

        node_tag = ''.join(
            random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(6))
        self.host_path = '{}/{}{}'.format(self.node_prefix, node_tag,
                                          time.time())
214 215 216 217 218 219
        '''
        0 group mode, be aware of healthy status of other workers
        1 decouple mode, check own status only
        '''
        self.etcd.put(self.prefix, b'0')

220
        # register callback
221
        def host_call_back(event):
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            self.hosts = [
                six.ensure_str(i[0])
                for i in self.etcd.get_prefix(self.node_prefix)
            ]
            logger.info(
                f"host_call_back curr_host={self.host_port}, hosts:{self.hosts}")
            self.need_sync = True
            self.elastic_startup_time = None

        host_watch = self.etcd.add_watch_prefix_callback(self.node_prefix,
                                                         host_call_back)
        host_lease = self.etcd.lease(elastic_ttl)

        # register etcd lease heartbeat
        def lease_heartbeat():
            while True:
                try:
                    host_lease.refresh()

                    hosts = [
                        six.ensure_str(i[0])
                        for i in self.etcd.get_prefix(self.node_prefix)
                    ]
                    logger.info(
                        f"[lease_heartbeat] curr_host={self.host_port}, hosts={hosts}"
                    )
                    if self.host_port not in hosts:
                        logger.info(
                            f"[lease_heartbeat] register host={self.host_port}")
                        self.etcd.put(self.host_path,
                                      six.b(self.host_port),
                                      lease=host_lease)
                except Exception as e:
                    logger.error("[lease_heartbeat] internal error:{} {}".
                                 format(e, traceback.format_exc()))
                    break
                time.sleep(elastic_ttl / 3)

        keepalived_thread = threading.Thread(
            name='lease_heartbeat', target=lease_heartbeat, daemon=True)
        keepalived_thread.start()

        self.etcd.put(self.host_path, six.b(self.host_port), lease=host_lease)
265 266 267

        # endpoints handle DISTRIBUTED_TRAINER_ENDPOINTS and PADDLE_TRAINERS
        self.etcd.put(self.endpoints_path,
268
                      six.b('{}|{}'.format(self.dist_endpoints, self.trainers)))
269 270

        def endpoints_call_back(event):
271
            if not self.dist_endpoints:
272 273
                return
            edps = six.ensure_str(self.etcd.get(self.endpoints_path)[0] or '')
274
            self.dist_endpoints, self.trainers = edps.split('|')
275
            logger.info("set DISTRIBUTED_TRAINER_ENDPOINTS {} ".format(
276
                self.dist_endpoints))
277 278 279 280 281
            logger.info("set PADDLE_TRAINERS {} ".format(self.trainers))

        endpoints_watch = self.etcd.add_watch_callback(self.endpoints_path,
                                                       endpoints_call_back)

282
        self.watches = [host_watch, endpoints_watch]
K
kuizhiqing 已提交
283 284
        self.launcher = None

285 286 287
    def exit(self, completed=False):
        logger.info('manager exist completed {}'.format(completed))

K
kuizhiqing 已提交
288 289
        if self.launcher:
            self.launcher.stop()
K
kuizhiqing 已提交
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304
        if not self.enable:
            return

        if completed:
            self.etcd.put(self.prefix, b'1')

        for watch in self.watches:
            self.etcd.cancel_watch(watch)
        self.etcd.delete(self.host_path)

        hosts = [i for i in self.etcd.get_prefix(self.node_prefix)]
        if len(hosts) == 0:
            self.etcd.delete_prefix(self.prefix)

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    def _parse_np(self, np: str):
        """
        np format is "MIN" or "MIN:MAX" 
        """
        np_str = np or os.getenv('PADDLE_ELASTIC_NP', "0")
        np_dict = np_str.split(":")
        min_np = max_np = 0
        if len(np_dict) == 1:
            # Fault tolerant
            min_np = int(np_dict[0])
            min_np = 1 if min_np <= 0 else min_np
            max_np = 1
        elif len(np_dict) == 2:
            # Elastic
            min_np = int(np_dict[0])
            max_np = int(np_dict[1])
            min_np = 1 if min_np <= 0 else min_np
            max_np = min_np if min_np > max_np else max_np
        else:
            raise ValueError(
                f'the np={np} needs to be in "MIN" or "MIN:MAX" format')

        return min_np, max_np

329 330 331 332 333 334 335 336 337 338 339 340
    def _get_host(self):
        try:
            return socket.gethostbyname(socket.getfqdn(socket.gethostname()))
        except:
            return '127.0.0.1'

    def _completed(self):
        if not self.enable:
            return True

        return int(self.etcd.get(self.prefix)[0]) == 1

341 342 343
    def _match(self, host_list: list=None):
        if host_list:
            self.hosts = host_list
344
        else:
345 346 347 348
            self.hosts = [
                six.ensure_str(i[0])
                for i in self.etcd.get_prefix(self.node_prefix)
            ]
349

350 351 352 353 354
        if self.elastic_level == ElasticLevel.FAULT_TOLERANCE:
            if len(self.hosts) == self.np:
                return True
            else:
                return False
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        if self.elastic_level == ElasticLevel.ELASTIC:
            # FIXME(xym) add freeze status
            hosts_num = len(self.hosts)
            if hosts_num == self.np:
                return True

            if not self.elastic_startup_time:
                self.elastic_startup_time = time.time()
            if hosts_num == self.max_np:
                self.elastic_startup_time = None
                return True
            elif hosts_num >= self.min_np and hosts_num < self.max_np:
                interval_time = time.time() - self.elastic_startup_time
                if interval_time <= self.elastic_timeout:
                    logger.info(
                        f"wait for timeout, you can set value by PADDLE_ELASTIC_TIMEOUT, \
                        hosts_num={hosts_num}, min_np={self.min_np}, \
                        interval_time={interval_time}, elastic_timeout={self.elastic_timeout}"
                    )
                    return False
                return True
            else:
                self.elastic_startup_time = None
                return False
380

381 382 383 384 385 386 387 388
        return False

    def _update_endpoint(self, endpoints, hosts):
        self.etcd.put(self.endpoints_path,
                      six.b('{}|{}'.format(endpoints, hosts)))

    def _update_hosts(self):
        assert len(self.hosts) != 0, 'hosts empty'
389
        rank = int(os.getenv('PADDLE_TRAINER_ID', -1))
390 391 392 393 394 395 396 397 398 399
        if self.elastic_level == ElasticLevel.FAULT_TOLERANCE:
            if self.host_port in self.dist_endpoints:
                os.environ[
                    'DISTRIBUTED_TRAINER_ENDPOINTS'] = self.dist_endpoints
                os.environ['PADDLE_TRAINERS'] = self.trainers
                logger.info("update env DISTRIBUTED_TRAINER_ENDPOINTS {} ".
                            format(self.dist_endpoints))
                logger.info("update env PADDLE_TRAINERS {} ".format(
                    self.trainers))
                return
400

401 402 403 404 405 406 407 408 409 410 411 412 413
            # fault tolerance 
            idx = self.hosts.index(self.host_port)

            # swap if self.host not in the right position
            if rank >= 0:
                self.hosts[idx] = self.hosts[rank]
                self.hosts[rank] = self.host_port
            else:
                os.environ['PADDLE_TRAINER_ID'] = '{}'.format(idx)
            hosts = ','.join(
                [host_port.split(":")[0] for host_port in self.hosts])
            self.args.ips = hosts
            os.environ['PADDLE_TRAINERS'] = hosts
414
        else:
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
            # elastic, scale up/down
            endpoints = copy.deepcopy(self.all_host_endpoints)
            if len(self.hosts) > self.np:
                # scale up
                logger.info(
                    f"elastic scale up, from {self.np} to {len(self.hosts)}, hosts={self.hosts}, endpoints={endpoints}"
                )

                for curr_host_port in self.hosts:
                    if curr_host_port not in endpoints:
                        endpoints.append(curr_host_port)

                os.environ['PADDLE_TRAINER_ID'] = '{}'.format(
                    endpoints.index(self.host_port))
                hosts = ','.join(
                    [host_port.split(":")[0] for host_port in endpoints])
                self.args.ips = hosts
                os.environ['PADDLE_TRAINERS'] = hosts
                self.np = len(endpoints)
                os.environ['PADDLE_TRAINER_ENDPOINTS'] = ','.join(endpoints)
                os.environ[
                    'DISTRIBUTED_TRAINER_ENDPOINTS'] = self.dist_endpoints
                self.all_host_endpoints = endpoints
            else:
                # scale down
                logger.info(
                    f"elastic scale down, from {len(self.hosts)} to {self.np}, hosts={self.hosts}, endpoints={endpoints}"
                )

                # If the shrink node is from the first of the rank list, you need to minimize the movement of the rank
                # eg: 
                #   the source trainers is:10.10.10.0,10.10.10.1,10.10.10.2,10.10.10.3
                #   10.10.10.0 is removed
                #   the new trainers is:10.10.10.3,10.10.10.1,10.10.10.2
                #   In this case, the rank of 10.10.10.1 and 10.10.10.2 remains unchanged, while the rank of 10.10.10.3 is set to rank0
                endpoints_dict = dict()
                unsorted_endpoints = []
                for id, host_port in enumerate(self.hosts):
                    idx = endpoints.index(host_port)
                    if idx <= len(self.hosts) - 1 and not endpoints_dict.get(
                            idx):
                        endpoints_dict[idx] = host_port
                    else:
                        unsorted_endpoints.append(host_port)

                idle_index = 0
                sorted_endpoints = []
                for idx in range(len(self.hosts)):
                    if not endpoints_dict.get(idx) and len(
                            unsorted_endpoints) > 0:
                        endpoints_dict[idx] = unsorted_endpoints[idle_index]
                        idle_index += 1

                    sorted_endpoints.append(endpoints_dict.get(idx))

                logger.info(
                    f"elastic scale down, sorted_endpoints={sorted_endpoints}")
                self.all_host_endpoints = sorted_endpoints

                endpoint_list = []
                ip_list = []
                for host_port in sorted_endpoints:
                    host_port_list = host_port.split(":")
                    ip = host_port_list[0]
                    port = int(host_port_list[1])

                    ip_list.append(ip)
                    ports = [
                        x
                        for x in range(port, port + len(self.devices_per_proc))
                    ]
                    endpoint_list.extend(
                        ["%s:%d" % (ip, port) for port in ports])

                hosts = ','.join(ip_list)
                new_endpoints = ','.join(endpoint_list)

                self.args.ips = hosts
                os.environ['PADDLE_TRAINER_ID'] = '{}'.format(
                    sorted_endpoints.index(self.host_port))
                os.environ['PADDLE_TRAINERS'] = hosts
                self.np = len(sorted_endpoints)
                os.environ['PADDLE_TRAINER_ENDPOINTS'] = ','.join(
                    sorted_endpoints)
                os.environ['DISTRIBUTED_TRAINER_ENDPOINTS'] = new_endpoints
                self._update_endpoint(new_endpoints, hosts)
501 502 503 504 505

    def wait(self):
        if not self.enable:
            return

K
kuizhiqing 已提交
506
        idx = 1
507 508 509 510 511 512 513
        while not self.stopped:
            if self._match():
                logger.info('ready with hosts {}'.format(self.hosts))
                self._update_hosts()
                return
            logger.info('not ready for np {} with hosts {}'.format(self.np,
                                                                   self.hosts))
K
kuizhiqing 已提交
514
            idx += 1
K
kuizhiqing 已提交
515 516
            time.sleep(2)

517 518 519 520 521 522 523 524 525 526 527
        return

    def run(self, launcher):
        if self.stopped:
            return

        self.launcher = launcher(self.args)
        self.launcher.launch()

    def watch(self):

K
kuizhiqing 已提交
528 529 530
        if self.need_sync:
            self.need_sync = False

531 532
        while not self.stopped:
            ret = self.launcher.watch()
533
            logger.debug(f"launcher.watch():{ret}")
534 535 536 537 538 539 540 541

            if ret is not None:  # self terminated
                logger.info('job exit with code {}'.format(ret))
                # process is completed if ret >= 0 or error else
                completed = True if ret == 0 else False
                self.exit(completed=completed)
                if completed:
                    return ElasticStatus.COMPLETED
542
                if self.elastic_level == ElasticLevel.FAULT_TOLERANCE:
543 544 545 546
                    return ElasticStatus.RESTART
                else:
                    return ElasticStatus.ERROR

K
kuizhiqing 已提交
547
            if not self._completed() and (not self._match() or self.need_sync):
548 549 550
                self.launcher.stop()
                return ElasticStatus.HOLD

K
kuizhiqing 已提交
551
            time.sleep(2)
552

K
kuizhiqing 已提交
553 554
        if self.launcher:
            self.launcher.stop()
555

556 557 558 559 560 561 562
        return ElasticStatus.EXIT

    def signal_handler(self, sigint, frame):
        if self.enable:
            self.exit()
        self.sigint = sigint
        self.stopped = True