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

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