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

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

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