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

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

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