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

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

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