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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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_NCHW_QS8) {
  206. //! not support NonlineMode::SIGMOID and NonlineMode::H_SWISH
  207. require_compute_capability(6, 1);
  208. Checker<ConvBiasForward> checker(handle_cuda());
  209. UniformIntRNG int_rng{-128, 127};
  210. using NonlineMode = ConvBias::Param::NonlineMode;
  211. ConvBias::Param param;
  212. param.format = ConvBias::Param::Format::NCHW;
  213. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  214. .set_dtype(1, dtype::QuantizedS8(2.5f))
  215. .set_dtype(2, dtype::QuantizedS32(6.25f))
  216. .set_dtype(3, dtype::QuantizedS8(0.25f))
  217. .set_dtype(4, dtype::QuantizedS8(0.25f))
  218. .set_rng(0, &int_rng)
  219. .set_rng(1, &int_rng)
  220. .set_rng(2, &int_rng)
  221. .set_rng(3, &int_rng);
  222. for (NonlineMode mode : {NonlineMode::RELU,
  223. NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  224. for (size_t g : {1, 2}) {
  225. for (size_t b : {2}) {
  226. for (size_t ic : {6, 16}) {
  227. for (size_t oc : {4}) {
  228. for (size_t fh : {1, 3}) {
  229. for (int ph : {static_cast<int>(fh / 2)}) {
  230. for (int sh : {1, 2}) {
  231. size_t ih = 16, iw = 16;
  232. param.nonlineMode = mode;
  233. param.stride_h = param.stride_w = sh;
  234. param.pad_h = param.pad_w = ph;
  235. param.sparse =
  236. ConvBias::Param::Sparse::DENSE;
  237. checker.set_param(param)
  238. .execs({{b, ic / 2, ih, iw},
  239. {oc, ic / 2, fh, fh},
  240. {1, oc, 1, 1},
  241. {},
  242. {}});
  243. param.sparse =
  244. ConvBias::Param::Sparse::GROUP;
  245. checker.set_param(param)
  246. .execs({{b, ic, ih, iw},
  247. {g, oc/g, ic/g, fh, fh},
  248. {1, oc, 1, 1},
  249. {},
  250. {}});
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. for (NonlineMode mode : {NonlineMode::RELU,
  260. NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  261. for (size_t g : {13}) {
  262. for (size_t b : {1, 2}) {
  263. for (size_t ic : {13}) {
  264. for (size_t oc : {13}) {
  265. for (size_t fh : {1, 3}) {
  266. for (int ph : {static_cast<int>(fh / 2)}) {
  267. for (int sh : {1, 2}) {
  268. size_t ih = 16, iw = 16;
  269. param.nonlineMode = mode;
  270. param.stride_h = param.stride_w = sh;
  271. param.pad_h = param.pad_w = ph;
  272. param.sparse =
  273. ConvBias::Param::Sparse::GROUP;
  274. checker.set_param(param)
  275. .execs({{b, ic, ih, iw},
  276. {g, oc/g, ic/g, fh, fh},
  277. {1, oc, 1, 1},
  278. {},
  279. {}});
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. {
  289. size_t ih = 16, iw = 16, b = 1, oc = 14, ic = 14;
  290. size_t fh = 3, sh = 1, ph = 1;
  291. param.nonlineMode = NonlineMode::IDENTITY;
  292. param.stride_h = param.stride_w = sh;
  293. param.pad_h = param.pad_w = ph;
  294. param.sparse = ConvBias::Param::Sparse::DENSE;
  295. checker.set_param(param).execs(
  296. {{b, ic, ih, iw}, {oc, ic, fh, fh}, {}, {}, {}});
  297. }
  298. }
  299. TEST_F(CUDA, CONV_BIAS_NCHW_QS8_FUSE_Z) {
  300. require_compute_capability(6, 1);
  301. Checker<ConvBiasForward> checker(handle_cuda());
  302. UniformIntRNG int_rng{-128, 127};
  303. using NonlineMode = ConvBias::Param::NonlineMode;
  304. ConvBias::Param param;
  305. param.format = ConvBias::Param::Format::NCHW;
  306. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  307. .set_dtype(1, dtype::QuantizedS8(2.5f))
  308. .set_dtype(2, dtype::QuantizedS32(6.25f))
  309. .set_dtype(3, dtype::QuantizedS8(0.25f))
  310. .set_dtype(4, dtype::QuantizedS8(0.25f))
  311. .set_rng(0, &int_rng)
  312. .set_rng(1, &int_rng)
  313. .set_rng(2, &int_rng)
  314. .set_rng(3, &int_rng);
  315. for (NonlineMode mode :
  316. {NonlineMode::RELU, NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  317. for (size_t b : {2}) {
  318. for (size_t ic : {6, 16}) {
  319. for (size_t oc : {4}) {
  320. for (size_t fh : {1, 3}) {
  321. for (int ph : {static_cast<int>(fh / 2)}) {
  322. for (int sh : {1, 2}) {
  323. size_t ih = 16, iw = 16;
  324. param.nonlineMode = mode;
  325. param.stride_h = param.stride_w = sh;
  326. param.pad_h = param.pad_w = ph;
  327. param.sparse = ConvBias::Param::Sparse::DENSE;
  328. const size_t oh = (ih - fh + 2 * ph) / sh + 1;
  329. const size_t ow = (iw - fh + 2 * ph) / sh + 1;
  330. checker.set_param(param).execs(
  331. {{b, ic, ih, iw},
  332. {oc, ic, fh, fh},
  333. {1, oc, 1, 1},
  334. {b, oc, oh, ow},
  335. {}});
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. #if MEGDNN_WITH_BENCHMARK
  345. TEST_F(CUDA, BENCHMARK_CONV_BIAS_NCHW4_INT8) {
  346. require_compute_capability(6, 1);
  347. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  348. bencher.set_display(false);
  349. ConvBias::Param param_nchw;
  350. param_nchw.format = ConvBias::Param::Format::NCHW;
  351. ConvBias::Param param_nchw4;
  352. param_nchw4.format = ConvBias::Param::Format::NCHW4;
  353. auto i8_min = std::numeric_limits<int8_t>().min();
  354. auto i8_max = std::numeric_limits<int8_t>().max();
  355. UniformIntRNG int_rng{i8_min, i8_max};
  356. param_nchw.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  357. auto run_bench = [&](size_t b, size_t ci, size_t hi, size_t wi,
  358. size_t co, size_t fh, size_t fw, size_t sh,
  359. size_t sw, size_t nr_times) {
  360. param_nchw.pad_h = fh / 2;
  361. param_nchw.pad_w = fw / 2;
  362. param_nchw.stride_h = sh;
  363. param_nchw.stride_w = sw;
  364. param_nchw4.pad_h = fh / 2;
  365. param_nchw4.pad_w = fh / 2;
  366. param_nchw4.stride_h = sh;
  367. param_nchw4.stride_w = sw;
  368. bencher.set_times(nr_times)
  369. .set_dtype(0, dtype::QuantizedS8(2.5f))
  370. .set_dtype(1, dtype::QuantizedS8(2.5f))
  371. .set_dtype(2, dtype::QuantizedS32(6.25f))
  372. .set_dtype(4, dtype::QuantizedS8(0.35f))
  373. .set_rng(0, &int_rng)
  374. .set_rng(1, &int_rng)
  375. .set_rng(2, &int_rng);
  376. bencher.set_param(param_nchw);
  377. size_t ho = infer_conv_shape(hi, fh, sh, param_nchw.pad_h);
  378. size_t wo = infer_conv_shape(wi, fw, sw, param_nchw.pad_w);
  379. TensorShape inp{b, ci, hi, wi}, kern{co, ci, fh, fw},
  380. out{b, co, ho, wo};
  381. auto time_in_ms = bencher.execs(
  382. {inp, kern, {1, co, 1, 1}, {}, out}) / nr_times;
  383. auto ops_nchw = 2.0 * b * co * ho * wo * ci * fh * fw /
  384. (time_in_ms * 1e-3) * 1e-12;
  385. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  386. "(NCHW)\n",
  387. inp.to_string().c_str(), kern.to_string().c_str(),
  388. out.to_string().c_str(), time_in_ms, ops_nchw);
  389. bencher.set_param(param_nchw4);
  390. decltype(ops_nchw) ops_nchw4;
  391. {
  392. TensorShape inp{b, ci / 4, hi, wi, 4},
  393. kern{co, ci / 4, fh, fw, 4}, out{b, co / 4, ho, wo, 4};
  394. auto time_in_ms = bencher.execs(
  395. {inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) / nr_times;
  396. ops_nchw4 = 2.0 * b * co * ho * wo * ci * fh * fw /
  397. (time_in_ms * 1e-3) * 1e-12;
  398. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  399. "(NCHW4)\n",
  400. inp.to_string().c_str(), kern.to_string().c_str(),
  401. out.to_string().c_str(), time_in_ms, ops_nchw4);
  402. }
  403. printf("speedup: %.2fx\n", ops_nchw4 / ops_nchw);
  404. };
  405. // resnet-50
  406. // bottleneck-1
  407. // proj
  408. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  409. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  410. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  411. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  412. // bottleneck-2
  413. // proj
  414. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  415. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  416. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  417. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  418. // bottleneck-3
  419. // proj
  420. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  421. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  422. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  423. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  424. // bottleneck-4
  425. // proj
  426. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  427. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  428. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  429. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  430. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  431. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  432. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  433. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  434. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  435. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  436. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  437. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  438. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  439. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  440. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  441. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  442. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  443. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  444. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  445. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  446. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  447. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  448. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  449. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  450. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  451. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  452. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  453. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  454. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  455. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  456. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  457. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  458. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  459. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  460. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  461. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  462. }
  463. #endif
  464. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4) {
  465. require_compute_capability(6, 1);
  466. using namespace conv_bias;
  467. Checker<ConvBiasForward> checker(handle_cuda());
  468. UniformIntRNG int_rng{-5, 5};
  469. ConvBias::Param param;
  470. param.format = ConvBias::Param::Format::NCHW4;
  471. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  472. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  473. .set_dtype(1, dtype::QuantizedS8(0.5f))
  474. .set_dtype(2, dtype::QuantizedS32(0.25f))
  475. .set_dtype(3, dtype::QuantizedS8(0.13f))
  476. .set_dtype(4, dtype::QuantizedS8(0.35f))
  477. .set_rng(0, &int_rng)
  478. .set_rng(1, &int_rng)
  479. .set_rng(2, &int_rng)
  480. .set_rng(3, &int_rng)
  481. .set_param(param);
  482. auto opr = handle_cuda()->create_operator<ConvBias>();
  483. auto run = [&](const TensorShapeArray& shapes) {
  484. opr->param() = param;
  485. TensorLayout dst_layout;
  486. opr->deduce_layout({shapes[0], dtype::Float32()},
  487. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  488. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  489. };
  490. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 1, 1, 1, 4}});
  491. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 6, 1, 1, 4}});
  492. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 6, 1, 1, 4}});
  493. param.sparse = ConvBias::Param::Sparse::GROUP;
  494. checker.set_param(param);
  495. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 4, 1, 1, 4}});
  496. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 24, 1, 1, 4}});
  497. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 6, 1, 1, 4}});
  498. param.pad_h = param.pad_w = 1;
  499. param.stride_h = param.stride_w = 2;
  500. checker.set_param(param);
  501. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 16, 1, 1, 4}});
  502. // case which cudnn not supported
  503. param.sparse = ConvBias::Param::Sparse::DENSE;
  504. param.pad_h = param.pad_w = 1;
  505. param.stride_h = param.stride_w = 1;
  506. checker.set_param(param);
  507. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  508. }
  509. //! FIXME: conv kernel of cudnn for NCHW4_NCHW tensor format causes illegal
  510. //! memory access errors, so we have to disable this test here.
  511. #if 0
  512. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4_NCHW) {
  513. require_compute_capability(6, 1);
  514. using namespace conv_bias;
  515. Checker<ConvBiasForward> checker(handle_cuda());
  516. UniformIntRNG int_rng{-3, 3};
  517. UniformFloatRNG float_rng{-50, 50};
  518. ConvBias::Param param;
  519. param.format = ConvBias::Param::Format::NCHW4_NCHW;
  520. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  521. checker.set_dtype(0, dtype::QuantizedS8(1.9980618f))
  522. .set_dtype(1, dtype::QuantizedS8(1.9980927f))
  523. .set_dtype(2, dtype::Float32())
  524. .set_dtype(3, dtype::Float32())
  525. .set_dtype(4, dtype::Float32())
  526. .set_rng(0, &int_rng)
  527. .set_rng(1, &int_rng)
  528. .set_rng(2, &float_rng)
  529. .set_rng(3, &float_rng)
  530. .set_param(param);
  531. auto opr = handle_cuda()->create_operator<ConvBias>();
  532. auto run = [&](const TensorShapeArray& shapes) {
  533. opr->param() = param;
  534. TensorLayout dst_layout;
  535. opr->deduce_layout({shapes[0], dtype::Float32()},
  536. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  537. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  538. };
  539. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 4, 1, 1}});
  540. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 24, 1, 1}});
  541. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 24, 1, 1}});
  542. param.sparse = ConvBias::Param::Sparse::GROUP;
  543. param.nonlineMode = ConvBias::Param::NonlineMode::RELU;
  544. checker.set_param(param);
  545. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 16, 1, 1}});
  546. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 96, 1, 1}});
  547. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 24, 1, 1}});
  548. param.pad_h = param.pad_w = 1;
  549. param.stride_h = param.stride_w = 2;
  550. checker.set_param(param);
  551. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 64, 1, 1}});
  552. // case which cudnn not supported
  553. param.sparse = ConvBias::Param::Sparse::DENSE;
  554. param.pad_h = param.pad_w = 1;
  555. param.stride_h = param.stride_w = 1;
  556. param.nonlineMode = ConvBias::Param::NonlineMode::H_SWISH;
  557. checker.set_param(param);
  558. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 16, 1, 1}, {}, {}});
  559. }
  560. #endif
  561. #endif
  562. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE) {
  563. Checker<ConvBiasForward> checker(handle_cuda());
  564. std::vector<TestArg> args = get_chanwise_args();
  565. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  566. ConvBiasForward::algo_name<ConvBias::DirectParam>("CHANNEL_WISE",
  567. {})
  568. .c_str()));
  569. for (auto dtype : std::vector<DType>{dtype::Float32(), dtype::Float16()}) {
  570. checker.set_dtype(0, dtype)
  571. .set_dtype(1, dtype)
  572. .set_dtype(2, dtype)
  573. .set_dtype(3, dtype)
  574. .set_dtype(4, dtype);
  575. if (dtype.enumv() == DTypeEnum::Float16)
  576. checker.set_epsilon(2e-2);
  577. for (auto&& arg : args) {
  578. checker.set_param(arg.param).execs(
  579. {arg.src, arg.filter, arg.bias, {}, {}});
  580. }
  581. }
  582. }
  583. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_SMALL) {
  584. Checker<ConvBiasForward> checker(handle_cuda());
  585. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  586. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  587. "CHANNEL_WISE_SMALL", {})
  588. .c_str()));
  589. param::ConvBias cur_param;
  590. using NLMode = param::ConvBias::NonlineMode;
  591. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  592. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  593. for (auto nlmode :
  594. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  595. cur_param.nonlineMode = nlmode;
  596. for (auto dtype : std::vector<DType> {
  597. dtype::Float32(),
  598. #if CUDA_VERSION >= 9000
  599. dtype::Float16()
  600. #endif
  601. }) {
  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 (uint32_t s : {1}) {
  610. for (uint32_t f : {1, 3, 5, 7}) {
  611. cur_param.pad_h = cur_param.pad_w = f / 2;
  612. cur_param.stride_h = cur_param.stride_w = s;
  613. checker.set_param(cur_param).execs({{2, 3, 16, 16},
  614. {3, 1, 1, f, f},
  615. {1, 3, 1, 1},
  616. {},
  617. {}});
  618. }
  619. }
  620. cur_param.pad_h = cur_param.pad_w = 1;
  621. cur_param.stride_h = cur_param.stride_w = 1;
  622. checker.set_param(cur_param)
  623. .execs({{2, 3, 3, 16},
  624. {3, 1, 1, 3, 3},
  625. {1, 3, 1, 1},
  626. {},
  627. {}})
  628. .execs({{2, 3, 8, 3},
  629. {3, 1, 1, 3, 3},
  630. {1, 3, 1, 1},
  631. {},
  632. {}});
  633. }
  634. }
  635. }
  636. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_8x8x32) {
  637. require_compute_capability(6, 1);
  638. Checker<ConvBiasForward> checker(handle_cuda());
  639. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  640. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  641. "CHANNEL_WISE_8X8X32", {})
  642. .c_str()));
  643. param::ConvBias cur_param;
  644. using NLMode = param::ConvBias::NonlineMode;
  645. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  646. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  647. cur_param.format = ConvBias::Param::Format::NHWC;
  648. UniformIntRNG rng(-4, 4);
  649. checker.set_dtype(0, dtype::Int8{})
  650. .set_dtype(1, dtype::Int8{})
  651. .set_dtype(2, dtype::Int32{})
  652. .set_dtype(4, dtype::Int32{})
  653. .set_rng(0, &rng)
  654. .set_rng(1, &rng)
  655. .set_rng(2, &rng);
  656. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  657. cur_param.nonlineMode = nlmode;
  658. for (uint32_t s : {1, 2}) {
  659. for (uint32_t f : {1, 3, 5, 7}) {
  660. for (uint32_t g : {4, 8}) {
  661. cur_param.pad_h = cur_param.pad_w = f / 2;
  662. cur_param.stride_h = cur_param.stride_w = s;
  663. checker.set_param(cur_param).execs({{2, 9, 16, g},
  664. {g, 1, f, f, 1},
  665. {1, 1, 1, g},
  666. {},
  667. {}});
  668. }
  669. }
  670. }
  671. }
  672. }
  673. TEST_F(CUDA, CONV_BIAS_FORWARD_CUDNN_CONVOLUTION) {
  674. using namespace conv_bias;
  675. std::vector<TestArg> args = get_args();
  676. Checker<ConvBiasForward> checker(handle_cuda());
  677. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  678. ConvBiasForward::algo_name<ConvBias::DefaultParam>(
  679. "CUDNN:Convolution", {})
  680. .c_str()));
  681. NormalRNG default_rng;
  682. for (auto&& arg : args) {
  683. checker.set_dtype(0, dtype::Float32())
  684. .set_dtype(1, dtype::Float32())
  685. .set_dtype(2, dtype::Float32())
  686. .set_rng(0, &default_rng)
  687. .set_rng(1, &default_rng)
  688. .set_rng(2, &default_rng)
  689. .set_epsilon(1e-3)
  690. .set_param(arg.param)
  691. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  692. }
  693. }
  694. TEST_F(CUDA, CONV_BIAS_FORWARD_INPLACE_MATMUL) {
  695. using namespace conv_bias;
  696. std::vector<TestArg> args = get_args();
  697. Checker<ConvBiasForward> checker(handle_cuda());
  698. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  699. ConvBiasForward::algo_name<ConvBias::MatmulParam>("INPLACE_MATMUL",
  700. {})
  701. .c_str()));
  702. param::ConvBias cur_param;
  703. using NLMode = param::ConvBias::NonlineMode;
  704. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  705. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  706. NormalRNG default_rng;
  707. checker.set_dtype(0, dtype::Float32())
  708. .set_dtype(1, dtype::Float32())
  709. .set_dtype(2, dtype::Float32())
  710. .set_rng(0, &default_rng)
  711. .set_rng(1, &default_rng)
  712. .set_rng(2, &default_rng)
  713. .set_epsilon(1e-3);
  714. for (auto nlmode :
  715. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  716. cur_param.nonlineMode = nlmode;
  717. for (uint32_t s : {1}) {
  718. for (uint32_t f : {1, 3, 5, 7}) {
  719. cur_param.pad_h = cur_param.pad_w = f / 2;
  720. cur_param.stride_h = cur_param.stride_w = s;
  721. checker.set_param(cur_param).execs(
  722. {{2, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  723. }
  724. }
  725. cur_param.pad_h = cur_param.pad_w = 1;
  726. cur_param.stride_h = cur_param.stride_w = 1;
  727. checker.set_param(cur_param)
  728. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  729. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  730. }
  731. }
  732. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL) {
  733. using namespace conv_bias;
  734. std::vector<TestArg> args = get_args();
  735. Checker<ConvBiasForward> checker(handle_cuda());
  736. checker.set_before_exec_callback(
  737. AlgoChecker<ConvBiasForward>(ExecutionPolicyAlgoName{
  738. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  739. "MATMUL", {})
  740. .c_str(),
  741. {{"CUBLAS", {}}}}));
  742. param::ConvBias cur_param;
  743. using NLMode = param::ConvBias::NonlineMode;
  744. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  745. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  746. NormalRNG default_rng;
  747. checker.set_dtype(0, dtype::Float32())
  748. .set_dtype(1, dtype::Float32())
  749. .set_dtype(2, dtype::Float32())
  750. .set_rng(0, &default_rng)
  751. .set_rng(1, &default_rng)
  752. .set_rng(2, &default_rng)
  753. .set_epsilon(1e-3);
  754. for (auto nlmode :
  755. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  756. cur_param.nonlineMode = nlmode;
  757. for (uint32_t s : {1}) {
  758. for (uint32_t f : {1, 3, 5, 7}) {
  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, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  763. }
  764. }
  765. cur_param.pad_h = cur_param.pad_w = 0;
  766. cur_param.stride_h = cur_param.stride_w = 1;
  767. checker.set_param(cur_param)
  768. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  769. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  770. }
  771. }
  772. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_8x8x32) {
  773. require_compute_capability(6, 1);
  774. Checker<ConvBiasForward> checker(handle_cuda());
  775. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  776. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  777. "MATMUL8X8X32", {})
  778. .c_str()));
  779. param::ConvBias cur_param;
  780. using NLMode = param::ConvBias::NonlineMode;
  781. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  782. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  783. cur_param.format = param::ConvBias::Format::NHWC;
  784. UniformIntRNG rng{-100, 100};
  785. UniformIntRNG bias_rng{-1000, 1000};
  786. checker.set_rng(0, &rng)
  787. .set_rng(1, &rng)
  788. .set_rng(2, &bias_rng)
  789. .set_rng(3, &rng)
  790. .set_dtype(0, dtype::QuantizedS8{1.2f})
  791. .set_dtype(1, dtype::QuantizedS8{1.3f})
  792. .set_dtype(2, dtype::QuantizedS32{1.2f * 1.3f})
  793. .set_dtype(3, dtype::QuantizedS8{1.1f})
  794. .set_dtype(4, dtype::QuantizedS8{1.0f})
  795. .set_epsilon(1);
  796. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  797. cur_param.nonlineMode = nlmode;
  798. for (uint32_t s : {1}) {
  799. for (uint32_t f : {1, 3, 5, 7}) {
  800. cur_param.pad_h = cur_param.pad_w = f / 2;
  801. cur_param.stride_h = cur_param.stride_w = s;
  802. checker.set_param(cur_param).execs(
  803. {{2, 16, 16, 4}, {4, f, f, 4}, {1, 1, 1, 4}, {}, {}});
  804. }
  805. }
  806. cur_param.pad_h = cur_param.pad_w = 0;
  807. cur_param.stride_h = cur_param.stride_w = 1;
  808. checker.set_param(cur_param)
  809. .execs({{2, 3, 16, 3}, {5, 3, 3, 3}, {1, 1, 1, 5}, {}, {}})
  810. .execs({{2, 8, 3, 2}, {3, 3, 3, 2}, {1, 1, 1, 3}, {}, {}});
  811. }
  812. }
  813. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_NCHW4) {
  814. require_compute_capability(6, 1);
  815. Checker<ConvBiasForward> checker(handle_cuda());
  816. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  817. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  818. "MATMUL8X8X32", {})
  819. .c_str()));
  820. UniformIntRNG int_rng{-127, 127};
  821. ConvBias::Param param;
  822. param.format = ConvBias::Param::Format::NCHW4;
  823. using NLMode = ConvBias::Param::NonlineMode;
  824. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  825. .set_dtype(1, dtype::QuantizedS8(0.5f))
  826. .set_dtype(2, dtype::QuantizedS32(0.25f))
  827. .set_dtype(4, dtype::QuantizedS8(0.35f))
  828. .set_rng(0, &int_rng)
  829. .set_rng(1, &int_rng)
  830. .set_rng(2, &int_rng);
  831. param.sparse = Convolution::Param::Sparse::DENSE;
  832. param.nonlineMode = NLMode::IDENTITY;
  833. param.pad_h = param.pad_w = 1;
  834. param.stride_h = param.stride_w = 1;
  835. checker.set_param(param);
  836. checker.exec(
  837. {{8, 4, 10, 10, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  838. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  839. checker.exec(
  840. {{8, 64, 12, 12, 4}, {256, 64, 3, 3, 4}, {1, 64, 1, 1, 4}, {}, {}});
  841. }
  842. TEST_F(CUDA, CONV_BIAS_FORWARD_BATCHED_MATMUL) {
  843. using namespace conv_bias;
  844. std::vector<TestArg> args = get_args_1x1();
  845. Checker<ConvBiasForward> checker(handle_cuda());
  846. NormalRNG default_rng;
  847. checker.set_dtype(0, dtype::Float32())
  848. .set_dtype(1, dtype::Float32())
  849. .set_dtype(2, dtype::Float32())
  850. .set_rng(0, &default_rng)
  851. .set_rng(1, &default_rng)
  852. .set_rng(2, &default_rng)
  853. .set_epsilon(1e-3);
  854. checker.set_before_exec_callback(
  855. AlgoChecker<ConvBiasForward>(ExecutionPolicyAlgoName{
  856. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  857. "BATCHED_MATMUL", {})
  858. .c_str(),
  859. {{"CUBLAS", {}}}}));
  860. for (auto&& arg : args) {
  861. checker.set_param(arg.param);
  862. checker.execs({arg.src, arg.filter, arg.bias, {}, {}});
  863. }
  864. }
  865. TEST_F(CUDA, CONV_BIAS_FORWARD_GROUP) {
  866. using NLMode = ConvBias::Param::NonlineMode;
  867. bool is_int_available = false;
  868. if (megdnn::test::check_compute_capability(6, 1)) {
  869. is_int_available = true;
  870. } else {
  871. is_int_available = false;
  872. }
  873. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  874. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  875. size_t SW, size_t DH, size_t DW, size_t group, NLMode mode) {
  876. {
  877. // float case
  878. Checker<ConvBiasForward> checker(handle_cuda());
  879. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<
  880. ConvBias>(
  881. ConvBiasForward::algo_name<ConvBiasForward::DirectParam>(
  882. "CUDA:GROUP_CONV", {})
  883. .c_str()));
  884. ConvBias::Param param;
  885. param.sparse = ConvBias::Param::Sparse::GROUP;
  886. param.nonlineMode = mode;
  887. param.pad_h = PH;
  888. param.pad_w = PW;
  889. param.stride_h = SH;
  890. param.stride_w = SW;
  891. param.dilate_h = DH;
  892. param.dilate_w = DW;
  893. auto ICg = IC / group;
  894. auto OCg = OC / group;
  895. checker.set_param(param).exec({{N, IC, IH, IW},
  896. {group, OCg, ICg, FH, FW},
  897. {1, OCg * group, 1, 1},
  898. {},
  899. {}});
  900. }
  901. if (is_int_available) {
  902. // int 8x8x32 case
  903. Checker<ConvBiasForward> checker(handle_cuda());
  904. ConvBias::Param param;
  905. param.sparse = Convolution::Param::Sparse::GROUP;
  906. param.format = Convolution::Param::Format::NHWC;
  907. param.nonlineMode = NLMode::IDENTITY;
  908. param.pad_h = PH;
  909. param.pad_w = PW;
  910. param.stride_h = SH;
  911. param.stride_w = SW;
  912. param.dilate_h = DH;
  913. param.dilate_w = DW;
  914. auto ICg = IC / group;
  915. auto OCg = OC / group;
  916. UniformIntRNG rng(-4, 4);
  917. checker.set_param(param)
  918. .set_dtype(0, dtype::QuantizedS8(0.5f))
  919. .set_dtype(1, dtype::QuantizedS8(0.5f))
  920. .set_dtype(2, dtype::QuantizedS32(0.25f))
  921. .set_dtype(3, dtype::QuantizedS8(0.13f))
  922. .set_dtype(4, dtype::QuantizedS8(0.35f))
  923. .set_rng(0, &rng)
  924. .set_rng(1, &rng)
  925. .set_rng(2, &rng)
  926. .exec({{N, IH, IW, IC},
  927. {group, OCg, FH, FW, ICg},
  928. {1, 1, 1, OCg * group},
  929. {},
  930. {}});
  931. }
  932. };
  933. for (NLMode nlmode :
  934. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  935. // normal case
  936. run(2, 64, 7, 7, 3, 3, 32, 0, 0, 1, 1, 1, 1, 2, nlmode);
  937. // padded case
  938. run(2, 32, 7, 7, 3, 3, 64, 1, 1, 1, 1, 1, 1, 4, nlmode);
  939. // strided case
  940. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 2, 2, 1, 1, 8, nlmode);
  941. // dilated case
  942. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 1, 1, 2, 2, 8, nlmode);
  943. }
  944. }
  945. #if CUDA_VERSION >= 10000
  946. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_1) {
  947. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 3);
  948. }
  949. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_2) {
  950. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 5);
  951. }
  952. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_3) {
  953. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 7);
  954. }
  955. #if MEGDNN_WITH_BENCHMARK
  956. TEST_F(CUDA, BENCHMARK_CONV_BIAS_QUANTIZED4x4x32) {
  957. require_compute_capability(7, 5);
  958. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  959. UniformIntRNG int_rng{0, 8};
  960. ConvBias::Param param;
  961. param.format = ConvBias::Param::Format::NCHW8;
  962. param.stride_h = param.stride_w = 1;
  963. using NonlineMode = ConvBias::Param::NonlineMode;
  964. param.nonlineMode = NonlineMode::RELU;
  965. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  966. size_t co, size_t fh, size_t fw, size_t nr_times) {
  967. param.pad_h = fh / 2;
  968. param.pad_w = fw / 2;
  969. bencher.set_param(param)
  970. .set_dtype(0, dtype::Quantized4Asymm(1.3f, (uint8_t)(1)))
  971. .set_dtype(1, dtype::Quantized4Asymm(1.3f, (uint8_t)(2)))
  972. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  973. .set_dtype(4, dtype::QuantizedS32(1.3f * 1.3f))
  974. .set_rng(0, &int_rng)
  975. .set_rng(1, &int_rng)
  976. .set_rng(2, &int_rng);
  977. bencher.set_times(nr_times);
  978. size_t ho = infer_conv_shape(hi, fh, 1, param.pad_h);
  979. size_t wo = infer_conv_shape(wi, fw, 1, param.pad_w);
  980. TensorShape inp{batch, ci / 8, hi, wi, 8}, kern{co, ci / 8, fh, fw, 8},
  981. out{batch, co / 8, ho, wo, 8};
  982. auto time_in_ms =
  983. bencher.execs({inp, kern, {1, co / 8, 1, 1, 8}, {}, out}) /
  984. nr_times;
  985. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  986. (time_in_ms * 1e-3) * 1e-12;
  987. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops\n",
  988. inp.to_string().c_str(), kern.to_string().c_str(),
  989. out.to_string().c_str(), time_in_ms, ops);
  990. };
  991. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  992. run_bench(1, 32, 224, 224, 64, 7, 7, 1000);
  993. run_bench(1, 8192, 64, 64, 4096, 3, 3, 1000);
  994. run_bench(1, 256, 64, 64, 256, 3, 3, 1000);
  995. run_bench(1, 64, 128, 128, 64, 3, 3, 1000);
  996. run_bench(1, 512, 32, 32, 512, 3, 3, 1000);
  997. run_bench(1, 1024, 16, 16, 1024, 3, 3, 1000);
  998. run_bench(1, 64, 56, 56, 64, 3, 3, 1000);
  999. run_bench(1, 128, 32, 32, 128, 3, 3, 1000);
  1000. run_bench(1, 256, 16, 16, 256, 3, 3, 1000);
  1001. run_bench(1, 512, 8, 8, 512, 3, 3, 1000);
  1002. run_bench(32, 32, 224, 224, 64, 7, 7, 1000);
  1003. run_bench(32, 64, 56, 56, 64, 3, 3, 1000);
  1004. run_bench(32, 128, 32, 32, 128, 3, 3, 1000);
  1005. run_bench(32, 256, 16, 16, 256, 3, 3, 1000);
  1006. run_bench(32, 512, 8, 8, 512, 3, 3, 1000);
  1007. run_bench(256, 32, 224, 224, 64, 7, 7, 1000);
  1008. run_bench(256, 64, 56, 56, 64, 3, 3, 1000);
  1009. run_bench(256, 128, 32, 32, 128, 3, 3, 1000);
  1010. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  1011. run_bench(256, 512, 8, 8, 512, 3, 3, 1000);
  1012. }
  1013. #endif
  1014. #endif
  1015. TEST_F(CUDA, CONV_BIAS_FORWARD_DILATED) {
  1016. require_compute_capability(6, 0);
  1017. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  1018. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  1019. size_t SW, size_t DH, size_t DW) {
  1020. {
  1021. // float case
  1022. Checker<ConvBiasForward> checker(handle_cuda());
  1023. ConvBias::Param param;
  1024. param.sparse = ConvBias::Param::Sparse::DENSE;
  1025. param.pad_h = PH;
  1026. param.pad_w = PW;
  1027. param.stride_h = SH;
  1028. param.stride_w = SW;
  1029. param.dilate_h = DH;
  1030. param.dilate_w = DW;
  1031. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  1032. checker.set_param(param).exec(
  1033. {{N, IC, IH, IW}, {OC, IC, FH, FW}, {1, OC, 1, 1}, {}, {}});
  1034. }
  1035. };
  1036. // dilated case
  1037. run(2, 8, 7, 7, 3, 3, 4, 0, 0, 1, 1, 2, 2);
  1038. }
  1039. #if CUDNN_VERSION >= 7500
  1040. TEST_F(CUDA, CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  1041. require_compute_capability(7, 5);
  1042. using namespace conv_bias;
  1043. Checker<ConvBiasForward> checker(handle_cuda());
  1044. auto opr = handle_cuda()->create_operator<ConvBias>();
  1045. auto i8_min = std::numeric_limits<int8_t>().min();
  1046. auto i8_max = std::numeric_limits<int8_t>().max();
  1047. UniformIntRNG int_rng{i8_min, i8_max};
  1048. ConvBias::Param param;
  1049. param.format = ConvBias::Param::Format::NCHW32;
  1050. using NonlineMode = ConvBias::Param::NonlineMode;
  1051. for (NonlineMode mode : {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1052. for (size_t batch : {2}) {
  1053. for (size_t ic : {64, 32}) {
  1054. for (size_t oc : {32}) {
  1055. for (size_t fh : {3, 5, 7}) {
  1056. for (int ph : {static_cast<int>(fh / 2), 0}) {
  1057. for (int sh : {1, 2}) {
  1058. for (size_t ih : {9, 11, 12, 13, 16}) {
  1059. for (size_t iw : {8, 27, 32, 40}) {
  1060. param.nonlineMode = mode;
  1061. param.stride_h = param.stride_w = sh;
  1062. param.pad_h = param.pad_w = ph;
  1063. opr->param() = param;
  1064. TensorLayout dst_layout;
  1065. opr->deduce_layout(
  1066. {{batch, ic / 32, ih, iw, 32},
  1067. dtype::Float32()},
  1068. {{oc, ic / 32, fh, fh, 32},
  1069. dtype::Float32()},
  1070. {}, {}, dst_layout);
  1071. checker.set_dtype(0, dtype::QuantizedS8(
  1072. 1.3f))
  1073. .set_dtype(1,
  1074. dtype::QuantizedS8(
  1075. 1.3f))
  1076. .set_dtype(2,
  1077. dtype::QuantizedS32(
  1078. 1.3f * 1.3f))
  1079. .set_dtype(3,
  1080. dtype::QuantizedS8(
  1081. 1.7f))
  1082. .set_dtype(4,
  1083. dtype::QuantizedS8(
  1084. 1.2f * 1.2f))
  1085. .set_rng(0, &int_rng)
  1086. .set_rng(1, &int_rng)
  1087. .set_rng(2, &int_rng)
  1088. .set_rng(3, &int_rng)
  1089. .set_epsilon(1 + 1e-3)
  1090. .set_param(param)
  1091. .execs({{batch, ic / 32, ih, iw,
  1092. 32},
  1093. {oc, ic / 32, fh, fh,
  1094. 32},
  1095. {1, oc / 32, 1, 1, 32},
  1096. dst_layout,
  1097. {}});
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. #if MEGDNN_WITH_BENCHMARK
  1109. TEST_F(CUDA, BENCHMARK_CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  1110. require_compute_capability(7, 5);
  1111. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  1112. bencher.set_display(false);
  1113. ConvBias::Param param;
  1114. param.format = ConvBias::Param::Format::NCHW32;
  1115. ConvBias::Param param_without_tensorcore;
  1116. param_without_tensorcore.format = ConvBias::Param::Format::NCHW4;
  1117. auto i8_min = std::numeric_limits<int8_t>().min();
  1118. auto i8_max = std::numeric_limits<int8_t>().max();
  1119. UniformIntRNG int_rng{i8_min, i8_max};
  1120. using NonlineMode = ConvBias::Param::NonlineMode;
  1121. param.nonlineMode = NonlineMode::IDENTITY;
  1122. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  1123. size_t co, size_t fh, size_t fw, size_t sh, size_t sw,
  1124. size_t nr_times) {
  1125. param.pad_h = fh / 2;
  1126. param.pad_w = fw / 2;
  1127. param.stride_h = sh;
  1128. param.stride_w = sw;
  1129. param_without_tensorcore.pad_h = fh / 2;
  1130. param_without_tensorcore.pad_w = fw / 2;
  1131. param_without_tensorcore.stride_h = sh;
  1132. param_without_tensorcore.stride_w = sw;
  1133. bencher.set_param(param)
  1134. .set_dtype(0, dtype::QuantizedS8(1.3f))
  1135. .set_dtype(1, dtype::QuantizedS8(1.3f))
  1136. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  1137. .set_dtype(4, dtype::QuantizedS8(1.2f))
  1138. .set_rng(0, &int_rng)
  1139. .set_rng(1, &int_rng)
  1140. .set_rng(2, &int_rng);
  1141. bencher.set_times(nr_times);
  1142. size_t ho = infer_conv_shape(hi, fh, sh, param.pad_h);
  1143. size_t wo = infer_conv_shape(wi, fw, sw, param.pad_w);
  1144. TensorShape inp{batch, ci / 32, hi, wi, 32},
  1145. kern{co, ci / 32, fh, fw, 32}, out{batch, co / 32, ho, wo, 32};
  1146. auto time_in_ms =
  1147. bencher.execs({inp, kern, {1, co / 32, 1, 1, 32}, {}, out}) /
  1148. nr_times;
  1149. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1150. (time_in_ms * 1e-3) * 1e-12;
  1151. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  1152. "(TensorCore)",
  1153. inp.to_string().c_str(), kern.to_string().c_str(),
  1154. out.to_string().c_str(), time_in_ms, ops);
  1155. decltype(ops) ops_without_tensorcore;
  1156. bencher.set_param(param_without_tensorcore);
  1157. {
  1158. TensorShape inp{batch, ci / 4, hi, wi, 4},
  1159. kern{co, ci / 4, fh, fw, 4}, out{batch, co / 4, ho, wo, 4};
  1160. auto time_in_ms =
  1161. bencher.execs({inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) /
  1162. nr_times;
  1163. ops_without_tensorcore = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1164. (time_in_ms * 1e-3) * 1e-12;
  1165. printf(", time: %.2fms perf: %.2f Tops (without TensorCore) ",
  1166. time_in_ms, ops_without_tensorcore);
  1167. }
  1168. printf("speedup: %.2fx\n", ops / ops_without_tensorcore);
  1169. };
  1170. // resnet-50
  1171. // bottleneck-1
  1172. // proj
  1173. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1174. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1175. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1176. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1177. // bottleneck-2
  1178. // proj
  1179. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1180. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1181. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1182. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1183. // bottleneck-3
  1184. // proj
  1185. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1186. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1187. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1188. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1189. // bottleneck-4
  1190. // proj
  1191. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1192. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1193. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1194. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1195. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1196. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1197. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1198. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1199. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1200. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1201. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1202. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1203. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1204. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1205. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1206. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1207. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1208. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1209. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1210. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1211. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1212. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1213. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1214. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1215. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1216. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1217. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1218. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1219. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1220. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1221. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1222. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1223. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1224. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1225. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1226. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1227. }
  1228. #endif
  1229. #endif
  1230. // vim: syntax=cpp.doxygen

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