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

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