• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.operations import _grad_ops as G
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_grad = G.BiasAddGrad()
30
31    def construct(self, dout):
32        return self.bias_add_grad(dout)
33
34
35@pytest.mark.level0
36@pytest.mark.platform_x86_cpu
37@pytest.mark.env_onecard
38def test_bias_add_grad2d():
39    dout = np.ones([2, 3]).astype(np.float32)
40    bias_add_grad = Net()
41    output = bias_add_grad(Tensor(dout))
42    expect_output = np.array([2., 2., 2.]).astype(np.float32)
43    print(output.asnumpy())
44    assert np.all(output.asnumpy() == expect_output), "bias_add_grad execute failed, please check current code commit"
45
46
47@pytest.mark.level0
48@pytest.mark.platform_x86_cpu
49@pytest.mark.env_onecard
50def test_bias_add_grad4d():
51    dout = np.ones([2, 3, 4, 4]).astype(np.float32)
52    bias_add_grad = Net()
53    output = bias_add_grad(Tensor(dout))
54    expect_output = np.array([32., 32., 32.]).astype(np.float32)
55    print(output.asnumpy())
56    assert np.all(output.asnumpy() == expect_output), "bias_add_grad execute failed, please check current code commit"
57
58@pytest.mark.level0
59@pytest.mark.platform_x86_cpu
60@pytest.mark.env_onecard
61def test_bias_add_grad5d():
62    dout = np.ones([2, 3, 4, 4, 2]).astype(np.float32)
63    bias_add_grad = Net()
64    output = bias_add_grad(Tensor(dout))
65    expect_output = np.array([64., 64., 64.]).astype(np.float32)
66    print(output.asnumpy())
67    assert np.all(output.asnumpy() == expect_output), "bias_add_grad execute failed, please check current code commit"
68