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 5.9 kB

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