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

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