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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/cuda/fixture.h"
  12. #include "megdnn/opr_param_defs.h"
  13. #include "megdnn/oprs.h"
  14. #include "src/cuda/handle.h"
  15. #include "test/common/benchmarker.h"
  16. #include "test/common/checker.h"
  17. #include "test/common/conv_bias.h"
  18. #include "test/common/rng.h"
  19. #include "test/common/tensor.h"
  20. #include "test/common/workspace_wrapper.h"
  21. #include "test/cuda/utils.h"
  22. using namespace megdnn;
  23. using namespace test;
  24. using namespace conv_bias;
  25. namespace {
  26. #if CUDA_VERSION >= 10000
  27. void test_conv_bias_forward_wmma_int4_nchw8(Handle* handle_cuda, size_t fh) {
  28. require_compute_capability(7, 5);
  29. using namespace conv_bias;
  30. Checker<ConvBiasForward> checker(handle_cuda);
  31. UniformIntRNG int_rng{0, 8};
  32. ConvBias::Param param;
  33. param.format = ConvBias::Param::Format::NCHW8;
  34. using NonlineMode = ConvBias::Param::NonlineMode;
  35. for (NonlineMode mode : {NonlineMode::RELU}) {
  36. for (size_t batch : {1}) {
  37. for (size_t ic : {128, 32}) {
  38. for (size_t oc : {32}) {
  39. for (int ph : {static_cast<int>(fh / 2), 0}) {
  40. for (size_t ih : {8, 9, 13, 15, 16}) {
  41. for (size_t iw : {8, 16, 24, 32, 40}) {
  42. param.nonlineMode = mode;
  43. param.stride_h = param.stride_w = 1;
  44. param.pad_h = param.pad_w = ph;
  45. checker.set_dtype(0,
  46. dtype::Quantized4Asymm(
  47. 1.3f, (uint8_t)(1)))
  48. .set_dtype(1,
  49. dtype::Quantized4Asymm(
  50. 1.3f, (uint8_t)(2)))
  51. .set_dtype(2, dtype::QuantizedS32(1.3f *
  52. 1.3f))
  53. .set_dtype(4, dtype::QuantizedS32(1.3f *
  54. 1.3f))
  55. .set_rng(0, &int_rng)
  56. .set_rng(1, &int_rng)
  57. .set_rng(2, &int_rng)
  58. .set_param(param);
  59. if (!ph)
  60. iw += 2 * (fh / 2);
  61. size_t oh = infer_conv_shape(ih, fh, 1, ph);
  62. size_t ow = infer_conv_shape(iw, fh, 1, ph);
  63. if (ow % 8 != 0)
  64. continue;
  65. checker.execs({{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({{batch, ic / 8, ih, iw, 8},
  71. {oc, ic / 8, fh, fh, 8},
  72. {batch, oc / 8, oh, ow, 8},
  73. {},
  74. {}});
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. #endif
  84. } // namespace
  85. #if CUDNN_VERSION >= 7400
  86. TEST_F(CUDA, CONV_BIAS_FORWARD_F32) {
  87. using namespace conv_bias;
  88. std::vector<TestArg> args = get_args();
  89. Checker<ConvBiasForward> checker(handle_cuda());
  90. NormalRNG default_rng;
  91. for (auto&& arg : args) {
  92. checker.set_dtype(0, dtype::Float32())
  93. .set_dtype(1, dtype::Float32())
  94. .set_dtype(2, dtype::Float32())
  95. .set_rng(0, &default_rng)
  96. .set_rng(1, &default_rng)
  97. .set_rng(2, &default_rng)
  98. .set_epsilon(1e-3)
  99. .set_param(arg.param)
  100. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  101. }
  102. }
  103. TEST_F(CUDA, CONV_BIAS_FORWARD_QS8) {
  104. require_compute_capability(6, 1);
  105. UniformIntRNG int_rng{-50, 50};
  106. Checker<ConvBiasForward> checker(handle_cuda());
  107. ConvBias::Param param;
  108. param.format = ConvBias::Param::Format::NHWC;
  109. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  110. {
  111. auto src_shape = TensorShape{20, 224, 224, 4};
  112. auto filter_shape = TensorShape{24, 1, 1, 4};
  113. auto bias_shape = TensorShape{1, 1, 1, 24};
  114. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  115. .set_dtype(1, dtype::QuantizedS8(2.5f))
  116. .set_dtype(2, dtype::QuantizedS32(6.25f))
  117. .set_dtype(4, dtype::QuantizedS8(60.25f))
  118. .set_rng(0, &int_rng)
  119. .set_rng(1, &int_rng)
  120. .set_rng(2, &int_rng)
  121. .set_param(param)
  122. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  123. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  124. .set_dtype(1, dtype::QuantizedS8(2.5f))
  125. .set_dtype(2, dtype::QuantizedS32(6.25f))
  126. .set_dtype(4, dtype::QuantizedS8(40.25f))
  127. .set_rng(0, &int_rng)
  128. .set_rng(1, &int_rng)
  129. .set_rng(2, &int_rng)
  130. .set_param(param)
  131. .execs({src_shape, filter_shape, bias_shape, {}, {}});
  132. }
  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. param.sparse = ConvBias::Param::Sparse::GROUP;
  158. auto src_shape = TensorShape{20, 224, 224, 16};
  159. auto filter_shape = TensorShape{4, 4, 1, 1, 4};
  160. auto bias_shape = TensorShape{1, 1, 1, 16};
  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. TEST_F(CUDA, CONV_BIAS_NCHW_QS8) {
  182. //! not support NonlineMode::SIGMOID and NonlineMode::H_SWISH
  183. require_compute_capability(6, 1);
  184. Checker<ConvBiasForward> checker(handle_cuda());
  185. UniformIntRNG int_rng{-128, 127};
  186. using NonlineMode = ConvBias::Param::NonlineMode;
  187. ConvBias::Param param;
  188. param.format = ConvBias::Param::Format::NCHW;
  189. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  190. .set_dtype(1, dtype::QuantizedS8(2.5f))
  191. .set_dtype(2, dtype::QuantizedS32(6.25f))
  192. .set_dtype(3, dtype::QuantizedS8(0.25f))
  193. .set_dtype(4, dtype::QuantizedS8(0.25f))
  194. .set_rng(0, &int_rng)
  195. .set_rng(1, &int_rng)
  196. .set_rng(2, &int_rng)
  197. .set_rng(3, &int_rng);
  198. for (NonlineMode mode : {NonlineMode::RELU,
  199. NonlineMode::IDENTITY, NonlineMode::H_SWISH}) {
  200. for (size_t g : {1, 2}) {
  201. for (size_t b : {2}) {
  202. for (size_t ic : {6, 16}) {
  203. for (size_t oc : {4}) {
  204. for (size_t fh : {1, 3}) {
  205. for (int ph : {static_cast<int>(fh / 2)}) {
  206. for (int sh : {1, 2}) {
  207. size_t ih = 16, iw = 16;
  208. param.nonlineMode = mode;
  209. param.stride_h = param.stride_w = sh;
  210. param.pad_h = param.pad_w = ph;
  211. param.sparse =
  212. ConvBias::Param::Sparse::DENSE;
  213. checker.set_param(param)
  214. .execs({{b, ic / 2, ih, iw},
  215. {oc, ic / 2, fh, fh},
  216. {1, oc, 1, 1},
  217. {},
  218. {}});
  219. param.sparse =
  220. ConvBias::Param::Sparse::GROUP;
  221. checker.set_param(param)
  222. .execs({{b, ic, ih, iw},
  223. {g, oc/g, ic/g, fh, fh},
  224. {1, oc, 1, 1},
  225. {},
  226. {}});
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. #if MEGDNN_WITH_BENCHMARK
  237. TEST_F(CUDA, BENCHMARK_CONV_BIAS_NCHW4_INT8) {
  238. require_compute_capability(6, 1);
  239. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  240. bencher.set_display(false);
  241. ConvBias::Param param_nchw;
  242. param_nchw.format = ConvBias::Param::Format::NCHW;
  243. ConvBias::Param param_nchw4;
  244. param_nchw4.format = ConvBias::Param::Format::NCHW4;
  245. auto i8_min = std::numeric_limits<int8_t>().min();
  246. auto i8_max = std::numeric_limits<int8_t>().max();
  247. UniformIntRNG int_rng{i8_min, i8_max};
  248. param_nchw.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  249. auto run_bench = [&](size_t b, size_t ci, size_t hi, size_t wi,
  250. size_t co, size_t fh, size_t fw, size_t sh,
  251. size_t sw, size_t nr_times) {
  252. param_nchw.pad_h = fh / 2;
  253. param_nchw.pad_w = fw / 2;
  254. param_nchw.stride_h = sh;
  255. param_nchw.stride_w = sw;
  256. param_nchw4.pad_h = fh / 2;
  257. param_nchw4.pad_w = fh / 2;
  258. param_nchw4.stride_h = sh;
  259. param_nchw4.stride_w = sw;
  260. bencher.set_times(nr_times)
  261. .set_dtype(0, dtype::QuantizedS8(2.5f))
  262. .set_dtype(1, dtype::QuantizedS8(2.5f))
  263. .set_dtype(2, dtype::QuantizedS32(6.25f))
  264. .set_dtype(4, dtype::QuantizedS8(0.35f))
  265. .set_rng(0, &int_rng)
  266. .set_rng(1, &int_rng)
  267. .set_rng(2, &int_rng);
  268. bencher.set_param(param_nchw);
  269. size_t ho = infer_conv_shape(hi, fh, sh, param_nchw.pad_h);
  270. size_t wo = infer_conv_shape(wi, fw, sw, param_nchw.pad_w);
  271. TensorShape inp{b, ci, hi, wi}, kern{co, ci, fh, fw},
  272. out{b, co, ho, wo};
  273. auto time_in_ms = bencher.execs(
  274. {inp, kern, {1, co, 1, 1}, {}, out}) / nr_times;
  275. auto ops_nchw = 2.0 * b * co * ho * wo * ci * fh * fw /
  276. (time_in_ms * 1e-3) * 1e-12;
  277. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  278. "(NCHW)\n",
  279. inp.to_string().c_str(), kern.to_string().c_str(),
  280. out.to_string().c_str(), time_in_ms, ops_nchw);
  281. bencher.set_param(param_nchw4);
  282. decltype(ops_nchw) ops_nchw4;
  283. {
  284. TensorShape inp{b, ci / 4, hi, wi, 4},
  285. kern{co, ci / 4, fh, fw, 4}, out{b, co / 4, ho, wo, 4};
  286. auto time_in_ms = bencher.execs(
  287. {inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) / nr_times;
  288. ops_nchw4 = 2.0 * b * co * ho * wo * ci * fh * fw /
  289. (time_in_ms * 1e-3) * 1e-12;
  290. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  291. "(NCHW4)\n",
  292. inp.to_string().c_str(), kern.to_string().c_str(),
  293. out.to_string().c_str(), time_in_ms, ops_nchw4);
  294. }
  295. printf("speedup: %.2fx\n", ops_nchw4 / ops_nchw);
  296. };
  297. // resnet-50
  298. // bottleneck-1
  299. // proj
  300. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  301. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  302. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  303. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  304. // bottleneck-2
  305. // proj
  306. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  307. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  308. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  309. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  310. // bottleneck-3
  311. // proj
  312. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  313. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  314. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  315. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  316. // bottleneck-4
  317. // proj
  318. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  319. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  320. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  321. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  322. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  323. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  324. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  325. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  326. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  327. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  328. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  329. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  330. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  331. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  332. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  333. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  334. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  335. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  336. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  337. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  338. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  339. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  340. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  341. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  342. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  343. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  344. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  345. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  346. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  347. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  348. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  349. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  350. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  351. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  352. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  353. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  354. }
  355. #endif
  356. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4) {
  357. require_compute_capability(6, 1);
  358. using namespace conv_bias;
  359. Checker<ConvBiasForward> checker(handle_cuda());
  360. UniformIntRNG int_rng{-5, 5};
  361. ConvBias::Param param;
  362. param.format = ConvBias::Param::Format::NCHW4;
  363. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  364. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  365. .set_dtype(1, dtype::QuantizedS8(0.5f))
  366. .set_dtype(2, dtype::QuantizedS32(0.25f))
  367. .set_dtype(3, dtype::QuantizedS8(0.13f))
  368. .set_dtype(4, dtype::QuantizedS8(0.35f))
  369. .set_rng(0, &int_rng)
  370. .set_rng(1, &int_rng)
  371. .set_rng(2, &int_rng)
  372. .set_rng(3, &int_rng)
  373. .set_param(param);
  374. auto opr = handle_cuda()->create_operator<ConvBias>();
  375. auto run = [&](const TensorShapeArray& shapes) {
  376. opr->param() = param;
  377. TensorLayout dst_layout;
  378. opr->deduce_layout({shapes[0], dtype::Float32()},
  379. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  380. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  381. };
  382. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 1, 1, 1, 4}});
  383. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 6, 1, 1, 4}});
  384. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 6, 1, 1, 4}});
  385. param.sparse = ConvBias::Param::Sparse::GROUP;
  386. checker.set_param(param);
  387. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 4, 1, 1, 4}});
  388. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 24, 1, 1, 4}});
  389. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 6, 1, 1, 4}});
  390. param.pad_h = param.pad_w = 1;
  391. param.stride_h = param.stride_w = 2;
  392. checker.set_param(param);
  393. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 16, 1, 1, 4}});
  394. // case which cudnn not supported
  395. param.sparse = ConvBias::Param::Sparse::DENSE;
  396. param.pad_h = param.pad_w = 1;
  397. param.stride_h = param.stride_w = 1;
  398. checker.set_param(param);
  399. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  400. }
  401. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW4_NCHW) {
  402. require_compute_capability(6, 1);
  403. using namespace conv_bias;
  404. Checker<ConvBiasForward> checker(handle_cuda());
  405. UniformIntRNG int_rng{-3, 3};
  406. UniformFloatRNG float_rng{-50, 50};
  407. ConvBias::Param param;
  408. param.format = ConvBias::Param::Format::NCHW4_NCHW;
  409. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  410. checker.set_dtype(0, dtype::QuantizedS8(1.9980618f))
  411. .set_dtype(1, dtype::QuantizedS8(1.9980927f))
  412. .set_dtype(2, dtype::Float32())
  413. .set_dtype(3, dtype::Float32())
  414. .set_dtype(4, dtype::Float32())
  415. .set_rng(0, &int_rng)
  416. .set_rng(1, &int_rng)
  417. .set_rng(2, &float_rng)
  418. .set_rng(3, &float_rng)
  419. .set_param(param);
  420. auto opr = handle_cuda()->create_operator<ConvBias>();
  421. auto run = [&](const TensorShapeArray& shapes) {
  422. opr->param() = param;
  423. TensorLayout dst_layout;
  424. opr->deduce_layout({shapes[0], dtype::Float32()},
  425. {shapes[1], dtype::Float32()}, {}, {}, dst_layout);
  426. checker.execs({shapes[0], shapes[1], shapes[2], dst_layout, {}});
  427. };
  428. run({{1, 4, 4, 4, 4}, {4, 4, 3, 3, 4}, {1, 4, 1, 1}});
  429. run({{20, 1, 24, 24, 4}, {24, 1, 2, 2, 4}, {1, 24, 1, 1}});
  430. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 24, 1, 1}});
  431. param.sparse = ConvBias::Param::Sparse::GROUP;
  432. param.nonlineMode = ConvBias::Param::NonlineMode::RELU;
  433. checker.set_param(param);
  434. run({{1, 4, 24, 24, 4}, {4, 4, 1, 1, 1, 4}, {1, 16, 1, 1}});
  435. run({{20, 8, 24, 24, 4}, {4, 24, 2, 2, 2, 4}, {1, 96, 1, 1}});
  436. run({{1, 3, 24, 24, 4}, {3, 8, 1, 3, 3, 4}, {1, 24, 1, 1}});
  437. param.pad_h = param.pad_w = 1;
  438. param.stride_h = param.stride_w = 2;
  439. checker.set_param(param);
  440. run({{10, 16, 28, 28, 4}, {8, 8, 2, 3, 3, 4}, {1, 64, 1, 1}});
  441. // case which cudnn not supported
  442. param.sparse = ConvBias::Param::Sparse::DENSE;
  443. param.pad_h = param.pad_w = 1;
  444. param.stride_h = param.stride_w = 1;
  445. param.nonlineMode = ConvBias::Param::NonlineMode::H_SWISH;
  446. checker.set_param(param);
  447. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 16, 1, 1}, {}, {}});
  448. }
  449. #endif
  450. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE) {
  451. Checker<ConvBiasForward> checker(handle_cuda());
  452. std::vector<TestArg> args = get_chanwise_args();
  453. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  454. ConvBiasForward::algo_name<ConvBias::DirectParam>("CHANNEL_WISE",
  455. {})
  456. .c_str()));
  457. for (auto dtype : std::vector<DType>{dtype::Float32(), dtype::Float16()}) {
  458. checker.set_dtype(0, dtype)
  459. .set_dtype(1, dtype)
  460. .set_dtype(2, dtype)
  461. .set_dtype(3, dtype)
  462. .set_dtype(4, dtype);
  463. if (dtype.enumv() == DTypeEnum::Float16)
  464. checker.set_epsilon(2e-2);
  465. for (auto&& arg : args) {
  466. checker.set_param(arg.param).execs(
  467. {arg.src, arg.filter, arg.bias, {}, {}});
  468. }
  469. }
  470. }
  471. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_SMALL) {
  472. Checker<ConvBiasForward> checker(handle_cuda());
  473. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  474. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  475. "CHANNEL_WISE_SMALL", {})
  476. .c_str()));
  477. param::ConvBias cur_param;
  478. using NLMode = param::ConvBias::NonlineMode;
  479. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  480. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  481. for (auto nlmode :
  482. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  483. cur_param.nonlineMode = nlmode;
  484. for (auto dtype : std::vector<DType> {
  485. dtype::Float32(),
  486. #if CUDA_VERSION >= 9000
  487. dtype::Float16()
  488. #endif
  489. }) {
  490. checker.set_dtype(0, dtype)
  491. .set_dtype(1, dtype)
  492. .set_dtype(2, dtype)
  493. .set_dtype(3, dtype)
  494. .set_dtype(4, dtype);
  495. if (dtype.enumv() == DTypeEnum::Float16)
  496. checker.set_epsilon(2e-2);
  497. for (uint32_t s : {1}) {
  498. for (uint32_t f : {1, 3, 5, 7}) {
  499. cur_param.pad_h = cur_param.pad_w = f / 2;
  500. cur_param.stride_h = cur_param.stride_w = s;
  501. checker.set_param(cur_param).execs({{2, 3, 16, 16},
  502. {3, 1, 1, f, f},
  503. {1, 3, 1, 1},
  504. {},
  505. {}});
  506. }
  507. }
  508. cur_param.pad_h = cur_param.pad_w = 1;
  509. cur_param.stride_h = cur_param.stride_w = 1;
  510. checker.set_param(cur_param)
  511. .execs({{2, 3, 3, 16},
  512. {3, 1, 1, 3, 3},
  513. {1, 3, 1, 1},
  514. {},
  515. {}})
  516. .execs({{2, 3, 8, 3},
  517. {3, 1, 1, 3, 3},
  518. {1, 3, 1, 1},
  519. {},
  520. {}});
  521. }
  522. }
  523. }
  524. TEST_F(CUDA, CONV_BIAS_FORWARD_CHANWISE_8x8x32) {
  525. require_compute_capability(6, 1);
  526. Checker<ConvBiasForward> checker(handle_cuda());
  527. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  528. ConvBiasForward::algo_name<ConvBias::DirectParam>(
  529. "CHANNEL_WISE_8X8X32", {})
  530. .c_str()));
  531. param::ConvBias cur_param;
  532. using NLMode = param::ConvBias::NonlineMode;
  533. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  534. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  535. cur_param.format = ConvBias::Param::Format::NHWC;
  536. UniformIntRNG rng(-4, 4);
  537. checker.set_dtype(0, dtype::Int8{})
  538. .set_dtype(1, dtype::Int8{})
  539. .set_dtype(2, dtype::Int32{})
  540. .set_dtype(4, dtype::Int32{})
  541. .set_rng(0, &rng)
  542. .set_rng(1, &rng)
  543. .set_rng(2, &rng);
  544. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  545. cur_param.nonlineMode = nlmode;
  546. for (uint32_t s : {1, 2}) {
  547. for (uint32_t f : {1, 3, 5, 7}) {
  548. for (uint32_t g : {4, 8}) {
  549. cur_param.pad_h = cur_param.pad_w = f / 2;
  550. cur_param.stride_h = cur_param.stride_w = s;
  551. checker.set_param(cur_param).execs({{2, 9, 16, g},
  552. {g, 1, f, f, 1},
  553. {1, 1, 1, g},
  554. {},
  555. {}});
  556. }
  557. }
  558. }
  559. }
  560. }
  561. TEST_F(CUDA, CONV_BIAS_FORWARD_CUDNN_CONVOLUTION) {
  562. using namespace conv_bias;
  563. std::vector<TestArg> args = get_args();
  564. Checker<ConvBiasForward> checker(handle_cuda());
  565. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  566. ConvBiasForward::algo_name<ConvBias::DefaultParam>(
  567. "CUDNN:Convolution", {})
  568. .c_str()));
  569. NormalRNG default_rng;
  570. for (auto&& arg : args) {
  571. checker.set_dtype(0, dtype::Float32())
  572. .set_dtype(1, dtype::Float32())
  573. .set_dtype(2, dtype::Float32())
  574. .set_rng(0, &default_rng)
  575. .set_rng(1, &default_rng)
  576. .set_rng(2, &default_rng)
  577. .set_epsilon(1e-3)
  578. .set_param(arg.param)
  579. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  580. }
  581. }
  582. TEST_F(CUDA, CONV_BIAS_FORWARD_INPLACE_MATMUL) {
  583. using namespace conv_bias;
  584. std::vector<TestArg> args = get_args();
  585. Checker<ConvBiasForward> checker(handle_cuda());
  586. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  587. ConvBiasForward::algo_name<ConvBias::MatmulParam>("INPLACE_MATMUL",
  588. {})
  589. .c_str()));
  590. param::ConvBias cur_param;
  591. using NLMode = param::ConvBias::NonlineMode;
  592. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  593. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  594. NormalRNG default_rng;
  595. checker.set_dtype(0, dtype::Float32())
  596. .set_dtype(1, dtype::Float32())
  597. .set_dtype(2, dtype::Float32())
  598. .set_rng(0, &default_rng)
  599. .set_rng(1, &default_rng)
  600. .set_rng(2, &default_rng)
  601. .set_epsilon(1e-3);
  602. for (auto nlmode :
  603. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  604. cur_param.nonlineMode = nlmode;
  605. for (uint32_t s : {1}) {
  606. for (uint32_t f : {1, 3, 5, 7}) {
  607. cur_param.pad_h = cur_param.pad_w = f / 2;
  608. cur_param.stride_h = cur_param.stride_w = s;
  609. checker.set_param(cur_param).execs(
  610. {{2, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  611. }
  612. }
  613. cur_param.pad_h = cur_param.pad_w = 1;
  614. cur_param.stride_h = cur_param.stride_w = 1;
  615. checker.set_param(cur_param)
  616. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  617. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  618. }
  619. }
  620. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL) {
  621. using namespace conv_bias;
  622. std::vector<TestArg> args = get_args();
  623. Checker<ConvBiasForward> checker(handle_cuda());
  624. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  625. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>("MATMUL",
  626. {})
  627. .c_str()));
  628. param::ConvBias cur_param;
  629. using NLMode = param::ConvBias::NonlineMode;
  630. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  631. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  632. NormalRNG default_rng;
  633. checker.set_dtype(0, dtype::Float32())
  634. .set_dtype(1, dtype::Float32())
  635. .set_dtype(2, dtype::Float32())
  636. .set_rng(0, &default_rng)
  637. .set_rng(1, &default_rng)
  638. .set_rng(2, &default_rng)
  639. .set_epsilon(1e-3);
  640. for (auto nlmode :
  641. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  642. cur_param.nonlineMode = nlmode;
  643. for (uint32_t s : {1}) {
  644. for (uint32_t f : {1, 3, 5, 7}) {
  645. cur_param.pad_h = cur_param.pad_w = f / 2;
  646. cur_param.stride_h = cur_param.stride_w = s;
  647. checker.set_param(cur_param).execs(
  648. {{2, 4, 16, 16}, {4, 4, f, f}, {1, 4, 1, 1}, {}, {}});
  649. }
  650. }
  651. cur_param.pad_h = cur_param.pad_w = 0;
  652. cur_param.stride_h = cur_param.stride_w = 1;
  653. checker.set_param(cur_param)
  654. .execs({{2, 3, 3, 16}, {5, 3, 3, 3}, {1, 5, 1, 1}, {}, {}})
  655. .execs({{2, 2, 8, 3}, {3, 2, 3, 3}, {1, 3, 1, 1}, {}, {}});
  656. }
  657. }
  658. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_8x8x32) {
  659. require_compute_capability(6, 1);
  660. Checker<ConvBiasForward> checker(handle_cuda());
  661. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  662. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  663. "MATMUL8X8X32", {})
  664. .c_str()));
  665. param::ConvBias cur_param;
  666. using NLMode = param::ConvBias::NonlineMode;
  667. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  668. cur_param.sparse = ConvBias::Param::Sparse::DENSE;
  669. cur_param.format = param::ConvBias::Format::NHWC;
  670. UniformIntRNG rng{-100, 100};
  671. UniformIntRNG bias_rng{-1000, 1000};
  672. checker.set_rng(0, &rng)
  673. .set_rng(1, &rng)
  674. .set_rng(2, &bias_rng)
  675. .set_rng(3, &rng)
  676. .set_dtype(0, dtype::QuantizedS8{1.2f})
  677. .set_dtype(1, dtype::QuantizedS8{1.3f})
  678. .set_dtype(2, dtype::QuantizedS32{1.2f * 1.3f})
  679. .set_dtype(3, dtype::QuantizedS8{1.1f})
  680. .set_dtype(4, dtype::QuantizedS8{1.0f})
  681. .set_epsilon(1);
  682. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  683. cur_param.nonlineMode = nlmode;
  684. for (uint32_t s : {1}) {
  685. for (uint32_t f : {1, 3, 5, 7}) {
  686. cur_param.pad_h = cur_param.pad_w = f / 2;
  687. cur_param.stride_h = cur_param.stride_w = s;
  688. checker.set_param(cur_param).execs(
  689. {{2, 16, 16, 4}, {4, f, f, 4}, {1, 1, 1, 4}, {}, {}});
  690. }
  691. }
  692. cur_param.pad_h = cur_param.pad_w = 0;
  693. cur_param.stride_h = cur_param.stride_w = 1;
  694. checker.set_param(cur_param)
  695. .execs({{2, 3, 16, 3}, {5, 3, 3, 3}, {1, 1, 1, 5}, {}, {}})
  696. .execs({{2, 8, 3, 2}, {3, 3, 3, 2}, {1, 1, 1, 3}, {}, {}});
  697. }
  698. }
  699. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_NCHW4) {
  700. require_compute_capability(6, 1);
  701. Checker<ConvBiasForward> checker(handle_cuda());
  702. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  703. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  704. "MATMUL8X8X32", {})
  705. .c_str()));
  706. UniformIntRNG int_rng{-127, 127};
  707. ConvBias::Param param;
  708. param.format = ConvBias::Param::Format::NCHW4;
  709. using NLMode = ConvBias::Param::NonlineMode;
  710. checker.set_dtype(0, dtype::QuantizedS8(0.5f))
  711. .set_dtype(1, dtype::QuantizedS8(0.5f))
  712. .set_dtype(2, dtype::QuantizedS32(0.25f))
  713. .set_dtype(4, dtype::QuantizedS8(0.35f))
  714. .set_rng(0, &int_rng)
  715. .set_rng(1, &int_rng)
  716. .set_rng(2, &int_rng);
  717. param.sparse = Convolution::Param::Sparse::DENSE;
  718. param.nonlineMode = NLMode::IDENTITY;
  719. param.pad_h = param.pad_w = 1;
  720. param.stride_h = param.stride_w = 1;
  721. checker.set_param(param);
  722. checker.exec(
  723. {{8, 4, 10, 10, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  724. checker.exec({{1, 4, 2, 2, 4}, {16, 4, 3, 3, 4}, {1, 4, 1, 1, 4}, {}, {}});
  725. checker.exec(
  726. {{8, 64, 12, 12, 4}, {256, 64, 3, 3, 4}, {1, 64, 1, 1, 4}, {}, {}});
  727. }
  728. TEST_F(CUDA, CONV_BIAS_FORWARD_MATMUL_1x1) {
  729. using namespace conv_bias;
  730. std::vector<TestArg> args = get_args_1x1();
  731. Checker<ConvBiasForward> checker(handle_cuda());
  732. NormalRNG default_rng;
  733. checker.set_dtype(0, dtype::Float32())
  734. .set_dtype(1, dtype::Float32())
  735. .set_dtype(2, dtype::Float32())
  736. .set_rng(0, &default_rng)
  737. .set_rng(1, &default_rng)
  738. .set_rng(2, &default_rng)
  739. .set_epsilon(1e-3);
  740. for (auto&& arg : args) {
  741. checker.set_param(arg.param);
  742. checker.set_before_exec_callback(
  743. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  744. ConvBiasForward::algo_name<
  745. ConvBiasForward::MatmulParam>("MATMUL1X1", {})
  746. .c_str()));
  747. checker.execs({arg.src, arg.filter, arg.bias, {}, {}});
  748. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<
  749. ConvBias>(
  750. ConvBiasForward::algo_name<ConvBiasForward::MatmulParam>(
  751. "BATCHEDMATMUL", {})
  752. .c_str()));
  753. checker.execs({arg.src, arg.filter, arg.bias, {}, {}});
  754. }
  755. }
  756. TEST_F(CUDA, CONV_BIAS_FORWARD_GROUP) {
  757. using NLMode = ConvBias::Param::NonlineMode;
  758. bool is_int_available = false;
  759. if (megdnn::test::check_compute_capability(6, 1)) {
  760. is_int_available = true;
  761. } else {
  762. is_int_available = false;
  763. }
  764. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  765. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  766. size_t SW, size_t DH, size_t DW, size_t group, NLMode mode) {
  767. {
  768. // float case
  769. Checker<ConvBiasForward> checker(handle_cuda());
  770. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<
  771. ConvBias>(
  772. ConvBiasForward::algo_name<ConvBiasForward::DirectParam>(
  773. "CUDA:GROUP_CONV", {})
  774. .c_str()));
  775. ConvBias::Param param;
  776. param.sparse = ConvBias::Param::Sparse::GROUP;
  777. param.nonlineMode = mode;
  778. param.pad_h = PH;
  779. param.pad_w = PW;
  780. param.stride_h = SH;
  781. param.stride_w = SW;
  782. param.dilate_h = DH;
  783. param.dilate_w = DW;
  784. auto ICg = IC / group;
  785. auto OCg = OC / group;
  786. checker.set_param(param).exec({{N, IC, IH, IW},
  787. {group, OCg, ICg, FH, FW},
  788. {1, OCg * group, 1, 1},
  789. {},
  790. {}});
  791. }
  792. if (is_int_available) {
  793. // int 8x8x32 case
  794. Checker<ConvBiasForward> checker(handle_cuda());
  795. ConvBias::Param param;
  796. param.sparse = Convolution::Param::Sparse::GROUP;
  797. param.format = Convolution::Param::Format::NHWC;
  798. param.nonlineMode = NLMode::IDENTITY;
  799. param.pad_h = PH;
  800. param.pad_w = PW;
  801. param.stride_h = SH;
  802. param.stride_w = SW;
  803. param.dilate_h = DH;
  804. param.dilate_w = DW;
  805. auto ICg = IC / group;
  806. auto OCg = OC / group;
  807. UniformIntRNG rng(-4, 4);
  808. checker.set_param(param)
  809. .set_dtype(0, dtype::QuantizedS8(0.5f))
  810. .set_dtype(1, dtype::QuantizedS8(0.5f))
  811. .set_dtype(2, dtype::QuantizedS32(0.25f))
  812. .set_dtype(3, dtype::QuantizedS8(0.13f))
  813. .set_dtype(4, dtype::QuantizedS8(0.35f))
  814. .set_rng(0, &rng)
  815. .set_rng(1, &rng)
  816. .set_rng(2, &rng)
  817. .exec({{N, IH, IW, IC},
  818. {group, OCg, FH, FW, ICg},
  819. {1, 1, 1, OCg * group},
  820. {},
  821. {}});
  822. }
  823. };
  824. for (NLMode nlmode :
  825. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  826. // normal case
  827. run(2, 64, 7, 7, 3, 3, 32, 0, 0, 1, 1, 1, 1, 2, nlmode);
  828. // padded case
  829. run(2, 32, 7, 7, 3, 3, 64, 1, 1, 1, 1, 1, 1, 4, nlmode);
  830. // strided case
  831. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 2, 2, 1, 1, 8, nlmode);
  832. // dilated case
  833. run(2, 32, 7, 7, 3, 3, 64, 0, 0, 1, 1, 2, 2, 8, nlmode);
  834. }
  835. }
  836. #if CUDA_VERSION >= 10000
  837. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_1) {
  838. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 3);
  839. }
  840. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_2) {
  841. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 5);
  842. }
  843. TEST_F(CUDA, CONV_BIAS_FORWARD_NCHW8_PART_3) {
  844. test_conv_bias_forward_wmma_int4_nchw8(handle_cuda(), 7);
  845. }
  846. #if MEGDNN_WITH_BENCHMARK
  847. TEST_F(CUDA, BENCHMARK_CONV_BIAS_QUANTIZED4x4x32) {
  848. require_compute_capability(7, 5);
  849. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  850. UniformIntRNG int_rng{0, 8};
  851. ConvBias::Param param;
  852. param.format = ConvBias::Param::Format::NCHW8;
  853. param.stride_h = param.stride_w = 1;
  854. using NonlineMode = ConvBias::Param::NonlineMode;
  855. param.nonlineMode = NonlineMode::RELU;
  856. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  857. size_t co, size_t fh, size_t fw, size_t nr_times) {
  858. param.pad_h = fh / 2;
  859. param.pad_w = fw / 2;
  860. bencher.set_param(param)
  861. .set_dtype(0, dtype::Quantized4Asymm(1.3f, (uint8_t)(1)))
  862. .set_dtype(1, dtype::Quantized4Asymm(1.3f, (uint8_t)(2)))
  863. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  864. .set_dtype(4, dtype::QuantizedS32(1.3f * 1.3f))
  865. .set_rng(0, &int_rng)
  866. .set_rng(1, &int_rng)
  867. .set_rng(2, &int_rng);
  868. bencher.set_times(nr_times);
  869. size_t ho = infer_conv_shape(hi, fh, 1, param.pad_h);
  870. size_t wo = infer_conv_shape(wi, fw, 1, param.pad_w);
  871. TensorShape inp{batch, ci / 8, hi, wi, 8}, kern{co, ci / 8, fh, fw, 8},
  872. out{batch, co / 8, ho, wo, 8};
  873. auto time_in_ms =
  874. bencher.execs({inp, kern, {1, co / 8, 1, 1, 8}, {}, out}) /
  875. nr_times;
  876. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  877. (time_in_ms * 1e-3) * 1e-12;
  878. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops\n",
  879. inp.to_string().c_str(), kern.to_string().c_str(),
  880. out.to_string().c_str(), time_in_ms, ops);
  881. };
  882. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  883. run_bench(1, 32, 224, 224, 64, 7, 7, 1000);
  884. run_bench(1, 8192, 64, 64, 4096, 3, 3, 1000);
  885. run_bench(1, 256, 64, 64, 256, 3, 3, 1000);
  886. run_bench(1, 64, 128, 128, 64, 3, 3, 1000);
  887. run_bench(1, 512, 32, 32, 512, 3, 3, 1000);
  888. run_bench(1, 1024, 16, 16, 1024, 3, 3, 1000);
  889. run_bench(1, 64, 56, 56, 64, 3, 3, 1000);
  890. run_bench(1, 128, 32, 32, 128, 3, 3, 1000);
  891. run_bench(1, 256, 16, 16, 256, 3, 3, 1000);
  892. run_bench(1, 512, 8, 8, 512, 3, 3, 1000);
  893. run_bench(32, 32, 224, 224, 64, 7, 7, 1000);
  894. run_bench(32, 64, 56, 56, 64, 3, 3, 1000);
  895. run_bench(32, 128, 32, 32, 128, 3, 3, 1000);
  896. run_bench(32, 256, 16, 16, 256, 3, 3, 1000);
  897. run_bench(32, 512, 8, 8, 512, 3, 3, 1000);
  898. run_bench(256, 32, 224, 224, 64, 7, 7, 1000);
  899. run_bench(256, 64, 56, 56, 64, 3, 3, 1000);
  900. run_bench(256, 128, 32, 32, 128, 3, 3, 1000);
  901. run_bench(256, 256, 16, 16, 256, 3, 3, 1000);
  902. run_bench(256, 512, 8, 8, 512, 3, 3, 1000);
  903. }
  904. #endif
  905. #endif
  906. TEST_F(CUDA, CONV_BIAS_FORWARD_DILATED) {
  907. require_compute_capability(6, 0);
  908. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW, size_t FH,
  909. size_t FW, size_t OC, size_t PH, size_t PW, size_t SH,
  910. size_t SW, size_t DH, size_t DW) {
  911. {
  912. // float case
  913. Checker<ConvBiasForward> checker(handle_cuda());
  914. ConvBias::Param param;
  915. param.sparse = ConvBias::Param::Sparse::DENSE;
  916. param.pad_h = PH;
  917. param.pad_w = PW;
  918. param.stride_h = SH;
  919. param.stride_w = SW;
  920. param.dilate_h = DH;
  921. param.dilate_w = DW;
  922. param.nonlineMode = ConvBias::Param::NonlineMode::IDENTITY;
  923. checker.set_param(param).exec(
  924. {{N, IC, IH, IW}, {OC, IC, FH, FW}, {1, OC, 1, 1}, {}, {}});
  925. }
  926. };
  927. // dilated case
  928. run(2, 8, 7, 7, 3, 3, 4, 0, 0, 1, 1, 2, 2);
  929. }
  930. #if CUDNN_VERSION >= 7500
  931. TEST_F(CUDA, CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  932. require_compute_capability(7, 5);
  933. using namespace conv_bias;
  934. Checker<ConvBiasForward> checker(handle_cuda());
  935. auto opr = handle_cuda()->create_operator<ConvBias>();
  936. auto i8_min = std::numeric_limits<int8_t>().min();
  937. auto i8_max = std::numeric_limits<int8_t>().max();
  938. UniformIntRNG int_rng{i8_min, i8_max};
  939. ConvBias::Param param;
  940. param.format = ConvBias::Param::Format::NCHW32;
  941. using NonlineMode = ConvBias::Param::NonlineMode;
  942. for (NonlineMode mode : {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  943. for (size_t batch : {2}) {
  944. for (size_t ic : {64, 32}) {
  945. for (size_t oc : {32}) {
  946. for (size_t fh : {3, 5, 7}) {
  947. for (int ph : {static_cast<int>(fh / 2), 0}) {
  948. for (int sh : {1, 2}) {
  949. for (size_t ih : {9, 11, 12, 13, 16}) {
  950. for (size_t iw : {8, 27, 32, 40}) {
  951. param.nonlineMode = mode;
  952. param.stride_h = param.stride_w = sh;
  953. param.pad_h = param.pad_w = ph;
  954. opr->param() = param;
  955. TensorLayout dst_layout;
  956. opr->deduce_layout(
  957. {{batch, ic / 32, ih, iw, 32},
  958. dtype::Float32()},
  959. {{oc, ic / 32, fh, fh, 32},
  960. dtype::Float32()},
  961. {}, {}, dst_layout);
  962. checker.set_dtype(0, dtype::QuantizedS8(
  963. 1.3f))
  964. .set_dtype(1,
  965. dtype::QuantizedS8(
  966. 1.3f))
  967. .set_dtype(2,
  968. dtype::QuantizedS32(
  969. 1.3f * 1.3f))
  970. .set_dtype(3,
  971. dtype::QuantizedS8(
  972. 1.7f))
  973. .set_dtype(4,
  974. dtype::QuantizedS8(
  975. 1.2f * 1.2f))
  976. .set_rng(0, &int_rng)
  977. .set_rng(1, &int_rng)
  978. .set_rng(2, &int_rng)
  979. .set_rng(3, &int_rng)
  980. .set_epsilon(1 + 1e-3)
  981. .set_param(param)
  982. .execs({{batch, ic / 32, ih, iw,
  983. 32},
  984. {oc, ic / 32, fh, fh,
  985. 32},
  986. {1, oc / 32, 1, 1, 32},
  987. dst_layout,
  988. {}});
  989. }
  990. }
  991. }
  992. }
  993. }
  994. }
  995. }
  996. }
  997. }
  998. }
  999. #if MEGDNN_WITH_BENCHMARK
  1000. TEST_F(CUDA, BENCHMARK_CONV_BIAS_FORWARD_TENSORCORE_INT8) {
  1001. require_compute_capability(7, 5);
  1002. Benchmarker<ConvBiasForward> bencher(handle_cuda());
  1003. bencher.set_display(false);
  1004. ConvBias::Param param;
  1005. param.format = ConvBias::Param::Format::NCHW32;
  1006. ConvBias::Param param_without_tensorcore;
  1007. param_without_tensorcore.format = ConvBias::Param::Format::NCHW4;
  1008. auto i8_min = std::numeric_limits<int8_t>().min();
  1009. auto i8_max = std::numeric_limits<int8_t>().max();
  1010. UniformIntRNG int_rng{i8_min, i8_max};
  1011. using NonlineMode = ConvBias::Param::NonlineMode;
  1012. param.nonlineMode = NonlineMode::IDENTITY;
  1013. auto run_bench = [&](size_t batch, size_t ci, size_t hi, size_t wi,
  1014. size_t co, size_t fh, size_t fw, size_t sh, size_t sw,
  1015. size_t nr_times) {
  1016. param.pad_h = fh / 2;
  1017. param.pad_w = fw / 2;
  1018. param.stride_h = sh;
  1019. param.stride_w = sw;
  1020. param_without_tensorcore.pad_h = fh / 2;
  1021. param_without_tensorcore.pad_w = fw / 2;
  1022. param_without_tensorcore.stride_h = sh;
  1023. param_without_tensorcore.stride_w = sw;
  1024. bencher.set_param(param)
  1025. .set_dtype(0, dtype::QuantizedS8(1.3f))
  1026. .set_dtype(1, dtype::QuantizedS8(1.3f))
  1027. .set_dtype(2, dtype::QuantizedS32(1.3f * 1.3f))
  1028. .set_dtype(4, dtype::QuantizedS8(1.2f))
  1029. .set_rng(0, &int_rng)
  1030. .set_rng(1, &int_rng)
  1031. .set_rng(2, &int_rng);
  1032. bencher.set_times(nr_times);
  1033. size_t ho = infer_conv_shape(hi, fh, sh, param.pad_h);
  1034. size_t wo = infer_conv_shape(wi, fw, sw, param.pad_w);
  1035. TensorShape inp{batch, ci / 32, hi, wi, 32},
  1036. kern{co, ci / 32, fh, fw, 32}, out{batch, co / 32, ho, wo, 32};
  1037. auto time_in_ms =
  1038. bencher.execs({inp, kern, {1, co / 32, 1, 1, 32}, {}, out}) /
  1039. nr_times;
  1040. auto ops = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1041. (time_in_ms * 1e-3) * 1e-12;
  1042. printf("inp=%s, kern=%s, out=%s, time: %.2fms, perf: %.2f Tops "
  1043. "(TensorCore)",
  1044. inp.to_string().c_str(), kern.to_string().c_str(),
  1045. out.to_string().c_str(), time_in_ms, ops);
  1046. decltype(ops) ops_without_tensorcore;
  1047. bencher.set_param(param_without_tensorcore);
  1048. {
  1049. TensorShape inp{batch, ci / 4, hi, wi, 4},
  1050. kern{co, ci / 4, fh, fw, 4}, out{batch, co / 4, ho, wo, 4};
  1051. auto time_in_ms =
  1052. bencher.execs({inp, kern, {1, co / 4, 1, 1, 4}, {}, out}) /
  1053. nr_times;
  1054. ops_without_tensorcore = 2.0 * batch * co * ho * wo * ci * fh * fw /
  1055. (time_in_ms * 1e-3) * 1e-12;
  1056. printf(", time: %.2fms perf: %.2f Tops (without TensorCore) ",
  1057. time_in_ms, ops_without_tensorcore);
  1058. }
  1059. printf("speedup: %.2fx\n", ops / ops_without_tensorcore);
  1060. };
  1061. // resnet-50
  1062. // bottleneck-1
  1063. // proj
  1064. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1065. run_bench(1, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1066. run_bench(1, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1067. run_bench(1, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1068. // bottleneck-2
  1069. // proj
  1070. run_bench(1, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1071. run_bench(1, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1072. run_bench(1, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1073. run_bench(1, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1074. // bottleneck-3
  1075. // proj
  1076. run_bench(1, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1077. run_bench(1, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1078. run_bench(1, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1079. run_bench(1, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1080. // bottleneck-4
  1081. // proj
  1082. run_bench(1, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1083. run_bench(1, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1084. run_bench(1, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1085. run_bench(1, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1086. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1087. run_bench(32, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1088. run_bench(32, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1089. run_bench(32, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1090. run_bench(32, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1091. run_bench(32, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1092. run_bench(32, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1093. run_bench(32, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1094. run_bench(32, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1095. run_bench(32, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1096. run_bench(32, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1097. run_bench(32, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1098. run_bench(32, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1099. run_bench(32, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1100. run_bench(32, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1101. run_bench(32, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1102. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1103. run_bench(256, 64, 56, 56, 64, 1, 1, 1, 1, 1000);
  1104. run_bench(256, 64, 56, 56, 64, 3, 3, 1, 1, 1000);
  1105. run_bench(256, 64, 56, 56, 256, 1, 1, 1, 1, 1000);
  1106. run_bench(256, 256, 56, 56, 512, 1, 1, 2, 2, 1000);
  1107. run_bench(256, 256, 56, 56, 128, 1, 1, 2, 2, 1000);
  1108. run_bench(256, 128, 28, 28, 128, 3, 3, 1, 1, 1000);
  1109. run_bench(256, 128, 28, 28, 512, 1, 1, 1, 1, 1000);
  1110. run_bench(256, 512, 28, 28, 1024, 1, 1, 2, 2, 1000);
  1111. run_bench(256, 512, 28, 28, 256, 1, 1, 2, 2, 1000);
  1112. run_bench(256, 256, 14, 14, 256, 3, 3, 1, 1, 1000);
  1113. run_bench(256, 256, 14, 14, 1024, 1, 1, 1, 1, 1000);
  1114. run_bench(256, 1024, 14, 14, 2048, 1, 1, 2, 2, 1000);
  1115. run_bench(256, 1024, 14, 14, 512, 1, 1, 2, 2, 1000);
  1116. run_bench(256, 512, 7, 7, 512, 3, 3, 1, 1, 1000);
  1117. run_bench(256, 512, 7, 7, 2048, 1, 1, 1, 1, 1000);
  1118. }
  1119. #endif
  1120. #endif
  1121. // vim: syntax=cpp.doxygen

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