1# Copyright 2020 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""" test BiasAdd """ 16import numpy as np 17 18import mindspore.nn as nn 19from mindspore import Tensor, Parameter 20from mindspore.common.initializer import initializer 21from mindspore.ops import operations as P 22from ..ut_filter import non_graph_engine 23 24 25class Net(nn.Cell): 26 """Net definition""" 27 28 def __init__(self, 29 output_channels, 30 bias_init='zeros', 31 ): 32 super(Net, self).__init__() 33 self.biasAdd = P.BiasAdd() 34 35 if isinstance(bias_init, Tensor): 36 if bias_init.ndim != 1 or bias_init.shape[0] != output_channels: 37 raise ValueError("bias_init shape error") 38 39 self.bias = Parameter(initializer( 40 bias_init, [output_channels]), name="bias") 41 42 def construct(self, input_x): 43 return self.biasAdd(input_x, self.bias) 44 45 46@non_graph_engine 47def test_compile(): 48 bias_init = Tensor(np.ones([3]).astype(np.float32)) 49 net = Net(3, bias_init=bias_init) 50 input_data = Tensor(np.ones([1, 3, 3, 3], np.float32)) 51 # since simulator currently not support matMul 52 # enable it when staging function is ready 53 output = net(input_data) 54 print(output.asnumpy()) 55