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

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