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

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

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