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

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