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

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