You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conv_bias.cpp 122 kB

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

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