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

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