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_transpose_op.py 11 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # Copyright 2019 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.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore.ops import operations as P
  24. from mindspore.ops.operations import _inner_ops as inner
  25. context.set_context(device_target='GPU')
  26. class Transpose(nn.Cell):
  27. def __init__(self, nptype):
  28. super(Transpose, self).__init__()
  29. self.transpose = P.Transpose()
  30. self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(nptype)), [5, 6]),
  31. name='x_2D')
  32. self.perm_2D = (1, 0)
  33. self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(nptype)), [2, 2, 4]),
  34. name='x_3D')
  35. self.perm_3D = (1, 0, 2)
  36. self.x_4D = Parameter(
  37. initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2, 3, 4, 5).astype(nptype)), [2, 3, 4, 5]),
  38. name='x_4D')
  39. self.perm_4D = (0, 1, 2, 3)
  40. self.x_5D = Parameter(
  41. initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(nptype)),
  42. [1, 2, 3, 4, 5]), name='x_5D')
  43. self.perm_5D = (1, 0, 3, 4, 2)
  44. @ms_function
  45. def construct(self):
  46. return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D),
  47. self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D))
  48. class Transpose_dynamic(nn.Cell):
  49. def __init__(self, nptype):
  50. super(Transpose_dynamic, self).__init__()
  51. self.transpose = P.Transpose()
  52. self.test_dynamic = inner.GpuConvertToDynamicShape()
  53. self.x = Parameter(
  54. initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(nptype)),
  55. [1, 2, 3, 4, 5]), name='5D')
  56. self.perm = (1, 0, 3, 4, 2)
  57. @ms_function
  58. def construct(self):
  59. out = self.test_dynamic(self.x)
  60. return self.transpose(out, self.perm)
  61. class Transpose_dynamic2(nn.Cell):
  62. def __init__(self, input_1, input_2, perm_1, perm_2):
  63. super(Transpose_dynamic2, self).__init__()
  64. self.transpose = P.Transpose()
  65. self.test_dynamic = inner.GpuConvertToDynamicShape()
  66. self.x_1 = input_1
  67. self.x_2 = input_2
  68. self.perm_1 = perm_1
  69. self.perm_2 = perm_2
  70. @ms_function
  71. def construct(self):
  72. out_1 = self.test_dynamic(self.x_1)
  73. out_1 = self.transpose(out_1, self.perm_1)
  74. out_2 = self.test_dynamic(self.x_2)
  75. out_2 = self.transpose(out_2, self.perm_2)
  76. return (out_1, out_2)
  77. def transpose1(nptype):
  78. transpose = Transpose(nptype)
  79. output = transpose()
  80. expect0 = np.array([[[0, 6, 12, 18, 24],
  81. [1, 7, 13, 19, 25],
  82. [2, 8, 14, 20, 26],
  83. [3, 9, 15, 21, 27],
  84. [4, 10, 16, 22, 28],
  85. [5, 11, 17, 23, 29]]]).astype(nptype)
  86. expect1 = np.array([[[[0, 1, 2, 3],
  87. [8, 9, 10, 11]],
  88. [[4, 5, 6, 7],
  89. [12, 13, 14, 15]]]]).astype(nptype)
  90. expect2 = np.array([[[[[0, 1, 2, 3, 4],
  91. [5, 6, 7, 8, 9],
  92. [10, 11, 12, 13, 14],
  93. [15, 16, 17, 18, 19]],
  94. [[20, 21, 22, 23, 24],
  95. [25, 26, 27, 28, 29],
  96. [30, 31, 32, 33, 34],
  97. [35, 36, 37, 38, 39]],
  98. [[40, 41, 42, 43, 44],
  99. [45, 46, 47, 48, 49],
  100. [50, 51, 52, 53, 54],
  101. [55, 56, 57, 58, 59]]],
  102. [[[60, 61, 62, 63, 64],
  103. [65, 66, 67, 68, 69],
  104. [70, 71, 72, 73, 74],
  105. [75, 76, 77, 78, 79]],
  106. [[80, 81, 82, 83, 84],
  107. [85, 86, 87, 88, 89],
  108. [90, 91, 92, 93, 94],
  109. [95, 96, 97, 98, 99]],
  110. [[100, 101, 102, 103, 104],
  111. [105, 106, 107, 108, 109],
  112. [110, 111, 112, 113, 114],
  113. [115, 116, 117, 118, 119]]]]]).astype(nptype)
  114. expect3 = np.array([[[[[[0, 20, 40],
  115. [1, 21, 41],
  116. [2, 22, 42],
  117. [3, 23, 43],
  118. [4, 24, 44]],
  119. [[5, 25, 45],
  120. [6, 26, 46],
  121. [7, 27, 47],
  122. [8, 28, 48],
  123. [9, 29, 49]],
  124. [[10, 30, 50],
  125. [11, 31, 51],
  126. [12, 32, 52],
  127. [13, 33, 53],
  128. [14, 34, 54]],
  129. [[15, 35, 55],
  130. [16, 36, 56],
  131. [17, 37, 57],
  132. [18, 38, 58],
  133. [19, 39, 59]]]],
  134. [[[[60, 80, 100],
  135. [61, 81, 101],
  136. [62, 82, 102],
  137. [63, 83, 103],
  138. [64, 84, 104]],
  139. [[65, 85, 105],
  140. [66, 86, 106],
  141. [67, 87, 107],
  142. [68, 88, 108],
  143. [69, 89, 109]],
  144. [[70, 90, 110],
  145. [71, 91, 111],
  146. [72, 92, 112],
  147. [73, 93, 113],
  148. [74, 94, 114]],
  149. [[75, 95, 115],
  150. [76, 96, 116],
  151. [77, 97, 117],
  152. [78, 98, 118],
  153. [79, 99, 119]]]]]]).astype(nptype)
  154. assert (output[0].asnumpy() == expect0).all()
  155. assert (output[1].asnumpy() == expect1).all()
  156. assert (output[2].asnumpy() == expect2).all()
  157. assert (output[3].asnumpy() == expect3).all()
  158. def transpose_d(nptype):
  159. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  160. transpose = Transpose_dynamic(nptype)
  161. output = transpose()
  162. expect = np.array([[[[[[0, 20, 40],
  163. [1, 21, 41],
  164. [2, 22, 42],
  165. [3, 23, 43],
  166. [4, 24, 44]],
  167. [[5, 25, 45],
  168. [6, 26, 46],
  169. [7, 27, 47],
  170. [8, 28, 48],
  171. [9, 29, 49]],
  172. [[10, 30, 50],
  173. [11, 31, 51],
  174. [12, 32, 52],
  175. [13, 33, 53],
  176. [14, 34, 54]],
  177. [[15, 35, 55],
  178. [16, 36, 56],
  179. [17, 37, 57],
  180. [18, 38, 58],
  181. [19, 39, 59]]]],
  182. [[[[60, 80, 100],
  183. [61, 81, 101],
  184. [62, 82, 102],
  185. [63, 83, 103],
  186. [64, 84, 104]],
  187. [[65, 85, 105],
  188. [66, 86, 106],
  189. [67, 87, 107],
  190. [68, 88, 108],
  191. [69, 89, 109]],
  192. [[70, 90, 110],
  193. [71, 91, 111],
  194. [72, 92, 112],
  195. [73, 93, 113],
  196. [74, 94, 114]],
  197. [[75, 95, 115],
  198. [76, 96, 116],
  199. [77, 97, 117],
  200. [78, 98, 118],
  201. [79, 99, 119]]]]]]).astype(nptype)
  202. assert (output.asnumpy() == expect).all()
  203. def transpose_d2(nptype):
  204. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  205. input_1 = Parameter(Tensor(np.arange(5 * 6).reshape(5, 6).astype(nptype)),
  206. name="input_1")
  207. input_2 = Parameter(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(nptype)),
  208. name="input_2")
  209. perm_1 = (1, 0)
  210. perm_2 = (1, 0, 2)
  211. expect_1 = np.array([[[0, 6, 12, 18, 24],
  212. [1, 7, 13, 19, 25],
  213. [2, 8, 14, 20, 26],
  214. [3, 9, 15, 21, 27],
  215. [4, 10, 16, 22, 28],
  216. [5, 11, 17, 23, 29]]]).astype(nptype)
  217. expect_2 = np.array([[[[0, 1, 2, 3],
  218. [8, 9, 10, 11]],
  219. [[4, 5, 6, 7],
  220. [12, 13, 14, 15]]]]).astype(nptype)
  221. net = Transpose_dynamic2(input_1, input_2, perm_1, perm_2)
  222. output_1, output_2 = net()
  223. assert (output_1.asnumpy() == expect_1).all()
  224. assert (output_2.asnumpy() == expect_2).all()
  225. @pytest.mark.level0
  226. @pytest.mark.platform_x86_gpu_training
  227. @pytest.mark.env_onecard
  228. def test_transpose_float32():
  229. transpose1(np.float32)
  230. @pytest.mark.level0
  231. @pytest.mark.platform_x86_gpu_training
  232. @pytest.mark.env_onecard
  233. def test_transpose_float16():
  234. transpose1(np.float16)
  235. @pytest.mark.level0
  236. @pytest.mark.platform_x86_gpu_training
  237. @pytest.mark.env_onecard
  238. def test_transpose_int32():
  239. transpose1(np.int32)
  240. @pytest.mark.level0
  241. @pytest.mark.platform_x86_gpu_training
  242. @pytest.mark.env_onecard
  243. def test_transpose_dynamic_float32():
  244. transpose_d(np.float32)
  245. @pytest.mark.level0
  246. @pytest.mark.platform_x86_gpu_training
  247. @pytest.mark.env_onecard
  248. def test_transpose_dynamic_float16():
  249. transpose_d(np.float16)
  250. @pytest.mark.level0
  251. @pytest.mark.platform_x86_gpu_training
  252. @pytest.mark.env_onecard
  253. def test_transpose_dynamic_int32():
  254. transpose_d(np.int32)
  255. @pytest.mark.level0
  256. @pytest.mark.platform_x86_gpu_training
  257. @pytest.mark.env_onecard
  258. def test_transpose_dynamic_two_inputs_float32():
  259. transpose_d2(np.float32)
  260. @pytest.mark.level0
  261. @pytest.mark.platform_x86_gpu_training
  262. @pytest.mark.env_onecard
  263. def test_transpose_dynamic_two_inputs_float16():
  264. transpose_d2(np.float16)
  265. @pytest.mark.level0
  266. @pytest.mark.platform_x86_gpu_training
  267. @pytest.mark.env_onecard
  268. def test_transpose_dynamic_two_inputs_int32():
  269. transpose_d2(np.int32)