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.

convolution.cpp 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * \file dnn/test/cpu/convolution.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/cpu/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/convolution.h"
  15. using namespace megdnn;
  16. using namespace test;
  17. namespace {
  18. Convolution::Param gconv_param(Convolution::Param p) {
  19. p.sparse = Convolution::Param::Sparse::GROUP;
  20. return p;
  21. }
  22. } // anonymous namespace
  23. #define CONVOLUTION_ARG_DIV_SIZE 100
  24. TEST_F(CPU, CONVOLUTION_0) {
  25. using namespace convolution;
  26. std::vector<TestArg> args = get_args();
  27. auto loop_size = args.size();
  28. ASSERT_GT(loop_size, CONVOLUTION_ARG_DIV_SIZE);
  29. Checker<Convolution> checker(handle());
  30. for (unsigned int i = 0; i < CONVOLUTION_ARG_DIV_SIZE; i++) {
  31. checker.set_param(args[i].param).execs({args[i].src, args[i].filter, {}});
  32. }
  33. }
  34. #define CONVOLUTION1_ARG_LOOP_END_TIME (CONVOLUTION_ARG_DIV_SIZE + 205)
  35. TEST_F(CPU, CONVOLUTION_1) {
  36. using namespace convolution;
  37. std::vector<TestArg> args = get_args();
  38. auto loop_size = args.size();
  39. ASSERT_GT(loop_size, CONVOLUTION_ARG_DIV_SIZE);
  40. ASSERT_GT(loop_size, CONVOLUTION1_ARG_LOOP_END_TIME);
  41. Checker<Convolution> checker(handle());
  42. for (unsigned int i = CONVOLUTION_ARG_DIV_SIZE; i < CONVOLUTION1_ARG_LOOP_END_TIME;
  43. i++) {
  44. checker.set_param(args[i].param).execs({args[i].src, args[i].filter, {}});
  45. }
  46. }
  47. #define CONVOLUTION2_ARG_LOOP_END_TIME (CONVOLUTION1_ARG_LOOP_END_TIME + 200)
  48. TEST_F(CPU, CONVOLUTION_2) {
  49. using namespace convolution;
  50. std::vector<TestArg> args = get_args();
  51. auto loop_size = args.size();
  52. ASSERT_GT(loop_size, CONVOLUTION2_ARG_LOOP_END_TIME);
  53. Checker<Convolution> checker(handle());
  54. for (unsigned int i = CONVOLUTION1_ARG_LOOP_END_TIME;
  55. i < CONVOLUTION2_ARG_LOOP_END_TIME; i++) {
  56. checker.set_param(args[i].param).execs({args[i].src, args[i].filter, {}});
  57. }
  58. }
  59. TEST_F(CPU, CONVOLUTION_3) {
  60. using namespace convolution;
  61. std::vector<TestArg> args = get_args();
  62. auto loop_size = args.size();
  63. ASSERT_GT(loop_size, CONVOLUTION2_ARG_LOOP_END_TIME);
  64. Checker<Convolution> checker(handle());
  65. for (unsigned int i = CONVOLUTION2_ARG_LOOP_END_TIME; i < loop_size; i++) {
  66. checker.set_param(args[i].param).execs({args[i].src, args[i].filter, {}});
  67. }
  68. }
  69. #undef CONVOLUTION_ARG_DIV_SIZE
  70. #undef CONVOLUTION1_ARG_LOOP_END_TIME
  71. #undef CONVOLUTION2_ARG_LOOP_END_TIME
  72. #define CB_CONV_CONFIG_COMBINATIONS(KSIZE) \
  73. TEST_F(CPU, CONV_CONFIG_COMBINATIONS_KSIZE_1_KSIZE_##KSIZE) { \
  74. convolution::test_conv_config_combinations( \
  75. KSIZE, handle(), true, false, false); \
  76. }
  77. // FIXME: only test ksize=1, will crash on IOS, so we tmp test ksize_1##other_ksize
  78. CB_CONV_CONFIG_COMBINATIONS(2);
  79. CB_CONV_CONFIG_COMBINATIONS(3);
  80. CB_CONV_CONFIG_COMBINATIONS(5);
  81. #undef CB_CONV_CONFIG_COMBINATIONS
  82. #if MEGDNN_WITH_BENCHMARK
  83. TEST_F(CPU, BENCHMARK_CONVOLUTION) {
  84. using TestArg = convolution::TestArg;
  85. using Param = param::Convolution;
  86. std::vector<TestArg> args;
  87. // case 1: detection-like (padding x stride x kernel_size)
  88. // clang-format off
  89. for (size_t has_pad = 0; has_pad < 2; ++has_pad)
  90. for (uint32_t stride = 1; stride <= 2; ++stride)
  91. for (std::pair<size_t, size_t> kersize :
  92. std::vector<std::pair<size_t, size_t>>{
  93. {2, 2}, {3, 3}, {5, 5}, {7, 7}}) {
  94. uint32_t pad_h, pad_w;
  95. if (has_pad)
  96. pad_h = kersize.first / 2;
  97. else
  98. pad_h = 0;
  99. if (has_pad)
  100. pad_w = kersize.second / 2;
  101. else
  102. pad_w = 0;
  103. auto param = Param{Param::Mode::CROSS_CORRELATION, pad_h, pad_w,
  104. stride, stride};
  105. {
  106. auto arg = TestArg{param,
  107. {2, 3, 320, 240},
  108. {4, 3, kersize.first, kersize.second}};
  109. args.push_back(arg);
  110. }
  111. }
  112. // clang-format on
  113. Checker<Convolution> checker(handle());
  114. checker.set_perf_check(true).set_perf_check_threshold(2.0);
  115. for (auto&& arg : args) {
  116. checker.set_param(arg.param).execs({arg.src, arg.filter, {}});
  117. }
  118. }
  119. #endif
  120. TEST_F(CPU, CHANWISE_CONVOLUTION) {
  121. constexpr auto M = Convolution::Mode::CROSS_CORRELATION;
  122. Checker<Convolution> checker(handle());
  123. checker.set_param(gconv_param({M, 0, 0, 1, 1}))
  124. .execs({{1, 1, 2, 2}, {1, 1, 1, 2, 2}, {}})
  125. .execs({{1, 1, 5, 5}, {1, 1, 1, 2, 2}, {}})
  126. .execs({{2, 2, 5, 5}, {2, 3, 1, 2, 2}, {2, 6, 4, 4}});
  127. checker.set_param(gconv_param({M, 1, 1, 1, 1}))
  128. .execs({{2, 2, 5, 5}, {2, 1, 1, 2, 2}, {}});
  129. checker.set_param(gconv_param({M, 2, 3, 2, 1}))
  130. .execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  131. // padding larger than kern
  132. checker.set_param(gconv_param({M, 20, 30, 4, 5}))
  133. .execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  134. }
  135. TEST_F(CPU, CHANWISE_CONVOLUTION_INT8_INT8_INT16) {
  136. constexpr auto M = Convolution::Mode::CROSS_CORRELATION;
  137. Checker<Convolution> checker(handle());
  138. checker.set_dtype(0, dtype::Int8());
  139. checker.set_dtype(1, dtype::Int8());
  140. checker.set_dtype(2, dtype::Int16());
  141. checker.set_param(gconv_param({M, 0, 0, 1, 1, 1, 1}))
  142. .execs({{1, 1, 2, 2}, {1, 1, 1, 2, 2}, {}})
  143. .execs({{1, 1, 5, 5}, {1, 1, 1, 2, 2}, {}})
  144. .execs({{2, 2, 5, 5}, {2, 3, 1, 2, 2}, {2, 6, 4, 4}});
  145. checker.set_param(gconv_param({M, 1, 1, 1, 1, 1, 1}))
  146. .execs({{2, 2, 5, 5}, {2, 1, 1, 2, 2}, {}});
  147. checker.set_param(gconv_param({M, 2, 3, 2, 1, 1, 1}))
  148. .execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  149. // padding larger than kern
  150. checker.set_param(gconv_param({M, 20, 30, 4, 5, 1, 1}))
  151. .execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  152. // clang-format off
  153. for (uint32_t s : {1, 2})
  154. for (uint32_t p : {0, 1})
  155. for (size_t kh : {2, 3, 5})
  156. for (size_t kw : {kh, kh + 1})
  157. for (size_t ic : {5})
  158. for (size_t oc : {3})
  159. for (size_t h = 20; h <= 60; h += 7)
  160. for (size_t w : {h, h + 1}) {
  161. checker.set_param(gconv_param({M, p, p, s, s, 1, 1}))
  162. .execs({{2, ic, h, w}, {ic, oc, 1, kh, kw}, {}});
  163. }
  164. // clang-format on
  165. }
  166. TEST_F(CPU, GROUP_CONV) {
  167. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH, size_t FW,
  168. size_t OC, size_t /* OH */, size_t /* OW */, size_t PH, size_t PW,
  169. size_t SH, size_t SW, size_t group) {
  170. Checker<Convolution> checker(handle());
  171. Convolution::Param param;
  172. param.pad_h = PH;
  173. param.pad_w = PW;
  174. param.stride_h = SH;
  175. param.stride_w = SW;
  176. auto ICg = IC / group;
  177. auto OCg = OC / group;
  178. checker.set_param(gconv_param(param))
  179. .exec({{N, IC, IH, IW}, {group, OCg, ICg, FH, FW}, {}});
  180. };
  181. // normal case
  182. run(2, 64, 7, 7, 3, 3, 32, 5, 5, 0, 0, 1, 1, 1);
  183. // padded case
  184. run(2, 32, 7, 7, 3, 3, 64, 7, 7, 1, 1, 1, 1, 4);
  185. // strided case
  186. run(2, 32, 7, 7, 3, 3, 64, 3, 3, 0, 0, 2, 2, 8);
  187. }
  188. #if MEGDNN_WITH_BENCHMARK
  189. TEST_F(CPU, BENCHMARK_7X7_CONVOLUTION) {
  190. using Param = param::Convolution;
  191. auto run = [&](const TensorShapeArray& shapes, Param param) {
  192. auto handle_naive = create_cpu_handle(2);
  193. Benchmarker<Convolution> benchmarker_naive(handle_naive.get());
  194. Benchmarker<Convolution> benchmarker_float(handle());
  195. size_t RUN = 10;
  196. auto tfloat = benchmarker_float.set_display(false)
  197. .set_times(RUN)
  198. .set_param(param)
  199. .exec(shapes);
  200. auto tnaive = benchmarker_naive.set_display(false)
  201. .set_times(RUN)
  202. .set_param(param)
  203. .exec(shapes);
  204. printf("src: %s filter: %s dst: %s naive=%.3fms float=%.3fms\n",
  205. shapes[0].to_string().c_str(), shapes[1].to_string().c_str(),
  206. shapes[2].to_string().c_str(), tnaive / RUN, tfloat / RUN);
  207. };
  208. Param param;
  209. param.stride_h = 2;
  210. param.stride_w = 2;
  211. param.pad_h = 3;
  212. param.pad_w = 3;
  213. // clang-format off
  214. for (size_t ic : {1, 3, 8, 16, 24}) {
  215. for (size_t oc : {8, 16}) {
  216. for (size_t h : {128, 224, 256, 512}) {
  217. for (size_t w : {128, 224, 256, 512}) {
  218. run({{1, ic, h, w}, {oc, ic, 7, 7}, {1, oc, h / 2, w / 2}}, param);
  219. } } } }
  220. // clang-format on
  221. // Used in FaceModel
  222. // run({{2, 3, 512, 512}, {8, 3, 7, 7}, {2, 8, 256, 256}}, param);
  223. // run({{2, 3, 128, 128}, {16, 3, 7, 7}, {2, 16, 64, 64}}, param);
  224. // run({{2, 3, 224, 224}, {32, 3, 7, 7}, {2, 32, 112, 112}}, param);
  225. }
  226. #endif
  227. // vim: syntax=cpp.doxygen