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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  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_DENSE) {
  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_LARGE_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_GROUP) {
  581. using namespace conv_bias;
  582. std::vector<TestArg> args;
  583. auto run = [&](size_t group, size_t channel, size_t w, size_t h,
  584. size_t kernel, 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. param.sparse = param::ConvBias::Sparse::GROUP;
  594. //! no bias
  595. args.emplace_back(
  596. param, TensorShape{1, channel, h, w},
  597. TensorShape{group, channel / group, channel / group, kernel, kernel},
  598. TensorShape{});
  599. //! bias channel
  600. args.emplace_back(param, TensorShape{2, channel, h, w},
  601. TensorShape{group, channel / group, channel / group,
  602. kernel, kernel},
  603. TensorShape{1, channel, 1, 1});
  604. //! bias
  605. args.emplace_back(
  606. param, TensorShape{2, channel, h, w},
  607. TensorShape{group, channel / group, channel / group, kernel,
  608. kernel},
  609. TensorShape{2, channel, (h + param.pad_h * 2 - kernel) + 1,
  610. (w + param.pad_w * 2 - kernel) + 1});
  611. };
  612. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
  613. for (size_t channel : {4, 8, 16})
  614. for (size_t group : {1, 2, 4})
  615. for (size_t p : {0, 2})
  616. for (size_t size : {20, 21, 24})
  617. for (NonlineMode nonline_mode :
  618. {NonlineMode::RELU, NonlineMode::SIGMOID,
  619. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  620. run(group, channel, size, size, kernel, p,
  621. nonline_mode);
  622. }
  623. Checker<ConvBias> checker(handle());
  624. UniformIntRNG rng{-50, 50};
  625. checker.set_dtype(0, dtype::Float32())
  626. .set_dtype(1, dtype::Float32())
  627. .set_dtype(2, dtype::Float32())
  628. .set_rng(0, &rng)
  629. .set_rng(1, &rng)
  630. .set_rng(2, &rng);
  631. checker.set_before_exec_callback(
  632. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  633. "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP"));
  634. for (auto&& arg : args) {
  635. checker.set_param(arg.param).exec(
  636. {arg.src, arg.filter, arg.bias, {}, {}});
  637. }
  638. }
  639. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE2_DENSE) {
  640. using namespace conv_bias;
  641. std::vector<TestArg> args;
  642. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  643. size_t p, NonlineMode nonline_mode) {
  644. if (w + 2 * p < kernel || h + 2 * p < kernel)
  645. return;
  646. param::ConvBias param;
  647. param.stride_h = 2;
  648. param.stride_w = 2;
  649. param.pad_h = p;
  650. param.pad_w = p;
  651. param.nonlineMode = nonline_mode;
  652. //! no bias
  653. args.emplace_back(param, TensorShape{1, ic, h, w},
  654. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  655. };
  656. for (size_t kernel : {2, 3, 5, 7})
  657. for (size_t ic : {1, 4, 8, 16})
  658. for (size_t oc : {1, 4, 8})
  659. for (size_t p : {0, 2})
  660. for (size_t size : {20, 21, 24})
  661. for (NonlineMode nonline_mode :
  662. {NonlineMode::RELU, NonlineMode::SIGMOID,
  663. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  664. run(oc, ic, size, size, kernel, p, nonline_mode);
  665. }
  666. Checker<ConvBias> checker(handle());
  667. UniformIntRNG rng{-50, 50};
  668. checker.set_dtype(0, dtype::Float32())
  669. .set_dtype(1, dtype::Float32())
  670. .set_dtype(2, dtype::Float32())
  671. .set_rng(0, &rng)
  672. .set_rng(1, &rng)
  673. .set_rng(2, &rng);
  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_DIRECT_STRIDE2_GROUP) {
  683. using namespace conv_bias;
  684. std::vector<TestArg> args;
  685. auto run = [&](size_t group, size_t channel, size_t w, size_t h,
  686. size_t kernel, 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 = 2;
  691. param.stride_w = 2;
  692. param.pad_h = p;
  693. param.pad_w = p;
  694. param.nonlineMode = nonline_mode;
  695. param.sparse = param::ConvBias::Sparse::GROUP;
  696. //! no bias
  697. args.emplace_back(
  698. param, TensorShape{1, channel, h, w},
  699. TensorShape{group, channel / group, channel / group, kernel, kernel},
  700. TensorShape{});
  701. //! bias channel
  702. args.emplace_back(param, TensorShape{2, channel, h, w},
  703. TensorShape{group, channel / group, channel / group,
  704. kernel, kernel},
  705. TensorShape{1, channel, 1, 1});
  706. //! bias
  707. args.emplace_back(
  708. param, TensorShape{2, channel, h, w},
  709. TensorShape{group, channel / group, channel / group, kernel,
  710. kernel},
  711. TensorShape{2, channel, (h + param.pad_h * 2 - kernel) / 2 + 1,
  712. (w + param.pad_w * 2 - kernel) / 2 + 1});
  713. };
  714. for (size_t kernel : {2, 3, 5, 7})
  715. for (size_t channel : {4, 8, 16})
  716. for (size_t group : {1, 2, 4})
  717. for (size_t p : {0, 2})
  718. for (size_t size : {20, 21, 24})
  719. for (NonlineMode nonline_mode :
  720. {NonlineMode::RELU, NonlineMode::SIGMOID,
  721. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  722. run(group, channel, size, size, kernel, p,
  723. nonline_mode);
  724. }
  725. Checker<ConvBias> checker(handle());
  726. UniformIntRNG rng{-50, 50};
  727. checker.set_dtype(0, dtype::Float32())
  728. .set_dtype(1, dtype::Float32())
  729. .set_dtype(2, dtype::Float32())
  730. .set_rng(0, &rng)
  731. .set_rng(1, &rng)
  732. .set_rng(2, &rng);
  733. checker.set_before_exec_callback(
  734. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  735. "X86_CONV_BIAS_DIRECT_STRIDE2_LARGE_GROUP"));
  736. for (auto&& arg : args) {
  737. checker.set_param(arg.param).exec(
  738. {arg.src, arg.filter, arg.bias, {}, {}});
  739. }
  740. }
  741. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_INT8X8X32) {
  742. using namespace conv_bias;
  743. std::vector<TestArg> args;
  744. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  745. size_t p, NonlineMode nonline_mode) {
  746. if (w + 2 * p < kernel || h + 2 * p < kernel)
  747. return;
  748. param::ConvBias param;
  749. param.stride_h = 1;
  750. param.stride_w = 1;
  751. param.pad_h = p;
  752. param.pad_w = p;
  753. param.nonlineMode = nonline_mode;
  754. //! no bias
  755. args.emplace_back(param, TensorShape{1, ic, h, w},
  756. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  757. args.emplace_back(param, TensorShape{1, ic, h, w},
  758. TensorShape{oc, ic, kernel, kernel},
  759. TensorShape{1, oc, 1, 1});
  760. args.emplace_back(param, TensorShape{1, ic, h, w},
  761. TensorShape{oc, ic, kernel, kernel},
  762. TensorShape{1, oc, (h + 2 * p - kernel) + 1,
  763. (h + 2 * p - kernel) + 1});
  764. };
  765. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  766. for (size_t ic : {1, 4, 8, 16})
  767. for (size_t oc : {1, 4, 8})
  768. for (size_t p : {0, 2})
  769. for (size_t size : {20, 21, 24})
  770. for (NonlineMode nonline_mode :
  771. {NonlineMode::IDENTITY}) {
  772. run(oc, ic, size, size, kernel, p, nonline_mode);
  773. }
  774. //! test OC block
  775. run(2046, 1, 8, 8, 2, 0, NonlineMode::IDENTITY);
  776. Checker<ConvBias> checker(handle());
  777. UniformIntRNG rng{-50, 50};
  778. #define cb(algo_name) \
  779. checker.set_before_exec_callback( \
  780. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  781. checker.set_dtype(0, dtype::Int8()); \
  782. checker.set_dtype(1, dtype::Int8()); \
  783. checker.set_dtype(2, dtype::Int32()); \
  784. checker.set_dtype(4, dtype::Int32()); \
  785. for (auto&& arg : args) { \
  786. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
  787. } \
  788. for (auto&& arg : args) { \
  789. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  790. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  791. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  792. .set_dtype(4, {}) \
  793. .set_rng(0, &rng) \
  794. .set_rng(1, &rng) \
  795. .set_rng(2, &rng) \
  796. .set_param(arg.param) \
  797. .execs({arg.src, arg.filter, {}, {}, {}}); \
  798. }
  799. #define cb2(algo_name) \
  800. checker.set_before_exec_callback( \
  801. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  802. checker.set_dtype(0, dtype::Int8()); \
  803. checker.set_dtype(1, dtype::Int8()); \
  804. checker.set_dtype(2, dtype::Int16()); \
  805. checker.set_dtype(4, dtype::Int16()); \
  806. for (auto&& arg : args) { \
  807. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
  808. }
  809. #if MEGDNN_X86_WITH_MKL_DNN
  810. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  811. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  812. }
  813. #endif
  814. #if MEGDNN_X86_WITH_VNNI
  815. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  816. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  817. }
  818. #endif
  819. if (megdnn::x86::is_supported(x86::SIMDType::AVX2)) {
  820. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  821. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2");
  822. cb2("IM2COLMATMUL:X86_INT8X8X16_AVX2");
  823. }
  824. if (::megdnn::x86::is_supported(::megdnn::x86::SIMDType::SSE4_2)) {
  825. cb("IM2COLMATMUL:X86_INT8X8X32_SSE_4X8X2");
  826. cb2("IM2COLMATMUL:X86_INT8X8X16_SSE");
  827. }
  828. #undef cb
  829. #undef cb2
  830. }
  831. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_INT8X8X32_FILTER_PREPROCESS) {
  832. using namespace conv_bias;
  833. std::vector<TestArg> args;
  834. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  835. size_t p, NonlineMode nonline_mode) {
  836. if (w + 2 * p < kernel || h + 2 * p < kernel)
  837. return;
  838. param::ConvBias param;
  839. param.stride_h = 1;
  840. param.stride_w = 1;
  841. param.pad_h = p;
  842. param.pad_w = p;
  843. param.nonlineMode = nonline_mode;
  844. //! no bias
  845. args.emplace_back(param, TensorShape{1, ic, h, w},
  846. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  847. };
  848. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  849. for (size_t ic : {1, 4, 8, 16})
  850. for (size_t oc : {1, 4, 8})
  851. for (size_t p : {0, 2})
  852. for (size_t size : {20, 21, 24})
  853. for (NonlineMode nonline_mode :
  854. {NonlineMode::IDENTITY}) {
  855. run(oc, ic, size, size, kernel, p, nonline_mode);
  856. }
  857. //! test OC block
  858. run(2046, 1, 8, 8, 2, 0, NonlineMode::IDENTITY);
  859. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  860. handle());
  861. UniformIntRNG rng{-50, 50};
  862. #define cb(algo_name) \
  863. checker.set_before_exec_callback( \
  864. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  865. checker.set_dtype(0, dtype::Int8()); \
  866. checker.set_dtype(1, dtype::Int8()); \
  867. checker.set_dtype(2, dtype::Int32()); \
  868. checker.set_dtype(4, dtype::Int32()); \
  869. for (auto&& arg : args) { \
  870. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
  871. } \
  872. for (auto&& arg : args) { \
  873. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  874. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  875. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  876. .set_dtype(4, {}) \
  877. .set_rng(0, &rng) \
  878. .set_rng(1, &rng) \
  879. .set_rng(2, &rng) \
  880. .set_param(arg.param) \
  881. .execs({arg.src, arg.filter, {}, {}, {}}); \
  882. }
  883. #define cb2(algo_name) \
  884. checker.set_before_exec_callback( \
  885. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  886. checker.set_dtype(0, dtype::Int8()); \
  887. checker.set_dtype(1, dtype::Int8()); \
  888. checker.set_dtype(2, dtype::Int16()); \
  889. checker.set_dtype(4, dtype::Int16()); \
  890. for (auto&& arg : args) { \
  891. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
  892. }
  893. #if MEGDNN_X86_WITH_MKL_DNN
  894. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  895. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  896. }
  897. #endif
  898. #if MEGDNN_X86_WITH_VNNI
  899. if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
  900. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  901. }
  902. #endif
  903. if (megdnn::x86::is_supported(x86::SIMDType::AVX2)) {
  904. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  905. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2");
  906. cb2("IM2COLMATMUL:X86_INT8X8X16_AVX2");
  907. }
  908. if (::megdnn::x86::is_supported(::megdnn::x86::SIMDType::SSE4_2)) {
  909. cb("IM2COLMATMUL:X86_INT8X8X32_SSE_4X8X2");
  910. cb2("IM2COLMATMUL:X86_INT8X8X16_SSE");
  911. }
  912. #undef cb
  913. #undef cb2
  914. }
  915. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32) {
  916. using namespace conv_bias;
  917. std::vector<TestArg> args;
  918. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  919. size_t p, NonlineMode nonline_mode) {
  920. if (w + 2 * p < kernel || h + 2 * p < kernel)
  921. return;
  922. param::ConvBias param;
  923. param.stride_h = 1;
  924. param.stride_w = 1;
  925. param.pad_h = p;
  926. param.pad_w = p;
  927. param.nonlineMode = nonline_mode;
  928. //! no bias
  929. args.emplace_back(param, TensorShape{1, ic, h, w},
  930. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  931. args.emplace_back(param, TensorShape{1, ic, h, w},
  932. TensorShape{oc, ic, kernel, kernel},
  933. TensorShape{1, oc, 1, 1});
  934. args.emplace_back(
  935. param, TensorShape{1, ic, h, w},
  936. TensorShape{oc, ic, kernel, kernel},
  937. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  938. (w + 2 * p - kernel) / param.stride_w + 1});
  939. };
  940. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  941. for (size_t ic : {1, 4, 8, 16})
  942. for (size_t oc : {1, 4, 8, 16, 300})
  943. for (size_t p : {0, 2})
  944. for (size_t size : {8, 24})
  945. for (NonlineMode nonline_mode :
  946. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  947. run(oc, ic, size, size, kernel, p, nonline_mode);
  948. }
  949. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  950. Checker<ConvBias> checker(handle());
  951. #define cb(algo_name) \
  952. checker.set_before_exec_callback( \
  953. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  954. for (auto&& arg : args) { \
  955. checker.set_param(arg.param).execs( \
  956. {arg.src, arg.filter, arg.bias, {}, {}}); \
  957. }
  958. #if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
  959. cb("IM2COLMATMUL:X86_F32_BLAS");
  960. #endif
  961. #undef cb
  962. }
  963. #if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
  964. TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32) {
  965. using namespace conv_bias;
  966. std::vector<TestArg> args;
  967. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  968. size_t p, NonlineMode nonline_mode) {
  969. if (w + 2 * p < kernel || h + 2 * p < kernel)
  970. return;
  971. param::ConvBias param;
  972. param.stride_h = 1;
  973. param.stride_w = 1;
  974. param.pad_h = p;
  975. param.pad_w = p;
  976. param.nonlineMode = nonline_mode;
  977. //! no bias
  978. args.emplace_back(param, TensorShape{1, ic, h, w},
  979. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  980. args.emplace_back(param, TensorShape{1, ic, h, w},
  981. TensorShape{oc, ic, kernel, kernel},
  982. TensorShape{1, oc, 1, 1});
  983. args.emplace_back(
  984. param, TensorShape{1, ic, h, w},
  985. TensorShape{oc, ic, kernel, kernel},
  986. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  987. (w + 2 * p - kernel) / param.stride_w + 1});
  988. };
  989. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  990. for (size_t ic : {1, 4, 8, 16})
  991. for (size_t oc : {1, 4, 8, 16, 300})
  992. for (size_t p : {0, 2})
  993. for (size_t size : {8, 24})
  994. for (NonlineMode nonline_mode :
  995. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  996. run(oc, ic, size, size, kernel, p, nonline_mode);
  997. }
  998. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  999. Checker<ConvBias> checker(handle());
  1000. #define cb(algo_name) \
  1001. checker.set_before_exec_callback( \
  1002. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1003. for (auto&& arg : args) { \
  1004. checker.set_param(arg.param).execs( \
  1005. {arg.src, arg.filter, arg.bias, {}, {}}); \
  1006. }
  1007. cb("IM2COLMATMUL:X86_F32_BLAS");
  1008. #undef cb
  1009. }
  1010. TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32_NOPACK_PREPROCESS) {
  1011. using namespace conv_bias;
  1012. std::vector<TestArg> args;
  1013. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1014. size_t p, NonlineMode nonline_mode) {
  1015. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1016. return;
  1017. param::ConvBias param;
  1018. param.stride_h = 1;
  1019. param.stride_w = 1;
  1020. param.pad_h = p;
  1021. param.pad_w = p;
  1022. param.nonlineMode = nonline_mode;
  1023. //! no bias
  1024. args.emplace_back(param, TensorShape{1, ic, h, w},
  1025. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1026. args.emplace_back(param, TensorShape{1, ic, h, w},
  1027. TensorShape{oc, ic, kernel, kernel},
  1028. TensorShape{1, oc, 1, 1});
  1029. args.emplace_back(
  1030. param, TensorShape{1, ic, h, w},
  1031. TensorShape{oc, ic, kernel, kernel},
  1032. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1033. (w + 2 * p - kernel) / param.stride_w + 1});
  1034. };
  1035. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1036. for (size_t ic : {1, 4, 8, 16})
  1037. for (size_t oc : {1, 4, 8, 16, 300})
  1038. for (size_t p : {0, 2})
  1039. for (size_t size : {8, 24})
  1040. for (NonlineMode nonline_mode :
  1041. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1042. run(oc, ic, size, size, kernel, p, nonline_mode);
  1043. }
  1044. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1045. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1046. handle());
  1047. #define cb(algo_name) \
  1048. checker.set_before_exec_callback( \
  1049. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1050. for (auto&& arg : args) { \
  1051. checker.set_param(arg.param).execs( \
  1052. {arg.src, arg.filter, arg.bias, {}, {}}); \
  1053. }
  1054. cb("IM2COLMATMUL:X86_F32_BLAS");
  1055. #undef cb
  1056. }
  1057. #endif
  1058. #if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
  1059. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA) {
  1060. using namespace conv_bias;
  1061. std::vector<TestArg> args;
  1062. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1063. size_t p, NonlineMode nonline_mode) {
  1064. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1065. return;
  1066. param::ConvBias param;
  1067. param.stride_h = 1;
  1068. param.stride_w = 1;
  1069. param.pad_h = p;
  1070. param.pad_w = p;
  1071. param.nonlineMode = nonline_mode;
  1072. //! no bias
  1073. args.emplace_back(param, TensorShape{1, ic, h, w},
  1074. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1075. args.emplace_back(param, TensorShape{1, ic, h, w},
  1076. TensorShape{oc, ic, kernel, kernel},
  1077. TensorShape{1, oc, 1, 1});
  1078. args.emplace_back(
  1079. param, TensorShape{1, ic, h, w},
  1080. TensorShape{oc, ic, kernel, kernel},
  1081. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1082. (w + 2 * p - kernel) / param.stride_w + 1});
  1083. param.sparse = param::ConvBias::Sparse::GROUP;
  1084. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  1085. TensorShape{2, oc, ic, kernel, kernel},
  1086. TensorShape{});
  1087. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  1088. TensorShape{2, oc, ic, kernel, kernel},
  1089. TensorShape{1, oc * 2, 1, 1});
  1090. args.emplace_back(
  1091. param, TensorShape{1, 2 * ic, h, w},
  1092. TensorShape{2, oc, ic, kernel, kernel},
  1093. TensorShape{1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  1094. (w + 2 * param.pad_w - kernel) / 1 + 1});
  1095. };
  1096. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1097. for (size_t ic : {1, 4, 8, 16})
  1098. for (size_t oc : {1, 4, 8, 16})
  1099. for (size_t p : {0, 1})
  1100. for (size_t size : {8, 24})
  1101. for (NonlineMode nonline_mode :
  1102. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1103. run(oc, ic, size, size, kernel, p, nonline_mode);
  1104. }
  1105. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1106. Checker<ConvBias> checker(handle());
  1107. #define cb(algo_name) \
  1108. checker.set_before_exec_callback( \
  1109. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1110. for (auto&& arg : args) { \
  1111. checker.set_param(arg.param).execs( \
  1112. {arg.src, arg.filter, arg.bias, {}, {}}); \
  1113. }
  1114. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  1115. #undef cb
  1116. }
  1117. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA_FILTER_PREPROCESS) {
  1118. using namespace conv_bias;
  1119. std::vector<TestArg> args;
  1120. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1121. size_t p, NonlineMode nonline_mode) {
  1122. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1123. return;
  1124. param::ConvBias param;
  1125. param.stride_h = 1;
  1126. param.stride_w = 1;
  1127. param.pad_h = p;
  1128. param.pad_w = p;
  1129. param.nonlineMode = nonline_mode;
  1130. //! no bias
  1131. args.emplace_back(param, TensorShape{1, ic, h, w},
  1132. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1133. args.emplace_back(param, TensorShape{1, ic, h, w},
  1134. TensorShape{oc, ic, kernel, kernel},
  1135. TensorShape{1, oc, 1, 1});
  1136. args.emplace_back(
  1137. param, TensorShape{1, ic, h, w},
  1138. TensorShape{oc, ic, kernel, kernel},
  1139. TensorShape{1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1140. (w + 2 * p - kernel) / param.stride_w + 1});
  1141. param.sparse = param::ConvBias::Sparse::GROUP;
  1142. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  1143. TensorShape{2, oc, ic, kernel, kernel},
  1144. TensorShape{});
  1145. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  1146. TensorShape{2, oc, ic, kernel, kernel},
  1147. TensorShape{1, oc * 2, 1, 1});
  1148. args.emplace_back(
  1149. param, TensorShape{1, 2 * ic, h, w},
  1150. TensorShape{2, oc, ic, kernel, kernel},
  1151. TensorShape{1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  1152. (w + 2 * param.pad_w - kernel) / 1 + 1});
  1153. };
  1154. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1155. for (size_t ic : {1, 4, 8, 16})
  1156. for (size_t oc : {1, 4, 8, 16})
  1157. for (size_t p : {0, 1})
  1158. for (size_t size : {8, 24})
  1159. for (NonlineMode nonline_mode :
  1160. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1161. run(oc, ic, size, size, kernel, p, nonline_mode);
  1162. }
  1163. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1164. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1165. handle());
  1166. #define cb(algo_name) \
  1167. checker.set_before_exec_callback( \
  1168. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1169. for (auto&& arg : args) { \
  1170. checker.set_param(arg.param).execs( \
  1171. {arg.src, arg.filter, arg.bias, {}, {}}); \
  1172. }
  1173. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  1174. #undef cb
  1175. }
  1176. /**************************** Conv1x1 PackA *************************/
  1177. namespace {
  1178. void checker_conv_bias(std::vector<conv_bias::TestArg> args, Handle* handle,
  1179. RNG* rng, float epsilon, DType type0, DType type1,
  1180. DType type2, DType type3, const char* algo_name) {
  1181. using namespace conv_bias;
  1182. Checker<ConvBias> checker(handle);
  1183. checker.set_before_exec_callback(
  1184. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1185. checker.set_dtype(0, type0);
  1186. checker.set_dtype(1, type1);
  1187. checker.set_dtype(2, type2);
  1188. checker.set_dtype(4, type3);
  1189. checker.set_epsilon(epsilon);
  1190. if (NULL != rng) {
  1191. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1192. }
  1193. for (auto&& arg : args) {
  1194. checker.set_param(arg.param).execs(
  1195. {arg.src, arg.filter, arg.bias, {}, {}});
  1196. }
  1197. }
  1198. void checker_conv_bias_preprocess(std::vector<conv_bias::TestArg> args, Handle* handle,
  1199. RNG* rng, float epsilon, DType type0, DType type1,
  1200. DType type2, DType type3, const char* algo_name) {
  1201. using namespace conv_bias;
  1202. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1203. handle);
  1204. checker.set_before_exec_callback(
  1205. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1206. checker.set_dtype(0, type0);
  1207. checker.set_dtype(1, type1);
  1208. checker.set_dtype(2, type2);
  1209. checker.set_dtype(4, type3);
  1210. checker.set_epsilon(epsilon);
  1211. if (NULL != rng) {
  1212. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1213. }
  1214. for (auto&& arg : args) {
  1215. checker.set_param(arg.param).execs(
  1216. {arg.src, arg.filter, arg.bias, {}, {}});
  1217. }
  1218. }
  1219. } // namespace
  1220. #if MEGDNN_X86_WITH_MKL
  1221. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA) {
  1222. using namespace conv_bias;
  1223. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1224. check_conv_bias(args, handle(), "CONV1x1:X86_F32_MKL_PACKA:24");
  1225. }
  1226. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA_PREPROCESS) {
  1227. using namespace conv_bias;
  1228. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1229. checker_conv_bias_preprocess(args, handle(), nullptr, 0.001,
  1230. dtype::Float32{}, dtype::Float32{},
  1231. dtype::Float32{}, dtype::Float32{},
  1232. "CONV1x1:X86_F32_MKL_PACKA:24");
  1233. }
  1234. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS) {
  1235. using namespace conv_bias;
  1236. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1237. check_conv_bias(args, handle(), "CONV1x1:X86_F32_BLAS:48");
  1238. }
  1239. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS_NOPACK_REPROCESS) {
  1240. using namespace conv_bias;
  1241. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1242. checker_conv_bias_preprocess(args, handle(), nullptr, 0.001,
  1243. dtype::Float32{}, dtype::Float32{},
  1244. dtype::Float32{}, dtype::Float32{},
  1245. "CONV1x1:X86_F32_BLAS:24");
  1246. }
  1247. #endif
  1248. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32) {
  1249. using namespace conv_bias;
  1250. UniformIntRNG rng{-50, 50};
  1251. float epsilon = 0.001;
  1252. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
  1253. #if MEGDNN_X86_WITH_MKL_DNN
  1254. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1255. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1256. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1257. "CONV1x1:X86_INT8X8X32_MKLDNN:24");
  1258. }
  1259. #endif
  1260. #if MEGDNN_X86_WITH_VNNI
  1261. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1262. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1263. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1264. "CONV1x1:X86_INT8X8X32_VNNI:24");
  1265. }
  1266. #endif
  1267. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1268. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1269. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1270. "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
  1271. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1272. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1273. "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
  1274. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1275. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  1276. "CONV1x1:X86_INT8X8X16_AVX2");
  1277. }
  1278. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1279. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1280. "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
  1281. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  1282. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  1283. "CONV1x1:X86_INT8X8X16_SSE");
  1284. }
  1285. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32_PREPROCESS) {
  1286. using namespace conv_bias;
  1287. UniformIntRNG rng{-50, 50};
  1288. float epsilon = 0.001;
  1289. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
  1290. #if MEGDNN_X86_WITH_VNNI
  1291. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1292. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1293. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1294. "CONV1x1:X86_INT8X8X32_VNNI:24");
  1295. }
  1296. #endif
  1297. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1298. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1299. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1300. "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
  1301. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1302. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1303. "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
  1304. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1305. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  1306. "CONV1x1:X86_INT8X8X16_AVX2");
  1307. }
  1308. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1309. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  1310. "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
  1311. checker_conv_bias_preprocess(args, handle(), &rng, epsilon, dtype::Int8{},
  1312. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  1313. "CONV1x1:X86_INT8X8X16_SSE");
  1314. }
  1315. /************************* End Conv1x1 PackA ************************/
  1316. #endif
  1317. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8) {
  1318. using namespace conv_bias;
  1319. std::vector<TestArg> args;
  1320. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1321. size_t p, NonlineMode nonline_mode) {
  1322. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1323. return;
  1324. param::ConvBias param;
  1325. param.stride_h = 1;
  1326. param.stride_w = 1;
  1327. param.pad_h = p;
  1328. param.pad_w = p;
  1329. param.nonlineMode = nonline_mode;
  1330. //! no bias
  1331. args.emplace_back(param, TensorShape{1, ic, h, w},
  1332. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1333. //! bias channel
  1334. args.emplace_back(param, TensorShape{2, ic, h, w},
  1335. TensorShape{oc, ic, kernel, kernel},
  1336. TensorShape{1, oc, 1, 1});
  1337. };
  1338. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1339. for (size_t ic : {1, 4, 8, 16})
  1340. for (size_t oc : {1, 4, 8})
  1341. for (size_t p : {0, 2})
  1342. for (size_t size : {20, 21, 24})
  1343. for (NonlineMode nonline_mode :
  1344. {NonlineMode::IDENTITY, NonlineMode::RELU,
  1345. NonlineMode::H_SWISH}) {
  1346. run(oc, ic, size, size, kernel, p, nonline_mode);
  1347. }
  1348. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1349. Checker<ConvBias> checker(handle());
  1350. #define cb(algo_name) \
  1351. checker.set_before_exec_callback( \
  1352. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1353. UniformIntRNG rng{-50, 50}; \
  1354. for (auto&& arg : args) { \
  1355. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  1356. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  1357. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  1358. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  1359. .set_rng(0, &rng) \
  1360. .set_rng(1, &rng) \
  1361. .set_rng(2, &rng) \
  1362. .set_param(arg.param) \
  1363. .execs({arg.src, arg.filter, {}, {}, {}}); \
  1364. }
  1365. #if MEGDNN_X86_WITH_MKL_DNN
  1366. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1367. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  1368. }
  1369. #endif
  1370. #if MEGDNN_X86_WITH_VNNI
  1371. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1372. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  1373. }
  1374. #endif
  1375. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1376. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  1377. }
  1378. #undef cb
  1379. }
  1380. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8_FILTER_PREPROCESS) {
  1381. using namespace conv_bias;
  1382. std::vector<TestArg> args;
  1383. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1384. size_t p, NonlineMode nonline_mode) {
  1385. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1386. return;
  1387. param::ConvBias param;
  1388. param.stride_h = 1;
  1389. param.stride_w = 1;
  1390. param.pad_h = p;
  1391. param.pad_w = p;
  1392. param.nonlineMode = nonline_mode;
  1393. //! no bias
  1394. args.emplace_back(param, TensorShape{1, ic, h, w},
  1395. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1396. //! bias channel
  1397. args.emplace_back(param, TensorShape{2, ic, h, w},
  1398. TensorShape{oc, ic, kernel, kernel},
  1399. TensorShape{1, oc, 1, 1});
  1400. };
  1401. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1402. for (size_t ic : {1, 4, 8, 16})
  1403. for (size_t oc : {1, 4, 8})
  1404. for (size_t p : {0, 2})
  1405. for (size_t size : {20, 21, 24})
  1406. for (NonlineMode nonline_mode :
  1407. {NonlineMode::IDENTITY, NonlineMode::RELU,
  1408. NonlineMode::H_SWISH}) {
  1409. run(oc, ic, size, size, kernel, p, nonline_mode);
  1410. }
  1411. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1412. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1413. handle());
  1414. #define cb(algo_name) \
  1415. checker.set_before_exec_callback( \
  1416. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1417. UniformIntRNG rng{-50, 50}; \
  1418. for (auto&& arg : args) { \
  1419. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  1420. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  1421. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  1422. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  1423. .set_rng(0, &rng) \
  1424. .set_rng(1, &rng) \
  1425. .set_rng(2, &rng) \
  1426. .set_param(arg.param) \
  1427. .execs({arg.src, arg.filter, {}, {}, {}}); \
  1428. }
  1429. #if MEGDNN_X86_WITH_MKL_DNN
  1430. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1431. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  1432. }
  1433. #endif
  1434. #if MEGDNN_X86_WITH_VNNI
  1435. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1436. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  1437. }
  1438. #endif
  1439. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1440. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  1441. }
  1442. #undef cb
  1443. }
  1444. TEST_F(X86, CONV_BIAS_MATMUL) {
  1445. using namespace conv_bias;
  1446. std::vector<TestArg> args;
  1447. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1448. size_t p, NonlineMode nonline_mode) {
  1449. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1450. return;
  1451. param::ConvBias param;
  1452. param.stride_h = 1;
  1453. param.stride_w = 1;
  1454. param.pad_h = p;
  1455. param.pad_w = p;
  1456. param.nonlineMode = nonline_mode;
  1457. //! no bias
  1458. param.sparse = param::ConvBias::Sparse::DENSE;
  1459. args.emplace_back(param, TensorShape{1, ic, h, w},
  1460. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1461. //! bias channel
  1462. args.emplace_back(param, TensorShape{2, ic, h, w},
  1463. TensorShape{oc, ic, kernel, kernel},
  1464. TensorShape{1, oc, 1, 1});
  1465. //! bias
  1466. args.emplace_back(param, TensorShape{2, ic, h, w},
  1467. TensorShape{oc, ic, kernel, kernel},
  1468. TensorShape{2, oc, (h + param.pad_h * 2 - kernel) + 1,
  1469. (w + param.pad_w * 2 - kernel) + 1});
  1470. //! gruop
  1471. param.sparse = param::ConvBias::Sparse::GROUP;
  1472. args.emplace_back(
  1473. param, TensorShape{2, 2 * ic, h, w},
  1474. TensorShape{2, oc, ic, kernel, kernel},
  1475. TensorShape{2, 2 * oc, (h + param.pad_h * 2 - kernel) + 1,
  1476. (w + param.pad_w * 2 - kernel) + 1});
  1477. };
  1478. for (size_t kernel : {2, 3, 5, 7})
  1479. for (size_t ic : {1, 2, 3, 4})
  1480. for (size_t oc : {1, 2, 3, 4})
  1481. for (size_t p : {0, 2})
  1482. for (size_t size : {20, 21, 22, 23, 24})
  1483. for (NonlineMode nonline_mode :
  1484. {NonlineMode::RELU, NonlineMode::SIGMOID,
  1485. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  1486. run(oc, ic, size, size, kernel, p, nonline_mode);
  1487. }
  1488. Checker<ConvBias> checker(handle());
  1489. checker.set_before_exec_callback(
  1490. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1491. "X86_CONV_BIAS_MATMUL"));
  1492. checker.set_epsilon(1);
  1493. UniformIntRNG rng{-50, 50};
  1494. checker.set_dtype(0, dtype::Float32())
  1495. .set_dtype(1, dtype::Float32())
  1496. .set_dtype(2, dtype::Float32())
  1497. .set_rng(0, &rng)
  1498. .set_rng(1, &rng)
  1499. .set_rng(2, &rng);
  1500. for (auto&& arg : args) {
  1501. checker.set_param(arg.param).exec(
  1502. {arg.src, arg.filter, arg.bias, {}, {}});
  1503. }
  1504. }
  1505. #if MEGDNN_WITH_BENCHMARK
  1506. #if MEGDNN_X86_WITH_MKL_DNN
  1507. static void x86_benchmark_fp32_mkldnn(Handle* handle) {
  1508. constexpr size_t RUNS = 30;
  1509. param::ConvBias param;
  1510. Benchmarker<ConvBias> benchmarker_mkldnn(handle);
  1511. benchmarker_mkldnn.set_display(false).set_times(RUNS);
  1512. benchmarker_mkldnn.set_before_exec_callback(
  1513. AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));
  1514. Benchmarker<ConvBias> benchmarker_im2col(handle);
  1515. benchmarker_im2col.set_display(false).set_times(RUNS);
  1516. benchmarker_im2col.set_before_exec_callback(
  1517. AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
  1518. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1519. size_t FS, size_t SZ, size_t GROUP = 1) {
  1520. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  1521. bias({1, OC, 1, 1}), z({}), dst({N, OC, H / SZ, W / SZ});
  1522. param.pad_h = FS / 2;
  1523. param.pad_w = FS / 2;
  1524. param.stride_h = SZ;
  1525. param.stride_w = SZ;
  1526. param.format = param::ConvBias::Format::NCHW;
  1527. param.sparse = param::ConvBias::Sparse::DENSE;
  1528. if (GROUP > 1) {
  1529. param.sparse = param::ConvBias::Sparse::GROUP;
  1530. filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
  1531. }
  1532. auto im2col_used = benchmarker_im2col.set_param(param).exec(
  1533. {src, filter, bias, z, dst}) /
  1534. RUNS;
  1535. src = IC < 8 ? TensorShape{N, IC, H, W}
  1536. : TensorShape{N, IC / 8, H, W, 8};
  1537. filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
  1538. : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
  1539. if (GROUP > 1 && OC == GROUP && IC == GROUP) {
  1540. filter = {GROUP / 8, 1, 1, FS, FS, 8};
  1541. } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
  1542. filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
  1543. }
  1544. bias = {1, OC / 8, 1, 1, 8};
  1545. z = {};
  1546. dst = {N, OC / 8, H / SZ, W / SZ, 8};
  1547. param.format = param::ConvBias::Format::NCHW88;
  1548. auto mkldnn_used = benchmarker_mkldnn.set_param(param).exec(
  1549. {src, filter, bias, z, dst}) /
  1550. RUNS;
  1551. float computations =
  1552. (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  1553. std::cout << "run " << src.to_string() << " " << filter.to_string()
  1554. << " " << bias.to_string() << " " << dst.to_string()
  1555. << std::endl;
  1556. std::cout << "im2col: " << im2col_used << " ms, "
  1557. << (computations / im2col_used) << " Gops, ";
  1558. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  1559. << (computations / mkldnn_used) << " Gops, "
  1560. << "spped up: " << (im2col_used / mkldnn_used) << ", ";
  1561. std::cout << std::endl;
  1562. };
  1563. run(1, 64, 64, 56, 56, 3, 1);
  1564. run(1, 3, 64, 224, 224, 3, 1);
  1565. run(1, 3, 64, 224, 224, 7, 2);
  1566. run(1, 64, 64, 56, 56, 3, 1);
  1567. run(1, 128, 128, 28, 28, 3, 1);
  1568. run(1, 256, 256, 14, 14, 3, 1);
  1569. run(1, 512, 512, 7, 7, 3, 1);
  1570. run(1, 256, 64, 56, 56, 1, 1);
  1571. run(1, 512, 128, 28, 28, 1, 1);
  1572. run(1, 1024, 256, 14, 14, 1, 1);
  1573. run(1, 2048, 512, 7, 7, 1, 1);
  1574. run(1, 32, 32, 112, 112, 3, 1, 32);
  1575. run(1, 144, 144, 56, 56, 3, 1, 144);
  1576. run(1, 192, 192, 28, 28, 3, 1, 192);
  1577. run(1, 384, 384, 28, 28, 3, 1, 384);
  1578. run(1, 576, 576, 14, 14, 3, 1, 576);
  1579. run(1, 960, 960, 7, 7, 3, 1, 960);
  1580. run(1, 256, 128, 56, 56, 1, 2, 1);
  1581. run(1, 512, 256, 28, 28, 1, 2, 1);
  1582. run(1, 1024, 512, 14, 14, 1, 2, 1);
  1583. run(1, 96, 96, 112, 112, 3, 2, 96);
  1584. run(1, 144, 144, 56, 56, 3, 2, 144);
  1585. run(1, 384, 384, 28, 28, 3, 2, 384);
  1586. run(1, 576, 576, 14, 14, 3, 2, 576);
  1587. }
  1588. TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1589. x86_benchmark_fp32_mkldnn(handle());
  1590. }
  1591. TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1592. x86_benchmark_fp32_mkldnn(handle());
  1593. }
  1594. #endif
  1595. #endif
  1596. /************************* Winograd ****************************/
  1597. namespace {
  1598. std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
  1599. std::vector<conv_bias::TestArg> args;
  1600. param::ConvBias cur_param;
  1601. cur_param.format = param::ConvBias::Format::NCHW88;
  1602. using NLMode = param::ConvBias::NonlineMode;
  1603. // clang-format off
  1604. for (auto nlmode :
  1605. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  1606. for (size_t ic : {1, 2}) {
  1607. for (size_t oc : {1, 2}) {
  1608. for (size_t i : {9, 63}) {
  1609. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  1610. cur_param.nonlineMode = nlmode;
  1611. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1612. cur_param.pad_h = cur_param.pad_w = 1;
  1613. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1614. TensorShape{oc, ic, 3, 3, 8, 8},
  1615. TensorShape{1, oc, 1, 1, 8});
  1616. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1617. TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
  1618. //! bias
  1619. args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
  1620. TensorShape{oc, ic, 3, 3, 8, 8},
  1621. TensorShape{2, oc, i, i, 8});
  1622. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1623. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
  1624. TensorShape{2, oc, ic, 3, 3, 8, 8},
  1625. TensorShape{1, 2 * oc, 1, 1, 8});*/
  1626. }}}
  1627. // clang-format on
  1628. //! test for multi-thread OC parallel
  1629. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1630. cur_param.pad_h = cur_param.pad_w = 1;
  1631. args.emplace_back(cur_param, TensorShape{2, 1, 9, 9, 8},
  1632. TensorShape{128, 1, 3, 3, 8, 8},
  1633. TensorShape{1, 128, 1, 1, 8});
  1634. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1635. args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
  1636. TensorShape{2, 128, 1, 3, 3, 8, 8},
  1637. TensorShape{1, 2 * 128, 1, 1, 8});*/
  1638. }
  1639. return args;
  1640. }
  1641. } // namespace
  1642. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
  1643. using namespace conv_bias;
  1644. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1645. Checker<ConvBiasForward> checker(handle());
  1646. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1647. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1648. for (auto&& arg : args) {
  1649. checker.set_param(arg.param).execs(
  1650. {arg.src, arg.filter, arg.bias, {}, {}});
  1651. }
  1652. }
  1653. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63_WEIGHT_PREPROCESS) {
  1654. using namespace conv_bias;
  1655. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1656. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1657. handle());
  1658. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1659. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1660. for (auto&& arg : args) {
  1661. checker.set_param(arg.param).execs(
  1662. {arg.src, arg.filter, arg.bias, {}, {}});
  1663. }
  1664. }
  1665. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
  1666. using namespace conv_bias;
  1667. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1668. Checker<ConvBiasForward> checker(handle());
  1669. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1670. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1671. for (auto&& arg : args) {
  1672. checker.set_param(arg.param).execs(
  1673. {arg.src, arg.filter, arg.bias, {}, {}});
  1674. }
  1675. }
  1676. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23_WEIGHT_PREPROCESS) {
  1677. using namespace conv_bias;
  1678. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1679. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1680. handle());
  1681. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1682. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1683. for (auto&& arg : args) {
  1684. checker.set_param(arg.param).execs(
  1685. {arg.src, arg.filter, arg.bias, {}, {}});
  1686. }
  1687. }
  1688. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
  1689. using namespace conv_bias;
  1690. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1691. Checker<ConvBiasForward> checker(handle());
  1692. auto extra_impl = [](const TensorNDArray& tensors, uint32_t m,
  1693. param::ConvBias param, Handle* handle) {
  1694. megdnn_assert(param.format == param::ConvBias::Format::NCHW88);
  1695. auto winograd_preprocess_opr =
  1696. handle->create_operator<WinogradFilterPreprocess>();
  1697. winograd_preprocess_opr->param().output_block_size = m;
  1698. winograd_preprocess_opr->param().format = param::MatrixMul::Format::MK8;
  1699. TensorLayout filter_transform_layout;
  1700. winograd_preprocess_opr->deduce_layout(tensors[1].layout,
  1701. filter_transform_layout);
  1702. size_t winograd_preprocess_workspace_in_bytes =
  1703. winograd_preprocess_opr->get_workspace_in_bytes(
  1704. tensors[1].layout, filter_transform_layout);
  1705. auto conv_bias_opr = handle->create_operator<ConvBias>();
  1706. conv_bias_opr->param() = param;
  1707. conv_bias_opr->param().format =
  1708. param::ConvBias::Format::NCHW88_WINOGRAD;
  1709. conv_bias_opr->param().output_block_size = m;
  1710. size_t conv_bias_workspace_in_bytes =
  1711. conv_bias_opr->get_workspace_in_bytes(
  1712. tensors[0].layout, filter_transform_layout,
  1713. tensors[2].layout, tensors[3].layout, tensors[4].layout,
  1714. nullptr);
  1715. WorkspaceBundle wb(nullptr, {filter_transform_layout.span().dist_byte(),
  1716. conv_bias_workspace_in_bytes,
  1717. winograd_preprocess_workspace_in_bytes});
  1718. wb.set(malloc(wb.total_size_in_bytes()));
  1719. TensorND filter_transform_tensor(wb.get(0),
  1720. std::move(filter_transform_layout));
  1721. winograd_preprocess_opr->exec(tensors[1], filter_transform_tensor,
  1722. wb.get_workspace(2));
  1723. conv_bias_opr->exec(tensors[0], filter_transform_tensor, tensors[2],
  1724. tensors[3], tensors[4], nullptr,
  1725. wb.get_workspace(1));
  1726. free(wb.ptr());
  1727. };
  1728. auto run = [&checker, &extra_impl](
  1729. Handle* handle, const std::vector<TestArg>& args,
  1730. const std::vector<size_t>& out_size, DType A_dtype,
  1731. DType B_dtype, DType C_dtype, DType D_dtype,
  1732. const float eps) {
  1733. for (auto&& arg : args) {
  1734. for (uint32_t m : out_size) {
  1735. checker.set_extra_opr_impl(std::bind(extra_impl,
  1736. std::placeholders::_1, m,
  1737. arg.param, handle));
  1738. checker.set_dtype(0, A_dtype)
  1739. .set_dtype(1, B_dtype)
  1740. .set_dtype(2, C_dtype)
  1741. .set_dtype(4, D_dtype)
  1742. .set_epsilon(eps)
  1743. .set_param(arg.param)
  1744. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  1745. }
  1746. }
  1747. };
  1748. run(handle(), args, {2, 6}, dtype::Float32(), dtype::Float32(),
  1749. dtype::Float32(), dtype::Float32(), 1e-3f);
  1750. }
  1751. /*********************************** End winograd ************************/
  1752. #if MEGDNN_X86_WITH_MKL_DNN
  1753. static void x86_correctness_fp32_mkldnn_run(
  1754. Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
  1755. ConvBiasForward::BiasMode bias_mode,
  1756. param::ConvBias::NonlineMode noline_mode, size_t n, size_t stride,
  1757. size_t kernel, size_t oc, size_t ic, size_t h, size_t w, size_t group) {
  1758. auto oc_per_group = oc / group;
  1759. auto ic_per_group = ic / group;
  1760. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  1761. (ic_per_group % 8 == 0 || ic_per_group == 3) &&
  1762. ic_per_group > 0;
  1763. bool ok_depthwise = oc == ic && oc == group;
  1764. if (!(ok_group || ok_depthwise)) {
  1765. return;
  1766. }
  1767. size_t pad = kernel / 2;
  1768. size_t kernel_h = kernel;
  1769. size_t kernel_w = kernel;
  1770. param::ConvBias param;
  1771. param.format = param::ConvBias::Format::NCHW88;
  1772. param.stride_h = stride;
  1773. param.stride_w = stride;
  1774. param.pad_h = pad;
  1775. param.pad_w = pad;
  1776. param.nonlineMode = noline_mode;
  1777. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  1778. if (ic == 3) {
  1779. src_tensor_shape = TensorShape{n, ic, h, w};
  1780. }
  1781. auto weight_tensor_shape =
  1782. TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  1783. if (ic == 3) {
  1784. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  1785. }
  1786. auto bias_tensor_shape = TensorShape{};
  1787. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1788. bias_tensor_shape = {1, oc / 8, 1, 1, 8};
  1789. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1790. TensorLayout dst_layout;
  1791. auto ConvBiasOp = handle->create_operator<ConvBias>();
  1792. ConvBiasOp->param() = param;
  1793. ConvBiasOp->deduce_layout({src_tensor_shape, dtype::Float32()},
  1794. {weight_tensor_shape, dtype::Float32()}, {},
  1795. {}, dst_layout);
  1796. bias_tensor_shape = dst_layout;
  1797. }
  1798. if (group == 1) {
  1799. param.sparse = param::ConvBias::Sparse::DENSE;
  1800. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1801. param.sparse = param::ConvBias::Sparse::GROUP;
  1802. weight_tensor_shape =
  1803. TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  1804. } else if (group > 1 && oc / group % 8 == 0 && oc / group > 0 &&
  1805. ic / group % 8 == 0 && ic / group > 0) {
  1806. param.sparse = param::ConvBias::Sparse::GROUP;
  1807. weight_tensor_shape = TensorShape{
  1808. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8,
  1809. 8};
  1810. }
  1811. checker.set_dtype(0, dtype::Float32())
  1812. .set_dtype(1, dtype::Float32())
  1813. .set_dtype(2, dtype::Float32())
  1814. .set_dtype(4, dtype::Float32())
  1815. .set_rng(0, &rng)
  1816. .set_rng(1, &rng)
  1817. .set_rng(2, &rng)
  1818. .set_epsilon(1e-3)
  1819. .set_param(param)
  1820. .execs({src_tensor_shape,
  1821. weight_tensor_shape,
  1822. bias_tensor_shape,
  1823. {},
  1824. {}});
  1825. }
  1826. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  1827. Checker<ConvBias> checker(handle);
  1828. UniformIntRNG rng{-127, 127};
  1829. checker.set_before_exec_callback(
  1830. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1831. "MKLDNN_CONV_FP32"));
  1832. for (auto bias_mode :
  1833. {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
  1834. megdnn::BiasMode::BIAS})
  1835. for (auto noline_mode : {param::ConvBias::NonlineMode::IDENTITY,
  1836. param::ConvBias::NonlineMode::SIGMOID,
  1837. param::ConvBias::NonlineMode::H_SWISH})
  1838. for (size_t n : {1, 2})
  1839. for (size_t stride : {1, 2})
  1840. for (size_t kernel : {3, 5, 7})
  1841. for (size_t oc : {8, 16})
  1842. for (size_t ic : {3, 8, 16})
  1843. for (size_t h : {22, 33})
  1844. for (size_t w : {22, 33}) {
  1845. for (size_t group = 1;
  1846. group <= std::min(oc, ic);
  1847. ++group) {
  1848. x86_correctness_fp32_mkldnn_run(
  1849. checker, rng, handle,
  1850. bias_mode, noline_mode, n,
  1851. stride, kernel, oc, ic, h,
  1852. w, group);
  1853. }
  1854. }
  1855. }
  1856. TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1857. x86_correctness_fp32_mkldnn(handle());
  1858. }
  1859. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1860. x86_correctness_fp32_mkldnn(handle());
  1861. }
  1862. TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
  1863. using namespace conv_bias;
  1864. std::vector<TestArg> args;
  1865. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1866. size_t p, NonlineMode nonline_mode) {
  1867. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1868. return;
  1869. param::ConvBias param;
  1870. param.stride_h = 1;
  1871. param.stride_w = 1;
  1872. param.pad_h = p;
  1873. param.pad_w = p;
  1874. param.nonlineMode = nonline_mode;
  1875. //! no bias
  1876. args.emplace_back(param, TensorShape{1, ic, h, w},
  1877. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1878. };
  1879. for (size_t kernel : {2, 3, 5, 7})
  1880. for (size_t ic : {1, 2, 3, 4})
  1881. for (size_t oc : {1, 2, 4})
  1882. for (size_t p : {0, 2})
  1883. for (size_t size : {20, 21, 22, 23, 24})
  1884. for (NonlineMode nonline_mode :
  1885. {NonlineMode::IDENTITY}) {
  1886. run(oc, ic, size, size, kernel, p, nonline_mode);
  1887. }
  1888. Checker<ConvBias> checker(handle());
  1889. checker.set_before_exec_callback(
  1890. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1891. "MKLDNN_MATMUL_INT8"));
  1892. checker.set_epsilon(1);
  1893. UniformIntRNG rng{-50, 50};
  1894. checker.set_dtype(0, dtype::Int8())
  1895. .set_dtype(1, dtype::Int8())
  1896. .set_dtype(2, dtype::Int32())
  1897. .set_dtype(4, dtype::Int32())
  1898. .set_rng(0, &rng)
  1899. .set_rng(1, &rng)
  1900. .set_rng(2, &rng);
  1901. for (auto&& arg : args) {
  1902. checker.set_param(arg.param).exec(
  1903. {arg.src, arg.filter, arg.bias, {}, {}});
  1904. }
  1905. }
  1906. TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
  1907. using namespace conv_bias;
  1908. std::vector<TestArg> args;
  1909. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1910. size_t p, NonlineMode nonline_mode) {
  1911. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1912. return;
  1913. param::ConvBias param;
  1914. param.stride_h = 1;
  1915. param.stride_w = 1;
  1916. param.pad_h = p;
  1917. param.pad_w = p;
  1918. param.nonlineMode = nonline_mode;
  1919. //! no bias
  1920. args.emplace_back(param, TensorShape{1, ic, h, w},
  1921. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1922. };
  1923. for (size_t kernel : {2, 3, 5, 7})
  1924. for (size_t ic : {1, 2, 3, 4})
  1925. for (size_t oc : {1, 2, 4})
  1926. for (size_t p : {0, 2})
  1927. for (size_t size : {20, 22, 24})
  1928. for (NonlineMode nonline_mode :
  1929. {NonlineMode::IDENTITY}) {
  1930. run(oc, ic, size, size, kernel, p, nonline_mode);
  1931. }
  1932. Checker<ConvBias> checker(handle());
  1933. checker.set_before_exec_callback(
  1934. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1935. checker.set_epsilon(1);
  1936. UniformIntRNG rng{-50, 50};
  1937. checker.set_dtype(0, dtype::Int8())
  1938. .set_dtype(1, dtype::Int8())
  1939. .set_dtype(2, dtype::Int32())
  1940. .set_dtype(4, dtype::Int32())
  1941. .set_rng(0, &rng)
  1942. .set_rng(1, &rng)
  1943. .set_rng(2, &rng);
  1944. for (auto&& arg : args) {
  1945. checker.set_param(arg.param).exec(
  1946. {arg.src, arg.filter, arg.bias, {}, {}});
  1947. }
  1948. }
  1949. TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
  1950. using namespace conv_bias;
  1951. std::vector<TestArg> args;
  1952. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1953. size_t p, NonlineMode nonline_mode) {
  1954. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1955. return;
  1956. param::ConvBias param;
  1957. param.stride_h = 1;
  1958. param.stride_w = 1;
  1959. param.pad_h = p;
  1960. param.pad_w = p;
  1961. param.nonlineMode = nonline_mode;
  1962. //! no bias
  1963. args.emplace_back(param, TensorShape{1, ic, h, w},
  1964. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1965. };
  1966. for (size_t kernel : {2, 3, 5, 7})
  1967. for (size_t ic : {1, 2, 3, 4})
  1968. for (size_t oc : {1, 2, 4})
  1969. for (size_t p : {0, 2})
  1970. for (size_t size : {20, 22, 24})
  1971. for (NonlineMode nonline_mode :
  1972. {NonlineMode::IDENTITY}) {
  1973. run(oc, ic, size, size, kernel, p, nonline_mode);
  1974. }
  1975. Checker<ConvBias> checker(handle());
  1976. checker.set_before_exec_callback(
  1977. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1978. checker.set_epsilon(1);
  1979. UniformIntRNG rng{-50, 50};
  1980. checker.set_dtype(0, dtype::Int8())
  1981. .set_dtype(1, dtype::Int8())
  1982. .set_dtype(2, dtype::Int32())
  1983. .set_dtype(4, dtype::Int32())
  1984. .set_rng(0, &rng)
  1985. .set_rng(1, &rng)
  1986. .set_rng(2, &rng);
  1987. for (auto&& arg : args) {
  1988. checker.set_param(arg.param).exec(
  1989. {arg.src, arg.filter, arg.bias, {}, {}});
  1990. }
  1991. }
  1992. #endif
  1993. #if MEGDNN_WITH_BENCHMARK
  1994. namespace {
  1995. void benchmark_impl(const param::ConvBias param,
  1996. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1997. shapes_and_computation,
  1998. const std::string algo_name, size_t RUNS,
  1999. TaskExecutorConfig&& multi_thread_config,
  2000. TaskExecutorConfig&& single_thread_config,
  2001. std::vector<DType> dtype_v) {
  2002. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2003. dtype::Float32(), dtype::Float32()};
  2004. std::vector<float> multi_thread_times, single_thread_times;
  2005. {
  2006. auto multi_thread_hanle =
  2007. create_cpu_handle(0, true, &multi_thread_config);
  2008. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  2009. benchmarker.set_times(RUNS)
  2010. .set_display(false)
  2011. .set_dtype(0, dtype_v[0])
  2012. .set_dtype(1, dtype_v[1])
  2013. .set_dtype(2, dtype_v[2])
  2014. .set_dtype(4, dtype_v[3])
  2015. .set_param(param)
  2016. .set_before_exec_callback(
  2017. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2018. algo_name.c_str()));
  2019. for (auto shape : shapes_and_computation) {
  2020. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2021. }
  2022. }
  2023. {
  2024. auto single_thread_handle =
  2025. create_cpu_handle(0, true, &single_thread_config);
  2026. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  2027. benchmarker.set_times(RUNS)
  2028. .set_display(false)
  2029. .set_dtype(0, dtype_v[0])
  2030. .set_dtype(1, dtype_v[1])
  2031. .set_dtype(2, dtype_v[2])
  2032. .set_dtype(4, dtype_v[3])
  2033. .set_param(param)
  2034. .set_before_exec_callback(
  2035. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2036. algo_name.c_str()));
  2037. for (auto shape : shapes_and_computation) {
  2038. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2039. }
  2040. }
  2041. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  2042. printf("core_ids:");
  2043. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  2044. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  2045. }
  2046. printf(", Single thread core_id %zu\n",
  2047. single_thread_config.affinity_core_set[0]);
  2048. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  2049. auto shapes = shapes_and_computation[i];
  2050. printf("Bench case: ");
  2051. for (auto&& shape : shapes.first) {
  2052. printf("%s ", shape.to_string().c_str());
  2053. }
  2054. float computations = shapes.second;
  2055. printf("%zu threads gflops: %f,\n single thread gflops: "
  2056. "%f. spead up = %f, speedup/cores=%f\n",
  2057. multi_thread_config.nr_thread,
  2058. computations / multi_thread_times[i],
  2059. computations / single_thread_times[i],
  2060. single_thread_times[i] / multi_thread_times[i],
  2061. single_thread_times[i] / multi_thread_times[i] /
  2062. multi_thread_config.nr_thread);
  2063. }
  2064. }
  2065. void benchmark_impl_comp(
  2066. const param::ConvBias param,
  2067. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  2068. shapes_and_computation,
  2069. const std::string algo_name, const std::string algo_name1, size_t RUNS,
  2070. TaskExecutorConfig&& multi_thread_config,
  2071. TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
  2072. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2073. dtype::Float32(), dtype::Float32()};
  2074. std::vector<float> multi_thread_times, single_thread_times;
  2075. {
  2076. auto multi_thread_hanle =
  2077. create_cpu_handle(0, true, &multi_thread_config);
  2078. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  2079. benchmarker.set_times(RUNS)
  2080. .set_display(false)
  2081. .set_dtype(0, dtype_v[0])
  2082. .set_dtype(1, dtype_v[1])
  2083. .set_dtype(2, dtype_v[2])
  2084. .set_dtype(4, dtype_v[3])
  2085. .set_param(param)
  2086. .set_before_exec_callback(
  2087. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2088. algo_name.c_str()));
  2089. for (auto shape : shapes_and_computation) {
  2090. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2091. }
  2092. }
  2093. {
  2094. auto single_thread_handle =
  2095. create_cpu_handle(0, true, &single_thread_config);
  2096. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  2097. benchmarker.set_times(RUNS)
  2098. .set_display(false)
  2099. .set_dtype(0, dtype_v[0])
  2100. .set_dtype(1, dtype_v[1])
  2101. .set_dtype(2, dtype_v[2])
  2102. .set_dtype(4, dtype_v[3])
  2103. .set_param(param)
  2104. .set_before_exec_callback(
  2105. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2106. algo_name1.c_str()));
  2107. for (auto shape : shapes_and_computation) {
  2108. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2109. }
  2110. }
  2111. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  2112. printf("core_ids:");
  2113. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  2114. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  2115. }
  2116. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  2117. auto shapes = shapes_and_computation[i];
  2118. printf("Bench case: ");
  2119. for (auto&& shape : shapes.first) {
  2120. printf("%s ", shape.to_string().c_str());
  2121. }
  2122. float computations = shapes.second;
  2123. printf("algo:%s gflops: %f,\n algo:%s gflops: "
  2124. "%f. spead up = %f\n",
  2125. algo_name.c_str(), computations / multi_thread_times[i],
  2126. algo_name1.c_str(), computations / single_thread_times[i],
  2127. single_thread_times[i] / multi_thread_times[i]);
  2128. }
  2129. }
  2130. } // namespace
  2131. static void benchmark_convbias_chanwise_avx2_int8(uint32_t stride,
  2132. const char* algo) {
  2133. constexpr size_t RUNS = 50;
  2134. param::ConvBias param;
  2135. param.stride_h = stride;
  2136. param.stride_w = stride;
  2137. param.sparse = param::ConvBias::Sparse::GROUP;
  2138. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2139. dtype::Int32(), dtype::Int32()};
  2140. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2141. shapes_and_computation;
  2142. auto bench_case = [&](size_t N, size_t IC, size_t H, size_t W, size_t FS) {
  2143. param.pad_h = FS / 2;
  2144. param.pad_w = FS / 2;
  2145. SmallVector<TensorShape> shapes{
  2146. {N, IC, H, W}, {IC, 1, 1, FS, FS}, {}, {}, {}};
  2147. TensorShape dst{N, IC, (H + 2 * param.pad_h - FS) + 1,
  2148. (W + 2 * param.pad_w - FS) + 1};
  2149. float computations = (FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2150. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2151. };
  2152. bench_case(1, 32, 112, 112, 7);
  2153. bench_case(1, 144, 56, 56, 7);
  2154. bench_case(1, 192, 28, 28, 7);
  2155. bench_case(1, 384, 28, 28, 7);
  2156. bench_case(1, 576, 14, 14, 7);
  2157. bench_case(1, 960, 7, 7, 7);
  2158. bench_case(1, 32, 112, 112, 5);
  2159. bench_case(1, 144, 56, 56, 5);
  2160. bench_case(1, 192, 28, 28, 5);
  2161. bench_case(1, 384, 28, 28, 5);
  2162. bench_case(1, 576, 14, 14, 5);
  2163. bench_case(1, 960, 7, 7, 5);
  2164. bench_case(1, 32, 112, 112, 3);
  2165. bench_case(1, 144, 56, 56, 3);
  2166. bench_case(1, 192, 28, 28, 3);
  2167. bench_case(1, 384, 28, 28, 3);
  2168. bench_case(1, 576, 14, 14, 3);
  2169. bench_case(1, 960, 7, 7, 3);
  2170. bench_case(1, 32, 112, 112, 2);
  2171. bench_case(1, 144, 56, 56, 2);
  2172. bench_case(1, 192, 28, 28, 2);
  2173. bench_case(1, 384, 28, 28, 2);
  2174. bench_case(1, 576, 14, 14, 2);
  2175. bench_case(1, 960, 7, 7, 2);
  2176. std::string algo_name = algo;
  2177. printf("Benchmark %s\n", algo);
  2178. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2179. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2180. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2181. {1, {4}}, data_type);
  2182. shapes_and_computation.clear();
  2183. }
  2184. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S1) {
  2185. benchmark_convbias_chanwise_avx2_int8(
  2186. 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
  2187. }
  2188. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S2) {
  2189. benchmark_convbias_chanwise_avx2_int8(
  2190. 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
  2191. }
  2192. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
  2193. constexpr size_t RUNS = 50;
  2194. param::ConvBias param;
  2195. param.stride_h = 1;
  2196. param.stride_w = 1;
  2197. param.sparse = param::ConvBias::Sparse::DENSE;
  2198. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2199. dtype::Int32(), dtype::Int32()};
  2200. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2201. shapes_and_computation;
  2202. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2203. size_t FS) {
  2204. param.pad_h = FS / 2;
  2205. param.pad_w = FS / 2;
  2206. SmallVector<TensorShape> shapes{
  2207. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2208. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) + 1,
  2209. (W + 2 * param.pad_w - FS) + 1};
  2210. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2211. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2212. };
  2213. bench_case(1, 32, 32, 200, 200, 7);
  2214. bench_case(1, 32, 64, 200, 200, 7);
  2215. bench_case(1, 32, 32, 128, 128, 7);
  2216. bench_case(1, 32, 64, 128, 128, 7);
  2217. bench_case(1, 32, 32, 100, 100, 7);
  2218. bench_case(1, 32, 64, 100, 100, 7);
  2219. bench_case(1, 32, 32, 80, 80, 7);
  2220. bench_case(1, 32, 64, 80, 80, 7);
  2221. bench_case(1, 32, 32, 200, 200, 5);
  2222. bench_case(1, 32, 64, 200, 200, 5);
  2223. bench_case(1, 32, 32, 128, 128, 5);
  2224. bench_case(1, 32, 64, 128, 128, 5);
  2225. bench_case(1, 32, 32, 100, 100, 5);
  2226. bench_case(1, 32, 64, 100, 100, 5);
  2227. bench_case(1, 32, 32, 80, 80, 5);
  2228. bench_case(1, 32, 64, 80, 80, 5);
  2229. bench_case(1, 32, 32, 200, 200, 3);
  2230. bench_case(1, 32, 64, 200, 200, 3);
  2231. bench_case(1, 32, 32, 128, 128, 3);
  2232. bench_case(1, 32, 64, 128, 128, 3);
  2233. bench_case(1, 32, 32, 100, 100, 3);
  2234. bench_case(1, 32, 64, 100, 100, 3);
  2235. bench_case(1, 32, 32, 80, 80, 3);
  2236. bench_case(1, 32, 64, 80, 80, 3);
  2237. bench_case(1, 32, 32, 200, 200, 2);
  2238. bench_case(1, 32, 64, 200, 200, 2);
  2239. bench_case(1, 32, 32, 128, 128, 2);
  2240. bench_case(1, 32, 64, 128, 128, 2);
  2241. bench_case(1, 32, 32, 100, 100, 2);
  2242. bench_case(1, 32, 64, 100, 100, 2);
  2243. bench_case(1, 32, 32, 80, 80, 2);
  2244. bench_case(1, 32, 64, 80, 80, 2);
  2245. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
  2246. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
  2247. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2248. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2249. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2250. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2251. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2252. {1, {4}}, data_type);
  2253. shapes_and_computation.clear();
  2254. }
  2255. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_8816) {
  2256. constexpr size_t RUNS = 30;
  2257. param::ConvBias param;
  2258. param.stride_h = 1;
  2259. param.stride_w = 1;
  2260. param.sparse = param::ConvBias::Sparse::DENSE;
  2261. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2262. dtype::Int16(), dtype::Int16()};
  2263. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2264. shapes_and_computation;
  2265. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2266. size_t FS) {
  2267. param.pad_h = FS / 2;
  2268. param.pad_w = FS / 2;
  2269. SmallVector<TensorShape> shapes{
  2270. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2271. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2272. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2273. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2274. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2275. };
  2276. bench_case(1, 48, 192, 15, 15, 1);
  2277. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X16_AVX2";
  2278. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2279. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2280. shapes_and_computation.clear();
  2281. }
  2282. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  2283. BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
  2284. constexpr size_t RUNS = 50;
  2285. param::ConvBias param;
  2286. param.stride_h = 2;
  2287. param.stride_w = 2;
  2288. param.sparse = param::ConvBias::Sparse::DENSE;
  2289. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2290. dtype::Int32(), dtype::Int32()};
  2291. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2292. shapes_and_computation;
  2293. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2294. size_t FS) {
  2295. param.pad_h = FS / 2;
  2296. param.pad_w = FS / 2;
  2297. SmallVector<TensorShape> shapes{
  2298. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2299. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2300. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2301. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2302. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2303. };
  2304. bench_case(1, 32, 32, 200, 200, 7);
  2305. bench_case(1, 32, 64, 200, 200, 7);
  2306. bench_case(1, 32, 32, 128, 128, 7);
  2307. bench_case(1, 32, 64, 128, 128, 7);
  2308. bench_case(1, 32, 32, 100, 100, 7);
  2309. bench_case(1, 32, 64, 100, 100, 7);
  2310. bench_case(1, 32, 32, 80, 80, 7);
  2311. bench_case(1, 32, 64, 80, 80, 7);
  2312. bench_case(1, 32, 32, 200, 200, 5);
  2313. bench_case(1, 32, 64, 200, 200, 5);
  2314. bench_case(1, 32, 32, 128, 128, 5);
  2315. bench_case(1, 32, 64, 128, 128, 5);
  2316. bench_case(1, 32, 32, 100, 100, 5);
  2317. bench_case(1, 32, 64, 100, 100, 5);
  2318. bench_case(1, 32, 32, 80, 80, 5);
  2319. bench_case(1, 32, 64, 80, 80, 5);
  2320. bench_case(1, 32, 32, 200, 200, 3);
  2321. bench_case(1, 32, 64, 200, 200, 3);
  2322. bench_case(1, 32, 32, 128, 128, 3);
  2323. bench_case(1, 32, 64, 128, 128, 3);
  2324. bench_case(1, 32, 32, 100, 100, 3);
  2325. bench_case(1, 32, 64, 100, 100, 3);
  2326. bench_case(1, 32, 32, 80, 80, 3);
  2327. bench_case(1, 32, 64, 80, 80, 3);
  2328. bench_case(1, 32, 32, 200, 200, 2);
  2329. bench_case(1, 32, 64, 200, 200, 2);
  2330. bench_case(1, 32, 32, 128, 128, 2);
  2331. bench_case(1, 32, 64, 128, 128, 2);
  2332. bench_case(1, 32, 32, 100, 100, 2);
  2333. bench_case(1, 32, 64, 100, 100, 2);
  2334. bench_case(1, 32, 32, 80, 80, 2);
  2335. bench_case(1, 32, 64, 80, 80, 2);
  2336. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
  2337. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
  2338. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2339. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2340. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2341. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2342. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2343. {1, {4}}, data_type);
  2344. shapes_and_computation.clear();
  2345. }
  2346. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
  2347. constexpr size_t RUNS = 50;
  2348. param::ConvBias param;
  2349. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2350. param.pad_h = 1;
  2351. param.pad_w = 1;
  2352. param.stride_h = 1;
  2353. param.stride_w = 1;
  2354. param.sparse = param::ConvBias::Sparse::GROUP;
  2355. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2356. dtype::Float32(), dtype::Float32()};
  2357. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2358. shapes_and_computation;
  2359. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2360. size_t FS, size_t group) {
  2361. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2362. {group, OC / group, IC / group, FS, FS},
  2363. {1, OC, 1, 1},
  2364. {},
  2365. {N, OC, H, W}};
  2366. TensorShape dst{N, OC, H, W};
  2367. float computations =
  2368. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2369. dst.total_nr_elems()) *
  2370. 1e-6;
  2371. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2372. };
  2373. bench_case(1, 32, 32, 200, 200, 3, 4);
  2374. bench_case(1, 32, 32, 200, 200, 3, 32);
  2375. bench_case(1, 32, 32, 128, 128, 3, 4);
  2376. bench_case(1, 32, 32, 128, 128, 3, 32);
  2377. bench_case(1, 32, 32, 100, 100, 3, 4);
  2378. bench_case(1, 32, 32, 100, 100, 3, 32);
  2379. bench_case(1, 32, 32, 80, 80, 3, 4);
  2380. bench_case(1, 32, 32, 80, 80, 3, 32);
  2381. std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2382. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_GROUP algo\n");
  2383. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2384. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2385. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2386. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2387. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2388. {1, {4}}, data_type);
  2389. shapes_and_computation.clear();
  2390. algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2391. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_DENSE algo\n");
  2392. bench_case(1, 32, 32, 200, 200, 3, 1);
  2393. bench_case(1, 32, 32, 128, 128, 3, 1);
  2394. bench_case(1, 32, 32, 100, 100, 3, 1);
  2395. bench_case(1, 32, 32, 80, 80, 3, 1);
  2396. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2397. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2398. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2399. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2400. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2401. {1, {4}}, data_type);
  2402. }
  2403. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
  2404. constexpr size_t RUNS = 50;
  2405. param::ConvBias param;
  2406. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2407. param.pad_h = 1;
  2408. param.pad_w = 1;
  2409. param.stride_h = 1;
  2410. param.stride_w = 1;
  2411. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2412. dtype::Float32(), dtype::Float32()};
  2413. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2414. shapes_and_computation;
  2415. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2416. size_t FS, size_t group) {
  2417. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2418. {OC / group, IC / group, FS, FS},
  2419. {1, OC, 1, 1},
  2420. {},
  2421. {N, OC, H, W}};
  2422. TensorShape dst{N, OC, H, W};
  2423. float computations =
  2424. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2425. dst.total_nr_elems()) *
  2426. 1e-6;
  2427. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2428. };
  2429. bench_case(1, 32, 32, 200, 200, 3, 1);
  2430. bench_case(1, 32, 32, 200, 200, 3, 1);
  2431. bench_case(1, 32, 32, 128, 128, 3, 1);
  2432. bench_case(1, 32, 32, 128, 128, 3, 1);
  2433. bench_case(1, 32, 32, 100, 100, 3, 1);
  2434. bench_case(1, 32, 32, 100, 100, 3, 1);
  2435. bench_case(1, 32, 32, 80, 80, 3, 1);
  2436. bench_case(1, 32, 32, 80, 80, 3, 1);
  2437. bench_case(1, 64, 32, 7, 7, 3, 1);
  2438. bench_case(1, 64, 64, 7, 7, 3, 1);
  2439. bench_case(1, 64, 128, 7, 7, 3, 1);
  2440. bench_case(1, 64, 256, 7, 7, 3, 1);
  2441. bench_case(1, 64, 512, 7, 7, 3, 1);
  2442. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2443. bench_case(1, 64, 32, 14, 14, 3, 1);
  2444. bench_case(1, 64, 64, 14, 14, 3, 1);
  2445. bench_case(1, 64, 128, 14, 14, 3, 1);
  2446. bench_case(1, 64, 256, 14, 14, 3, 1);
  2447. bench_case(1, 64, 512, 14, 14, 3, 1);
  2448. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2449. bench_case(1, 128, 128, 14, 14, 3, 1);
  2450. bench_case(1, 128, 256, 14, 14, 3, 1);
  2451. bench_case(1, 512, 512, 14, 14, 3, 1);
  2452. bench_case(1, 256, 512, 14, 14, 3, 1);
  2453. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2454. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2455. std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
  2456. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2457. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2458. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2459. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2460. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2461. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2462. {1, {4}}, data_type);
  2463. shapes_and_computation.clear();
  2464. }
  2465. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  2466. BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
  2467. constexpr size_t RUNS = 50;
  2468. param::ConvBias param;
  2469. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2470. param.pad_h = 1;
  2471. param.pad_w = 1;
  2472. param.stride_h = 1;
  2473. param.stride_w = 1;
  2474. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2475. dtype::Float32(), dtype::Float32()};
  2476. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2477. shapes_and_computation;
  2478. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2479. size_t FS, size_t group) {
  2480. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2481. {OC / group, IC / group, FS, FS},
  2482. {1, OC, 1, 1},
  2483. {},
  2484. {N, OC, H, W}};
  2485. TensorShape dst{N, OC, H, W};
  2486. float computations =
  2487. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2488. dst.total_nr_elems()) *
  2489. 1e-6;
  2490. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2491. };
  2492. bench_case(1, 32, 32, 200, 200, 3, 1);
  2493. bench_case(1, 32, 32, 200, 200, 3, 1);
  2494. bench_case(1, 32, 32, 128, 128, 3, 1);
  2495. bench_case(1, 32, 32, 128, 128, 3, 1);
  2496. bench_case(1, 32, 32, 100, 100, 3, 1);
  2497. bench_case(1, 32, 32, 100, 100, 3, 1);
  2498. bench_case(1, 32, 32, 80, 80, 3, 1);
  2499. bench_case(1, 32, 32, 80, 80, 3, 1);
  2500. bench_case(1, 64, 32, 7, 7, 3, 1);
  2501. bench_case(1, 64, 64, 7, 7, 3, 1);
  2502. bench_case(1, 64, 128, 7, 7, 3, 1);
  2503. bench_case(1, 64, 256, 7, 7, 3, 1);
  2504. bench_case(1, 64, 512, 7, 7, 3, 1);
  2505. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2506. bench_case(1, 64, 32, 14, 14, 3, 1);
  2507. bench_case(1, 64, 64, 14, 14, 3, 1);
  2508. bench_case(1, 64, 128, 14, 14, 3, 1);
  2509. bench_case(1, 64, 256, 14, 14, 3, 1);
  2510. bench_case(1, 64, 512, 14, 14, 3, 1);
  2511. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2512. bench_case(1, 128, 128, 14, 14, 3, 1);
  2513. bench_case(1, 128, 256, 14, 14, 3, 1);
  2514. bench_case(1, 512, 512, 14, 14, 3, 1);
  2515. bench_case(1, 256, 512, 14, 14, 3, 1);
  2516. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2517. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2518. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  2519. std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
  2520. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2521. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2522. RUNS, {1, {4}}, {1, {4}}, data_type);
  2523. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2524. RUNS, {1, {7}}, {1, {7}}, data_type);
  2525. shapes_and_computation.clear();
  2526. }
  2527. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
  2528. constexpr size_t RUNS = 50;
  2529. param::ConvBias param;
  2530. param.pad_h = 1;
  2531. param.pad_w = 1;
  2532. param.stride_h = 1;
  2533. param.stride_w = 1;
  2534. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2535. shapes_and_computation;
  2536. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2537. size_t FS, size_t group) {
  2538. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2539. {OC / group, IC / group, FS, FS},
  2540. {1, OC, 1, 1},
  2541. {},
  2542. {N, OC, H, W}};
  2543. TensorShape dst{N, OC, H, W};
  2544. float computations =
  2545. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2546. dst.total_nr_elems()) *
  2547. 1e-6;
  2548. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2549. };
  2550. bench_case(1, 32, 32, 200, 200, 3, 1);
  2551. bench_case(1, 32, 32, 200, 200, 3, 1);
  2552. bench_case(1, 32, 32, 128, 128, 3, 1);
  2553. bench_case(1, 32, 32, 128, 128, 3, 1);
  2554. bench_case(1, 32, 32, 100, 100, 3, 1);
  2555. bench_case(1, 32, 32, 100, 100, 3, 1);
  2556. bench_case(1, 32, 32, 80, 80, 3, 1);
  2557. bench_case(1, 32, 32, 80, 80, 3, 1);
  2558. bench_case(1, 64, 32, 7, 7, 3, 1);
  2559. bench_case(1, 64, 64, 7, 7, 3, 1);
  2560. bench_case(1, 64, 128, 7, 7, 3, 1);
  2561. bench_case(1, 64, 256, 7, 7, 3, 1);
  2562. bench_case(1, 64, 512, 7, 7, 3, 1);
  2563. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2564. bench_case(1, 64, 32, 14, 14, 3, 1);
  2565. bench_case(1, 64, 64, 14, 14, 3, 1);
  2566. bench_case(1, 64, 128, 14, 14, 3, 1);
  2567. bench_case(1, 64, 256, 14, 14, 3, 1);
  2568. bench_case(1, 64, 512, 14, 14, 3, 1);
  2569. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2570. bench_case(1, 128, 128, 14, 14, 3, 1);
  2571. bench_case(1, 128, 256, 14, 14, 3, 1);
  2572. bench_case(1, 512, 512, 14, 14, 3, 1);
  2573. bench_case(1, 256, 512, 14, 14, 3, 1);
  2574. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2575. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2576. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2577. dtype::Int32(), dtype::Int32()};
  2578. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2:192";
  2579. // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
  2580. // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
  2581. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2582. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2583. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2584. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2585. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2586. {1, {4}}, data_type);
  2587. shapes_and_computation.clear();
  2588. }
  2589. namespace {
  2590. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  2591. size_t pack_size) {
  2592. std::vector<conv_bias::TestArg> args;
  2593. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  2594. size_t p) {
  2595. if (ic % pack_size != 0 || oc % pack_size != 0)
  2596. return;
  2597. if (w + 2 * p < kernel || h + 2 * p < kernel)
  2598. return;
  2599. param::ConvBias param;
  2600. param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  2601. param.format = param::ConvBias::Format::NCHW88;
  2602. param.sparse = param::ConvBias::Sparse::DENSE;
  2603. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2604. param.stride_h = 1;
  2605. param.stride_w = 1;
  2606. param.pad_h = p;
  2607. param.pad_w = p;
  2608. args.push_back(conv_bias::TestArg{
  2609. param,
  2610. TensorShape{1, ic / 8, h, w, 8},
  2611. TensorShape{oc / 8, ic / 8, kernel, kernel, 8, 8},
  2612. {1, oc / 8, 1, 1, 8}});
  2613. };
  2614. for (size_t ic : {64, 128, 256}) {
  2615. for (size_t oc : {64, 128, 256}) {
  2616. pack(oc, ic, 56, 56, kernel, kernel / 2);
  2617. pack(oc, ic, 14, 14, kernel, kernel / 2);
  2618. pack(oc, ic, 28, 28, kernel, kernel / 2);
  2619. }
  2620. }
  2621. //! conv in vgg16
  2622. pack(512, 512, 15, 15, kernel, kernel / 2);
  2623. pack(512, 256, 15, 15, kernel, kernel / 2);
  2624. pack(256, 256, 29, 29, kernel, kernel / 2);
  2625. pack(256, 128, 29, 29, kernel, kernel / 2);
  2626. pack(128, 128, 57, 57, kernel, kernel / 2);
  2627. pack(128, 64, 57, 57, kernel, kernel / 2);
  2628. pack(64, 64, 56, 56, kernel, kernel / 2);
  2629. pack(128, 128, 28, 28, kernel, kernel / 2);
  2630. pack(512, 512, 14, 14, kernel, kernel / 2);
  2631. return args;
  2632. }
  2633. void benchmark_winograd(const char* algo_name, Handle* handle, size_t kernel,
  2634. size_t pack_size) {
  2635. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  2636. using namespace conv_bias;
  2637. constexpr size_t RUN = 10;
  2638. Benchmarker<ConvBias> benchmark(handle);
  2639. benchmark.set_display(false);
  2640. benchmark.set_times(RUN);
  2641. Benchmarker<ConvBias> benchmark_winograd(handle);
  2642. benchmark_winograd.set_display(false);
  2643. benchmark_winograd.set_times(RUN);
  2644. for (auto&& arg : args) {
  2645. TensorLayout dst_layout;
  2646. auto opr = handle->create_operator<ConvBias>();
  2647. opr->param() = arg.param;
  2648. opr->deduce_layout({arg.src, dtype::Float32()},
  2649. {arg.filter, dtype::Float32()},
  2650. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2651. //! dst.nr_elems * IC * FH * FW * 2
  2652. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2653. arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
  2654. (1024 * 1024 * 1024) * 1e3;
  2655. auto used = benchmark.set_param(arg.param).exec(
  2656. {arg.src, arg.filter, {}, {}, {}}) /
  2657. RUN;
  2658. benchmark_winograd.set_param(arg.param);
  2659. auto used_winograd =
  2660. algo_benchmark<ConvBias>(benchmark_winograd,
  2661. {arg.src, arg.filter, {}, {}, {}},
  2662. algo_name) /
  2663. RUN;
  2664. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  2665. "speedup: "
  2666. "%f\n",
  2667. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2668. used, computations / used, used_winograd,
  2669. computations / used_winograd, used / used_winograd);
  2670. }
  2671. }
  2672. } // namespace
  2673. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
  2674. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
  2675. }
  2676. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  2677. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
  2678. }
  2679. #endif
  2680. } // namespace test
  2681. } // namespace megdnn
  2682. // vim: syntax=cpp.doxygen

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