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

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

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