launch_utils.py 16.3 KB
Newer Older
R
Roc 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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.

15
import copy
16 17
import os
import signal
18
import socket
19
import subprocess
20 21
import sys
import time
22
from contextlib import closing
J
Jiangxinz 已提交
23
from distutils.util import strtobool
24

R
Roc 已提交
25
from paddle.distributed.fleet.launch_utils import get_backend_by_compile_flag
26

R
Roc 已提交
27
from ..utils.log_utils import get_logger
28

R
Roc 已提交
29
logger = get_logger("INFO", "root")
30 31


32 33 34 35 36
def get_cluster_from_args(args, selected_gpus):
    node_ips = [x.strip() for x in args.cluster_node_ips.split(',')]
    node_ip = args.node_ip
    node_rank = node_ips.index(node_ip)

37 38 39 40 41
    logger.debug(
        "parsed from args:node_ips:{} node_ip:{} node_rank:{}".format(
            node_ips, node_ip, node_rank
        )
    )
42 43

    free_ports = None
44 45 46 47 48
    if (
        not args.use_paddlecloud
        and len(node_ips) <= 1
        and args.started_port is None
    ):
49 50 51 52 53 54 55 56
        free_ports = find_free_ports(len(selected_gpus))
        if free_ports is not None:
            free_ports = list(free_ports)
    else:
        started_port = 6070
        if args.started_port is not None:
            started_port = args.started_port

57 58 59
        free_ports = list(
            range(started_port, started_port + len(selected_gpus))
        )
60 61 62 63 64 65 66 67 68

    trainer_endpoints = []
    for ip in node_ips:
        trainer_endpoints.append(["%s:%d" % (ip, port) for port in free_ports])
    return get_cluster(node_ips, node_ip, trainer_endpoints, selected_gpus)


def get_gpus(selected_gpus):
    if selected_gpus is None:
W
wuhuachaocoding 已提交
69
        from paddle.framework import core
70

71 72 73 74 75 76 77 78 79 80 81 82
        gpus_num = core.get_cuda_device_count()
        gpus = [str(x) for x in range(0, gpus_num)]
    else:
        cuda_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES")
        if cuda_visible_devices is None or cuda_visible_devices == "":
            gpus = [x.strip() for x in selected_gpus.split(',')]
        else:
            # change selected_gpus into relative values
            # e.g. CUDA_VISIBLE_DEVICES=4,5,6,7; args.selected_gpus=4,5,6,7;
            # therefore selected_gpus=0,1,2,3
            cuda_visible_devices_list = cuda_visible_devices.split(',')
            for x in selected_gpus.split(','):
83 84 85 86 87
                assert x in cuda_visible_devices_list, (
                    "Can't find "
                    "your selected_gpus %s in CUDA_VISIBLE_DEVICES[%s]."
                    % (x, cuda_visible_devices)
                )
88 89 90 91
            gpus = [
                cuda_visible_devices_list.index(x.strip())
                for x in selected_gpus.split(',')
            ]
92 93 94 95 96 97 98
            logger.info(
                "Change selected_gpus into reletive values. --ips:{} "
                "will change into relative_ips:{} according to your "
                "CUDA_VISIBLE_DEVICES:{}".format(
                    selected_gpus, gpus, cuda_visible_devices_list
                )
            )
99 100 101 102

    return gpus


103
class Hdfs:
104 105 106 107 108 109
    def __init__(self):
        self.hdfs_ugi = None
        self.hdfs_name = None
        self.hdfs_path = None

    def is_valid(self):
110 111 112 113 114
        return (
            self.hdfs_ugi is not None
            and self.hdfs_name is not None
            and self.hdfs_path is not None
        )
115 116 117

    def __str__(self):
        return "hdfs_ugi:{} hdfs_name:{} hdfs_path{}".format(
118 119
            self.hdfs_ugi, self.hdfs_name, self.hdfs_path
        )
120 121

    def __eq__(self, n):
122 123 124 125 126
        return (
            self.hdfs_ugi == n.hdfs_ugi
            and self.hdfs_name == n.hdfs_name
            and self.hdfs_path == n.hdfs_path
        )
127 128 129 130 131

    def __ne__(self, n):
        return not self == n


132
class Cluster:
133 134 135 136 137 138 139 140
    def __init__(self, hdfs):
        self.job_server = None
        self.pods = []
        self.hdfs = None
        self.job_stage_flag = None

    def __str__(self):
        return "job_server:{} pods:{} job_stage_flag:{} hdfs:{}".format(
141 142 143 144 145
            self.job_server,
            [str(pod) for pod in self.pods],
            self.job_stage_flag,
            self.hdfs,
        )
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    def __eq__(self, cluster):
        if len(self.pods) != len(cluster.pods):
            return False

        for a, b in zip(self.pods, cluster.pods):
            if a != b:
                return False

        if self.job_stage_flag != cluster.job_stage_flag:
            return False

        return True

    def __ne__(self, cluster):
        return not self.__eq__(cluster)

163
    def update_pods(self, cluster):
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        self.pods = copy.copy(cluster.pods)

    def trainers_nranks(self):
        return len(self.trainers_endpoints())

    def pods_nranks(self):
        return len(self.pods)

    def trainers_endpoints(self):
        r = []
        for pod in self.pods:
            for t in pod.trainers:
                r.append(t.endpoint)
        return r

    def pods_endpoints(self):
        r = []
        for pod in self.pods:
182
            ep = f"{pod.addr}:{pod.port}"
183
            assert (
184
                pod.port is not None and pod.addr is not None
185
            ), f"{ep} not a valid endpoint"
186 187 188 189 190 191 192 193 194 195 196 197
            r.append(ep)

        return r

    def get_pod_by_id(self, pod_id):
        for pod in self.pods:
            if str(pod_id) == str(pod.id):
                return pod

        return None


198
class JobServer:
199 200 201 202
    def __init__(self):
        self.endpoint = None

    def __str__(self):
203
        return f"{self.endpoint}"
204 205 206 207 208 209 210 211

    def __eq__(self, j):
        return self.endpint == j.endpoint

    def __ne__(self, j):
        return not self == j


212
class Trainer:
213 214 215 216 217 218
    def __init__(self):
        self.gpus = []
        self.endpoint = None
        self.rank = None

    def __str__(self):
219 220 221
        return "gpu:{} endpoint:{} rank:{}".format(
            self.gpus, self.endpoint, self.rank
        )
222 223 224 225 226

    def __eq__(self, t):
        if len(self.gpus) != len(t.gpus):
            return False

227
        if self.endpoint != t.endpoint or self.rank != t.rank:
228 229 230 231 232 233 234 235 236 237 238
            return False

        for a, b in zip(self.gpus, t.gpus):
            if a != b:
                return False

        return True

    def __ne__(self, t):
        return not self == t

239
    def get_rank(self):
240 241 242
        return self.rank


243
class Pod:
244 245 246 247 248 249 250 251 252
    def __init__(self):
        self.rank = None
        self.id = None
        self.addr = None
        self.port = None
        self.trainers = []
        self.gpus = []

    def __str__(self):
253 254 255 256 257 258 259 260 261 262
        return (
            "rank:{} id:{} addr:{} port:{} visible_gpu:{} trainers:{}".format(
                self.rank,
                self.id,
                self.addr,
                self.port,
                self.gpus,
                [str(t) for t in self.trainers],
            )
        )
263 264

    def __eq__(self, pod):
265 266 267 268 269 270
        if (
            self.rank != pod.rank
            or self.id != pod.id
            or self.addr != pod.addr
            or self.port != pod.port
        ):
271
            logger.debug(f"pod {self} != {pod}")
272 273 274
            return False

        if len(self.trainers) != len(pod.trainers):
275
            logger.debug(f"trainers {self.trainers} != {pod.trainers}")
276 277 278 279
            return False

        for i in range(len(self.trainers)):
            if self.trainers[i] != pod.trainers[i]:
280
                logger.debug(f"trainer {self.trainers[i]} != {pod.trainers[i]}")
281 282 283 284 285 286 287 288 289 290 291 292 293
                return False

        return True

    def __ne__(self, pod):
        return not self == pod

    def parse_response(self, res_pods):
        pass

    def get_visible_gpus(self):
        r = ""
        for g in self.gpus:
294
            r += f"{g},"
295

296
        assert r != "", f"this pod {self} can't see any gpus"
297 298 299 300 301

        r = r[:-1]
        return r


302 303
def get_cluster(node_ips, node_ip, trainer_endpoints, selected_gpus):
    assert type(trainer_endpoints) is list, "trainer_endpoints must be list"
304 305 306 307 308 309
    cluster = Cluster(hdfs=None)
    trainer_rank = 0
    for node_rank, ip in enumerate(node_ips):
        pod = Pod()
        pod.rank = node_rank
        pod.addr = ip
310 311 312 313 314
        cur_node_endpoints = trainer_endpoints[node_rank]
        # when use paddlecloud, endpoints may > selected_gpus(user_defined)
        assert len(cur_node_endpoints) >= len(
            selected_gpus
        ), "current trainer_endpoints size should be greater equal than selected_gpus size."
315 316 317
        for i in range(len(selected_gpus)):
            trainer = Trainer()
            trainer.gpus.append(selected_gpus[i])
318
            trainer.endpoint = "%s" % (cur_node_endpoints[i])
319 320 321 322 323 324 325 326 327 328 329 330 331
            trainer.rank = trainer_rank
            trainer_rank += 1

            pod.trainers.append(trainer)
        cluster.pods.append(pod)

    pod_rank = node_ips.index(node_ip)
    return cluster, cluster.pods[pod_rank]


def terminate_local_procs(procs):
    for p in procs:
        if p.proc.poll() is None:
M
mapingshuo 已提交
332
            p.proc.terminate()
333 334
            if p.log_fn:
                p.log_fn.close()
335
            logger.debug(f"terminate process id:{p.proc.pid}")
336

337
    # wait all process terminiated
M
mapingshuo 已提交
338
    time.sleep(3)
339 340 341 342 343 344 345 346 347 348 349 350 351 352
    for step in range(0, 50):
        alive = False
        for p in procs:
            if p.proc.poll() is None:  # not termniate
                os.kill(p.proc.pid, signal.SIGKILL)
                alive = True

        if not alive:
            logger.info("terminate all the procs")
            return

        time.sleep(3)

    logger.fatal("can't kill all process and exit")
353
    sys.exit(1)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372


def get_host_name_ip():
    try:
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        return host_name, host_ip
    except:
        return None


def add_arguments(argname, type, default, help, argparser, **kwargs):
    """Add argparse's argument.
    Usage:
    .. code-block:: python
        parser = argparse.ArgumentParser()
        add_argument("name", str, "Jonh", "User name.", parser)
        args = parser.parse_args()
    """
J
Jiangxinz 已提交
373
    type = strtobool if type == bool else type
374 375 376 377 378
    argparser.add_argument(
        "--" + argname,
        default=default,
        type=type,
        help=help + ' Default: %(default)s.',
379
        **kwargs,
380
    )
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408


def find_free_ports(num):
    def __free_port():
        with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
            s.bind(('', 0))
            return s.getsockname()[1]

    port_set = set()
    step = 0
    while True:
        port = __free_port()
        if port not in port_set:
            port_set.add(port)

        if len(port_set) >= num:
            return port_set

        step += 1
        if step > 100:
            print(
                "can't find avilable port and use the specified static port now!"
            )
            return None

    return None


X
xiongkun 已提交
409 410 411 412
def _prepare_trainer_env(cluster, trainer, backend=None):
    if backend is None:
        backend = get_backend_by_compile_flag()  # for compatibility
    if backend == 'bkcl':
413
        proc_env = {
414 415
            "FLAGS_selected_xpus": "%s"
            % ",".join([str(g) for g in trainer.gpus]),
416 417 418
            "PADDLE_TRAINER_ID": "%d" % trainer.rank,
            "PADDLE_CURRENT_ENDPOINT": "%s" % trainer.endpoint,
            "PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(),
419
            "PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()),
420
        }
X
xiongkun 已提交
421
    elif backend == 'nccl':
422
        proc_env = {
423 424
            "FLAGS_selected_gpus": "%s"
            % ",".join([str(g) for g in trainer.gpus]),
425 426 427
            "PADDLE_TRAINER_ID": "%d" % trainer.rank,
            "PADDLE_CURRENT_ENDPOINT": "%s" % trainer.endpoint,
            "PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(),
428
            "PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()),
429 430 431
        }
    elif backend == 'cncl':
        proc_env = {
432 433
            "FLAGS_selected_mlus": "%s"
            % ",".join([str(g) for g in trainer.gpus]),
434 435 436
            "PADDLE_TRAINER_ID": "%d" % trainer.rank,
            "PADDLE_CURRENT_ENDPOINT": "%s" % trainer.endpoint,
            "PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(),
437
            "PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()),
438
        }
X
xiongkun 已提交
439 440 441 442 443 444 445
    elif backend == 'gloo':
        # NOTE (xiongkun) default fall back into cpu only
        proc_env = {
            "PADDLE_TRAINER_ID": "%d" % trainer.rank,
            "PADDLE_CURRENT_ENDPOINT": "%s" % trainer.endpoint,
            "PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(),
            "PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()),
446
            "PADDLE_DISTRI_BACKEND": backend,  # only add here, other will be auto
X
xiongkun 已提交
447 448 449 450
        }
    else:
        raise ValueError("backend must be one of 'gloo, nccl, bkcl'")

451 452 453
    return proc_env


454
class TrainerProc:
455 456 457
    def __init__(self):
        self.proc = None
        self.log_fn = None
458
        self.log_offset = None
459
        self.rank = None
460
        self.local_rank = None
461 462 463
        self.cmd = None


464 465 466
def start_local_trainers(
    cluster, pod, training_script, training_script_args, log_dir=None
):
467
    current_env = copy.copy(os.environ.copy())
468 469 470 471
    # paddle broadcast ncclUniqueId use socket, and
    # proxy maybe make trainers unreachable, so delete them.
    # if we set them to "", grpc will log error message "bad uri"
    # so just delete them.
472 473 474 475 476
    current_env.pop("http_proxy", None)
    current_env.pop("https_proxy", None)

    procs = []
    for idx, t in enumerate(pod.trainers):
477
        proc_env = _prepare_trainer_env(cluster, t)
478 479
        current_env.update(proc_env)

480
        logger.debug(f"trainer proc env:{current_env}")
481 482 483

        cmd = [sys.executable, "-u", training_script] + training_script_args

484
        logger.info(f"start trainer proc:{cmd} env:{proc_env}")
485 486 487

        fn = None
        if log_dir is not None:
488
            os.system(f"mkdir -p {log_dir}")
489
            fn = open("%s/workerlog.%d" % (log_dir, idx), "a")
490
            proc = subprocess.Popen(cmd, env=current_env, stdout=fn, stderr=fn)
491 492 493 494 495 496
        else:
            proc = subprocess.Popen(cmd, env=current_env)

        tp = TrainerProc()
        tp.proc = proc
        tp.rank = t.rank
497
        tp.local_rank = idx
498
        tp.log_fn = fn
499
        tp.log_offset = fn.tell() if fn else None
500 501 502 503 504 505 506
        tp.cmd = cmd

        procs.append(tp)

    return procs


507 508 509 510 511 512 513 514 515 516
def pull_worker_log(tp):
    if tp.log_fn:
        with open(tp.log_fn.name, 'r') as fin:
            fin.seek(tp.log_offset, 0)
            for line in fin:
                try:
                    sys.stdout.write(line)
                except UnicodeEncodeError:
                    sys.stdout.write(
                        'UnicodeEncodeError occurs at this line. '
517 518 519
                        'Please refer to the original log file "%s"\n'
                        % tp.log_fn.name
                    )
520 521 522
            tp.log_offset = fin.tell()


523 524 525 526 527 528 529
def watch_local_trainers(procs, nranks):
    try:
        error = False
        error_rank = []
        # wait all process finish or one error
        alive = False
        for p in procs:
530 531 532
            if p.log_fn and p.local_rank == 0:
                pull_worker_log(p)

533 534 535 536 537 538 539 540 541
            ret = p.proc.poll()
            if ret is None:
                alive = True
            elif ret != 0:
                error = True
                error_rank.append(p.rank)

        if error:
            terminate_local_procs(procs)
542
            sys.exit(1)
543 544 545 546 547 548 549

    except KeyboardInterrupt:
        logger.warning("KeyboardInterrupt, exit")
        terminate_local_procs(procs)
        raise
    except SystemExit:
        logger.error(
550 551 552 553
            "ABORT!!! Out of all {} trainers, the trainer process with rank={} was aborted. Please check its log.".format(
                nranks, error_rank
            )
        )
554 555 556 557
        terminate_local_procs(procs)
        raise
    except:
        logger.error(
558 559 560 561
            "ABORT!!! Out of all {} trainers, the trainer process with rank={} was aborted. Please check its log.".format(
                nranks, error_rank
            )
        )
562 563 564 565
        terminate_local_procs(procs)
        raise

    return alive
R
Roc 已提交
566 567 568 569


def _print_arguments(args):
    print("-----------  Configuration Arguments -----------")
570
    for arg, value in sorted(vars(args).items()):
571
        print(f"{arg}: {value}")
R
Roc 已提交
572
    print("------------------------------------------------")