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

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

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