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

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

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