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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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-2020 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/convolution.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/benchmarker.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)
  32. .execs({args[i].src, args[i].filter, {}});
  33. }
  34. }
  35. #define CONVOLUTION1_ARG_LOOP_END_TIME (CONVOLUTION_ARG_DIV_SIZE + 205)
  36. TEST_F(CPU, CONVOLUTION_1) {
  37. using namespace convolution;
  38. std::vector<TestArg> args = get_args();
  39. auto loop_size = args.size();
  40. ASSERT_GT(loop_size, CONVOLUTION_ARG_DIV_SIZE);
  41. ASSERT_GT(loop_size, CONVOLUTION1_ARG_LOOP_END_TIME);
  42. Checker<Convolution> checker(handle());
  43. for (unsigned int i = CONVOLUTION_ARG_DIV_SIZE;
  44. i < CONVOLUTION1_ARG_LOOP_END_TIME; i++) {
  45. checker.set_param(args[i].param)
  46. .execs({args[i].src, args[i].filter, {}});
  47. }
  48. }
  49. #define CONVOLUTION2_ARG_LOOP_END_TIME (CONVOLUTION1_ARG_LOOP_END_TIME + 200)
  50. TEST_F(CPU, CONVOLUTION_2) {
  51. using namespace convolution;
  52. std::vector<TestArg> args = get_args();
  53. auto loop_size = args.size();
  54. ASSERT_GT(loop_size, CONVOLUTION2_ARG_LOOP_END_TIME);
  55. Checker<Convolution> checker(handle());
  56. for (unsigned int i = CONVOLUTION1_ARG_LOOP_END_TIME;
  57. i < CONVOLUTION2_ARG_LOOP_END_TIME; i++) {
  58. checker.set_param(args[i].param)
  59. .execs({args[i].src, args[i].filter, {}});
  60. }
  61. }
  62. TEST_F(CPU, CONVOLUTION_3) {
  63. using namespace convolution;
  64. std::vector<TestArg> args = get_args();
  65. auto loop_size = args.size();
  66. ASSERT_GT(loop_size, CONVOLUTION2_ARG_LOOP_END_TIME);
  67. Checker<Convolution> checker(handle());
  68. for (unsigned int i = CONVOLUTION2_ARG_LOOP_END_TIME; i < loop_size; i++) {
  69. checker.set_param(args[i].param)
  70. .execs({args[i].src, args[i].filter, {}});
  71. }
  72. }
  73. #undef CONVOLUTION_ARG_DIV_SIZE
  74. #undef CONVOLUTION1_ARG_LOOP_END_TIME
  75. #undef CONVOLUTION2_ARG_LOOP_END_TIME
  76. #define CB_CONV_CONFIG_COMBINATIONS(KSIZE) \
  77. TEST_F(CPU, CONV_CONFIG_COMBINATIONS_KSIZE_1_KSIZE_##KSIZE) { \
  78. convolution::test_conv_config_combinations(KSIZE, handle(), true, \
  79. false, false); \
  80. }
  81. // FIXME: only test ksize=1, will crash on IOS, so we tmp test ksize_1##other_ksize
  82. CB_CONV_CONFIG_COMBINATIONS(2);
  83. CB_CONV_CONFIG_COMBINATIONS(3);
  84. CB_CONV_CONFIG_COMBINATIONS(5);
  85. #undef CB_CONV_CONFIG_COMBINATIONS
  86. #if MEGDNN_WITH_BENCHMARK
  87. TEST_F(CPU, BENCHMARK_CONVOLUTION)
  88. {
  89. using TestArg = convolution::TestArg;
  90. using Param = param::Convolution;
  91. std::vector<TestArg> args;
  92. // case 1: detection-like (padding x stride x kernel_size)
  93. // clang-format off
  94. for (size_t has_pad = 0; has_pad < 2; ++has_pad)
  95. for (uint32_t stride = 1; stride <= 2; ++stride)
  96. for (std::pair<size_t, size_t> kersize :
  97. std::vector<std::pair<size_t, size_t>>{
  98. {2, 2}, {3, 3}, {5, 5}, {7, 7}}) {
  99. uint32_t pad_h, pad_w;
  100. if (has_pad)
  101. pad_h = kersize.first / 2;
  102. else
  103. pad_h = 0;
  104. if (has_pad)
  105. pad_w = kersize.second / 2;
  106. else
  107. pad_w = 0;
  108. auto param = Param{Param::Mode::CROSS_CORRELATION, pad_h, pad_w,
  109. stride, stride};
  110. {
  111. auto arg = TestArg{param,
  112. {2, 3, 320, 240},
  113. {4, 3, kersize.first, kersize.second}};
  114. args.push_back(arg);
  115. }
  116. }
  117. // clang-format on
  118. Checker<Convolution> checker(handle());
  119. checker.set_perf_check(true).set_perf_check_threshold(2.0);
  120. for (auto &&arg: args) {
  121. checker.set_param(arg.param).execs({arg.src, arg.filter, {}});
  122. }
  123. }
  124. #endif
  125. TEST_F(CPU, CHANWISE_CONVOLUTION)
  126. {
  127. constexpr auto M = Convolution::Mode::CROSS_CORRELATION;
  128. Checker<Convolution> checker(handle());
  129. checker.set_param(gconv_param({M, 0, 0, 1, 1})).
  130. execs({{1, 1, 2, 2}, {1, 1, 1, 2, 2}, {}}).
  131. execs({{1, 1, 5, 5}, {1, 1, 1, 2, 2}, {}}).
  132. execs({{2, 2, 5, 5}, {2, 3, 1, 2, 2}, {2, 6, 4, 4}});
  133. checker.set_param(gconv_param({M, 1, 1, 1, 1})).
  134. execs({{2, 2, 5, 5}, {2, 1, 1, 2, 2}, {}});
  135. checker.set_param(gconv_param({M, 2, 3, 2, 1})).
  136. execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  137. // padding larger than kern
  138. checker.set_param(gconv_param({M, 20, 30, 4, 5})).
  139. execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  140. }
  141. TEST_F(CPU, CHANWISE_CONVOLUTION_INT8_INT8_INT16)
  142. {
  143. constexpr auto M = Convolution::Mode::CROSS_CORRELATION;
  144. Checker<Convolution> checker(handle());
  145. checker.set_dtype(0, dtype::Int8());
  146. checker.set_dtype(1, dtype::Int8());
  147. checker.set_dtype(2, dtype::Int16());
  148. checker.set_param(gconv_param({M, 0, 0, 1, 1, 1, 1})).
  149. execs({{1, 1, 2, 2}, {1, 1, 1, 2, 2}, {}}).
  150. execs({{1, 1, 5, 5}, {1, 1, 1, 2, 2}, {}}).
  151. execs({{2, 2, 5, 5}, {2, 3, 1, 2, 2}, {2, 6, 4, 4}});
  152. checker.set_param(gconv_param({M, 1, 1, 1, 1, 1, 1})).
  153. execs({{2, 2, 5, 5}, {2, 1, 1, 2, 2}, {}});
  154. checker.set_param(gconv_param({M, 2, 3, 2, 1, 1, 1})).
  155. execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  156. // padding larger than kern
  157. checker.set_param(gconv_param({M, 20, 30, 4, 5, 1, 1})).
  158. execs({{32, 12, 20, 10}, {12, 2, 1, 4, 5}, {}});
  159. // clang-format off
  160. for (uint32_t s : {1, 2})
  161. for (uint32_t p : {0, 1})
  162. for (size_t kh : {2, 3, 5})
  163. for (size_t kw : {kh, kh + 1})
  164. for (size_t ic : {5})
  165. for (size_t oc : {3})
  166. for (size_t h = 20; h <= 60; h += 7)
  167. for (size_t w : {h, h + 1}) {
  168. checker.set_param(gconv_param({M, p, p, s, s, 1, 1}))
  169. .execs({{2, ic, h, w}, {ic, oc, 1, kh, kw}, {}});
  170. }
  171. // clang-format on
  172. }
  173. TEST_F(CPU, GROUP_CONV)
  174. {
  175. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW,
  176. size_t FH, size_t FW,
  177. size_t OC, size_t /* OH */, size_t /* OW */,
  178. size_t PH, size_t PW,
  179. size_t SH, size_t SW,
  180. size_t group)
  181. {
  182. Checker<Convolution> checker(handle());
  183. Convolution::Param param;
  184. param.pad_h = PH;
  185. param.pad_w = PW;
  186. param.stride_h = SH;
  187. param.stride_w = SW;
  188. auto ICg = IC / group;
  189. auto OCg = OC / group;
  190. checker.set_param(gconv_param(param)).exec({{N, IC, IH, IW},
  191. {group, OCg, ICg, FH, FW}, {}});
  192. };
  193. // normal case
  194. run(2, 64, 7, 7,
  195. 3, 3,
  196. 32, 5, 5,
  197. 0, 0,
  198. 1, 1,
  199. 1);
  200. // padded case
  201. run(2, 32, 7, 7,
  202. 3, 3,
  203. 64, 7, 7,
  204. 1, 1,
  205. 1, 1,
  206. 4);
  207. // strided case
  208. run(2, 32, 7, 7,
  209. 3, 3,
  210. 64, 3, 3,
  211. 0, 0,
  212. 2, 2,
  213. 8);
  214. }
  215. #if MEGDNN_WITH_BENCHMARK
  216. TEST_F(CPU, BENCHMARK_7X7_CONVOLUTION)
  217. {
  218. using Param = param::Convolution;
  219. auto run = [&](const TensorShapeArray& shapes, Param param) {
  220. auto handle_naive = create_cpu_handle(2);
  221. Benchmarker<Convolution> benchmarker_naive(handle_naive.get());
  222. Benchmarker<Convolution> benchmarker_float(handle());
  223. size_t RUN = 10;
  224. auto tfloat = benchmarker_float.set_display(false)
  225. .set_times(RUN)
  226. .set_param(param)
  227. .exec(shapes);
  228. auto tnaive = benchmarker_naive.set_display(false)
  229. .set_times(RUN)
  230. .set_param(param)
  231. .exec(shapes);
  232. printf("src: %s filter: %s dst: %s naive=%.3fms float=%.3fms\n",
  233. shapes[0].to_string().c_str(), shapes[1].to_string().c_str(),
  234. shapes[2].to_string().c_str(), tnaive / RUN, tfloat / RUN);
  235. };
  236. Param param;
  237. param.stride_h = 2;
  238. param.stride_w = 2;
  239. param.pad_h = 3;
  240. param.pad_w = 3;
  241. // clang-format off
  242. for (size_t ic : {1, 3, 8, 16, 24}) {
  243. for (size_t oc : {8, 16}) {
  244. for (size_t h : {128, 224, 256, 512}) {
  245. for (size_t w : {128, 224, 256, 512}) {
  246. run({{1, ic, h, w}, {oc, ic, 7, 7}, {1, oc, h / 2, w / 2}}, param);
  247. } } } }
  248. // clang-format on
  249. // Used in FaceModel
  250. //run({{2, 3, 512, 512}, {8, 3, 7, 7}, {2, 8, 256, 256}}, param);
  251. //run({{2, 3, 128, 128}, {16, 3, 7, 7}, {2, 16, 64, 64}}, param);
  252. //run({{2, 3, 224, 224}, {32, 3, 7, 7}, {2, 32, 112, 112}}, param);
  253. }
  254. #endif
  255. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台