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

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

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