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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  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. #if MEGDNN_WITH_BENCHMARK
  1445. #if MEGDNN_X86_WITH_MKL_DNN
  1446. static void x86_benchmark_fp32_mkldnn(Handle* handle) {
  1447. constexpr size_t RUNS = 30;
  1448. param::ConvBias param;
  1449. Benchmarker<ConvBias> benchmarker_mkldnn(handle);
  1450. benchmarker_mkldnn.set_display(false).set_times(RUNS);
  1451. benchmarker_mkldnn.set_before_exec_callback(
  1452. AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));
  1453. Benchmarker<ConvBias> benchmarker_im2col(handle);
  1454. benchmarker_im2col.set_display(false).set_times(RUNS);
  1455. benchmarker_im2col.set_before_exec_callback(
  1456. AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
  1457. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1458. size_t FS, size_t SZ, size_t GROUP = 1) {
  1459. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  1460. bias({1, OC, 1, 1}), z({}), dst({N, OC, H / SZ, W / SZ});
  1461. param.pad_h = FS / 2;
  1462. param.pad_w = FS / 2;
  1463. param.stride_h = SZ;
  1464. param.stride_w = SZ;
  1465. param.format = param::ConvBias::Format::NCHW;
  1466. param.sparse = param::ConvBias::Sparse::DENSE;
  1467. if (GROUP > 1) {
  1468. param.sparse = param::ConvBias::Sparse::GROUP;
  1469. filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
  1470. }
  1471. auto im2col_used = benchmarker_im2col.set_param(param).exec(
  1472. {src, filter, bias, z, dst}) /
  1473. RUNS;
  1474. src = IC < 8 ? TensorShape{N, IC, H, W}
  1475. : TensorShape{N, IC / 8, H, W, 8};
  1476. filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
  1477. : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
  1478. if (GROUP > 1 && OC == GROUP && IC == GROUP) {
  1479. filter = {GROUP / 8, 1, 1, FS, FS, 8};
  1480. } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
  1481. filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
  1482. }
  1483. bias = {1, OC / 8, 1, 1, 8};
  1484. z = {};
  1485. dst = {N, OC / 8, H / SZ, W / SZ, 8};
  1486. param.format = param::ConvBias::Format::NCHW88;
  1487. auto mkldnn_used = benchmarker_mkldnn.set_param(param).exec(
  1488. {src, filter, bias, z, dst}) /
  1489. RUNS;
  1490. float computations =
  1491. (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  1492. std::cout << "run " << src.to_string() << " " << filter.to_string()
  1493. << " " << bias.to_string() << " " << dst.to_string()
  1494. << std::endl;
  1495. std::cout << "im2col: " << im2col_used << " ms, "
  1496. << (computations / im2col_used) << " Gops, ";
  1497. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  1498. << (computations / mkldnn_used) << " Gops, "
  1499. << "spped up: " << (im2col_used / mkldnn_used) << ", ";
  1500. std::cout << std::endl;
  1501. };
  1502. run(1, 64, 64, 56, 56, 3, 1);
  1503. run(1, 3, 64, 224, 224, 3, 1);
  1504. run(1, 3, 64, 224, 224, 7, 2);
  1505. run(1, 64, 64, 56, 56, 3, 1);
  1506. run(1, 128, 128, 28, 28, 3, 1);
  1507. run(1, 256, 256, 14, 14, 3, 1);
  1508. run(1, 512, 512, 7, 7, 3, 1);
  1509. run(1, 256, 64, 56, 56, 1, 1);
  1510. run(1, 512, 128, 28, 28, 1, 1);
  1511. run(1, 1024, 256, 14, 14, 1, 1);
  1512. run(1, 2048, 512, 7, 7, 1, 1);
  1513. run(1, 32, 32, 112, 112, 3, 1, 32);
  1514. run(1, 144, 144, 56, 56, 3, 1, 144);
  1515. run(1, 192, 192, 28, 28, 3, 1, 192);
  1516. run(1, 384, 384, 28, 28, 3, 1, 384);
  1517. run(1, 576, 576, 14, 14, 3, 1, 576);
  1518. run(1, 960, 960, 7, 7, 3, 1, 960);
  1519. run(1, 256, 128, 56, 56, 1, 2, 1);
  1520. run(1, 512, 256, 28, 28, 1, 2, 1);
  1521. run(1, 1024, 512, 14, 14, 1, 2, 1);
  1522. run(1, 96, 96, 112, 112, 3, 2, 96);
  1523. run(1, 144, 144, 56, 56, 3, 2, 144);
  1524. run(1, 384, 384, 28, 28, 3, 2, 384);
  1525. run(1, 576, 576, 14, 14, 3, 2, 576);
  1526. }
  1527. TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1528. x86_benchmark_fp32_mkldnn(handle());
  1529. }
  1530. TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1531. x86_benchmark_fp32_mkldnn(handle());
  1532. }
  1533. #endif
  1534. #endif
  1535. /************************* Winograd ****************************/
  1536. namespace {
  1537. std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
  1538. std::vector<conv_bias::TestArg> args;
  1539. param::ConvBias cur_param;
  1540. cur_param.format = param::ConvBias::Format::NCHW88;
  1541. using NLMode = param::ConvBias::NonlineMode;
  1542. // clang-format off
  1543. for (auto nlmode :
  1544. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  1545. for (size_t ic : {1, 2}) {
  1546. for (size_t oc : {1, 2}) {
  1547. for (size_t i : {9, 63}) {
  1548. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  1549. cur_param.nonlineMode = nlmode;
  1550. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1551. cur_param.pad_h = cur_param.pad_w = 1;
  1552. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1553. TensorShape{oc, ic, 3, 3, 8, 8},
  1554. TensorShape{1, oc, 1, 1, 8});
  1555. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1556. TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
  1557. //! bias
  1558. args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
  1559. TensorShape{oc, ic, 3, 3, 8, 8},
  1560. TensorShape{2, oc, i, i, 8});
  1561. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1562. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
  1563. TensorShape{2, oc, ic, 3, 3, 8, 8},
  1564. TensorShape{1, 2 * oc, 1, 1, 8});*/
  1565. }}}
  1566. // clang-format on
  1567. //! test for multi-thread OC parallel
  1568. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1569. cur_param.pad_h = cur_param.pad_w = 1;
  1570. args.emplace_back(cur_param, TensorShape{2, 1, 9, 9, 8},
  1571. TensorShape{128, 1, 3, 3, 8, 8},
  1572. TensorShape{1, 128, 1, 1, 8});
  1573. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1574. args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
  1575. TensorShape{2, 128, 1, 3, 3, 8, 8},
  1576. TensorShape{1, 2 * 128, 1, 1, 8});*/
  1577. }
  1578. return args;
  1579. }
  1580. } // namespace
  1581. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
  1582. using namespace conv_bias;
  1583. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1584. Checker<ConvBiasForward> checker(handle());
  1585. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1586. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1587. for (auto&& arg : args) {
  1588. checker.set_param(arg.param).execs(
  1589. {arg.src, arg.filter, arg.bias, {}, {}});
  1590. }
  1591. }
  1592. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63_WEIGHT_PREPROCESS) {
  1593. using namespace conv_bias;
  1594. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1595. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1596. handle());
  1597. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1598. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1599. for (auto&& arg : args) {
  1600. checker.set_param(arg.param).execs(
  1601. {arg.src, arg.filter, arg.bias, {}, {}});
  1602. }
  1603. }
  1604. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
  1605. using namespace conv_bias;
  1606. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1607. Checker<ConvBiasForward> checker(handle());
  1608. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1609. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1610. for (auto&& arg : args) {
  1611. checker.set_param(arg.param).execs(
  1612. {arg.src, arg.filter, arg.bias, {}, {}});
  1613. }
  1614. }
  1615. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23_WEIGHT_PREPROCESS) {
  1616. using namespace conv_bias;
  1617. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1618. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1619. handle());
  1620. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1621. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1622. for (auto&& arg : args) {
  1623. checker.set_param(arg.param).execs(
  1624. {arg.src, arg.filter, arg.bias, {}, {}});
  1625. }
  1626. }
  1627. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
  1628. using namespace conv_bias;
  1629. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1630. Checker<ConvBiasForward> checker(handle());
  1631. auto run = [&checker](const std::vector<TestArg>& args, DType A_dtype,
  1632. DType B_dtype, DType C_dtype, DType D_dtype,
  1633. const float eps) {
  1634. for (auto&& arg : args) {
  1635. checker.set_dtype(0, A_dtype)
  1636. .set_dtype(1, B_dtype)
  1637. .set_dtype(2, C_dtype)
  1638. .set_dtype(4, D_dtype)
  1639. .set_epsilon(eps)
  1640. .set_param(arg.param)
  1641. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  1642. }
  1643. };
  1644. run(args, dtype::Float32(), dtype::Float32(), dtype::Float32(),
  1645. dtype::Float32(), 1e-3f);
  1646. }
  1647. /*********************************** End winograd ************************/
  1648. #if MEGDNN_X86_WITH_MKL_DNN
  1649. static void x86_correctness_fp32_mkldnn_run(
  1650. Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
  1651. ConvBiasForward::BiasMode bias_mode,
  1652. param::ConvBias::NonlineMode noline_mode, size_t n, size_t stride,
  1653. size_t kernel, size_t oc, size_t ic, size_t h, size_t w, size_t group) {
  1654. auto oc_per_group = oc / group;
  1655. auto ic_per_group = ic / group;
  1656. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  1657. (ic_per_group % 8 == 0 || ic_per_group == 3) &&
  1658. ic_per_group > 0;
  1659. bool ok_depthwise = oc == ic && oc == group;
  1660. if (!(ok_group || ok_depthwise)) {
  1661. return;
  1662. }
  1663. size_t pad = kernel / 2;
  1664. size_t kernel_h = kernel;
  1665. size_t kernel_w = kernel;
  1666. param::ConvBias param;
  1667. param.format = param::ConvBias::Format::NCHW88;
  1668. param.stride_h = stride;
  1669. param.stride_w = stride;
  1670. param.pad_h = pad;
  1671. param.pad_w = pad;
  1672. param.nonlineMode = noline_mode;
  1673. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  1674. if (ic == 3) {
  1675. src_tensor_shape = TensorShape{n, ic, h, w};
  1676. }
  1677. auto weight_tensor_shape =
  1678. TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  1679. if (ic == 3) {
  1680. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  1681. }
  1682. auto bias_tensor_shape = TensorShape{};
  1683. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1684. bias_tensor_shape = {1, oc / 8, 1, 1, 8};
  1685. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1686. TensorLayout dst_layout;
  1687. auto ConvBiasOp = handle->create_operator<ConvBias>();
  1688. ConvBiasOp->param() = param;
  1689. ConvBiasOp->deduce_layout({src_tensor_shape, dtype::Float32()},
  1690. {weight_tensor_shape, dtype::Float32()}, {},
  1691. {}, dst_layout);
  1692. bias_tensor_shape = dst_layout;
  1693. }
  1694. if (group == 1) {
  1695. param.sparse = param::ConvBias::Sparse::DENSE;
  1696. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1697. param.sparse = param::ConvBias::Sparse::GROUP;
  1698. weight_tensor_shape =
  1699. TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  1700. } else if (group > 1 && oc / group % 8 == 0 && oc / group > 0 &&
  1701. ic / group % 8 == 0 && ic / group > 0) {
  1702. param.sparse = param::ConvBias::Sparse::GROUP;
  1703. weight_tensor_shape = TensorShape{
  1704. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8,
  1705. 8};
  1706. }
  1707. checker.set_dtype(0, dtype::Float32())
  1708. .set_dtype(1, dtype::Float32())
  1709. .set_dtype(2, dtype::Float32())
  1710. .set_dtype(4, dtype::Float32())
  1711. .set_rng(0, &rng)
  1712. .set_rng(1, &rng)
  1713. .set_rng(2, &rng)
  1714. .set_epsilon(1e-3)
  1715. .set_param(param)
  1716. .execs({src_tensor_shape,
  1717. weight_tensor_shape,
  1718. bias_tensor_shape,
  1719. {},
  1720. {}});
  1721. }
  1722. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  1723. Checker<ConvBias> checker(handle);
  1724. UniformIntRNG rng{-127, 127};
  1725. checker.set_before_exec_callback(
  1726. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1727. "MKLDNN_CONV_FP32"));
  1728. for (auto bias_mode :
  1729. {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
  1730. megdnn::BiasMode::BIAS})
  1731. for (auto noline_mode : {param::ConvBias::NonlineMode::IDENTITY,
  1732. param::ConvBias::NonlineMode::SIGMOID,
  1733. param::ConvBias::NonlineMode::H_SWISH})
  1734. for (size_t n : {1, 2})
  1735. for (size_t stride : {1, 2})
  1736. for (size_t kernel : {3, 5, 7})
  1737. for (size_t oc : {8, 16})
  1738. for (size_t ic : {3, 8, 16})
  1739. for (size_t h : {22, 33})
  1740. for (size_t w : {22, 33}) {
  1741. for (size_t group = 1;
  1742. group <= std::min(oc, ic);
  1743. ++group) {
  1744. x86_correctness_fp32_mkldnn_run(
  1745. checker, rng, handle,
  1746. bias_mode, noline_mode, n,
  1747. stride, kernel, oc, ic, h,
  1748. w, group);
  1749. }
  1750. }
  1751. }
  1752. TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1753. x86_correctness_fp32_mkldnn(handle());
  1754. }
  1755. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1756. x86_correctness_fp32_mkldnn(handle());
  1757. }
  1758. TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
  1759. using namespace conv_bias;
  1760. std::vector<TestArg> args;
  1761. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1762. size_t p, NonlineMode nonline_mode) {
  1763. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1764. return;
  1765. param::ConvBias param;
  1766. param.stride_h = 1;
  1767. param.stride_w = 1;
  1768. param.pad_h = p;
  1769. param.pad_w = p;
  1770. param.nonlineMode = nonline_mode;
  1771. //! no bias
  1772. args.emplace_back(param, TensorShape{1, ic, h, w},
  1773. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1774. };
  1775. for (size_t kernel : {2, 3, 5, 7})
  1776. for (size_t ic : {1, 2, 3, 4})
  1777. for (size_t oc : {1, 2, 4})
  1778. for (size_t p : {0, 2})
  1779. for (size_t size : {20, 21, 22, 23, 24})
  1780. for (NonlineMode nonline_mode :
  1781. {NonlineMode::IDENTITY}) {
  1782. run(oc, ic, size, size, kernel, p, nonline_mode);
  1783. }
  1784. Checker<ConvBias> checker(handle());
  1785. checker.set_before_exec_callback(
  1786. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1787. "MKLDNN_MATMUL_INT8"));
  1788. checker.set_epsilon(1);
  1789. UniformIntRNG rng{-50, 50};
  1790. checker.set_dtype(0, dtype::Int8())
  1791. .set_dtype(1, dtype::Int8())
  1792. .set_dtype(2, dtype::Int32())
  1793. .set_dtype(4, dtype::Int32())
  1794. .set_rng(0, &rng)
  1795. .set_rng(1, &rng)
  1796. .set_rng(2, &rng);
  1797. for (auto&& arg : args) {
  1798. checker.set_param(arg.param).exec(
  1799. {arg.src, arg.filter, arg.bias, {}, {}});
  1800. }
  1801. }
  1802. TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
  1803. using namespace conv_bias;
  1804. std::vector<TestArg> args;
  1805. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1806. size_t p, NonlineMode nonline_mode) {
  1807. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1808. return;
  1809. param::ConvBias param;
  1810. param.stride_h = 1;
  1811. param.stride_w = 1;
  1812. param.pad_h = p;
  1813. param.pad_w = p;
  1814. param.nonlineMode = nonline_mode;
  1815. //! no bias
  1816. args.emplace_back(param, TensorShape{1, ic, h, w},
  1817. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1818. };
  1819. for (size_t kernel : {2, 3, 5, 7})
  1820. for (size_t ic : {1, 2, 3, 4})
  1821. for (size_t oc : {1, 2, 4})
  1822. for (size_t p : {0, 2})
  1823. for (size_t size : {20, 22, 24})
  1824. for (NonlineMode nonline_mode :
  1825. {NonlineMode::IDENTITY}) {
  1826. run(oc, ic, size, size, kernel, p, nonline_mode);
  1827. }
  1828. Checker<ConvBias> checker(handle());
  1829. checker.set_before_exec_callback(
  1830. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1831. checker.set_epsilon(1);
  1832. UniformIntRNG rng{-50, 50};
  1833. checker.set_dtype(0, dtype::Int8())
  1834. .set_dtype(1, dtype::Int8())
  1835. .set_dtype(2, dtype::Int32())
  1836. .set_dtype(4, dtype::Int32())
  1837. .set_rng(0, &rng)
  1838. .set_rng(1, &rng)
  1839. .set_rng(2, &rng);
  1840. for (auto&& arg : args) {
  1841. checker.set_param(arg.param).exec(
  1842. {arg.src, arg.filter, arg.bias, {}, {}});
  1843. }
  1844. }
  1845. TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
  1846. using namespace conv_bias;
  1847. std::vector<TestArg> args;
  1848. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1849. size_t p, NonlineMode nonline_mode) {
  1850. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1851. return;
  1852. param::ConvBias param;
  1853. param.stride_h = 1;
  1854. param.stride_w = 1;
  1855. param.pad_h = p;
  1856. param.pad_w = p;
  1857. param.nonlineMode = nonline_mode;
  1858. //! no bias
  1859. args.emplace_back(param, TensorShape{1, ic, h, w},
  1860. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1861. };
  1862. for (size_t kernel : {2, 3, 5, 7})
  1863. for (size_t ic : {1, 2, 3, 4})
  1864. for (size_t oc : {1, 2, 4})
  1865. for (size_t p : {0, 2})
  1866. for (size_t size : {20, 22, 24})
  1867. for (NonlineMode nonline_mode :
  1868. {NonlineMode::IDENTITY}) {
  1869. run(oc, ic, size, size, kernel, p, nonline_mode);
  1870. }
  1871. Checker<ConvBias> checker(handle());
  1872. checker.set_before_exec_callback(
  1873. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1874. checker.set_epsilon(1);
  1875. UniformIntRNG rng{-50, 50};
  1876. checker.set_dtype(0, dtype::Int8())
  1877. .set_dtype(1, dtype::Int8())
  1878. .set_dtype(2, dtype::Int32())
  1879. .set_dtype(4, dtype::Int32())
  1880. .set_rng(0, &rng)
  1881. .set_rng(1, &rng)
  1882. .set_rng(2, &rng);
  1883. for (auto&& arg : args) {
  1884. checker.set_param(arg.param).exec(
  1885. {arg.src, arg.filter, arg.bias, {}, {}});
  1886. }
  1887. }
  1888. #endif
  1889. #if MEGDNN_WITH_BENCHMARK
  1890. namespace {
  1891. void benchmark_impl(const param::ConvBias param,
  1892. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1893. shapes_and_computation,
  1894. const std::string algo_name, size_t RUNS,
  1895. TaskExecutorConfig&& multi_thread_config,
  1896. TaskExecutorConfig&& single_thread_config,
  1897. std::vector<DType> dtype_v) {
  1898. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1899. dtype::Float32(), dtype::Float32()};
  1900. std::vector<float> multi_thread_times, single_thread_times;
  1901. {
  1902. auto multi_thread_hanle =
  1903. create_cpu_handle(0, true, &multi_thread_config);
  1904. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1905. benchmarker.set_times(RUNS)
  1906. .set_display(false)
  1907. .set_dtype(0, dtype_v[0])
  1908. .set_dtype(1, dtype_v[1])
  1909. .set_dtype(2, dtype_v[2])
  1910. .set_dtype(4, dtype_v[3])
  1911. .set_param(param)
  1912. .set_before_exec_callback(
  1913. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1914. algo_name.c_str()));
  1915. for (auto shape : shapes_and_computation) {
  1916. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1917. }
  1918. }
  1919. {
  1920. auto single_thread_handle =
  1921. create_cpu_handle(0, true, &single_thread_config);
  1922. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1923. benchmarker.set_times(RUNS)
  1924. .set_display(false)
  1925. .set_dtype(0, dtype_v[0])
  1926. .set_dtype(1, dtype_v[1])
  1927. .set_dtype(2, dtype_v[2])
  1928. .set_dtype(4, dtype_v[3])
  1929. .set_param(param)
  1930. .set_before_exec_callback(
  1931. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1932. algo_name.c_str()));
  1933. for (auto shape : shapes_and_computation) {
  1934. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1935. }
  1936. }
  1937. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  1938. printf("core_ids:");
  1939. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  1940. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  1941. }
  1942. printf(", Single thread core_id %zu\n",
  1943. single_thread_config.affinity_core_set[0]);
  1944. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  1945. auto shapes = shapes_and_computation[i];
  1946. printf("Bench case: ");
  1947. for (auto&& shape : shapes.first) {
  1948. printf("%s ", shape.to_string().c_str());
  1949. }
  1950. float computations = shapes.second;
  1951. printf("%zu threads gflops: %f,\n single thread gflops: "
  1952. "%f. spead up = %f, speedup/cores=%f\n",
  1953. multi_thread_config.nr_thread,
  1954. computations / multi_thread_times[i],
  1955. computations / single_thread_times[i],
  1956. single_thread_times[i] / multi_thread_times[i],
  1957. single_thread_times[i] / multi_thread_times[i] /
  1958. multi_thread_config.nr_thread);
  1959. }
  1960. }
  1961. void benchmark_impl_comp(
  1962. const param::ConvBias param,
  1963. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1964. shapes_and_computation,
  1965. const std::string algo_name, const std::string algo_name1, size_t RUNS,
  1966. TaskExecutorConfig&& multi_thread_config,
  1967. TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
  1968. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1969. dtype::Float32(), dtype::Float32()};
  1970. std::vector<float> multi_thread_times, single_thread_times;
  1971. {
  1972. auto multi_thread_hanle =
  1973. create_cpu_handle(0, true, &multi_thread_config);
  1974. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1975. benchmarker.set_times(RUNS)
  1976. .set_display(false)
  1977. .set_dtype(0, dtype_v[0])
  1978. .set_dtype(1, dtype_v[1])
  1979. .set_dtype(2, dtype_v[2])
  1980. .set_dtype(4, dtype_v[3])
  1981. .set_param(param)
  1982. .set_before_exec_callback(
  1983. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1984. algo_name.c_str()));
  1985. for (auto shape : shapes_and_computation) {
  1986. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1987. }
  1988. }
  1989. {
  1990. auto single_thread_handle =
  1991. create_cpu_handle(0, true, &single_thread_config);
  1992. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1993. benchmarker.set_times(RUNS)
  1994. .set_display(false)
  1995. .set_dtype(0, dtype_v[0])
  1996. .set_dtype(1, dtype_v[1])
  1997. .set_dtype(2, dtype_v[2])
  1998. .set_dtype(4, dtype_v[3])
  1999. .set_param(param)
  2000. .set_before_exec_callback(
  2001. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2002. algo_name1.c_str()));
  2003. for (auto shape : shapes_and_computation) {
  2004. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2005. }
  2006. }
  2007. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  2008. printf("core_ids:");
  2009. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  2010. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  2011. }
  2012. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  2013. auto shapes = shapes_and_computation[i];
  2014. printf("Bench case: ");
  2015. for (auto&& shape : shapes.first) {
  2016. printf("%s ", shape.to_string().c_str());
  2017. }
  2018. float computations = shapes.second;
  2019. printf("algo:%s gflops: %f,\n algo:%s gflops: "
  2020. "%f. spead up = %f\n",
  2021. algo_name.c_str(), computations / multi_thread_times[i],
  2022. algo_name1.c_str(), computations / single_thread_times[i],
  2023. single_thread_times[i] / multi_thread_times[i]);
  2024. }
  2025. }
  2026. } // namespace
  2027. static void benchmark_convbias_chanwise_avx2_int8(uint32_t stride,
  2028. const char* algo) {
  2029. constexpr size_t RUNS = 50;
  2030. param::ConvBias param;
  2031. param.stride_h = stride;
  2032. param.stride_w = stride;
  2033. param.sparse = param::ConvBias::Sparse::GROUP;
  2034. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2035. dtype::Int32(), dtype::Int32()};
  2036. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2037. shapes_and_computation;
  2038. auto bench_case = [&](size_t N, size_t IC, size_t H, size_t W, size_t FS) {
  2039. param.pad_h = FS / 2;
  2040. param.pad_w = FS / 2;
  2041. SmallVector<TensorShape> shapes{
  2042. {N, IC, H, W}, {IC, 1, 1, FS, FS}, {}, {}, {}};
  2043. TensorShape dst{N, IC, (H + 2 * param.pad_h - FS) + 1,
  2044. (W + 2 * param.pad_w - FS) + 1};
  2045. float computations = (FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2046. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2047. };
  2048. bench_case(1, 32, 112, 112, 7);
  2049. bench_case(1, 144, 56, 56, 7);
  2050. bench_case(1, 192, 28, 28, 7);
  2051. bench_case(1, 384, 28, 28, 7);
  2052. bench_case(1, 576, 14, 14, 7);
  2053. bench_case(1, 960, 7, 7, 7);
  2054. bench_case(1, 32, 112, 112, 5);
  2055. bench_case(1, 144, 56, 56, 5);
  2056. bench_case(1, 192, 28, 28, 5);
  2057. bench_case(1, 384, 28, 28, 5);
  2058. bench_case(1, 576, 14, 14, 5);
  2059. bench_case(1, 960, 7, 7, 5);
  2060. bench_case(1, 32, 112, 112, 3);
  2061. bench_case(1, 144, 56, 56, 3);
  2062. bench_case(1, 192, 28, 28, 3);
  2063. bench_case(1, 384, 28, 28, 3);
  2064. bench_case(1, 576, 14, 14, 3);
  2065. bench_case(1, 960, 7, 7, 3);
  2066. bench_case(1, 32, 112, 112, 2);
  2067. bench_case(1, 144, 56, 56, 2);
  2068. bench_case(1, 192, 28, 28, 2);
  2069. bench_case(1, 384, 28, 28, 2);
  2070. bench_case(1, 576, 14, 14, 2);
  2071. bench_case(1, 960, 7, 7, 2);
  2072. std::string algo_name = algo;
  2073. printf("Benchmark %s\n", algo);
  2074. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2075. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2076. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2077. {1, {4}}, data_type);
  2078. shapes_and_computation.clear();
  2079. }
  2080. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S1) {
  2081. benchmark_convbias_chanwise_avx2_int8(
  2082. 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
  2083. }
  2084. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S2) {
  2085. benchmark_convbias_chanwise_avx2_int8(
  2086. 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
  2087. }
  2088. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
  2089. constexpr size_t RUNS = 50;
  2090. param::ConvBias param;
  2091. param.stride_h = 1;
  2092. param.stride_w = 1;
  2093. param.sparse = param::ConvBias::Sparse::DENSE;
  2094. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2095. dtype::Int32(), dtype::Int32()};
  2096. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2097. shapes_and_computation;
  2098. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2099. size_t FS) {
  2100. param.pad_h = FS / 2;
  2101. param.pad_w = FS / 2;
  2102. SmallVector<TensorShape> shapes{
  2103. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2104. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) + 1,
  2105. (W + 2 * param.pad_w - FS) + 1};
  2106. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2107. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2108. };
  2109. bench_case(1, 32, 32, 200, 200, 7);
  2110. bench_case(1, 32, 64, 200, 200, 7);
  2111. bench_case(1, 32, 32, 128, 128, 7);
  2112. bench_case(1, 32, 64, 128, 128, 7);
  2113. bench_case(1, 32, 32, 100, 100, 7);
  2114. bench_case(1, 32, 64, 100, 100, 7);
  2115. bench_case(1, 32, 32, 80, 80, 7);
  2116. bench_case(1, 32, 64, 80, 80, 7);
  2117. bench_case(1, 32, 32, 200, 200, 5);
  2118. bench_case(1, 32, 64, 200, 200, 5);
  2119. bench_case(1, 32, 32, 128, 128, 5);
  2120. bench_case(1, 32, 64, 128, 128, 5);
  2121. bench_case(1, 32, 32, 100, 100, 5);
  2122. bench_case(1, 32, 64, 100, 100, 5);
  2123. bench_case(1, 32, 32, 80, 80, 5);
  2124. bench_case(1, 32, 64, 80, 80, 5);
  2125. bench_case(1, 32, 32, 200, 200, 3);
  2126. bench_case(1, 32, 64, 200, 200, 3);
  2127. bench_case(1, 32, 32, 128, 128, 3);
  2128. bench_case(1, 32, 64, 128, 128, 3);
  2129. bench_case(1, 32, 32, 100, 100, 3);
  2130. bench_case(1, 32, 64, 100, 100, 3);
  2131. bench_case(1, 32, 32, 80, 80, 3);
  2132. bench_case(1, 32, 64, 80, 80, 3);
  2133. bench_case(1, 32, 32, 200, 200, 2);
  2134. bench_case(1, 32, 64, 200, 200, 2);
  2135. bench_case(1, 32, 32, 128, 128, 2);
  2136. bench_case(1, 32, 64, 128, 128, 2);
  2137. bench_case(1, 32, 32, 100, 100, 2);
  2138. bench_case(1, 32, 64, 100, 100, 2);
  2139. bench_case(1, 32, 32, 80, 80, 2);
  2140. bench_case(1, 32, 64, 80, 80, 2);
  2141. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
  2142. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
  2143. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2144. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2145. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2146. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2147. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2148. {1, {4}}, data_type);
  2149. shapes_and_computation.clear();
  2150. }
  2151. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_8816) {
  2152. constexpr size_t RUNS = 30;
  2153. param::ConvBias param;
  2154. param.stride_h = 1;
  2155. param.stride_w = 1;
  2156. param.sparse = param::ConvBias::Sparse::DENSE;
  2157. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2158. dtype::Int16(), dtype::Int16()};
  2159. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2160. shapes_and_computation;
  2161. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2162. size_t FS) {
  2163. param.pad_h = FS / 2;
  2164. param.pad_w = FS / 2;
  2165. SmallVector<TensorShape> shapes{
  2166. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2167. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2168. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2169. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2170. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2171. };
  2172. bench_case(1, 48, 192, 15, 15, 1);
  2173. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X16_AVX2";
  2174. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2175. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2176. shapes_and_computation.clear();
  2177. }
  2178. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  2179. BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
  2180. constexpr size_t RUNS = 50;
  2181. param::ConvBias param;
  2182. param.stride_h = 2;
  2183. param.stride_w = 2;
  2184. param.sparse = param::ConvBias::Sparse::DENSE;
  2185. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2186. dtype::Int32(), dtype::Int32()};
  2187. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2188. shapes_and_computation;
  2189. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2190. size_t FS) {
  2191. param.pad_h = FS / 2;
  2192. param.pad_w = FS / 2;
  2193. SmallVector<TensorShape> shapes{
  2194. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2195. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2196. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2197. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2198. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2199. };
  2200. bench_case(1, 32, 32, 200, 200, 7);
  2201. bench_case(1, 32, 64, 200, 200, 7);
  2202. bench_case(1, 32, 32, 128, 128, 7);
  2203. bench_case(1, 32, 64, 128, 128, 7);
  2204. bench_case(1, 32, 32, 100, 100, 7);
  2205. bench_case(1, 32, 64, 100, 100, 7);
  2206. bench_case(1, 32, 32, 80, 80, 7);
  2207. bench_case(1, 32, 64, 80, 80, 7);
  2208. bench_case(1, 32, 32, 200, 200, 5);
  2209. bench_case(1, 32, 64, 200, 200, 5);
  2210. bench_case(1, 32, 32, 128, 128, 5);
  2211. bench_case(1, 32, 64, 128, 128, 5);
  2212. bench_case(1, 32, 32, 100, 100, 5);
  2213. bench_case(1, 32, 64, 100, 100, 5);
  2214. bench_case(1, 32, 32, 80, 80, 5);
  2215. bench_case(1, 32, 64, 80, 80, 5);
  2216. bench_case(1, 32, 32, 200, 200, 3);
  2217. bench_case(1, 32, 64, 200, 200, 3);
  2218. bench_case(1, 32, 32, 128, 128, 3);
  2219. bench_case(1, 32, 64, 128, 128, 3);
  2220. bench_case(1, 32, 32, 100, 100, 3);
  2221. bench_case(1, 32, 64, 100, 100, 3);
  2222. bench_case(1, 32, 32, 80, 80, 3);
  2223. bench_case(1, 32, 64, 80, 80, 3);
  2224. bench_case(1, 32, 32, 200, 200, 2);
  2225. bench_case(1, 32, 64, 200, 200, 2);
  2226. bench_case(1, 32, 32, 128, 128, 2);
  2227. bench_case(1, 32, 64, 128, 128, 2);
  2228. bench_case(1, 32, 32, 100, 100, 2);
  2229. bench_case(1, 32, 64, 100, 100, 2);
  2230. bench_case(1, 32, 32, 80, 80, 2);
  2231. bench_case(1, 32, 64, 80, 80, 2);
  2232. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
  2233. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
  2234. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2235. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2236. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2237. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2238. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2239. {1, {4}}, data_type);
  2240. shapes_and_computation.clear();
  2241. }
  2242. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
  2243. constexpr size_t RUNS = 50;
  2244. param::ConvBias param;
  2245. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2246. param.pad_h = 1;
  2247. param.pad_w = 1;
  2248. param.stride_h = 1;
  2249. param.stride_w = 1;
  2250. param.sparse = param::ConvBias::Sparse::GROUP;
  2251. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2252. dtype::Float32(), dtype::Float32()};
  2253. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2254. shapes_and_computation;
  2255. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2256. size_t FS, size_t group) {
  2257. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2258. {group, OC / group, IC / group, FS, FS},
  2259. {1, OC, 1, 1},
  2260. {},
  2261. {N, OC, H, W}};
  2262. TensorShape dst{N, OC, H, W};
  2263. float computations =
  2264. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2265. dst.total_nr_elems()) *
  2266. 1e-6;
  2267. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2268. };
  2269. bench_case(1, 32, 32, 200, 200, 3, 4);
  2270. bench_case(1, 32, 32, 200, 200, 3, 32);
  2271. bench_case(1, 32, 32, 128, 128, 3, 4);
  2272. bench_case(1, 32, 32, 128, 128, 3, 32);
  2273. bench_case(1, 32, 32, 100, 100, 3, 4);
  2274. bench_case(1, 32, 32, 100, 100, 3, 32);
  2275. bench_case(1, 32, 32, 80, 80, 3, 4);
  2276. bench_case(1, 32, 32, 80, 80, 3, 32);
  2277. std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2278. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_GROUP algo\n");
  2279. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2280. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2281. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2282. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2283. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2284. {1, {4}}, data_type);
  2285. shapes_and_computation.clear();
  2286. algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2287. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_DENSE algo\n");
  2288. bench_case(1, 32, 32, 200, 200, 3, 1);
  2289. bench_case(1, 32, 32, 128, 128, 3, 1);
  2290. bench_case(1, 32, 32, 100, 100, 3, 1);
  2291. bench_case(1, 32, 32, 80, 80, 3, 1);
  2292. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2293. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2294. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2295. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2296. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2297. {1, {4}}, data_type);
  2298. }
  2299. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
  2300. constexpr size_t RUNS = 50;
  2301. param::ConvBias param;
  2302. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2303. param.pad_h = 1;
  2304. param.pad_w = 1;
  2305. param.stride_h = 1;
  2306. param.stride_w = 1;
  2307. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2308. dtype::Float32(), dtype::Float32()};
  2309. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2310. shapes_and_computation;
  2311. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2312. size_t FS, size_t group) {
  2313. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2314. {OC / group, IC / group, FS, FS},
  2315. {1, OC, 1, 1},
  2316. {},
  2317. {N, OC, H, W}};
  2318. TensorShape dst{N, OC, H, W};
  2319. float computations =
  2320. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2321. dst.total_nr_elems()) *
  2322. 1e-6;
  2323. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2324. };
  2325. bench_case(1, 32, 32, 200, 200, 3, 1);
  2326. bench_case(1, 32, 32, 200, 200, 3, 1);
  2327. bench_case(1, 32, 32, 128, 128, 3, 1);
  2328. bench_case(1, 32, 32, 128, 128, 3, 1);
  2329. bench_case(1, 32, 32, 100, 100, 3, 1);
  2330. bench_case(1, 32, 32, 100, 100, 3, 1);
  2331. bench_case(1, 32, 32, 80, 80, 3, 1);
  2332. bench_case(1, 32, 32, 80, 80, 3, 1);
  2333. bench_case(1, 64, 32, 7, 7, 3, 1);
  2334. bench_case(1, 64, 64, 7, 7, 3, 1);
  2335. bench_case(1, 64, 128, 7, 7, 3, 1);
  2336. bench_case(1, 64, 256, 7, 7, 3, 1);
  2337. bench_case(1, 64, 512, 7, 7, 3, 1);
  2338. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2339. bench_case(1, 64, 32, 14, 14, 3, 1);
  2340. bench_case(1, 64, 64, 14, 14, 3, 1);
  2341. bench_case(1, 64, 128, 14, 14, 3, 1);
  2342. bench_case(1, 64, 256, 14, 14, 3, 1);
  2343. bench_case(1, 64, 512, 14, 14, 3, 1);
  2344. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2345. bench_case(1, 128, 128, 14, 14, 3, 1);
  2346. bench_case(1, 128, 256, 14, 14, 3, 1);
  2347. bench_case(1, 512, 512, 14, 14, 3, 1);
  2348. bench_case(1, 256, 512, 14, 14, 3, 1);
  2349. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2350. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2351. std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
  2352. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2353. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2354. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2355. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2356. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2357. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2358. {1, {4}}, data_type);
  2359. shapes_and_computation.clear();
  2360. }
  2361. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  2362. BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
  2363. constexpr size_t RUNS = 50;
  2364. param::ConvBias param;
  2365. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2366. param.pad_h = 1;
  2367. param.pad_w = 1;
  2368. param.stride_h = 1;
  2369. param.stride_w = 1;
  2370. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2371. dtype::Float32(), dtype::Float32()};
  2372. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2373. shapes_and_computation;
  2374. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2375. size_t FS, size_t group) {
  2376. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2377. {OC / group, IC / group, FS, FS},
  2378. {1, OC, 1, 1},
  2379. {},
  2380. {N, OC, H, W}};
  2381. TensorShape dst{N, OC, H, W};
  2382. float computations =
  2383. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2384. dst.total_nr_elems()) *
  2385. 1e-6;
  2386. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2387. };
  2388. bench_case(1, 32, 32, 200, 200, 3, 1);
  2389. bench_case(1, 32, 32, 200, 200, 3, 1);
  2390. bench_case(1, 32, 32, 128, 128, 3, 1);
  2391. bench_case(1, 32, 32, 128, 128, 3, 1);
  2392. bench_case(1, 32, 32, 100, 100, 3, 1);
  2393. bench_case(1, 32, 32, 100, 100, 3, 1);
  2394. bench_case(1, 32, 32, 80, 80, 3, 1);
  2395. bench_case(1, 32, 32, 80, 80, 3, 1);
  2396. bench_case(1, 64, 32, 7, 7, 3, 1);
  2397. bench_case(1, 64, 64, 7, 7, 3, 1);
  2398. bench_case(1, 64, 128, 7, 7, 3, 1);
  2399. bench_case(1, 64, 256, 7, 7, 3, 1);
  2400. bench_case(1, 64, 512, 7, 7, 3, 1);
  2401. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2402. bench_case(1, 64, 32, 14, 14, 3, 1);
  2403. bench_case(1, 64, 64, 14, 14, 3, 1);
  2404. bench_case(1, 64, 128, 14, 14, 3, 1);
  2405. bench_case(1, 64, 256, 14, 14, 3, 1);
  2406. bench_case(1, 64, 512, 14, 14, 3, 1);
  2407. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2408. bench_case(1, 128, 128, 14, 14, 3, 1);
  2409. bench_case(1, 128, 256, 14, 14, 3, 1);
  2410. bench_case(1, 512, 512, 14, 14, 3, 1);
  2411. bench_case(1, 256, 512, 14, 14, 3, 1);
  2412. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2413. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2414. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  2415. std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
  2416. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2417. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2418. RUNS, {1, {4}}, {1, {4}}, data_type);
  2419. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2420. RUNS, {1, {7}}, {1, {7}}, data_type);
  2421. shapes_and_computation.clear();
  2422. }
  2423. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
  2424. constexpr size_t RUNS = 50;
  2425. param::ConvBias param;
  2426. param.pad_h = 1;
  2427. param.pad_w = 1;
  2428. param.stride_h = 1;
  2429. param.stride_w = 1;
  2430. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2431. shapes_and_computation;
  2432. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2433. size_t FS, size_t group) {
  2434. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2435. {OC / group, IC / group, FS, FS},
  2436. {1, OC, 1, 1},
  2437. {},
  2438. {N, OC, H, W}};
  2439. TensorShape dst{N, OC, H, W};
  2440. float computations =
  2441. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2442. dst.total_nr_elems()) *
  2443. 1e-6;
  2444. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2445. };
  2446. bench_case(1, 32, 32, 200, 200, 3, 1);
  2447. bench_case(1, 32, 32, 200, 200, 3, 1);
  2448. bench_case(1, 32, 32, 128, 128, 3, 1);
  2449. bench_case(1, 32, 32, 128, 128, 3, 1);
  2450. bench_case(1, 32, 32, 100, 100, 3, 1);
  2451. bench_case(1, 32, 32, 100, 100, 3, 1);
  2452. bench_case(1, 32, 32, 80, 80, 3, 1);
  2453. bench_case(1, 32, 32, 80, 80, 3, 1);
  2454. bench_case(1, 64, 32, 7, 7, 3, 1);
  2455. bench_case(1, 64, 64, 7, 7, 3, 1);
  2456. bench_case(1, 64, 128, 7, 7, 3, 1);
  2457. bench_case(1, 64, 256, 7, 7, 3, 1);
  2458. bench_case(1, 64, 512, 7, 7, 3, 1);
  2459. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2460. bench_case(1, 64, 32, 14, 14, 3, 1);
  2461. bench_case(1, 64, 64, 14, 14, 3, 1);
  2462. bench_case(1, 64, 128, 14, 14, 3, 1);
  2463. bench_case(1, 64, 256, 14, 14, 3, 1);
  2464. bench_case(1, 64, 512, 14, 14, 3, 1);
  2465. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2466. bench_case(1, 128, 128, 14, 14, 3, 1);
  2467. bench_case(1, 128, 256, 14, 14, 3, 1);
  2468. bench_case(1, 512, 512, 14, 14, 3, 1);
  2469. bench_case(1, 256, 512, 14, 14, 3, 1);
  2470. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2471. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2472. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2473. dtype::Int32(), dtype::Int32()};
  2474. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2:192";
  2475. // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
  2476. // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
  2477. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2478. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2479. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2480. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2481. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2482. {1, {4}}, data_type);
  2483. shapes_and_computation.clear();
  2484. }
  2485. namespace {
  2486. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  2487. size_t pack_size) {
  2488. std::vector<conv_bias::TestArg> args;
  2489. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  2490. size_t p) {
  2491. if (ic % pack_size != 0 || oc % pack_size != 0)
  2492. return;
  2493. if (w + 2 * p < kernel || h + 2 * p < kernel)
  2494. return;
  2495. param::ConvBias param;
  2496. param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  2497. param.format = param::ConvBias::Format::NCHW88;
  2498. param.sparse = param::ConvBias::Sparse::DENSE;
  2499. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2500. param.stride_h = 1;
  2501. param.stride_w = 1;
  2502. param.pad_h = p;
  2503. param.pad_w = p;
  2504. args.push_back(conv_bias::TestArg{
  2505. param,
  2506. TensorShape{1, ic / 8, h, w, 8},
  2507. TensorShape{oc / 8, ic / 8, kernel, kernel, 8, 8},
  2508. {1, oc / 8, 1, 1, 8}});
  2509. };
  2510. for (size_t ic : {64, 128, 256}) {
  2511. for (size_t oc : {64, 128, 256}) {
  2512. pack(oc, ic, 56, 56, kernel, kernel / 2);
  2513. pack(oc, ic, 14, 14, kernel, kernel / 2);
  2514. pack(oc, ic, 28, 28, kernel, kernel / 2);
  2515. }
  2516. }
  2517. //! conv in vgg16
  2518. pack(512, 512, 15, 15, kernel, kernel / 2);
  2519. pack(512, 256, 15, 15, kernel, kernel / 2);
  2520. pack(256, 256, 29, 29, kernel, kernel / 2);
  2521. pack(256, 128, 29, 29, kernel, kernel / 2);
  2522. pack(128, 128, 57, 57, kernel, kernel / 2);
  2523. pack(128, 64, 57, 57, kernel, kernel / 2);
  2524. pack(64, 64, 56, 56, kernel, kernel / 2);
  2525. pack(128, 128, 28, 28, kernel, kernel / 2);
  2526. pack(512, 512, 14, 14, kernel, kernel / 2);
  2527. return args;
  2528. }
  2529. void benchmark_winograd(const char* algo_name, Handle* handle, size_t kernel,
  2530. size_t pack_size) {
  2531. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  2532. using namespace conv_bias;
  2533. constexpr size_t RUN = 10;
  2534. Benchmarker<ConvBias> benchmark(handle);
  2535. benchmark.set_display(false);
  2536. benchmark.set_times(RUN);
  2537. Benchmarker<ConvBias> benchmark_winograd(handle);
  2538. benchmark_winograd.set_display(false);
  2539. benchmark_winograd.set_times(RUN);
  2540. for (auto&& arg : args) {
  2541. TensorLayout dst_layout;
  2542. auto opr = handle->create_operator<ConvBias>();
  2543. opr->param() = arg.param;
  2544. opr->deduce_layout({arg.src, dtype::Float32()},
  2545. {arg.filter, dtype::Float32()},
  2546. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2547. //! dst.nr_elems * IC * FH * FW * 2
  2548. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2549. arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
  2550. (1024 * 1024 * 1024) * 1e3;
  2551. auto used = benchmark.set_param(arg.param).exec(
  2552. {arg.src, arg.filter, {}, {}, {}}) /
  2553. RUN;
  2554. benchmark_winograd.set_param(arg.param);
  2555. auto used_winograd =
  2556. algo_benchmark<ConvBias>(benchmark_winograd,
  2557. {arg.src, arg.filter, {}, {}, {}},
  2558. algo_name) /
  2559. RUN;
  2560. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  2561. "speedup: "
  2562. "%f\n",
  2563. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2564. used, computations / used, used_winograd,
  2565. computations / used_winograd, used / used_winograd);
  2566. }
  2567. }
  2568. } // namespace
  2569. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
  2570. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
  2571. }
  2572. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  2573. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
  2574. }
  2575. #endif
  2576. } // namespace test
  2577. } // namespace megdnn
  2578. // vim: syntax=cpp.doxygen

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