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

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

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