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

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

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