util_factory.py 25.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   Copyright (c) 2020 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.
"""Fleet Utils."""
"""distributed operations"""
"""basic collective operations in python"""
"""remote file system"""

19
from ..utils.fs import FS, LocalFS, HDFSClient
20 21 22 23 24 25 26 27 28 29
from paddle.fluid.proto import framework_pb2
from paddle.fluid.framework import Program
from paddle.fluid import debugger
from google.protobuf import text_format
import paddle.fluid as fluid
from collections import OrderedDict
from paddle.fluid import core
import subprocess
import os
import numpy as np
30 31

__all__ = []
32

33 34

class UtilFactory(object):
35

36
    def _create_util(self, context=None):
37
        util = UtilBase()
38 39 40 41
        if context is not None and "valid_strategy" in context:
            util._set_strategy(context["valid_strategy"])
        if context is not None and "role_maker" in context:
            util._set_role_maker(context["role_maker"])
42 43 44
        return util


45
class UtilBase(object):
46

47 48 49 50 51 52 53 54 55
    def __init__(self):
        self.role_maker = None
        self.dist_strategy = None

    def _set_strategy(self, dist_strategy):
        self.dist_strategy = dist_strategy

    def _set_role_maker(self, role_maker):
        self.role_maker = role_maker
56

57
    def _set_file_system(self, fs_client):
58
        assert isinstance(
59 60
            fs_client, FS
        ), "fs_client must be the instance of paddle.distributed.fleet.utils.FS"
61 62
        self.fs_client = fs_client

63
    def all_reduce(self, input, mode="sum", comm_world="worker"):
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        """
        All reduce `input` between specified collection. This is a distributed API.

        Args:
            input (list|numpy.array): The input variable to do all_reduce between specified collection.
            mode (str): "sum" or "min" or "max".
            comm_world (str, optional): Collection used to execute all_reduce operation. Supported collections incude `worker` , `server` and `all` . The default is `worker` .

        Returns:
            output(Numpy.array|None): A numpy array with the same shape as the `input` .

        Examples:
            .. code-block:: python

                # Save the following code in `train.py` , and then execute the command `fleetrun --server_num 2 --worker_num 2 train.py` .
                import paddle.distributed.fleet as fleet
                from paddle.distributed.fleet import PaddleCloudRoleMaker
                import sys
                import numpy as np
83 84 85
                import os

                os.environ["PADDLE_WITH_GLOO"] = "2"
86 87 88 89 90 91 92 93 94 95

                def train():
                    role = PaddleCloudRoleMaker(
                        is_collective=False,
                        init_gloo=True,
                        path="./tmp_gloo")
                    fleet.init(role)

                    if fleet.is_server():
                        input = [1, 2]
96
                        output = fleet.util.all_reduce(input, "sum", "server")
97 98 99 100
                        print(output)
                        # [2, 4]
                    elif fleet.is_worker():
                        input = np.array([3, 4])
101
                        output = fleet.util.all_reduce(input, "sum", "worker")
102 103
                        print(output)
                        # [6, 8]
104
                    output = fleet.util.all_reduce(input, "sum", "all")
105 106 107 108 109
                    print(output)
                    # [8, 12]
                if __name__ == "__main__":
                    train()
        """
110
        return self.role_maker._all_reduce(input, mode, comm_world)
111 112

    def barrier(self, comm_world="worker"):
113 114 115 116 117 118 119 120 121
        """
        Barrier between specified collection.

        Args:
            comm_world (str, optional): Collection used to execute barrier operation. Supported collections incude `worker` , `server` and `all` . The default is `worker` .

        Examples:

            .. code-block:: python
122

123 124 125 126 127
                # Save the following code in `train.py` , and then execute the command `fleetrun --server_num 2 --worker_num 2 train.py` .

                import paddle.distributed.fleet as fleet
                from paddle.distributed.fleet import PaddleCloudRoleMaker
                import sys
128 129 130
                import os

                os.environ["PADDLE_WITH_GLOO"] = "2"
131 132 133 134 135 136 137 138 139

                def train():
                    role = PaddleCloudRoleMaker(
                        is_collective=False,
                        init_gloo=True,
                        path="./tmp_gloo")
                    fleet.init(role)

                    if fleet.is_server():
140
                        fleet.util.barrier("server")
141 142
                        print("all server arrive here")
                    elif fleet.is_worker():
143
                        fleet.util.barrier("worker")
144
                        print("all server arrive here")
145
                    fleet.util.barrier("all")
146 147 148 149 150
                    print("all servers and workers arrive here")

                if __name__ == "__main__":
                    train()
        """
151
        self.role_maker._barrier(comm_world)
152 153

    def all_gather(self, input, comm_world="worker"):
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        """
        All gather `input` between specified collection.

        Args:
            input (Int|Float): The input variable to do all_gather between specified collection.
            comm_world (str, optional): Collection used to execute all_reduce operation. Supported collections incude `worker` , `server` and `all` . The default is `worker` .

        Returns:
            output (List): A list of gathered values.

        Examples:

            .. code-block:: python

                # Save the following code in `train.py` , and then execute the command `fleetrun --server_num 2 --worker_num 2 train.py` .
                import paddle.distributed.fleet as fleet
                from paddle.distributed.fleet import PaddleCloudRoleMaker
                import sys
172 173 174
                import os

                os.environ["PADDLE_WITH_GLOO"] = "2"
175 176 177 178 179 180 181 182 183 184

                def train():
                    role = PaddleCloudRoleMaker(
                        is_collective=False,
                        init_gloo=True,
                        path="./tmp_gloo")
                    fleet.init(role)

                    if fleet.is_server():
                        input = fleet.server_index()
185
                        output = fleet.util.all_gather(input, "server")
186 187 188 189
                        print(output)
                        # output = [0, 1]
                    elif fleet.is_worker():
                        input = fleet.worker_index()
190
                        output = fleet.util.all_gather(input, "worker")
191 192
                        # output = [0, 1]
                        print(output)
193
                    output = fleet.util.all_gather(input, "all")
194 195 196 197 198 199
                    print(output)
                    # output = [0, 1, 0, 1]

                if __name__ == "__main__":
                    train()
        """
200 201

        return self.role_maker._all_gather(input, comm_world)
202

203
    def _broadcast(self):
204 205
        pass

206
    def _scatter(self):
207 208
        pass

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    def get_heter_file_shard(self, files):
        if not isinstance(files, list):
            raise TypeError("files should be a list of file need to be read.")
        trainers = self.role_maker._worker_num()
        trainer_id = self.role_maker._worker_index() - trainers
        remainder = len(files) % trainers
        blocksize = int(len(files) / trainers)

        blocks = [blocksize] * trainers
        for i in range(remainder):
            blocks[i] += 1

        trainer_files = [[]] * trainers
        begin = 0
        for i in range(trainers):
            trainer_files[i] = files[begin:begin + blocks[i]]
            begin += blocks[i]

        return trainer_files[trainer_id]

229
    def get_file_shard(self, files):
230
        """
231 232 233 234 235 236 237 238
        Split files before distributed training, and return filelist assigned to the current trainer.

        .. code-block:: text

            example 1: files is [a, b, c ,d, e]  and trainer_num = 2, then trainer
                    0 gets [a, b, c] and trainer 1 gets [d, e].
            example 2: files is [a, b], and trainer_num = 3, then trainer 0 gets
                    [a], trainer 1 gets [b],  trainer 2 gets []
239

240
        Args:
241
            files(list): File list need to be read.
242

243
        Returns:
244 245 246 247 248 249
            List: Files belong to this worker.

        Examples:

            .. code-block:: python

250 251
                import paddle.distributed.fleet as fleet
                from paddle.distributed.fleet import UserDefinedRoleMaker
252

253
                role = UserDefinedRoleMaker(
254 255 256
                    is_collective=False,
                    init_gloo=False,
                    current_id=0,
257
                    role=fleet.Role.WORKER,
258 259
                    worker_endpoints=["127.0.0.1:6003", "127.0.0.1:6004"],
                    server_endpoints=["127.0.0.1:6001", "127.0.0.1:6002"])
260 261 262 263
                fleet.init(role)

                files = fleet.util.get_file_shard(["file1", "file2", "file3"])
                print(files)
264
                # files = ["file1", "file2"]
265 266 267
        """
        if not isinstance(files, list):
            raise TypeError("files should be a list of file need to be read.")
268

269 270
        trainer_id = self.role_maker._worker_index()
        trainers = self.role_maker._worker_num()
271

272 273
        remainder = len(files) % trainers
        blocksize = int(len(files) / trainers)
274

275 276 277
        blocks = [blocksize] * trainers
        for i in range(remainder):
            blocks[i] += 1
278

279 280 281 282 283 284 285 286 287
        trainer_files = [[]] * trainers
        begin = 0
        for i in range(trainers):
            trainer_files[i] = files[begin:begin + blocks[i]]
            begin += blocks[i]

        return trainer_files[trainer_id]

    def print_on_rank(self, message, rank_id):
288 289 290 291 292 293 294 295 296 297 298
        """
        Woker of rank `rank_id` print some message. 

        Args:
            message(str): Log to be printed.
            rank_id(int): trainer id.

        Examples:

            .. code-block:: python

299 300
                import paddle.distributed.fleet as fleet
                from paddle.distributed.fleet import UserDefinedRoleMaker
301

302
                role = UserDefinedRoleMaker(
303 304 305
                    is_collective=False,
                    init_gloo=False,
                    current_id=0,
306
                    role=fleet.Role.WORKER,
307 308
                    worker_endpoints=["127.0.0.1:6003", "127.0.0.1:6004"],
                    server_endpoints=["127.0.0.1:6001", "127.0.0.1:6002"])
309 310 311
                fleet.init(role)

                fleet.util.print_on_rank("I'm worker 0", 0)
312
        """
313
        if self.role_maker._worker_index() != rank_id:
314 315 316 317 318 319 320 321 322 323 324 325
            return
        print(message)

    def _save_program(self, program, model_filename='__model__', is_text=False):
        if is_text:
            with open(model_filename, "w") as f:
                f.write(str(program))
        else:
            with open(model_filename, "wb") as f:
                f.write(program.desc.serialize_to_string())

    def _load_program(self, path, is_text):
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        def load_program_binary(path):
            """load program from binary string file"""
            with open(path, "rb") as f:
                program_desc_str = f.read()
            return Program.parse_from_string(program_desc_str)

        def load_program_text(path):
            """load program from human-readable text file"""
            with open(path, "r") as f:
                program_desc_text = f.read()

            prog_desc = framework_pb2.ProgramDesc()
            text_format.Merge(program_desc_text, prog_desc)
            return Program.parse_from_string(prog_desc.SerializeToString())

        if is_text:
            return load_program_text(path)
        else:
            return load_program_binary(path)

    def _program_type_trans(self, prog_dir, prog_fn, is_text):
        prog = self._load_program(os.path.join(prog_dir, prog_fn), is_text)
        prog_out_fn = prog_fn + ".bin" if is_text else prog_fn + ".pbtxt"
350 351
        self._save_program(prog, os.path.join(prog_dir, prog_out_fn),
                           1 - is_text)
352 353 354 355 356 357 358 359
        return prog_out_fn

    def _visualize_graphviz(self, program, output_dir, output_filename):
        block = program.global_block()
        dot_path = os.path.join(output_dir, output_filename + '.dot')
        pdf_path = os.path.join(output_dir, output_filename + '.pdf')
        debugger.draw_block_graphviz(block, path=dot_path)
        cmd = ["dot", "-Tpdf", dot_path, "-o", pdf_path]
360 361 362 363
        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        p.wait()

    def _proto_check(self, config):
        train_prog = self._load_program(config.train_prog_path,
                                        config.is_text_train_program)
        pruned_prog = self._load_program(config.pruned_prog_path,
                                         config.is_text_pruned_program)

        is_match = True

        pruned_vars = [(v.name, v) for v in pruned_prog.list_vars()
                       if fluid.io.is_persistable(v)]
        pruned_vars = OrderedDict(pruned_vars)
        pruned_vars_name = [name for name in pruned_vars]
        print("persistable vars in pruned program: {}".format(pruned_vars_name))

        # feed and fetch op is added in pruned program when pruning, not need to be found in train program
        feed_fetch_type_list = [
            core.VarDesc.VarType.FEED_MINIBATCH, core.VarDesc.VarType.FETCH_LIST
        ]

        for var_name in pruned_vars:
            var = pruned_vars[var_name]
            # feed and fetch op is added in pruned program when pruning, not need to be found in train program
            if var.type in feed_fetch_type_list:
                break
            try:
                train_prog_var = train_prog.global_block().var(var_name)
            except ValueError as e:
                print(
                    "Not find variable '%s' in train program. please check pruning."
                    % var_name)
                is_match = False
                continue
            if var.shape != train_prog_var.shape or var.dtype != train_prog_var.dtype:
                print(
400 401 402
                    "variable: {} not match. in pruned program shape: {} dtype:{}, in train program shape: {} dtype: {}"
                    .format(var_name, var.shape, var.dtype,
                            train_prog_var.shape, train_prog_var.dtype))
403 404 405 406
                is_match = False
        return is_match

    def _params_check(self, config):
407

408
        def feed_gen(batch_size, feeded_vars_dims, feeded_vars_filelist):
409

410 411 412 413 414 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
            def reader(batch_size, fn, dim):
                data = []
                if isinstance(dim, list) or isinstance(dim, tuple):
                    shape = list(dim)
                    _temp = 1
                    for x in dim:
                        _temp = _temp * x
                    dim = _temp
                else:
                    shape = [dim]

                shape = [batch_size] + shape
                dim = dim * batch_size

                for line in open(fn, 'r'):
                    fields = line.strip().split(' ')
                    fields = [float(d) for d in fields]
                    while len(fields) >= dim:
                        tmp = fields[:dim]
                        fields = fields[dim:]
                        data.append(np.array(tmp).reshape(shape))
                return data

            batch_feed = []
            for i, fn in enumerate(feeded_vars_filelist):
                batch_feed.append(reader(batch_size, fn, feeded_vars_dims[i]))
            return batch_feed

        prog = self._load_program(
            os.path.join(config.dump_model_dir, config.dump_program_filename),
            config.is_text_dump_program)
        if config.is_text_dump_program:
            model_filename = self._program_type_trans(
                config.dump_model_dir, config.dump_program_filename,
                config.is_text_dump_program)

        saved_params = [
            v for v in prog.list_vars() if fluid.io.is_persistable(v)
        ]
        print("persistable vars in dump program: {}".format(
            [v.name for v in saved_params]))

        def check_not_expected_ops(prog, not_expected_op_types):
            op_types_set = set()
            for op in prog.global_block().ops:
                if op.type in not_expected_op_types and op.type not in op_types_set:
                    op_types_set.add(op.type)
            return op_types_set

        not_expected_op_types = check_not_expected_ops(prog, ["lookup_table"])
        if len(not_expected_op_types) > 0:
            print(
462 463
                "find op type '{}' in program, please check if your program is pruned correctly !"
                .format(list(not_expected_op_types)))
464 465 466 467 468 469 470 471
            return False

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        scope = fluid.core.Scope()
        with fluid.scope_guard(scope):
            inference_program, feed_target_names, fetch_targets = \
                fluid.io.load_inference_model(config.dump_model_dir, exe, model_filename=model_filename,
472
                                              params_filename=config.save_params_filename)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

            # check program vars and saved vars shape
            orig_para_shape = {
                each_var.name: tuple(each_var.desc.shape())
                for each_var in saved_params
            }
            for each_var in saved_params:
                var_temp = fluid.global_scope().find_var(each_var.name)
                assert var_temp != None, "can't not find var: " + each_var.name
                new_shape = (np.array(var_temp.get_tensor())).shape
                assert each_var.name in orig_para_shape, each_var.name + "MUST in var list"
                orig_shape = orig_para_shape.get(each_var.name)
                if new_shape != orig_shape:
                    raise RuntimeError(
                        "Shape not matching: the Program requires a parameter with a shape of ({}), "
488 489
                        "while the loaded parameter (namely [ {} ]) has a shape of  ({})."
                        .format(orig_shape, each_var.name, new_shape))
490 491 492 493 494 495 496 497 498 499 500 501 502

            # check feed/fetch vars in program and config
            feed_config = config.feed_config
            fetch_config = config.fetch_config
            fetch_targets_names = [v.name for v in fetch_targets]
            if not feed_target_names:
                print("warning! no feed targets in program.")
            if not fetch_targets_names:
                print("warning! no fetch targets in program.")
            fetch_list = fetch_targets
            feed_name_list = feed_target_names
            if feed_config.feeded_vars_names is not None and feed_target_names != feed_config.feeded_vars_names:
                print(
503 504
                    "warning! feed vars in program and config are diff: feed in program: {}. feed in config {}."
                    .format(feed_target_names, feed_config.feeded_vars_names))
505 506 507 508 509 510 511 512 513 514 515 516
                feed_name_list = feed_config.feeded_vars_names
                # remove feed op in inference_program. new feed op will be added in exe.run
                global_block = inference_program.global_block()
                need_to_remove_op_index = []
                for i, op in enumerate(global_block.ops):
                    op.desc.set_is_target(False)
                    if op.type == "feed":  # only remove feed op here
                        need_to_remove_op_index.append(i)
                for index in need_to_remove_op_index[::-1]:
                    global_block._remove_op(index)
            if fetch_config.fetch_vars_names is not None and fetch_targets_names != fetch_config.fetch_vars_names:
                print(
517 518
                    "warning! fetch vars in program and config are diff: fetch in program: {}. fetch in config {}."
                    .format(fetch_targets_names, fetch_config.fetch_vars_names))
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
                fetch_list = [
                    inference_program.global_block().var(i)
                    for i in fetch_config.fetch_vars_names
                ]
                # remove fetch op in inference_program. new fetch op will be added in exe.run
                global_block = inference_program.global_block()
                need_to_remove_op_index = []
                for i, op in enumerate(global_block.ops):
                    op.desc.set_is_target(False)
                    if op.type == "fetch":  # only remove fetch op here
                        need_to_remove_op_index.append(i)
                for index in need_to_remove_op_index[::-1]:
                    global_block._remove_op(index)

            # if fetch_list have lod tensor
            return_numpy = all([v.lod_level == 0 for v in fetch_list])

            # try dump fetch_targets
            feed_tensors = []
            assert len(feed_config.feeded_vars_names) == len(
                feed_config.feeded_vars_dims) == len(
                    feed_config.feeded_vars_types)
            # check program vars and feed tensor shape in config
            for i in range(len(feed_config.feeded_vars_names)):
                var = inference_program.global_block().var(
                    feed_config.feeded_vars_names[i])
                if not isinstance(feed_config.feeded_vars_dims[i],
                                  (list, tuple)):
                    tensor_shape = (feed_config.feeded_vars_dims[i], )
                else:
                    tensor_shape = tuple(feed_config.feeded_vars_dims[i])
                feed_config.feeded_vars_dims[i] = tensor_shape
                var_shape = var.shape[1:]
                if tensor_shape != var_shape:
                    raise RuntimeError(
554 555 556
                        "feed variable '{}' shape not match. infer program  shape: {}. feed tensor shape: {}"
                        .format(feed_config.feeded_vars_names[i], var_shape,
                                tensor_shape))
557 558 559 560 561 562 563 564 565

            if not feed_config.feeded_vars_filelist:
                print("generate random feed vars.")
                for i in range(len(feed_config.feeded_vars_names)):
                    var = inference_program.global_block().var(
                        feed_config.feeded_vars_names[i])
                    # create fake feed tensor. if lod_level > 1, should create_lod_tensor()
                    if var.lod_level == 0:
                        feed_tensors.append(
566 567 568 569
                            np.array(np.random.random(
                                tuple([config.batch_size] +
                                      list(feed_config.feeded_vars_dims[i]))),
                                     dtype=feed_config.feeded_vars_types[i]))
570
                    elif var.lod_level == 1:
571 572 573 574
                        t = np.array(np.random.random(
                            tuple([config.batch_size] +
                                  list(feed_config.feeded_vars_dims[i]))),
                                     dtype=feed_config.feeded_vars_types[i])
575
                        feed_tensors.append(
576 577 578
                            fluid.create_lod_tensor(t,
                                                    [[1] * config.batch_size],
                                                    place))
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
                    else:
                        raise RuntimeError(
                            "vars with lod_level >= 2 is not supported now in this infer program check tool."
                        )
                results = exe.run(inference_program,
                                  feed={
                                      name: feed_tensors[i]
                                      for i, name in enumerate(feed_name_list)
                                  },
                                  fetch_list=fetch_list,
                                  return_numpy=return_numpy)
            else:
                print("load feed vars from files: {}.".format(
                    feed_config.feeded_vars_filelist))
                feed_vars = [
                    inference_program.global_block().var(
                        feed_config.feeded_vars_names[i])
                    for i in range(len(feed_config.feeded_vars_names))
                ]
                feeder = fluid.DataFeeder(feed_list=feed_vars, place=place)
                batch_feed = feed_gen(config.batch_size,
                                      feed_config.feeded_vars_dims,
                                      feed_config.feeded_vars_filelist)
                slots = [batch_feed]
                results = exe.run(inference_program,
                                  feed=feeder.feed(slots),
                                  fetch_list=fetch_list,
                                  return_numpy=return_numpy)
            for i, v in enumerate(fetch_list):
                print("fetch_targets name: %s" % v.name)
                print("fetch_targets: {}".format(results[i]))
            return results