• 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# ============================================================================
15import numpy as np
16
17import mindspore.context as context
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore.common.initializer import initializer
21from mindspore.common.parameter import Parameter
22from mindspore.communication.management import init, get_rank, get_group_size
23from mindspore.ops import operations as P
24
25context.set_context(mode=context.GRAPH_MODE, device_target='Ascend')
26context.set_context(jit_level='O0')
27
28init()
29rank = get_rank()
30size = get_group_size()
31x = np.ones([3, 1, 3, 3]).astype(np.float32) * 0.01 * (rank + 1)
32
33
34class Net(nn.Cell):
35    def __init__(self):
36        super(Net, self).__init__()
37        self.x1 = Parameter(initializer(Tensor(x), x.shape), name='x1')
38        self.x2 = Parameter(initializer(Tensor(x), x.shape), name='x2')
39        self.x3 = Parameter(initializer(Tensor(x), x.shape), name='x3')
40
41        self.broadcast1 = P.Broadcast(0)
42        self.broadcast2 = P.Broadcast(1)
43        self.broadcast3 = P.Broadcast(2)
44
45    def construct(self):
46        return (self.broadcast1((self.x1,)),
47                self.broadcast2((self.x2,)),
48                self.broadcast3((self.x3,)))
49
50
51def test_Broadcast():
52    """
53    Feature: lccl operator test.
54    Description: msrun lccl broadcast 8P case.
55    Expectation: success
56    """
57    broadcast = Net()
58    output = broadcast()
59
60    expect0 = np.ones([3, 1, 3, 3]).astype(np.float32) * 1
61    expect1 = np.ones([3, 1, 3, 3]).astype(np.float32) * 2
62    expect2 = np.ones([3, 1, 3, 3]).astype(np.float32) * 3
63
64    diff0 = output[0][0].asnumpy() - expect0
65    error0 = np.ones(shape=expect0.shape) * 1.0e-5
66    assert np.all(diff0 < error0)
67    assert output[0][0].shape == expect0.shape
68
69    diff1 = output[1][0].asnumpy() - expect1
70    error1 = np.ones(shape=expect1.shape) * 1.0e-5
71    assert np.all(diff1 < error1)
72    assert output[1][0].shape == expect1.shape
73
74    diff2 = output[2][0].asnumpy() - expect2
75    error2 = np.ones(shape=expect2.shape) * 1.0e-5
76    assert np.all(diff2 < error2)
77    assert output[2][0].shape == expect2.shape
78