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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. src.dtype = dst.dtype = filter.dtype = dtype::BFloat16();
  314. checker.set_rng(0, &rng)
  315. .set_rng(1, &rng)
  316. .set_epsilon(1e-1)
  317. .set_param(arg.param)
  318. .exec(TensorLayoutArray{src, dst, filter});
  319. }
  320. }
  321. TEST_F(CUDA, CONV_CONFIG_COMBINATIONS) {
  322. auto eps_getter = [](bool f16, int stage, const char *name) -> float {
  323. if (f16) {
  324. return stage == 2 ? 0.5 : 0.2;
  325. }
  326. if (strstr(name, "WINOGRAD_NONFUSED"))
  327. return 0.3;
  328. return 1e-3;
  329. };
  330. convolution::test_conv_config_combinations(2, handle_cuda(), false, true,
  331. true, eps_getter, true);
  332. convolution::test_conv_config_combinations(3, handle_cuda(), false, true,
  333. true, eps_getter, true);
  334. convolution::test_conv_config_combinations(5, handle_cuda(), false, true,
  335. true, eps_getter, true);
  336. }
  337. TEST_F(CUDA, CONVOLUTION_BACKWARD_DATA_1) {
  338. if (cuda::is_compute_capability_required(7, 0))
  339. return;
  340. using namespace convolution;
  341. Checker<ConvolutionBackwardData> checker(handle_cuda());
  342. checker.set_before_exec_callback(AlgoChecker<ConvolutionBackwardData>(
  343. "CUDNN_CONVOLUTION_BWD_DATA_ALGO_1" CUDNN_VERSION_STRING));
  344. NormalRNG default_rng;
  345. TensorShape s_filter = TensorShape{8, 8, 2, 2},
  346. s_src = TensorShape{2, 8, 18, 18};
  347. float scale = 1.0f / sqrt(s_filter[0] * s_filter[2] * s_filter[3]);
  348. UniformFloatRNG rng(scale, 2 * scale);
  349. auto src = TensorLayout(s_src, dtype::Float16());
  350. auto filter = TensorLayout(s_filter, dtype::Float16());
  351. TensorLayout dst;
  352. param::Convolution param;
  353. param.pad_h = param.pad_w = 2;
  354. param.stride_h = param.stride_w = 2;
  355. {
  356. auto opr = handle_cuda()->create_operator<Convolution>();
  357. opr->param() = param;
  358. opr->deduce_layout(src, filter, dst);
  359. }
  360. src.dtype = dst.dtype = filter.dtype = dtype::Float16();
  361. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  362. checker.set_rng(0, &rng)
  363. .set_rng(1, &rng)
  364. .set_epsilon(0.2)
  365. .set_param(param)
  366. .exec(TensorLayoutArray{filter, dst, src});
  367. }
  368. #if MEGDNN_WITH_BENCHMARK
  369. TEST_F(CUDA, CONV_FWD_BENCHMARK) {
  370. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  371. size_t SH = 1, size_t SW = 1, size_t FH = 1, size_t FW = 1,
  372. size_t PH = 0, size_t PW = 0, bool fp16io_c32 = false) {
  373. auto benchmarker = Benchmarker<ConvolutionForward>(handle_cuda());
  374. benchmarker.set_dtype(0, dtype::Float16())
  375. .set_dtype(1, dtype::Float16())
  376. .set_dtype(2, dtype::Float16());
  377. ConvolutionForward::Param param;
  378. param.stride_h = SH;
  379. param.stride_w = SW;
  380. param.pad_h = PH;
  381. param.pad_w = PW;
  382. if (fp16io_c32) {
  383. param.compute_mode =
  384. ConvolutionForward::Param::ComputeMode::FLOAT32;
  385. }
  386. benchmarker.set_param(param);
  387. std::unique_ptr<OprProxy<ConvolutionForward>> proxy{
  388. new OprProxy<ConvolutionForward>{true}};
  389. benchmarker.set_proxy(proxy);
  390. size_t OH = (IH - FH + 2 * PH) / SH + 1;
  391. size_t OW = (IW - FW + 2 * PW) / SW + 1;
  392. auto time = benchmarker.execs(
  393. {{N, IC, IH, IW}, {OC, IC, FH, FW}, {N, OC, OH, OW}});
  394. time /= 1000.0 * 10.0;
  395. auto flo = (double)N * OC * IC * OH * OW * FH * FW * 2;
  396. auto flops = flo / time / 1e12;
  397. printf("comp_type %s: ", fp16io_c32 ? "32" : "16");
  398. printf("%.3fG FLO, flops %.3fTFLOPS\n", flo / 1e9, flops);
  399. };
  400. run(32, 512, 256, 56, 56, 1, 1, 1, 1, 0, 0, false);
  401. run(32, 512, 256, 56, 56, 1, 1, 1, 1, 0, 0, true);
  402. }
  403. TEST_F(CUDA, CONVOLUTION_FWD_BENCHMARK) {
  404. CUBenchmarker<ConvolutionForward> bench{handle_cuda()};
  405. std::unique_ptr<OprProxy<ConvolutionForward>> proxy{
  406. new OprProxy<ConvolutionForward>{true}};
  407. size_t RUNS = 10;
  408. bench.set_proxy(proxy).set_times(RUNS);
  409. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  410. size_t FH, size_t SH, size_t PH) {
  411. bench.set_dtype(0, dtype::Float32())
  412. .set_dtype(1, dtype::Float32())
  413. .set_dtype(2, dtype::Float32());
  414. param::Convolution param;
  415. param.stride_h = param.stride_w = SH;
  416. param.pad_h = param.pad_w = PH;
  417. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  418. bench.set_param(param);
  419. bench.proxy()->target_execution_policy.algo.reset();
  420. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  421. filter{{OC, IC, FH, FH}, dtype::Float32()};
  422. TensorLayout dst;
  423. {
  424. auto&& opr = handle_cuda()->create_operator<Convolution>();
  425. opr->param() = param;
  426. opr->deduce_layout(src, filter, dst);
  427. }
  428. auto time_ms_fp32 = bench.execl({src, filter, dst}) / RUNS;
  429. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  430. bench.proxy()->target_execution_policy.algo.reset();
  431. bench.set_dtype(0, dtype::Float16())
  432. .set_dtype(1, dtype::Float16())
  433. .set_dtype(2, dtype::Float16());
  434. auto time_ms_true_fp16 = bench.execl({src, filter, dst}) / RUNS;
  435. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  436. bench.proxy()->target_execution_policy.algo.reset();
  437. bench.set_param(param);
  438. auto time_ms_pseudo_fp16 = bench.execl({src, filter, dst}) / RUNS;
  439. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  440. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  441. filter.to_string().c_str(), dst.to_string().c_str());
  442. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  443. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  444. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  445. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  446. (flo / (time_ms_pseudo_fp16 * 1e9)));
  447. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  448. time_ms_fp32 / time_ms_true_fp16,
  449. time_ms_pseudo_fp16 / time_ms_true_fp16);
  450. };
  451. run(32, 64, 3, 224, 224, 7, 2, 3);
  452. run(32, 128, 128, 28, 28, 3, 1, 1);
  453. run(32, 256, 256, 14, 14, 3, 1, 1);
  454. run(32, 512, 512, 7, 7, 3, 1, 1);
  455. run(32, 64, 64, 56, 56, 3, 1, 1);
  456. run(32, 512, 256, 56, 56, 1, 2, 0);
  457. run(32, 1024, 512, 28, 28, 1, 2, 0);
  458. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  459. run(32, 512, 128, 28, 28, 1, 1, 0);
  460. run(32, 128, 512, 28, 28, 1, 1, 0);
  461. run(32, 1024, 256, 14, 14, 1, 1, 0);
  462. run(32, 256, 1024, 14, 14, 1, 1, 0);
  463. run(32, 2048, 512, 7, 7, 1, 1, 0);
  464. run(32, 512, 2048, 7, 7, 1, 1, 0);
  465. run(32, 256, 64, 56, 56, 1, 1, 0);
  466. run(32, 64, 256, 56, 56, 1, 1, 0);
  467. run(32, 128, 256, 56, 56, 1, 2, 0);
  468. run(32, 256, 512, 28, 28, 1, 2, 0);
  469. run(32, 512, 1024, 14, 14, 1, 2, 0);
  470. run(32, 64, 64, 56, 56, 1, 1, 0);
  471. }
  472. TEST_F(CUDA, CONVOLUTION_BWD_DATA_BENCHMARK) {
  473. CUBenchmarker<ConvolutionBackwardData> bench{handle_cuda()};
  474. std::unique_ptr<OprProxy<ConvolutionBackwardData>> proxy{
  475. new OprProxy<ConvolutionBackwardData>{true}};
  476. size_t RUNS = 10;
  477. bench.set_proxy(proxy).set_times(RUNS);
  478. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  479. size_t FH, size_t SH, size_t PH) {
  480. bench.set_dtype(0, dtype::Float32())
  481. .set_dtype(1, dtype::Float32())
  482. .set_dtype(2, dtype::Float32());
  483. param::Convolution param;
  484. param.stride_h = param.stride_w = SH;
  485. param.pad_h = param.pad_w = PH;
  486. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  487. bench.set_param(param);
  488. bench.proxy()->target_execution_policy.algo.reset();
  489. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  490. filter{{OC, IC, FH, FH}, dtype::Float32()};
  491. TensorLayout dst;
  492. {
  493. auto&& opr = handle_cuda()->create_operator<Convolution>();
  494. opr->param() = param;
  495. opr->deduce_layout(src, filter, dst);
  496. }
  497. auto time_ms_fp32 = bench.execl({filter, dst, src}) / RUNS;
  498. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  499. bench.proxy()->target_execution_policy.algo.reset();
  500. bench.set_dtype(0, dtype::Float16())
  501. .set_dtype(1, dtype::Float16())
  502. .set_dtype(2, dtype::Float16());
  503. auto time_ms_true_fp16 = bench.execl({filter, dst, src}) / RUNS;
  504. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  505. bench.proxy()->target_execution_policy.algo.reset();
  506. bench.set_param(param);
  507. auto time_ms_pseudo_fp16 = bench.execl({filter, dst, src}) / RUNS;
  508. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  509. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  510. filter.to_string().c_str(), dst.to_string().c_str());
  511. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  512. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  513. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  514. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  515. (flo / (time_ms_pseudo_fp16 * 1e9)));
  516. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  517. time_ms_fp32 / time_ms_true_fp16,
  518. time_ms_pseudo_fp16 / time_ms_true_fp16);
  519. };
  520. run(32, 64, 3, 224, 224, 7, 2, 3);
  521. run(32, 128, 128, 28, 28, 3, 1, 1);
  522. run(32, 256, 256, 14, 14, 3, 1, 1);
  523. run(32, 512, 512, 7, 7, 3, 1, 1);
  524. run(32, 64, 64, 56, 56, 3, 1, 1);
  525. run(32, 512, 256, 56, 56, 1, 2, 0);
  526. run(32, 1024, 512, 28, 28, 1, 2, 0);
  527. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  528. run(32, 512, 128, 28, 28, 1, 1, 0);
  529. run(32, 128, 512, 28, 28, 1, 1, 0);
  530. run(32, 1024, 256, 14, 14, 1, 1, 0);
  531. run(32, 256, 1024, 14, 14, 1, 1, 0);
  532. run(32, 2048, 512, 7, 7, 1, 1, 0);
  533. run(32, 512, 2048, 7, 7, 1, 1, 0);
  534. run(32, 256, 64, 56, 56, 1, 1, 0);
  535. run(32, 64, 256, 56, 56, 1, 1, 0);
  536. run(32, 128, 256, 56, 56, 1, 2, 0);
  537. run(32, 256, 512, 28, 28, 1, 2, 0);
  538. run(32, 512, 1024, 14, 14, 1, 2, 0);
  539. run(32, 64, 64, 56, 56, 1, 1, 0);
  540. }
  541. TEST_F(CUDA, BENCHMARK_CONVOLUTION_BWD_DATA_BF16) {
  542. CUBenchmarker<ConvolutionBackwardData> bench{handle_cuda()};
  543. std::unique_ptr<OprProxy<ConvolutionBackwardData>> proxy{
  544. new OprProxy<ConvolutionBackwardData>{true}};
  545. size_t RUNS = 10;
  546. bench.set_proxy(proxy).set_times(RUNS);
  547. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  548. size_t FH, size_t SH, size_t PH) {
  549. bench.set_dtype(0, dtype::BFloat16())
  550. .set_dtype(1, dtype::BFloat16())
  551. .set_dtype(2, dtype::BFloat16());
  552. param::Convolution param;
  553. param.stride_h = param.stride_w = SH;
  554. param.pad_h = param.pad_w = PH;
  555. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  556. bench.set_param(param);
  557. bench.proxy()->target_execution_policy = {};
  558. TensorLayout src{{N, IC, IH, IW}, dtype::BFloat16()},
  559. filter{{OC, IC, FH, FH}, dtype::BFloat16()};
  560. TensorLayout dst;
  561. {
  562. auto&& opr = handle_cuda()->create_operator<Convolution>();
  563. opr->param() = param;
  564. opr->deduce_layout(src, filter, dst);
  565. }
  566. auto used = bench.execl({filter, dst, src}) / RUNS;
  567. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  568. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  569. filter.to_string().c_str(), dst.to_string().c_str());
  570. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\n", used,
  571. (flo / (used * 1e9)));
  572. };
  573. run(32, 64, 3, 224, 224, 7, 2, 3);
  574. run(32, 128, 128, 28, 28, 3, 1, 1);
  575. run(32, 256, 256, 14, 14, 3, 1, 1);
  576. run(32, 512, 512, 7, 7, 3, 1, 1);
  577. run(32, 64, 64, 56, 56, 3, 1, 1);
  578. run(32, 512, 256, 56, 56, 1, 2, 0);
  579. run(32, 1024, 512, 28, 28, 1, 2, 0);
  580. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  581. run(32, 512, 128, 28, 28, 1, 1, 0);
  582. run(32, 128, 512, 28, 28, 1, 1, 0);
  583. run(32, 1024, 256, 14, 14, 1, 1, 0);
  584. run(32, 256, 1024, 14, 14, 1, 1, 0);
  585. run(32, 2048, 512, 7, 7, 1, 1, 0);
  586. run(32, 512, 2048, 7, 7, 1, 1, 0);
  587. run(32, 256, 64, 56, 56, 1, 1, 0);
  588. run(32, 64, 256, 56, 56, 1, 1, 0);
  589. run(32, 128, 256, 56, 56, 1, 2, 0);
  590. run(32, 256, 512, 28, 28, 1, 2, 0);
  591. run(32, 512, 1024, 14, 14, 1, 2, 0);
  592. run(32, 64, 64, 56, 56, 1, 1, 0);
  593. }
  594. TEST_F(CUDA, CONVOLUTION_BWD_FILTER_BENCHMARK) {
  595. CUBenchmarker<ConvolutionBackwardFilter> bench{handle_cuda()};
  596. std::unique_ptr<OprProxy<ConvolutionBackwardFilter>> proxy{
  597. new OprProxy<ConvolutionBackwardFilter>{true}};
  598. size_t RUNS = 10;
  599. bench.set_proxy(proxy).set_times(RUNS);
  600. auto run = [&](size_t N, size_t OC, size_t IC, size_t IH, size_t IW,
  601. size_t FH, size_t SH, size_t PH) {
  602. bench.set_dtype(0, dtype::Float32())
  603. .set_dtype(1, dtype::Float32())
  604. .set_dtype(2, dtype::Float32());
  605. param::Convolution param;
  606. param.stride_h = param.stride_w = SH;
  607. param.pad_h = param.pad_w = PH;
  608. param.compute_mode = param::Convolution::ComputeMode::DEFAULT;
  609. bench.set_param(param);
  610. bench.proxy()->target_execution_policy.algo.reset();
  611. TensorLayout src{{N, IC, IH, IW}, dtype::Float32()},
  612. filter{{OC, IC, FH, FH}, dtype::Float32()};
  613. TensorLayout dst;
  614. {
  615. auto&& opr = handle_cuda()->create_operator<Convolution>();
  616. opr->param() = param;
  617. opr->deduce_layout(src, filter, dst);
  618. }
  619. auto time_ms_fp32 = bench.execl({src, dst, filter}) / RUNS;
  620. src.dtype = filter.dtype = dst.dtype = dtype::Float16();
  621. bench.proxy()->target_execution_policy.algo.reset();
  622. bench.set_dtype(0, dtype::Float16())
  623. .set_dtype(1, dtype::Float16())
  624. .set_dtype(2, dtype::Float16());
  625. auto time_ms_true_fp16 = bench.execl({src, dst, filter}) / RUNS;
  626. param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  627. bench.proxy()->target_execution_policy.algo.reset();
  628. bench.set_param(param);
  629. auto time_ms_pseudo_fp16 = bench.execl({src, dst, filter}) / RUNS;
  630. float flo = 2.0 * N * OC * IC * dst[2] * dst[3] * FH * FH;
  631. printf("inp=%s, kern=%s, dst=%s ", src.to_string().c_str(),
  632. filter.to_string().c_str(), dst.to_string().c_str());
  633. printf("time_fp32=%.2fms, flops=%.3fTFLOPS\ntime_true_fp16=%.2fms, "
  634. "flops=%.3fTFLOPS\ntime_pseudo_fp16=%.2fms, flops=%.3fFLOPS\n",
  635. time_ms_fp32, (flo / (time_ms_fp32 * 1e9)), time_ms_true_fp16,
  636. (flo / (time_ms_true_fp16 * 1e9)), time_ms_pseudo_fp16,
  637. (flo / (time_ms_pseudo_fp16 * 1e9)));
  638. printf("speedup (true_fp16/fp32)=%.2f, (true_fp16/pseudo_fp16)=%.2f\n",
  639. time_ms_fp32 / time_ms_true_fp16,
  640. time_ms_pseudo_fp16 / time_ms_true_fp16);
  641. };
  642. run(32, 64, 3, 224, 224, 7, 2, 3);
  643. run(32, 128, 128, 28, 28, 3, 1, 1);
  644. run(32, 256, 256, 14, 14, 3, 1, 1);
  645. run(32, 512, 512, 7, 7, 3, 1, 1);
  646. run(32, 64, 64, 56, 56, 3, 1, 1);
  647. run(32, 512, 256, 56, 56, 1, 2, 0);
  648. run(32, 1024, 512, 28, 28, 1, 2, 0);
  649. run(32, 2048, 1024, 14, 14, 1, 2, 0);
  650. run(32, 512, 128, 28, 28, 1, 1, 0);
  651. run(32, 128, 512, 28, 28, 1, 1, 0);
  652. run(32, 1024, 256, 14, 14, 1, 1, 0);
  653. run(32, 256, 1024, 14, 14, 1, 1, 0);
  654. run(32, 2048, 512, 7, 7, 1, 1, 0);
  655. run(32, 512, 2048, 7, 7, 1, 1, 0);
  656. run(32, 256, 64, 56, 56, 1, 1, 0);
  657. run(32, 64, 256, 56, 56, 1, 1, 0);
  658. run(32, 128, 256, 56, 56, 1, 2, 0);
  659. run(32, 256, 512, 28, 28, 1, 2, 0);
  660. run(32, 512, 1024, 14, 14, 1, 2, 0);
  661. run(32, 64, 64, 56, 56, 1, 1, 0);
  662. }
  663. #endif
  664. #undef CUDNN_VERSION_STRING
  665. #undef V
  666. #undef V1
  667. } // namespace test
  668. } // namespace megdnn
  669. // vim: syntax=cpp.doxygen

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