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

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