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

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

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