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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 pytest
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. from mindspore.common.api import ms_function
  20. from mindspore.common.initializer import initializer
  21. from mindspore.common.parameter import Parameter
  22. import mindspore.nn as nn
  23. import mindspore.context as context
  24. context.set_context(device_target='CPU')
  25. class Transpose(nn.Cell):
  26. def __init__(self):
  27. super(Transpose, self).__init__()
  28. self.transpose = P.Transpose()
  29. self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.float32)), [5, 6]),
  30. name='x_2D')
  31. self.perm_2D = (1, 0)
  32. self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.float32)), [2, 2, 4]),
  33. name='x_3D')
  34. self.perm_3D = (1, 0, 2)
  35. self.x_4D = Parameter(
  36. initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2,
  37. 3, 4, 5).astype(np.float32)), [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(np.float32)),
  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. @pytest.mark.level0
  49. @pytest.mark.platform_x86_cpu
  50. @pytest.mark.env_onecard
  51. def test_transpose():
  52. transpose = Transpose()
  53. output = transpose()
  54. expect0 = np.array([[[0, 6, 12, 18, 24],
  55. [1, 7, 13, 19, 25],
  56. [2, 8, 14, 20, 26],
  57. [3, 9, 15, 21, 27],
  58. [4, 10, 16, 22, 28],
  59. [5, 11, 17, 23, 29]]]).astype(np.float32)
  60. expect1 = np.array([[[[0, 1, 2, 3],
  61. [8, 9, 10, 11]],
  62. [[4, 5, 6, 7],
  63. [12, 13, 14, 15]]]]).astype(np.float32)
  64. expect2 = np.array([[[[[0, 1, 2, 3, 4],
  65. [5, 6, 7, 8, 9],
  66. [10, 11, 12, 13, 14],
  67. [15, 16, 17, 18, 19]],
  68. [[20, 21, 22, 23, 24],
  69. [25, 26, 27, 28, 29],
  70. [30, 31, 32, 33, 34],
  71. [35, 36, 37, 38, 39]],
  72. [[40, 41, 42, 43, 44],
  73. [45, 46, 47, 48, 49],
  74. [50, 51, 52, 53, 54],
  75. [55, 56, 57, 58, 59]]],
  76. [[[60, 61, 62, 63, 64],
  77. [65, 66, 67, 68, 69],
  78. [70, 71, 72, 73, 74],
  79. [75, 76, 77, 78, 79]],
  80. [[80, 81, 82, 83, 84],
  81. [85, 86, 87, 88, 89],
  82. [90, 91, 92, 93, 94],
  83. [95, 96, 97, 98, 99]],
  84. [[100, 101, 102, 103, 104],
  85. [105, 106, 107, 108, 109],
  86. [110, 111, 112, 113, 114],
  87. [115, 116, 117, 118, 119]]]]]).astype(np.float32)
  88. expect3 = np.array([[[[[[0, 20, 40],
  89. [1, 21, 41],
  90. [2, 22, 42],
  91. [3, 23, 43],
  92. [4, 24, 44]],
  93. [[5, 25, 45],
  94. [6, 26, 46],
  95. [7, 27, 47],
  96. [8, 28, 48],
  97. [9, 29, 49]],
  98. [[10, 30, 50],
  99. [11, 31, 51],
  100. [12, 32, 52],
  101. [13, 33, 53],
  102. [14, 34, 54]],
  103. [[15, 35, 55],
  104. [16, 36, 56],
  105. [17, 37, 57],
  106. [18, 38, 58],
  107. [19, 39, 59]]]],
  108. [[[[60, 80, 100],
  109. [61, 81, 101],
  110. [62, 82, 102],
  111. [63, 83, 103],
  112. [64, 84, 104]],
  113. [[65, 85, 105],
  114. [66, 86, 106],
  115. [67, 87, 107],
  116. [68, 88, 108],
  117. [69, 89, 109]],
  118. [[70, 90, 110],
  119. [71, 91, 111],
  120. [72, 92, 112],
  121. [73, 93, 113],
  122. [74, 94, 114]],
  123. [[75, 95, 115],
  124. [76, 96, 116],
  125. [77, 97, 117],
  126. [78, 98, 118],
  127. [79, 99, 119]]]]]]).astype(np.float32)
  128. assert (output[0].asnumpy() == expect0).all()
  129. assert (output[1].asnumpy() == expect1).all()
  130. assert (output[2].asnumpy() == expect2).all()
  131. assert (output[3].asnumpy() == expect3).all()
  132. test_transpose()
  133. class Transpose_int64(nn.Cell):
  134. def __init__(self):
  135. super(Transpose_int64, self).__init__()
  136. self.transpose = P.Transpose()
  137. self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.int64)), [5, 6]),
  138. name='x_2D')
  139. self.perm_2D = (1, 0)
  140. self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.int64)), [2, 2, 4]),
  141. name='x_3D')
  142. self.perm_3D = (1, 0, 2)
  143. self.x_4D = Parameter(
  144. initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2,
  145. 3, 4, 5).astype(np.int64)), [2, 3, 4, 5]),
  146. name='x_4D')
  147. self.perm_4D = (0, 1, 2, 3)
  148. self.x_5D = Parameter(
  149. initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.int64)),
  150. [1, 2, 3, 4, 5]), name='x_5D')
  151. self.perm_5D = (1, 0, 3, 4, 2)
  152. @ms_function
  153. def construct(self):
  154. return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D),
  155. self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D))
  156. @pytest.mark.level0
  157. @pytest.mark.platform_x86_cpu
  158. @pytest.mark.env_onecard
  159. def test_transpose_int64():
  160. transpose = Transpose_int64()
  161. output = transpose()
  162. expect0 = np.array([[[0, 6, 12, 18, 24],
  163. [1, 7, 13, 19, 25],
  164. [2, 8, 14, 20, 26],
  165. [3, 9, 15, 21, 27],
  166. [4, 10, 16, 22, 28],
  167. [5, 11, 17, 23, 29]]]).astype(np.int64)
  168. expect1 = np.array([[[[0, 1, 2, 3],
  169. [8, 9, 10, 11]],
  170. [[4, 5, 6, 7],
  171. [12, 13, 14, 15]]]]).astype(np.int64)
  172. expect2 = np.array([[[[[0, 1, 2, 3, 4],
  173. [5, 6, 7, 8, 9],
  174. [10, 11, 12, 13, 14],
  175. [15, 16, 17, 18, 19]],
  176. [[20, 21, 22, 23, 24],
  177. [25, 26, 27, 28, 29],
  178. [30, 31, 32, 33, 34],
  179. [35, 36, 37, 38, 39]],
  180. [[40, 41, 42, 43, 44],
  181. [45, 46, 47, 48, 49],
  182. [50, 51, 52, 53, 54],
  183. [55, 56, 57, 58, 59]]],
  184. [[[60, 61, 62, 63, 64],
  185. [65, 66, 67, 68, 69],
  186. [70, 71, 72, 73, 74],
  187. [75, 76, 77, 78, 79]],
  188. [[80, 81, 82, 83, 84],
  189. [85, 86, 87, 88, 89],
  190. [90, 91, 92, 93, 94],
  191. [95, 96, 97, 98, 99]],
  192. [[100, 101, 102, 103, 104],
  193. [105, 106, 107, 108, 109],
  194. [110, 111, 112, 113, 114],
  195. [115, 116, 117, 118, 119]]]]]).astype(np.int64)
  196. expect3 = np.array([[[[[[0, 20, 40],
  197. [1, 21, 41],
  198. [2, 22, 42],
  199. [3, 23, 43],
  200. [4, 24, 44]],
  201. [[5, 25, 45],
  202. [6, 26, 46],
  203. [7, 27, 47],
  204. [8, 28, 48],
  205. [9, 29, 49]],
  206. [[10, 30, 50],
  207. [11, 31, 51],
  208. [12, 32, 52],
  209. [13, 33, 53],
  210. [14, 34, 54]],
  211. [[15, 35, 55],
  212. [16, 36, 56],
  213. [17, 37, 57],
  214. [18, 38, 58],
  215. [19, 39, 59]]]],
  216. [[[[60, 80, 100],
  217. [61, 81, 101],
  218. [62, 82, 102],
  219. [63, 83, 103],
  220. [64, 84, 104]],
  221. [[65, 85, 105],
  222. [66, 86, 106],
  223. [67, 87, 107],
  224. [68, 88, 108],
  225. [69, 89, 109]],
  226. [[70, 90, 110],
  227. [71, 91, 111],
  228. [72, 92, 112],
  229. [73, 93, 113],
  230. [74, 94, 114]],
  231. [[75, 95, 115],
  232. [76, 96, 116],
  233. [77, 97, 117],
  234. [78, 98, 118],
  235. [79, 99, 119]]]]]]).astype(np.int64)
  236. assert (output[0].asnumpy() == expect0).all()
  237. assert (output[1].asnumpy() == expect1).all()
  238. assert (output[2].asnumpy() == expect2).all()
  239. assert (output[3].asnumpy() == expect3).all()
  240. test_transpose_int64()
  241. class Transpose_uint8(nn.Cell):
  242. def __init__(self):
  243. super(Transpose_uint8, self).__init__()
  244. self.transpose = P.Transpose()
  245. self.x_2D = Parameter(initializer(Tensor(np.arange(5 * 6).reshape(5, 6).astype(np.uint8)), [5, 6]),
  246. name='x_2D')
  247. self.perm_2D = (1, 0)
  248. self.x_3D = Parameter(initializer(Tensor(np.arange(2 * 2 * 4).reshape(2, 2, 4).astype(np.uint8)), [2, 2, 4]),
  249. name='x_3D')
  250. self.perm_3D = (1, 0, 2)
  251. self.x_4D = Parameter(
  252. initializer(Tensor(np.arange(2 * 3 * 4 * 5).reshape(2,
  253. 3, 4, 5).astype(np.uint8)), [2, 3, 4, 5]),
  254. name='x_4D')
  255. self.perm_4D = (0, 1, 2, 3)
  256. self.x_5D = Parameter(
  257. initializer(Tensor(np.arange(1 * 2 * 3 * 4 * 5).reshape(1, 2, 3, 4, 5).astype(np.uint8)),
  258. [1, 2, 3, 4, 5]), name='x_5D')
  259. self.perm_5D = (1, 0, 3, 4, 2)
  260. @ms_function
  261. def construct(self):
  262. return (self.transpose(self.x_2D, self.perm_2D), self.transpose(self.x_3D, self.perm_3D),
  263. self.transpose(self.x_4D, self.perm_4D), self.transpose(self.x_5D, self.perm_5D))
  264. @pytest.mark.level0
  265. @pytest.mark.platform_x86_cpu
  266. @pytest.mark.env_onecard
  267. def test_transpose_uint8():
  268. transpose = Transpose_uint8()
  269. output = transpose()
  270. expect0 = np.array([[[0, 6, 12, 18, 24],
  271. [1, 7, 13, 19, 25],
  272. [2, 8, 14, 20, 26],
  273. [3, 9, 15, 21, 27],
  274. [4, 10, 16, 22, 28],
  275. [5, 11, 17, 23, 29]]]).astype(np.uint8)
  276. expect1 = np.array([[[[0, 1, 2, 3],
  277. [8, 9, 10, 11]],
  278. [[4, 5, 6, 7],
  279. [12, 13, 14, 15]]]]).astype(np.uint8)
  280. expect2 = np.array([[[[[0, 1, 2, 3, 4],
  281. [5, 6, 7, 8, 9],
  282. [10, 11, 12, 13, 14],
  283. [15, 16, 17, 18, 19]],
  284. [[20, 21, 22, 23, 24],
  285. [25, 26, 27, 28, 29],
  286. [30, 31, 32, 33, 34],
  287. [35, 36, 37, 38, 39]],
  288. [[40, 41, 42, 43, 44],
  289. [45, 46, 47, 48, 49],
  290. [50, 51, 52, 53, 54],
  291. [55, 56, 57, 58, 59]]],
  292. [[[60, 61, 62, 63, 64],
  293. [65, 66, 67, 68, 69],
  294. [70, 71, 72, 73, 74],
  295. [75, 76, 77, 78, 79]],
  296. [[80, 81, 82, 83, 84],
  297. [85, 86, 87, 88, 89],
  298. [90, 91, 92, 93, 94],
  299. [95, 96, 97, 98, 99]],
  300. [[100, 101, 102, 103, 104],
  301. [105, 106, 107, 108, 109],
  302. [110, 111, 112, 113, 114],
  303. [115, 116, 117, 118, 119]]]]]).astype(np.uint8)
  304. expect3 = np.array([[[[[[0, 20, 40],
  305. [1, 21, 41],
  306. [2, 22, 42],
  307. [3, 23, 43],
  308. [4, 24, 44]],
  309. [[5, 25, 45],
  310. [6, 26, 46],
  311. [7, 27, 47],
  312. [8, 28, 48],
  313. [9, 29, 49]],
  314. [[10, 30, 50],
  315. [11, 31, 51],
  316. [12, 32, 52],
  317. [13, 33, 53],
  318. [14, 34, 54]],
  319. [[15, 35, 55],
  320. [16, 36, 56],
  321. [17, 37, 57],
  322. [18, 38, 58],
  323. [19, 39, 59]]]],
  324. [[[[60, 80, 100],
  325. [61, 81, 101],
  326. [62, 82, 102],
  327. [63, 83, 103],
  328. [64, 84, 104]],
  329. [[65, 85, 105],
  330. [66, 86, 106],
  331. [67, 87, 107],
  332. [68, 88, 108],
  333. [69, 89, 109]],
  334. [[70, 90, 110],
  335. [71, 91, 111],
  336. [72, 92, 112],
  337. [73, 93, 113],
  338. [74, 94, 114]],
  339. [[75, 95, 115],
  340. [76, 96, 116],
  341. [77, 97, 117],
  342. [78, 98, 118],
  343. [79, 99, 119]]]]]]).astype(np.uint8)
  344. assert (output[0].asnumpy() == expect0).all()
  345. assert (output[1].asnumpy() == expect1).all()
  346. assert (output[2].asnumpy() == expect2).all()
  347. assert (output[3].asnumpy() == expect3).all()
  348. test_transpose_uint8()