You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_slice_grad_op.py 5.2 kB

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