• 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.mul = P.Mul()
29
30    def construct(self, x):
31        mul_res = self.mul(x, x)
32        square_res = P.Square()(x)
33        return self.add(mul_res, square_res)
34
35
36def test_basic():
37    input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
38    mul_res = input_x * input_x
39    square_res = np.square(input_x)
40    expect = mul_res + square_res
41
42    net = Net()
43    result = net(Tensor(input_x))
44
45    res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
46    assert res
47
48
49@pytest.mark.level0
50@pytest.mark.platform_x86_gpu_training
51@pytest.mark.env_onecard
52def test_basic_gpu():
53    context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
54    test_basic()
55
56
57@pytest.mark.level0
58@pytest.mark.platform_arm_ascend_training
59@pytest.mark.platform_x86_ascend_training
60@pytest.mark.env_onecard
61def test_basic_ascend():
62    context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
63    test_basic()
64