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 numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.common import dtype as mstype 23from mindspore.common.api import ms_function 24from mindspore.ops.operations import _grad_ops as G 25from mindspore.ops import operations as P 26 27context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 28 29 30class SliceGrad(nn.Cell): 31 def __init__(self): 32 super(SliceGrad, self).__init__() 33 34 self.slicegrad = G.SliceGrad() 35 36 @ms_function 37 def construct(self, dy, x): 38 return self.slicegrad(dy, x, (0, 1, 0), (2, 1, 3)) 39 40 41@pytest.mark.level0 42@pytest.mark.platform_x86_cpu 43@pytest.mark.env_onecard 44def test_slice_grad(): 45 x = Tensor(np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]]), mstype.float32) 46 dy = Tensor(np.array([[[3., 1., 2.]], [[4., 1., 4.]]]), mstype.float32) 47 slicegrad = SliceGrad() 48 output = slicegrad(dy, x) 49 expect = [[[0., 0., 0.], 50 [3., 1., 2.]], 51 [[0., 0., 0.], 52 [4., 1., 4.]], 53 [[0., 0., 0.], 54 [0., 0., 0.]]] 55 print("output:\n", output) 56 assert (output.asnumpy() == expect).all() 57 58 59class SliceGrad2(nn.Cell): 60 def __init__(self): 61 super(SliceGrad2, self).__init__() 62 self.slicegrad = G.SliceGrad() 63 64 def construct(self, dy, x): 65 return self.slicegrad(dy, x, (0, 1, 0), (2, 2, 2)) 66 67 68@pytest.mark.level0 69@pytest.mark.platform_x86_cpu 70@pytest.mark.env_onecard 71def test_slice_grad2(): 72 dy = Tensor(np.array([[[2., 3.], [4., 5.]], [[8., 9.], [10., 11.]]]), mstype.float32) 73 x = Tensor(np.arange(2 * 3 * 2).reshape(2, 3, 2), mstype.float32) 74 grad = SliceGrad2() 75 output = grad(dy, x) 76 print("output:\n", output) 77 expect = [[[0., 0.], [2., 3.], [4., 5.]], 78 [[0., 0.], [8., 9.], [10., 11.]]] 79 assert (output.asnumpy() == expect).all() 80 81def test_slice_grad3(): 82 x = Tensor(np.array([[[1.0, 3.5, 5.8], [2.5, 4, 1]], [[3.5, 15.3, 3.1], [2.2, 4.0, 1.1]], 83 [[43.4, 1.1, 12.1], [2.4, 6.5, 6.3]]]), mstype.float64) 84 dy = Tensor(np.array([[[3.1, 1.1, 2.2]], [[4.4, 1.2, 4.2]]]), mstype.float64) 85 slicegrad = SliceGrad() 86 output = slicegrad(dy, x) 87 expect = [[[0., 0., 0.], 88 [3.1, 1.1, 2.2]], 89 [[0., 0., 0.], 90 [4.4, 1.2, 4.2]], 91 [[0., 0., 0.], 92 [0., 0., 0.]]] 93 print("output:\n", output) 94 assert (output.asnumpy() == expect).all() 95 96class StridedSliceGrad(nn.Cell): 97 def __init__(self, x, begin, end, stride): 98 super(StridedSliceGrad, self).__init__() 99 self.shape_op = P.Shape() 100 self.shapex = self.shape_op(x) 101 self.begin = begin 102 self.end = end 103 self.stride = stride 104 self.stride_slice = G.StridedSliceGrad() 105 106 def construct(self, dy): 107 return self.stride_slice(dy, self.shapex, self.begin, self.end, self.stride) 108 109@pytest.mark.level0 110@pytest.mark.platform_x86_cpu 111@pytest.mark.env_onecard 112def test_strided_slice_grad_bool_type(): 113 x = Tensor([[[False, False, True], [False, True, False]], [[False, True, False], [True, False, False]], 114 [[False, True, True], [True, False, True]]], mstype.bool_) 115 dy = Tensor([False, True, False], mstype.bool_) 116 begin = (1, 0, 0) 117 end = (2, 1, 3) 118 stride = (1, 1, 1) 119 slice_op = StridedSliceGrad(x, begin, end, stride) 120 output = slice_op(dy) 121 expected_output = np.array([[[False, False, False], [False, False, False]], 122 [[False, True, False], [False, False, False]], 123 [[False, False, False], [False, False, False]]]) 124 assert (output.asnumpy() == expected_output).all() 125 126@pytest.mark.level0 127@pytest.mark.platform_x86_cpu 128@pytest.mark.env_onecard 129def test_strided_slice_grad_float32_type(): 130 x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]], mstype.float32) 131 dy = Tensor([3, 3, 3], mstype.float32) 132 begin = (1, 0, 0) 133 end = (2, 1, 3) 134 stride = (1, 1, 1) 135 slice_op = StridedSliceGrad(x, begin, end, stride) 136 output = slice_op(dy) 137 expected_output = np.array([[[0, 0, 0], [0, 0, 0]], [[3, 3, 3], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]) 138 assert (output.asnumpy() == expected_output).all() 139 140if __name__ == '__main__': 141 test_slice_grad() 142 test_slice_grad2() 143 test_strided_slice_grad_bool_type() 144 test_strided_slice_grad_float32_type() 145