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_depthtospace_op.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. def DepthToSpaceNumpy(arr, block_size):
  25. '''
  26. DepthToSpace ops use numpy
  27. '''
  28. tmpshape = arr.shape
  29. newshape = []
  30. newshape.append(tmpshape[0])
  31. newshape.append(tmpshape[1]//block_size//block_size)
  32. newshape.append(tmpshape[2]*block_size)
  33. newshape.append(tmpshape[3]*block_size)
  34. output = arr.reshape(newshape[0], newshape[1], block_size, block_size, tmpshape[2], tmpshape[3])
  35. output = np.transpose(output, (0, 1, 4, 2, 5, 3))
  36. output = output.reshape(newshape)
  37. return output
  38. class DepthToSpaceNet(nn.Cell):
  39. def __init__(self, nptype, block_size=2, input_shape=(1, 4, 3, 3)):
  40. super(DepthToSpaceNet, self).__init__()
  41. self.DepthToSpace = P.DepthToSpace(2)
  42. input_size = 1
  43. for i in input_shape:
  44. input_size = input_size*i
  45. self.data_np = np.arange(input_size).reshape(input_shape).astype(nptype)
  46. self.x = Parameter(initializer(Tensor(self.data_np), input_shape), name='x')
  47. @ms_function
  48. def construct(self):
  49. return self.DepthToSpace(self.x)
  50. def DepthToSpace(nptype, block_size=2, input_shape=(1, 4, 3, 3)):
  51. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  52. input_size = 1
  53. for i in input_shape:
  54. input_size = input_size*i
  55. expect = np.arange(input_size).reshape(input_shape).astype(nptype)
  56. expect = DepthToSpaceNumpy(expect, block_size)
  57. dts = DepthToSpaceNet(nptype, block_size, input_shape)
  58. output = dts()
  59. assert (output.asnumpy() == expect).all()
  60. def DepthToSpace_pynative(nptype, block_size=2, input_shape=(1, 4, 3, 3)):
  61. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  62. input_size = 1
  63. for i in input_shape:
  64. input_size = input_size*i
  65. expect = np.arange(input_size).reshape(input_shape).astype(nptype)
  66. expect = DepthToSpaceNumpy(expect, block_size)
  67. dts = P.DepthToSpace(2)
  68. arr_input = Tensor(np.arange(input_size).reshape(input_shape).astype(nptype))
  69. output = dts(arr_input)
  70. assert (output.asnumpy() == expect).all()
  71. @pytest.mark.level0
  72. @pytest.mark.platform_x86_gpu_training
  73. @pytest.mark.env_onecard
  74. def test_depthtospace_graph_float32():
  75. DepthToSpace(np.float32)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_depthtospace_graph_float16():
  80. DepthToSpace(np.float16)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_depthtospace_graph_int32():
  85. DepthToSpace(np.int32)
  86. @pytest.mark.level0
  87. @pytest.mark.platform_x86_gpu_training
  88. @pytest.mark.env_onecard
  89. def test_depthtospace_graph_int64():
  90. DepthToSpace(np.int64)
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_depthtospace_graph_int8():
  95. DepthToSpace(np.int8)
  96. @pytest.mark.level0
  97. @pytest.mark.platform_x86_gpu_training
  98. @pytest.mark.env_onecard
  99. def test_depthtospace_graph_int16():
  100. DepthToSpace(np.int16)
  101. @pytest.mark.level0
  102. @pytest.mark.platform_x86_gpu_training
  103. @pytest.mark.env_onecard
  104. def test_depthtospace_graph_uint8():
  105. DepthToSpace(np.uint8)
  106. @pytest.mark.level0
  107. @pytest.mark.platform_x86_gpu_training
  108. @pytest.mark.env_onecard
  109. def test_depthtospace_graph_uint16():
  110. DepthToSpace(np.uint16)
  111. @pytest.mark.level0
  112. @pytest.mark.platform_x86_gpu_training
  113. @pytest.mark.env_onecard
  114. def test_depthtospace_graph_uint32():
  115. DepthToSpace(np.uint32)
  116. @pytest.mark.level0
  117. @pytest.mark.platform_x86_gpu_training
  118. @pytest.mark.env_onecard
  119. def test_depthtospace_graph_uint64():
  120. DepthToSpace(np.uint64)
  121. @pytest.mark.level0
  122. @pytest.mark.platform_x86_gpu_training
  123. @pytest.mark.env_onecard
  124. def test_depthtospace_pynative_float32():
  125. DepthToSpace_pynative(np.float32)
  126. @pytest.mark.level0
  127. @pytest.mark.platform_x86_gpu_training
  128. @pytest.mark.env_onecard
  129. def test_depthtospace_pynative_float16():
  130. DepthToSpace_pynative(np.float16)
  131. @pytest.mark.level0
  132. @pytest.mark.platform_x86_gpu_training
  133. @pytest.mark.env_onecard
  134. def test_depthtospace_pynative_int32():
  135. DepthToSpace_pynative(np.int32)
  136. @pytest.mark.level0
  137. @pytest.mark.platform_x86_gpu_training
  138. @pytest.mark.env_onecard
  139. def test_depthtospace_pynative_int64():
  140. DepthToSpace_pynative(np.int64)
  141. @pytest.mark.level0
  142. @pytest.mark.platform_x86_gpu_training
  143. @pytest.mark.env_onecard
  144. def test_depthtospace_pynative_int8():
  145. DepthToSpace_pynative(np.int8)
  146. @pytest.mark.level0
  147. @pytest.mark.platform_x86_gpu_training
  148. @pytest.mark.env_onecard
  149. def test_depthtospace_pynative_int16():
  150. DepthToSpace_pynative(np.int16)
  151. @pytest.mark.level0
  152. @pytest.mark.platform_x86_gpu_training
  153. @pytest.mark.env_onecard
  154. def test_depthtospace_pynative_uint8():
  155. DepthToSpace_pynative(np.uint8)
  156. @pytest.mark.level0
  157. @pytest.mark.platform_x86_gpu_training
  158. @pytest.mark.env_onecard
  159. def test_depthtospace_pynative_uint16():
  160. DepthToSpace_pynative(np.uint16)
  161. @pytest.mark.level0
  162. @pytest.mark.platform_x86_gpu_training
  163. @pytest.mark.env_onecard
  164. def test_depthtospace_pynative_uint32():
  165. DepthToSpace_pynative(np.uint32)
  166. @pytest.mark.level0
  167. @pytest.mark.platform_x86_gpu_training
  168. @pytest.mark.env_onecard
  169. def test_depthtospace_pynative_uint64():
  170. DepthToSpace_pynative(np.uint64)