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_spacetodepth_op.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. import mindspore.ops.operations.array_ops as P
  20. from mindspore import Tensor
  21. from mindspore.common.api import ms_function
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. class SpaceToDepthNet(nn.Cell):
  25. def __init__(self, nptype):
  26. super(SpaceToDepthNet, self).__init__()
  27. self.SpaceToDepth = P.SpaceToDepth(2)
  28. data_np = np.array([[[[0, 3],
  29. [6, 9]],
  30. [[1, 4],
  31. [7, 10]],
  32. [[2, 5],
  33. [8, 11]]]]).astype(nptype)
  34. self.data_np = data_np
  35. self.x = Parameter(initializer(Tensor(self.data_np), (1, 3, 2, 2)), name='x')
  36. @ms_function
  37. def construct(self):
  38. return self.SpaceToDepth(self.x)
  39. def SpaceToDepth(nptype):
  40. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  41. expect = np.arange(12).reshape((1, 12, 1, 1)).astype(nptype)
  42. std = SpaceToDepthNet(nptype)
  43. output = std()
  44. assert (output.asnumpy() == expect).all()
  45. def SpaceToDepth_pynative(nptype):
  46. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  47. expect = np.arange(12).reshape((1, 12, 1, 1)).astype(nptype)
  48. std = P.SpaceToDepth(2)
  49. data_np = np.array([[[[0, 3],
  50. [6, 9]],
  51. [[1, 4],
  52. [7, 10]],
  53. [[2, 5],
  54. [8, 11]]]]).astype(nptype)
  55. tensor_input = Tensor(data_np)
  56. output = std(tensor_input)
  57. assert (output.asnumpy() == expect).all()
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.env_onecard
  61. def test_spacetodepth_graph_float32():
  62. SpaceToDepth(np.float32)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_training
  65. @pytest.mark.env_onecard
  66. def test_spacetodepth_graph_float16():
  67. SpaceToDepth(np.float16)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_gpu_training
  70. @pytest.mark.env_onecard
  71. def test_spacetodepth_graph_int32():
  72. SpaceToDepth(np.int32)
  73. @pytest.mark.level0
  74. @pytest.mark.platform_x86_gpu_training
  75. @pytest.mark.env_onecard
  76. def test_spacetodepth_graph_int64():
  77. SpaceToDepth(np.int64)
  78. @pytest.mark.level0
  79. @pytest.mark.platform_x86_gpu_training
  80. @pytest.mark.env_onecard
  81. def test_spacetodepth_graph_int8():
  82. SpaceToDepth(np.int8)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_gpu_training
  85. @pytest.mark.env_onecard
  86. def test_spacetodepth_graph_int16():
  87. SpaceToDepth(np.int16)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_spacetodepth_graph_uint8():
  92. SpaceToDepth(np.uint8)
  93. @pytest.mark.level0
  94. @pytest.mark.platform_x86_gpu_training
  95. @pytest.mark.env_onecard
  96. def test_spacetodepth_graph_uint16():
  97. SpaceToDepth(np.uint16)
  98. @pytest.mark.level0
  99. @pytest.mark.platform_x86_gpu_training
  100. @pytest.mark.env_onecard
  101. def test_spacetodepth_graph_uint32():
  102. SpaceToDepth(np.uint32)
  103. @pytest.mark.level0
  104. @pytest.mark.platform_x86_gpu_training
  105. @pytest.mark.env_onecard
  106. def test_spacetodepth_graph_uint64():
  107. SpaceToDepth(np.uint64)
  108. @pytest.mark.level0
  109. @pytest.mark.platform_x86_gpu_training
  110. @pytest.mark.env_onecard
  111. def test_spacetodepth_pynative_float32():
  112. SpaceToDepth_pynative(np.float32)
  113. @pytest.mark.level0
  114. @pytest.mark.platform_x86_gpu_training
  115. @pytest.mark.env_onecard
  116. def test_spacetodepth_pynative_float16():
  117. SpaceToDepth_pynative(np.float16)
  118. @pytest.mark.level0
  119. @pytest.mark.platform_x86_gpu_training
  120. @pytest.mark.env_onecard
  121. def test_spacetodepth_pynative_int32():
  122. SpaceToDepth_pynative(np.int32)
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_gpu_training
  125. @pytest.mark.env_onecard
  126. def test_spacetodepth_pynative_int64():
  127. SpaceToDepth_pynative(np.int64)
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_gpu_training
  130. @pytest.mark.env_onecard
  131. def test_spacetodepth_pynative_int8():
  132. SpaceToDepth_pynative(np.int8)
  133. @pytest.mark.level0
  134. @pytest.mark.platform_x86_gpu_training
  135. @pytest.mark.env_onecard
  136. def test_spacetodepth_pynative_int16():
  137. SpaceToDepth_pynative(np.int16)
  138. @pytest.mark.level0
  139. @pytest.mark.platform_x86_gpu_training
  140. @pytest.mark.env_onecard
  141. def test_spacetodepth_pynative_uint8():
  142. SpaceToDepth_pynative(np.uint8)
  143. @pytest.mark.level0
  144. @pytest.mark.platform_x86_gpu_training
  145. @pytest.mark.env_onecard
  146. def test_spacetodepth_pynative_uint16():
  147. SpaceToDepth_pynative(np.uint16)
  148. @pytest.mark.level0
  149. @pytest.mark.platform_x86_gpu_training
  150. @pytest.mark.env_onecard
  151. def test_spacetodepth_pynative_uint32():
  152. SpaceToDepth_pynative(np.uint32)
  153. @pytest.mark.level0
  154. @pytest.mark.platform_x86_gpu_training
  155. @pytest.mark.env_onecard
  156. def test_spacetodepth_pynative_uint64():
  157. SpaceToDepth_pynative(np.uint64)
  158. test_spacetodepth_graph_float32()
  159. test_spacetodepth_pynative_int32()