• 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# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.common.initializer import initializer
23from mindspore.common.parameter import Parameter
24from mindspore.ops.operations import _grad_ops as G
25
26context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
27
28
29class Net_Pool_Grad(nn.Cell):
30    def __init__(self):
31        super(Net_Pool_Grad, self).__init__()
32        self.maxpool_grad_fun = G.MaxPoolGrad(pad_mode="VALID",
33                                              kernel_size=2,
34                                              strides=2)
35
36        self.x = Parameter(initializer(
37            Tensor(np.array([[[
38                [0, 1, 2, 3, 4, 5],
39                [6, 7, 8, 9, 10, 11],
40                [12, 13, 14, 15, 16, 17],
41                [18, 19, 20, 21, 22, 23],
42                [24, 25, 26, 27, 28, 29],
43                [30, 31, 32, 33, 34, 35]
44            ]]]).astype(np.float32)), [1, 1, 6, 6]), name='x')
45
46        self.a = Parameter(initializer(
47            Tensor(np.array([[[
48                [3, 3, 3],
49                [3, 3, 3],
50                [3, 3, 3]
51            ]]]).astype(np.float32)), [1, 1, 3, 3]), name='a')
52
53        self.d = Parameter(initializer(
54            Tensor(np.array([[[
55                [7, 9, 11],
56                [19, 21, 23],
57                [31, 33, 35]
58            ]]]).astype(np.float32)), [1, 1, 3, 3]), name='d')
59
60    def construct(self):
61        return self.maxpool_grad_fun(self.x, self.a, self.d)
62
63
64@pytest.mark.level0
65@pytest.mark.platform_x86_cpu
66@pytest.mark.env_onecard
67def test_maxpool2d_grad():
68    maxpool2d_grad = Net_Pool_Grad()
69    output = maxpool2d_grad()
70    print(output)
71
72    expect_result = (np.array([[[
73        [0, 0, 0, 0, 0, 0],
74        [0, 7, 0, 9, 0, 11],
75        [0, 0, 0, 0, 0, 0],
76        [0, 19, 0, 21, 0, 23],
77        [0, 0, 0, 0, 0, 0],
78        [0, 31, 0, 33, 0, 35]
79    ]]]))
80    assert (output.asnumpy() == expect_result).all()
81