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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  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. run(oc, ic, size, size, kernel, p, nonline_mode);
  988. }
  989. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  990. Checker<ConvBias> checker(handle());
  991. #define cb(algo_name) \
  992. checker.set_before_exec_callback( \
  993. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  994. for (auto&& arg : args) { \
  995. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  996. }
  997. #if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
  998. cb("IM2COLMATMUL:X86_F32_BLAS");
  999. #endif
  1000. #undef cb
  1001. }
  1002. #if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
  1003. TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32) {
  1004. using namespace conv_bias;
  1005. std::vector<TestArg> args;
  1006. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1007. NonlineMode nonline_mode) {
  1008. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1009. return;
  1010. param::ConvBias param;
  1011. param.stride_h = 1;
  1012. param.stride_w = 1;
  1013. param.pad_h = p;
  1014. param.pad_w = p;
  1015. param.nonlineMode = nonline_mode;
  1016. //! no bias
  1017. args.emplace_back(
  1018. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1019. TensorShape{});
  1020. args.emplace_back(
  1021. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1022. TensorShape{1, oc, 1, 1});
  1023. args.emplace_back(
  1024. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1025. TensorShape{
  1026. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1027. (w + 2 * p - kernel) / param.stride_w + 1});
  1028. };
  1029. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1030. for (size_t ic : {1, 4, 8, 16})
  1031. for (size_t oc : {1, 4, 8, 16, 300})
  1032. for (size_t p : {0, 2})
  1033. for (size_t size : {8, 24})
  1034. for (NonlineMode nonline_mode :
  1035. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1036. run(oc, ic, size, size, kernel, p, nonline_mode);
  1037. }
  1038. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1039. Checker<ConvBias> checker(handle());
  1040. #define cb(algo_name) \
  1041. checker.set_before_exec_callback( \
  1042. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1043. for (auto&& arg : args) { \
  1044. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  1045. }
  1046. cb("IM2COLMATMUL:X86_F32_BLAS");
  1047. #undef cb
  1048. }
  1049. TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32_RECORD) {
  1050. using namespace conv_bias;
  1051. std::vector<TestArg> args;
  1052. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1053. NonlineMode nonline_mode) {
  1054. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1055. return;
  1056. param::ConvBias param;
  1057. param.stride_h = 1;
  1058. param.stride_w = 1;
  1059. param.pad_h = p;
  1060. param.pad_w = p;
  1061. param.nonlineMode = nonline_mode;
  1062. //! no bias
  1063. args.emplace_back(
  1064. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1065. TensorShape{});
  1066. args.emplace_back(
  1067. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1068. TensorShape{1, oc, 1, 1});
  1069. args.emplace_back(
  1070. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1071. TensorShape{
  1072. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1073. (w + 2 * p - kernel) / param.stride_w + 1});
  1074. };
  1075. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1076. run(1, 1, 24, 24, 2, 2, nonline_mode);
  1077. }
  1078. TaskRecordChecker<ConvBias> checker(0);
  1079. for (auto&& arg : args) {
  1080. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1081. }
  1082. }
  1083. TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32_NOPACK_PREPROCESS) {
  1084. using namespace conv_bias;
  1085. std::vector<TestArg> args;
  1086. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1087. NonlineMode nonline_mode) {
  1088. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1089. return;
  1090. param::ConvBias param;
  1091. param.stride_h = 1;
  1092. param.stride_w = 1;
  1093. param.pad_h = p;
  1094. param.pad_w = p;
  1095. param.nonlineMode = nonline_mode;
  1096. //! no bias
  1097. args.emplace_back(
  1098. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1099. TensorShape{});
  1100. args.emplace_back(
  1101. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1102. TensorShape{1, oc, 1, 1});
  1103. args.emplace_back(
  1104. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1105. TensorShape{
  1106. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1107. (w + 2 * p - kernel) / param.stride_w + 1});
  1108. };
  1109. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1110. for (size_t ic : {1, 4, 8, 16})
  1111. for (size_t oc : {1, 4, 8, 16, 300})
  1112. for (size_t p : {0, 2})
  1113. for (size_t size : {8, 24})
  1114. for (NonlineMode nonline_mode :
  1115. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1116. run(oc, ic, size, size, kernel, p, nonline_mode);
  1117. }
  1118. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1119. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1120. handle());
  1121. #define cb(algo_name) \
  1122. checker.set_before_exec_callback( \
  1123. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1124. for (auto&& arg : args) { \
  1125. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  1126. }
  1127. cb("IM2COLMATMUL:X86_F32_BLAS");
  1128. #undef cb
  1129. }
  1130. #endif
  1131. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_6x16) {
  1132. using namespace conv_bias;
  1133. std::vector<TestArg> args;
  1134. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1135. NonlineMode nonline_mode) {
  1136. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1137. return;
  1138. param::ConvBias param;
  1139. param.stride_h = 1;
  1140. param.stride_w = 1;
  1141. param.pad_h = p;
  1142. param.pad_w = p;
  1143. param.nonlineMode = nonline_mode;
  1144. //! no bias
  1145. args.emplace_back(
  1146. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1147. TensorShape{});
  1148. args.emplace_back(
  1149. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1150. TensorShape{1, oc, 1, 1});
  1151. args.emplace_back(
  1152. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1153. TensorShape{
  1154. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1155. (w + 2 * p - kernel) / param.stride_w + 1});
  1156. };
  1157. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1158. for (size_t ic : {1, 4, 8, 16})
  1159. for (size_t oc : {1, 4, 8, 16, 300})
  1160. for (size_t p : {0, 2})
  1161. for (size_t size : {8, 24})
  1162. for (NonlineMode nonline_mode :
  1163. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1164. run(oc, ic, size, size, kernel, p, nonline_mode);
  1165. }
  1166. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1167. Checker<ConvBias> checker(handle());
  1168. #define cb(algo_name) \
  1169. checker.set_before_exec_callback( \
  1170. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1171. for (auto&& arg : args) { \
  1172. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  1173. }
  1174. cb("IM2COLMATMUL:X86_F32_6x16:192");
  1175. }
  1176. #if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
  1177. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA) {
  1178. using namespace conv_bias;
  1179. std::vector<TestArg> args;
  1180. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1181. NonlineMode nonline_mode) {
  1182. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1183. return;
  1184. param::ConvBias param;
  1185. param.stride_h = 1;
  1186. param.stride_w = 1;
  1187. param.pad_h = p;
  1188. param.pad_w = p;
  1189. param.nonlineMode = nonline_mode;
  1190. //! no bias
  1191. args.emplace_back(
  1192. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1193. TensorShape{});
  1194. args.emplace_back(
  1195. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1196. TensorShape{1, oc, 1, 1});
  1197. args.emplace_back(
  1198. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1199. TensorShape{
  1200. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1201. (w + 2 * p - kernel) / param.stride_w + 1});
  1202. param.sparse = param::ConvBias::Sparse::GROUP;
  1203. args.emplace_back(
  1204. param, TensorShape{1, 2 * ic, h, w},
  1205. TensorShape{2, oc, ic, kernel, kernel}, TensorShape{});
  1206. args.emplace_back(
  1207. param, TensorShape{1, 2 * ic, h, w},
  1208. TensorShape{2, oc, ic, kernel, kernel}, TensorShape{1, oc * 2, 1, 1});
  1209. args.emplace_back(
  1210. param, TensorShape{1, 2 * ic, h, w},
  1211. TensorShape{2, oc, ic, kernel, kernel},
  1212. TensorShape{
  1213. 1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  1214. (w + 2 * param.pad_w - kernel) / 1 + 1});
  1215. };
  1216. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1217. for (size_t ic : {1, 4, 8, 16})
  1218. for (size_t oc : {1, 4, 8, 16})
  1219. for (size_t p : {0, 1})
  1220. for (size_t size : {8, 24})
  1221. for (NonlineMode nonline_mode :
  1222. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1223. run(oc, ic, size, size, kernel, p, nonline_mode);
  1224. }
  1225. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1226. Checker<ConvBias> checker(handle());
  1227. #define cb(algo_name) \
  1228. checker.set_before_exec_callback( \
  1229. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1230. for (auto&& arg : args) { \
  1231. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  1232. }
  1233. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  1234. #undef cb
  1235. }
  1236. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA_FILTER_PREPROCESS) {
  1237. using namespace conv_bias;
  1238. std::vector<TestArg> args;
  1239. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1240. NonlineMode nonline_mode) {
  1241. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1242. return;
  1243. param::ConvBias param;
  1244. param.stride_h = 1;
  1245. param.stride_w = 1;
  1246. param.pad_h = p;
  1247. param.pad_w = p;
  1248. param.nonlineMode = nonline_mode;
  1249. //! no bias
  1250. args.emplace_back(
  1251. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1252. TensorShape{});
  1253. args.emplace_back(
  1254. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1255. TensorShape{1, oc, 1, 1});
  1256. args.emplace_back(
  1257. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1258. TensorShape{
  1259. 1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
  1260. (w + 2 * p - kernel) / param.stride_w + 1});
  1261. param.sparse = param::ConvBias::Sparse::GROUP;
  1262. args.emplace_back(
  1263. param, TensorShape{1, 2 * ic, h, w},
  1264. TensorShape{2, oc, ic, kernel, kernel}, TensorShape{});
  1265. args.emplace_back(
  1266. param, TensorShape{1, 2 * ic, h, w},
  1267. TensorShape{2, oc, ic, kernel, kernel}, TensorShape{1, oc * 2, 1, 1});
  1268. args.emplace_back(
  1269. param, TensorShape{1, 2 * ic, h, w},
  1270. TensorShape{2, oc, ic, kernel, kernel},
  1271. TensorShape{
  1272. 1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  1273. (w + 2 * param.pad_w - kernel) / 1 + 1});
  1274. };
  1275. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1276. for (size_t ic : {1, 4, 8, 16})
  1277. for (size_t oc : {1, 4, 8, 16})
  1278. for (size_t p : {0, 1})
  1279. for (size_t size : {8, 24})
  1280. for (NonlineMode nonline_mode :
  1281. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  1282. run(oc, ic, size, size, kernel, p, nonline_mode);
  1283. }
  1284. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1285. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1286. handle());
  1287. #define cb(algo_name) \
  1288. checker.set_before_exec_callback( \
  1289. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1290. for (auto&& arg : args) { \
  1291. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  1292. }
  1293. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  1294. #undef cb
  1295. }
  1296. /**************************** Conv1x1 PackA *************************/
  1297. namespace {
  1298. void checker_conv_bias(
  1299. std::vector<conv_bias::TestArg> args, Handle* handle, RNG* rng, float epsilon,
  1300. DType type0, DType type1, DType type2, DType type3, const char* algo_name) {
  1301. using namespace conv_bias;
  1302. Checker<ConvBias> checker(handle);
  1303. checker.set_before_exec_callback(
  1304. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1305. checker.set_dtype(0, type0);
  1306. checker.set_dtype(1, type1);
  1307. checker.set_dtype(2, type2);
  1308. checker.set_dtype(4, type3);
  1309. checker.set_epsilon(epsilon);
  1310. if (NULL != rng) {
  1311. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1312. }
  1313. for (auto&& arg : args) {
  1314. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1315. }
  1316. }
  1317. void checker_conv_bias_preprocess(
  1318. std::vector<conv_bias::TestArg> args, Handle* handle, RNG* rng, float epsilon,
  1319. DType type0, DType type1, DType type2, DType type3, const char* algo_name) {
  1320. using namespace conv_bias;
  1321. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(handle);
  1322. checker.set_before_exec_callback(
  1323. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1324. checker.set_dtype(0, type0);
  1325. checker.set_dtype(1, type1);
  1326. checker.set_dtype(2, type2);
  1327. checker.set_dtype(4, type3);
  1328. checker.set_epsilon(epsilon);
  1329. if (NULL != rng) {
  1330. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1331. }
  1332. for (auto&& arg : args) {
  1333. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1334. }
  1335. }
  1336. } // namespace
  1337. #if MEGDNN_X86_WITH_MKL
  1338. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA) {
  1339. using namespace conv_bias;
  1340. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1341. check_conv_bias(args, handle(), "CONV1x1:X86_F32_MKL_PACKA:24");
  1342. }
  1343. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA_PREPROCESS) {
  1344. using namespace conv_bias;
  1345. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1346. checker_conv_bias_preprocess(
  1347. args, handle(), nullptr, 0.001, dtype::Float32{}, dtype::Float32{},
  1348. dtype::Float32{}, dtype::Float32{}, "CONV1x1:X86_F32_MKL_PACKA:24");
  1349. }
  1350. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS) {
  1351. using namespace conv_bias;
  1352. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1353. check_conv_bias(args, handle(), "CONV1x1:X86_F32_BLAS:48");
  1354. }
  1355. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS_NOPACK_REPROCESS) {
  1356. using namespace conv_bias;
  1357. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1358. checker_conv_bias_preprocess(
  1359. args, handle(), nullptr, 0.001, dtype::Float32{}, dtype::Float32{},
  1360. dtype::Float32{}, dtype::Float32{}, "CONV1x1:X86_F32_BLAS:24");
  1361. }
  1362. #endif
  1363. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32) {
  1364. using namespace conv_bias;
  1365. UniformIntRNG rng{-50, 50};
  1366. float epsilon = 0.001;
  1367. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
  1368. #if MEGDNN_X86_WITH_MKL_DNN
  1369. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1370. checker_conv_bias(
  1371. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1372. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_MKLDNN:24");
  1373. }
  1374. #endif
  1375. #if MEGDNN_X86_WITH_VNNI
  1376. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1377. checker_conv_bias(
  1378. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1379. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_VNNI:24");
  1380. }
  1381. #endif
  1382. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1383. checker_conv_bias(
  1384. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1385. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
  1386. checker_conv_bias(
  1387. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1388. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
  1389. checker_conv_bias(
  1390. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1391. dtype::Int16{}, dtype::Int16{}, "CONV1x1:X86_INT8X8X16_AVX2");
  1392. }
  1393. checker_conv_bias(
  1394. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  1395. dtype::Int32{}, "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
  1396. checker_conv_bias(
  1397. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  1398. dtype::Int16{}, "CONV1x1:X86_INT8X8X16_SSE");
  1399. }
  1400. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32_PREPROCESS) {
  1401. using namespace conv_bias;
  1402. UniformIntRNG rng{-50, 50};
  1403. float epsilon = 0.001;
  1404. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
  1405. #if MEGDNN_X86_WITH_VNNI
  1406. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1407. checker_conv_bias_preprocess(
  1408. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1409. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_VNNI:24");
  1410. }
  1411. #endif
  1412. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1413. checker_conv_bias_preprocess(
  1414. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1415. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
  1416. checker_conv_bias_preprocess(
  1417. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1418. dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
  1419. checker_conv_bias_preprocess(
  1420. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
  1421. dtype::Int16{}, dtype::Int16{}, "CONV1x1:X86_INT8X8X16_AVX2");
  1422. }
  1423. checker_conv_bias_preprocess(
  1424. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  1425. dtype::Int32{}, "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
  1426. checker_conv_bias_preprocess(
  1427. args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  1428. dtype::Int16{}, "CONV1x1:X86_INT8X8X16_SSE");
  1429. }
  1430. /************************* End Conv1x1 PackA ************************/
  1431. #endif
  1432. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_6x16) {
  1433. using namespace conv_bias;
  1434. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  1435. check_conv_bias(args, handle(), "CONV1x1:X86_F32_6x16:48");
  1436. }
  1437. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8) {
  1438. using namespace conv_bias;
  1439. std::vector<TestArg> args;
  1440. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1441. NonlineMode nonline_mode) {
  1442. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1443. return;
  1444. param::ConvBias param;
  1445. param.stride_h = 1;
  1446. param.stride_w = 1;
  1447. param.pad_h = p;
  1448. param.pad_w = p;
  1449. param.nonlineMode = nonline_mode;
  1450. //! no bias
  1451. args.emplace_back(
  1452. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1453. TensorShape{});
  1454. //! bias channel
  1455. args.emplace_back(
  1456. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1457. TensorShape{1, oc, 1, 1});
  1458. };
  1459. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1460. for (size_t ic : {1, 4, 8, 16})
  1461. for (size_t oc : {1, 4, 8})
  1462. for (size_t p : {0, 2})
  1463. for (size_t size : {20, 21, 24})
  1464. for (NonlineMode nonline_mode :
  1465. {NonlineMode::IDENTITY, NonlineMode::RELU,
  1466. NonlineMode::H_SWISH}) {
  1467. run(oc, ic, size, size, kernel, p, nonline_mode);
  1468. }
  1469. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1470. Checker<ConvBias> checker(handle());
  1471. #define cb(algo_name) \
  1472. checker.set_before_exec_callback( \
  1473. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1474. UniformIntRNG rng{-50, 50}; \
  1475. for (auto&& arg : args) { \
  1476. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  1477. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  1478. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  1479. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  1480. .set_rng(0, &rng) \
  1481. .set_rng(1, &rng) \
  1482. .set_rng(2, &rng) \
  1483. .set_param(arg.param) \
  1484. .execs({arg.src, arg.filter, {}, {}, {}}); \
  1485. }
  1486. #if MEGDNN_X86_WITH_MKL_DNN
  1487. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1488. cb("IM2COLMATMUL");
  1489. }
  1490. #endif
  1491. #if MEGDNN_X86_WITH_VNNI
  1492. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1493. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  1494. }
  1495. #endif
  1496. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1497. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  1498. }
  1499. #undef cb
  1500. }
  1501. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8_FILTER_PREPROCESS) {
  1502. using namespace conv_bias;
  1503. std::vector<TestArg> args;
  1504. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1505. NonlineMode nonline_mode) {
  1506. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1507. return;
  1508. param::ConvBias param;
  1509. param.stride_h = 1;
  1510. param.stride_w = 1;
  1511. param.pad_h = p;
  1512. param.pad_w = p;
  1513. param.nonlineMode = nonline_mode;
  1514. //! no bias
  1515. args.emplace_back(
  1516. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1517. TensorShape{});
  1518. //! bias channel
  1519. args.emplace_back(
  1520. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1521. TensorShape{1, oc, 1, 1});
  1522. };
  1523. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  1524. for (size_t ic : {1, 4, 8, 16})
  1525. for (size_t oc : {1, 4, 8})
  1526. for (size_t p : {0, 2})
  1527. for (size_t size : {20, 21, 24})
  1528. for (NonlineMode nonline_mode :
  1529. {NonlineMode::IDENTITY, NonlineMode::RELU,
  1530. NonlineMode::H_SWISH}) {
  1531. run(oc, ic, size, size, kernel, p, nonline_mode);
  1532. }
  1533. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  1534. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1535. handle());
  1536. #define cb(algo_name) \
  1537. checker.set_before_exec_callback( \
  1538. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  1539. UniformIntRNG rng{-50, 50}; \
  1540. for (auto&& arg : args) { \
  1541. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  1542. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  1543. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  1544. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  1545. .set_rng(0, &rng) \
  1546. .set_rng(1, &rng) \
  1547. .set_rng(2, &rng) \
  1548. .set_param(arg.param) \
  1549. .execs({arg.src, arg.filter, {}, {}, {}}); \
  1550. }
  1551. #if MEGDNN_X86_WITH_MKL_DNN
  1552. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1553. cb("IM2COLMATMUL");
  1554. }
  1555. #endif
  1556. #if MEGDNN_X86_WITH_VNNI
  1557. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1558. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  1559. }
  1560. #endif
  1561. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1562. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  1563. }
  1564. #undef cb
  1565. }
  1566. #if MEGDNN_WITH_BENCHMARK
  1567. #if MEGDNN_X86_WITH_MKL_DNN
  1568. static void x86_benchmark_fp32_mkldnn(Handle* handle) {
  1569. constexpr size_t RUNS = 30;
  1570. param::ConvBias param;
  1571. Benchmarker<ConvBias> benchmarker_mkldnn(handle);
  1572. benchmarker_mkldnn.set_display(false).set_times(RUNS);
  1573. benchmarker_mkldnn.set_before_exec_callback(
  1574. AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));
  1575. Benchmarker<ConvBias> benchmarker_im2col(handle);
  1576. benchmarker_im2col.set_display(false).set_times(RUNS);
  1577. benchmarker_im2col.set_before_exec_callback(
  1578. AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
  1579. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  1580. size_t SZ, size_t GROUP = 1) {
  1581. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}), bias({1, OC, 1, 1}),
  1582. z({}), dst({N, OC, H / SZ, W / SZ});
  1583. param.pad_h = FS / 2;
  1584. param.pad_w = FS / 2;
  1585. param.stride_h = SZ;
  1586. param.stride_w = SZ;
  1587. param.format = param::ConvBias::Format::NCHW;
  1588. param.sparse = param::ConvBias::Sparse::DENSE;
  1589. if (GROUP > 1) {
  1590. param.sparse = param::ConvBias::Sparse::GROUP;
  1591. filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
  1592. }
  1593. auto im2col_used =
  1594. benchmarker_im2col.set_param(param).exec({src, filter, bias, z, dst}) /
  1595. RUNS;
  1596. src = IC < 8 ? TensorShape{N, IC, H, W} : TensorShape{N, IC / 8, H, W, 8};
  1597. filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
  1598. : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
  1599. if (GROUP > 1 && OC == GROUP && IC == GROUP) {
  1600. filter = {GROUP / 8, 1, 1, FS, FS, 8};
  1601. } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
  1602. filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
  1603. }
  1604. bias = {1, OC / 8, 1, 1, 8};
  1605. z = {};
  1606. dst = {N, OC / 8, H / SZ, W / SZ, 8};
  1607. param.format = param::ConvBias::Format::NCHW88;
  1608. auto mkldnn_used =
  1609. benchmarker_mkldnn.set_param(param).exec({src, filter, bias, z, dst}) /
  1610. RUNS;
  1611. float computations =
  1612. (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  1613. std::cout << "run " << src.to_string() << " " << filter.to_string() << " "
  1614. << bias.to_string() << " " << dst.to_string() << std::endl;
  1615. std::cout << "im2col: " << im2col_used << " ms, "
  1616. << (computations / im2col_used) << " Gops, ";
  1617. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  1618. << (computations / mkldnn_used) << " Gops, "
  1619. << "spped up: " << (im2col_used / mkldnn_used) << ", ";
  1620. std::cout << std::endl;
  1621. };
  1622. run(1, 64, 64, 56, 56, 3, 1);
  1623. run(1, 3, 64, 224, 224, 3, 1);
  1624. run(1, 3, 64, 224, 224, 7, 2);
  1625. run(1, 64, 64, 56, 56, 3, 1);
  1626. run(1, 128, 128, 28, 28, 3, 1);
  1627. run(1, 256, 256, 14, 14, 3, 1);
  1628. run(1, 512, 512, 7, 7, 3, 1);
  1629. run(1, 256, 64, 56, 56, 1, 1);
  1630. run(1, 512, 128, 28, 28, 1, 1);
  1631. run(1, 1024, 256, 14, 14, 1, 1);
  1632. run(1, 2048, 512, 7, 7, 1, 1);
  1633. run(1, 32, 32, 112, 112, 3, 1, 32);
  1634. run(1, 144, 144, 56, 56, 3, 1, 144);
  1635. run(1, 192, 192, 28, 28, 3, 1, 192);
  1636. run(1, 384, 384, 28, 28, 3, 1, 384);
  1637. run(1, 576, 576, 14, 14, 3, 1, 576);
  1638. run(1, 960, 960, 7, 7, 3, 1, 960);
  1639. run(1, 256, 128, 56, 56, 1, 2, 1);
  1640. run(1, 512, 256, 28, 28, 1, 2, 1);
  1641. run(1, 1024, 512, 14, 14, 1, 2, 1);
  1642. run(1, 96, 96, 112, 112, 3, 2, 96);
  1643. run(1, 144, 144, 56, 56, 3, 2, 144);
  1644. run(1, 384, 384, 28, 28, 3, 2, 384);
  1645. run(1, 576, 576, 14, 14, 3, 2, 576);
  1646. }
  1647. TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1648. x86_benchmark_fp32_mkldnn(handle());
  1649. }
  1650. TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1651. x86_benchmark_fp32_mkldnn(handle());
  1652. }
  1653. #endif
  1654. #endif
  1655. /************************* Winograd ****************************/
  1656. namespace {
  1657. std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
  1658. std::vector<conv_bias::TestArg> args;
  1659. param::ConvBias cur_param;
  1660. cur_param.format = param::ConvBias::Format::NCHW88;
  1661. using NLMode = param::ConvBias::NonlineMode;
  1662. // clang-format off
  1663. for (auto nlmode :
  1664. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  1665. for (size_t ic : {1, 2}) {
  1666. for (size_t oc : {1, 2}) {
  1667. for (size_t i : {9, 63}) {
  1668. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  1669. cur_param.nonlineMode = nlmode;
  1670. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1671. cur_param.pad_h = cur_param.pad_w = 1;
  1672. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1673. TensorShape{oc, ic, 3, 3, 8, 8},
  1674. TensorShape{1, oc, 1, 1, 8});
  1675. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1676. TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
  1677. //! bias
  1678. args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
  1679. TensorShape{oc, ic, 3, 3, 8, 8},
  1680. TensorShape{2, oc, i, i, 8});
  1681. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1682. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
  1683. TensorShape{2, oc, ic, 3, 3, 8, 8},
  1684. TensorShape{1, 2 * oc, 1, 1, 8});*/
  1685. }}}
  1686. // clang-format on
  1687. //! test for multi-thread OC parallel
  1688. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1689. cur_param.pad_h = cur_param.pad_w = 1;
  1690. args.emplace_back(
  1691. cur_param, TensorShape{2, 1, 9, 9, 8}, TensorShape{128, 1, 3, 3, 8, 8},
  1692. TensorShape{1, 128, 1, 1, 8});
  1693. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1694. args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
  1695. TensorShape{2, 128, 1, 3, 3, 8, 8},
  1696. TensorShape{1, 2 * 128, 1, 1, 8});*/
  1697. }
  1698. return args;
  1699. }
  1700. } // namespace
  1701. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
  1702. using namespace conv_bias;
  1703. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1704. Checker<ConvBiasForward> checker(handle());
  1705. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1706. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1707. for (auto&& arg : args) {
  1708. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1709. }
  1710. }
  1711. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63_WEIGHT_PREPROCESS) {
  1712. using namespace conv_bias;
  1713. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1714. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1715. handle());
  1716. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1717. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1718. for (auto&& arg : args) {
  1719. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1720. }
  1721. }
  1722. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
  1723. using namespace conv_bias;
  1724. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1725. Checker<ConvBiasForward> checker(handle());
  1726. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1727. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1728. for (auto&& arg : args) {
  1729. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1730. }
  1731. }
  1732. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23_WEIGHT_PREPROCESS) {
  1733. using namespace conv_bias;
  1734. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1735. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1736. handle());
  1737. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1738. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1739. for (auto&& arg : args) {
  1740. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  1741. }
  1742. }
  1743. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
  1744. using namespace conv_bias;
  1745. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1746. Checker<ConvBiasForward> checker(handle());
  1747. auto run = [&checker](
  1748. const std::vector<TestArg>& args, DType A_dtype, DType B_dtype,
  1749. DType C_dtype, DType D_dtype, const float eps) {
  1750. for (auto&& arg : args) {
  1751. checker.set_dtype(0, A_dtype)
  1752. .set_dtype(1, B_dtype)
  1753. .set_dtype(2, C_dtype)
  1754. .set_dtype(4, D_dtype)
  1755. .set_epsilon(eps)
  1756. .set_param(arg.param)
  1757. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  1758. }
  1759. };
  1760. run(args, dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32(),
  1761. 1e-3f);
  1762. }
  1763. /*********************************** End winograd ************************/
  1764. #if MEGDNN_X86_WITH_MKL_DNN
  1765. static void x86_correctness_fp32_mkldnn_run(
  1766. Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
  1767. ConvBiasForward::BiasMode bias_mode, param::ConvBias::NonlineMode noline_mode,
  1768. size_t n, size_t stride, size_t kernel, size_t oc, size_t ic, size_t h,
  1769. size_t w, size_t group) {
  1770. auto oc_per_group = oc / group;
  1771. auto ic_per_group = ic / group;
  1772. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  1773. (ic_per_group % 8 == 0 || ic_per_group == 3) && ic_per_group > 0;
  1774. bool ok_depthwise = oc == ic && oc == group;
  1775. if (!(ok_group || ok_depthwise)) {
  1776. return;
  1777. }
  1778. size_t pad = kernel / 2;
  1779. size_t kernel_h = kernel;
  1780. size_t kernel_w = kernel;
  1781. param::ConvBias param;
  1782. param.format = param::ConvBias::Format::NCHW88;
  1783. param.stride_h = stride;
  1784. param.stride_w = stride;
  1785. param.pad_h = pad;
  1786. param.pad_w = pad;
  1787. param.nonlineMode = noline_mode;
  1788. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  1789. if (ic == 3) {
  1790. src_tensor_shape = TensorShape{n, ic, h, w};
  1791. }
  1792. auto weight_tensor_shape = TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  1793. if (ic == 3) {
  1794. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  1795. }
  1796. auto bias_tensor_shape = TensorShape{};
  1797. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1798. bias_tensor_shape = {1, oc / 8, 1, 1, 8};
  1799. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1800. TensorLayout dst_layout;
  1801. auto ConvBiasOp = handle->create_operator<ConvBias>();
  1802. ConvBiasOp->param() = param;
  1803. ConvBiasOp->deduce_layout(
  1804. {src_tensor_shape, dtype::Float32()},
  1805. {weight_tensor_shape, dtype::Float32()}, {}, {}, dst_layout);
  1806. bias_tensor_shape = dst_layout;
  1807. }
  1808. if (group == 1) {
  1809. param.sparse = param::ConvBias::Sparse::DENSE;
  1810. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1811. param.sparse = param::ConvBias::Sparse::GROUP;
  1812. weight_tensor_shape = TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  1813. } else if (
  1814. group > 1 && oc / group % 8 == 0 && oc / group > 0 && ic / group % 8 == 0 &&
  1815. ic / group > 0) {
  1816. param.sparse = param::ConvBias::Sparse::GROUP;
  1817. weight_tensor_shape = TensorShape{
  1818. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8, 8};
  1819. }
  1820. checker.set_dtype(0, dtype::Float32())
  1821. .set_dtype(1, dtype::Float32())
  1822. .set_dtype(2, dtype::Float32())
  1823. .set_dtype(4, dtype::Float32())
  1824. .set_rng(0, &rng)
  1825. .set_rng(1, &rng)
  1826. .set_rng(2, &rng)
  1827. .set_epsilon(1e-3)
  1828. .set_param(param)
  1829. .execs({src_tensor_shape, weight_tensor_shape, bias_tensor_shape, {}, {}});
  1830. }
  1831. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  1832. Checker<ConvBias> checker(handle);
  1833. UniformIntRNG rng{-127, 127};
  1834. checker.set_before_exec_callback(
  1835. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_CONV_FP32"));
  1836. for (auto bias_mode :
  1837. {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
  1838. megdnn::BiasMode::BIAS})
  1839. for (auto noline_mode :
  1840. {param::ConvBias::NonlineMode::IDENTITY,
  1841. param::ConvBias::NonlineMode::SIGMOID,
  1842. param::ConvBias::NonlineMode::H_SWISH})
  1843. for (size_t n : {1, 2})
  1844. for (size_t stride : {1, 2})
  1845. for (size_t kernel : {3, 5, 7})
  1846. for (size_t oc : {8, 16})
  1847. for (size_t ic : {3, 8, 16})
  1848. for (size_t h : {22, 33})
  1849. for (size_t w : {22, 33}) {
  1850. for (size_t group = 1;
  1851. group <= std::min(oc, ic); ++group) {
  1852. x86_correctness_fp32_mkldnn_run(
  1853. checker, rng, handle, bias_mode,
  1854. noline_mode, n, stride, kernel, oc,
  1855. ic, h, w, group);
  1856. }
  1857. }
  1858. }
  1859. TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1860. x86_correctness_fp32_mkldnn(handle());
  1861. }
  1862. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1863. x86_correctness_fp32_mkldnn(handle());
  1864. }
  1865. TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
  1866. using namespace conv_bias;
  1867. std::vector<TestArg> args;
  1868. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1869. NonlineMode nonline_mode) {
  1870. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1871. return;
  1872. param::ConvBias param;
  1873. param.stride_h = 1;
  1874. param.stride_w = 1;
  1875. param.pad_h = p;
  1876. param.pad_w = p;
  1877. param.nonlineMode = nonline_mode;
  1878. //! no bias
  1879. args.emplace_back(
  1880. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1881. TensorShape{});
  1882. };
  1883. for (size_t kernel : {2, 3, 5, 7})
  1884. for (size_t ic : {1, 2, 3, 4})
  1885. for (size_t oc : {1, 2, 4})
  1886. for (size_t p : {0, 2})
  1887. for (size_t size : {20, 21, 22, 23, 24})
  1888. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1889. run(oc, ic, size, size, kernel, p, nonline_mode);
  1890. }
  1891. Checker<ConvBias> checker(handle());
  1892. checker.set_before_exec_callback(
  1893. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_MATMUL_INT8"));
  1894. checker.set_epsilon(1);
  1895. UniformIntRNG rng{-50, 50};
  1896. checker.set_dtype(0, dtype::Int8())
  1897. .set_dtype(1, dtype::Int8())
  1898. .set_dtype(2, dtype::Int32())
  1899. .set_dtype(4, dtype::Int32())
  1900. .set_rng(0, &rng)
  1901. .set_rng(1, &rng)
  1902. .set_rng(2, &rng);
  1903. for (auto&& arg : args) {
  1904. checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
  1905. }
  1906. }
  1907. TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
  1908. using namespace conv_bias;
  1909. std::vector<TestArg> args;
  1910. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1911. NonlineMode nonline_mode) {
  1912. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1913. return;
  1914. param::ConvBias param;
  1915. param.stride_h = 1;
  1916. param.stride_w = 1;
  1917. param.pad_h = p;
  1918. param.pad_w = p;
  1919. param.nonlineMode = nonline_mode;
  1920. //! no bias
  1921. args.emplace_back(
  1922. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1923. TensorShape{});
  1924. };
  1925. for (size_t kernel : {2, 3, 5, 7})
  1926. for (size_t ic : {1, 2, 3, 4})
  1927. for (size_t oc : {1, 2, 4})
  1928. for (size_t p : {0, 2})
  1929. for (size_t size : {20, 22, 24})
  1930. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1931. run(oc, ic, size, size, kernel, p, nonline_mode);
  1932. }
  1933. Checker<ConvBias> checker(handle());
  1934. checker.set_before_exec_callback(
  1935. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1936. checker.set_epsilon(1);
  1937. UniformIntRNG rng{-50, 50};
  1938. checker.set_dtype(0, dtype::Int8())
  1939. .set_dtype(1, dtype::Int8())
  1940. .set_dtype(2, dtype::Int32())
  1941. .set_dtype(4, dtype::Int32())
  1942. .set_rng(0, &rng)
  1943. .set_rng(1, &rng)
  1944. .set_rng(2, &rng);
  1945. for (auto&& arg : args) {
  1946. checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
  1947. }
  1948. }
  1949. TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
  1950. using namespace conv_bias;
  1951. std::vector<TestArg> args;
  1952. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1953. NonlineMode nonline_mode) {
  1954. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1955. return;
  1956. param::ConvBias param;
  1957. param.stride_h = 1;
  1958. param.stride_w = 1;
  1959. param.pad_h = p;
  1960. param.pad_w = p;
  1961. param.nonlineMode = nonline_mode;
  1962. //! no bias
  1963. args.emplace_back(
  1964. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1965. TensorShape{});
  1966. };
  1967. for (size_t kernel : {2, 3, 5, 7})
  1968. for (size_t ic : {1, 2, 3, 4})
  1969. for (size_t oc : {1, 2, 4})
  1970. for (size_t p : {0, 2})
  1971. for (size_t size : {20, 22, 24})
  1972. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1973. run(oc, ic, size, size, kernel, p, nonline_mode);
  1974. }
  1975. Checker<ConvBias> checker(handle());
  1976. checker.set_before_exec_callback(
  1977. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1978. checker.set_epsilon(1);
  1979. UniformIntRNG rng{-50, 50};
  1980. checker.set_dtype(0, dtype::Int8())
  1981. .set_dtype(1, dtype::Int8())
  1982. .set_dtype(2, dtype::Int32())
  1983. .set_dtype(4, dtype::Int32())
  1984. .set_rng(0, &rng)
  1985. .set_rng(1, &rng)
  1986. .set_rng(2, &rng);
  1987. for (auto&& arg : args) {
  1988. checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
  1989. }
  1990. }
  1991. #endif
  1992. #if MEGDNN_WITH_BENCHMARK
  1993. namespace {
  1994. void benchmark_impl(
  1995. const param::ConvBias param,
  1996. std::vector<std::pair<SmallVector<TensorShape>, float>>& shapes_and_computation,
  1997. const std::string algo_name, size_t RUNS,
  1998. TaskExecutorConfig&& multi_thread_config,
  1999. TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
  2000. std::vector<DType> data_type = {
  2001. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2002. std::vector<float> multi_thread_times, single_thread_times;
  2003. {
  2004. auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
  2005. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  2006. benchmarker.set_times(RUNS)
  2007. .set_display(false)
  2008. .set_dtype(0, dtype_v[0])
  2009. .set_dtype(1, dtype_v[1])
  2010. .set_dtype(2, dtype_v[2])
  2011. .set_dtype(4, dtype_v[3])
  2012. .set_param(param)
  2013. .set_before_exec_callback(
  2014. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
  2015. for (auto shape : shapes_and_computation) {
  2016. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2017. }
  2018. }
  2019. {
  2020. auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
  2021. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  2022. benchmarker.set_times(RUNS)
  2023. .set_display(false)
  2024. .set_dtype(0, dtype_v[0])
  2025. .set_dtype(1, dtype_v[1])
  2026. .set_dtype(2, dtype_v[2])
  2027. .set_dtype(4, dtype_v[3])
  2028. .set_param(param)
  2029. .set_before_exec_callback(
  2030. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
  2031. for (auto shape : shapes_and_computation) {
  2032. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2033. }
  2034. }
  2035. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  2036. printf("core_ids:");
  2037. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  2038. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  2039. }
  2040. printf(", Single thread core_id %zu\n", single_thread_config.affinity_core_set[0]);
  2041. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  2042. auto shapes = shapes_and_computation[i];
  2043. printf("Bench case: ");
  2044. for (auto&& shape : shapes.first) {
  2045. printf("%s ", shape.to_string().c_str());
  2046. }
  2047. float computations = shapes.second;
  2048. printf("%zu threads gflops: %f,\n single thread gflops: "
  2049. "%f. spead up = %f, speedup/cores=%f\n",
  2050. multi_thread_config.nr_thread, computations / multi_thread_times[i],
  2051. computations / single_thread_times[i],
  2052. single_thread_times[i] / multi_thread_times[i],
  2053. single_thread_times[i] / multi_thread_times[i] /
  2054. multi_thread_config.nr_thread);
  2055. }
  2056. }
  2057. void benchmark_impl_comp(
  2058. const param::ConvBias param,
  2059. std::vector<std::pair<SmallVector<TensorShape>, float>>& shapes_and_computation,
  2060. const std::string algo_name, const std::string algo_name1, size_t RUNS,
  2061. TaskExecutorConfig&& multi_thread_config,
  2062. TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
  2063. std::vector<DType> data_type = {
  2064. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2065. std::vector<float> multi_thread_times, single_thread_times;
  2066. {
  2067. auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
  2068. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  2069. benchmarker.set_times(RUNS)
  2070. .set_display(false)
  2071. .set_dtype(0, dtype_v[0])
  2072. .set_dtype(1, dtype_v[1])
  2073. .set_dtype(2, dtype_v[2])
  2074. .set_dtype(4, dtype_v[3])
  2075. .set_param(param)
  2076. .set_before_exec_callback(
  2077. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
  2078. for (auto shape : shapes_and_computation) {
  2079. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2080. }
  2081. }
  2082. {
  2083. auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
  2084. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  2085. benchmarker.set_times(RUNS)
  2086. .set_display(false)
  2087. .set_dtype(0, dtype_v[0])
  2088. .set_dtype(1, dtype_v[1])
  2089. .set_dtype(2, dtype_v[2])
  2090. .set_dtype(4, dtype_v[3])
  2091. .set_param(param)
  2092. .set_before_exec_callback(
  2093. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name1.c_str()));
  2094. for (auto shape : shapes_and_computation) {
  2095. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  2096. }
  2097. }
  2098. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  2099. printf("core_ids:");
  2100. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  2101. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  2102. }
  2103. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  2104. auto shapes = shapes_and_computation[i];
  2105. printf("Bench case: ");
  2106. for (auto&& shape : shapes.first) {
  2107. printf("%s ", shape.to_string().c_str());
  2108. }
  2109. float computations = shapes.second;
  2110. printf("algo:%s gflops: %f,\n algo:%s gflops: "
  2111. "%f. spead up = %f\n",
  2112. algo_name.c_str(), computations / multi_thread_times[i],
  2113. algo_name1.c_str(), computations / single_thread_times[i],
  2114. single_thread_times[i] / multi_thread_times[i]);
  2115. }
  2116. }
  2117. } // namespace
  2118. static void benchmark_convbias_chanwise_avx2_int8(uint32_t stride, const char* algo) {
  2119. constexpr size_t RUNS = 50;
  2120. param::ConvBias param;
  2121. param.stride_h = stride;
  2122. param.stride_w = stride;
  2123. param.sparse = param::ConvBias::Sparse::GROUP;
  2124. std::vector<DType> data_type = {
  2125. dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
  2126. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2127. auto bench_case = [&](size_t N, size_t IC, size_t H, size_t W, size_t FS) {
  2128. param.pad_h = FS / 2;
  2129. param.pad_w = FS / 2;
  2130. SmallVector<TensorShape> shapes{{N, IC, H, W}, {IC, 1, 1, FS, FS}, {}, {}, {}};
  2131. TensorShape dst{
  2132. N, IC, (H + 2 * param.pad_h - FS) + 1, (W + 2 * param.pad_w - FS) + 1};
  2133. float computations = (FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2134. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2135. };
  2136. bench_case(1, 32, 112, 112, 7);
  2137. bench_case(1, 144, 56, 56, 7);
  2138. bench_case(1, 192, 28, 28, 7);
  2139. bench_case(1, 384, 28, 28, 7);
  2140. bench_case(1, 576, 14, 14, 7);
  2141. bench_case(1, 960, 7, 7, 7);
  2142. bench_case(1, 32, 112, 112, 5);
  2143. bench_case(1, 144, 56, 56, 5);
  2144. bench_case(1, 192, 28, 28, 5);
  2145. bench_case(1, 384, 28, 28, 5);
  2146. bench_case(1, 576, 14, 14, 5);
  2147. bench_case(1, 960, 7, 7, 5);
  2148. bench_case(1, 32, 112, 112, 3);
  2149. bench_case(1, 144, 56, 56, 3);
  2150. bench_case(1, 192, 28, 28, 3);
  2151. bench_case(1, 384, 28, 28, 3);
  2152. bench_case(1, 576, 14, 14, 3);
  2153. bench_case(1, 960, 7, 7, 3);
  2154. bench_case(1, 32, 112, 112, 2);
  2155. bench_case(1, 144, 56, 56, 2);
  2156. bench_case(1, 192, 28, 28, 2);
  2157. bench_case(1, 384, 28, 28, 2);
  2158. bench_case(1, 576, 14, 14, 2);
  2159. bench_case(1, 960, 7, 7, 2);
  2160. std::string algo_name = algo;
  2161. printf("Benchmark %s\n", algo);
  2162. benchmark_impl(
  2163. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2164. data_type);
  2165. benchmark_impl(
  2166. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2167. data_type);
  2168. shapes_and_computation.clear();
  2169. }
  2170. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S1) {
  2171. benchmark_convbias_chanwise_avx2_int8(
  2172. 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
  2173. }
  2174. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S2) {
  2175. benchmark_convbias_chanwise_avx2_int8(
  2176. 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
  2177. }
  2178. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
  2179. constexpr size_t RUNS = 50;
  2180. param::ConvBias param;
  2181. param.stride_h = 1;
  2182. param.stride_w = 1;
  2183. param.sparse = param::ConvBias::Sparse::DENSE;
  2184. std::vector<DType> data_type = {
  2185. dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
  2186. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2187. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2188. size_t FS) {
  2189. param.pad_h = FS / 2;
  2190. param.pad_w = FS / 2;
  2191. SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2192. TensorShape dst{
  2193. N, OC, (H + 2 * param.pad_h - FS) + 1, (W + 2 * param.pad_w - FS) + 1};
  2194. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2195. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2196. };
  2197. bench_case(1, 32, 32, 200, 200, 7);
  2198. bench_case(1, 32, 64, 200, 200, 7);
  2199. bench_case(1, 32, 32, 128, 128, 7);
  2200. bench_case(1, 32, 64, 128, 128, 7);
  2201. bench_case(1, 32, 32, 100, 100, 7);
  2202. bench_case(1, 32, 64, 100, 100, 7);
  2203. bench_case(1, 32, 32, 80, 80, 7);
  2204. bench_case(1, 32, 64, 80, 80, 7);
  2205. bench_case(1, 32, 32, 200, 200, 5);
  2206. bench_case(1, 32, 64, 200, 200, 5);
  2207. bench_case(1, 32, 32, 128, 128, 5);
  2208. bench_case(1, 32, 64, 128, 128, 5);
  2209. bench_case(1, 32, 32, 100, 100, 5);
  2210. bench_case(1, 32, 64, 100, 100, 5);
  2211. bench_case(1, 32, 32, 80, 80, 5);
  2212. bench_case(1, 32, 64, 80, 80, 5);
  2213. bench_case(1, 32, 32, 200, 200, 3);
  2214. bench_case(1, 32, 64, 200, 200, 3);
  2215. bench_case(1, 32, 32, 128, 128, 3);
  2216. bench_case(1, 32, 64, 128, 128, 3);
  2217. bench_case(1, 32, 32, 100, 100, 3);
  2218. bench_case(1, 32, 64, 100, 100, 3);
  2219. bench_case(1, 32, 32, 80, 80, 3);
  2220. bench_case(1, 32, 64, 80, 80, 3);
  2221. bench_case(1, 32, 32, 200, 200, 2);
  2222. bench_case(1, 32, 64, 200, 200, 2);
  2223. bench_case(1, 32, 32, 128, 128, 2);
  2224. bench_case(1, 32, 64, 128, 128, 2);
  2225. bench_case(1, 32, 32, 100, 100, 2);
  2226. bench_case(1, 32, 64, 100, 100, 2);
  2227. bench_case(1, 32, 32, 80, 80, 2);
  2228. bench_case(1, 32, 64, 80, 80, 2);
  2229. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
  2230. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
  2231. benchmark_impl(
  2232. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2233. data_type);
  2234. benchmark_impl(
  2235. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2236. data_type);
  2237. benchmark_impl(
  2238. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2239. data_type);
  2240. shapes_and_computation.clear();
  2241. }
  2242. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_8816) {
  2243. constexpr size_t RUNS = 30;
  2244. param::ConvBias param;
  2245. param.stride_h = 1;
  2246. param.stride_w = 1;
  2247. param.sparse = param::ConvBias::Sparse::DENSE;
  2248. std::vector<DType> data_type = {
  2249. dtype::Int8(), dtype::Int8(), dtype::Int16(), dtype::Int16()};
  2250. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2251. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2252. size_t FS) {
  2253. param.pad_h = FS / 2;
  2254. param.pad_w = FS / 2;
  2255. SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2256. TensorShape dst{
  2257. N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2258. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2259. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2260. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2261. };
  2262. bench_case(1, 48, 192, 15, 15, 1);
  2263. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X16_AVX2";
  2264. benchmark_impl(
  2265. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2266. data_type);
  2267. shapes_and_computation.clear();
  2268. }
  2269. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
  2270. constexpr size_t RUNS = 50;
  2271. param::ConvBias param;
  2272. param.stride_h = 2;
  2273. param.stride_w = 2;
  2274. param.sparse = param::ConvBias::Sparse::DENSE;
  2275. std::vector<DType> data_type = {
  2276. dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
  2277. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2278. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2279. size_t FS) {
  2280. param.pad_h = FS / 2;
  2281. param.pad_w = FS / 2;
  2282. SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  2283. TensorShape dst{
  2284. N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  2285. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  2286. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  2287. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2288. };
  2289. bench_case(1, 32, 32, 200, 200, 7);
  2290. bench_case(1, 32, 64, 200, 200, 7);
  2291. bench_case(1, 32, 32, 128, 128, 7);
  2292. bench_case(1, 32, 64, 128, 128, 7);
  2293. bench_case(1, 32, 32, 100, 100, 7);
  2294. bench_case(1, 32, 64, 100, 100, 7);
  2295. bench_case(1, 32, 32, 80, 80, 7);
  2296. bench_case(1, 32, 64, 80, 80, 7);
  2297. bench_case(1, 32, 32, 200, 200, 5);
  2298. bench_case(1, 32, 64, 200, 200, 5);
  2299. bench_case(1, 32, 32, 128, 128, 5);
  2300. bench_case(1, 32, 64, 128, 128, 5);
  2301. bench_case(1, 32, 32, 100, 100, 5);
  2302. bench_case(1, 32, 64, 100, 100, 5);
  2303. bench_case(1, 32, 32, 80, 80, 5);
  2304. bench_case(1, 32, 64, 80, 80, 5);
  2305. bench_case(1, 32, 32, 200, 200, 3);
  2306. bench_case(1, 32, 64, 200, 200, 3);
  2307. bench_case(1, 32, 32, 128, 128, 3);
  2308. bench_case(1, 32, 64, 128, 128, 3);
  2309. bench_case(1, 32, 32, 100, 100, 3);
  2310. bench_case(1, 32, 64, 100, 100, 3);
  2311. bench_case(1, 32, 32, 80, 80, 3);
  2312. bench_case(1, 32, 64, 80, 80, 3);
  2313. bench_case(1, 32, 32, 200, 200, 2);
  2314. bench_case(1, 32, 64, 200, 200, 2);
  2315. bench_case(1, 32, 32, 128, 128, 2);
  2316. bench_case(1, 32, 64, 128, 128, 2);
  2317. bench_case(1, 32, 32, 100, 100, 2);
  2318. bench_case(1, 32, 64, 100, 100, 2);
  2319. bench_case(1, 32, 32, 80, 80, 2);
  2320. bench_case(1, 32, 64, 80, 80, 2);
  2321. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
  2322. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
  2323. benchmark_impl(
  2324. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2325. data_type);
  2326. benchmark_impl(
  2327. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2328. data_type);
  2329. benchmark_impl(
  2330. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2331. data_type);
  2332. shapes_and_computation.clear();
  2333. }
  2334. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
  2335. constexpr size_t RUNS = 50;
  2336. param::ConvBias param;
  2337. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2338. param.pad_h = 1;
  2339. param.pad_w = 1;
  2340. param.stride_h = 1;
  2341. param.stride_w = 1;
  2342. param.sparse = param::ConvBias::Sparse::GROUP;
  2343. std::vector<DType> data_type = {
  2344. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2345. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2346. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2347. size_t group) {
  2348. SmallVector<TensorShape> shapes{
  2349. {N, IC, H, W},
  2350. {group, OC / group, IC / group, FS, FS},
  2351. {1, OC, 1, 1},
  2352. {},
  2353. {N, OC, H, W}};
  2354. TensorShape dst{N, OC, H, W};
  2355. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2356. dst.total_nr_elems()) *
  2357. 1e-6;
  2358. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2359. };
  2360. bench_case(1, 32, 32, 200, 200, 3, 4);
  2361. bench_case(1, 32, 32, 200, 200, 3, 32);
  2362. bench_case(1, 32, 32, 128, 128, 3, 4);
  2363. bench_case(1, 32, 32, 128, 128, 3, 32);
  2364. bench_case(1, 32, 32, 100, 100, 3, 4);
  2365. bench_case(1, 32, 32, 100, 100, 3, 32);
  2366. bench_case(1, 32, 32, 80, 80, 3, 4);
  2367. bench_case(1, 32, 32, 80, 80, 3, 32);
  2368. std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2369. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_GROUP algo\n");
  2370. benchmark_impl(
  2371. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2372. data_type);
  2373. benchmark_impl(
  2374. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2375. data_type);
  2376. benchmark_impl(
  2377. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2378. data_type);
  2379. shapes_and_computation.clear();
  2380. algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  2381. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_DENSE algo\n");
  2382. bench_case(1, 32, 32, 200, 200, 3, 1);
  2383. bench_case(1, 32, 32, 128, 128, 3, 1);
  2384. bench_case(1, 32, 32, 100, 100, 3, 1);
  2385. bench_case(1, 32, 32, 80, 80, 3, 1);
  2386. benchmark_impl(
  2387. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2388. data_type);
  2389. benchmark_impl(
  2390. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2391. data_type);
  2392. benchmark_impl(
  2393. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2394. data_type);
  2395. }
  2396. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
  2397. constexpr size_t RUNS = 50;
  2398. param::ConvBias param;
  2399. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2400. param.pad_h = 1;
  2401. param.pad_w = 1;
  2402. param.stride_h = 1;
  2403. param.stride_w = 1;
  2404. std::vector<DType> data_type = {
  2405. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2406. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2407. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2408. size_t group) {
  2409. SmallVector<TensorShape> shapes{
  2410. {N, IC, H, W},
  2411. {OC / group, IC / group, FS, FS},
  2412. {1, OC, 1, 1},
  2413. {},
  2414. {N, OC, H, W}};
  2415. TensorShape dst{N, OC, H, W};
  2416. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2417. dst.total_nr_elems()) *
  2418. 1e-6;
  2419. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2420. };
  2421. bench_case(1, 32, 32, 200, 200, 3, 1);
  2422. bench_case(1, 32, 32, 200, 200, 3, 1);
  2423. bench_case(1, 32, 32, 128, 128, 3, 1);
  2424. bench_case(1, 32, 32, 128, 128, 3, 1);
  2425. bench_case(1, 32, 32, 100, 100, 3, 1);
  2426. bench_case(1, 32, 32, 100, 100, 3, 1);
  2427. bench_case(1, 32, 32, 80, 80, 3, 1);
  2428. bench_case(1, 32, 32, 80, 80, 3, 1);
  2429. bench_case(1, 64, 32, 7, 7, 3, 1);
  2430. bench_case(1, 64, 64, 7, 7, 3, 1);
  2431. bench_case(1, 64, 128, 7, 7, 3, 1);
  2432. bench_case(1, 64, 256, 7, 7, 3, 1);
  2433. bench_case(1, 64, 512, 7, 7, 3, 1);
  2434. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2435. bench_case(1, 64, 32, 14, 14, 3, 1);
  2436. bench_case(1, 64, 64, 14, 14, 3, 1);
  2437. bench_case(1, 64, 128, 14, 14, 3, 1);
  2438. bench_case(1, 64, 256, 14, 14, 3, 1);
  2439. bench_case(1, 64, 512, 14, 14, 3, 1);
  2440. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2441. bench_case(1, 128, 128, 14, 14, 3, 1);
  2442. bench_case(1, 128, 256, 14, 14, 3, 1);
  2443. bench_case(1, 512, 512, 14, 14, 3, 1);
  2444. bench_case(1, 256, 512, 14, 14, 3, 1);
  2445. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2446. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2447. std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
  2448. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2449. benchmark_impl(
  2450. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2451. data_type);
  2452. benchmark_impl(
  2453. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2454. data_type);
  2455. benchmark_impl(
  2456. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2457. data_type);
  2458. shapes_and_computation.clear();
  2459. }
  2460. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
  2461. constexpr size_t RUNS = 50;
  2462. param::ConvBias param;
  2463. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2464. param.pad_h = 1;
  2465. param.pad_w = 1;
  2466. param.stride_h = 1;
  2467. param.stride_w = 1;
  2468. std::vector<DType> data_type = {
  2469. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2470. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2471. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2472. size_t group) {
  2473. SmallVector<TensorShape> shapes{
  2474. {N, IC, H, W},
  2475. {OC / group, IC / group, FS, FS},
  2476. {1, OC, 1, 1},
  2477. {},
  2478. {N, OC, H, W}};
  2479. TensorShape dst{N, OC, H, W};
  2480. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2481. dst.total_nr_elems()) *
  2482. 1e-6;
  2483. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2484. };
  2485. bench_case(1, 32, 32, 200, 200, 3, 1);
  2486. bench_case(1, 32, 32, 200, 200, 3, 1);
  2487. bench_case(1, 32, 32, 128, 128, 3, 1);
  2488. bench_case(1, 32, 32, 128, 128, 3, 1);
  2489. bench_case(1, 32, 32, 100, 100, 3, 1);
  2490. bench_case(1, 32, 32, 100, 100, 3, 1);
  2491. bench_case(1, 32, 32, 80, 80, 3, 1);
  2492. bench_case(1, 32, 32, 80, 80, 3, 1);
  2493. bench_case(1, 64, 32, 7, 7, 3, 1);
  2494. bench_case(1, 64, 64, 7, 7, 3, 1);
  2495. bench_case(1, 64, 128, 7, 7, 3, 1);
  2496. bench_case(1, 64, 256, 7, 7, 3, 1);
  2497. bench_case(1, 64, 512, 7, 7, 3, 1);
  2498. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2499. bench_case(1, 64, 32, 14, 14, 3, 1);
  2500. bench_case(1, 64, 64, 14, 14, 3, 1);
  2501. bench_case(1, 64, 128, 14, 14, 3, 1);
  2502. bench_case(1, 64, 256, 14, 14, 3, 1);
  2503. bench_case(1, 64, 512, 14, 14, 3, 1);
  2504. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2505. bench_case(1, 128, 128, 14, 14, 3, 1);
  2506. bench_case(1, 128, 256, 14, 14, 3, 1);
  2507. bench_case(1, 512, 512, 14, 14, 3, 1);
  2508. bench_case(1, 256, 512, 14, 14, 3, 1);
  2509. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2510. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2511. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  2512. std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
  2513. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2514. benchmark_impl_comp(
  2515. param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {4}},
  2516. {1, {4}}, data_type);
  2517. benchmark_impl_comp(
  2518. param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {7}},
  2519. {1, {7}}, data_type);
  2520. shapes_and_computation.clear();
  2521. }
  2522. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_6x16) {
  2523. constexpr size_t RUNS = 50;
  2524. param::ConvBias param;
  2525. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2526. param.pad_h = 1;
  2527. param.pad_w = 1;
  2528. param.stride_h = 1;
  2529. param.stride_w = 1;
  2530. std::vector<DType> data_type = {
  2531. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2532. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2533. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2534. size_t group) {
  2535. SmallVector<TensorShape> shapes{
  2536. {N, IC, H, W},
  2537. {OC / group, IC / group, FS, FS},
  2538. {1, OC, 1, 1},
  2539. {},
  2540. {N, OC, H, W}};
  2541. TensorShape dst{N, OC, H, W};
  2542. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2543. dst.total_nr_elems()) *
  2544. 1e-6;
  2545. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2546. };
  2547. bench_case(1, 32, 32, 200, 200, 3, 1);
  2548. bench_case(1, 32, 32, 200, 200, 3, 1);
  2549. bench_case(1, 32, 32, 128, 128, 3, 1);
  2550. bench_case(1, 32, 32, 128, 128, 3, 1);
  2551. bench_case(1, 32, 32, 100, 100, 3, 1);
  2552. bench_case(1, 32, 32, 100, 100, 3, 1);
  2553. bench_case(1, 32, 32, 80, 80, 3, 1);
  2554. bench_case(1, 32, 32, 80, 80, 3, 1);
  2555. bench_case(1, 64, 32, 7, 7, 3, 1);
  2556. bench_case(1, 64, 64, 7, 7, 3, 1);
  2557. bench_case(1, 64, 128, 7, 7, 3, 1);
  2558. bench_case(1, 64, 256, 7, 7, 3, 1);
  2559. bench_case(1, 64, 512, 7, 7, 3, 1);
  2560. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2561. bench_case(1, 64, 32, 14, 14, 3, 1);
  2562. bench_case(1, 64, 64, 14, 14, 3, 1);
  2563. bench_case(1, 64, 128, 14, 14, 3, 1);
  2564. bench_case(1, 64, 256, 14, 14, 3, 1);
  2565. bench_case(1, 64, 512, 14, 14, 3, 1);
  2566. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2567. bench_case(1, 128, 128, 14, 14, 3, 1);
  2568. bench_case(1, 128, 256, 14, 14, 3, 1);
  2569. bench_case(1, 512, 512, 14, 14, 3, 1);
  2570. bench_case(1, 256, 512, 14, 14, 3, 1);
  2571. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2572. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2573. std::string algo_name = "IM2COLMATMUL:X86_F32_6x16:192";
  2574. printf("Benchmark IM2COLMATMUL:X86_F32_6x16 algo\n");
  2575. benchmark_impl(
  2576. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2577. data_type);
  2578. benchmark_impl(
  2579. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2580. data_type);
  2581. benchmark_impl(
  2582. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2583. data_type);
  2584. shapes_and_computation.clear();
  2585. }
  2586. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_6X16_single_thread) {
  2587. constexpr size_t RUNS = 50;
  2588. param::ConvBias param;
  2589. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2590. param.pad_h = 1;
  2591. param.pad_w = 1;
  2592. param.stride_h = 1;
  2593. param.stride_w = 1;
  2594. std::vector<DType> data_type = {
  2595. dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
  2596. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2597. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2598. size_t group) {
  2599. SmallVector<TensorShape> shapes{
  2600. {N, IC, H, W},
  2601. {OC / group, IC / group, FS, FS},
  2602. {1, OC, 1, 1},
  2603. {},
  2604. {N, OC, H, W}};
  2605. TensorShape dst{N, OC, H, W};
  2606. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2607. dst.total_nr_elems()) *
  2608. 1e-6;
  2609. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2610. };
  2611. bench_case(1, 32, 32, 200, 200, 3, 1);
  2612. bench_case(1, 32, 32, 200, 200, 3, 1);
  2613. bench_case(1, 32, 32, 128, 128, 3, 1);
  2614. bench_case(1, 32, 32, 128, 128, 3, 1);
  2615. bench_case(1, 32, 32, 100, 100, 3, 1);
  2616. bench_case(1, 32, 32, 100, 100, 3, 1);
  2617. bench_case(1, 32, 32, 80, 80, 3, 1);
  2618. bench_case(1, 32, 32, 80, 80, 3, 1);
  2619. bench_case(1, 64, 32, 7, 7, 3, 1);
  2620. bench_case(1, 64, 64, 7, 7, 3, 1);
  2621. bench_case(1, 64, 128, 7, 7, 3, 1);
  2622. bench_case(1, 64, 256, 7, 7, 3, 1);
  2623. bench_case(1, 64, 512, 7, 7, 3, 1);
  2624. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2625. bench_case(1, 64, 32, 14, 14, 3, 1);
  2626. bench_case(1, 64, 64, 14, 14, 3, 1);
  2627. bench_case(1, 64, 128, 14, 14, 3, 1);
  2628. bench_case(1, 64, 256, 14, 14, 3, 1);
  2629. bench_case(1, 64, 512, 14, 14, 3, 1);
  2630. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2631. bench_case(1, 128, 128, 14, 14, 3, 1);
  2632. bench_case(1, 128, 256, 14, 14, 3, 1);
  2633. bench_case(1, 512, 512, 14, 14, 3, 1);
  2634. bench_case(1, 256, 512, 14, 14, 3, 1);
  2635. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2636. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2637. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  2638. std::string algo_name1 = "IM2COLMATMUL:X86_F32_6x16:192";
  2639. printf("Benchmark IM2COLMATMUL:X86_F32_6x16 algo\n");
  2640. benchmark_impl_comp(
  2641. param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {4}},
  2642. {1, {4}}, data_type);
  2643. benchmark_impl_comp(
  2644. param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {7}},
  2645. {1, {7}}, data_type);
  2646. shapes_and_computation.clear();
  2647. }
  2648. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
  2649. constexpr size_t RUNS = 50;
  2650. param::ConvBias param;
  2651. param.pad_h = 1;
  2652. param.pad_w = 1;
  2653. param.stride_h = 1;
  2654. param.stride_w = 1;
  2655. std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
  2656. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
  2657. size_t group) {
  2658. SmallVector<TensorShape> shapes{
  2659. {N, IC, H, W},
  2660. {OC / group, IC / group, FS, FS},
  2661. {1, OC, 1, 1},
  2662. {},
  2663. {N, OC, H, W}};
  2664. TensorShape dst{N, OC, H, W};
  2665. float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2666. dst.total_nr_elems()) *
  2667. 1e-6;
  2668. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2669. };
  2670. bench_case(1, 32, 32, 200, 200, 3, 1);
  2671. bench_case(1, 32, 32, 200, 200, 3, 1);
  2672. bench_case(1, 32, 32, 128, 128, 3, 1);
  2673. bench_case(1, 32, 32, 128, 128, 3, 1);
  2674. bench_case(1, 32, 32, 100, 100, 3, 1);
  2675. bench_case(1, 32, 32, 100, 100, 3, 1);
  2676. bench_case(1, 32, 32, 80, 80, 3, 1);
  2677. bench_case(1, 32, 32, 80, 80, 3, 1);
  2678. bench_case(1, 64, 32, 7, 7, 3, 1);
  2679. bench_case(1, 64, 64, 7, 7, 3, 1);
  2680. bench_case(1, 64, 128, 7, 7, 3, 1);
  2681. bench_case(1, 64, 256, 7, 7, 3, 1);
  2682. bench_case(1, 64, 512, 7, 7, 3, 1);
  2683. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2684. bench_case(1, 64, 32, 14, 14, 3, 1);
  2685. bench_case(1, 64, 64, 14, 14, 3, 1);
  2686. bench_case(1, 64, 128, 14, 14, 3, 1);
  2687. bench_case(1, 64, 256, 14, 14, 3, 1);
  2688. bench_case(1, 64, 512, 14, 14, 3, 1);
  2689. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2690. bench_case(1, 128, 128, 14, 14, 3, 1);
  2691. bench_case(1, 128, 256, 14, 14, 3, 1);
  2692. bench_case(1, 512, 512, 14, 14, 3, 1);
  2693. bench_case(1, 256, 512, 14, 14, 3, 1);
  2694. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2695. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2696. std::vector<DType> data_type = {
  2697. dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
  2698. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2:192";
  2699. // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
  2700. // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
  2701. benchmark_impl(
  2702. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
  2703. data_type);
  2704. benchmark_impl(
  2705. param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
  2706. data_type);
  2707. benchmark_impl(
  2708. param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
  2709. data_type);
  2710. shapes_and_computation.clear();
  2711. }
  2712. namespace {
  2713. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(
  2714. size_t kernel, size_t pack_size) {
  2715. std::vector<conv_bias::TestArg> args;
  2716. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p) {
  2717. if (ic % pack_size != 0 || oc % pack_size != 0)
  2718. return;
  2719. if (w + 2 * p < kernel || h + 2 * p < kernel)
  2720. return;
  2721. param::ConvBias param;
  2722. param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  2723. param.format = param::ConvBias::Format::NCHW88;
  2724. param.sparse = param::ConvBias::Sparse::DENSE;
  2725. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2726. param.stride_h = 1;
  2727. param.stride_w = 1;
  2728. param.pad_h = p;
  2729. param.pad_w = p;
  2730. args.push_back(conv_bias::TestArg{
  2731. param,
  2732. TensorShape{1, ic / 8, h, w, 8},
  2733. TensorShape{oc / 8, ic / 8, kernel, kernel, 8, 8},
  2734. {1, oc / 8, 1, 1, 8}});
  2735. };
  2736. for (size_t ic : {64, 128, 256}) {
  2737. for (size_t oc : {64, 128, 256}) {
  2738. pack(oc, ic, 56, 56, kernel, kernel / 2);
  2739. pack(oc, ic, 14, 14, kernel, kernel / 2);
  2740. pack(oc, ic, 28, 28, kernel, kernel / 2);
  2741. }
  2742. }
  2743. //! conv in vgg16
  2744. pack(512, 512, 15, 15, kernel, kernel / 2);
  2745. pack(512, 256, 15, 15, kernel, kernel / 2);
  2746. pack(256, 256, 29, 29, kernel, kernel / 2);
  2747. pack(256, 128, 29, 29, kernel, kernel / 2);
  2748. pack(128, 128, 57, 57, kernel, kernel / 2);
  2749. pack(128, 64, 57, 57, kernel, kernel / 2);
  2750. pack(64, 64, 56, 56, kernel, kernel / 2);
  2751. pack(128, 128, 28, 28, kernel, kernel / 2);
  2752. pack(512, 512, 14, 14, kernel, kernel / 2);
  2753. return args;
  2754. }
  2755. void benchmark_winograd(
  2756. const char* algo_name, Handle* handle, size_t kernel, size_t pack_size) {
  2757. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  2758. using namespace conv_bias;
  2759. constexpr size_t RUN = 10;
  2760. Benchmarker<ConvBias> benchmark(handle);
  2761. benchmark.set_display(false);
  2762. benchmark.set_times(RUN);
  2763. Benchmarker<ConvBias> benchmark_winograd(handle);
  2764. benchmark_winograd.set_display(false);
  2765. benchmark_winograd.set_times(RUN);
  2766. for (auto&& arg : args) {
  2767. TensorLayout dst_layout;
  2768. auto opr = handle->create_operator<ConvBias>();
  2769. opr->param() = arg.param;
  2770. opr->deduce_layout(
  2771. {arg.src, dtype::Float32()}, {arg.filter, dtype::Float32()},
  2772. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2773. //! dst.nr_elems * IC * FH * FW * 2
  2774. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2775. arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
  2776. (1024 * 1024 * 1024) * 1e3;
  2777. auto used =
  2778. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}, {}, {}}) /
  2779. RUN;
  2780. benchmark_winograd.set_param(arg.param);
  2781. auto used_winograd = algo_benchmark<ConvBias>(
  2782. benchmark_winograd,
  2783. {arg.src, arg.filter, {}, {}, {}}, algo_name) /
  2784. RUN;
  2785. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  2786. "speedup: "
  2787. "%f\n",
  2788. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used,
  2789. computations / used, used_winograd, computations / used_winograd,
  2790. used / used_winograd);
  2791. }
  2792. }
  2793. } // namespace
  2794. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
  2795. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
  2796. }
  2797. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  2798. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
  2799. }
  2800. #endif
  2801. } // namespace test
  2802. } // namespace megdnn
  2803. // vim: syntax=cpp.doxygen