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

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

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