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

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

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