1# Copyright 2019 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 16import pytest 17 18import mindspore.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 24 25 26class Net(nn.Cell): 27 def __init__(self): 28 super(Net, self).__init__() 29 self.bias_add = P.BiasAdd() 30 31 def construct(self, x, b): 32 return self.bias_add(x, b) 33 34 35@pytest.mark.level0 36@pytest.mark.platform_x86_cpu 37@pytest.mark.env_onecard 38def test_bias_add4d(): 39 x_shape = [2, 3, 4, 5] 40 x = np.ones(x_shape).astype(np.float32) 41 b = np.array([0.3, 0.5, 0.7]).astype(np.float32) 42 bias_add = Net() 43 output = bias_add(Tensor(x), Tensor(b)) 44 expect_output = x 45 for i in range(x_shape[0]): 46 for j in range(x_shape[1]): 47 expect_output[i][j] = x[i][j] + b[j] 48 print(output) 49 assert np.all(output.asnumpy() == expect_output), "bias_add execute failed, please check current code commit" 50 51 52@pytest.mark.level0 53@pytest.mark.platform_x86_cpu 54@pytest.mark.env_onecard 55def test_bias_add2d(): 56 x_shape = [2, 3] 57 x = np.ones(x_shape).astype(np.float32) 58 b = np.array([0.3, 0.5, 0.7]).astype(np.float32) 59 bias_add = Net() 60 output = bias_add(Tensor(x), Tensor(b)) 61 expect_output = x 62 for i in range(x_shape[0]): 63 for j in range(x_shape[1]): 64 expect_output[i][j] = x[i][j] + b[j] 65 print(output) 66 assert np.all(output.asnumpy() == expect_output), "bias_add execute failed, please check current code commit" 67 68 69@pytest.mark.level0 70@pytest.mark.platform_x86_cpu 71@pytest.mark.env_onecard 72def test_bias_add3d(): 73 x_shape = [2, 3, 4] 74 x = np.ones(x_shape).astype(np.float32) 75 b = np.array([0.3, 0.5, 0.7]).astype(np.float32) 76 bias_add = Net() 77 output = bias_add(Tensor(x), Tensor(b)) 78 expect_output = x 79 for i in range(x_shape[0]): 80 for j in range(x_shape[1]): 81 expect_output[i][j] = x[i][j] + b[j] 82 print(output) 83 assert np.all(output.asnumpy() == expect_output), "bias_add execute failed, please check current code commit" 84 85@pytest.mark.level0 86@pytest.mark.platform_x86_cpu 87@pytest.mark.env_onecard 88def test_bias_add5d(): 89 x_shape = [2, 5, 2, 3, 4] 90 x = np.ones(x_shape).astype(np.float32) 91 b = np.array([0.1, 0.3, 0.5, 0.7, 0.9]).astype(np.float32) 92 bias_add = Net() 93 output = bias_add(Tensor(x), Tensor(b)) 94 expect_output = x 95 for i in range(x_shape[0]): 96 for j in range(x_shape[1]): 97 expect_output[i][j] = x[i][j] + b[j] 98 print(output) 99 assert np.all(output.asnumpy() == expect_output), "bias_add execute failed, please check current code commit" 100 101 102class Net2(nn.Cell): 103 def __init__(self): 104 super(Net2, self).__init__() 105 self.bias_add = P.BiasAdd() 106 self.mul = P.Mul() 107 self.div = P.Div() 108 self.add = P.Add() 109 110 def construct(self, x, y, z, w): 111 mul_ = self.mul(x, y) 112 div_ = self.div(z, w) 113 temp = self.bias_add(mul_, div_) 114 temp = self.bias_add(temp, div_) 115 return self.add(temp, x) 116 117 118@pytest.mark.level0 119@pytest.mark.platform_x86_cpu 120@pytest.mark.env_onecard 121def test_net2(): 122 x_shape = [2, 3, 4] 123 x = np.ones(x_shape).astype(np.float32) 124 y = np.ones(x_shape).astype(np.float32) 125 z = np.array([1.1, 2.2, 3.4]).astype(np.float32) 126 w = np.array([10, 10, 10]).astype(np.float32) 127 net2 = Net2() 128 output = net2(Tensor(x), Tensor(y), Tensor(z), Tensor(w)) 129 expect_out = (np.array([[[2.22, 2.22, 2.22, 2.22], 130 [2.44, 2.44, 2.44, 2.44], 131 [2.68, 2.68, 2.68, 2.68]], 132 [[2.22, 2.22, 2.22, 2.22], 133 [2.44, 2.44, 2.44, 2.44], 134 [2.68, 2.68, 2.68, 2.68]]])) 135 assert np.allclose(output.asnumpy(), expect_out) 136