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

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

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