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

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

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