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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /**
  2. * \file dnn/test/cuda/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 implied.
  10. */
  11. #include "megdnn/dtype.h"
  12. #include "megdnn/oprs.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "test/cuda/fixture.h"
  15. #include "test/common/tensor.h"
  16. #include "test/common/workspace_wrapper.h"
  17. #include "test/common/checker.h"
  18. #include "test/common/convolution.h"
  19. #include "test/common/rng.h"
  20. #include "test/cuda/benchmark.h"
  21. #include "src/cuda/utils.h"
  22. #define V1(x) #x
  23. #define V(x) V1(x)
  24. #define CUDNN_VERSION_STRING \
  25. "v" V(CUDNN_MAJOR) "." V(CUDNN_MINOR) "." V(CUDNN_PATCHLEVEL)
  26. namespace megdnn {
  27. namespace test {
  28. TEST_F(CUDA, CONVOLUTION_8X8X32)
  29. {
  30. if (!cuda::is_compute_capability_required(6, 1)) {
  31. printf("Skip CUDA.CONVOLUTION_8X8X32 test as current device"
  32. "doesn't support\n");
  33. return;
  34. }
  35. using namespace convolution;
  36. std::vector<TestArg> args;
  37. {
  38. auto v = get_args();
  39. for (auto &&a: v) {
  40. args.push_back(std::move(a));
  41. }
  42. }
  43. {
  44. auto v = get_dilated_args();
  45. for (auto &&a: v) {
  46. args.push_back(std::move(a));
  47. }
  48. }
  49. {
  50. auto v = get_chanwise_args();
  51. for (auto &&a: v) {
  52. args.push_back(std::move(a));
  53. }
  54. }
  55. Checker<ConvolutionForward> checker(handle_cuda());
  56. UniformIntRNG rng(-4, 4);
  57. for (auto arg: args) {
  58. arg.param.format = param::Convolution::Format::NHWC;
  59. arg.src = cvt_src_or_dst_nchw2nhwc(arg.src);
  60. arg.filter = cvt_filter_nchw2nhwc(arg.filter);
  61. checker.set_dtype(0, dtype::Int8()).
  62. set_dtype(1, dtype::Int8()).
  63. set_dtype(2, dtype::Int32()).
  64. set_param(arg.param).
  65. set_rng(0, &rng).
  66. set_rng(1, &rng).
  67. execs({arg.src, arg.filter, {}});
  68. }
  69. }
  70. TEST_F(CUDA, CONVOLUTION_FORWARD)
  71. {
  72. using namespace convolution;
  73. std::vector<TestArg> args = get_args();
  74. Checker<ConvolutionForward> checker(handle_cuda());
  75. NormalRNG default_rng;
  76. for (auto &&arg: args) {
  77. float scale =
  78. 1.0f / sqrt(arg.filter[1] * arg.filter[2] * arg.filter[3]);
  79. UniformFloatRNG rng(scale, 2 * scale);
  80. checker.
  81. set_dtype(0, dtype::Float32()).
  82. set_dtype(1, dtype::Float32()).
  83. set_dtype(2, dtype::Float32()).
  84. set_rng(0, &default_rng).
  85. set_rng(1, &default_rng).
  86. set_epsilon(1e-3).
  87. set_param(arg.param).
  88. execs({arg.src, arg.filter, {}});
  89. checker.
  90. set_dtype(0, dtype::Float16()).
  91. set_dtype(1, dtype::Float16()).
  92. set_dtype(2, dtype::Float16()).
  93. set_rng(0, &rng).
  94. set_rng(1, &rng).
  95. set_epsilon(1e-1).
  96. set_param(arg.param).
  97. execs({arg.src, arg.filter, {}});
  98. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  99. checker.set_dtype(0, dtype::Float16())
  100. .set_dtype(1, dtype::Float16())
  101. .set_dtype(2, dtype::Float16())
  102. .set_rng(0, &rng)
  103. .set_rng(1, &rng)
  104. .set_epsilon(1e-1)
  105. .set_param(arg.param)
  106. .execs({arg.src, arg.filter, {}});
  107. checker.set_dtype(0, dtype::BFloat16())
  108. .set_dtype(1, dtype::BFloat16())
  109. .set_dtype(2, dtype::BFloat16())
  110. .set_epsilon(1e-1)
  111. .set_param(arg.param)
  112. .execs({arg.src, arg.filter, {}});
  113. }
  114. }
  115. TEST_F(CUDA, CONV_FORWARD_MATMUL_NCHW4) {
  116. if (!cuda::is_compute_capability_required(6, 1))
  117. return;
  118. using namespace convolution;
  119. Checker<Convolution> checker(handle_cuda());
  120. UniformIntRNG int_rng{-127, 127};
  121. Convolution::Param param;
  122. param.format = Convolution::Param::Format::NCHW4;
  123. checker.set_dtype(0, dtype::QuantizedS8(0.132f))
  124. .set_dtype(1, dtype::QuantizedS8(0.0239f))
  125. .set_dtype(2, dtype::QuantizedS32(0.132f * 0.0239f))
  126. .set_rng(0, &int_rng)
  127. .set_rng(1, &int_rng)
  128. .set_param(param);
  129. checker.set_before_exec_callback(AlgoChecker<Convolution>(
  130. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  131. "MATMUL8X8X32", {})
  132. .c_str()));
  133. param.sparse = Convolution::Param::Sparse::DENSE;
  134. param.pad_h = param.pad_w = 1;
  135. param.stride_h = param.stride_w = 1;
  136. checker.set_param(param);
  137. checker.exec({{8, 4, 10, 10, 4}, {16, 4, 3, 3, 4}, {}});
  138. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {}});
  139. checker.exec({{8, 64, 12, 12, 4}, {256, 64, 3, 3, 4}, {}});
  140. }
  141. TEST_F(CUDA, CONVOLUTION_1X1_FORWARD)
  142. {
  143. using namespace convolution;
  144. std::vector<TestArg> args = get_1x1_args();
  145. Checker<ConvolutionForward> checker(handle_cuda());
  146. NormalRNG default_rng;
  147. for (auto &&arg: args) {
  148. float scale = 1.0f / sqrt(arg.filter[1] * arg.filter[2] * arg.filter[3]);
  149. UniformFloatRNG rng(scale, 2 * scale);
  150. checker.
  151. set_dtype(0, dtype::Float32()).
  152. set_dtype(1, dtype::Float32()).
  153. set_rng(0, &default_rng).
  154. set_rng(1, &default_rng).
  155. set_epsilon(1e-3).
  156. set_param(arg.param).
  157. execs({arg.src, arg.filter, {}});
  158. }
  159. }
  160. TEST_F(CUDA, BENCHMARK_CONVOLUTION_1X1_FORWARD)
  161. {
  162. using namespace convolution;
  163. std::vector<TestArg> args = get_1x1_args();
  164. Benchmarker<ConvolutionForward> marker(handle_cuda());
  165. NormalRNG default_rng;
  166. for (auto &&arg: args) {
  167. float scale = 1.0f / sqrt(arg.filter[1] * arg.filter[2] * arg.filter[3]);
  168. UniformFloatRNG rng(scale, 2 * scale);
  169. marker.set_dtype(0, dtype::Float32()).
  170. set_dtype(1, dtype::Float32()).
  171. set_rng(0, &default_rng).
  172. set_rng(1, &default_rng).
  173. set_param(arg.param).
  174. execs({arg.src, arg.filter, {}});
  175. }
  176. }
  177. TEST_F(CUDA, CONVOLUTION_BACKWARD_DATA)
  178. {
  179. using namespace convolution;
  180. std::vector<TestArg> args = get_args_cuda_conv_bwd_data();
  181. Checker<ConvolutionBackwardData> checker(handle_cuda());
  182. NormalRNG default_rng;
  183. for (auto &&arg: args) {
  184. float scale =
  185. 64.f / sqrt(arg.filter[0] * arg.filter[2] * arg.filter[3]);
  186. UniformFloatRNG rng(scale, 2 * scale);
  187. auto src = TensorLayout(arg.src, dtype::Float32());
  188. auto filter = TensorLayout(arg.filter, dtype::Float32());
  189. TensorLayout dst;
  190. {
  191. auto opr = handle_cuda()->create_operator<Convolution>();
  192. opr->param() = arg.param;
  193. opr->deduce_layout(src, filter, dst);
  194. }
  195. src.dtype = dst.dtype = filter.dtype = dtype::Float32();
  196. checker.set_rng(0, &default_rng)
  197. .set_rng(1, &default_rng)
  198. .set_epsilon(1e-3)
  199. .set_param(arg.param)
  200. .exec(TensorLayoutArray{filter, dst, src});
  201. if (!cuda::is_compute_capability_required(6, 0)) {
  202. src.dtype = dst.dtype = filter.dtype = dtype::Float16();
  203. checker.set_rng(0, &rng)
  204. .set_rng(1, &rng)
  205. .set_epsilon(1e-1)
  206. .set_param(arg.param)
  207. .exec(TensorLayoutArray{filter, dst, src});
  208. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  209. checker.set_rng(0, &rng)
  210. .set_rng(1, &rng)
  211. .set_epsilon(1e-1)
  212. .set_param(arg.param)
  213. .exec(TensorLayoutArray{filter, dst, src});
  214. }
  215. checker.set_before_exec_callback(AlgoChecker<ConvolutionBackwardData>(
  216. ExecutionPolicyAlgoName{"CONVOLUTION_BACKWARD_DATD_BFLOAT16",
  217. {{"MATMUL", {}}}}));
  218. src.dtype = dst.dtype = filter.dtype = dtype::BFloat16();
  219. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  220. checker.set_rng(0, &rng)
  221. .set_rng(1, &rng)
  222. .set_epsilon(1e-1)
  223. .set_param(arg.param)
  224. .exec(TensorLayoutArray{filter, dst, src});
  225. checker.reset_before_exec_callback();
  226. checker.opr()->execution_policy() = {};
  227. }
  228. }
  229. TEST_F(CUDA, CONVOLUTION_BACKWARD_DATA_FAILED_CUDNN7_5)
  230. {
  231. // BRAIN-481 failed on architectures 7.0, remove the following if statement,
  232. // when cudnn fixed the problem.
  233. if (cuda::is_compute_capability_required(7, 0))
  234. return;
  235. using namespace convolution;
  236. std::vector<TestArg> args = get_args_cudnn_7_5_failures();
  237. Checker<ConvolutionBackwardData> checker(handle_cuda());
  238. NormalRNG default_rng;
  239. for (auto &&arg: args) {
  240. float scale = 128.f / sqrt(arg.filter[0] * arg.filter[2] * arg.filter[3]);
  241. scale = std::max(scale, 1.f);
  242. UniformFloatRNG rng(scale, 2 * scale);
  243. auto src = TensorLayout(arg.src, dtype::Float32());
  244. auto filter = TensorLayout(arg.filter, dtype::Float32());
  245. TensorLayout dst;
  246. {
  247. auto opr = handle_cuda()->create_operator<Convolution>();
  248. opr->param() = arg.param;
  249. opr->deduce_layout(src, filter, dst);
  250. }
  251. src.dtype = dst.dtype = filter.dtype = dtype::Float32();
  252. checker.
  253. set_rng(0, &default_rng).
  254. set_rng(1, &default_rng).
  255. set_epsilon(1e-3).
  256. set_param(arg.param).
  257. exec(TensorLayoutArray{filter, dst, src});
  258. src.dtype = dst.dtype = filter.dtype = dtype::Float16();
  259. checker.
  260. set_rng(0, &rng).
  261. set_rng(1, &rng).
  262. set_epsilon(1e-1).
  263. set_param(arg.param).
  264. exec(TensorLayoutArray{filter, dst, src});
  265. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  266. checker.set_rng(0, &rng)
  267. .set_rng(1, &rng)
  268. .set_epsilon(1e-1)
  269. .set_param(arg.param)
  270. .exec(TensorLayoutArray{filter, dst, src});
  271. }
  272. }
  273. TEST_F(CUDA, CONVOLUTION_BACKWARD_FILTER)
  274. {
  275. using namespace convolution;
  276. std::vector<TestArg> args = get_args();
  277. Checker<ConvolutionBackwardFilter> checker(handle_cuda());
  278. bool f16_checked = false;
  279. for (auto &&arg: args) {
  280. auto src = TensorLayout(arg.src, dtype::Float32());
  281. auto filter = TensorLayout(arg.filter, dtype::Float32());
  282. TensorLayout dst;
  283. {
  284. auto opr = handle_cuda()->create_operator<Convolution>();
  285. opr->param() = arg.param;
  286. opr->deduce_layout(src, filter, dst);
  287. }
  288. float scale = 1.0f / sqrt(dst[2] * dst[3]);
  289. UniformFloatRNG rng(scale, 2 * scale);
  290. src.dtype = dst.dtype = filter.dtype = dtype::Float32();
  291. checker.
  292. set_rng(0, &rng).
  293. set_rng(1, &rng).
  294. set_epsilon(1e-3).
  295. set_param(arg.param).
  296. exec(TensorLayoutArray{src, dst, filter});
  297. // reduce on large f16 array may introduce significant error
  298. if (dst.total_nr_elems() >= 1000 && f16_checked)
  299. continue;
  300. f16_checked = true;
  301. src.dtype = dst.dtype = filter.dtype = dtype::Float16();
  302. checker.
  303. set_rng(0, &rng).
  304. set_rng(1, &rng).
  305. set_epsilon(1e-1).
  306. set_param(arg.param).
  307. exec(TensorLayoutArray{src, dst, filter});
  308. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  309. checker.set_rng(0, &rng)
  310. .set_rng(1, &rng)
  311. .set_epsilon(1e-1)
  312. .set_param(arg.param)
  313. .exec(TensorLayoutArray{src, dst, filter});
  314. checker.set_before_exec_callback(AlgoChecker<ConvolutionBackwardFilter>(
  315. ExecutionPolicyAlgoName{"CONVOLUTION_BACKWARD_FILTER_BFLOAT16",
  316. {{"MATMUL", {}}}}));
  317. src.dtype = dst.dtype = filter.dtype = dtype::BFloat16();
  318. checker.set_rng(0, &rng)
  319. .set_rng(1, &rng)
  320. .set_epsilon(1e-1)
  321. .set_param(arg.param)
  322. .exec(TensorLayoutArray{src, dst, filter});
  323. checker.reset_before_exec_callback();
  324. checker.opr()->execution_policy() = {};
  325. }
  326. }
  327. TEST_F(CUDA, CONV_CONFIG_COMBINATIONS) {
  328. auto eps_getter = [](bool f16, int stage, const char *name) -> float {
  329. if (f16) {
  330. return stage == 2 ? 0.5 : 0.2;
  331. }
  332. if (strstr(name, "WINOGRAD_NONFUSED"))
  333. return 0.3;
  334. return 1e-3;
  335. };
  336. convolution::test_conv_config_combinations(2, handle_cuda(), false, true,
  337. true, eps_getter, true);
  338. convolution::test_conv_config_combinations(3, handle_cuda(), false, true,
  339. true, eps_getter, true);
  340. convolution::test_conv_config_combinations(5, handle_cuda(), false, true,
  341. true, eps_getter, true);
  342. }
  343. TEST_F(CUDA, CONVOLUTION_BACKWARD_DATA_1) {
  344. if (cuda::is_compute_capability_required(7, 0))
  345. return;
  346. using namespace convolution;
  347. Checker<ConvolutionBackwardData> checker(handle_cuda());
  348. checker.set_before_exec_callback(AlgoChecker<ConvolutionBackwardData>(
  349. "CUDNN_CONVOLUTION_BWD_DATA_ALGO_1" CUDNN_VERSION_STRING));
  350. NormalRNG default_rng;
  351. TensorShape s_filter = TensorShape{8, 8, 2, 2},
  352. s_src = TensorShape{2, 8, 18, 18};
  353. float scale = 1.0f / sqrt(s_filter[0] * s_filter[2] * s_filter[3]);
  354. UniformFloatRNG rng(scale, 2 * scale);
  355. auto src = TensorLayout(s_src, dtype::Float16());
  356. auto filter = TensorLayout(s_filter, dtype::Float16());
  357. TensorLayout dst;
  358. param::Convolution param;
  359. param.pad_h = param.pad_w = 2;
  360. param.stride_h = param.stride_w = 2;
  361. {
  362. auto opr = handle_cuda()->create_operator<Convolution>();
  363. opr->param() = param;
  364. opr->deduce_layout(src, filter, dst);
  365. }
  366. src.dtype = dst.dtype = filter.dtype = dtype::Float16();
  367. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  368. checker.set_rng(0, &rng)
  369. .set_rng(1, &rng)
  370. .set_epsilon(0.2)
  371. .set_param(param)
  372. .exec(TensorLayoutArray{filter, dst, src});
  373. }
  374. #if MEGDNN_WITH_BENCHMARK
  375. TEST_F(CUDA, CONV_FWD_BENCHMARK) {
  376. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  377. size_t SH = 1, size_t SW = 1, size_t FH = 1, size_t FW = 1,
  378. size_t PH = 0, size_t PW = 0, bool fp16io_c32 = false) {
  379. auto benchmarker = Benchmarker<ConvolutionForward>(handle_cuda());
  380. benchmarker.set_dtype(0, dtype::Float16())
  381. .set_dtype(1, dtype::Float16())
  382. .set_dtype(2, dtype::Float16());
  383. ConvolutionForward::Param param;
  384. param.stride_h = SH;
  385. param.stride_w = SW;
  386. param.pad_h = PH;
  387. param.pad_w = PW;
  388. if (fp16io_c32) {
  389. param.compute_mode =
  390. ConvolutionForward::Param::ComputeMode::FLOAT32;
  391. }
  392. benchmarker.set_param(param);
  393. std::unique_ptr<OprProxy<ConvolutionForward>> proxy{
  394. new OprProxy<ConvolutionForward>{true}};
  395. benchmarker.set_proxy(proxy);
  396. size_t OH = (IH - FH + 2 * PH) / SH + 1;
  397. size_t OW = (IW - FW + 2 * PW) / SW + 1;
  398. auto time = benchmarker.execs(
  399. {{N, IC, IH, IW}, {OC, IC, FH, FW}, {N, OC, OH, OW}});
  400. time /= 1000.0 * 10.0;
  401. auto flo = (double)N * OC * IC * OH * OW * FH * FW * 2;
  402. auto flops = flo / time / 1e12;
  403. printf("comp_type %s: ", fp16io_c32 ? "32" : "16");
  404. printf("%.3fG FLO, flops %.3fTFLOPS\n", flo / 1e9, flops);
  405. };
  406. run(32, 512, 256, 56, 56, 1, 1, 1, 1, 0, 0, false);
  407. run(32, 512, 256, 56, 56, 1, 1, 1, 1, 0, 0, true);
  408. }
  409. TEST_F(CUDA, CONVOLUTION_FWD_BENCHMARK) {
  410. CUBenchmarker<ConvolutionForward> bench{handle_cuda()};
  411. std::unique_ptr<OprProxy<ConvolutionForward>> proxy{
  412. new OprProxy<ConvolutionForward>{true}};
  413. size_t RUNS = 10;
  414. bench.set_proxy(proxy).set_times(RUNS);
  415. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  416. size_t FH, size_t SH, size_t PH) {
  417. bench.set_dtype(0, dtype::Float32())
  418. .set_dtype(1, dtype::Float32())
  419. .set_dtype(2, dtype::Float32());
  420. param::Convolution param;
  421. param.stride_h = param.stride_w = SH;
  422. param.pad_h = param.pad_w = PH;
  423. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  424. bench.set_param(param);
  425. bench.proxy()->target_execution_policy.algo.reset();
  426. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  427. filter{{OC, IC, FH, FH}, dtype::Float32()};
  428. TensorLayout dst;
  429. {
  430. auto&& opr = handle_cuda()->create_operator<Convolution>();
  431. opr->param() = param;
  432. opr->deduce_layout(src, filter, dst);
  433. }
  434. auto time_ms_fp32 = bench.execl({src, filter, dst}) / RUNS;
  435. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  436. bench.proxy()->target_execution_policy.algo.reset();
  437. bench.set_dtype(0, dtype::Float16())
  438. .set_dtype(1, dtype::Float16())
  439. .set_dtype(2, dtype::Float16());
  440. auto time_ms_true_fp16 = bench.execl({src, filter, dst}) / RUNS;
  441. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  442. bench.proxy()->target_execution_policy.algo.reset();
  443. bench.set_param(param);
  444. auto time_ms_pseudo_fp16 = bench.execl({src, filter, dst}) / RUNS;
  445. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  446. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  447. filter.to_string().c_str(), dst.to_string().c_str());
  448. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  449. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  450. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  451. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  452. (flo / (time_ms_pseudo_fp16 * 1e9)));
  453. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  454. time_ms_fp32 / time_ms_true_fp16,
  455. time_ms_pseudo_fp16 / time_ms_true_fp16);
  456. };
  457. run(32, 64, 3, 224, 224, 7, 2, 3);
  458. run(32, 128, 128, 28, 28, 3, 1, 1);
  459. run(32, 256, 256, 14, 14, 3, 1, 1);
  460. run(32, 512, 512, 7, 7, 3, 1, 1);
  461. run(32, 64, 64, 56, 56, 3, 1, 1);
  462. run(32, 512, 256, 56, 56, 1, 2, 0);
  463. run(32, 1024, 512, 28, 28, 1, 2, 0);
  464. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  465. run(32, 512, 128, 28, 28, 1, 1, 0);
  466. run(32, 128, 512, 28, 28, 1, 1, 0);
  467. run(32, 1024, 256, 14, 14, 1, 1, 0);
  468. run(32, 256, 1024, 14, 14, 1, 1, 0);
  469. run(32, 2048, 512, 7, 7, 1, 1, 0);
  470. run(32, 512, 2048, 7, 7, 1, 1, 0);
  471. run(32, 256, 64, 56, 56, 1, 1, 0);
  472. run(32, 64, 256, 56, 56, 1, 1, 0);
  473. run(32, 128, 256, 56, 56, 1, 2, 0);
  474. run(32, 256, 512, 28, 28, 1, 2, 0);
  475. run(32, 512, 1024, 14, 14, 1, 2, 0);
  476. run(32, 64, 64, 56, 56, 1, 1, 0);
  477. }
  478. TEST_F(CUDA, CONVOLUTION_BWD_DATA_BENCHMARK) {
  479. CUBenchmarker<ConvolutionBackwardData> bench{handle_cuda()};
  480. std::unique_ptr<OprProxy<ConvolutionBackwardData>> proxy{
  481. new OprProxy<ConvolutionBackwardData>{true}};
  482. size_t RUNS = 10;
  483. bench.set_proxy(proxy).set_times(RUNS);
  484. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  485. size_t FH, size_t SH, size_t PH) {
  486. bench.set_dtype(0, dtype::Float32())
  487. .set_dtype(1, dtype::Float32())
  488. .set_dtype(2, dtype::Float32());
  489. param::Convolution param;
  490. param.stride_h = param.stride_w = SH;
  491. param.pad_h = param.pad_w = PH;
  492. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  493. bench.set_param(param);
  494. bench.proxy()->target_execution_policy.algo.reset();
  495. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  496. filter{{OC, IC, FH, FH}, dtype::Float32()};
  497. TensorLayout dst;
  498. {
  499. auto&& opr = handle_cuda()->create_operator<Convolution>();
  500. opr->param() = param;
  501. opr->deduce_layout(src, filter, dst);
  502. }
  503. auto time_ms_fp32 = bench.execl({filter, dst, src}) / RUNS;
  504. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  505. bench.proxy()->target_execution_policy.algo.reset();
  506. bench.set_dtype(0, dtype::Float16())
  507. .set_dtype(1, dtype::Float16())
  508. .set_dtype(2, dtype::Float16());
  509. auto time_ms_true_fp16 = bench.execl({filter, dst, src}) / RUNS;
  510. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  511. bench.proxy()->target_execution_policy.algo.reset();
  512. bench.set_param(param);
  513. auto time_ms_pseudo_fp16 = bench.execl({filter, dst, src}) / RUNS;
  514. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  515. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  516. filter.to_string().c_str(), dst.to_string().c_str());
  517. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  518. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  519. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  520. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  521. (flo / (time_ms_pseudo_fp16 * 1e9)));
  522. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  523. time_ms_fp32 / time_ms_true_fp16,
  524. time_ms_pseudo_fp16 / time_ms_true_fp16);
  525. };
  526. run(32, 64, 3, 224, 224, 7, 2, 3);
  527. run(32, 128, 128, 28, 28, 3, 1, 1);
  528. run(32, 256, 256, 14, 14, 3, 1, 1);
  529. run(32, 512, 512, 7, 7, 3, 1, 1);
  530. run(32, 64, 64, 56, 56, 3, 1, 1);
  531. run(32, 512, 256, 56, 56, 1, 2, 0);
  532. run(32, 1024, 512, 28, 28, 1, 2, 0);
  533. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  534. run(32, 512, 128, 28, 28, 1, 1, 0);
  535. run(32, 128, 512, 28, 28, 1, 1, 0);
  536. run(32, 1024, 256, 14, 14, 1, 1, 0);
  537. run(32, 256, 1024, 14, 14, 1, 1, 0);
  538. run(32, 2048, 512, 7, 7, 1, 1, 0);
  539. run(32, 512, 2048, 7, 7, 1, 1, 0);
  540. run(32, 256, 64, 56, 56, 1, 1, 0);
  541. run(32, 64, 256, 56, 56, 1, 1, 0);
  542. run(32, 128, 256, 56, 56, 1, 2, 0);
  543. run(32, 256, 512, 28, 28, 1, 2, 0);
  544. run(32, 512, 1024, 14, 14, 1, 2, 0);
  545. run(32, 64, 64, 56, 56, 1, 1, 0);
  546. }
  547. TEST_F(CUDA, BENCHMARK_CONVOLUTION_BWD_DATA_BF16) {
  548. CUBenchmarker<ConvolutionBackwardData> bench{handle_cuda()};
  549. std::unique_ptr<OprProxy<ConvolutionBackwardData>> proxy{
  550. new OprProxy<ConvolutionBackwardData>{true}};
  551. size_t RUNS = 10;
  552. bench.set_proxy(proxy).set_times(RUNS);
  553. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  554. size_t FH, size_t SH, size_t PH) {
  555. bench.set_dtype(0, dtype::BFloat16())
  556. .set_dtype(1, dtype::BFloat16())
  557. .set_dtype(2, dtype::BFloat16());
  558. param::Convolution param;
  559. param.stride_h = param.stride_w = SH;
  560. param.pad_h = param.pad_w = PH;
  561. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  562. bench.set_param(param);
  563. bench.proxy()->target_execution_policy = {};
  564. TensorLayout src{{N, IC, IH, IW}, dtype::BFloat16()},
  565. filter{{OC, IC, FH, FH}, dtype::BFloat16()};
  566. TensorLayout dst;
  567. {
  568. auto&& opr = handle_cuda()->create_operator<Convolution>();
  569. opr->param() = param;
  570. opr->deduce_layout(src, filter, dst);
  571. }
  572. auto used = bench.execl({filter, dst, src}) / RUNS;
  573. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  574. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  575. filter.to_string().c_str(), dst.to_string().c_str());
  576. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\n", used,
  577. (flo / (used * 1e9)));
  578. };
  579. run(32, 64, 3, 224, 224, 7, 2, 3);
  580. run(32, 128, 128, 28, 28, 3, 1, 1);
  581. run(32, 256, 256, 14, 14, 3, 1, 1);
  582. run(32, 512, 512, 7, 7, 3, 1, 1);
  583. run(32, 64, 64, 56, 56, 3, 1, 1);
  584. run(32, 512, 256, 56, 56, 1, 2, 0);
  585. run(32, 1024, 512, 28, 28, 1, 2, 0);
  586. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  587. run(32, 512, 128, 28, 28, 1, 1, 0);
  588. run(32, 128, 512, 28, 28, 1, 1, 0);
  589. run(32, 1024, 256, 14, 14, 1, 1, 0);
  590. run(32, 256, 1024, 14, 14, 1, 1, 0);
  591. run(32, 2048, 512, 7, 7, 1, 1, 0);
  592. run(32, 512, 2048, 7, 7, 1, 1, 0);
  593. run(32, 256, 64, 56, 56, 1, 1, 0);
  594. run(32, 64, 256, 56, 56, 1, 1, 0);
  595. run(32, 128, 256, 56, 56, 1, 2, 0);
  596. run(32, 256, 512, 28, 28, 1, 2, 0);
  597. run(32, 512, 1024, 14, 14, 1, 2, 0);
  598. run(32, 64, 64, 56, 56, 1, 1, 0);
  599. }
  600. TEST_F(CUDA, CONVOLUTION_BWD_FILTER_BENCHMARK) {
  601. CUBenchmarker<ConvolutionBackwardFilter> bench{handle_cuda()};
  602. std::unique_ptr<OprProxy<ConvolutionBackwardFilter>> proxy{
  603. new OprProxy<ConvolutionBackwardFilter>{true}};
  604. size_t RUNS = 10;
  605. bench.set_proxy(proxy).set_times(RUNS);
  606. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  607. size_t FH, size_t SH, size_t PH) {
  608. bench.set_dtype(0, dtype::Float32())
  609. .set_dtype(1, dtype::Float32())
  610. .set_dtype(2, dtype::Float32());
  611. param::Convolution param;
  612. param.stride_h = param.stride_w = SH;
  613. param.pad_h = param.pad_w = PH;
  614. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  615. bench.set_param(param);
  616. bench.proxy()->target_execution_policy.algo.reset();
  617. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  618. filter{{OC, IC, FH, FH}, dtype::Float32()};
  619. TensorLayout dst;
  620. {
  621. auto&& opr = handle_cuda()->create_operator<Convolution>();
  622. opr->param() = param;
  623. opr->deduce_layout(src, filter, dst);
  624. }
  625. auto time_ms_fp32 = bench.execl({src, dst, filter}) / RUNS;
  626. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  627. bench.proxy()->target_execution_policy.algo.reset();
  628. bench.set_dtype(0, dtype::Float16())
  629. .set_dtype(1, dtype::Float16())
  630. .set_dtype(2, dtype::Float16());
  631. auto time_ms_true_fp16 = bench.execl({src, dst, filter}) / RUNS;
  632. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  633. bench.proxy()->target_execution_policy.algo.reset();
  634. bench.set_param(param);
  635. auto time_ms_pseudo_fp16 = bench.execl({src, dst, filter}) / RUNS;
  636. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  637. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  638. filter.to_string().c_str(), dst.to_string().c_str());
  639. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  640. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  641. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  642. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  643. (flo / (time_ms_pseudo_fp16 * 1e9)));
  644. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  645. time_ms_fp32 / time_ms_true_fp16,
  646. time_ms_pseudo_fp16 / time_ms_true_fp16);
  647. };
  648. run(32, 64, 3, 224, 224, 7, 2, 3);
  649. run(32, 128, 128, 28, 28, 3, 1, 1);
  650. run(32, 256, 256, 14, 14, 3, 1, 1);
  651. run(32, 512, 512, 7, 7, 3, 1, 1);
  652. run(32, 64, 64, 56, 56, 3, 1, 1);
  653. run(32, 512, 256, 56, 56, 1, 2, 0);
  654. run(32, 1024, 512, 28, 28, 1, 2, 0);
  655. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  656. run(32, 512, 128, 28, 28, 1, 1, 0);
  657. run(32, 128, 512, 28, 28, 1, 1, 0);
  658. run(32, 1024, 256, 14, 14, 1, 1, 0);
  659. run(32, 256, 1024, 14, 14, 1, 1, 0);
  660. run(32, 2048, 512, 7, 7, 1, 1, 0);
  661. run(32, 512, 2048, 7, 7, 1, 1, 0);
  662. run(32, 256, 64, 56, 56, 1, 1, 0);
  663. run(32, 64, 256, 56, 56, 1, 1, 0);
  664. run(32, 128, 256, 56, 56, 1, 2, 0);
  665. run(32, 256, 512, 28, 28, 1, 2, 0);
  666. run(32, 512, 1024, 14, 14, 1, 2, 0);
  667. run(32, 64, 64, 56, 56, 1, 1, 0);
  668. }
  669. #endif
  670. #undef CUDNN_VERSION_STRING
  671. #undef V
  672. #undef V1
  673. } // namespace test
  674. } // namespace megdnn
  675. // vim: syntax=cpp.doxygen

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