• 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
16import pytest
17import numpy as np
18
19import mindspore
20import mindspore.nn as nn
21import mindspore.context as context
22
23from mindspore import Tensor
24from mindspore.ops.composite import GradOperation
25
26@pytest.mark.level0
27@pytest.mark.platform_x86_cpu
28@pytest.mark.env_onecard
29def test_mirror_pad():
30    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
31
32    test1_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]
33    test_1_paddings = ((0, 0), (0, 0), (1, 1), (2, 2))
34    test1_arr_exp = [[[[6, 5, 4, 5, 6, 5, 4], [3, 2, 1, 2, 3, 2, 1], [6, 5, 4, 5, 6, 5, 4],
35                       [9, 8, 7, 8, 9, 8, 7], [6, 5, 4, 5, 6, 5, 4]]]]
36
37    test2_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]
38    test_2_paddings = ((0, 0), (0, 0), (1, 1), (2, 2))
39    test2_arr_exp = [[[[2, 1, 1, 2, 3, 3, 2], [2, 1, 1, 2, 3, 3, 2], [5, 4, 4, 5, 6, 6, 5],
40                       [8, 7, 7, 8, 9, 9, 8], [8, 7, 7, 8, 9, 9, 8]]]]
41
42    reflectOp = nn.Pad(mode='REFLECT', paddings=test_1_paddings)
43    symmOp = nn.Pad(mode='SYMMETRIC', paddings=test_2_paddings)
44
45    x_test_1 = Tensor(np.array(test1_arr_in), dtype=mindspore.float32)
46    x_test_2 = Tensor(np.array(test2_arr_in), dtype=mindspore.float32)
47
48    y_test_1 = reflectOp(x_test_1).asnumpy()
49    y_test_2 = symmOp(x_test_2).asnumpy()
50
51    print(np.array(test1_arr_in))
52    print(y_test_1)
53
54    np.testing.assert_equal(np.array(test1_arr_exp), y_test_1)
55    np.testing.assert_equal(np.array(test2_arr_exp), y_test_2)
56
57
58class Grad(nn.Cell):
59    def __init__(self, network):
60        super(Grad, self).__init__()
61        self.grad = GradOperation(get_all=True, sens_param=True)
62        self.network = network
63    def construct(self, input_, output_grad):
64        return self.grad(self.network)(input_, output_grad)
65
66class Net(nn.Cell):
67    def __init__(self, pads, mode_):
68        super(Net, self).__init__()
69        self.pad = nn.Pad(mode=mode_, paddings=pads)
70    def construct(self, x):
71        return self.pad(x)
72
73
74@pytest.mark.level0
75@pytest.mark.platform_x86_cpu
76@pytest.mark.env_onecard
77def test_mirror_pad_backprop():
78    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
79    test_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]] # size -> 3*3
80    test_arr_in = Tensor(test_arr_in, dtype=mindspore.float32)
81    dy = (np.ones((1, 1, 4, 5)) * 0.1).astype(np.float32)
82    expected_dx = np.array([[[[0.2, 0.2, 0.1],
83                              [0.4, 0.4, 0.2],
84                              [0.2, 0.2, 0.1]]]])
85    net = Grad(Net(((0, 0), (0, 0), (1, 0), (0, 2)), "REFLECT"))
86    dx = net(test_arr_in, Tensor(dy))
87    dx = dx[0].asnumpy()
88    np.testing.assert_array_almost_equal(dx, expected_dx)
89
90@pytest.mark.level0
91@pytest.mark.platform_x86_cpu
92@pytest.mark.env_onecard
93def test_mirror_pad_fwd_back_4d_int32_reflect():
94    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
95    # set constants
96    shape = (2, 3, 3, 5)
97    pads = ((1, 0), (2, 0), (1, 2), (3, 4))
98    total_val = np.prod(shape)
99    test_arr_np = np.arange(total_val).reshape(shape) + 1
100    test_arr_ms = Tensor(test_arr_np, dtype=mindspore.int32)
101    # fwd_pass_check
102    op = nn.Pad(mode="REFLECT", paddings=pads)
103    expected_np_result = np.pad(test_arr_np, pads, 'reflect')
104    obtained_ms_res = op(test_arr_ms).asnumpy()
105    np.testing.assert_array_equal(expected_np_result, obtained_ms_res)
106    # backwards pass check
107    GradNet = Grad(Net(pads, "REFLECT"))
108    dy_value = Tensor(np.ones(obtained_ms_res.shape), dtype=mindspore.int32)
109    dx_value_obtained = GradNet(test_arr_ms, dy_value)[0].asnumpy()
110    dx_value_expected = np.array([[[[4, 6, 6, 6, 2],
111                                    [6, 9, 9, 9, 3],
112                                    [2, 3, 3, 3, 1]],
113                                   [[8, 12, 12, 12, 4],
114                                    [12, 18, 18, 18, 6],
115                                    [4, 6, 6, 6, 2]],
116                                   [[8, 12, 12, 12, 4],
117                                    [12, 18, 18, 18, 6],
118                                    [4, 6, 6, 6, 2]]],
119                                  [[[8, 12, 12, 12, 4],
120                                    [12, 18, 18, 18, 6],
121                                    [4, 6, 6, 6, 2]],
122                                   [[16, 24, 24, 24, 8],
123                                    [24, 36, 36, 36, 12],
124                                    [8, 12, 12, 12, 4]],
125                                   [[16, 24, 24, 24, 8],
126                                    [24, 36, 36, 36, 12],
127                                    [8, 12, 12, 12, 4]]]], dtype=np.int32)
128    np.testing.assert_array_equal(dx_value_expected, dx_value_obtained)
129
130
131@pytest.mark.level0
132@pytest.mark.platform_x86_cpu
133@pytest.mark.env_onecard
134def test_mirror_pad_fwd_back_4d_int32_symm():
135    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
136    # set constants
137    shape = (2, 3, 3, 5)
138    pads = ((1, 0), (2, 0), (1, 2), (3, 4))
139    total_val = np.prod(shape)
140    test_arr_np = np.arange(total_val).reshape(shape) + 1
141    test_arr_ms = Tensor(test_arr_np, dtype=mindspore.int32)
142    # fwd_pass_check
143    op = nn.Pad(mode="SYMMETRIC", paddings=pads)
144    expected_np_result = np.pad(test_arr_np, pads, 'symmetric')
145    obtained_ms_res = op(test_arr_ms).asnumpy()
146    np.testing.assert_array_equal(expected_np_result, obtained_ms_res)
147    # backwards pass check
148    GradNet = Grad(Net(pads, "SYMMETRIC"))
149    dy_value = Tensor(np.ones(obtained_ms_res.shape), dtype=mindspore.int32)
150    dx_value_obtained = GradNet(test_arr_ms, dy_value)[0].asnumpy()
151    dx_value_expected = np.array([[[[16, 24, 24, 16, 16],
152                                    [16, 24, 24, 16, 16],
153                                    [16, 24, 24, 16, 16]],
154                                   [[16, 24, 24, 16, 16],
155                                    [16, 24, 24, 16, 16],
156                                    [16, 24, 24, 16, 16]],
157                                   [[8, 12, 12, 8, 8],
158                                    [8, 12, 12, 8, 8],
159                                    [8, 12, 12, 8, 8]]],
160                                  [[[8, 12, 12, 8, 8],
161                                    [8, 12, 12, 8, 8],
162                                    [8, 12, 12, 8, 8]],
163                                   [[8, 12, 12, 8, 8],
164                                    [8, 12, 12, 8, 8],
165                                    [8, 12, 12, 8, 8]],
166                                   [[4, 6, 6, 4, 4],
167                                    [4, 6, 6, 4, 4],
168                                    [4, 6, 6, 4, 4]]]], dtype=np.int32)
169    np.testing.assert_array_equal(dx_value_expected, dx_value_obtained)
170