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_reverse_v2_op.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright 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.ops import operations as P
  21. class ReverseV2Net(nn.Cell):
  22. def __init__(self, axis):
  23. super(ReverseV2Net, self).__init__()
  24. self.reverse_v2 = P.ReverseV2(axis)
  25. def construct(self, x):
  26. return self.reverse_v2(x)
  27. def reverse_v2(x_numpy, axis):
  28. x = Tensor(x_numpy)
  29. reverse_v2_net = ReverseV2Net(axis)
  30. output = reverse_v2_net(x).asnumpy()
  31. expected_output = np.flip(x_numpy, axis)
  32. np.testing.assert_array_equal(output, expected_output)
  33. def reverse_v2_3d(nptype):
  34. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  35. x_numpy = np.arange(60).reshape(3, 4, 5).astype(nptype)
  36. reverse_v2(x_numpy, (0,))
  37. reverse_v2(x_numpy, (1,))
  38. reverse_v2(x_numpy, (2,))
  39. reverse_v2(x_numpy, (2, -2))
  40. reverse_v2(x_numpy, (-3, 1, 2))
  41. def reverse_v2_1d(nptype):
  42. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  43. x_numpy = np.arange(4).astype(nptype)
  44. reverse_v2(x_numpy, (0,))
  45. reverse_v2(x_numpy, (-1,))
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_reverse_v2_float16():
  50. reverse_v2_1d(np.float16)
  51. reverse_v2_3d(np.float16)
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.env_onecard
  55. def test_reverse_v2_float32():
  56. reverse_v2_1d(np.float32)
  57. reverse_v2_3d(np.float32)
  58. @pytest.mark.level1
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.env_onecard
  61. def test_reverse_v2_uint8():
  62. reverse_v2_1d(np.uint8)
  63. reverse_v2_3d(np.uint8)
  64. @pytest.mark.level1
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_reverse_v2_int16():
  68. reverse_v2_1d(np.int16)
  69. reverse_v2_3d(np.int16)
  70. @pytest.mark.level1
  71. @pytest.mark.platform_x86_gpu_training
  72. @pytest.mark.env_onecard
  73. def test_reverse_v2_int32():
  74. reverse_v2_1d(np.int32)
  75. reverse_v2_3d(np.int32)
  76. @pytest.mark.level1
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_reverse_v2_int64():
  80. reverse_v2_1d(np.int64)
  81. reverse_v2_3d(np.int64)
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.env_onecard
  85. def test_reverse_v2_invalid_axis():
  86. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  87. x = Tensor(np.arange(60).reshape(1, 2, 3, 2, 5).astype(np.int32))
  88. with pytest.raises(ValueError) as info:
  89. reverse_v2_net = ReverseV2Net((0, 1, 2, 1))
  90. _ = reverse_v2_net(x)
  91. assert "'axis' cannot contain duplicate dimensions" in str(info.value)
  92. with pytest.raises(ValueError) as info:
  93. reverse_v2_net = ReverseV2Net((-2, -1, 3))
  94. _ = reverse_v2_net(x)
  95. assert "'axis' cannot contain duplicate dimensions" in str(info.value)