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

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

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