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

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