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_convolutiondepthwise_1.cpp 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2019 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_convolutiondepthwise_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group)
  5. {
  6. ncnn::Mat a = RandomMat(w, h, c);
  7. ncnn::ParamDict pd;
  8. pd.set(0, 0);
  9. pd.set(1, 0);
  10. pd.set(2, dilation);
  11. pd.set(3, stride);
  12. pd.set(4, pad);
  13. pd.set(5, bias);
  14. pd.set(6, 0);
  15. pd.set(7, group);
  16. pd.set(19, 1); // dynamic weight
  17. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  18. ncnn::Mat activation_params(2);
  19. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  20. activation_params[1] = RandomFloat(0, 1); // beta
  21. pd.set(9, activation_type);
  22. pd.set(10, activation_params);
  23. std::vector<ncnn::Mat> as(bias ? 3 : 2);
  24. as[0] = a;
  25. as[1] = RandomMat(kernel, kernel, c / group, outch);
  26. if (bias)
  27. as[2] = RandomMat(outch);
  28. std::vector<ncnn::Mat> weights(0);
  29. int ret = test_layer("ConvolutionDepthWise", pd, weights, as);
  30. if (ret != 0)
  31. {
  32. fprintf(stderr, "test_convolutiondepthwise_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]);
  33. }
  34. return ret;
  35. }
  36. static int test_convolutiondepthwise_2()
  37. {
  38. static const int kdsp[7][4] = {
  39. {1, 1, 1, 0},
  40. {1, 1, 2, 0},
  41. {2, 1, 1, 1},
  42. {2, 1, 2, -233},
  43. {3, 1, 1, 1},
  44. {3, 1, 2, 1},
  45. {3, 2, 1, -234},
  46. };
  47. for (int i = 0; i < 7; i++)
  48. {
  49. const int k = kdsp[i][0];
  50. const int d = kdsp[i][1];
  51. const int s = kdsp[i][2];
  52. const int p = kdsp[i][3];
  53. int ret = 0
  54. || test_convolutiondepthwise_dynamic(11, 10, 1, 1, k, d, s, p, 1, 1)
  55. || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 0, 1)
  56. || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 1, 2)
  57. || test_convolutiondepthwise_dynamic(11, 10, 3, 3, k, d, s, p, 0, 3)
  58. || test_convolutiondepthwise_dynamic(11, 10, 4, 2, k, d, s, p, 1, 2)
  59. || test_convolutiondepthwise_dynamic(11, 10, 4, 4, k, d, s, p, 0, 4)
  60. || test_convolutiondepthwise_dynamic(11, 10, 7, 7, k, d, s, p, 1, 7)
  61. || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 0, 2)
  62. || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 1, 8)
  63. || test_convolutiondepthwise_dynamic(11, 10, 12, 12, k, d, s, p, 0, 4)
  64. || test_convolutiondepthwise_dynamic(11, 10, 15, 15, k, d, s, p, 1, 15)
  65. || test_convolutiondepthwise_dynamic(11, 10, 16, 8, k, d, s, p, 0, 2)
  66. || test_convolutiondepthwise_dynamic(11, 10, 16, 16, k, d, s, p, 1, 16);
  67. if (ret != 0)
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. #if NCNN_INT8
  73. static int test_convolutiondepthwise_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, bool requant = false)
  74. {
  75. ncnn::Mat a = RandomMat(w, h, c);
  76. ncnn::ParamDict pd;
  77. pd.set(0, outch);
  78. pd.set(1, kernel);
  79. pd.set(2, dilation);
  80. pd.set(3, stride);
  81. pd.set(4, pad);
  82. pd.set(5, bias);
  83. pd.set(6, outch / group * c / group * kernel * kernel * group);
  84. pd.set(7, group);
  85. pd.set(8, requant ? 101 : 1); // int8_scale_term
  86. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  87. ncnn::Mat activation_params(2);
  88. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  89. activation_params[1] = RandomFloat(0, 1); // beta
  90. pd.set(9, activation_type);
  91. pd.set(10, activation_params);
  92. std::vector<ncnn::Mat> weights(bias ? 5 : 4);
  93. weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group);
  94. ncnn::Mat weight_scales = scales_mat(weights[0], group, c * kernel * kernel / group, c * kernel * kernel / group);
  95. ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep);
  96. ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat();
  97. if (bias)
  98. {
  99. weights[1] = RandomMat(outch);
  100. weights[2] = weight_scales;
  101. weights[3] = input_scales;
  102. weights[4] = top_scales;
  103. }
  104. else
  105. {
  106. weights[1] = weight_scales;
  107. weights[2] = input_scales;
  108. weights[3] = top_scales;
  109. }
  110. int flag = TEST_LAYER_DISABLE_GPU_TESTING;
  111. int ret = test_layer("ConvolutionDepthWise", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag);
  112. if (ret != 0)
  113. {
  114. fprintf(stderr, "test_convolutiondepthwise_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, requant, activation_type, activation_params[0], activation_params[1]);
  115. }
  116. return ret;
  117. }
  118. static int test_convolutiondepthwise_1()
  119. {
  120. static const int kdsp[16][4] = {
  121. {1, 1, 1, 0},
  122. {1, 1, 2, 0},
  123. {2, 1, 1, 1},
  124. {2, 1, 2, -233},
  125. {3, 1, 1, 1},
  126. {3, 1, 2, 1},
  127. {3, 2, 1, 1},
  128. {4, 1, 1, 2},
  129. {4, 1, 2, -233},
  130. {4, 2, 1, -234},
  131. {5, 1, 1, -234},
  132. {5, 1, 2, 2},
  133. {5, 2, 2, 2},
  134. {7, 1, 1, 3},
  135. {7, 1, 2, 3},
  136. {7, 2, 1, -233},
  137. };
  138. for (int i = 0; i < 16; i++)
  139. {
  140. const int k = kdsp[i][0];
  141. const int d = kdsp[i][1];
  142. const int s = kdsp[i][2];
  143. const int p = kdsp[i][3];
  144. int ret = 0
  145. || test_convolutiondepthwise_int8(15, 7, 1, 1, k, d, s, p, 1, 1)
  146. || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 0, 1)
  147. || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 1, 2)
  148. || test_convolutiondepthwise_int8(15, 7, 3, 3, k, d, s, p, 0, 3)
  149. || test_convolutiondepthwise_int8(15, 7, 4, 2, k, d, s, p, 1, 2)
  150. || test_convolutiondepthwise_int8(15, 7, 4, 4, k, d, s, p, 0, 4)
  151. || test_convolutiondepthwise_int8(15, 7, 7, 7, k, d, s, p, 1, 7)
  152. || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 0, 2)
  153. || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 1, 8)
  154. || test_convolutiondepthwise_int8(15, 7, 12, 12, k, d, s, p, 0, 4)
  155. || test_convolutiondepthwise_int8(15, 7, 15, 15, k, d, s, p, 1, 15)
  156. || test_convolutiondepthwise_int8(15, 7, 16, 8, k, d, s, p, 0, 2)
  157. || test_convolutiondepthwise_int8(15, 7, 16, 16, k, d, s, p, 1, 16);
  158. if (ret != 0)
  159. return -1;
  160. }
  161. for (int i = 0; i < 16; i++)
  162. {
  163. const int k = kdsp[i][0];
  164. const int d = kdsp[i][1];
  165. const int s = kdsp[i][2];
  166. const int p = kdsp[i][3];
  167. int ret = 0
  168. || test_convolutiondepthwise_int8(9, 7, 1, 1, k, d, s, p, 1, 1, true)
  169. || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 0, 1, true)
  170. || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 1, 2, true)
  171. || test_convolutiondepthwise_int8(9, 7, 3, 3, k, d, s, p, 0, 3, true)
  172. || test_convolutiondepthwise_int8(9, 7, 4, 2, k, d, s, p, 1, 2, true)
  173. || test_convolutiondepthwise_int8(9, 7, 4, 4, k, d, s, p, 0, 4, true)
  174. || test_convolutiondepthwise_int8(9, 7, 7, 7, k, d, s, p, 1, 7, true)
  175. || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 0, 2, true)
  176. || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 1, 8, true)
  177. || test_convolutiondepthwise_int8(9, 7, 12, 12, k, d, s, p, 0, 4, true)
  178. || test_convolutiondepthwise_int8(9, 7, 15, 15, k, d, s, p, 1, 15, true)
  179. || test_convolutiondepthwise_int8(9, 7, 16, 8, k, d, s, p, 0, 2, true)
  180. || test_convolutiondepthwise_int8(9, 7, 16, 16, k, d, s, p, 1, 16, true);
  181. if (ret != 0)
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. #endif // NCNN_INT8
  187. int main()
  188. {
  189. SRAND(7767517);
  190. #if NCNN_INT8
  191. return test_convolutiondepthwise_1() || test_convolutiondepthwise_2();
  192. #else
  193. return test_convolutiondepthwise_2();
  194. #endif
  195. }