• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16"""test lccl allreduce with 8p"""
17
18import numpy as np
19
20import mindspore.context as context
21import mindspore.nn as nn
22from mindspore import Tensor
23from mindspore.common.initializer import initializer
24from mindspore.common.parameter import Parameter
25from mindspore.communication.management import init, HCCL_WORLD_COMM_GROUP, get_rank, get_group_size
26from mindspore.ops import operations as P
27
28context.set_context(mode=context.GRAPH_MODE, device_target='Ascend')
29context.set_context(jit_level='O0')
30
31init()
32rank = get_rank()
33size = get_group_size()
34x = np.ones([3, 1, 3, 3]).astype(np.float32) * 0.01 * (rank + 1)
35
36class Net(nn.Cell):
37    def __init__(self):
38        super(Net, self).__init__()
39        self.x1 = Parameter(initializer(Tensor(x), x.shape), name='x1')
40        self.x2 = Parameter(initializer(Tensor(x), x.shape), name='x2')
41        self.x3 = Parameter(initializer(Tensor(x), x.shape), name='x3')
42
43        self.op0 = "sum"
44        self.op1 = "sum"
45        self.op2 = "sum"
46
47        self.all_reduce1 = P.AllReduce(self.op0, group=HCCL_WORLD_COMM_GROUP)
48        self.all_reduce2 = P.AllReduce(self.op1, group=HCCL_WORLD_COMM_GROUP)
49        self.all_reduce3 = P.AllReduce(self.op2, group=HCCL_WORLD_COMM_GROUP)
50
51    def construct(self):
52        return (self.all_reduce1(self.x1),
53                self.all_reduce2(self.x2),
54                self.all_reduce3(self.x3))
55
56
57def test_AllReduce():
58    """
59    Feature: lccl operator test.
60    Description: msrun lccl all_reduce 8P case.
61    Expectation: success
62    """
63    all_reduce = Net()
64    output = all_reduce()
65
66    expect0 = np.ones([3, 1, 3, 3]).astype(np.float32) * 0
67    for i in range(size):
68        part = np.ones([3, 1, 3, 3]).astype(np.float32) * 0.01 * (i + 1)
69        expect0 += part
70    diff0 = output[0].asnumpy() - expect0
71    error0 = np.ones(shape=expect0.shape) * 1.0e-5
72    assert np.all(diff0 < error0)
73    assert output[0].shape == expect0.shape
74
75    expect1 = expect0
76    diff1 = output[1].asnumpy() - expect1
77    error1 = np.ones(shape=expect1.shape) * 1.0e-5
78    assert np.all(diff1 < error1)
79    assert output[1].shape == expect1.shape
80
81    expect2 = expect1
82    diff2 = output[2].asnumpy() - expect2
83    error2 = np.ones(shape=expect2.shape) * 1.0e-5
84    assert np.all(diff2 < error2)
85    assert output[2].shape == expect2.shape
86