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 allgather 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([1, 1, 3, 3]).astype(np.float32) * 0.01 * (rank + 1) 35 36 37class Net(nn.Cell): 38 def __init__(self): 39 super(Net, self).__init__() 40 self.all_gather = P.AllGather(group=HCCL_WORLD_COMM_GROUP) 41 self.x = Parameter(initializer(Tensor(x), x.shape), name='x') 42 43 def construct(self): 44 return self.all_gather(self.x) 45 46 47def test_AllGather(): 48 """ 49 Feature: lccl operator test. 50 Description: msrun lccl all_gather 8P case. 51 Expectation: success 52 """ 53 all_gather = Net() 54 output = all_gather() 55 56 expect = np.ones([1, 1, 3, 3]).astype(np.float32) * 0.01 * (0 + 1) 57 for i in range(size - 1): 58 tmp = np.ones([1, 1, 3, 3]).astype(np.float32) * 0.01 * (i + 2) 59 expect = np.concatenate((expect, tmp)) 60 diff = np.absolute(output.asnumpy() - expect) 61 error = np.ones(shape=expect.shape) * 1.0e-5 62 assert np.all(diff < error) 63 assert output.shape == expect.shape 64