1# Copyright 2021 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 hccl allreduce with 8p""" 17 18import os 19import numpy as np 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore import dtype as mstype 23from mindspore.ops import operations as P 24from mindspore.communication.management import init 25 26np.random.seed(1) 27os.environ['GRAPH_OP_RUN'] = str(1) 28os.environ['HCCL_WHITELIST_DISABLE'] = str(1) 29init() 30 31class AllReduceNet(nn.Cell): 32 def __init__(self): 33 super(AllReduceNet, self).__init__() 34 self.mul = P.Mul() 35 self.all_reduce = P.AllReduce() 36 self.add = P.Add() 37 self.y1 = Tensor(np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]])).astype(np.float32) 38 self.y2 = Tensor(np.array([[-16, -16, -16, -16], [-16, -16, -16, -16], \ 39 [-16, -16, -16, -16]])).astype(np.float32) 40 41 def construct(self, x): 42 x = self.mul(x, 2) 43 z = self.add(x, self.y1) 44 z = self.all_reduce(z) 45 out = self.add(z, self.y2) 46 out = self.all_reduce(out) 47 out = self.mul(out, 2) 48 return out 49 50def test_hccl_allreduce_8p(): 51 net = AllReduceNet() 52 input_x = np.ones([3, 4]).astype(np.float32) 53 expect_output = [[256, 256, 256, 256], [256, 256, 256, 256], [256, 256, 256, 256]] 54 output = net(Tensor(input_x, mstype.float32)) 55 assert np.allclose(output.asnumpy(), expect_output) 56