• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_parser_construct """
16import pytest
17import numpy as np
18from mindspore import context
19from mindspore.nn import Cell
20from mindspore.common.tensor import Tensor
21from mindspore.ops import operations as P
22from mindspore.ops.composite import GradOperation
23from mindspore.common.parameter import Parameter
24
25def setup_module():
26    context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
27
28@pytest.mark.level1
29@pytest.mark.platform_arm_ascend_training
30@pytest.mark.platform_x86_ascend_training
31@pytest.mark.env_onecard
32def test_parser_construct():
33    class ParentNet(Cell):
34        def __init__(self):
35            super().__init__()
36            self.relu = P.ReLU()
37
38        def construct(self, x):
39            return self.relu(x)
40
41    class UncleNet(Cell):
42        def __init__(self):
43            super(UncleNet, self).__init__()
44            self.sigmoid = P.Sigmoid()
45
46        def construct(self, x):
47            return self.sigmoid(x)
48
49    class Net(UncleNet, ParentNet):
50        def __init__(self):
51            super().__init__()
52            super(UncleNet, self).__init__()
53
54        def construct(self, x):
55            return super(UncleNet, self).construct(x)
56
57    input_np_x = np.ones([2, 3, 4, 5]).astype(np.float32)
58    out_np = np.ones([2, 3, 4, 5]).astype(np.float32)
59
60    input_me = Tensor(input_np_x)
61    output_grad_me = Tensor(out_np)
62    net = Net()
63    out_me = net(input_me)
64
65    net1 = Net()
66    grad = GradOperation(sens_param=True)
67    grad_op = grad(net1)
68    grad_me = grad_op(input_me, output_grad_me)
69
70    assert np.allclose(input_np_x, out_me.asnumpy(), 0.001, 0.001)
71    assert np.allclose(input_np_x, grad_me.asnumpy(), 0.001, 0.001)
72
73
74@pytest.mark.level0
75@pytest.mark.platform_arm_ascend_training
76@pytest.mark.platform_x86_ascend_training
77@pytest.mark.env_onecard
78def test_sit_parser_input_parameter():
79    def tensor_add(x, y):
80        add = P.Add()
81        z = add(x, y)
82        return  z
83    x = Tensor(np.ones([2, 2]).astype(np.float32))
84    x = Parameter(x, name="x")
85    y = Tensor(np.ones([2, 2]).astype(np.float32))
86    y = Parameter(y, name="y")
87    grad = GradOperation(get_all=True, get_by_list=False, sens_param=False)
88
89    with pytest.raises(TypeError):
90        grad(tensor_add)(x, y)
91