• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-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.add = P.Add()
28        self.sub = P.Sub()
29        self.mul = P.Mul()
30        self.div = P.RealDiv()
31        self.sqrt = P.Sqrt()
32        self.pow = P.Pow()
33        self.neg = P.Neg()
34        self.reducemin = P.ReduceMin()
35        self.reducesum = P.ReduceSum(keep_dims=True)
36        self.reshape = P.Reshape()
37
38    def construct(self, x, y):
39        add_res1 = self.add(x, 4)
40        add_res2 = self.add(add_res1, 5)
41        sub_res = self.sub(y, 3)
42        mul_res = self.mul(self.sqrt(add_res2), self.sqrt(sub_res))
43        div_res = self.div(mul_res, self.sqrt(mul_res))
44        pow_res = self.pow(y, 2)
45        neg_res = self.neg(self.neg(pow_res))
46        add_res3 = self.add(neg_res, div_res)
47        resh_res = self.reshape(add_res3, (2, 12, 3))
48        neg_res = self.neg(resh_res)
49        red_res = self.reducesum(neg_res, 0)
50        return self.reducemin(self.reducemin(red_res, 1), 1)
51
52
53class EmptyNet(Cell):
54    def __init__(self):
55        super(EmptyNet, self).__init__()
56        self.add = P.Add()
57        self.neg = P.Neg()
58
59    def construct(self, x, y):
60        add_res1 = self.add(x, y)
61        neg_res1 = self.neg(x)
62        add_res2 = self.add(add_res1, neg_res1)
63        return add_res2
64
65
66def test_basic():
67    input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
68    input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
69    input_y = np.abs(input_y) + 3
70    add_res = input_x + 9
71    sub_res = input_y + (-3)
72    mul_res = np.sqrt(add_res * sub_res)
73    div_res = np.sqrt(mul_res)
74    pow_res = input_y * input_y
75    neg_res = pow_res
76    add_res3 = neg_res + div_res
77    neg_res = np.negative(add_res3)
78    red_res = np.sum(neg_res, axis=0, keepdims=True)
79    expect = np.min(red_res, (1, 2, 3))
80
81    net = Net()
82    result = net(Tensor(input_x), Tensor(input_y))
83
84    res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
85                      atol=1.e-7, equal_nan=True)
86    assert res
87
88
89def test_empty_graph():
90    input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
91    input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
92    expect = input_y
93
94    net = EmptyNet()
95    result = net(Tensor(input_x), Tensor(input_y))
96
97    res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
98                      atol=1.e-7, equal_nan=True)
99    assert res
100
101
102@pytest.mark.level0
103@pytest.mark.platform_x86_gpu_training
104@pytest.mark.env_onecard
105def test_basic_gpu():
106    context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
107    test_basic()
108    test_empty_graph()
109
110
111@pytest.mark.level1
112@pytest.mark.platform_arm_ascend_training
113@pytest.mark.platform_x86_ascend_training
114@pytest.mark.env_onecard
115def test_basic_ascend():
116    context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
117    test_basic()
118    test_empty_graph()
119