collective_global_scatter.py 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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 os
16 17

import numpy as np
T
tianshuo78520a 已提交
18 19
from legacy_test.test_collective_api_base import (
    TestCollectiveAPIRunnerBase,
20
    dump_output,
T
tianshuo78520a 已提交
21 22
    runtime_main,
)
23 24

import paddle
25 26
from paddle import fluid
from paddle.distributed.utils import moe_utils
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

paddle.enable_static()


class TestCollectiveGlobalScatterAPI(TestCollectiveAPIRunnerBase):
    def __init__(self):
        self.global_ring_id = 0

    def get_model(self, main_prog, startup_program, rank, indata=None):
        with fluid.program_guard(main_prog, startup_program):
            seed = os.getpid()
            np.random.seed(seed)
            in_feat = 2
            n_expert = 2
            world_size = 2
            tot_expert = n_expert * world_size
43 44 45 46 47 48
            local_input_buf = paddle.static.data(
                name="local_input_buf", shape=[-1, in_feat], dtype="float32"
            )
            local_expert_count = paddle.static.data(
                name="local_expert_count", shape=[tot_expert], dtype="int64"
            )
49 50
            global_expert_count = []
            paddle.distributed.alltoall(
51 52
                paddle.split(local_expert_count, 2, axis=0), global_expert_count
            )
53
            global_expert_count = paddle.concat(global_expert_count, axis=0)
54 55 56
            output = moe_utils.global_scatter(
                local_input_buf, local_expert_count, global_expert_count
            )
57 58 59 60 61 62 63 64 65 66 67 68 69
            return [output]

    def run_trainer(self, args):
        train_prog = fluid.Program()
        startup_prog = fluid.Program()
        endpoints = args["endpoints"].split(",")
        rank = args["trainerid"]
        current_endpoint = args["currentendpoint"]
        nranks = 2
        paddle.distributed.init_parallel_env()
        if args['backend'] == 'nccl':
            device_id = int(os.getenv("FLAGS_selected_gpus", "0"))
            place = fluid.CUDAPlace(
70 71
                device_id
            )  # if args.use_gpu else fluid.CPUPlace()
72 73 74 75 76 77 78 79 80 81
        elif args['backend'] == 'bkcl':
            device_id = int(os.getenv("FLAGS_selected_xpus", "0"))
            place = fluid.XPUPlace(device_id)
        else:
            place = fluid.CPUPlace()
        np.random.seed(os.getpid())
        in_feat = 2
        n_expert = 2
        world_size = 2
        tot_expert = n_expert * world_size
82 83 84
        local_expert_count = np.random.randint(1, 4, size=tot_expert).astype(
            "int64"
        )
85
        fwd_expert_count = sum(local_expert_count)
86 87 88
        local_input_buf = np.random.rand(fwd_expert_count, in_feat).astype(
            "float32"
        )
89 90 91 92 93 94 95
        if args['static_mode']:
            result = self.get_model(train_prog, startup_prog, rank)
            exe = fluid.Executor(place)
            exe.run(startup_prog)
            fetch_list = []
            for elem in result:
                fetch_list.append(elem.name)
96 97 98 99 100 101 102 103
            out = exe.run(
                train_prog,
                feed={
                    'local_expert_count': local_expert_count,
                    'local_input_buf': local_input_buf,
                },
                fetch_list=fetch_list,
            )
104

105
        dump_output(out)
106 107 108 109


if __name__ == "__main__":
    runtime_main(TestCollectiveGlobalScatterAPI, "global_scatter")