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.4 kB

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