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_convolution.cpp 7.8 kB

6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 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/convolution.h"
  16. static int test_convolution(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
  17. {
  18. ncnn::Mat a = RandomMat(w, h, c);
  19. ncnn::ParamDict pd;
  20. pd.set(0, outch);// num_output
  21. pd.set(1, kernel);// kernel_w
  22. pd.set(2, dilation);// dilation_w
  23. pd.set(3, stride);// stride_w
  24. pd.set(4, pad);// pad_w
  25. pd.set(5, bias);// bias_term
  26. pd.set(6, outch*c*kernel*kernel);
  27. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  28. weights[0] = RandomMat(outch*c*kernel*kernel);
  29. if (bias)
  30. weights[1] = RandomMat(outch);
  31. ncnn::Option opt;
  32. opt.num_threads = 1;
  33. opt.use_vulkan_compute = true;
  34. opt.use_int8_inference = false;
  35. opt.use_fp16_packed = false;
  36. opt.use_fp16_storage = false;
  37. opt.use_fp16_arithmetic = false;
  38. opt.use_int8_storage = false;
  39. opt.use_int8_arithmetic = false;
  40. int ret = test_layer<ncnn::Convolution>("Convolution", pd, weights, opt, a);
  41. if (ret != 0)
  42. {
  43. fprintf(stderr, "test_convolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias);
  44. }
  45. return ret;
  46. }
  47. static int test_convolution_0()
  48. {
  49. static const int kdsp[24][4] = {
  50. {1, 1, 1, 0},
  51. {1, 1, 2, 0},
  52. {2, 1, 1, 1},
  53. {2, 1, 2, 1},
  54. {2, 2, 1, 1},
  55. {2, 2, 2, 1},
  56. {3, 1, 1, 1},
  57. {3, 1, 2, 1},
  58. {3, 2, 1, 1},
  59. {3, 2, 2, 1},
  60. {4, 1, 1, 2},
  61. {4, 1, 2, 2},
  62. {4, 2, 1, 2},
  63. {4, 2, 2, 2},
  64. {5, 1, 1, 2},
  65. {5, 1, 2, 2},
  66. {5, 2, 1, 2},
  67. {5, 2, 2, 2},
  68. {7, 1, 1, 3},
  69. {7, 1, 2, 3},
  70. {7, 1, 3, 3},
  71. {7, 2, 1, 3},
  72. {7, 2, 2, 3},
  73. {7, 2, 3, 3},
  74. };
  75. for (int i=0; i<24; i++)
  76. {
  77. int ret = 0
  78. || test_convolution(9, 7, 1, 1, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  79. || test_convolution(9, 7, 2, 2, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  80. || test_convolution(9, 7, 3, 3, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  81. || test_convolution(9, 7, 4, 4, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  82. || test_convolution(9, 7, 7, 7, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  83. || test_convolution(9, 7, 8, 8, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  84. || test_convolution(9, 7, 15, 15, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  85. || test_convolution(9, 7, 16, 16, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  86. ;
  87. if (ret != 0)
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. void set_param(ncnn::Convolution* layer)
  93. {
  94. layer->use_int8_requantize = true;
  95. layer->top_blob_int8_scale = 64.f;
  96. return;
  97. }
  98. static int test_convolution_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false)
  99. {
  100. ncnn::Mat a = RandomMat(w, h, c);
  101. ncnn::ParamDict pd;
  102. pd.set(0, outch);// num_output
  103. pd.set(1, kernel);// kernel_w
  104. pd.set(2, dilation);// dilation_w
  105. pd.set(3, stride);// stride_w
  106. pd.set(4, pad);// pad_w
  107. pd.set(5, bias);// bias_term
  108. pd.set(6, outch*c*kernel*kernel);
  109. pd.set(8, 1);// int8_scale_term
  110. std::vector<ncnn::Mat> weights(bias ? 4 : 3);
  111. weights[0] = RandomMat(outch*c*kernel*kernel);
  112. if (bias)
  113. {
  114. weights[1] = RandomMat(outch);
  115. weights[2] = RandomMat(outch);
  116. weights[3] = RandomMat(1);
  117. }
  118. else
  119. {
  120. weights[1] = RandomMat(outch);
  121. weights[2] = RandomMat(1);
  122. }
  123. ncnn::Option opt;
  124. opt.num_threads = 1;
  125. opt.use_vulkan_compute = false;
  126. opt.use_int8_inference = true;
  127. opt.use_fp16_packed = false;
  128. opt.use_fp16_storage = false;
  129. opt.use_fp16_arithmetic = false;
  130. opt.use_int8_storage = false;
  131. opt.use_int8_arithmetic = false;
  132. int ret = test_layer<ncnn::Convolution>("Convolution", pd, weights, opt, a, 0.001f, requant ? set_param : 0);
  133. if (ret != 0)
  134. {
  135. fprintf(stderr, "test_convolution_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant);
  136. }
  137. return 0;
  138. }
  139. static int test_convolution_1()
  140. {
  141. static const int kdsp[24][4] = {
  142. {1, 1, 1, 0},
  143. {1, 1, 2, 0},
  144. {2, 1, 1, 1},
  145. {2, 1, 2, 1},
  146. {2, 2, 1, 1},
  147. {2, 2, 2, 1},
  148. {3, 1, 1, 1},
  149. {3, 1, 2, 1},
  150. {3, 2, 1, 1},
  151. {3, 2, 2, 1},
  152. {4, 1, 1, 2},
  153. {4, 1, 2, 2},
  154. {4, 2, 1, 2},
  155. {4, 2, 2, 2},
  156. {5, 1, 1, 2},
  157. {5, 1, 2, 2},
  158. {5, 2, 1, 2},
  159. {5, 2, 2, 2},
  160. {7, 1, 1, 3},
  161. {7, 1, 2, 3},
  162. {7, 1, 3, 3},
  163. {7, 2, 1, 3},
  164. {7, 2, 2, 3},
  165. {7, 2, 3, 3},
  166. };
  167. for (int i=0; i<24; i++)
  168. {
  169. int ret = 0
  170. || test_convolution_int8(9, 7, 1, 1, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  171. || test_convolution_int8(9, 7, 2, 2, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  172. || test_convolution_int8(9, 7, 3, 3, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  173. || test_convolution_int8(9, 7, 4, 4, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  174. || test_convolution_int8(9, 7, 7, 7, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  175. || test_convolution_int8(9, 7, 8, 8, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  176. || test_convolution_int8(9, 7, 15, 15, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  177. || test_convolution_int8(9, 7, 16, 16, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1)
  178. ;
  179. if (ret != 0)
  180. return -1;
  181. }
  182. for (int i=0; i<20; i++)
  183. {
  184. int ret = 0
  185. || test_convolution_int8(9, 7, 1, 1, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  186. || test_convolution_int8(9, 7, 1, 1, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  187. || test_convolution_int8(9, 7, 2, 2, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  188. || test_convolution_int8(9, 7, 3, 3, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  189. || test_convolution_int8(9, 7, 4, 4, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  190. || test_convolution_int8(9, 7, 7, 7, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  191. || test_convolution_int8(9, 7, 8, 8, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  192. || test_convolution_int8(9, 7, 15, 15, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  193. || test_convolution_int8(9, 7, 16, 16, kdsp[i][0], kdsp[i][1], kdsp[i][2], kdsp[i][3], 1, true)
  194. ;
  195. if (ret != 0)
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. int main()
  201. {
  202. SRAND(7767517);
  203. return test_convolution_0() || test_convolution_1();
  204. }