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.

conv_bias.cpp 61 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /**
  2. * \file dnn/test/cuda/conv_bias.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 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/dtype.h"
  12. #include "test/cuda/fixture.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "megdnn/oprs.h"
  15. #include "src/cuda/handle.h"
  16. #include "test/common/benchmarker.h"
  17. #include "test/common/checker.h"
  18. #include "test/common/conv_bias.h"
  19. #include "test/common/rng.h"
  20. #include "test/common/tensor.h"
  21. #include "test/common/workspace_wrapper.h"
  22. #include "test/cuda/utils.h"
  23. using namespace megdnn;
  24. using namespace test;
  25. using namespace conv_bias;
  26. namespace {
  27. #if CUDA_VERSION >= 10000
  28. void test_conv_bias_forward_wmma_int4_nchw8(Handle* handle_cuda, size_t fh) {
  29. require_compute_capability(7, 5);
  30. using namespace conv_bias;
  31. Checker<ConvBiasForward> checker(handle_cuda);
  32. UniformIntRNG int_rng{0, 8};
  33. ConvBias::Param param;
  34. param.format = ConvBias::Param::Format::NCHW8;
  35. using NonlineMode = ConvBias::Param::NonlineMode;
  36. for (NonlineMode mode : {NonlineMode::RELU}) {
  37. for (size_t batch : {1}) {
  38. for (size_t ic : {128, 32}) {
  39. for (size_t oc : {32}) {
  40. for (int ph : {static_cast<int>(fh / 2), 0}) {
  41. for (size_t ih : {8, 9, 13, 15, 16}) {
  42. for (size_t iw : {8, 16, 24, 32, 40}) {
  43. param.nonlineMode = mode;
  44. param.stride_h = param.stride_w = 1;
  45. param.pad_h = param.pad_w = ph;
  46. checker.set_dtype(0,
  47. dtype::Quantized4Asymm(
  48. 1.3f, (uint8_t)(1)))
  49. .set_dtype(1,
  50. dtype::Quantized4Asymm(
  51. 1.3f, (uint8_t)(2)))
  52. .set_dtype(2, dtype::QuantizedS32(1.3f *
  53. 1.3f))
  54. .set_dtype(4, dtype::QuantizedS32(1.3f *
  55. 1.3f))
  56. .set_rng(0, &int_rng)
  57. .set_rng(1, &int_rng)
  58. .set_rng(2, &int_rng)
  59. .set_param(param);
  60. if (!ph)
  61. iw += 2 * (fh / 2);
  62. size_t oh = infer_conv_shape(ih, fh, 1, ph);
  63. size_t ow = infer_conv_shape(iw, fh, 1, ph);
  64. if (ow % 8 != 0)
  65. continue;
  66. checker.execs({{batch, ic / 8, ih, iw, 8},
  67. {oc, ic / 8, fh, fh, 8},
  68. {1, oc / 8, 1, 1, 8},
  69. {},
  70. {}});
  71. checker.execs({{batch, ic / 8, ih, iw, 8},
  72. {oc, ic / 8, fh, fh, 8},
  73. {batch, oc / 8, oh, ow, 8},
  74. {},
  75. {}});
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. #endif
  85. } // namespace
  86. #if CUDNN_VERSION >= 7400
  87. TEST_F(CUDA, CONV_BIAS_FORWARD_F32) {
  88. using namespace conv_bias;
  89. std::vector<TestArg> args = get_args();
  90. Checker<ConvBiasForward> checker(handle_cuda());
  91. NormalRNG default_rng;
  92. for (auto&& arg : args) {
  93. checker.set_dtype(0, dtype::Float32())
  94. .set_dtype(1, dtype::Float32())
  95. .set_dtype(2, dtype::Float32())
  96. .set_rng(0, &default_rng)
  97. .set_rng(1, &default_rng)
  98. .set_rng(2, &default_rng)
  99. .set_epsilon(1e-3)
  100. .set_param(arg.param)
  101. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  102. }
  103. }
  104. TEST_F(CUDA, CONV_BIAS_FORWARD_BF16) {
  105. using namespace conv_bias;
  106. std::vector<TestArg> args = get_args();
  107. Checker<ConvBiasForward> checker(handle_cuda());
  108. checker.set_before_exec_callback(
  109. AlgoChecker<ConvBiasForward>(ExecutionPolicyAlgoName{
  110. "CONVBIAS_BFLOAT16", {{"MATMUL", {}}}}));
  111. NormalRNG default_rng;
  112. for (auto&& arg : args) {
  113. arg.param.compute_mode = param::Convolution::ComputeMode::FLOAT32;
  114. checker.set_dtype(0, dtype::BFloat16())
  115. .set_dtype(1, dtype::BFloat16())
  116. .set_dtype(2, dtype::BFloat16())
  117. .set_dtype(3, dtype::BFloat16())
  118. .set_dtype(4, dtype::BFloat16())
  119. .set_rng(0, &default_rng)
  120. .set_rng(1, &default_rng)
  121. .set_rng(2, &default_rng)
  122. .set_epsilon(2e-2)
  123. .set_param(arg.param)
  124. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  125. }
  126. }
  127. TEST_F(CUDA, CONV_BIAS_FORWARD_QS8) {
  128. require_compute_capability(6, 1);
  129. UniformIntRNG int_rng{-50, 50};
  130. Checker<ConvBiasForward> checker(handle_cuda());
  131. ConvBias::Param param;
  132. param.format = ConvBias::Param::Format::NHWC;
  133. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  134. {
  135. auto src_shape = TensorShape{20, 224, 224, 4};
  136. auto filter_shape = TensorShape{24, 1, 1, 4};
  137. auto bias_shape = TensorShape{1, 1, 1, 24};
  138. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  139. .set_dtype(1, dtype::QuantizedS8(2.5f))
  140. .set_dtype(2, dtype::QuantizedS32(6.25f))
  141. .set_dtype(4, dtype::QuantizedS8(60.25f))
  142. .set_rng(0, &int_rng)
  143. .set_rng(1, &int_rng)
  144. .set_rng(2, &int_rng)
  145. .set_param(param)
  146. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  147. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  148. .set_dtype(1, dtype::QuantizedS8(2.5f))
  149. .set_dtype(2, dtype::QuantizedS32(6.25f))
  150. .set_dtype(4, dtype::QuantizedS8(40.25f))
  151. .set_rng(0, &int_rng)
  152. .set_rng(1, &int_rng)
  153. .set_rng(2, &int_rng)
  154. .set_param(param)
  155. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  156. }
  157. {
  158. auto src_shape = TensorShape{20, 224, 224, 4};
  159. auto filter_shape = TensorShape{24, 1, 1, 4};
  160. auto bias_shape = TensorShape{1, 1, 1, 24};
  161. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  162. .set_dtype(1, dtype::QuantizedS8(2.5f))
  163. .set_dtype(2, dtype::QuantizedS32(6.25f))
  164. .set_dtype(4, dtype::QuantizedS8(60.25f))
  165. .set_rng(0, &int_rng)
  166. .set_rng(1, &int_rng)
  167. .set_rng(2, &int_rng)
  168. .set_param(param)
  169. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  170. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  171. .set_dtype(1, dtype::QuantizedS8(2.5f))
  172. .set_dtype(2, dtype::QuantizedS32(6.25f))
  173. .set_dtype(4, dtype::QuantizedS8(40.25f))
  174. .set_rng(0, &int_rng)
  175. .set_rng(1, &int_rng)
  176. .set_rng(2, &int_rng)
  177. .set_param(param)
  178. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  179. }
  180. {
  181. param.sparse = ConvBias::Param::Sparse::GROUP;
  182. auto src_shape = TensorShape{20, 224, 224, 16};
  183. auto filter_shape = TensorShape{4, 4, 1, 1, 4};
  184. auto bias_shape = TensorShape{1, 1, 1, 16};
  185. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  186. .set_dtype(1, dtype::QuantizedS8(2.5f))
  187. .set_dtype(2, dtype::QuantizedS32(6.25f))
  188. .set_dtype(4, dtype::QuantizedS8(60.25f))
  189. .set_rng(0, &int_rng)
  190. .set_rng(1, &int_rng)
  191. .set_rng(2, &int_rng)
  192. .set_param(param)
  193. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  194. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  195. .set_dtype(1, dtype::QuantizedS8(2.5f))
  196. .set_dtype(2, dtype::QuantizedS32(6.25f))
  197. .set_dtype(4, dtype::QuantizedS8(40.25f))
  198. .set_rng(0, &int_rng)
  199. .set_rng(1, &int_rng)
  200. .set_rng(2, &int_rng)
  201. .set_param(param)
  202. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  203. }
  204. }
  205. TEST_F(CUDA, CONV_BIAS_FORWARD_FLOAT16) {
  206. require_compute_capability(6, 1);
  207. Checker<ConvBiasForward> checker(handle_cuda());
  208. ConvBias::Param param;
  209. param.format = ConvBias::Param::Format::NHWC;
  210. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  211. checker.set_epsilon(2e-2)
  212. .set_dtype(0, dtype::Float16())
  213. .set_dtype(1, dtype::Float16())
  214. .set_dtype(2, dtype::Float16())
  215. .set_dtype(3, dtype::Float16())
  216. .set_dtype(4, dtype::Float16());
  217. {
  218. auto src_shape = TensorShape{20, 224, 224, 4};
  219. auto filter_shape = TensorShape{24, 1, 1, 4};
  220. auto bias_shape = TensorShape{1, 1, 1, 24};
  221. checker.set_param(param).execs(
  222. {src_shape, filter_shape, bias_shape, {}, {}});
  223. param.compute_mode = ConvBias::Param::ComputeMode::FLOAT32;
  224. checker.set_param(param).execs(
  225. {src_shape, filter_shape, bias_shape, {}, {}});
  226. }
  227. {
  228. param.sparse = ConvBias::Param::Sparse::GROUP;
  229. auto src_shape = TensorShape{20, 224, 224, 16};
  230. auto filter_shape = TensorShape{4, 4, 1, 1, 4};
  231. auto bias_shape = TensorShape{1, 1, 1, 16};
  232. checker.set_param(param).execs(
  233. {src_shape, filter_shape, bias_shape, {}, {}});
  234. }
  235. }
  236. TEST_F(CUDA, CONV_BIAS_NCHW_QS8) {
  237. //! not support NonlineMode::SIGMOID and NonlineMode::H_SWISH
  238. require_compute_capability(6, 1);
  239. Checker<ConvBiasForward> checker(handle_cuda());
  240. UniformIntRNG int_rng{-128, 127};
  241. using NonlineMode = ConvBias::Param::NonlineMode;
  242. ConvBias::Param param;
  243. param.format = ConvBias::Param::Format::NCHW;
  244. checker.set_dtype(0, dtype::QuantizedS8(1.f))
  245. .set_dtype(1, dtype::QuantizedS8(1.f))
  246. .set_dtype(2, dtype::QuantizedS32(1.f))
  247. .set_dtype(3, dtype::QuantizedS8(1.f))
  248. .set_dtype(4, dtype::QuantizedS8(1.f))
  249. .set_rng(0, &int_rng)
  250. .set_rng(1, &int_rng)
  251. .set_rng(2, &int_rng)
  252. .set_rng(3, &int_rng);
  253. for (NonlineMode mode : {NonlineMode::RELU,
  254. NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  255. for (size_t g : {1, 2}) {
  256. for (size_t b : {2}) {
  257. for (size_t ic : {6, 16}) {
  258. for (size_t oc : {4}) {
  259. for (size_t fh : {1, 3}) {
  260. for (int ph : {static_cast<int>(fh / 2)}) {
  261. for (int sh : {1, 2}) {
  262. size_t ih = 16, iw = 16;
  263. param.nonlineMode = mode;
  264. param.stride_h = param.stride_w = sh;
  265. param.pad_h = param.pad_w = ph;
  266. param.sparse =
  267. ConvBias::Param::Sparse::DENSE;
  268. checker.set_param(param)
  269. .execs({{b, ic / 2, ih, iw},
  270. {oc, ic / 2, fh, fh},
  271. {1, oc, 1, 1},
  272. {},
  273. {}});
  274. param.sparse =
  275. ConvBias::Param::Sparse::GROUP;
  276. checker.set_param(param)
  277. .execs({{b, ic, ih, iw},
  278. {g, oc/g, ic/g, fh, fh},
  279. {1, oc, 1, 1},
  280. {},
  281. {}});
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. for (NonlineMode mode : {NonlineMode::RELU,
  291. NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  292. for (size_t g : {13}) {
  293. for (size_t b : {1, 2}) {
  294. for (size_t ic : {13}) {
  295. for (size_t oc : {13}) {
  296. for (size_t fh : {1, 3}) {
  297. for (int ph : {static_cast<int>(fh / 2)}) {
  298. for (int sh : {1, 2}) {
  299. size_t ih = 16, iw = 16;
  300. param.nonlineMode = mode;
  301. param.stride_h = param.stride_w = sh;
  302. param.pad_h = param.pad_w = ph;
  303. param.sparse =
  304. ConvBias::Param::Sparse::GROUP;
  305. checker.set_param(param)
  306. .execs({{b, ic, ih, iw},
  307. {g, oc/g, ic/g, fh, fh},
  308. {1, oc, 1, 1},
  309. {},
  310. {}});
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. {
  320. size_t ih = 16, iw = 16, b = 1, oc = 14, ic = 14;
  321. size_t fh = 3, sh = 1, ph = 1;
  322. param.nonlineMode = NonlineMode::IDENTITY;
  323. param.stride_h = param.stride_w = sh;
  324. param.pad_h = param.pad_w = ph;
  325. param.sparse = ConvBias::Param::Sparse::DENSE;
  326. checker.set_param(param).execs(
  327. {{b, ic, ih, iw}, {oc, ic, fh, fh}, {}, {}, {}});
  328. }
  329. }
  330. TEST_F(CUDA, CONV_BIAS_NCHW_QS8_FUSE_Z) {
  331. require_compute_capability(6, 1);
  332. Checker<ConvBiasForward> checker(handle_cuda());
  333. UniformIntRNG int_rng{-128, 127};
  334. using NonlineMode = ConvBias::Param::NonlineMode;
  335. ConvBias::Param param;
  336. param.format = ConvBias::Param::Format::NCHW;
  337. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  338. .set_dtype(1, dtype::QuantizedS8(2.5f))
  339. .set_dtype(2, dtype::QuantizedS32(6.25f))
  340. .set_dtype(3, dtype::QuantizedS8(0.25f))
  341. .set_dtype(4, dtype::QuantizedS8(0.25f))
  342. .set_rng(0, &int_rng)
  343. .set_rng(1, &int_rng)
  344. .set_rng(2, &int_rng)
  345. .set_rng(3, &int_rng);
  346. for (NonlineMode mode :
  347. {NonlineMode::RELU, NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  348. for (size_t b : {2}) {
  349. for (size_t ic : {6, 16}) {
  350. for (size_t oc : {4}) {
  351. for (size_t fh : {1, 3}) {
  352. for (int ph : {static_cast<int>(fh / 2)}) {
  353. for (int sh : {1, 2}) {
  354. size_t ih = 16, iw = 16;
  355. param.nonlineMode = mode;
  356. param.stride_h = param.stride_w = sh;
  357. param.pad_h = param.pad_w = ph;
  358. param.sparse = ConvBias::Param::Sparse::DENSE;
  359. const size_t oh = (ih - fh + 2 * ph) / sh + 1;
  360. const size_t ow = (iw - fh + 2 * ph) / sh + 1;
  361. checker.set_param(param).execs(
  362. {{b, ic, ih, iw},
  363. {oc, ic, fh, fh},
  364. {1, oc, 1, 1},
  365. {b, oc, oh, ow},
  366. {}});
  367. }
  368. }
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. #if MEGDNN_WITH_BENCHMARK
  376. TEST_F(CUDA, BENCHMARK_CONV_BIAS_NCHW4_INT8) {
  377. require_compute_capability(6, 1);
  378. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  379. bencher.set_display(false);
  380. ConvBias::Param param_nchw;
  381. param_nchw.format = ConvBias::Param::Format::NCHW;
  382. ConvBias::Param param_nchw4;
  383. param_nchw4.format = ConvBias::Param::Format::NCHW4;
  384. auto i8_min = std::numeric_limits<int8_t>().min();
  385. auto i8_max = std::numeric_limits<int8_t>().max();
  386. UniformIntRNG int_rng{i8_min, i8_max};
  387. param_nchw.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  388. auto run_bench = [&](size_t b, size_t ci, size_t hi, size_t wi,
  389. size_t co, size_t fh, size_t fw, size_t sh,
  390. size_t sw, size_t nr_times) {
  391. param_nchw.pad_h = fh / 2;
  392. param_nchw.pad_w = fw / 2;
  393. param_nchw.stride_h = sh;
  394. param_nchw.stride_w = sw;
  395. param_nchw4.pad_h = fh / 2;
  396. param_nchw4.pad_w = fh / 2;
  397. param_nchw4.stride_h = sh;
  398. param_nchw4.stride_w = sw;
  399. bencher.set_times(nr_times)
  400. .set_dtype(0, dtype::QuantizedS8(2.5f))
  401. .set_dtype(1, dtype::QuantizedS8(2.5f))
  402. .set_dtype(2, dtype::QuantizedS32(6.25f))
  403. .set_dtype(4, dtype::QuantizedS8(0.35f))
  404. .set_rng(0, &int_rng)
  405. .set_rng(1, &int_rng)
  406. .set_rng(2, &int_rng);
  407. bencher.set_param(param_nchw);
  408. size_t ho = infer_conv_shape(hi, fh, sh, param_nchw.pad_h);
  409. size_t wo = infer_conv_shape(wi, fw, sw, param_nchw.pad_w);
  410. TensorShape inp{b, ci, hi, wi}, kern{co, ci, fh, fw},
  411. out{b, co, ho, wo};
  412. auto time_in_ms = bencher.execs(
  413. {inp, kern, {1, co, 1, 1}, {}, out}) / nr_times;
  414. auto ops_nchw = 2.0 * b * co * ho * wo * ci * fh * fw /
  415. (time_in_ms * 1e-3) * 1e-12;
  416. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  417. "(NCHW)\n",
  418. inp.to_string().c_str(), kern.to_string().c_str(),
  419. out.to_string().c_str(), time_in_ms, ops_nchw);
  420. bencher.set_param(param_nchw4);
  421. decltype(ops_nchw) ops_nchw4;
  422. {
  423. TensorShape inp{b, ci / 4, hi, wi, 4},
  424. kern{co, ci / 4, fh, fw, 4}, out{b, co / 4, ho, wo, 4};
  425. auto time_in_ms = bencher.execs(
  426. {inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) / nr_times;
  427. ops_nchw4 = 2.0 * b * co * ho * wo * ci * fh * fw /
  428. (time_in_ms * 1e-3) * 1e-12;
  429. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  430. "(NCHW4)\n",
  431. inp.to_string().c_str(), kern.to_string().c_str(),
  432. out.to_string().c_str(), time_in_ms, ops_nchw4);
  433. }
  434. printf("speedup: %.2fx\n", ops_nchw4 / ops_nchw);
  435. };
  436. // resnet-50
  437. // bottleneck-1
  438. // proj
  439. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  440. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  441. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  442. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  443. // bottleneck-2
  444. // proj
  445. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  446. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  447. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  448. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  449. // bottleneck-3
  450. // proj
  451. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  452. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  453. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  454. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  455. // bottleneck-4
  456. // proj
  457. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  458. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  459. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  460. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  461. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  462. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  463. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  464. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  465. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  466. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  467. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  468. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  469. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  470. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  471. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  472. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  473. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  474. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  475. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  476. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  477. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  478. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  479. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  480. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  481. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  482. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  483. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  484. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  485. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  486. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  487. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  488. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  489. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  490. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  491. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  492. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  493. }
  494. #endif
  495. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4) {
  496. require_compute_capability(6, 1);
  497. using namespace conv_bias;
  498. Checker<ConvBiasForward> checker(handle_cuda());
  499. UniformIntRNG int_rng{-5, 5};
  500. ConvBias::Param param;
  501. param.format = ConvBias::Param::Format::NCHW4;
  502. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  503. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  504. .set_dtype(1, dtype::QuantizedS8(0.5f))
  505. .set_dtype(2, dtype::QuantizedS32(0.25f))
  506. .set_dtype(3, dtype::QuantizedS8(0.13f))
  507. .set_dtype(4, dtype::QuantizedS8(0.35f))
  508. .set_rng(0, &int_rng)
  509. .set_rng(1, &int_rng)
  510. .set_rng(2, &int_rng)
  511. .set_rng(3, &int_rng)
  512. .set_param(param);
  513. auto opr = handle_cuda()->create_operator<ConvBias>();
  514. auto run = [&](const TensorShapeArray& shapes) {
  515. opr->param() = param;
  516. TensorLayout dst_layout;
  517. opr->deduce_layout({shapes[0], dtype::Float32()},
  518. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  519. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  520. };
  521. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 1, 1, 1, 4}});
  522. run({{1, 4, 4, 4, 4}, {260, 4, 3, 3, 4}, {1, 65, 1, 1, 4}});
  523. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 6, 1, 1, 4}});
  524. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 6, 1, 1, 4}});
  525. param.sparse = ConvBias::Param::Sparse::GROUP;
  526. checker.set_param(param);
  527. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 4, 1, 1, 4}});
  528. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 24, 1, 1, 4}});
  529. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 6, 1, 1, 4}});
  530. param.pad_h = param.pad_w = 1;
  531. param.stride_h = param.stride_w = 2;
  532. checker.set_param(param);
  533. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 16, 1, 1, 4}});
  534. // case which cudnn not supported
  535. param.sparse = ConvBias::Param::Sparse::DENSE;
  536. param.pad_h = param.pad_w = 1;
  537. param.stride_h = param.stride_w = 1;
  538. checker.set_param(param);
  539. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  540. }
  541. //! FIXME: conv kernel of cudnn for NCHW4_NCHW tensor format causes illegal
  542. //! memory access errors, so we have to disable this test here.
  543. #if 0
  544. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4_NCHW) {
  545. require_compute_capability(6, 1);
  546. using namespace conv_bias;
  547. Checker<ConvBiasForward> checker(handle_cuda());
  548. UniformIntRNG int_rng{-3, 3};
  549. UniformFloatRNG float_rng{-50, 50};
  550. ConvBias::Param param;
  551. param.format = ConvBias::Param::Format::NCHW4_NCHW;
  552. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  553. checker.set_dtype(0, dtype::QuantizedS8(1.9980618f))
  554. .set_dtype(1, dtype::QuantizedS8(1.9980927f))
  555. .set_dtype(2, dtype::Float32())
  556. .set_dtype(3, dtype::Float32())
  557. .set_dtype(4, dtype::Float32())
  558. .set_rng(0, &int_rng)
  559. .set_rng(1, &int_rng)
  560. .set_rng(2, &float_rng)
  561. .set_rng(3, &float_rng)
  562. .set_param(param);
  563. auto opr = handle_cuda()->create_operator<ConvBias>();
  564. auto run = [&](const TensorShapeArray& shapes) {
  565. opr->param() = param;
  566. TensorLayout dst_layout;
  567. opr->deduce_layout({shapes[0], dtype::Float32()},
  568. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  569. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  570. };
  571. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 4, 1, 1}});
  572. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 24, 1, 1}});
  573. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 24, 1, 1}});
  574. param.sparse = ConvBias::Param::Sparse::GROUP;
  575. param.nonlineMode = ConvBias::Param::NonlineMode::RELU;
  576. checker.set_param(param);
  577. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 16, 1, 1}});
  578. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 96, 1, 1}});
  579. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 24, 1, 1}});
  580. param.pad_h = param.pad_w = 1;
  581. param.stride_h = param.stride_w = 2;
  582. checker.set_param(param);
  583. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 64, 1, 1}});
  584. // case which cudnn not supported
  585. param.sparse = ConvBias::Param::Sparse::DENSE;
  586. param.pad_h = param.pad_w = 1;
  587. param.stride_h = param.stride_w = 1;
  588. param.nonlineMode = ConvBias::Param::NonlineMode::H_SWISH;
  589. checker.set_param(param);
  590. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 16, 1, 1}, {}, {}});
  591. }
  592. #endif
  593. #endif
  594. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE) {
  595. Checker<ConvBiasForward> checker(handle_cuda());
  596. std::vector<TestArg> args = get_chanwise_args();
  597. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  598. ConvBiasForward::algo_name<ConvBias::DirectParam>("CHANNEL_WISE",
  599. {})
  600. .c_str()));
  601. for (auto dtype : std::vector<DType>{dtype::Float32(), dtype::Float16()}) {
  602. checker.set_dtype(0, dtype)
  603. .set_dtype(1, dtype)
  604. .set_dtype(2, dtype)
  605. .set_dtype(3, dtype)
  606. .set_dtype(4, dtype);
  607. if (dtype.enumv() == DTypeEnum::Float16)
  608. checker.set_epsilon(2e-2);
  609. for (auto&& arg : args) {
  610. checker.set_param(arg.param).execs(
  611. {arg.src, arg.filter, arg.bias, {}, {}});
  612. }
  613. }
  614. }
  615. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_SMALL) {
  616. Checker<ConvBiasForward> checker(handle_cuda());
  617. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  618. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  619. "CHANNEL_WISE_SMALL", {})
  620. .c_str()));
  621. param::ConvBias cur_param;
  622. using NLMode = param::ConvBias::NonlineMode;
  623. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  624. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  625. for (auto nlmode :
  626. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  627. cur_param.nonlineMode = nlmode;
  628. for (auto dtype : std::vector<DType> {
  629. dtype::Float32(),
  630. #if CUDA_VERSION >= 9000
  631. dtype::Float16()
  632. #endif
  633. }) {
  634. checker.set_dtype(0, dtype)
  635. .set_dtype(1, dtype)
  636. .set_dtype(2, dtype)
  637. .set_dtype(3, dtype)
  638. .set_dtype(4, dtype);
  639. if (dtype.enumv() == DTypeEnum::Float16)
  640. checker.set_epsilon(2e-2);
  641. for (uint32_t s : {1}) {
  642. for (uint32_t f : {1, 3, 5, 7}) {
  643. cur_param.pad_h = cur_param.pad_w = f / 2;
  644. cur_param.stride_h = cur_param.stride_w = s;
  645. checker.set_param(cur_param).execs({{2, 3, 16, 16},
  646. {3, 1, 1, f, f},
  647. {1, 3, 1, 1},
  648. {},
  649. {}});
  650. }
  651. }
  652. cur_param.pad_h = cur_param.pad_w = 1;
  653. cur_param.stride_h = cur_param.stride_w = 1;
  654. checker.set_param(cur_param)
  655. .execs({{2, 3, 3, 16},
  656. {3, 1, 1, 3, 3},
  657. {1, 3, 1, 1},
  658. {},
  659. {}})
  660. .execs({{2, 3, 8, 3},
  661. {3, 1, 1, 3, 3},
  662. {1, 3, 1, 1},
  663. {},
  664. {}});
  665. }
  666. }
  667. }
  668. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_8x8x32) {
  669. require_compute_capability(6, 1);
  670. Checker<ConvBiasForward> checker(handle_cuda());
  671. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  672. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  673. "CHANNEL_WISE_8X8X32", {})
  674. .c_str()));
  675. param::ConvBias cur_param;
  676. using NLMode = param::ConvBias::NonlineMode;
  677. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  678. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  679. cur_param.format = ConvBias::Param::Format::NHWC;
  680. UniformIntRNG rng(-4, 4);
  681. checker.set_dtype(0, dtype::Int8{})
  682. .set_dtype(1, dtype::Int8{})
  683. .set_dtype(2, dtype::Int32{})
  684. .set_dtype(4, dtype::Int32{})
  685. .set_rng(0, &rng)
  686. .set_rng(1, &rng)
  687. .set_rng(2, &rng);
  688. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  689. cur_param.nonlineMode = nlmode;
  690. for (uint32_t s : {1, 2}) {
  691. for (uint32_t f : {1, 3, 5, 7}) {
  692. for (uint32_t g : {4, 8}) {
  693. cur_param.pad_h = cur_param.pad_w = f / 2;
  694. cur_param.stride_h = cur_param.stride_w = s;
  695. checker.set_param(cur_param).execs({{2, 9, 16, g},
  696. {g, 1, f, f, 1},
  697. {1, 1, 1, g},
  698. {},
  699. {}});
  700. }
  701. }
  702. }
  703. }
  704. }
  705. TEST_F(CUDA, CONV_BIAS_FORWARD_CUDNN_CONVOLUTION) {
  706. using namespace conv_bias;
  707. std::vector<TestArg> args = get_args();
  708. Checker<ConvBiasForward> checker(handle_cuda());
  709. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  710. ConvBiasForward::algo_name<ConvBias::DefaultParam>(
  711. "CUDNN:Convolution", {})
  712. .c_str()));
  713. NormalRNG default_rng;
  714. for (auto&& arg : args) {
  715. checker.set_dtype(0, dtype::Float32())
  716. .set_dtype(1, dtype::Float32())
  717. .set_dtype(2, dtype::Float32())
  718. .set_rng(0, &default_rng)
  719. .set_rng(1, &default_rng)
  720. .set_rng(2, &default_rng)
  721. .set_epsilon(1e-3)
  722. .set_param(arg.param)
  723. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  724. }
  725. //! noncontiguous case
  726. {
  727. param::ConvBias param;
  728. param.pad_h = param.pad_w = 1;
  729. checker.set_param(param).execl(TensorLayoutArray{
  730. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  731. {{16, 16, 3, 3}, {144, 9, 3, 1}, dtype::Float32()},
  732. {{}, {}, dtype::Float32()},
  733. {{}, {}, dtype::Float32()},
  734. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  735. });
  736. }
  737. }
  738. TEST_F(CUDA, CONV_BIAS_FORWARD_INPLACE_MATMUL) {
  739. using namespace conv_bias;
  740. std::vector<TestArg> args = get_args();
  741. Checker<ConvBiasForward> checker(handle_cuda());
  742. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  743. ConvBiasForward::algo_name<ConvBias::MatmulParam>("INPLACE_MATMUL",
  744. {})
  745. .c_str()));
  746. param::ConvBias cur_param;
  747. using NLMode = param::ConvBias::NonlineMode;
  748. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  749. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  750. NormalRNG default_rng;
  751. checker.set_dtype(0, dtype::Float32())
  752. .set_dtype(1, dtype::Float32())
  753. .set_dtype(2, dtype::Float32())
  754. .set_rng(0, &default_rng)
  755. .set_rng(1, &default_rng)
  756. .set_rng(2, &default_rng)
  757. .set_epsilon(1e-3);
  758. for (auto nlmode :
  759. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  760. cur_param.nonlineMode = nlmode;
  761. for (uint32_t s : {1}) {
  762. for (uint32_t f : {1, 3, 5, 7}) {
  763. cur_param.pad_h = cur_param.pad_w = f / 2;
  764. cur_param.stride_h = cur_param.stride_w = s;
  765. checker.set_param(cur_param).execs(
  766. {{2, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  767. }
  768. }
  769. cur_param.pad_h = cur_param.pad_w = 1;
  770. cur_param.stride_h = cur_param.stride_w = 1;
  771. checker.set_param(cur_param)
  772. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  773. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  774. }
  775. //! noncontiguous case
  776. {
  777. param::ConvBias param;
  778. param.pad_h = param.pad_w = 1;
  779. checker.set_param(param).execl(TensorLayoutArray{
  780. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  781. {{16, 16, 3, 3}, {144, 9, 3, 1}, dtype::Float32()},
  782. {{}, {}, dtype::Float32()},
  783. {{}, {}, dtype::Float32()},
  784. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  785. });
  786. }
  787. }
  788. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL) {
  789. using namespace conv_bias;
  790. std::vector<TestArg> args = get_args();
  791. Checker<ConvBiasForward> checker(handle_cuda());
  792. checker.set_before_exec_callback(
  793. AlgoChecker<ConvBiasForward>(ExecutionPolicyAlgoName{
  794. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  795. "MATMUL", {})
  796. .c_str(),
  797. {{"CUBLAS", {}}}}));
  798. param::ConvBias cur_param;
  799. using NLMode = param::ConvBias::NonlineMode;
  800. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  801. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  802. NormalRNG default_rng;
  803. checker.set_dtype(0, dtype::Float32())
  804. .set_dtype(1, dtype::Float32())
  805. .set_dtype(2, dtype::Float32())
  806. .set_rng(0, &default_rng)
  807. .set_rng(1, &default_rng)
  808. .set_rng(2, &default_rng)
  809. .set_epsilon(1e-3);
  810. for (auto nlmode :
  811. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  812. cur_param.nonlineMode = nlmode;
  813. for (uint32_t s : {1}) {
  814. for (uint32_t f : {1, 3, 5, 7}) {
  815. cur_param.pad_h = cur_param.pad_w = f / 2;
  816. cur_param.stride_h = cur_param.stride_w = s;
  817. checker.set_param(cur_param).execs(
  818. {{2, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  819. }
  820. }
  821. cur_param.pad_h = cur_param.pad_w = 0;
  822. cur_param.stride_h = cur_param.stride_w = 1;
  823. checker.set_param(cur_param)
  824. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  825. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  826. }
  827. //! noncontiguous case
  828. {
  829. param::ConvBias param;
  830. param.pad_h = param.pad_w = 1;
  831. checker.set_param(param).execl(TensorLayoutArray{
  832. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  833. {{16, 16, 3, 3}, {144, 9, 3, 1}, dtype::Float32()},
  834. {{}, {}, dtype::Float32()},
  835. {{}, {}, dtype::Float32()},
  836. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  837. });
  838. }
  839. }
  840. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_8x8x32) {
  841. require_compute_capability(6, 1);
  842. Checker<ConvBiasForward> checker(handle_cuda());
  843. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  844. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  845. "MATMUL8X8X32", {})
  846. .c_str()));
  847. param::ConvBias cur_param;
  848. using NLMode = param::ConvBias::NonlineMode;
  849. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  850. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  851. cur_param.format = param::ConvBias::Format::NHWC;
  852. UniformIntRNG rng{-100, 100};
  853. UniformIntRNG bias_rng{-1000, 1000};
  854. checker.set_rng(0, &rng)
  855. .set_rng(1, &rng)
  856. .set_rng(2, &bias_rng)
  857. .set_rng(3, &rng)
  858. .set_dtype(0, dtype::QuantizedS8{1.2f})
  859. .set_dtype(1, dtype::QuantizedS8{1.3f})
  860. .set_dtype(2, dtype::QuantizedS32{1.2f * 1.3f})
  861. .set_dtype(3, dtype::QuantizedS8{1.1f})
  862. .set_dtype(4, dtype::QuantizedS8{1.0f})
  863. .set_epsilon(1);
  864. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  865. cur_param.nonlineMode = nlmode;
  866. for (uint32_t s : {1}) {
  867. for (uint32_t f : {1, 3, 5, 7}) {
  868. cur_param.pad_h = cur_param.pad_w = f / 2;
  869. cur_param.stride_h = cur_param.stride_w = s;
  870. checker.set_param(cur_param).execs(
  871. {{2, 16, 16, 4}, {4, f, f, 4}, {1, 1, 1, 4}, {}, {}});
  872. }
  873. }
  874. cur_param.pad_h = cur_param.pad_w = 0;
  875. cur_param.stride_h = cur_param.stride_w = 1;
  876. checker.set_param(cur_param)
  877. .execs({{2, 3, 16, 3}, {5, 3, 3, 3}, {1, 1, 1, 5}, {}, {}})
  878. .execs({{2, 8, 3, 2}, {3, 3, 3, 2}, {1, 1, 1, 3}, {}, {}});
  879. }
  880. //! noncontiguous case
  881. {
  882. param::ConvBias param;
  883. param.pad_h = param.pad_w = 1;
  884. param.format = param::ConvBias::Format::NHWC;
  885. checker.set_param(param).execl(TensorLayoutArray{
  886. {{2, 7, 7, 16}, {1568, 224, 32, 1}, dtype::QuantizedS8{1.2f}},
  887. {{16, 3, 3, 16}, {144, 48, 16, 1}, dtype::QuantizedS8{1.3f}},
  888. {{}, {}, dtype::QuantizedS32{1.2f * 1.3f}},
  889. {{}, {}, dtype::QuantizedS8{1.1f}},
  890. {{2, 7, 7, 16},
  891. {1568, 224, 32, 1},
  892. dtype::QuantizedS32{1.2f * 1.3f}},
  893. });
  894. }
  895. }
  896. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_NCHW4) {
  897. require_compute_capability(6, 1);
  898. Checker<ConvBiasForward> checker(handle_cuda());
  899. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  900. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  901. "MATMUL8X8X32", {})
  902. .c_str()));
  903. UniformIntRNG int_rng{-127, 127};
  904. ConvBias::Param param;
  905. param.format = ConvBias::Param::Format::NCHW4;
  906. using NLMode = ConvBias::Param::NonlineMode;
  907. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  908. .set_dtype(1, dtype::QuantizedS8(0.5f))
  909. .set_dtype(2, dtype::QuantizedS32(0.25f))
  910. .set_dtype(4, dtype::QuantizedS8(0.35f))
  911. .set_rng(0, &int_rng)
  912. .set_rng(1, &int_rng)
  913. .set_rng(2, &int_rng);
  914. param.sparse = Convolution::Param::Sparse::DENSE;
  915. param.nonlineMode = NLMode::IDENTITY;
  916. param.pad_h = param.pad_w = 1;
  917. param.stride_h = param.stride_w = 1;
  918. checker.set_param(param);
  919. checker.exec(
  920. {{8, 4, 10, 10, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  921. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  922. checker.exec(
  923. {{8, 64, 12, 12, 4}, {256, 64, 3, 3, 4}, {1, 64, 1, 1, 4}, {}, {}});
  924. //! noncontiguous case
  925. {
  926. param::ConvBias param;
  927. param.pad_h = param.pad_w = 1;
  928. param.format = ConvBias::Param::Format::NCHW4;
  929. checker.set_param(param).execl(TensorLayoutArray{
  930. {{2, 4, 7, 7, 4}, {1568, 196, 28, 4, 1}, dtype::QuantizedS8{1.2f}},
  931. {{16, 4, 3, 3, 4}, {144, 36, 12, 4, 1}, dtype::QuantizedS8{1.3f}},
  932. {{}, {}, dtype::QuantizedS32{1.2f * 1.3f}},
  933. {{}, {}, dtype::QuantizedS8{1.1f}},
  934. {{2, 4, 7, 7, 4},
  935. {1568, 196, 28, 4, 1},
  936. dtype::QuantizedS32{1.2f * 1.3f}},
  937. });
  938. }
  939. }
  940. TEST_F(CUDA, CONV_BIAS_FORWARD_BATCHED_MATMUL) {
  941. using namespace conv_bias;
  942. std::vector<TestArg> args = get_args_1x1();
  943. Checker<ConvBiasForward> checker(handle_cuda());
  944. NormalRNG default_rng;
  945. checker.set_dtype(0, dtype::Float32())
  946. .set_dtype(1, dtype::Float32())
  947. .set_dtype(2, dtype::Float32())
  948. .set_rng(0, &default_rng)
  949. .set_rng(1, &default_rng)
  950. .set_rng(2, &default_rng)
  951. .set_epsilon(1e-3);
  952. checker.set_before_exec_callback(
  953. AlgoChecker<ConvBiasForward>(ExecutionPolicyAlgoName{
  954. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  955. "BATCHED_MATMUL", {})
  956. .c_str(),
  957. {{"CUBLAS", {}}}}));
  958. for (auto&& arg : args) {
  959. checker.set_param(arg.param);
  960. checker.execs({arg.src, arg.filter, arg.bias, {}, {}});
  961. }
  962. //! noncontiguous case
  963. {
  964. param::ConvBias param;
  965. checker.set_param(param).execl(TensorLayoutArray{
  966. {{2, 16, 7, 7}, {1568, 49, 7, 1}, dtype::Float32()},
  967. {{16, 16, 1, 1}, {16, 1, 1, 1}, dtype::Float32()},
  968. {{}, {}, dtype::Float32()},
  969. {{}, {}, dtype::Float32()},
  970. {{2, 16, 7, 7}, {784, 49, 7, 1}, dtype::Float32()},
  971. });
  972. }
  973. }
  974. TEST_F(CUDA, CONV_BIAS_FORWARD_GROUP) {
  975. using NLMode = ConvBias::Param::NonlineMode;
  976. bool is_int_available = false;
  977. if (megdnn::test::check_compute_capability(6, 1)) {
  978. is_int_available = true;
  979. } else {
  980. is_int_available = false;
  981. }
  982. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  983. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  984. size_t SW, size_t DH, size_t DW, size_t group, NLMode mode) {
  985. {
  986. // float case
  987. Checker<ConvBiasForward> checker(handle_cuda());
  988. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<
  989. ConvBias>(ExecutionPolicyAlgoName{
  990. ConvBiasForward::algo_name<ConvBiasForward::DirectParam>(
  991. "CUDA:GROUP_CONV", {})
  992. .c_str(),
  993. {{"CUDNN", {}}}}));
  994. ConvBias::Param param;
  995. param.sparse = ConvBias::Param::Sparse::GROUP;
  996. param.nonlineMode = mode;
  997. param.pad_h = PH;
  998. param.pad_w = PW;
  999. param.stride_h = SH;
  1000. param.stride_w = SW;
  1001. param.dilate_h = DH;
  1002. param.dilate_w = DW;
  1003. auto ICg = IC / group;
  1004. auto OCg = OC / group;
  1005. checker.set_param(param).exec({{N, IC, IH, IW},
  1006. {group, OCg, ICg, FH, FW},
  1007. {1, OCg * group, 1, 1},
  1008. {},
  1009. {}});
  1010. }
  1011. if (is_int_available) {
  1012. // int 8x8x32 case
  1013. Checker<ConvBiasForward> checker(handle_cuda());
  1014. ConvBias::Param param;
  1015. param.sparse = Convolution::Param::Sparse::GROUP;
  1016. param.format = Convolution::Param::Format::NHWC;
  1017. param.nonlineMode = NLMode::IDENTITY;
  1018. param.pad_h = PH;
  1019. param.pad_w = PW;
  1020. param.stride_h = SH;
  1021. param.stride_w = SW;
  1022. param.dilate_h = DH;
  1023. param.dilate_w = DW;
  1024. auto ICg = IC / group;
  1025. auto OCg = OC / group;
  1026. UniformIntRNG rng(-4, 4);
  1027. checker.set_param(param)
  1028. .set_dtype(0, dtype::QuantizedS8(0.5f))
  1029. .set_dtype(1, dtype::QuantizedS8(0.5f))
  1030. .set_dtype(2, dtype::QuantizedS32(0.25f))
  1031. .set_dtype(3, dtype::QuantizedS8(0.13f))
  1032. .set_dtype(4, dtype::QuantizedS8(0.35f))
  1033. .set_rng(0, &rng)
  1034. .set_rng(1, &rng)
  1035. .set_rng(2, &rng)
  1036. .exec({{N, IH, IW, IC},
  1037. {group, OCg, FH, FW, ICg},
  1038. {1, 1, 1, OCg * group},
  1039. {},
  1040. {}});
  1041. }
  1042. };
  1043. for (NLMode nlmode :
  1044. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  1045. // normal case
  1046. run(2, 64, 7, 7, 3, 3, 32, 0, 0, 1, 1, 1, 1, 2, nlmode);
  1047. // padded case
  1048. run(2, 32, 7, 7, 3, 3, 64, 1, 1, 1, 1, 1, 1, 4, nlmode);
  1049. // strided case
  1050. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 2, 2, 1, 1, 8, nlmode);
  1051. // dilated case
  1052. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 1, 1, 2, 2, 8, nlmode);
  1053. }
  1054. }
  1055. #if CUDA_VERSION >= 10000
  1056. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_1) {
  1057. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 3);
  1058. }
  1059. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_2) {
  1060. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 5);
  1061. }
  1062. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_3) {
  1063. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 7);
  1064. }
  1065. #if MEGDNN_WITH_BENCHMARK
  1066. TEST_F(CUDA, BENCHMARK_CONV_BIAS_QUANTIZED4x4x32) {
  1067. require_compute_capability(7, 5);
  1068. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  1069. UniformIntRNG int_rng{0, 8};
  1070. ConvBias::Param param;
  1071. param.format = ConvBias::Param::Format::NCHW8;
  1072. param.stride_h = param.stride_w = 1;
  1073. using NonlineMode = ConvBias::Param::NonlineMode;
  1074. param.nonlineMode = NonlineMode::RELU;
  1075. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  1076. size_t co, size_t fh, size_t fw, size_t nr_times) {
  1077. param.pad_h = fh / 2;
  1078. param.pad_w = fw / 2;
  1079. bencher.set_param(param)
  1080. .set_dtype(0, dtype::Quantized4Asymm(1.3f, (uint8_t)(1)))
  1081. .set_dtype(1, dtype::Quantized4Asymm(1.3f, (uint8_t)(2)))
  1082. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  1083. .set_dtype(4, dtype::QuantizedS32(1.3f * 1.3f))
  1084. .set_rng(0, &int_rng)
  1085. .set_rng(1, &int_rng)
  1086. .set_rng(2, &int_rng);
  1087. bencher.set_times(nr_times);
  1088. size_t ho = infer_conv_shape(hi, fh, 1, param.pad_h);
  1089. size_t wo = infer_conv_shape(wi, fw, 1, param.pad_w);
  1090. TensorShape inp{batch, ci / 8, hi, wi, 8}, kern{co, ci / 8, fh, fw, 8},
  1091. out{batch, co / 8, ho, wo, 8};
  1092. auto time_in_ms =
  1093. bencher.execs({inp, kern, {1, co / 8, 1, 1, 8}, {}, out}) /
  1094. nr_times;
  1095. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1096. (time_in_ms * 1e-3) * 1e-12;
  1097. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops\n",
  1098. inp.to_string().c_str(), kern.to_string().c_str(),
  1099. out.to_string().c_str(), time_in_ms, ops);
  1100. };
  1101. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  1102. run_bench(1, 32, 224, 224, 64, 7, 7, 1000);
  1103. run_bench(1, 8192, 64, 64, 4096, 3, 3, 1000);
  1104. run_bench(1, 256, 64, 64, 256, 3, 3, 1000);
  1105. run_bench(1, 64, 128, 128, 64, 3, 3, 1000);
  1106. run_bench(1, 512, 32, 32, 512, 3, 3, 1000);
  1107. run_bench(1, 1024, 16, 16, 1024, 3, 3, 1000);
  1108. run_bench(1, 64, 56, 56, 64, 3, 3, 1000);
  1109. run_bench(1, 128, 32, 32, 128, 3, 3, 1000);
  1110. run_bench(1, 256, 16, 16, 256, 3, 3, 1000);
  1111. run_bench(1, 512, 8, 8, 512, 3, 3, 1000);
  1112. run_bench(32, 32, 224, 224, 64, 7, 7, 1000);
  1113. run_bench(32, 64, 56, 56, 64, 3, 3, 1000);
  1114. run_bench(32, 128, 32, 32, 128, 3, 3, 1000);
  1115. run_bench(32, 256, 16, 16, 256, 3, 3, 1000);
  1116. run_bench(32, 512, 8, 8, 512, 3, 3, 1000);
  1117. run_bench(256, 32, 224, 224, 64, 7, 7, 1000);
  1118. run_bench(256, 64, 56, 56, 64, 3, 3, 1000);
  1119. run_bench(256, 128, 32, 32, 128, 3, 3, 1000);
  1120. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  1121. run_bench(256, 512, 8, 8, 512, 3, 3, 1000);
  1122. }
  1123. #endif
  1124. #endif
  1125. TEST_F(CUDA, CONV_BIAS_FORWARD_DILATED) {
  1126. require_compute_capability(6, 0);
  1127. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  1128. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  1129. size_t SW, size_t DH, size_t DW) {
  1130. {
  1131. // float case
  1132. Checker<ConvBiasForward> checker(handle_cuda());
  1133. ConvBias::Param param;
  1134. param.sparse = ConvBias::Param::Sparse::DENSE;
  1135. param.pad_h = PH;
  1136. param.pad_w = PW;
  1137. param.stride_h = SH;
  1138. param.stride_w = SW;
  1139. param.dilate_h = DH;
  1140. param.dilate_w = DW;
  1141. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  1142. checker.set_param(param).exec(
  1143. {{N, IC, IH, IW}, {OC, IC, FH, FW}, {1, OC, 1, 1}, {}, {}});
  1144. }
  1145. };
  1146. // dilated case
  1147. run(2, 8, 7, 7, 3, 3, 4, 0, 0, 1, 1, 2, 2);
  1148. }
  1149. #if CUDNN_VERSION >= 7500
  1150. TEST_F(CUDA, CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  1151. require_compute_capability(7, 5);
  1152. using namespace conv_bias;
  1153. Checker<ConvBiasForward> checker(handle_cuda());
  1154. auto opr = handle_cuda()->create_operator<ConvBias>();
  1155. auto i8_min = std::numeric_limits<int8_t>().min();
  1156. auto i8_max = std::numeric_limits<int8_t>().max();
  1157. UniformIntRNG int_rng{i8_min, i8_max};
  1158. ConvBias::Param param;
  1159. param.format = ConvBias::Param::Format::NCHW32;
  1160. using NonlineMode = ConvBias::Param::NonlineMode;
  1161. for (NonlineMode mode : {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1162. for (size_t batch : {2}) {
  1163. for (size_t ic : {64, 32}) {
  1164. for (size_t oc : {32}) {
  1165. for (size_t fh : {3, 5, 7}) {
  1166. for (int ph : {static_cast<int>(fh / 2), 0}) {
  1167. for (int sh : {1, 2}) {
  1168. for (size_t ih : {9, 11, 12, 13, 16}) {
  1169. for (size_t iw : {8, 27, 32, 40}) {
  1170. param.nonlineMode = mode;
  1171. param.stride_h = param.stride_w = sh;
  1172. param.pad_h = param.pad_w = ph;
  1173. opr->param() = param;
  1174. TensorLayout dst_layout;
  1175. opr->deduce_layout(
  1176. {{batch, ic / 32, ih, iw, 32},
  1177. dtype::Float32()},
  1178. {{oc, ic / 32, fh, fh, 32},
  1179. dtype::Float32()},
  1180. {}, {}, dst_layout);
  1181. checker.set_dtype(0, dtype::QuantizedS8(
  1182. 1.3f))
  1183. .set_dtype(1,
  1184. dtype::QuantizedS8(
  1185. 1.3f))
  1186. .set_dtype(2,
  1187. dtype::QuantizedS32(
  1188. 1.3f * 1.3f))
  1189. .set_dtype(3,
  1190. dtype::QuantizedS8(
  1191. 1.7f))
  1192. .set_dtype(4,
  1193. dtype::QuantizedS8(
  1194. 1.2f * 1.2f))
  1195. .set_rng(0, &int_rng)
  1196. .set_rng(1, &int_rng)
  1197. .set_rng(2, &int_rng)
  1198. .set_rng(3, &int_rng)
  1199. .set_epsilon(1 + 1e-3)
  1200. .set_param(param)
  1201. .execs({{batch, ic / 32, ih, iw,
  1202. 32},
  1203. {oc, ic / 32, fh, fh,
  1204. 32},
  1205. {1, oc / 32, 1, 1, 32},
  1206. dst_layout,
  1207. {}});
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. #if MEGDNN_WITH_BENCHMARK
  1219. TEST_F(CUDA, BENCHMARK_CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  1220. require_compute_capability(7, 5);
  1221. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  1222. bencher.set_display(false);
  1223. ConvBias::Param param;
  1224. param.format = ConvBias::Param::Format::NCHW32;
  1225. ConvBias::Param param_without_tensorcore;
  1226. param_without_tensorcore.format = ConvBias::Param::Format::NCHW4;
  1227. auto i8_min = std::numeric_limits<int8_t>().min();
  1228. auto i8_max = std::numeric_limits<int8_t>().max();
  1229. UniformIntRNG int_rng{i8_min, i8_max};
  1230. using NonlineMode = ConvBias::Param::NonlineMode;
  1231. param.nonlineMode = NonlineMode::IDENTITY;
  1232. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  1233. size_t co, size_t fh, size_t fw, size_t sh, size_t sw,
  1234. size_t nr_times) {
  1235. param.pad_h = fh / 2;
  1236. param.pad_w = fw / 2;
  1237. param.stride_h = sh;
  1238. param.stride_w = sw;
  1239. param_without_tensorcore.pad_h = fh / 2;
  1240. param_without_tensorcore.pad_w = fw / 2;
  1241. param_without_tensorcore.stride_h = sh;
  1242. param_without_tensorcore.stride_w = sw;
  1243. bencher.set_param(param)
  1244. .set_dtype(0, dtype::QuantizedS8(1.3f))
  1245. .set_dtype(1, dtype::QuantizedS8(1.3f))
  1246. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  1247. .set_dtype(4, dtype::QuantizedS8(1.2f))
  1248. .set_rng(0, &int_rng)
  1249. .set_rng(1, &int_rng)
  1250. .set_rng(2, &int_rng);
  1251. bencher.set_times(nr_times);
  1252. size_t ho = infer_conv_shape(hi, fh, sh, param.pad_h);
  1253. size_t wo = infer_conv_shape(wi, fw, sw, param.pad_w);
  1254. TensorShape inp{batch, ci / 32, hi, wi, 32},
  1255. kern{co, ci / 32, fh, fw, 32}, out{batch, co / 32, ho, wo, 32};
  1256. auto time_in_ms =
  1257. bencher.execs({inp, kern, {1, co / 32, 1, 1, 32}, {}, out}) /
  1258. nr_times;
  1259. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1260. (time_in_ms * 1e-3) * 1e-12;
  1261. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  1262. "(TensorCore)",
  1263. inp.to_string().c_str(), kern.to_string().c_str(),
  1264. out.to_string().c_str(), time_in_ms, ops);
  1265. decltype(ops) ops_without_tensorcore;
  1266. bencher.set_param(param_without_tensorcore);
  1267. {
  1268. TensorShape inp{batch, ci / 4, hi, wi, 4},
  1269. kern{co, ci / 4, fh, fw, 4}, out{batch, co / 4, ho, wo, 4};
  1270. auto time_in_ms =
  1271. bencher.execs({inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) /
  1272. nr_times;
  1273. ops_without_tensorcore = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1274. (time_in_ms * 1e-3) * 1e-12;
  1275. printf(", time: %.2fms perf: %.2f Tops (without TensorCore) ",
  1276. time_in_ms, ops_without_tensorcore);
  1277. }
  1278. printf("speedup: %.2fx\n", ops / ops_without_tensorcore);
  1279. };
  1280. // resnet-50
  1281. // bottleneck-1
  1282. // proj
  1283. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1284. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1285. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1286. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1287. // bottleneck-2
  1288. // proj
  1289. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1290. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1291. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1292. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1293. // bottleneck-3
  1294. // proj
  1295. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1296. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1297. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1298. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1299. // bottleneck-4
  1300. // proj
  1301. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1302. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1303. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1304. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1305. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1306. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1307. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1308. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1309. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1310. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1311. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1312. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1313. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1314. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1315. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1316. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1317. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1318. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1319. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1320. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1321. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1322. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1323. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1324. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1325. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1326. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1327. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1328. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1329. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1330. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1331. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1332. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1333. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1334. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1335. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1336. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1337. }
  1338. #endif
  1339. #endif
  1340. // vim: syntax=cpp.doxygen

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