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

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

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