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

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