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

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

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