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

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

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