• 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_bprop """
16import numpy as np
17
18import mindspore.nn as nn
19from mindspore import context
20from mindspore.common import Tensor
21from mindspore.common.api import ms_function
22from mindspore.common.parameter import Parameter
23from mindspore.ops import operations as P
24from ....mindspore_test_framework.utils.bprop_util import bprop
25
26
27def setup_module():
28    context.set_context(mode=context.PYNATIVE_MODE)
29
30
31class Net(nn.Cell):
32    """ Net definition """
33
34    def __init__(self):
35        super(Net, self).__init__()
36        self.matmul = P.MatMul()
37        self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
38
39    @ms_function
40    def construct(self, x, y):
41        x = x * self.z
42        out = self.matmul(x, y)
43        return x, out
44
45
46def test_bprop_no_sens():
47    grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)),
48                  Tensor(np.ones([3, 2]).astype(np.float32)), wrt=['inputs'])
49    print(grads)
50
51
52def test_bprop_sens():
53    grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
54                  grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
55                                     Tensor(np.ones([2, 2]).astype(np.float32))), wrt=['inputs'])
56    print(grads)
57
58
59def test_bprop_first_only():
60    grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
61                  grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
62                                     Tensor(np.ones([2, 2]).astype(np.float32))))
63    print(grads)
64
65
66def test_bprop_wrt_params():
67    net = Net()
68    grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
69                  grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
70                                     Tensor(np.ones([2, 2]).astype(np.float32))),
71                  wrt=['params'],
72                  params=net.trainable_params())
73    print(grads)
74
75
76def test_bprop_wrt_params_no_sens():
77    net = Net()
78    grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
79                  wrt=['params'],
80                  params=net.trainable_params())
81    print(grads)
82
83
84def test_bprop_wrt_inputs_and_params():
85    net = Net()
86    grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
87                  grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
88                                     Tensor(np.ones([2, 2]).astype(np.float32))),
89                  wrt=['inputs', 'params'],
90                  params=net.trainable_params())
91    print(grads)
92