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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. import mindspore.nn as nn
  20. import mindspore.ops as ops
  21. import mindspore.context as context
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class ConcatV10(nn.Cell):
  24. def __init__(self, nptype):
  25. super(ConcatV10, self).__init__()
  26. self.cat = P.Concat(axis=2)
  27. self.x1 = Tensor(np.array([[[0., 0., 1.],
  28. [1., 2., 3.]],
  29. [[2., 4., 5.],
  30. [3., 6., 7.]]]).astype(nptype))
  31. def construct(self):
  32. return self.cat((self.x1,))
  33. def axis10(nptype):
  34. cat = ConcatV10(nptype)
  35. output = cat()
  36. expect = np.array([[[0., 0., 1.],
  37. [1., 2., 3.]],
  38. [[2., 4., 5.],
  39. [3., 6., 7.]]]).astype(nptype)
  40. print(output)
  41. assert (output.asnumpy() == expect).all()
  42. @pytest.mark.level0
  43. @pytest.mark.platform_x86_cpu
  44. @pytest.mark.env_onecard
  45. def test_axis10_float32():
  46. axis10(np.float32)
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_cpu
  49. @pytest.mark.env_onecard
  50. def test_axis10_int32():
  51. axis10(np.int32)
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_cpu
  54. @pytest.mark.env_onecard
  55. def test_axis10_bool():
  56. axis10(np.bool)
  57. class ConcatV32(nn.Cell):
  58. def __init__(self, nptype):
  59. super(ConcatV32, self).__init__()
  60. self.cat = P.Concat(axis=2)
  61. self.x1 = Tensor(np.arange(2 * 2 * 1).reshape(2, 2, 1).astype(nptype))
  62. self.x2 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2).astype(nptype))
  63. def construct(self):
  64. return self.cat((self.x1, self.x2))
  65. def axis32(nptype):
  66. cat = ConcatV32(nptype)
  67. output = cat()
  68. expect = np.array([[[0., 0., 1.],
  69. [1., 2., 3.]],
  70. [[2., 4., 5.],
  71. [3., 6., 7.]]]).astype(nptype)
  72. print(output)
  73. assert (output.asnumpy() == expect).all()
  74. @pytest.mark.level0
  75. @pytest.mark.platform_x86_cpu
  76. @pytest.mark.env_onecard
  77. def test_axis32_float32():
  78. axis32(np.float32)
  79. @pytest.mark.level0
  80. @pytest.mark.platform_x86_cpu
  81. @pytest.mark.env_onecard
  82. def test_axis32_int32():
  83. axis32(np.int32)
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_cpu
  86. @pytest.mark.env_onecard
  87. def test_axis32_bool():
  88. axis32(np.bool)
  89. class ConcatWithList(nn.Cell):
  90. def __init__(self):
  91. super(ConcatWithList, self).__init__()
  92. self.concat = P.Concat(axis=2)
  93. def construct(self, x, y):
  94. input_list = [x, y]
  95. return self.concat(input_list)
  96. class ConcatWithTuple(nn.Cell):
  97. def __init__(self):
  98. super(ConcatWithTuple, self).__init__()
  99. self.concat = P.Concat(axis=2)
  100. def construct(self, x, y):
  101. input_list = (x, y)
  102. return self.concat(input_list)
  103. class GradConcat(nn.Cell):
  104. def __init__(self, network):
  105. super(GradConcat, self).__init__()
  106. self.grad = ops.GradOperation()
  107. self.network = network
  108. def construct(self, x, y):
  109. gout = self.grad(self.network)(x, y)
  110. return gout
  111. @pytest.mark.level0
  112. @pytest.mark.platform_x86_cpu
  113. @pytest.mark.env_onecard
  114. def test_concat_list_grad():
  115. x1 = Tensor(np.arange(2 * 2 * 1).reshape(2, 2, 1).astype(np.float32))
  116. x2 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2).astype(np.float32))
  117. concat = ConcatWithList()
  118. output = GradConcat(concat)(x1, x2)
  119. expect = np.array([[[1.],
  120. [1.]],
  121. [[1.],
  122. [1.]]]).astype(np.float32)
  123. print(output)
  124. assert (output.asnumpy() == expect).all()
  125. @pytest.mark.level0
  126. @pytest.mark.platform_x86_cpu
  127. @pytest.mark.env_onecard
  128. def test_concat_tuple_grad():
  129. x1 = Tensor(np.arange(2 * 2 * 1).reshape(2, 2, 1).astype(np.float32))
  130. x2 = Tensor(np.arange(2 * 2 * 2).reshape(2, 2, 2).astype(np.float32))
  131. concat = ConcatWithTuple()
  132. output = GradConcat(concat)(x1, x2)
  133. expect = np.array([[[1.],
  134. [1.]],
  135. [[1.],
  136. [1.]]]).astype(np.float32)
  137. print(output)
  138. assert (output.asnumpy() == expect).all()
  139. class ConcatV43(nn.Cell):
  140. def __init__(self, nptype):
  141. super(ConcatV43, self).__init__()
  142. self.cat = P.Concat(axis=3)
  143. self.x1 = Tensor(np.arange(2 * 2 * 2 * 2).reshape(2, 2, 2, 2).astype(nptype))
  144. self.x2 = Tensor(np.arange(2 * 2 * 2 * 3).reshape(2, 2, 2, 3).astype(nptype))
  145. def construct(self):
  146. return self.cat((self.x1, self.x2))
  147. def axis43(nptype):
  148. cat = ConcatV43(nptype)
  149. output = cat()
  150. expect = np.array([[[[0., 1., 0., 1., 2.],
  151. [2., 3., 3., 4., 5.]],
  152. [[4., 5., 6., 7., 8.],
  153. [6., 7., 9., 10., 11.]]],
  154. [[[8., 9., 12., 13., 14.],
  155. [10., 11., 15., 16., 17.]],
  156. [[12., 13., 18., 19., 20.],
  157. [14., 15., 21., 22., 23.]]]]).astype(nptype)
  158. assert (output.asnumpy() == expect).all()
  159. print(output)
  160. @pytest.mark.level0
  161. @pytest.mark.platform_x86_cpu
  162. @pytest.mark.env_onecard
  163. def test_axis43_float32():
  164. axis43(np.float32)
  165. @pytest.mark.level0
  166. @pytest.mark.platform_x86_cpu
  167. @pytest.mark.env_onecard
  168. def test_axis43_int32():
  169. axis43(np.int32)
  170. @pytest.mark.level0
  171. @pytest.mark.platform_x86_cpu
  172. @pytest.mark.env_onecard
  173. def test_axis43_bool():
  174. axis43(np.bool)
  175. class ConcatV21(nn.Cell):
  176. def __init__(self, nptype):
  177. super(ConcatV21, self).__init__()
  178. self.cat = P.Concat(axis=1)
  179. self.x1 = Tensor(np.arange(2 * 2).reshape(2, 2).astype(nptype))
  180. self.x2 = Tensor(np.arange(2 * 3).reshape(2, 3).astype(nptype))
  181. def construct(self):
  182. return self.cat((self.x1, self.x2))
  183. def axis21(nptype):
  184. cat = ConcatV21(nptype)
  185. output = cat()
  186. expect = np.array([[0., 1., 0., 1., 2.],
  187. [2., 3., 3., 4., 5.]]).astype(nptype)
  188. assert (output.asnumpy() == expect).all()
  189. print(output)
  190. @pytest.mark.level0
  191. @pytest.mark.platform_x86_cpu
  192. @pytest.mark.env_onecard
  193. def test_axis21_float32():
  194. axis21(np.float32)
  195. @pytest.mark.level0
  196. @pytest.mark.platform_x86_cpu
  197. @pytest.mark.env_onecard
  198. def test_axis21_int32():
  199. axis21(np.int32)
  200. @pytest.mark.level0
  201. @pytest.mark.platform_x86_cpu
  202. @pytest.mark.env_onecard
  203. def test_axis21_bool():
  204. axis21(np.bool)
  205. class Concat3INet(nn.Cell):
  206. def __init__(self):
  207. super(Concat3INet, self).__init__()
  208. self.cat = P.Concat(axis=1)
  209. def construct(self, x1, x2, x3):
  210. return self.cat((x1, x2, x3))
  211. def concat_3i(nptype):
  212. cat = Concat3INet()
  213. x1_np = np.random.randn(32, 4, 224, 224).astype(nptype)
  214. x2_np = np.random.randn(32, 8, 224, 224).astype(nptype)
  215. x3_np = np.random.randn(32, 10, 224, 224).astype(nptype)
  216. output_np = np.concatenate((x1_np, x2_np, x3_np), axis=1)
  217. x1_ms = Tensor(x1_np)
  218. x2_ms = Tensor(x2_np)
  219. x3_ms = Tensor(x3_np)
  220. output_ms = cat(x1_ms, x2_ms, x3_ms)
  221. error = np.ones(shape=output_np.shape) * 10e-6
  222. diff = output_ms.asnumpy() - output_np
  223. assert np.all(diff < error)
  224. @pytest.mark.level0
  225. @pytest.mark.platform_x86_cpu
  226. @pytest.mark.env_onecard
  227. def test_concat_3i_float32():
  228. concat_3i(np.float32)
  229. @pytest.mark.level0
  230. @pytest.mark.platform_x86_cpu
  231. @pytest.mark.env_onecard
  232. def test_concat_3i_int32():
  233. concat_3i(np.int32)
  234. @pytest.mark.level0
  235. @pytest.mark.platform_x86_cpu
  236. @pytest.mark.env_onecard
  237. def test_concat_3i_bool():
  238. cat = Concat3INet()
  239. x1_np = np.random.choice([True, False], (32, 4, 224, 224)).astype(np.bool)
  240. x2_np = np.random.choice([True, False], (32, 8, 224, 224)).astype(np.bool)
  241. x3_np = np.random.choice([True, False], (32, 10, 224, 224)).astype(np.bool)
  242. output_np = np.concatenate((x1_np, x2_np, x3_np), axis=1)
  243. x1_ms = Tensor(x1_np)
  244. x2_ms = Tensor(x2_np)
  245. x3_ms = Tensor(x3_np)
  246. output_ms = cat(x1_ms, x2_ms, x3_ms)
  247. assert (output_ms.asnumpy() == output_np).all()
  248. class Concat4INet(nn.Cell):
  249. def __init__(self):
  250. super(Concat4INet, self).__init__()
  251. self.cat = P.Concat(axis=1)
  252. def construct(self, x1, x2, x3, x4):
  253. return self.cat((x1, x2, x3, x4))
  254. def concat_4i(nptype):
  255. cat = Concat4INet()
  256. x1_np = np.random.randn(32, 4, 224, 224).astype(nptype)
  257. x2_np = np.random.randn(32, 8, 224, 224).astype(nptype)
  258. x3_np = np.random.randn(32, 10, 224, 224).astype(nptype)
  259. x4_np = np.random.randn(32, 5, 224, 224).astype(nptype)
  260. output_np = np.concatenate((x1_np, x2_np, x3_np, x4_np), axis=1)
  261. x1_ms = Tensor(x1_np)
  262. x2_ms = Tensor(x2_np)
  263. x3_ms = Tensor(x3_np)
  264. x4_ms = Tensor(x4_np)
  265. output_ms = cat(x1_ms, x2_ms, x3_ms, x4_ms)
  266. error = np.ones(shape=output_np.shape) * 10e-6
  267. diff = output_ms.asnumpy() - output_np
  268. assert np.all(diff < error)
  269. @pytest.mark.level0
  270. @pytest.mark.platform_x86_cpu
  271. @pytest.mark.env_onecard
  272. def test_concat_4i_float32():
  273. concat_4i(np.float32)
  274. @pytest.mark.level0
  275. @pytest.mark.platform_x86_cpu
  276. @pytest.mark.env_onecard
  277. def test_concat_4i_int32():
  278. concat_4i(np.int32)
  279. @pytest.mark.level0
  280. @pytest.mark.platform_x86_cpu
  281. @pytest.mark.env_onecard
  282. def test_concat_4i_int8():
  283. concat_4i(np.int8)
  284. @pytest.mark.level0
  285. @pytest.mark.platform_x86_cpu
  286. @pytest.mark.env_onecard
  287. def test_concat_4i_uint64():
  288. concat_4i(np.uint64)
  289. @pytest.mark.level0
  290. @pytest.mark.platform_x86_cpu
  291. @pytest.mark.env_onecard
  292. def test_concat_4i_bool():
  293. cat = Concat4INet()
  294. x1_np = np.random.choice([True, False], (32, 4, 224, 224)).astype(np.bool)
  295. x2_np = np.random.choice([True, False], (32, 8, 224, 224)).astype(np.bool)
  296. x3_np = np.random.choice([True, False], (32, 10, 224, 224)).astype(np.bool)
  297. x4_np = np.random.choice([True, False], (32, 5, 224, 224)).astype(np.bool)
  298. output_np = np.concatenate((x1_np, x2_np, x3_np, x4_np), axis=1)
  299. x1_ms = Tensor(x1_np)
  300. x2_ms = Tensor(x2_np)
  301. x3_ms = Tensor(x3_np)
  302. x4_ms = Tensor(x4_np)
  303. output_ms = cat(x1_ms, x2_ms, x3_ms, x4_ms)
  304. assert (output_ms.asnumpy() == output_np).all()