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.py 2.4 kB

5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2019-2021 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.api import ms_function
  21. from mindspore.ops.operations import _grad_ops as G
  22. context.set_context(device_target='GPU')
  23. class SliceGrad(nn.Cell):
  24. def __init__(self):
  25. super(SliceGrad, self).__init__()
  26. self.slicegrad = G.SliceGrad()
  27. @ms_function
  28. def construct(self, dy, x):
  29. return self.slicegrad(dy, x, (0, 1, 0), (2, 1, 3))
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_slice():
  34. x = Tensor(np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]]).astype(np.float32))
  35. dy = Tensor(np.array([[[3., 1., 2.]], [[4., 1., 4.]]]).astype(np.float32))
  36. slicegrad = SliceGrad()
  37. output = slicegrad(dy, x)
  38. expect = [[[0., 0., 0.],
  39. [3., 1., 2.]],
  40. [[0., 0., 0.],
  41. [4., 1., 4.]],
  42. [[0., 0., 0.],
  43. [0., 0., 0.]]]
  44. assert (output.asnumpy() == expect).all()
  45. @pytest.mark.level0
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.env_onecard
  48. def test_slice_float64():
  49. x = Tensor(np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]]).astype(np.float64))
  50. dy = Tensor(np.array([[[3., 1., 2.]], [[4., 1., 4.]]]).astype(np.float64))
  51. slicegrad = SliceGrad()
  52. output = slicegrad(dy, x)
  53. expect = np.array([[[0., 0., 0.],
  54. [3., 1., 2.]],
  55. [[0., 0., 0.],
  56. [4., 1., 4.]],
  57. [[0., 0., 0.],
  58. [0., 0., 0.]]]).astype(np.float64)
  59. assert (output.asnumpy() == expect).all()