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 16import numpy as np 17import pytest 18import mindspore.context as context 19from mindspore import Tensor 20from mindspore.nn import Cell 21import mindspore.ops.operations as P 22 23 24class Net(Cell): 25 def __init__(self): 26 super(Net, self).__init__() 27 self.addn = P.AddN() 28 29 def construct(self, *args): 30 return self.addn(*args) 31 32 33def get_output(*tensors): 34 net = Net() 35 output = net(tensors) 36 return output 37 38 39def test_basic(): 40 np.random.seed(0) 41 tensors = [] 42 expect = np.array([0], np.float32) 43 for _ in range(10): 44 t = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32) 45 expect = t + expect 46 tensors.append(Tensor(t)) 47 48 output = get_output(*tensors).asnumpy() 49 50 assert np.allclose(expect, output, 1.e-4, 1.e-7) 51 52 53@pytest.mark.level0 54@pytest.mark.platform_x86_gpu_training 55@pytest.mark.env_onecard 56def test_basic_gpu(): 57 context.set_context(mode=context.GRAPH_MODE, device_target="GPU", enable_graph_kernel=True) 58 test_basic() 59 60 61@pytest.mark.level0 62@pytest.mark.platform_arm_ascend_training 63@pytest.mark.platform_x86_ascend_training 64@pytest.mark.env_onecard 65def test_basic_ascend(): 66 context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", enable_graph_kernel=True) 67 test_basic() 68