process_group_xccl.py 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# Copyright (c) 2022 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 unittest
import random
import numpy as np

import paddle
from paddle.fluid import core
import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.dygraph.parallel import ParallelEnv


def init_process_group(strategy=None):
    nranks = ParallelEnv().nranks
    rank = ParallelEnv().local_rank
    is_master = True if rank == 0 else False
    store = paddle.fluid.core.TCPStore("127.0.0.1", 6173, is_master, nranks)
    pg_group = core.ProcessGroupCustom(
32
        store,
33
        ParallelEnv().device_type,
34 35 36
        rank,
        nranks,
    )
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    return pg_group


class TestProcessGroupFp32(unittest.TestCase):
    def setUp(self):
        paddle.seed(2022)
        random.seed(2022)
        np.random.seed(2022)
        self.config()

    def config(self):
        self.dtype = "float32"
        self.shape = (2, 10, 5)

    def test_create_process_group_xccl(self):
        with _test_eager_guard():
54 55
            device_id = paddle.distributed.ParallelEnv().dev_id
            paddle.set_device('custom_cpu:%d' % device_id)
56 57 58 59 60 61 62 63 64 65

            pg = init_process_group()

            x = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_y = paddle.to_tensor(y)

            sum_result = tensor_x + tensor_y
            if pg.rank() == 0:
66
                task = pg.all_reduce(tensor_x, core.ReduceOp.SUM, sync_op=True)
67 68 69
                task.wait()
                # assert np.array_equal(tensor_x, sum_result)
            else:
70
                task = pg.all_reduce(tensor_y, core.ReduceOp.SUM, sync_op=True)
71 72 73 74 75 76 77 78 79 80 81 82 83
                task.wait()
                # assert np.array_equal(tensor_y, sum_result)

            print("test allreduce sum api ok")

            x = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_y = paddle.to_tensor(y)

            max_result = paddle.maximum(tensor_x, tensor_y)

            if pg.rank() == 0:
84
                task = pg.all_reduce(tensor_x, core.ReduceOp.MAX, sync_op=True)
85 86 87
                task.wait()
                # assert np.array_equal(tensor_x, max_result)
            else:
88
                task = pg.all_reduce(tensor_y, core.ReduceOp.MAX, sync_op=True)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                task.wait()
                # assert np.array_equal(tensor_y, max_result)

            print("test allreduce max api ok")

            # test broadcast
            # rank 0
            x = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            # rank 1
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_y = paddle.to_tensor(y)

            broadcast_result = paddle.assign(tensor_x)
            if pg.rank() == 0:
104 105
                task = pg.broadcast(tensor_x, 0, sync_op=True)
                task.wait()
106 107 108 109
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
                assert task.is_completed()
                # assert np.array_equal(broadcast_result, tensor_x)
            else:
110 111
                task = pg.broadcast(tensor_y, 0, sync_op=True)
                task.wait()
112 113 114 115 116 117 118 119 120
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
                assert task.is_completed()
                # assert np.array_equal(broadcast_result, tensor_y)

            print("test broadcast api ok")

            # test barrier
            # rank 0
            if pg.rank() == 0:
121
                task = pg.barrier(device_id)
122 123 124
                task.wait()
            # rank 1
            else:
125
                task = pg.barrier(device_id)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                task.wait()

            print("test barrier api ok\n")
            return

            # test allgather
            # rank 0
            x = np.random.random(self.shape).astype(self.dtype)
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            tensor_y = paddle.to_tensor(y)
            out_shape = list(self.shape)
            out_shape[0] *= 2
            out = np.random.random(out_shape).astype(self.dtype)
            tensor_out = paddle.to_tensor(out)
            if pg.rank() == 0:
142
                task = pg.all_gather(tensor_out, tensor_x, sync_op=True)
143 144 145 146
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            # rank 1
            else:
147
                task = pg.all_gather(tensor_out, tensor_y, sync_op=True)
148 149 150
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2])
151 152 153
            out_2 = paddle.slice(
                tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
            )
154 155 156 157 158 159 160 161 162 163 164 165 166 167
            # assert np.array_equal(tensor_x, out_1)
            # assert np.array_equal(tensor_y, out_2)
            print("test allgather api ok\n")

            # test alltoall
            # rank 0
            x = np.random.random(self.shape).astype(self.dtype)
            y = np.random.random(self.shape).astype(self.dtype)
            out1 = np.random.random(self.shape).astype(self.dtype)
            out2 = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            tensor_y = paddle.to_tensor(y)
            tensor_out1 = paddle.to_tensor(out1)
            tensor_out2 = paddle.to_tensor(out2)
168 169 170 171 172 173
            raw_tensor_x_2 = paddle.slice(
                tensor_x, [0], [self.shape[0] // 2], [self.shape[0]]
            )
            raw_tensor_y_1 = paddle.slice(
                tensor_y, [0], [0], [self.shape[0] // 2]
            )
174 175 176 177 178 179 180 181 182
            if pg.rank() == 0:
                task = pg.alltoall(tensor_x, tensor_out1)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            # rank 1
            else:
                task = pg.alltoall(tensor_y, tensor_out2)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
183 184 185
            out1_2 = paddle.slice(
                tensor_out1, [0], [self.shape[0] // 2], [self.shape[0]]
            )
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
            out2_1 = paddle.slice(tensor_out2, [0], [0], [self.shape[0] // 2])
            # if pg.rank() == 0:
            #     assert np.array_equal(out1_2.numpy(), raw_tensor_y_1.numpy())
            # else:
            #     assert np.array_equal(out2_1, raw_tensor_x_2)
            print("test alltoall api ok\n")

            # test Reduce
            # rank 0
            x = np.random.random(self.shape).astype(self.dtype)
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            tensor_y = paddle.to_tensor(y)
            sum_result = tensor_x + tensor_y
            if pg.rank() == 0:
                task = pg.reduce(tensor_x, 0)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            # rank 1
            else:
                task = pg.reduce(tensor_y, 0)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            # if pg.rank() == 0:
            #     assert np.array_equal(tensor_x, sum_result)
            print("test reduce sum api ok\n")

            # test Scatter
            # rank 0
            in_shape = list(self.shape)
            in_shape[0] *= 2
            x = np.random.random(in_shape).astype(self.dtype)
            y = np.random.random(self.shape).astype(self.dtype)
            tensor_x = paddle.to_tensor(x)
            tensor_y = paddle.to_tensor(y)
            if pg.rank() == 0:
                task = pg.scatter(tensor_x, tensor_y, 0)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            # rank 1
            else:
                task = pg.scatter(tensor_x, tensor_y, 0)
                task.wait()
                # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1)
            out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]])
231 232 233
            out2 = paddle.slice(
                tensor_x, [0], [self.shape[0]], [self.shape[0] * 2]
            )
234 235 236 237 238 239 240 241 242
            # if pg.rank() == 0:
            #     assert np.array_equal(tensor_y, out1)
            # else:
            #     assert np.array_equal(tensor_y, out2)
            print("test scatter api ok\n")


if __name__ == "__main__":
    unittest.main()