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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. /**
  2. * \file dnn/test/x86/conv_bias.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "src/x86/utils.h"
  12. #include "test/x86/fixture.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "megdnn/oprs.h"
  15. #include "test/common/benchmarker.h"
  16. #include "test/common/checker.h"
  17. #include "test/common/conv_bias.h"
  18. #include "test/common/rng.h"
  19. #include "test/common/tensor.h"
  20. #include "test/common/workspace_wrapper.h"
  21. namespace megdnn {
  22. namespace test {
  23. TEST_F(X86, CONV_BIAS_FORWARD) {
  24. using namespace conv_bias;
  25. std::vector<TestArg> args = get_args();
  26. Checker<ConvBiasForward> checker(handle());
  27. NormalRNG default_rng;
  28. ConstValue const_val;
  29. for (auto&& arg : args) {
  30. checker.set_dtype(0, dtype::Float32())
  31. .set_dtype(1, dtype::Float32())
  32. .set_dtype(2, dtype::Float32())
  33. .set_rng(0, &default_rng)
  34. .set_rng(1, &default_rng)
  35. .set_rng(2, &default_rng)
  36. .set_epsilon(1e-3)
  37. .set_param(arg.param)
  38. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  39. }
  40. }
  41. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_INT8x8x32) {
  42. using namespace conv_bias;
  43. std::vector<TestArg> args;
  44. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  45. size_t p, NonlineMode nonline_mode) {
  46. if (w + 2 * p < kernel || h + 2 * p < kernel)
  47. return;
  48. param::ConvBias param;
  49. param.stride_h = 1;
  50. param.stride_w = 1;
  51. param.pad_h = p;
  52. param.pad_w = p;
  53. param.nonlineMode = nonline_mode;
  54. param.sparse = param::ConvBias::Sparse::DENSE;
  55. //! no bias
  56. args.emplace_back(param, TensorShape{2, ic, h, w},
  57. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  58. param.sparse = param::ConvBias::Sparse::GROUP;
  59. //! no bias
  60. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  61. TensorShape{2, oc / 2, ic, kernel, kernel},
  62. TensorShape{});
  63. };
  64. for (size_t kernel : {2, 3, 5, 7})
  65. for (size_t pad : {0, 1})
  66. for (size_t oc : {4, 8, 13, 16, 24})
  67. for (size_t ic : {2, 3, 7, 10})
  68. for (size_t h : {10, 11})
  69. for (size_t w : {8, 10})
  70. for (NonlineMode nonline_mode :
  71. {NonlineMode::IDENTITY})
  72. run(oc, ic, w, h, kernel, pad, nonline_mode);
  73. Checker<ConvBias> checker(handle());
  74. UniformIntRNG rng{-50, 50};
  75. checker.set_dtype(0, dtype::Int8())
  76. .set_dtype(1, dtype::Int8())
  77. .set_dtype(2, dtype::Int32())
  78. .set_dtype(4, dtype::Int32())
  79. .set_rng(0, &rng)
  80. .set_rng(1, &rng)
  81. .set_rng(2, &rng)
  82. .set_epsilon(1e-3);
  83. checker.set_before_exec_callback(
  84. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  85. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
  86. for (auto&& arg : args) {
  87. checker.set_param(arg.param).exec(
  88. {arg.src, arg.filter, arg.bias, {}, {}});
  89. }
  90. }
  91. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_QuantizedS32) {
  92. using namespace conv_bias;
  93. std::vector<TestArg> args;
  94. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  95. size_t p, NonlineMode nonline_mode) {
  96. if (w + 2 * p < kernel || h + 2 * p < kernel)
  97. return;
  98. param::ConvBias param;
  99. param.stride_h = 1;
  100. param.stride_w = 1;
  101. param.pad_h = p;
  102. param.pad_w = p;
  103. param.nonlineMode = nonline_mode;
  104. param.sparse = param::ConvBias::Sparse::DENSE;
  105. //! no bias
  106. args.emplace_back(param, TensorShape{2, ic, h, w},
  107. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  108. param.sparse = param::ConvBias::Sparse::GROUP;
  109. //! no bias
  110. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  111. TensorShape{2, oc / 2, ic, kernel, kernel},
  112. TensorShape{});
  113. };
  114. for (size_t kernel : {2, 3, 5, 7})
  115. for (size_t pad : {0, 1})
  116. for (size_t oc : {4, 8, 13, 16, 24})
  117. for (size_t ic : {2, 3, 7, 10})
  118. for (size_t h : {10, 11})
  119. for (size_t w : {8, 10})
  120. for (NonlineMode nonline_mode :
  121. {NonlineMode::IDENTITY})
  122. run(oc, ic, w, h, kernel, pad, nonline_mode);
  123. Checker<ConvBias> checker(handle());
  124. UniformIntRNG rng{-50, 50};
  125. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  126. .set_dtype(1, dtype::QuantizedS8(2.5f))
  127. .set_dtype(2, dtype::QuantizedS32(6.25f))
  128. .set_dtype(4, {})
  129. .set_rng(0, &rng)
  130. .set_rng(1, &rng)
  131. .set_rng(2, &rng)
  132. .set_epsilon(1e-3);
  133. checker.set_before_exec_callback(
  134. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  135. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
  136. for (auto&& arg : args) {
  137. checker.set_param(arg.param).exec(
  138. {arg.src, arg.filter, arg.bias, {}, {}});
  139. }
  140. }
  141. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_S8S8S8) {
  142. using namespace conv_bias;
  143. std::vector<TestArg> args;
  144. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  145. size_t p, NonlineMode nonline_mode) {
  146. if (w + 2 * p < kernel || h + 2 * p < kernel)
  147. return;
  148. param::ConvBias param;
  149. param.stride_h = 1;
  150. param.stride_w = 1;
  151. param.pad_h = p;
  152. param.pad_w = p;
  153. param.nonlineMode = nonline_mode;
  154. param.sparse = param::ConvBias::Sparse::DENSE;
  155. //! no bias
  156. args.emplace_back(param, TensorShape{1, ic, h, w},
  157. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  158. //! bias channel
  159. args.emplace_back(param, TensorShape{1, ic, h, w},
  160. TensorShape{oc, ic, kernel, kernel},
  161. TensorShape{1, oc, 1, 1});
  162. param.sparse = param::ConvBias::Sparse::GROUP;
  163. //! no bias
  164. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  165. TensorShape{2, oc / 2, ic, kernel, kernel},
  166. TensorShape{});
  167. //! bias channel
  168. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  169. TensorShape{2, oc / 2, ic, kernel, kernel},
  170. TensorShape{1, oc, 1, 1});
  171. };
  172. for (size_t kernel : {2, 3, 5, 7})
  173. for (size_t pad : {0, 1})
  174. for (size_t oc : {4, 8, 14, 16, 24})
  175. for (size_t ic : {2, 3, 7, 10})
  176. for (size_t h : {10, 11})
  177. for (size_t w : {8, 10})
  178. for (NonlineMode nonline_mode :
  179. {NonlineMode::IDENTITY, NonlineMode::RELU,
  180. NonlineMode::H_SWISH})
  181. run(oc, ic, w, h, kernel, pad, nonline_mode);
  182. Checker<ConvBias> checker(handle());
  183. UniformIntRNG rng{-50, 50};
  184. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  185. .set_dtype(1, dtype::QuantizedS8(2.5f))
  186. .set_dtype(2, dtype::QuantizedS32(6.25f))
  187. .set_dtype(4, dtype::QuantizedS8(60.25f))
  188. .set_rng(0, &rng)
  189. .set_rng(1, &rng)
  190. .set_rng(2, &rng)
  191. .set_epsilon(1e-3);
  192. checker.set_before_exec_callback(
  193. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  194. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
  195. for (auto&& arg : args) {
  196. checker.set_param(arg.param).exec(
  197. {arg.src, arg.filter, arg.bias, {}, {}});
  198. }
  199. }
  200. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_INT8x8x32) {
  201. using namespace conv_bias;
  202. std::vector<TestArg> args;
  203. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  204. size_t p, NonlineMode nonline_mode) {
  205. if (w + 2 * p < kernel || h + 2 * p < kernel)
  206. return;
  207. param::ConvBias param;
  208. param.stride_h = 2;
  209. param.stride_w = 2;
  210. param.pad_h = p;
  211. param.pad_w = p;
  212. param.nonlineMode = nonline_mode;
  213. param.sparse = param::ConvBias::Sparse::DENSE;
  214. //! no bias
  215. args.emplace_back(param, TensorShape{2, ic, h, w},
  216. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  217. param.sparse = param::ConvBias::Sparse::GROUP;
  218. //! no bias
  219. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  220. TensorShape{2, oc / 2, ic, kernel, kernel},
  221. TensorShape{});
  222. };
  223. for (size_t kernel : {2, 3, 5, 7})
  224. for (size_t pad : {0, 1, 2, 5})
  225. for (size_t oc : {4, 8, 13, 16, 24})
  226. for (size_t ic : {2, 3, 7, 10})
  227. for (size_t h : {10, 11})
  228. for (size_t w : {8, 10, 20})
  229. for (NonlineMode nonline_mode :
  230. {NonlineMode::IDENTITY})
  231. run(oc, ic, w, h, kernel, pad, nonline_mode);
  232. Checker<ConvBias> checker(handle());
  233. UniformIntRNG rng{-50, 50};
  234. checker.set_dtype(0, dtype::Int8())
  235. .set_dtype(1, dtype::Int8())
  236. .set_dtype(2, dtype::Int32())
  237. .set_dtype(4, dtype::Int32())
  238. .set_rng(0, &rng)
  239. .set_rng(1, &rng)
  240. .set_rng(2, &rng)
  241. .set_epsilon(1e-3);
  242. checker.set_before_exec_callback(
  243. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  244. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
  245. for (auto&& arg : args) {
  246. checker.set_param(arg.param).exec(
  247. {arg.src, arg.filter, arg.bias, {}, {}});
  248. }
  249. }
  250. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_QuantizedS32) {
  251. using namespace conv_bias;
  252. std::vector<TestArg> args;
  253. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  254. size_t p, NonlineMode nonline_mode) {
  255. if (w + 2 * p < kernel || h + 2 * p < kernel)
  256. return;
  257. param::ConvBias param;
  258. param.stride_h = 2;
  259. param.stride_w = 2;
  260. param.pad_h = p;
  261. param.pad_w = p;
  262. param.nonlineMode = nonline_mode;
  263. param.sparse = param::ConvBias::Sparse::DENSE;
  264. //! no bias
  265. args.emplace_back(param, TensorShape{2, ic, h, w},
  266. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  267. param.sparse = param::ConvBias::Sparse::GROUP;
  268. //! no bias
  269. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  270. TensorShape{2, oc / 2, ic, kernel, kernel},
  271. TensorShape{});
  272. };
  273. for (size_t kernel : {2, 3, 5, 7})
  274. for (size_t pad : {0, 1, 3, 5})
  275. for (size_t oc : {4, 8, 13, 16, 24})
  276. for (size_t ic : {2, 3, 7, 10})
  277. for (size_t h : {10, 11})
  278. for (size_t w : {8, 10, 19})
  279. for (NonlineMode nonline_mode :
  280. {NonlineMode::IDENTITY})
  281. run(oc, ic, w, h, kernel, pad, nonline_mode);
  282. Checker<ConvBias> checker(handle());
  283. UniformIntRNG rng{-50, 50};
  284. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  285. .set_dtype(1, dtype::QuantizedS8(2.5f))
  286. .set_dtype(2, dtype::QuantizedS32(6.25f))
  287. .set_dtype(4, {})
  288. .set_rng(0, &rng)
  289. .set_rng(1, &rng)
  290. .set_rng(2, &rng)
  291. .set_epsilon(1e-3);
  292. checker.set_before_exec_callback(
  293. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  294. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
  295. for (auto&& arg : args) {
  296. checker.set_param(arg.param).exec(
  297. {arg.src, arg.filter, arg.bias, {}, {}});
  298. }
  299. }
  300. TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_S8S8S8) {
  301. using namespace conv_bias;
  302. std::vector<TestArg> args;
  303. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  304. size_t p, NonlineMode nonline_mode) {
  305. if (w + 2 * p < kernel || h + 2 * p < kernel)
  306. return;
  307. param::ConvBias param;
  308. param.stride_h = 2;
  309. param.stride_w = 2;
  310. param.pad_h = p;
  311. param.pad_w = p;
  312. param.nonlineMode = nonline_mode;
  313. param.sparse = param::ConvBias::Sparse::DENSE;
  314. //! no bias
  315. args.emplace_back(param, TensorShape{1, ic, h, w},
  316. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  317. //! bias channel
  318. args.emplace_back(param, TensorShape{1, ic, h, w},
  319. TensorShape{oc, ic, kernel, kernel},
  320. TensorShape{1, oc, 1, 1});
  321. param.sparse = param::ConvBias::Sparse::GROUP;
  322. //! no bias
  323. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  324. TensorShape{2, oc / 2, ic, kernel, kernel},
  325. TensorShape{});
  326. //! bias channel
  327. args.emplace_back(param, TensorShape{2, 2 * ic, h, w},
  328. TensorShape{2, oc / 2, ic, kernel, kernel},
  329. TensorShape{1, oc, 1, 1});
  330. };
  331. for (size_t kernel : {2, 3, 5, 7})
  332. for (size_t pad : {0, 1, 3, 5})
  333. for (size_t oc : {4, 8, 14, 16, 24})
  334. for (size_t ic : {2, 3, 7, 10})
  335. for (size_t h : {10, 11})
  336. for (size_t w : {8, 10, 18})
  337. for (NonlineMode nonline_mode :
  338. {NonlineMode::IDENTITY, NonlineMode::RELU,
  339. NonlineMode::H_SWISH})
  340. run(oc, ic, w, h, kernel, pad, nonline_mode);
  341. Checker<ConvBias> checker(handle());
  342. UniformIntRNG rng{-50, 50};
  343. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  344. .set_dtype(1, dtype::QuantizedS8(2.5f))
  345. .set_dtype(2, dtype::QuantizedS32(6.25f))
  346. .set_dtype(4, dtype::QuantizedS8(60.25f))
  347. .set_rng(0, &rng)
  348. .set_rng(1, &rng)
  349. .set_rng(2, &rng)
  350. .set_epsilon(1e-3);
  351. checker.set_before_exec_callback(
  352. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  353. "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
  354. for (auto&& arg : args) {
  355. checker.set_param(arg.param).exec(
  356. {arg.src, arg.filter, arg.bias, {}, {}});
  357. }
  358. }
  359. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP) {
  360. using namespace conv_bias;
  361. std::vector<TestArg> args;
  362. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  363. size_t p, NonlineMode nonline_mode) {
  364. if (w + 2 * p < kernel || h + 2 * p < kernel)
  365. return;
  366. param::ConvBias param;
  367. param.stride_h = 1;
  368. param.stride_w = 1;
  369. param.pad_h = p;
  370. param.pad_w = p;
  371. param.nonlineMode = nonline_mode;
  372. //! no bias
  373. args.emplace_back(param, TensorShape{1, ic, h, w},
  374. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  375. //! bias channel
  376. args.emplace_back(param, TensorShape{2, ic, h, w},
  377. TensorShape{oc, ic, kernel, kernel},
  378. TensorShape{1, oc, 1, 1});
  379. //! bias
  380. args.emplace_back(param, TensorShape{2, ic, h, w},
  381. TensorShape{oc, ic, kernel, kernel},
  382. TensorShape{2, oc, (h + param.pad_h * 2 - kernel) + 1,
  383. (w + param.pad_w * 2 - kernel) + 1});
  384. };
  385. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  386. for (size_t ic : {1, 4, 8, 16})
  387. for (size_t oc : {1, 4, 8})
  388. for (size_t p : {0, 2})
  389. for (size_t size : {20, 21, 24})
  390. for (NonlineMode nonline_mode :
  391. {NonlineMode::RELU, NonlineMode::SIGMOID,
  392. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  393. run(oc, ic, size, size, kernel, p, nonline_mode);
  394. }
  395. Checker<ConvBias> checker(handle());
  396. UniformIntRNG rng{-50, 50};
  397. checker.set_dtype(0, dtype::Float32())
  398. .set_dtype(1, dtype::Float32())
  399. .set_dtype(2, dtype::Float32())
  400. .set_rng(0, &rng)
  401. .set_rng(1, &rng)
  402. .set_rng(2, &rng);
  403. checker.set_before_exec_callback(
  404. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  405. "X86_CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP"));
  406. for (auto&& arg : args) {
  407. checker.set_param(arg.param).exec(
  408. {arg.src, arg.filter, arg.bias, {}, {}});
  409. }
  410. }
  411. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP) {
  412. using namespace conv_bias;
  413. std::vector<TestArg> args;
  414. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  415. size_t p, NonlineMode nonline_mode) {
  416. if (w + 2 * p < kernel || h + 2 * p < kernel)
  417. return;
  418. param::ConvBias param;
  419. param.stride_h = 1;
  420. param.stride_w = 1;
  421. param.pad_h = p;
  422. param.pad_w = p;
  423. param.nonlineMode = nonline_mode;
  424. //! no bias
  425. args.emplace_back(param, TensorShape{1, ic, h, w},
  426. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  427. //! bias channel
  428. args.emplace_back(param, TensorShape{2, ic, h, w},
  429. TensorShape{oc, ic, kernel, kernel},
  430. TensorShape{1, oc, 1, 1});
  431. //! bias
  432. args.emplace_back(param, TensorShape{2, ic, h, w},
  433. TensorShape{oc, ic, kernel, kernel},
  434. TensorShape{2, oc, (h + param.pad_h * 2 - kernel) + 1,
  435. (w + param.pad_w * 2 - kernel) + 1});
  436. };
  437. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  438. for (size_t ic : {1, 4, 8, 16})
  439. for (size_t oc : {1, 4, 8})
  440. for (size_t p : {0, 2})
  441. for (size_t size : {20, 21, 24})
  442. for (NonlineMode nonline_mode :
  443. {NonlineMode::RELU, NonlineMode::SIGMOID,
  444. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  445. run(oc, ic, size, size, kernel, p, nonline_mode);
  446. }
  447. Checker<ConvBias> checker(handle());
  448. UniformIntRNG rng{-50, 50};
  449. checker.set_dtype(0, dtype::Float32())
  450. .set_dtype(1, dtype::Float32())
  451. .set_dtype(2, dtype::Float32())
  452. .set_rng(0, &rng)
  453. .set_rng(1, &rng)
  454. .set_rng(2, &rng);
  455. checker.set_before_exec_callback(
  456. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  457. "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP"));
  458. for (auto&& arg : args) {
  459. checker.set_param(arg.param).exec(
  460. {arg.src, arg.filter, arg.bias, {}, {}});
  461. }
  462. }
  463. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE2) {
  464. using namespace conv_bias;
  465. std::vector<TestArg> args;
  466. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  467. size_t p, NonlineMode nonline_mode) {
  468. if (w + 2 * p < kernel || h + 2 * p < kernel)
  469. return;
  470. param::ConvBias param;
  471. param.stride_h = 2;
  472. param.stride_w = 2;
  473. param.pad_h = p;
  474. param.pad_w = p;
  475. param.nonlineMode = nonline_mode;
  476. //! no bias
  477. args.emplace_back(param, TensorShape{1, ic, h, w},
  478. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  479. };
  480. for (size_t kernel : {2, 3, 5, 7})
  481. for (size_t ic : {1, 4, 8, 16})
  482. for (size_t oc : {1, 4, 8})
  483. for (size_t p : {0, 2})
  484. for (size_t size : {20, 21, 24})
  485. for (NonlineMode nonline_mode :
  486. {NonlineMode::RELU, NonlineMode::SIGMOID,
  487. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  488. run(oc, ic, size, size, kernel, p, nonline_mode);
  489. }
  490. Checker<ConvBias> checker(handle());
  491. UniformIntRNG rng{-50, 50};
  492. checker.set_dtype(0, dtype::Float32())
  493. .set_dtype(1, dtype::Float32())
  494. .set_dtype(2, dtype::Float32())
  495. .set_rng(0, &rng)
  496. .set_rng(1, &rng)
  497. .set_rng(2, &rng);
  498. checker.set_before_exec_callback(
  499. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  500. "X86_CONV_BIAS_DIRECT_STRIDE2_SMALL_GROUP"));
  501. for (auto&& arg : args) {
  502. checker.set_param(arg.param).exec(
  503. {arg.src, arg.filter, arg.bias, {}, {}});
  504. }
  505. checker.set_before_exec_callback(
  506. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  507. "X86_CONV_BIAS_DIRECT_STRIDE2_LARGE_GROUP"));
  508. for (auto&& arg : args) {
  509. checker.set_param(arg.param).exec(
  510. {arg.src, arg.filter, arg.bias, {}, {}});
  511. }
  512. }
  513. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_INT8x8x32) {
  514. using namespace conv_bias;
  515. std::vector<TestArg> args;
  516. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  517. size_t p, NonlineMode nonline_mode) {
  518. if (w + 2 * p < kernel || h + 2 * p < kernel)
  519. return;
  520. param::ConvBias param;
  521. param.stride_h = 1;
  522. param.stride_w = 1;
  523. param.pad_h = p;
  524. param.pad_w = p;
  525. param.nonlineMode = nonline_mode;
  526. //! no bias
  527. args.emplace_back(param, TensorShape{1, ic, h, w},
  528. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  529. };
  530. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  531. for (size_t ic : {1, 4, 8, 16})
  532. for (size_t oc : {1, 4, 8})
  533. for (size_t p : {0, 2})
  534. for (size_t size : {20, 21, 24})
  535. for (NonlineMode nonline_mode :
  536. {NonlineMode::IDENTITY}) {
  537. run(oc, ic, size, size, kernel, p, nonline_mode);
  538. }
  539. //! test OC block
  540. run(2046, 1, 8, 8, 1, 0, NonlineMode::IDENTITY);
  541. Checker<ConvBias> checker(handle());
  542. UniformIntRNG rng{-50, 50};
  543. #define cb(algo_name) \
  544. checker.set_before_exec_callback( \
  545. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  546. checker.set_dtype(0, dtype::Int8()); \
  547. checker.set_dtype(1, dtype::Int8()); \
  548. checker.set_dtype(2, dtype::Int32()); \
  549. checker.set_dtype(4, dtype::Int32()); \
  550. for (auto&& arg : args) { \
  551. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
  552. } \
  553. for (auto&& arg : args) { \
  554. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  555. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  556. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  557. .set_dtype(4, {}) \
  558. .set_rng(0, &rng) \
  559. .set_rng(1, &rng) \
  560. .set_rng(2, &rng) \
  561. .set_param(arg.param) \
  562. .execs({arg.src, arg.filter, {}, {}, {}}); \
  563. }
  564. #if defined(MEGDNN_X86_WITH_MKL_DNN)
  565. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  566. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  567. }
  568. #endif
  569. #if MEGDNN_X86_WITH_VNNI
  570. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  571. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  572. }
  573. #endif
  574. if (megdnn::x86::is_supported(x86::SIMDType::AVX2)) {
  575. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  576. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2");
  577. }
  578. if (::megdnn::x86::is_supported(::megdnn::x86::SIMDType::SSE4_2)) {
  579. cb("IM2COLMATMUL:X86_INT8X8X32_SSE_4X8X2");
  580. }
  581. #undef cb
  582. }
  583. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32) {
  584. using namespace conv_bias;
  585. std::vector<TestArg> args;
  586. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  587. size_t p, NonlineMode nonline_mode) {
  588. if (w + 2 * p < kernel || h + 2 * p < kernel)
  589. return;
  590. param::ConvBias param;
  591. param.stride_h = 1;
  592. param.stride_w = 1;
  593. param.pad_h = p;
  594. param.pad_w = p;
  595. param.nonlineMode = nonline_mode;
  596. //! no bias
  597. args.emplace_back(param, TensorShape{1, ic, h, w},
  598. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  599. args.emplace_back(param, TensorShape{1, ic, h, w},
  600. TensorShape{oc, ic, kernel, kernel},
  601. TensorShape{1, oc, 1, 1});
  602. args.emplace_back(
  603. param, TensorShape{1, ic, h, w},
  604. TensorShape{oc, ic, kernel, kernel},
  605. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  606. (w + 2 * p - kernel) / param.stride_w + 1});
  607. };
  608. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  609. for (size_t ic : {1, 4, 8, 16})
  610. for (size_t oc : {1, 4, 8, 16, 300})
  611. for (size_t p : {0, 2})
  612. for (size_t size : {8, 24})
  613. for (NonlineMode nonline_mode :
  614. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  615. run(oc, ic, size, size, kernel, p, nonline_mode);
  616. }
  617. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  618. Checker<ConvBias> checker(handle());
  619. #define cb(algo_name) \
  620. checker.set_before_exec_callback( \
  621. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  622. for (auto&& arg : args) { \
  623. checker.set_param(arg.param).execs( \
  624. {arg.src, arg.filter, arg.bias, {}, {}}); \
  625. }
  626. #if defined(MEGDNN_X86_WITH_MKL) || defined(MEGDNN_X86_WITH_OPENBLAS)
  627. cb("IM2COLMATMUL:X86_F32_BLAS");
  628. #endif
  629. #undef cb
  630. }
  631. #if defined(MEGDNN_X86_WITH_MKL)
  632. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA) {
  633. using namespace conv_bias;
  634. std::vector<TestArg> args;
  635. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  636. size_t p, NonlineMode nonline_mode) {
  637. if (w + 2 * p < kernel || h + 2 * p < kernel)
  638. return;
  639. param::ConvBias param;
  640. param.stride_h = 1;
  641. param.stride_w = 1;
  642. param.pad_h = p;
  643. param.pad_w = p;
  644. param.nonlineMode = nonline_mode;
  645. //! no bias
  646. args.emplace_back(param, TensorShape{1, ic, h, w},
  647. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  648. args.emplace_back(param, TensorShape{1, ic, h, w},
  649. TensorShape{oc, ic, kernel, kernel},
  650. TensorShape{1, oc, 1, 1});
  651. args.emplace_back(
  652. param, TensorShape{1, ic, h, w},
  653. TensorShape{oc, ic, kernel, kernel},
  654. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  655. (w + 2 * p - kernel) / param.stride_w + 1});
  656. param.sparse = param::ConvBias::Sparse::GROUP;
  657. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  658. TensorShape{2, oc, ic, kernel, kernel},
  659. TensorShape{});
  660. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  661. TensorShape{2, oc, ic, kernel, kernel},
  662. TensorShape{1, oc * 2, 1, 1});
  663. args.emplace_back(
  664. param, TensorShape{1, 2 * ic, h, w},
  665. TensorShape{2, oc, ic, kernel, kernel},
  666. TensorShape{1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  667. (w + 2 * param.pad_w - kernel) / 1 + 1});
  668. };
  669. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  670. for (size_t ic : {1, 4, 8, 16})
  671. for (size_t oc : {1, 4, 8, 16})
  672. for (size_t p : {0, 1})
  673. for (size_t size : {8, 24})
  674. for (NonlineMode nonline_mode :
  675. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  676. run(oc, ic, size, size, kernel, p, nonline_mode);
  677. }
  678. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  679. Checker<ConvBias> checker(handle());
  680. #define cb(algo_name) \
  681. checker.set_before_exec_callback( \
  682. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  683. for (auto&& arg : args) { \
  684. checker.set_param(arg.param).execs( \
  685. {arg.src, arg.filter, arg.bias, {}, {}}); \
  686. }
  687. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  688. #undef cb
  689. }
  690. #endif
  691. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8) {
  692. using namespace conv_bias;
  693. std::vector<TestArg> args;
  694. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  695. size_t p, NonlineMode nonline_mode) {
  696. if (w + 2 * p < kernel || h + 2 * p < kernel)
  697. return;
  698. param::ConvBias param;
  699. param.stride_h = 1;
  700. param.stride_w = 1;
  701. param.pad_h = p;
  702. param.pad_w = p;
  703. param.nonlineMode = nonline_mode;
  704. //! no bias
  705. args.emplace_back(param, TensorShape{1, ic, h, w},
  706. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  707. //! bias channel
  708. args.emplace_back(param, TensorShape{2, ic, h, w},
  709. TensorShape{oc, ic, kernel, kernel},
  710. TensorShape{1, oc, 1, 1});
  711. };
  712. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  713. for (size_t ic : {1, 4, 8, 16})
  714. for (size_t oc : {1, 4, 8})
  715. for (size_t p : {0, 2})
  716. for (size_t size : {20, 21, 24})
  717. for (NonlineMode nonline_mode :
  718. {NonlineMode::IDENTITY, NonlineMode::RELU,
  719. NonlineMode::H_SWISH}) {
  720. run(oc, ic, size, size, kernel, p, nonline_mode);
  721. }
  722. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  723. Checker<ConvBias> checker(handle());
  724. #define cb(algo_name) \
  725. checker.set_before_exec_callback( \
  726. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  727. UniformIntRNG rng{-50, 50}; \
  728. for (auto&& arg : args) { \
  729. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  730. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  731. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  732. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  733. .set_rng(0, &rng) \
  734. .set_rng(1, &rng) \
  735. .set_rng(2, &rng) \
  736. .set_param(arg.param) \
  737. .execs({arg.src, arg.filter, {}, {}, {}}); \
  738. }
  739. #if defined(MEGDNN_X86_WITH_MKL_DNN)
  740. if (x86::is_supported(x86::SIMDType::VNNI)) {
  741. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  742. }
  743. #endif
  744. #if MEGDNN_X86_WITH_VNNI
  745. if (x86::is_supported(x86::SIMDType::VNNI)) {
  746. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  747. }
  748. #endif
  749. if (x86::is_supported(x86::SIMDType::AVX2)) {
  750. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  751. }
  752. #undef cb
  753. }
  754. TEST_F(X86, CONV_BIAS_MATMUL) {
  755. using namespace conv_bias;
  756. std::vector<TestArg> args;
  757. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  758. size_t p, NonlineMode nonline_mode) {
  759. if (w + 2 * p < kernel || h + 2 * p < kernel)
  760. return;
  761. param::ConvBias param;
  762. param.stride_h = 1;
  763. param.stride_w = 1;
  764. param.pad_h = p;
  765. param.pad_w = p;
  766. param.nonlineMode = nonline_mode;
  767. //! no bias
  768. param.sparse = param::ConvBias::Sparse::DENSE;
  769. args.emplace_back(param, TensorShape{1, ic, h, w},
  770. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  771. //! bias channel
  772. args.emplace_back(param, TensorShape{2, ic, h, w},
  773. TensorShape{oc, ic, kernel, kernel},
  774. TensorShape{1, oc, 1, 1});
  775. //! bias
  776. args.emplace_back(param, TensorShape{2, ic, h, w},
  777. TensorShape{oc, ic, kernel, kernel},
  778. TensorShape{2, oc, (h + param.pad_h * 2 - kernel) + 1,
  779. (w + param.pad_w * 2 - kernel) + 1});
  780. //! gruop
  781. param.sparse = param::ConvBias::Sparse::GROUP;
  782. args.emplace_back(
  783. param, TensorShape{2, 2 * ic, h, w},
  784. TensorShape{2, oc, ic, kernel, kernel},
  785. TensorShape{2, 2 * oc, (h + param.pad_h * 2 - kernel) + 1,
  786. (w + param.pad_w * 2 - kernel) + 1});
  787. };
  788. for (size_t kernel : {2, 3, 5, 7})
  789. for (size_t ic : {1, 2, 3, 4})
  790. for (size_t oc : {1, 2, 3, 4})
  791. for (size_t p : {0, 2})
  792. for (size_t size : {20, 21, 22, 23, 24})
  793. for (NonlineMode nonline_mode :
  794. {NonlineMode::RELU, NonlineMode::SIGMOID,
  795. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  796. run(oc, ic, size, size, kernel, p, nonline_mode);
  797. }
  798. Checker<ConvBias> checker(handle());
  799. checker.set_before_exec_callback(
  800. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  801. "X86_CONV_BIAS_MATMUL"));
  802. checker.set_epsilon(1);
  803. UniformIntRNG rng{-50, 50};
  804. checker.set_dtype(0, dtype::Float32())
  805. .set_dtype(1, dtype::Float32())
  806. .set_dtype(2, dtype::Float32())
  807. .set_rng(0, &rng)
  808. .set_rng(1, &rng)
  809. .set_rng(2, &rng);
  810. for (auto&& arg : args) {
  811. checker.set_param(arg.param).exec(
  812. {arg.src, arg.filter, arg.bias, {}, {}});
  813. }
  814. }
  815. #if MEGDNN_WITH_BENCHMARK
  816. #if defined(MEGDNN_X86_WITH_MKL_DNN)
  817. static void x86_benchmark_fp32_mkldnn(Handle* handle) {
  818. constexpr size_t RUNS = 30;
  819. param::ConvBias param;
  820. Benchmarker<ConvBias> benchmarker_mkldnn(handle);
  821. benchmarker_mkldnn.set_display(false).set_times(RUNS);
  822. benchmarker_mkldnn.set_before_exec_callback(
  823. AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));
  824. Benchmarker<ConvBias> benchmarker_im2col(handle);
  825. benchmarker_im2col.set_display(false).set_times(RUNS);
  826. benchmarker_im2col.set_before_exec_callback(
  827. AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
  828. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  829. size_t FS, size_t SZ, size_t GROUP = 1) {
  830. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  831. bias({1, OC, 1, 1}), z({}), dst({N, OC, H / SZ, W / SZ});
  832. param.pad_h = FS / 2;
  833. param.pad_w = FS / 2;
  834. param.stride_h = SZ;
  835. param.stride_w = SZ;
  836. param.format = param::ConvBias::Format::NCHW;
  837. param.sparse = param::ConvBias::Sparse::DENSE;
  838. if (GROUP > 1) {
  839. param.sparse = param::ConvBias::Sparse::GROUP;
  840. filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
  841. }
  842. auto im2col_used = benchmarker_im2col.set_param(param).exec(
  843. {src, filter, bias, z, dst}) /
  844. RUNS;
  845. src = IC < 8 ? TensorShape{N, IC, H, W}
  846. : TensorShape{N, IC / 8, H, W, 8};
  847. filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
  848. : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
  849. if (GROUP > 1 && OC == GROUP && IC == GROUP) {
  850. filter = {GROUP / 8, 1, 1, FS, FS, 8};
  851. } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
  852. filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
  853. }
  854. bias = {1, OC / 8, 1, 1, 8};
  855. z = {};
  856. dst = {N, OC / 8, H / SZ, W / SZ, 8};
  857. param.format = param::ConvBias::Format::NCHW88;
  858. auto mkldnn_used = benchmarker_mkldnn.set_param(param).exec(
  859. {src, filter, bias, z, dst}) /
  860. RUNS;
  861. float computations =
  862. (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  863. std::cout << "run " << src.to_string() << " " << filter.to_string()
  864. << " " << bias.to_string() << " " << dst.to_string()
  865. << std::endl;
  866. std::cout << "im2col: " << im2col_used << " ms, "
  867. << (computations / im2col_used) << " Gops, ";
  868. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  869. << (computations / mkldnn_used) << " Gops, "
  870. << "spped up: " << (im2col_used / mkldnn_used) << ", ";
  871. std::cout << std::endl;
  872. };
  873. run(1, 64, 64, 56, 56, 3, 1);
  874. run(1, 3, 64, 224, 224, 3, 1);
  875. run(1, 3, 64, 224, 224, 7, 2);
  876. run(1, 64, 64, 56, 56, 3, 1);
  877. run(1, 128, 128, 28, 28, 3, 1);
  878. run(1, 256, 256, 14, 14, 3, 1);
  879. run(1, 512, 512, 7, 7, 3, 1);
  880. run(1, 256, 64, 56, 56, 1, 1);
  881. run(1, 512, 128, 28, 28, 1, 1);
  882. run(1, 1024, 256, 14, 14, 1, 1);
  883. run(1, 2048, 512, 7, 7, 1, 1);
  884. run(1, 32, 32, 112, 112, 3, 1, 32);
  885. run(1, 144, 144, 56, 56, 3, 1, 144);
  886. run(1, 192, 192, 28, 28, 3, 1, 192);
  887. run(1, 384, 384, 28, 28, 3, 1, 384);
  888. run(1, 576, 576, 14, 14, 3, 1, 576);
  889. run(1, 960, 960, 7, 7, 3, 1, 960);
  890. run(1, 256, 128, 56, 56, 1, 2, 1);
  891. run(1, 512, 256, 28, 28, 1, 2, 1);
  892. run(1, 1024, 512, 14, 14, 1, 2, 1);
  893. run(1, 96, 96, 112, 112, 3, 2, 96);
  894. run(1, 144, 144, 56, 56, 3, 2, 144);
  895. run(1, 384, 384, 28, 28, 3, 2, 384);
  896. run(1, 576, 576, 14, 14, 3, 2, 576);
  897. }
  898. TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  899. x86_benchmark_fp32_mkldnn(handle());
  900. }
  901. TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  902. x86_benchmark_fp32_mkldnn(handle());
  903. }
  904. #endif
  905. #endif
  906. /************************* Winograd ****************************/
  907. namespace{
  908. std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
  909. std::vector<conv_bias::TestArg> args;
  910. param::ConvBias cur_param;
  911. cur_param.format = param::ConvBias::Format::NCHW88;
  912. using NLMode = param::ConvBias::NonlineMode;
  913. // clang-format off
  914. for (auto nlmode :
  915. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  916. for (size_t ic : {1, 2}) {
  917. for (size_t oc : {1, 2}) {
  918. for (size_t i : {9, 63}) {
  919. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  920. cur_param.nonlineMode = nlmode;
  921. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  922. cur_param.pad_h = cur_param.pad_w = 1;
  923. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  924. TensorShape{oc, ic, 3, 3, 8, 8},
  925. TensorShape{1, oc, 1, 1, 8});
  926. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  927. TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
  928. //! bias
  929. args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
  930. TensorShape{oc, ic, 3, 3, 8, 8}, TensorShape{2, oc, i, i, 8});
  931. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  932. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
  933. TensorShape{2, oc, ic, 3, 3, 8, 8},
  934. TensorShape{1, 2 * oc, 1, 1, 8});*/
  935. }}}
  936. // clang-format on
  937. //! test for multi-thread OC parallel
  938. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  939. cur_param.pad_h = cur_param.pad_w = 1;
  940. args.emplace_back(cur_param, TensorShape{2, 1, 9, 9, 8},
  941. TensorShape{128, 1, 3, 3, 8, 8},
  942. TensorShape{1, 128, 1, 1, 8});
  943. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  944. args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
  945. TensorShape{2, 128, 1, 3, 3, 8, 8},
  946. TensorShape{1, 2 * 128, 1, 1, 8});*/
  947. }
  948. return args;
  949. }
  950. } // namespace
  951. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
  952. using namespace conv_bias;
  953. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  954. Checker<ConvBiasForward> checker(handle());
  955. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  956. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  957. for (auto&& arg : args) {
  958. checker.set_param(arg.param).execs(
  959. {arg.src, arg.filter, arg.bias, {}, {}});
  960. }
  961. }
  962. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
  963. using namespace conv_bias;
  964. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  965. Checker<ConvBiasForward> checker(handle());
  966. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  967. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  968. for (auto&& arg : args) {
  969. checker.set_param(arg.param).execs(
  970. {arg.src, arg.filter, arg.bias, {}, {}});
  971. }
  972. }
  973. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
  974. using namespace conv_bias;
  975. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  976. Checker<ConvBiasForward> checker(handle());
  977. auto extra_impl = [](const TensorNDArray& tensors, uint32_t m,
  978. param::ConvBias param, Handle* handle) {
  979. megdnn_assert(param.format == param::ConvBias::Format::NCHW88);
  980. auto winograd_preprocess_opr =
  981. handle->create_operator<WinogradFilterPreprocess>();
  982. winograd_preprocess_opr->param().output_block_size = m;
  983. winograd_preprocess_opr->param().format = param::MatrixMul::Format::MK8;
  984. TensorLayout filter_transform_layout;
  985. winograd_preprocess_opr->deduce_layout(tensors[1].layout,
  986. filter_transform_layout);
  987. size_t winograd_preprocess_workspace_in_bytes =
  988. winograd_preprocess_opr->get_workspace_in_bytes(
  989. tensors[1].layout, filter_transform_layout);
  990. auto conv_bias_opr = handle->create_operator<ConvBias>();
  991. conv_bias_opr->param() = param;
  992. conv_bias_opr->param().format = param::ConvBias::Format::NCHW88_WINOGRAD;
  993. conv_bias_opr->param().output_block_size = m;
  994. size_t conv_bias_workspace_in_bytes =
  995. conv_bias_opr->get_workspace_in_bytes(
  996. tensors[0].layout, filter_transform_layout,
  997. tensors[2].layout, tensors[3].layout,
  998. tensors[4].layout);
  999. WorkspaceBundle wb(nullptr, {filter_transform_layout.span().dist_byte(),
  1000. conv_bias_workspace_in_bytes,
  1001. winograd_preprocess_workspace_in_bytes});
  1002. wb.set(malloc(wb.total_size_in_bytes()));
  1003. TensorND filter_transform_tensor(wb.get(0),
  1004. std::move(filter_transform_layout));
  1005. winograd_preprocess_opr->exec(tensors[1], filter_transform_tensor,
  1006. wb.get_workspace(2));
  1007. conv_bias_opr->exec(tensors[0], filter_transform_tensor, tensors[2],
  1008. tensors[3], tensors[4], wb.get_workspace(1));
  1009. free(wb.ptr());
  1010. };
  1011. auto run = [&checker, &extra_impl](
  1012. Handle* handle, const std::vector<TestArg>& args,
  1013. const std::vector<size_t>& out_size, DType A_dtype,
  1014. DType B_dtype, DType C_dtype, DType D_dtype,
  1015. const float eps) {
  1016. for (auto&& arg : args) {
  1017. for (uint32_t m : out_size) {
  1018. checker.set_extra_opr_impl(std::bind(extra_impl,
  1019. std::placeholders::_1, m,
  1020. arg.param, handle));
  1021. checker.set_dtype(0, A_dtype)
  1022. .set_dtype(1, B_dtype)
  1023. .set_dtype(2, C_dtype)
  1024. .set_dtype(4, D_dtype)
  1025. .set_epsilon(eps)
  1026. .set_param(arg.param)
  1027. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  1028. }
  1029. }
  1030. };
  1031. run(handle(), args, {2, 6}, dtype::Float32(), dtype::Float32(),
  1032. dtype::Float32(), dtype::Float32(), 1e-3f);
  1033. }
  1034. /*********************************** End winograd ************************/
  1035. #if defined(MEGDNN_X86_WITH_MKL_DNN)
  1036. static void x86_correctness_fp32_mkldnn_run(
  1037. Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
  1038. ConvBiasForward::BiasMode bias_mode,
  1039. param::ConvBias::NonlineMode noline_mode, size_t n, size_t stride,
  1040. size_t kernel, size_t oc, size_t ic, size_t h, size_t w, size_t group) {
  1041. auto oc_per_group = oc / group;
  1042. auto ic_per_group = ic / group;
  1043. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  1044. (ic_per_group % 8 == 0 || ic_per_group == 3) &&
  1045. ic_per_group > 0;
  1046. bool ok_depthwise = oc == ic && oc == group;
  1047. if (!(ok_group || ok_depthwise)) {
  1048. return;
  1049. }
  1050. size_t pad = kernel / 2;
  1051. size_t kernel_h = kernel;
  1052. size_t kernel_w = kernel;
  1053. param::ConvBias param;
  1054. param.format = param::ConvBias::Format::NCHW88;
  1055. param.stride_h = stride;
  1056. param.stride_w = stride;
  1057. param.pad_h = pad;
  1058. param.pad_w = pad;
  1059. param.nonlineMode = noline_mode;
  1060. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  1061. if (ic == 3) {
  1062. src_tensor_shape = TensorShape{n, ic, h, w};
  1063. }
  1064. auto weight_tensor_shape =
  1065. TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  1066. if (ic == 3) {
  1067. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  1068. }
  1069. auto bias_tensor_shape = TensorShape{};
  1070. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1071. bias_tensor_shape = {1, oc / 8, 1, 1, 8};
  1072. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1073. TensorLayout dst_layout;
  1074. auto ConvBiasOp = handle->create_operator<ConvBias>();
  1075. ConvBiasOp->param() = param;
  1076. ConvBiasOp->deduce_layout({src_tensor_shape, dtype::Float32()},
  1077. {weight_tensor_shape, dtype::Float32()}, {},
  1078. {}, dst_layout);
  1079. bias_tensor_shape = dst_layout;
  1080. }
  1081. if (group == 1) {
  1082. param.sparse = param::ConvBias::Sparse::DENSE;
  1083. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1084. param.sparse = param::ConvBias::Sparse::GROUP;
  1085. weight_tensor_shape =
  1086. TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  1087. } else if (group > 1 && oc / group % 8 == 0 && oc / group > 0 &&
  1088. ic / group % 8 == 0 && ic / group > 0) {
  1089. param.sparse = param::ConvBias::Sparse::GROUP;
  1090. weight_tensor_shape = TensorShape{
  1091. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8,
  1092. 8};
  1093. }
  1094. checker.set_dtype(0, dtype::Float32())
  1095. .set_dtype(1, dtype::Float32())
  1096. .set_dtype(2, dtype::Float32())
  1097. .set_dtype(4, dtype::Float32())
  1098. .set_rng(0, &rng)
  1099. .set_rng(1, &rng)
  1100. .set_rng(2, &rng)
  1101. .set_epsilon(1e-3)
  1102. .set_param(param)
  1103. .execs({src_tensor_shape,
  1104. weight_tensor_shape,
  1105. bias_tensor_shape,
  1106. {},
  1107. {}});
  1108. }
  1109. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  1110. Checker<ConvBias> checker(handle);
  1111. UniformIntRNG rng{-127, 127};
  1112. checker.set_before_exec_callback(
  1113. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1114. "MKLDNN_CONV_FP32"));
  1115. for (auto bias_mode :
  1116. {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
  1117. megdnn::BiasMode::BIAS})
  1118. for (auto noline_mode : {param::ConvBias::NonlineMode::IDENTITY,
  1119. param::ConvBias::NonlineMode::SIGMOID,
  1120. param::ConvBias::NonlineMode::H_SWISH})
  1121. for (size_t n : {1, 2})
  1122. for (size_t stride : {1, 2})
  1123. for (size_t kernel : {3, 5, 7})
  1124. for (size_t oc : {8, 16})
  1125. for (size_t ic : {3, 8, 16})
  1126. for (size_t h : {22, 33})
  1127. for (size_t w : {22, 33}) {
  1128. for (size_t group = 1;
  1129. group <= std::min(oc, ic);
  1130. ++group) {
  1131. x86_correctness_fp32_mkldnn_run(
  1132. checker, rng, handle,
  1133. bias_mode, noline_mode, n,
  1134. stride, kernel, oc, ic, h,
  1135. w, group);
  1136. }
  1137. }
  1138. }
  1139. TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1140. x86_correctness_fp32_mkldnn(handle());
  1141. }
  1142. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1143. x86_correctness_fp32_mkldnn(handle());
  1144. }
  1145. TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
  1146. using namespace conv_bias;
  1147. std::vector<TestArg> args;
  1148. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1149. size_t p, NonlineMode nonline_mode) {
  1150. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1151. return;
  1152. param::ConvBias param;
  1153. param.stride_h = 1;
  1154. param.stride_w = 1;
  1155. param.pad_h = p;
  1156. param.pad_w = p;
  1157. param.nonlineMode = nonline_mode;
  1158. //! no bias
  1159. args.emplace_back(param, TensorShape{1, ic, h, w},
  1160. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1161. };
  1162. for (size_t kernel : {2, 3, 5, 7})
  1163. for (size_t ic : {1, 2, 3, 4})
  1164. for (size_t oc : {1, 2, 4})
  1165. for (size_t p : {0, 2})
  1166. for (size_t size : {20, 21, 22, 23, 24})
  1167. for (NonlineMode nonline_mode :
  1168. {NonlineMode::IDENTITY}) {
  1169. run(oc, ic, size, size, kernel, p, nonline_mode);
  1170. }
  1171. Checker<ConvBias> checker(handle());
  1172. checker.set_before_exec_callback(
  1173. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1174. "MKLDNN_MATMUL_INT8"));
  1175. checker.set_epsilon(1);
  1176. UniformIntRNG rng{-50, 50};
  1177. checker.set_dtype(0, dtype::Int8())
  1178. .set_dtype(1, dtype::Int8())
  1179. .set_dtype(2, dtype::Int32())
  1180. .set_dtype(4, dtype::Int32())
  1181. .set_rng(0, &rng)
  1182. .set_rng(1, &rng)
  1183. .set_rng(2, &rng);
  1184. for (auto&& arg : args) {
  1185. checker.set_param(arg.param).exec(
  1186. {arg.src, arg.filter, arg.bias, {}, {}});
  1187. }
  1188. }
  1189. TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
  1190. using namespace conv_bias;
  1191. std::vector<TestArg> args;
  1192. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1193. size_t p, NonlineMode nonline_mode) {
  1194. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1195. return;
  1196. param::ConvBias param;
  1197. param.stride_h = 1;
  1198. param.stride_w = 1;
  1199. param.pad_h = p;
  1200. param.pad_w = p;
  1201. param.nonlineMode = nonline_mode;
  1202. //! no bias
  1203. args.emplace_back(param, TensorShape{1, ic, h, w},
  1204. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1205. };
  1206. for (size_t kernel : {2, 3, 5, 7})
  1207. for (size_t ic : {1, 2, 3, 4})
  1208. for (size_t oc : {1, 2, 4})
  1209. for (size_t p : {0, 2})
  1210. for (size_t size : {20, 22, 24})
  1211. for (NonlineMode nonline_mode :
  1212. {NonlineMode::IDENTITY}) {
  1213. run(oc, ic, size, size, kernel, p, nonline_mode);
  1214. }
  1215. Checker<ConvBias> checker(handle());
  1216. checker.set_before_exec_callback(
  1217. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1218. checker.set_epsilon(1);
  1219. UniformIntRNG rng{-50, 50};
  1220. checker.set_dtype(0, dtype::Int8())
  1221. .set_dtype(1, dtype::Int8())
  1222. .set_dtype(2, dtype::Int32())
  1223. .set_dtype(4, dtype::Int32())
  1224. .set_rng(0, &rng)
  1225. .set_rng(1, &rng)
  1226. .set_rng(2, &rng);
  1227. for (auto&& arg : args) {
  1228. checker.set_param(arg.param).exec(
  1229. {arg.src, arg.filter, arg.bias, {}, {}});
  1230. }
  1231. }
  1232. TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
  1233. using namespace conv_bias;
  1234. std::vector<TestArg> args;
  1235. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1236. size_t p, NonlineMode nonline_mode) {
  1237. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1238. return;
  1239. param::ConvBias param;
  1240. param.stride_h = 1;
  1241. param.stride_w = 1;
  1242. param.pad_h = p;
  1243. param.pad_w = p;
  1244. param.nonlineMode = nonline_mode;
  1245. //! no bias
  1246. args.emplace_back(param, TensorShape{1, ic, h, w},
  1247. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1248. };
  1249. for (size_t kernel : {2, 3, 5, 7})
  1250. for (size_t ic : {1, 2, 3, 4})
  1251. for (size_t oc : {1, 2, 4})
  1252. for (size_t p : {0, 2})
  1253. for (size_t size : {20, 22, 24})
  1254. for (NonlineMode nonline_mode :
  1255. {NonlineMode::IDENTITY}) {
  1256. run(oc, ic, size, size, kernel, p, nonline_mode);
  1257. }
  1258. Checker<ConvBias> checker(handle());
  1259. checker.set_before_exec_callback(
  1260. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1261. checker.set_epsilon(1);
  1262. UniformIntRNG rng{-50, 50};
  1263. checker.set_dtype(0, dtype::Int8())
  1264. .set_dtype(1, dtype::Int8())
  1265. .set_dtype(2, dtype::Int32())
  1266. .set_dtype(4, dtype::Int32())
  1267. .set_rng(0, &rng)
  1268. .set_rng(1, &rng)
  1269. .set_rng(2, &rng);
  1270. for (auto&& arg : args) {
  1271. checker.set_param(arg.param).exec(
  1272. {arg.src, arg.filter, arg.bias, {}, {}});
  1273. }
  1274. }
  1275. #endif
  1276. #if MEGDNN_WITH_BENCHMARK
  1277. namespace {
  1278. void benchmark_impl(const param::ConvBias param,
  1279. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1280. shapes_and_computation,
  1281. const std::string algo_name, size_t RUNS,
  1282. TaskExecutorConfig&& multi_thread_config,
  1283. TaskExecutorConfig&& single_thread_config,
  1284. std::vector<DType> dtype_v) {
  1285. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1286. dtype::Float32(), dtype::Float32()};
  1287. std::vector<float> multi_thread_times, single_thread_times;
  1288. {
  1289. auto multi_thread_hanle =
  1290. create_cpu_handle(0, true, &multi_thread_config);
  1291. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1292. benchmarker.set_times(RUNS)
  1293. .set_display(false)
  1294. .set_dtype(0, dtype_v[0])
  1295. .set_dtype(1, dtype_v[1])
  1296. .set_dtype(2, dtype_v[2])
  1297. .set_dtype(4, dtype_v[3])
  1298. .set_param(param)
  1299. .set_before_exec_callback(
  1300. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1301. algo_name.c_str()));
  1302. for (auto shape : shapes_and_computation) {
  1303. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1304. }
  1305. }
  1306. {
  1307. auto single_thread_handle =
  1308. create_cpu_handle(0, true, &single_thread_config);
  1309. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1310. benchmarker.set_times(RUNS)
  1311. .set_display(false)
  1312. .set_dtype(0, dtype_v[0])
  1313. .set_dtype(1, dtype_v[1])
  1314. .set_dtype(2, dtype_v[2])
  1315. .set_dtype(4, dtype_v[3])
  1316. .set_param(param)
  1317. .set_before_exec_callback(
  1318. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1319. algo_name.c_str()));
  1320. for (auto shape : shapes_and_computation) {
  1321. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1322. }
  1323. }
  1324. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  1325. printf("core_ids:");
  1326. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  1327. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  1328. }
  1329. printf(", Single thread core_id %zu\n",
  1330. single_thread_config.affinity_core_set[0]);
  1331. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  1332. auto shapes = shapes_and_computation[i];
  1333. printf("Bench case: ");
  1334. for (auto&& shape : shapes.first) {
  1335. printf("%s ", shape.to_string().c_str());
  1336. }
  1337. float computations = shapes.second;
  1338. printf("%zu threads gflops: %f,\n single thread gflops: "
  1339. "%f. spead up = %f, speedup/cores=%f\n",
  1340. multi_thread_config.nr_thread,
  1341. computations / multi_thread_times[i],
  1342. computations / single_thread_times[i],
  1343. single_thread_times[i] / multi_thread_times[i],
  1344. single_thread_times[i] / multi_thread_times[i] /
  1345. multi_thread_config.nr_thread);
  1346. }
  1347. }
  1348. void benchmark_impl_comp(const param::ConvBias param,
  1349. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1350. shapes_and_computation,
  1351. const std::string algo_name, const std::string algo_name1,size_t RUNS,
  1352. TaskExecutorConfig&& multi_thread_config,
  1353. TaskExecutorConfig&& single_thread_config,std::vector<DType> dtype_v) {
  1354. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1355. dtype::Float32(), dtype::Float32()};
  1356. std::vector<float> multi_thread_times, single_thread_times;
  1357. {
  1358. auto multi_thread_hanle =
  1359. create_cpu_handle(0, true, &multi_thread_config);
  1360. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1361. benchmarker.set_times(RUNS)
  1362. .set_display(false)
  1363. .set_dtype(0,dtype_v[0])
  1364. .set_dtype(1,dtype_v[1])
  1365. .set_dtype(2,dtype_v[2])
  1366. .set_dtype(4,dtype_v[3])
  1367. .set_param(param)
  1368. .set_before_exec_callback(
  1369. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1370. algo_name.c_str()));
  1371. for (auto shape : shapes_and_computation) {
  1372. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1373. }
  1374. }
  1375. {
  1376. auto single_thread_handle =
  1377. create_cpu_handle(0, true, &single_thread_config);
  1378. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1379. benchmarker.set_times(RUNS)
  1380. .set_display(false)
  1381. .set_dtype(0,dtype_v[0])
  1382. .set_dtype(1,dtype_v[1])
  1383. .set_dtype(2,dtype_v[2])
  1384. .set_dtype(4,dtype_v[3])
  1385. .set_param(param)
  1386. .set_before_exec_callback(
  1387. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1388. algo_name1.c_str()));
  1389. for (auto shape : shapes_and_computation) {
  1390. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1391. }
  1392. }
  1393. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  1394. printf("core_ids:");
  1395. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  1396. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  1397. }
  1398. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  1399. auto shapes = shapes_and_computation[i];
  1400. printf("Bench case: ");
  1401. for (auto&& shape : shapes.first) {
  1402. printf("%s ", shape.to_string().c_str());
  1403. }
  1404. float computations = shapes.second;
  1405. printf("algo:%s gflops: %f,\n algo:%s gflops: "
  1406. "%f. spead up = %f\n",
  1407. algo_name.c_str(), computations / multi_thread_times[i],
  1408. algo_name1.c_str(), computations / single_thread_times[i],
  1409. single_thread_times[i] / multi_thread_times[i]);
  1410. }
  1411. }
  1412. } // namespace
  1413. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
  1414. constexpr size_t RUNS = 50;
  1415. param::ConvBias param;
  1416. param.stride_h = 1;
  1417. param.stride_w = 1;
  1418. param.sparse = param::ConvBias::Sparse::DENSE;
  1419. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1420. dtype::Int32(), dtype::Int32()};
  1421. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1422. shapes_and_computation;
  1423. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1424. size_t FS) {
  1425. param.pad_h = FS / 2;
  1426. param.pad_w = FS / 2;
  1427. SmallVector<TensorShape> shapes{
  1428. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  1429. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) + 1,
  1430. (W + 2 * param.pad_w - FS) + 1};
  1431. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1432. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1433. };
  1434. bench_case(1, 32, 32, 200, 200, 7);
  1435. bench_case(1, 32, 64, 200, 200, 7);
  1436. bench_case(1, 32, 32, 128, 128, 7);
  1437. bench_case(1, 32, 64, 128, 128, 7);
  1438. bench_case(1, 32, 32, 100, 100, 7);
  1439. bench_case(1, 32, 64, 100, 100, 7);
  1440. bench_case(1, 32, 32, 80, 80, 7);
  1441. bench_case(1, 32, 64, 80, 80, 7);
  1442. bench_case(1, 32, 32, 200, 200, 5);
  1443. bench_case(1, 32, 64, 200, 200, 5);
  1444. bench_case(1, 32, 32, 128, 128, 5);
  1445. bench_case(1, 32, 64, 128, 128, 5);
  1446. bench_case(1, 32, 32, 100, 100, 5);
  1447. bench_case(1, 32, 64, 100, 100, 5);
  1448. bench_case(1, 32, 32, 80, 80, 5);
  1449. bench_case(1, 32, 64, 80, 80, 5);
  1450. bench_case(1, 32, 32, 200, 200, 3);
  1451. bench_case(1, 32, 64, 200, 200, 3);
  1452. bench_case(1, 32, 32, 128, 128, 3);
  1453. bench_case(1, 32, 64, 128, 128, 3);
  1454. bench_case(1, 32, 32, 100, 100, 3);
  1455. bench_case(1, 32, 64, 100, 100, 3);
  1456. bench_case(1, 32, 32, 80, 80, 3);
  1457. bench_case(1, 32, 64, 80, 80, 3);
  1458. bench_case(1, 32, 32, 200, 200, 2);
  1459. bench_case(1, 32, 64, 200, 200, 2);
  1460. bench_case(1, 32, 32, 128, 128, 2);
  1461. bench_case(1, 32, 64, 128, 128, 2);
  1462. bench_case(1, 32, 32, 100, 100, 2);
  1463. bench_case(1, 32, 64, 100, 100, 2);
  1464. bench_case(1, 32, 32, 80, 80, 2);
  1465. bench_case(1, 32, 64, 80, 80, 2);
  1466. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
  1467. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
  1468. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1469. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1470. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1471. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1472. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1473. {1, {4}}, data_type);
  1474. shapes_and_computation.clear();
  1475. }
  1476. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  1477. BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
  1478. constexpr size_t RUNS = 50;
  1479. param::ConvBias param;
  1480. param.stride_h = 2;
  1481. param.stride_w = 2;
  1482. param.sparse = param::ConvBias::Sparse::DENSE;
  1483. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1484. dtype::Int32(), dtype::Int32()};
  1485. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1486. shapes_and_computation;
  1487. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1488. size_t FS) {
  1489. param.pad_h = FS / 2;
  1490. param.pad_w = FS / 2;
  1491. SmallVector<TensorShape> shapes{
  1492. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  1493. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  1494. (W + 2 * param.pad_w - FS) / param.pad_w + 1};
  1495. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1496. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1497. };
  1498. bench_case(1, 32, 32, 200, 200, 7);
  1499. bench_case(1, 32, 64, 200, 200, 7);
  1500. bench_case(1, 32, 32, 128, 128, 7);
  1501. bench_case(1, 32, 64, 128, 128, 7);
  1502. bench_case(1, 32, 32, 100, 100, 7);
  1503. bench_case(1, 32, 64, 100, 100, 7);
  1504. bench_case(1, 32, 32, 80, 80, 7);
  1505. bench_case(1, 32, 64, 80, 80, 7);
  1506. bench_case(1, 32, 32, 200, 200, 5);
  1507. bench_case(1, 32, 64, 200, 200, 5);
  1508. bench_case(1, 32, 32, 128, 128, 5);
  1509. bench_case(1, 32, 64, 128, 128, 5);
  1510. bench_case(1, 32, 32, 100, 100, 5);
  1511. bench_case(1, 32, 64, 100, 100, 5);
  1512. bench_case(1, 32, 32, 80, 80, 5);
  1513. bench_case(1, 32, 64, 80, 80, 5);
  1514. bench_case(1, 32, 32, 200, 200, 3);
  1515. bench_case(1, 32, 64, 200, 200, 3);
  1516. bench_case(1, 32, 32, 128, 128, 3);
  1517. bench_case(1, 32, 64, 128, 128, 3);
  1518. bench_case(1, 32, 32, 100, 100, 3);
  1519. bench_case(1, 32, 64, 100, 100, 3);
  1520. bench_case(1, 32, 32, 80, 80, 3);
  1521. bench_case(1, 32, 64, 80, 80, 3);
  1522. bench_case(1, 32, 32, 200, 200, 2);
  1523. bench_case(1, 32, 64, 200, 200, 2);
  1524. bench_case(1, 32, 32, 128, 128, 2);
  1525. bench_case(1, 32, 64, 128, 128, 2);
  1526. bench_case(1, 32, 32, 100, 100, 2);
  1527. bench_case(1, 32, 64, 100, 100, 2);
  1528. bench_case(1, 32, 32, 80, 80, 2);
  1529. bench_case(1, 32, 64, 80, 80, 2);
  1530. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
  1531. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
  1532. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1533. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1534. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1535. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1536. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1537. {1, {4}}, data_type);
  1538. shapes_and_computation.clear();
  1539. }
  1540. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
  1541. constexpr size_t RUNS = 50;
  1542. param::ConvBias param;
  1543. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1544. param.pad_h = 1;
  1545. param.pad_w = 1;
  1546. param.stride_h = 1;
  1547. param.stride_w = 1;
  1548. param.sparse = param::ConvBias::Sparse::GROUP;
  1549. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1550. dtype::Float32(), dtype::Float32()};
  1551. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1552. shapes_and_computation;
  1553. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1554. size_t FS, size_t group) {
  1555. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1556. {group, OC / group, IC / group, FS, FS},
  1557. {1, OC, 1, 1},
  1558. {},
  1559. {N, OC, H, W}};
  1560. TensorShape dst{N, OC, H, W};
  1561. float computations =
  1562. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1563. dst.total_nr_elems()) *
  1564. 1e-6;
  1565. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1566. };
  1567. bench_case(1, 32, 32, 200, 200, 3, 4);
  1568. bench_case(1, 32, 32, 200, 200, 3, 32);
  1569. bench_case(1, 32, 32, 128, 128, 3, 4);
  1570. bench_case(1, 32, 32, 128, 128, 3, 32);
  1571. bench_case(1, 32, 32, 100, 100, 3, 4);
  1572. bench_case(1, 32, 32, 100, 100, 3, 32);
  1573. bench_case(1, 32, 32, 80, 80, 3, 4);
  1574. bench_case(1, 32, 32, 80, 80, 3, 32);
  1575. std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  1576. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP algo\n");
  1577. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1578. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1579. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1580. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1581. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1582. {1, {4}}, data_type);
  1583. shapes_and_computation.clear();
  1584. algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP";
  1585. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP algo\n");
  1586. bench_case(1, 32, 32, 200, 200, 3, 1);
  1587. bench_case(1, 32, 32, 128, 128, 3, 1);
  1588. bench_case(1, 32, 32, 100, 100, 3, 1);
  1589. bench_case(1, 32, 32, 80, 80, 3, 1);
  1590. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1591. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1592. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1593. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1594. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1595. {1, {4}}, data_type);
  1596. }
  1597. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
  1598. constexpr size_t RUNS = 50;
  1599. param::ConvBias param;
  1600. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1601. param.pad_h = 1;
  1602. param.pad_w = 1;
  1603. param.stride_h = 1;
  1604. param.stride_w = 1;
  1605. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1606. dtype::Float32(), dtype::Float32()};
  1607. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1608. shapes_and_computation;
  1609. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1610. size_t FS, size_t group) {
  1611. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1612. {OC / group, IC / group, FS, FS},
  1613. {1, OC, 1, 1},
  1614. {},
  1615. {N, OC, H, W}};
  1616. TensorShape dst{N, OC, H, W};
  1617. float computations =
  1618. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1619. dst.total_nr_elems()) *
  1620. 1e-6;
  1621. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1622. };
  1623. bench_case(1, 32, 32, 200, 200, 3, 1);
  1624. bench_case(1, 32, 32, 200, 200, 3, 1);
  1625. bench_case(1, 32, 32, 128, 128, 3, 1);
  1626. bench_case(1, 32, 32, 128, 128, 3, 1);
  1627. bench_case(1, 32, 32, 100, 100, 3, 1);
  1628. bench_case(1, 32, 32, 100, 100, 3, 1);
  1629. bench_case(1, 32, 32, 80, 80, 3, 1);
  1630. bench_case(1, 32, 32, 80, 80, 3, 1);
  1631. bench_case(1, 64, 32, 7, 7, 3, 1);
  1632. bench_case(1, 64, 64, 7, 7, 3, 1);
  1633. bench_case(1, 64, 128, 7, 7, 3, 1);
  1634. bench_case(1, 64, 256, 7, 7, 3, 1);
  1635. bench_case(1, 64, 512, 7, 7, 3, 1);
  1636. bench_case(1, 64, 1024, 7, 7, 3, 1);
  1637. bench_case(1, 64, 32, 14, 14, 3, 1);
  1638. bench_case(1, 64, 64, 14, 14, 3, 1);
  1639. bench_case(1, 64, 128, 14, 14, 3, 1);
  1640. bench_case(1, 64, 256, 14, 14, 3, 1);
  1641. bench_case(1, 64, 512, 14, 14, 3, 1);
  1642. bench_case(1, 64, 1024, 14, 14, 3, 1);
  1643. bench_case(1, 128, 128, 14, 14, 3, 1);
  1644. bench_case(1, 128, 256, 14, 14, 3, 1);
  1645. bench_case(1, 512, 512, 14, 14, 3, 1);
  1646. bench_case(1, 256, 512, 14, 14, 3, 1);
  1647. bench_case(1, 512, 1024, 14, 14, 3, 1);
  1648. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  1649. std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
  1650. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  1651. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1652. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1653. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1654. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1655. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1656. {1, {4}}, data_type);
  1657. shapes_and_computation.clear();
  1658. }
  1659. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
  1660. constexpr size_t RUNS = 50;
  1661. param::ConvBias param;
  1662. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1663. param.pad_h = 1;
  1664. param.pad_w = 1;
  1665. param.stride_h = 1;
  1666. param.stride_w = 1;
  1667. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1668. dtype::Float32(), dtype::Float32()};
  1669. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1670. shapes_and_computation;
  1671. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H,
  1672. size_t W, size_t FS,
  1673. size_t group) {
  1674. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1675. {OC / group, IC / group, FS, FS},
  1676. {1, OC, 1, 1},
  1677. {},
  1678. {N, OC, H, W}};
  1679. TensorShape dst{N, OC, H, W};
  1680. float computations =
  1681. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1682. dst.total_nr_elems()) *
  1683. 1e-6;
  1684. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1685. };
  1686. bench_case(1, 32, 32, 200, 200, 3, 1);
  1687. bench_case(1, 32, 32, 200, 200, 3, 1);
  1688. bench_case(1, 32, 32, 128, 128, 3, 1);
  1689. bench_case(1, 32, 32, 128, 128, 3, 1);
  1690. bench_case(1, 32, 32, 100, 100, 3, 1);
  1691. bench_case(1, 32, 32, 100, 100, 3, 1);
  1692. bench_case(1, 32, 32, 80, 80, 3, 1);
  1693. bench_case(1, 32, 32, 80, 80, 3, 1);
  1694. bench_case(1, 64, 32, 7, 7, 3, 1);
  1695. bench_case(1, 64, 64, 7, 7, 3, 1);
  1696. bench_case(1, 64, 128, 7, 7, 3, 1);
  1697. bench_case(1, 64, 256, 7, 7, 3, 1);
  1698. bench_case(1, 64, 512, 7, 7, 3, 1);
  1699. bench_case(1, 64, 1024, 7, 7, 3, 1);
  1700. bench_case(1, 64, 32, 14, 14, 3, 1);
  1701. bench_case(1, 64, 64, 14, 14, 3, 1);
  1702. bench_case(1, 64, 128, 14, 14, 3, 1);
  1703. bench_case(1, 64, 256, 14, 14, 3, 1);
  1704. bench_case(1, 64, 512, 14, 14, 3, 1);
  1705. bench_case(1, 64, 1024, 14, 14, 3, 1);
  1706. bench_case(1, 128, 128, 14, 14, 3, 1);
  1707. bench_case(1, 128, 256, 14, 14, 3, 1);
  1708. bench_case(1, 512, 512, 14, 14, 3, 1);
  1709. bench_case(1, 256, 512, 14, 14, 3, 1);
  1710. bench_case(1, 512, 1024, 14, 14, 3, 1);
  1711. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  1712. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  1713. std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
  1714. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  1715. benchmark_impl_comp(param, shapes_and_computation, algo_name,algo_name1, RUNS,
  1716. {1, {4}}, {1, {4}},data_type);
  1717. benchmark_impl_comp(param, shapes_and_computation, algo_name,algo_name1, RUNS,
  1718. {1, {7}}, {1, {7}},data_type);
  1719. shapes_and_computation.clear();
  1720. }
  1721. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
  1722. constexpr size_t RUNS = 50;
  1723. param::ConvBias param;
  1724. param.pad_h = 1;
  1725. param.pad_w = 1;
  1726. param.stride_h = 1;
  1727. param.stride_w = 1;
  1728. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1729. shapes_and_computation;
  1730. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1731. size_t FS, size_t group) {
  1732. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1733. {OC / group, IC / group, FS, FS},
  1734. {1, OC, 1, 1},
  1735. {},
  1736. {N, OC, H, W}};
  1737. TensorShape dst{N, OC, H, W};
  1738. float computations =
  1739. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1740. dst.total_nr_elems()) *
  1741. 1e-6;
  1742. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1743. };
  1744. bench_case(1, 32, 32, 200, 200, 3, 1);
  1745. bench_case(1, 32, 32, 200, 200, 3, 1);
  1746. bench_case(1, 32, 32, 128, 128, 3, 1);
  1747. bench_case(1, 32, 32, 128, 128, 3, 1);
  1748. bench_case(1, 32, 32, 100, 100, 3, 1);
  1749. bench_case(1, 32, 32, 100, 100, 3, 1);
  1750. bench_case(1, 32, 32, 80, 80, 3, 1);
  1751. bench_case(1, 32, 32, 80, 80, 3, 1);
  1752. bench_case(1, 64, 32, 7, 7, 3, 1);
  1753. bench_case(1, 64, 64, 7, 7, 3, 1);
  1754. bench_case(1, 64, 128, 7, 7, 3, 1);
  1755. bench_case(1, 64, 256, 7, 7, 3, 1);
  1756. bench_case(1, 64, 512, 7, 7, 3, 1);
  1757. bench_case(1, 64, 1024, 7, 7, 3, 1);
  1758. bench_case(1, 64, 32, 14, 14, 3, 1);
  1759. bench_case(1, 64, 64, 14, 14, 3, 1);
  1760. bench_case(1, 64, 128, 14, 14, 3, 1);
  1761. bench_case(1, 64, 256, 14, 14, 3, 1);
  1762. bench_case(1, 64, 512, 14, 14, 3, 1);
  1763. bench_case(1, 64, 1024, 14, 14, 3, 1);
  1764. bench_case(1, 128, 128, 14, 14, 3, 1);
  1765. bench_case(1, 128, 256, 14, 14, 3, 1);
  1766. bench_case(1, 512, 512, 14, 14, 3, 1);
  1767. bench_case(1, 256, 512, 14, 14, 3, 1);
  1768. bench_case(1, 512, 1024, 14, 14, 3, 1);
  1769. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  1770. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1771. dtype::Int32(), dtype::Int32()};
  1772. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2";
  1773. // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
  1774. // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
  1775. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1776. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1777. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1778. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1779. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1780. {1, {4}}, data_type);
  1781. shapes_and_computation.clear();
  1782. }
  1783. namespace{
  1784. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  1785. size_t pack_size) {
  1786. std::vector<conv_bias::TestArg> args;
  1787. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1788. size_t p) {
  1789. if (ic % pack_size != 0 || oc % pack_size != 0)
  1790. return;
  1791. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1792. return;
  1793. param::ConvBias param;
  1794. param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  1795. param.format = param::ConvBias::Format::NCHW88;
  1796. param.sparse = param::ConvBias::Sparse::DENSE;
  1797. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1798. param.stride_h = 1;
  1799. param.stride_w = 1;
  1800. param.pad_h = p;
  1801. param.pad_w = p;
  1802. args.push_back(conv_bias::TestArg{param,
  1803. TensorShape{1, ic/8, h, w, 8},
  1804. TensorShape{oc/8, ic/8, kernel, kernel, 8, 8},
  1805. {1, oc/8, 1, 1, 8}});
  1806. };
  1807. for (size_t ic : {64, 128, 256}) {
  1808. for (size_t oc : {64,128,256}) {
  1809. pack(oc, ic, 56, 56, kernel, kernel / 2);
  1810. pack(oc, ic, 14, 14, kernel, kernel / 2);
  1811. pack(oc, ic, 28, 28, kernel, kernel / 2);
  1812. }
  1813. }
  1814. //! conv in vgg16
  1815. pack(512, 512, 15, 15, kernel, kernel / 2);
  1816. pack(512, 256, 15, 15, kernel, kernel / 2);
  1817. pack(256, 256, 29, 29, kernel, kernel / 2);
  1818. pack(256, 128, 29, 29, kernel, kernel / 2);
  1819. pack(128, 128, 57, 57, kernel, kernel / 2);
  1820. pack(128, 64, 57, 57, kernel, kernel / 2);
  1821. pack(64, 64, 56, 56, kernel, kernel / 2);
  1822. pack(128, 128, 28, 28, kernel, kernel / 2);
  1823. pack(512, 512, 14, 14, kernel, kernel / 2);
  1824. return args;
  1825. }
  1826. void benchmark_winograd(const char* algo_name, Handle* handle,
  1827. size_t kernel, size_t pack_size) {
  1828. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  1829. using namespace conv_bias;
  1830. constexpr size_t RUN = 10;
  1831. Benchmarker<ConvBias> benchmark(handle);
  1832. benchmark.set_display(false);
  1833. benchmark.set_times(RUN);
  1834. Benchmarker<ConvBias> benchmark_winograd(handle);
  1835. benchmark_winograd.set_display(false);
  1836. benchmark_winograd.set_times(RUN);
  1837. for (auto&& arg : args) {
  1838. TensorLayout dst_layout;
  1839. auto opr = handle->create_operator<ConvBias>();
  1840. opr->param() = arg.param;
  1841. opr->deduce_layout({arg.src, dtype::Float32()},
  1842. {arg.filter, dtype::Float32()},
  1843. {arg.bias, dtype::Float32()}, {}, dst_layout);
  1844. //! dst.nr_elems * IC * FH * FW * 2
  1845. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1846. arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
  1847. (1024 * 1024 * 1024) * 1e3;
  1848. auto used = benchmark.set_param(arg.param).exec(
  1849. {arg.src, arg.filter, {}, {}, {}}) /
  1850. RUN;
  1851. benchmark_winograd.set_param(arg.param);
  1852. auto used_winograd =
  1853. algo_benchmark<ConvBias>(benchmark_winograd,
  1854. {arg.src, arg.filter, {}, {}, {}},
  1855. algo_name) /
  1856. RUN;
  1857. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  1858. "speedup: "
  1859. "%f\n",
  1860. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1861. used, computations / used, used_winograd,
  1862. computations / used_winograd, used / used_winograd);
  1863. }
  1864. }
  1865. }
  1866. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
  1867. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
  1868. }
  1869. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  1870. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
  1871. }
  1872. #endif
  1873. } // namespace test
  1874. } // namespace megdnn
  1875. // vim: syntax=cpp.doxygen

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

Contributors (1)