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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * \file dnn/test/x86/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
  10. * implied.
  11. */
  12. #include "test/x86/fixture.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "megdnn/oprs.h"
  15. #include "test/common/benchmarker.h"
  16. #include "test/common/checker.h"
  17. #include "test/common/convolution.h"
  18. #include "test/common/rng.h"
  19. #include "test/common/tensor.h"
  20. #include "test/common/workspace_wrapper.h"
  21. namespace {
  22. #if MEGDNN_X86_WITH_MKL_DNN
  23. struct ConvArg {
  24. size_t batch_size, fh, sh, ph, ic, ih, iw, oc, groups;
  25. };
  26. std::vector<ConvArg> get_dense_conv_args() {
  27. std::vector<ConvArg> args;
  28. for (size_t batch_size : {1}) {
  29. for (size_t fh : {3, 5, 7}) {
  30. for (size_t sh : {1, 2}) {
  31. for (size_t ph : std::vector<size_t>{0, fh / 2}) {
  32. for (size_t oc : {3, 4}) {
  33. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  34. 15, oc, 1});
  35. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  36. 14, oc, 1});
  37. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  38. 13, oc, 1});
  39. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  40. 12, oc, 1});
  41. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  42. 11, oc, 1});
  43. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  44. 10, oc, 1});
  45. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  46. 9, oc, 1});
  47. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  48. 8, oc, 1});
  49. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 4, 7,
  50. 8, oc, 1});
  51. } // end oc
  52. } // end ph
  53. } // end sh
  54. } // end fh
  55. } // end batch_size
  56. return args;
  57. }
  58. std::vector<ConvArg> get_group_conv_args() {
  59. std::vector<ConvArg> args;
  60. for (size_t batch_size : {1}) {
  61. for (size_t fh : {3, 5, 7}) {
  62. for (size_t sh : {1, 2}) {
  63. for (size_t ph : std::vector<size_t>{0, fh / 2}) {
  64. for (size_t oc : {3}) {
  65. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  66. 15, oc, 2});
  67. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  68. 14, oc, 2});
  69. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  70. 13, oc, 2});
  71. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  72. 12, oc, 2});
  73. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  74. 11, oc, 2});
  75. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  76. 10, oc, 2});
  77. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  78. 9, oc, 2});
  79. args.emplace_back(ConvArg{batch_size, fh, sh, ph, 2, 7,
  80. 8, oc, 2});
  81. } // end oc
  82. } // end ph
  83. } // end sh
  84. } // end fh
  85. } // end batch_size
  86. args.emplace_back(ConvArg{2, 1, 1, 0, 6, 18, 18, 9, 3});
  87. return args;
  88. }
  89. #endif
  90. } // namespace
  91. namespace megdnn {
  92. namespace test {
  93. TEST_F(X86, DEFAULT_CONV_DIRECT_STRIDE1) {
  94. using namespace convolution;
  95. std::vector<TestArg> args;
  96. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  97. size_t p) {
  98. if (w + 2 * p < kernel || h + 2 * p < kernel)
  99. return;
  100. param::Convolution param;
  101. param.stride_h = 1;
  102. param.stride_w = 1;
  103. param.pad_h = p;
  104. param.pad_w = p;
  105. args.emplace_back(param, TensorShape{1, ic, h, w},
  106. TensorShape{oc, ic, kernel, kernel});
  107. };
  108. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  109. for (size_t ic : {1, 4, 8, 16})
  110. for (size_t oc : {1, 4, 8})
  111. for (size_t p : {0, 2})
  112. for (size_t size : {20, 21, 24})
  113. run(oc, ic, size, size, kernel, p);
  114. Checker<ConvolutionForward> checker(handle());
  115. checker.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  116. "CONVOLUTION_DEFAULT_X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP"));
  117. checker.set_epsilon(1);
  118. UniformIntRNG rng{-50, 50};
  119. checker.set_dtype(0, dtype::Float32())
  120. .set_dtype(1, dtype::Float32())
  121. .set_dtype(2, dtype::Float32())
  122. .set_rng(0, &rng)
  123. .set_rng(1, &rng)
  124. .set_rng(2, &rng);
  125. for (auto&& arg : args) {
  126. checker.set_param(arg.param).exec({arg.src, arg.filter, {}});
  127. }
  128. }
  129. TEST_F(X86, DEFAULT_CONV_DIRECT_STRIDE2) {
  130. using namespace convolution;
  131. std::vector<TestArg> args;
  132. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  133. size_t p) {
  134. if (w + 2 * p < kernel || h + 2 * p < kernel)
  135. return;
  136. param::Convolution param;
  137. param.stride_h = 2;
  138. param.stride_w = 2;
  139. param.pad_h = p;
  140. param.pad_w = p;
  141. args.emplace_back(param, TensorShape{1, ic, h, w},
  142. TensorShape{oc, ic, kernel, kernel});
  143. };
  144. for (size_t kernel : {2, 3, 5, 7})
  145. for (size_t ic : {1, 4, 8, 16})
  146. for (size_t oc : {1, 4, 8})
  147. for (size_t p : {0, 2})
  148. for (size_t size : {20, 21, 24})
  149. run(oc, ic, size, size, kernel, p);
  150. Checker<ConvolutionForward> checker(handle());
  151. checker.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  152. "CONVOLUTION_DEFAULT_X86_CONV_BIAS_DIRECT_STRIDE2_LARGE_GROUP"));
  153. checker.set_epsilon(1);
  154. UniformIntRNG rng{-50, 50};
  155. checker.set_dtype(0, dtype::Float32())
  156. .set_dtype(1, dtype::Float32())
  157. .set_dtype(2, dtype::Float32())
  158. .set_rng(0, &rng)
  159. .set_rng(1, &rng)
  160. .set_rng(2, &rng);
  161. for (auto&& arg : args) {
  162. checker.set_param(arg.param).exec({arg.src, arg.filter, {}});
  163. }
  164. }
  165. #if MEGDNN_X86_WITH_MKL_DNN
  166. TEST_F(X86, CONVOLUTION_FORWARD_INT8) {
  167. Checker<ConvolutionForward> checker(handle());
  168. checker.set_before_exec_callback(
  169. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_MKLDNN_INT8"));
  170. param::Convolution param;
  171. param.sparse = param::Convolution::Sparse::GROUP;
  172. UniformIntRNG rng{-128, 127};
  173. std::vector<ConvArg> args = get_group_conv_args();
  174. for (auto&& arg : args) {
  175. param.stride_h = param.stride_w = arg.sh;
  176. param.pad_h = param.pad_w = arg.ph;
  177. checker.set_dtype(0, dtype::Int8())
  178. .set_dtype(1, dtype::Int8())
  179. .set_dtype(2, dtype::Int32())
  180. .set_rng(0, &rng)
  181. .set_rng(1, &rng)
  182. .set_param(param)
  183. .execs({{arg.batch_size, arg.ic * arg.groups, arg.ih, arg.iw},
  184. {arg.groups, arg.oc, arg.ic, arg.fh, arg.fh},
  185. {}});
  186. }
  187. args = get_dense_conv_args();
  188. param.sparse = param::Convolution::Sparse::DENSE;
  189. for (auto&& arg : args) {
  190. param.stride_h = param.stride_w = arg.sh;
  191. param.pad_h = param.pad_w = arg.ph;
  192. checker.set_dtype(0, dtype::Int8())
  193. .set_dtype(1, dtype::Int8())
  194. .set_dtype(2, dtype::Int32())
  195. .set_rng(0, &rng)
  196. .set_rng(1, &rng)
  197. .set_param(param)
  198. .execs({{arg.batch_size, arg.ic, arg.ih, arg.iw},
  199. {arg.oc, arg.ic, arg.fh, arg.fh},
  200. {}});
  201. }
  202. }
  203. TEST_F(X86, CONVOLUTION_FORWARD_MATMUL_INT8) {
  204. std::vector<ConvArg> args = get_dense_conv_args();
  205. Checker<ConvolutionForward> checker(handle());
  206. checker.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  207. "CONVOLUTION_DEFAULT_MKLDNN_MATMUL_INT8"));
  208. param::Convolution param;
  209. param.sparse = param::Convolution::Sparse::DENSE;
  210. UniformIntRNG rng{-128, 127};
  211. for (auto&& arg : args) {
  212. param.stride_h = param.stride_w = arg.sh;
  213. param.pad_h = param.pad_w = arg.ph;
  214. checker.set_dtype(0, dtype::Int8())
  215. .set_dtype(1, dtype::Int8())
  216. .set_dtype(2, dtype::Int32())
  217. .set_rng(0, &rng)
  218. .set_rng(1, &rng)
  219. .set_param(param)
  220. .execs({{arg.batch_size, arg.ic, arg.ih, arg.iw},
  221. {arg.oc, arg.ic, arg.fh, arg.fh},
  222. {}});
  223. }
  224. }
  225. static void x86_correctness_fp32_mkldnn_run(Checker<Convolution>& checker,
  226. UniformIntRNG& rng, Handle* handle,
  227. size_t n, size_t stride,
  228. size_t kernel, size_t oc, size_t ic,
  229. size_t h, size_t w, size_t group) {
  230. auto oc_per_group = oc / group;
  231. auto ic_per_group = ic / group;
  232. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  233. (ic_per_group % 8 == 0 || ic_per_group == 3) &&
  234. ic_per_group > 0;
  235. bool ok_depthwise = oc == ic && oc == group;
  236. if (!(ok_group || ok_depthwise)) {
  237. return;
  238. }
  239. size_t pad = kernel / 2;
  240. size_t kernel_h = kernel;
  241. size_t kernel_w = kernel;
  242. param::Convolution param;
  243. param.format = param::Convolution::Format::NCHW88;
  244. param.stride_h = stride;
  245. param.stride_w = stride;
  246. param.pad_h = pad;
  247. param.pad_w = pad;
  248. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  249. if (ic == 3) {
  250. src_tensor_shape = TensorShape{n, ic, h, w};
  251. }
  252. auto weight_tensor_shape =
  253. TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  254. if (ic == 3) {
  255. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  256. }
  257. if (group == 1) {
  258. param.sparse = param::Convolution::Sparse::DENSE;
  259. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  260. param.sparse = param::Convolution::Sparse::GROUP;
  261. weight_tensor_shape =
  262. TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  263. } else if (group > 1 && oc / group % 8 == 0 && oc / group > 0 &&
  264. ic / group % 8 == 0 && ic / group > 0) {
  265. param.sparse = param::Convolution::Sparse::GROUP;
  266. weight_tensor_shape = TensorShape{
  267. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8,
  268. 8};
  269. }
  270. checker.set_dtype(0, dtype::Float32())
  271. .set_dtype(1, dtype::Float32())
  272. .set_rng(0, &rng)
  273. .set_rng(1, &rng)
  274. .set_epsilon(1e-3)
  275. .set_param(param)
  276. .execs({src_tensor_shape, weight_tensor_shape, {}});
  277. }
  278. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  279. Checker<Convolution> checker(handle);
  280. UniformIntRNG rng{-127, 127};
  281. checker.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  282. "CONVOLUTION_DEFAULT_MKLDNN_CONV_FP32"));
  283. for (size_t n : {1, 2})
  284. for (size_t stride : {1, 2})
  285. for (size_t kernel : {3, 5, 7})
  286. for (size_t oc : {8, 16})
  287. for (size_t ic : {3, 8, 16})
  288. for (size_t h : {22, 33})
  289. for (size_t w : {22, 33}) {
  290. for (size_t group = 1;
  291. group <= std::min(oc, ic); ++group) {
  292. x86_correctness_fp32_mkldnn_run(
  293. checker, rng, handle, n, stride,
  294. kernel, oc, ic, h, w, group);
  295. }
  296. }
  297. }
  298. TEST_F(X86, CONVOLUTION_DIRECT_MKLDNN_C8) {
  299. x86_correctness_fp32_mkldnn(handle());
  300. }
  301. #endif
  302. #if MEGDNN_WITH_BENCHMARK
  303. TEST_F(X86, BENCHMARK_CONVOLUTION_I8x8x16) {
  304. using namespace convolution;
  305. using Param = param::Convolution;
  306. std::vector<TestArg> args;
  307. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  308. size_t stride, size_t group = 1) {
  309. Param param;
  310. param.stride_h = stride;
  311. param.stride_w = stride;
  312. param.pad_h = kernel / 2;
  313. param.pad_w = kernel / 2;
  314. if (group > 1) {
  315. param.sparse = param::Convolution::Sparse::GROUP;
  316. args.emplace_back(
  317. param, TensorShape{1, ic, h, w},
  318. TensorShape{group, oc / group, ic / group, kernel, kernel});
  319. } else {
  320. param.sparse = param::Convolution::Sparse::DENSE;
  321. args.emplace_back(param, TensorShape{1, ic, h, w},
  322. TensorShape{oc, ic, kernel, kernel});
  323. }
  324. };
  325. run(48, 96, 15, 15, 1, 1);
  326. run(64, 64, 60, 60, 3, 1);
  327. run(64, 64, 60, 60, 3, 1, 64);
  328. constexpr size_t RUN = 30;
  329. Benchmarker<Convolution> benchmark(handle());
  330. benchmark.set_dtype(0, dtype::Int8())
  331. .set_dtype(1, dtype::Int8())
  332. .set_dtype(2, dtype::Int16());
  333. benchmark.set_before_exec_callback(AlgoChecker<Convolution>(".*"));
  334. benchmark.set_display(false);
  335. benchmark.set_times(RUN);
  336. for (auto&& arg : args) {
  337. TensorLayout dst_layout;
  338. auto opr = handle()->create_operator<Convolution>();
  339. opr->param() = arg.param;
  340. opr->deduce_layout({arg.src, dtype::Float32()},
  341. {arg.filter, dtype::Float32()}, dst_layout);
  342. //! dst.nr_elems * IC * FH * FW * 2
  343. float icpg = arg.filter.ndim == 4 ? arg.filter[1] : arg.filter[2];
  344. float filter = arg.filter.ndim == 4 ? arg.filter[2] : arg.filter[3];
  345. float computations = dst_layout.total_nr_elems() * icpg * filter *
  346. filter * 2.0 / (1024 * 1024 * 1024) * 1e3;
  347. auto used_int =
  348. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  349. RUN;
  350. printf("%s %s: int: %f ms %f Gflops \n", arg.src.to_string().c_str(),
  351. arg.filter.to_string().c_str(), used_int,
  352. computations / used_int);
  353. }
  354. }
  355. #if MEGDNN_X86_WITH_MKL_DNN
  356. TEST_F(X86, BENCHMARK_CONVOLUTION_I8x8x32_MKLDNN) {
  357. using namespace convolution;
  358. using Param = param::Convolution;
  359. std::vector<TestArg> args;
  360. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  361. size_t stride) {
  362. Param param;
  363. param.stride_h = stride;
  364. param.stride_w = stride;
  365. param.pad_h = kernel / 2;
  366. param.pad_w = kernel / 2;
  367. args.emplace_back(param, TensorShape{1, ic, h, w},
  368. TensorShape{oc, ic, kernel, kernel});
  369. };
  370. for (size_t kernel : {2, 3, 5, 7}) {
  371. for (size_t ic : {1, 8, 16, 32, 64}) {
  372. for (size_t oc : {1, 8, 16, 32, 64}) {
  373. run(oc, ic, 56, 56, kernel, 1);
  374. run(oc, ic, 128, 128, kernel, 1);
  375. run(oc, ic, 256, 256, kernel, 1);
  376. }
  377. }
  378. }
  379. constexpr size_t RUN = 50;
  380. Benchmarker<Convolution> benchmark(handle());
  381. benchmark.set_dtype(0, dtype::Int8())
  382. .set_dtype(1, dtype::Int8())
  383. .set_dtype(2, dtype::Int32());
  384. benchmark.set_display(false);
  385. benchmark.set_times(RUN);
  386. Benchmarker<Convolution> benchmark_float(handle());
  387. benchmark_float.set_display(false);
  388. benchmark_float.set_times(RUN);
  389. for (auto&& arg : args) {
  390. TensorLayout dst_layout;
  391. auto opr = handle()->create_operator<Convolution>();
  392. opr->param() = arg.param;
  393. opr->deduce_layout({arg.src, dtype::Float32()},
  394. {arg.filter, dtype::Float32()}, dst_layout);
  395. //! dst.nr_elems * IC * FH * FW * 2
  396. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  397. arg.filter[2] * arg.filter[3] * 2.0 /
  398. (1024 * 1024 * 1024) * 1e3;
  399. auto used_int =
  400. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  401. RUN;
  402. auto used_float = benchmark_float.set_param(arg.param).exec(
  403. {arg.src, arg.filter, {}}) /
  404. RUN;
  405. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  406. "%f\n",
  407. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  408. used_int, computations / used_int, used_float,
  409. computations / used_float, used_float / used_int);
  410. }
  411. }
  412. #endif
  413. #endif
  414. } // namespace test
  415. } // namespace megdnn
  416. // vim: syntax=cpp.doxygen

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