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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 outc)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, outw); // w
  20. pd.set(1, outh); // h
  21. pd.set(2, outc); // c
  22. std::vector<ncnn::Mat> weights(0);
  23. int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
  24. if (ret != 0)
  25. {
  26. fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d) outw=%d outh=%d outc=%d\n", a.dims, a.w, a.h, a.c, outw, outh, outc);
  27. }
  28. return ret;
  29. }
  30. static int test_reshape_permute(const ncnn::Mat& a, int outw, int outh, int outc)
  31. {
  32. ncnn::ParamDict pd;
  33. pd.set(0, outw); // w
  34. pd.set(1, outh); // h
  35. pd.set(2, outc); // c
  36. pd.set(3, 1); // permute
  37. std::vector<ncnn::Mat> weights(0);
  38. int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
  39. if (ret != 0)
  40. {
  41. fprintf(stderr, "test_reshape_permute failed a.dims=%d a=(%d %d %d) outw=%d outh=%d outc=%d\n", a.dims, a.w, a.h, a.c, outw, outh, outc);
  42. }
  43. return ret;
  44. }
  45. static int test_reshape_0()
  46. {
  47. ncnn::Mat a = RandomMat(3, 7, 16);
  48. return 0
  49. || test_reshape(a, 7, 3, 16)
  50. || test_reshape(a, 3, 16, 7)
  51. || test_reshape(a, 16, 7, 3)
  52. || test_reshape(a, 2, 3, -1)
  53. || test_reshape(a, -1, 8, 2)
  54. || test_reshape(a, -1, 4, -233)
  55. || test_reshape(a, 8, -1, -233)
  56. || test_reshape(a, 16, 21, -233)
  57. || test_reshape(a, -1, -233, -233);
  58. }
  59. static int test_reshape_1()
  60. {
  61. ncnn::Mat a = RandomMat(4, 14, 13);
  62. return 0
  63. || test_reshape(a, 14, 4, 13)
  64. || test_reshape(a, 4, 13, 14)
  65. || test_reshape(a, 13, 14, 4)
  66. || test_reshape(a, 2, 7, -1)
  67. || test_reshape(a, -1, 13, 2)
  68. || test_reshape(a, -1, 4, -233)
  69. || test_reshape(a, 8, -1, -233)
  70. || test_reshape(a, 8, 91, -233)
  71. || test_reshape(a, -1, -233, -233);
  72. }
  73. static int test_reshape_2()
  74. {
  75. ncnn::Mat a = RandomMat(14, 16);
  76. return 0
  77. || test_reshape(a, 7, 2, 16)
  78. || test_reshape(a, 2, 16, 7)
  79. || test_reshape(a, 16, 7, 2)
  80. || test_reshape(a, 2, 4, -1)
  81. || test_reshape(a, -1, 8, 2)
  82. || test_reshape(a, 28, 8, -233)
  83. || test_reshape(a, -1, 7, -233)
  84. || test_reshape(a, 16, -1, -233)
  85. || test_reshape(a, -1, -233, -233);
  86. }
  87. static int test_reshape_3()
  88. {
  89. ncnn::Mat a = RandomMat(12, 14);
  90. return 0
  91. || test_reshape(a, 7, 2, 12)
  92. || test_reshape(a, 2, 12, 7)
  93. || test_reshape(a, 12, 7, 2)
  94. || test_reshape(a, 2, 4, -1)
  95. || test_reshape(a, -1, 4, 2)
  96. || test_reshape(a, 21, 8, -233)
  97. || test_reshape(a, -1, 7, -233)
  98. || test_reshape(a, 3, -1, -233)
  99. || test_reshape(a, -1, -233, -233);
  100. }
  101. static int test_reshape_4()
  102. {
  103. ncnn::Mat a = RandomMat(120);
  104. return 0
  105. || test_reshape(a, 3, 5, 8)
  106. || test_reshape(a, 3, 8, 5)
  107. || test_reshape(a, 8, 5, 3)
  108. || test_reshape(a, 2, 5, -1)
  109. || test_reshape(a, -1, 5, 2)
  110. || test_reshape(a, 4, 30, -233)
  111. || test_reshape(a, -1, 2, -233)
  112. || test_reshape(a, 24, -1, -233)
  113. || test_reshape(a, -1, -233, -233);
  114. }
  115. static int test_reshape_5()
  116. {
  117. ncnn::Mat a = RandomMat(210);
  118. return 0
  119. || test_reshape(a, 3, 5, 14)
  120. || test_reshape(a, 3, 14, 5)
  121. || test_reshape(a, 14, 5, 3)
  122. || test_reshape(a, 2, 5, -1)
  123. || test_reshape(a, -1, 5, 2)
  124. || test_reshape(a, 6, 35, -233)
  125. || test_reshape(a, -1, 7, -233)
  126. || test_reshape(a, 21, -1, -233)
  127. || test_reshape(a, -1, -233, -233);
  128. }
  129. static int test_reshape_6()
  130. {
  131. ncnn::Mat a = RandomMat(3, 7, 16);
  132. return 0
  133. || test_reshape_permute(a, 7, 3, 16)
  134. || test_reshape_permute(a, 3, 16, 7)
  135. || test_reshape_permute(a, 16, 7, 3)
  136. || test_reshape_permute(a, 2, 3, -1)
  137. || test_reshape_permute(a, -1, 8, 2)
  138. || test_reshape_permute(a, -1, 4, -233)
  139. || test_reshape_permute(a, 8, -1, -233)
  140. || test_reshape_permute(a, 16, 21, -233)
  141. || test_reshape_permute(a, -1, -233, -233);
  142. }
  143. static int test_reshape_7()
  144. {
  145. ncnn::Mat a = RandomMat(4, 14, 13);
  146. return 0
  147. || test_reshape_permute(a, 14, 4, 13)
  148. || test_reshape_permute(a, 4, 13, 14)
  149. || test_reshape_permute(a, 13, 14, 4)
  150. || test_reshape_permute(a, 2, 7, -1)
  151. || test_reshape_permute(a, -1, 13, 2)
  152. || test_reshape_permute(a, -1, 4, -233)
  153. || test_reshape_permute(a, 8, -1, -233)
  154. || test_reshape_permute(a, 8, 91, -233)
  155. || test_reshape_permute(a, -1, -233, -233);
  156. }
  157. static int test_reshape_8()
  158. {
  159. ncnn::Mat a = RandomMat(14, 16);
  160. return 0
  161. || test_reshape_permute(a, 7, 2, 16)
  162. || test_reshape_permute(a, 2, 16, 7)
  163. || test_reshape_permute(a, 16, 7, 2)
  164. || test_reshape_permute(a, 2, 4, -1)
  165. || test_reshape_permute(a, -1, 8, 2)
  166. || test_reshape_permute(a, 28, 8, -233)
  167. || test_reshape_permute(a, -1, 7, -233)
  168. || test_reshape_permute(a, 16, -1, -233)
  169. || test_reshape_permute(a, -1, -233, -233);
  170. }
  171. static int test_reshape_9()
  172. {
  173. ncnn::Mat a = RandomMat(12, 14);
  174. return 0
  175. || test_reshape_permute(a, 7, 2, 12)
  176. || test_reshape_permute(a, 2, 12, 7)
  177. || test_reshape_permute(a, 12, 7, 2)
  178. || test_reshape_permute(a, 2, 4, -1)
  179. || test_reshape_permute(a, -1, 4, 2)
  180. || test_reshape_permute(a, 21, 8, -233)
  181. || test_reshape_permute(a, -1, 7, -233)
  182. || test_reshape_permute(a, 3, -1, -233)
  183. || test_reshape_permute(a, -1, -233, -233);
  184. }
  185. static int test_reshape_10()
  186. {
  187. ncnn::Mat a = RandomMat(120);
  188. return 0
  189. || test_reshape_permute(a, 3, 5, 8)
  190. || test_reshape_permute(a, 3, 8, 5)
  191. || test_reshape_permute(a, 8, 5, 3)
  192. || test_reshape_permute(a, 2, 5, -1)
  193. || test_reshape_permute(a, -1, 5, 2)
  194. || test_reshape_permute(a, 4, 30, -233)
  195. || test_reshape_permute(a, -1, 2, -233)
  196. || test_reshape_permute(a, 24, -1, -233)
  197. || test_reshape_permute(a, -1, -233, -233);
  198. }
  199. static int test_reshape_11()
  200. {
  201. ncnn::Mat a = RandomMat(210);
  202. return 0
  203. || test_reshape_permute(a, 3, 5, 14)
  204. || test_reshape_permute(a, 3, 14, 5)
  205. || test_reshape_permute(a, 14, 5, 3)
  206. || test_reshape_permute(a, 2, 5, -1)
  207. || test_reshape_permute(a, -1, 5, 2)
  208. || test_reshape_permute(a, 6, 35, -233)
  209. || test_reshape_permute(a, -1, 7, -233)
  210. || test_reshape_permute(a, 21, -1, -233)
  211. || test_reshape_permute(a, -1, -233, -233);
  212. }
  213. int main()
  214. {
  215. SRAND(7767517);
  216. return 0
  217. || test_reshape_0()
  218. || test_reshape_1()
  219. || test_reshape_2()
  220. || test_reshape_3()
  221. || test_reshape_4()
  222. || test_reshape_5()
  223. || test_reshape_6()
  224. || test_reshape_7()
  225. || test_reshape_8()
  226. || test_reshape_9()
  227. || test_reshape_10()
  228. || test_reshape_11();
  229. }