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

conv_bias.cpp 125 kB

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