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_reshape.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "layer/reshape.h"
  15. #include "testutil.h"
  16. static int test_reshape(const ncnn::Mat& a, int outw, int outh, int outd, int outc)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, outw); // w
  20. pd.set(1, outh); // h
  21. pd.set(11, outd); // d
  22. pd.set(2, outc); // c
  23. std::vector<ncnn::Mat> weights(0);
  24. int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
  25. if (ret != 0)
  26. {
  27. fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc);
  28. }
  29. return ret;
  30. }
  31. static int test_reshape_permute(const ncnn::Mat& a, int outw, int outh, int outd, int outc)
  32. {
  33. ncnn::ParamDict pd;
  34. pd.set(0, outw); // w
  35. pd.set(1, outh); // h
  36. pd.set(11, outd); // d
  37. pd.set(2, outc); // c
  38. pd.set(3, 1); // permute
  39. std::vector<ncnn::Mat> weights(0);
  40. int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
  41. if (ret != 0)
  42. {
  43. fprintf(stderr, "test_reshape_permute failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc);
  44. }
  45. return ret;
  46. }
  47. static int test_reshape_0()
  48. {
  49. ncnn::Mat a = RandomMat(3, 4, 5, 16);
  50. return 0
  51. || test_reshape(a, 5, 4, 3, 16)
  52. || test_reshape(a, 3, 4, 16, 5)
  53. || test_reshape(a, 16, 5, 4, 3)
  54. || test_reshape(a, 2, 3, 8, -1)
  55. || test_reshape(a, 3, 8, -1, 2)
  56. || test_reshape(a, 4, -1, 4, 4)
  57. || test_reshape(a, -1, 8, 3, 2)
  58. || test_reshape(a, 8, 3, -233, -1)
  59. || test_reshape(a, 4, -1, -233, 4)
  60. || test_reshape(a, -1, 3, -233, 8)
  61. || test_reshape(a, 4, -1, -233, -233)
  62. || test_reshape(a, -1, 3, -233, -233)
  63. || test_reshape(a, -1, -233, -233, -233);
  64. }
  65. static int test_reshape_1()
  66. {
  67. ncnn::Mat a = RandomMat(4, 5, 6, 13);
  68. return 0
  69. || test_reshape(a, 6, 5, 4, 13)
  70. || test_reshape(a, 4, 13, 6, 5)
  71. || test_reshape(a, 13, 5, 6, 4)
  72. || test_reshape(a, 2, 5, 4, -1)
  73. || test_reshape(a, 13, 2, -1, 5)
  74. || test_reshape(a, 13, -1, 3, 4)
  75. || test_reshape(a, -1, 13, 3, 8)
  76. || test_reshape(a, 13, 2, -233, -1)
  77. || test_reshape(a, 13, -1, -233, 6)
  78. || test_reshape(a, -1, 13, -233, 8)
  79. || test_reshape(a, 6, -1, -233, -233)
  80. || test_reshape(a, -1, 24, -233, -233)
  81. || test_reshape(a, -1, -233, -233, -233);
  82. }
  83. static int test_reshape_2()
  84. {
  85. ncnn::Mat a = RandomMat(3, 7, 16);
  86. return 0
  87. || test_reshape(a, 3, 8, 2, 7)
  88. || test_reshape(a, 2, 3, 7, 8)
  89. || test_reshape(a, 7, 3, -233, 16)
  90. || test_reshape(a, 3, 16, -233, 7)
  91. || test_reshape(a, 16, 7, -233, 3)
  92. || test_reshape(a, 2, 3, -233, -1)
  93. || test_reshape(a, -1, 8, -233, 2)
  94. || test_reshape(a, -1, 4, -233, -233)
  95. || test_reshape(a, 8, -1, -233, -233)
  96. || test_reshape(a, 16, 21, -233, -233)
  97. || test_reshape(a, -1, -233, -233, -233);
  98. }
  99. static int test_reshape_3()
  100. {
  101. ncnn::Mat a = RandomMat(4, 14, 13);
  102. return 0
  103. || test_reshape(a, 13, 4, 2, 7)
  104. || test_reshape(a, 1, 13, 7, 8)
  105. || test_reshape(a, 14, 4, -233, 13)
  106. || test_reshape(a, 4, 13, -233, 14)
  107. || test_reshape(a, 13, 14, -233, 4)
  108. || test_reshape(a, 2, 7, -233, -1)
  109. || test_reshape(a, -1, 13, -233, 2)
  110. || test_reshape(a, -1, 4, -233, -233)
  111. || test_reshape(a, 8, -1, -233, -233)
  112. || test_reshape(a, 8, 91, -233, -233)
  113. || test_reshape(a, -1, -233, -233, -233);
  114. }
  115. static int test_reshape_4()
  116. {
  117. ncnn::Mat a = RandomMat(14, 16);
  118. return 0
  119. || test_reshape(a, 2, 7, 2, 8)
  120. || test_reshape(a, 8, 1, 7, 4)
  121. || test_reshape(a, 7, 2, -233, 16)
  122. || test_reshape(a, 2, 16, -233, 7)
  123. || test_reshape(a, 16, 7, -233, 2)
  124. || test_reshape(a, 2, 4, -233, -1)
  125. || test_reshape(a, -1, 8, -233, 2)
  126. || test_reshape(a, 28, 8, -233, -233)
  127. || test_reshape(a, -1, 7, -233, -233)
  128. || test_reshape(a, 16, -1, -233, -233)
  129. || test_reshape(a, -1, -233, -233, -233);
  130. }
  131. static int test_reshape_5()
  132. {
  133. ncnn::Mat a = RandomMat(12, 14);
  134. return 0
  135. || test_reshape(a, 4, 3, 2, 7)
  136. || test_reshape(a, 1, 3, 14, 4)
  137. || test_reshape(a, 7, 2, -233, 12)
  138. || test_reshape(a, 2, 12, -233, 7)
  139. || test_reshape(a, 12, 7, -233, 2)
  140. || test_reshape(a, 2, 4, -233, -1)
  141. || test_reshape(a, -1, 4, -233, 2)
  142. || test_reshape(a, 21, 8, -233, -233)
  143. || test_reshape(a, -1, 7, -233, -233)
  144. || test_reshape(a, 3, -1, -233, -233)
  145. || test_reshape(a, -1, -233, -233, -233);
  146. }
  147. static int test_reshape_6()
  148. {
  149. ncnn::Mat a = RandomMat(120);
  150. return 0
  151. || test_reshape(a, 1, 1, 1, 120)
  152. || test_reshape(a, 10, 1, 1, 12)
  153. || test_reshape(a, 3, 5, -233, 8)
  154. || test_reshape(a, 3, 8, -233, 5)
  155. || test_reshape(a, 8, 5, -233, 3)
  156. || test_reshape(a, 2, 5, -233, -1)
  157. || test_reshape(a, -1, 5, -233, 2)
  158. || test_reshape(a, 4, 30, -233, -233)
  159. || test_reshape(a, -1, 2, -233, -233)
  160. || test_reshape(a, 24, -1, -233, -233)
  161. || test_reshape(a, -1, -233, -233, -233);
  162. }
  163. static int test_reshape_7()
  164. {
  165. ncnn::Mat a = RandomMat(210);
  166. return 0
  167. || test_reshape(a, 1, 1, 210, 1)
  168. || test_reshape(a, 5, 2, 7, 3)
  169. || test_reshape(a, 3, 5, -233, 14)
  170. || test_reshape(a, 3, 14, -233, 5)
  171. || test_reshape(a, 14, 5, -233, 3)
  172. || test_reshape(a, 2, 5, -233, -1)
  173. || test_reshape(a, -1, 5, -233, 2)
  174. || test_reshape(a, 6, 35, -233, -233)
  175. || test_reshape(a, -1, 7, -233, -233)
  176. || test_reshape(a, 21, -1, -233, -233)
  177. || test_reshape(a, -1, -233, -233, -233);
  178. }
  179. static int test_reshape_8()
  180. {
  181. ncnn::Mat a = RandomMat(3, 4, 5, 16);
  182. return 0
  183. || test_reshape_permute(a, 5, 4, 3, 16)
  184. || test_reshape_permute(a, 3, 4, 16, 5)
  185. || test_reshape_permute(a, 16, 5, 4, 3)
  186. || test_reshape_permute(a, 2, 3, 8, -1)
  187. || test_reshape_permute(a, 3, 8, -1, 2)
  188. || test_reshape_permute(a, 4, -1, 4, 4)
  189. || test_reshape_permute(a, -1, 8, 3, 2)
  190. || test_reshape_permute(a, 8, 3, -233, -1)
  191. || test_reshape_permute(a, 4, -1, -233, 4)
  192. || test_reshape_permute(a, -1, 3, -233, 8)
  193. || test_reshape_permute(a, 4, -1, -233, -233)
  194. || test_reshape_permute(a, -1, 3, -233, -233)
  195. || test_reshape_permute(a, -1, -233, -233, -233);
  196. }
  197. static int test_reshape_9()
  198. {
  199. ncnn::Mat a = RandomMat(4, 5, 6, 13);
  200. return 0
  201. || test_reshape_permute(a, 6, 5, 4, 13)
  202. || test_reshape_permute(a, 4, 13, 6, 5)
  203. || test_reshape_permute(a, 13, 5, 6, 4)
  204. || test_reshape_permute(a, 2, 5, 4, -1)
  205. || test_reshape_permute(a, 13, 2, -1, 5)
  206. || test_reshape_permute(a, 13, -1, 3, 4)
  207. || test_reshape_permute(a, -1, 13, 3, 8)
  208. || test_reshape_permute(a, 13, 2, -233, -1)
  209. || test_reshape_permute(a, 13, -1, -233, 6)
  210. || test_reshape_permute(a, -1, 13, -233, 8)
  211. || test_reshape_permute(a, 6, -1, -233, -233)
  212. || test_reshape_permute(a, -1, 24, -233, -233)
  213. || test_reshape_permute(a, -1, -233, -233, -233);
  214. }
  215. static int test_reshape_10()
  216. {
  217. ncnn::Mat a = RandomMat(3, 7, 16);
  218. return 0
  219. || test_reshape_permute(a, 3, 8, 2, 7)
  220. || test_reshape_permute(a, 2, 3, 7, 8)
  221. || test_reshape_permute(a, 7, 3, -233, 16)
  222. || test_reshape_permute(a, 3, 16, -233, 7)
  223. || test_reshape_permute(a, 16, 7, -233, 3)
  224. || test_reshape_permute(a, 2, 3, -233, -1)
  225. || test_reshape_permute(a, -1, 8, -233, 2)
  226. || test_reshape_permute(a, -1, 4, -233, -233)
  227. || test_reshape_permute(a, 8, -1, -233, -233)
  228. || test_reshape_permute(a, 16, 21, -233, -233)
  229. || test_reshape_permute(a, -1, -233, -233, -233);
  230. }
  231. static int test_reshape_11()
  232. {
  233. ncnn::Mat a = RandomMat(4, 14, 13);
  234. return 0
  235. || test_reshape_permute(a, 13, 4, 2, 7)
  236. || test_reshape_permute(a, 1, 13, 7, 8)
  237. || test_reshape_permute(a, 14, 4, -233, 13)
  238. || test_reshape_permute(a, 4, 13, -233, 14)
  239. || test_reshape_permute(a, 13, 14, -233, 4)
  240. || test_reshape_permute(a, 2, 7, -233, -1)
  241. || test_reshape_permute(a, -1, 13, -233, 2)
  242. || test_reshape_permute(a, -1, 4, -233, -233)
  243. || test_reshape_permute(a, 8, -1, -233, -233)
  244. || test_reshape_permute(a, 8, 91, -233, -233)
  245. || test_reshape_permute(a, -1, -233, -233, -233);
  246. }
  247. static int test_reshape_12()
  248. {
  249. ncnn::Mat a = RandomMat(14, 16);
  250. return 0
  251. || test_reshape_permute(a, 2, 7, 2, 8)
  252. || test_reshape_permute(a, 8, 1, 7, 4)
  253. || test_reshape_permute(a, 7, 2, -233, 16)
  254. || test_reshape_permute(a, 2, 16, -233, 7)
  255. || test_reshape_permute(a, 16, 7, -233, 2)
  256. || test_reshape_permute(a, 2, 4, -233, -1)
  257. || test_reshape_permute(a, -1, 8, -233, 2)
  258. || test_reshape_permute(a, 28, 8, -233, -233)
  259. || test_reshape_permute(a, -1, 7, -233, -233)
  260. || test_reshape_permute(a, 16, -1, -233, -233)
  261. || test_reshape_permute(a, -1, -233, -233, -233);
  262. }
  263. static int test_reshape_13()
  264. {
  265. ncnn::Mat a = RandomMat(12, 14);
  266. return 0
  267. || test_reshape_permute(a, 4, 3, 2, 7)
  268. || test_reshape_permute(a, 1, 3, 14, 4)
  269. || test_reshape_permute(a, 7, 2, -233, 12)
  270. || test_reshape_permute(a, 2, 12, -233, 7)
  271. || test_reshape_permute(a, 12, 7, -233, 2)
  272. || test_reshape_permute(a, 2, 4, -233, -1)
  273. || test_reshape_permute(a, -1, 4, -233, 2)
  274. || test_reshape_permute(a, 21, 8, -233, -233)
  275. || test_reshape_permute(a, -1, 7, -233, -233)
  276. || test_reshape_permute(a, 3, -1, -233, -233)
  277. || test_reshape_permute(a, -1, -233, -233, -233);
  278. }
  279. static int test_reshape_14()
  280. {
  281. ncnn::Mat a = RandomMat(120);
  282. return 0
  283. || test_reshape_permute(a, 1, 1, 1, 120)
  284. || test_reshape_permute(a, 10, 1, 1, 12)
  285. || test_reshape_permute(a, 3, 5, -233, 8)
  286. || test_reshape_permute(a, 3, 8, -233, 5)
  287. || test_reshape_permute(a, 8, 5, -233, 3)
  288. || test_reshape_permute(a, 2, 5, -233, -1)
  289. || test_reshape_permute(a, -1, 5, -233, 2)
  290. || test_reshape_permute(a, 4, 30, -233, -233)
  291. || test_reshape_permute(a, -1, 2, -233, -233)
  292. || test_reshape_permute(a, 24, -1, -233, -233)
  293. || test_reshape_permute(a, -1, -233, -233, -233);
  294. }
  295. static int test_reshape_15()
  296. {
  297. ncnn::Mat a = RandomMat(210);
  298. return 0
  299. || test_reshape_permute(a, 1, 1, 210, 1)
  300. || test_reshape_permute(a, 5, 2, 7, 3)
  301. || test_reshape_permute(a, 3, 5, -233, 14)
  302. || test_reshape_permute(a, 3, 14, -233, 5)
  303. || test_reshape_permute(a, 14, 5, -233, 3)
  304. || test_reshape_permute(a, 2, 5, -233, -1)
  305. || test_reshape_permute(a, -1, 5, -233, 2)
  306. || test_reshape_permute(a, 6, 35, -233, -233)
  307. || test_reshape_permute(a, -1, 7, -233, -233)
  308. || test_reshape_permute(a, 21, -1, -233, -233)
  309. || test_reshape_permute(a, -1, -233, -233, -233);
  310. }
  311. int main()
  312. {
  313. SRAND(7767517);
  314. return 0
  315. || test_reshape_0()
  316. || test_reshape_1()
  317. || test_reshape_2()
  318. || test_reshape_3()
  319. || test_reshape_4()
  320. || test_reshape_5()
  321. || test_reshape_6()
  322. || test_reshape_7()
  323. || test_reshape_8()
  324. || test_reshape_9()
  325. || test_reshape_10()
  326. || test_reshape_11()
  327. || test_reshape_12()
  328. || test_reshape_13()
  329. || test_reshape_14()
  330. || test_reshape_15();
  331. }