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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  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 && SUPPORT_MKL_PACKED_GEMM
  814. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA) {
  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. param.sparse = param::ConvBias::Sparse::GROUP;
  839. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  840. TensorShape{2, oc, ic, kernel, kernel},
  841. TensorShape{});
  842. args.emplace_back(param, TensorShape{1, 2 * ic, h, w},
  843. TensorShape{2, oc, ic, kernel, kernel},
  844. TensorShape{1, oc * 2, 1, 1});
  845. args.emplace_back(
  846. param, TensorShape{1, 2 * ic, h, w},
  847. TensorShape{2, oc, ic, kernel, kernel},
  848. TensorShape{1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
  849. (w + 2 * param.pad_w - kernel) / 1 + 1});
  850. };
  851. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  852. for (size_t ic : {1, 4, 8, 16})
  853. for (size_t oc : {1, 4, 8, 16})
  854. for (size_t p : {0, 1})
  855. for (size_t size : {8, 24})
  856. for (NonlineMode nonline_mode :
  857. {NonlineMode::IDENTITY, NonlineMode::RELU}) {
  858. run(oc, ic, size, size, kernel, p, nonline_mode);
  859. }
  860. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  861. Checker<ConvBias> checker(handle());
  862. #define cb(algo_name) \
  863. checker.set_before_exec_callback( \
  864. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  865. for (auto&& arg : args) { \
  866. checker.set_param(arg.param).execs( \
  867. {arg.src, arg.filter, arg.bias, {}, {}}); \
  868. }
  869. cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");
  870. #undef cb
  871. }
  872. /**************************** Conv1x1 PackA *************************/
  873. namespace {
  874. void checker_conv_bias(std::vector<conv_bias::TestArg> args, Handle* handle,
  875. RNG* rng, float epsilon, DType type0, DType type1,
  876. DType type2, DType type3, const char* algo_name) {
  877. using namespace conv_bias;
  878. Checker<ConvBias> checker(handle);
  879. checker.set_before_exec_callback(
  880. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  881. checker.set_dtype(0, type0);
  882. checker.set_dtype(1, type1);
  883. checker.set_dtype(2, type2);
  884. checker.set_dtype(4, type3);
  885. checker.set_epsilon(epsilon);
  886. if (NULL != rng) {
  887. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  888. }
  889. for (auto&& arg : args) {
  890. checker.set_param(arg.param).execs(
  891. {arg.src, arg.filter, arg.bias, {}, {}});
  892. }
  893. }
  894. } // namespace
  895. #if MEGDNN_X86_WITH_MKL
  896. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA) {
  897. using namespace conv_bias;
  898. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  899. check_conv_bias(args, handle(), "CONV1x1:X86_F32_MKL_PACKA:24");
  900. }
  901. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS) {
  902. using namespace conv_bias;
  903. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
  904. check_conv_bias(args, handle(), "CONV1x1:X86_F32_BLAS:48");
  905. }
  906. #endif
  907. TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X) {
  908. using namespace conv_bias;
  909. UniformIntRNG rng{-50, 50};
  910. float epsilon = 0.001;
  911. std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(true, true);
  912. #if MEGDNN_X86_WITH_MKL_DNN
  913. if (x86::is_supported(x86::SIMDType::VNNI)) {
  914. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  915. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  916. "CONV1x1:X86_INT8X8X32_MKLDNN:24");
  917. }
  918. #endif
  919. #if MEGDNN_X86_WITH_VNNI
  920. if (x86::is_supported(x86::SIMDType::VNNI)) {
  921. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  922. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  923. "CONV1x1:X86_INT8X8X32_VNNI:24");
  924. }
  925. #endif
  926. if (x86::is_supported(x86::SIMDType::AVX2)) {
  927. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  928. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  929. "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
  930. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  931. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  932. "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
  933. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  934. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  935. "CONV1x1:X86_INT8X8X16_AVX2");
  936. }
  937. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  938. dtype::Int8{}, dtype::Int32{}, dtype::Int32{},
  939. "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
  940. checker_conv_bias(args, handle(), &rng, epsilon, dtype::Int8{},
  941. dtype::Int8{}, dtype::Int16{}, dtype::Int16{},
  942. "CONV1x1:X86_INT8X8X16_SSE");
  943. }
  944. /************************* End Conv1x1 PackA ************************/
  945. #endif
  946. TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8) {
  947. using namespace conv_bias;
  948. std::vector<TestArg> args;
  949. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  950. size_t p, NonlineMode nonline_mode) {
  951. if (w + 2 * p < kernel || h + 2 * p < kernel)
  952. return;
  953. param::ConvBias param;
  954. param.stride_h = 1;
  955. param.stride_w = 1;
  956. param.pad_h = p;
  957. param.pad_w = p;
  958. param.nonlineMode = nonline_mode;
  959. //! no bias
  960. args.emplace_back(param, TensorShape{1, ic, h, w},
  961. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  962. //! bias channel
  963. args.emplace_back(param, TensorShape{2, ic, h, w},
  964. TensorShape{oc, ic, kernel, kernel},
  965. TensorShape{1, oc, 1, 1});
  966. };
  967. for (size_t kernel : {2, 3, 4, 5, 6, 7})
  968. for (size_t ic : {1, 4, 8, 16})
  969. for (size_t oc : {1, 4, 8})
  970. for (size_t p : {0, 2})
  971. for (size_t size : {20, 21, 24})
  972. for (NonlineMode nonline_mode :
  973. {NonlineMode::IDENTITY, NonlineMode::RELU,
  974. NonlineMode::H_SWISH}) {
  975. run(oc, ic, size, size, kernel, p, nonline_mode);
  976. }
  977. run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
  978. Checker<ConvBias> checker(handle());
  979. #define cb(algo_name) \
  980. checker.set_before_exec_callback( \
  981. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
  982. UniformIntRNG rng{-50, 50}; \
  983. for (auto&& arg : args) { \
  984. checker.set_dtype(0, dtype::QuantizedS8(2.5f)) \
  985. .set_dtype(1, dtype::QuantizedS8(2.5f)) \
  986. .set_dtype(2, dtype::QuantizedS32(6.25f)) \
  987. .set_dtype(4, dtype::QuantizedS8(60.25)) \
  988. .set_rng(0, &rng) \
  989. .set_rng(1, &rng) \
  990. .set_rng(2, &rng) \
  991. .set_param(arg.param) \
  992. .execs({arg.src, arg.filter, {}, {}, {}}); \
  993. }
  994. #if MEGDNN_X86_WITH_MKL_DNN
  995. if (x86::is_supported(x86::SIMDType::VNNI)) {
  996. cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
  997. }
  998. #endif
  999. #if MEGDNN_X86_WITH_VNNI
  1000. if (x86::is_supported(x86::SIMDType::VNNI)) {
  1001. cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
  1002. }
  1003. #endif
  1004. if (x86::is_supported(x86::SIMDType::AVX2)) {
  1005. cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
  1006. }
  1007. #undef cb
  1008. }
  1009. TEST_F(X86, CONV_BIAS_MATMUL) {
  1010. using namespace conv_bias;
  1011. std::vector<TestArg> args;
  1012. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1013. size_t p, NonlineMode nonline_mode) {
  1014. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1015. return;
  1016. param::ConvBias param;
  1017. param.stride_h = 1;
  1018. param.stride_w = 1;
  1019. param.pad_h = p;
  1020. param.pad_w = p;
  1021. param.nonlineMode = nonline_mode;
  1022. //! no bias
  1023. param.sparse = param::ConvBias::Sparse::DENSE;
  1024. args.emplace_back(param, TensorShape{1, ic, h, w},
  1025. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1026. //! bias channel
  1027. args.emplace_back(param, TensorShape{2, ic, h, w},
  1028. TensorShape{oc, ic, kernel, kernel},
  1029. TensorShape{1, oc, 1, 1});
  1030. //! bias
  1031. args.emplace_back(param, TensorShape{2, ic, h, w},
  1032. TensorShape{oc, ic, kernel, kernel},
  1033. TensorShape{2, oc, (h + param.pad_h * 2 - kernel) + 1,
  1034. (w + param.pad_w * 2 - kernel) + 1});
  1035. //! gruop
  1036. param.sparse = param::ConvBias::Sparse::GROUP;
  1037. args.emplace_back(
  1038. param, TensorShape{2, 2 * ic, h, w},
  1039. TensorShape{2, oc, ic, kernel, kernel},
  1040. TensorShape{2, 2 * oc, (h + param.pad_h * 2 - kernel) + 1,
  1041. (w + param.pad_w * 2 - kernel) + 1});
  1042. };
  1043. for (size_t kernel : {2, 3, 5, 7})
  1044. for (size_t ic : {1, 2, 3, 4})
  1045. for (size_t oc : {1, 2, 3, 4})
  1046. for (size_t p : {0, 2})
  1047. for (size_t size : {20, 21, 22, 23, 24})
  1048. for (NonlineMode nonline_mode :
  1049. {NonlineMode::RELU, NonlineMode::SIGMOID,
  1050. NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
  1051. run(oc, ic, size, size, kernel, p, nonline_mode);
  1052. }
  1053. Checker<ConvBias> checker(handle());
  1054. checker.set_before_exec_callback(
  1055. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1056. "X86_CONV_BIAS_MATMUL"));
  1057. checker.set_epsilon(1);
  1058. UniformIntRNG rng{-50, 50};
  1059. checker.set_dtype(0, dtype::Float32())
  1060. .set_dtype(1, dtype::Float32())
  1061. .set_dtype(2, dtype::Float32())
  1062. .set_rng(0, &rng)
  1063. .set_rng(1, &rng)
  1064. .set_rng(2, &rng);
  1065. for (auto&& arg : args) {
  1066. checker.set_param(arg.param).exec(
  1067. {arg.src, arg.filter, arg.bias, {}, {}});
  1068. }
  1069. }
  1070. #if MEGDNN_WITH_BENCHMARK
  1071. #if MEGDNN_X86_WITH_MKL_DNN
  1072. static void x86_benchmark_fp32_mkldnn(Handle* handle) {
  1073. constexpr size_t RUNS = 30;
  1074. param::ConvBias param;
  1075. Benchmarker<ConvBias> benchmarker_mkldnn(handle);
  1076. benchmarker_mkldnn.set_display(false).set_times(RUNS);
  1077. benchmarker_mkldnn.set_before_exec_callback(
  1078. AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));
  1079. Benchmarker<ConvBias> benchmarker_im2col(handle);
  1080. benchmarker_im2col.set_display(false).set_times(RUNS);
  1081. benchmarker_im2col.set_before_exec_callback(
  1082. AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
  1083. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1084. size_t FS, size_t SZ, size_t GROUP = 1) {
  1085. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  1086. bias({1, OC, 1, 1}), z({}), dst({N, OC, H / SZ, W / SZ});
  1087. param.pad_h = FS / 2;
  1088. param.pad_w = FS / 2;
  1089. param.stride_h = SZ;
  1090. param.stride_w = SZ;
  1091. param.format = param::ConvBias::Format::NCHW;
  1092. param.sparse = param::ConvBias::Sparse::DENSE;
  1093. if (GROUP > 1) {
  1094. param.sparse = param::ConvBias::Sparse::GROUP;
  1095. filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
  1096. }
  1097. auto im2col_used = benchmarker_im2col.set_param(param).exec(
  1098. {src, filter, bias, z, dst}) /
  1099. RUNS;
  1100. src = IC < 8 ? TensorShape{N, IC, H, W}
  1101. : TensorShape{N, IC / 8, H, W, 8};
  1102. filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
  1103. : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
  1104. if (GROUP > 1 && OC == GROUP && IC == GROUP) {
  1105. filter = {GROUP / 8, 1, 1, FS, FS, 8};
  1106. } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
  1107. filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
  1108. }
  1109. bias = {1, OC / 8, 1, 1, 8};
  1110. z = {};
  1111. dst = {N, OC / 8, H / SZ, W / SZ, 8};
  1112. param.format = param::ConvBias::Format::NCHW88;
  1113. auto mkldnn_used = benchmarker_mkldnn.set_param(param).exec(
  1114. {src, filter, bias, z, dst}) /
  1115. RUNS;
  1116. float computations =
  1117. (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  1118. std::cout << "run " << src.to_string() << " " << filter.to_string()
  1119. << " " << bias.to_string() << " " << dst.to_string()
  1120. << std::endl;
  1121. std::cout << "im2col: " << im2col_used << " ms, "
  1122. << (computations / im2col_used) << " Gops, ";
  1123. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  1124. << (computations / mkldnn_used) << " Gops, "
  1125. << "spped up: " << (im2col_used / mkldnn_used) << ", ";
  1126. std::cout << std::endl;
  1127. };
  1128. run(1, 64, 64, 56, 56, 3, 1);
  1129. run(1, 3, 64, 224, 224, 3, 1);
  1130. run(1, 3, 64, 224, 224, 7, 2);
  1131. run(1, 64, 64, 56, 56, 3, 1);
  1132. run(1, 128, 128, 28, 28, 3, 1);
  1133. run(1, 256, 256, 14, 14, 3, 1);
  1134. run(1, 512, 512, 7, 7, 3, 1);
  1135. run(1, 256, 64, 56, 56, 1, 1);
  1136. run(1, 512, 128, 28, 28, 1, 1);
  1137. run(1, 1024, 256, 14, 14, 1, 1);
  1138. run(1, 2048, 512, 7, 7, 1, 1);
  1139. run(1, 32, 32, 112, 112, 3, 1, 32);
  1140. run(1, 144, 144, 56, 56, 3, 1, 144);
  1141. run(1, 192, 192, 28, 28, 3, 1, 192);
  1142. run(1, 384, 384, 28, 28, 3, 1, 384);
  1143. run(1, 576, 576, 14, 14, 3, 1, 576);
  1144. run(1, 960, 960, 7, 7, 3, 1, 960);
  1145. run(1, 256, 128, 56, 56, 1, 2, 1);
  1146. run(1, 512, 256, 28, 28, 1, 2, 1);
  1147. run(1, 1024, 512, 14, 14, 1, 2, 1);
  1148. run(1, 96, 96, 112, 112, 3, 2, 96);
  1149. run(1, 144, 144, 56, 56, 3, 2, 144);
  1150. run(1, 384, 384, 28, 28, 3, 2, 384);
  1151. run(1, 576, 576, 14, 14, 3, 2, 576);
  1152. }
  1153. TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1154. x86_benchmark_fp32_mkldnn(handle());
  1155. }
  1156. TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
  1157. x86_benchmark_fp32_mkldnn(handle());
  1158. }
  1159. #endif
  1160. #endif
  1161. /************************* Winograd ****************************/
  1162. namespace {
  1163. std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
  1164. std::vector<conv_bias::TestArg> args;
  1165. param::ConvBias cur_param;
  1166. cur_param.format = param::ConvBias::Format::NCHW88;
  1167. using NLMode = param::ConvBias::NonlineMode;
  1168. // clang-format off
  1169. for (auto nlmode :
  1170. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  1171. for (size_t ic : {1, 2}) {
  1172. for (size_t oc : {1, 2}) {
  1173. for (size_t i : {9, 63}) {
  1174. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  1175. cur_param.nonlineMode = nlmode;
  1176. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1177. cur_param.pad_h = cur_param.pad_w = 1;
  1178. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1179. TensorShape{oc, ic, 3, 3, 8, 8},
  1180. TensorShape{1, oc, 1, 1, 8});
  1181. args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
  1182. TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
  1183. //! bias
  1184. args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
  1185. TensorShape{oc, ic, 3, 3, 8, 8}, TensorShape{2, oc, i, i, 8});
  1186. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1187. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
  1188. TensorShape{2, oc, ic, 3, 3, 8, 8},
  1189. TensorShape{1, 2 * oc, 1, 1, 8});*/
  1190. }}}
  1191. // clang-format on
  1192. //! test for multi-thread OC parallel
  1193. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  1194. cur_param.pad_h = cur_param.pad_w = 1;
  1195. args.emplace_back(cur_param, TensorShape{2, 1, 9, 9, 8},
  1196. TensorShape{128, 1, 3, 3, 8, 8},
  1197. TensorShape{1, 128, 1, 1, 8});
  1198. /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
  1199. args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
  1200. TensorShape{2, 128, 1, 3, 3, 8, 8},
  1201. TensorShape{1, 2 * 128, 1, 1, 8});*/
  1202. }
  1203. return args;
  1204. }
  1205. } // namespace
  1206. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
  1207. using namespace conv_bias;
  1208. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1209. Checker<ConvBiasForward> checker(handle());
  1210. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1211. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));
  1212. for (auto&& arg : args) {
  1213. checker.set_param(arg.param).execs(
  1214. {arg.src, arg.filter, arg.bias, {}, {}});
  1215. }
  1216. }
  1217. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
  1218. using namespace conv_bias;
  1219. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1220. Checker<ConvBiasForward> checker(handle());
  1221. checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1222. ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));
  1223. for (auto&& arg : args) {
  1224. checker.set_param(arg.param).execs(
  1225. {arg.src, arg.filter, arg.bias, {}, {}});
  1226. }
  1227. }
  1228. TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
  1229. using namespace conv_bias;
  1230. std::vector<TestArg> args = get_winograd_mk_nchw88_args();
  1231. Checker<ConvBiasForward> checker(handle());
  1232. auto extra_impl = [](const TensorNDArray& tensors, uint32_t m,
  1233. param::ConvBias param, Handle* handle) {
  1234. megdnn_assert(param.format == param::ConvBias::Format::NCHW88);
  1235. auto winograd_preprocess_opr =
  1236. handle->create_operator<WinogradFilterPreprocess>();
  1237. winograd_preprocess_opr->param().output_block_size = m;
  1238. winograd_preprocess_opr->param().format = param::MatrixMul::Format::MK8;
  1239. TensorLayout filter_transform_layout;
  1240. winograd_preprocess_opr->deduce_layout(tensors[1].layout,
  1241. filter_transform_layout);
  1242. size_t winograd_preprocess_workspace_in_bytes =
  1243. winograd_preprocess_opr->get_workspace_in_bytes(
  1244. tensors[1].layout, filter_transform_layout);
  1245. auto conv_bias_opr = handle->create_operator<ConvBias>();
  1246. conv_bias_opr->param() = param;
  1247. conv_bias_opr->param().format =
  1248. param::ConvBias::Format::NCHW88_WINOGRAD;
  1249. conv_bias_opr->param().output_block_size = m;
  1250. size_t conv_bias_workspace_in_bytes =
  1251. conv_bias_opr->get_workspace_in_bytes(
  1252. tensors[0].layout, filter_transform_layout,
  1253. tensors[2].layout, tensors[3].layout, tensors[4].layout,
  1254. nullptr);
  1255. WorkspaceBundle wb(nullptr, {filter_transform_layout.span().dist_byte(),
  1256. conv_bias_workspace_in_bytes,
  1257. winograd_preprocess_workspace_in_bytes});
  1258. wb.set(malloc(wb.total_size_in_bytes()));
  1259. TensorND filter_transform_tensor(wb.get(0),
  1260. std::move(filter_transform_layout));
  1261. winograd_preprocess_opr->exec(tensors[1], filter_transform_tensor,
  1262. wb.get_workspace(2));
  1263. conv_bias_opr->exec(tensors[0], filter_transform_tensor, tensors[2],
  1264. tensors[3], tensors[4], nullptr,
  1265. wb.get_workspace(1));
  1266. free(wb.ptr());
  1267. };
  1268. auto run = [&checker, &extra_impl](
  1269. Handle* handle, const std::vector<TestArg>& args,
  1270. const std::vector<size_t>& out_size, DType A_dtype,
  1271. DType B_dtype, DType C_dtype, DType D_dtype,
  1272. const float eps) {
  1273. for (auto&& arg : args) {
  1274. for (uint32_t m : out_size) {
  1275. checker.set_extra_opr_impl(std::bind(extra_impl,
  1276. std::placeholders::_1, m,
  1277. arg.param, handle));
  1278. checker.set_dtype(0, A_dtype)
  1279. .set_dtype(1, B_dtype)
  1280. .set_dtype(2, C_dtype)
  1281. .set_dtype(4, D_dtype)
  1282. .set_epsilon(eps)
  1283. .set_param(arg.param)
  1284. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  1285. }
  1286. }
  1287. };
  1288. run(handle(), args, {2, 6}, dtype::Float32(), dtype::Float32(),
  1289. dtype::Float32(), dtype::Float32(), 1e-3f);
  1290. }
  1291. /*********************************** End winograd ************************/
  1292. #if MEGDNN_X86_WITH_MKL_DNN
  1293. static void x86_correctness_fp32_mkldnn_run(
  1294. Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
  1295. ConvBiasForward::BiasMode bias_mode,
  1296. param::ConvBias::NonlineMode noline_mode, size_t n, size_t stride,
  1297. size_t kernel, size_t oc, size_t ic, size_t h, size_t w, size_t group) {
  1298. auto oc_per_group = oc / group;
  1299. auto ic_per_group = ic / group;
  1300. bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
  1301. (ic_per_group % 8 == 0 || ic_per_group == 3) &&
  1302. ic_per_group > 0;
  1303. bool ok_depthwise = oc == ic && oc == group;
  1304. if (!(ok_group || ok_depthwise)) {
  1305. return;
  1306. }
  1307. size_t pad = kernel / 2;
  1308. size_t kernel_h = kernel;
  1309. size_t kernel_w = kernel;
  1310. param::ConvBias param;
  1311. param.format = param::ConvBias::Format::NCHW88;
  1312. param.stride_h = stride;
  1313. param.stride_w = stride;
  1314. param.pad_h = pad;
  1315. param.pad_w = pad;
  1316. param.nonlineMode = noline_mode;
  1317. auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
  1318. if (ic == 3) {
  1319. src_tensor_shape = TensorShape{n, ic, h, w};
  1320. }
  1321. auto weight_tensor_shape =
  1322. TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
  1323. if (ic == 3) {
  1324. weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
  1325. }
  1326. auto bias_tensor_shape = TensorShape{};
  1327. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1328. bias_tensor_shape = {1, oc / 8, 1, 1, 8};
  1329. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1330. TensorLayout dst_layout;
  1331. auto ConvBiasOp = handle->create_operator<ConvBias>();
  1332. ConvBiasOp->param() = param;
  1333. ConvBiasOp->deduce_layout({src_tensor_shape, dtype::Float32()},
  1334. {weight_tensor_shape, dtype::Float32()}, {},
  1335. {}, dst_layout);
  1336. bias_tensor_shape = dst_layout;
  1337. }
  1338. if (group == 1) {
  1339. param.sparse = param::ConvBias::Sparse::DENSE;
  1340. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1341. param.sparse = param::ConvBias::Sparse::GROUP;
  1342. weight_tensor_shape =
  1343. TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
  1344. } else if (group > 1 && oc / group % 8 == 0 && oc / group > 0 &&
  1345. ic / group % 8 == 0 && ic / group > 0) {
  1346. param.sparse = param::ConvBias::Sparse::GROUP;
  1347. weight_tensor_shape = TensorShape{
  1348. group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8,
  1349. 8};
  1350. }
  1351. checker.set_dtype(0, dtype::Float32())
  1352. .set_dtype(1, dtype::Float32())
  1353. .set_dtype(2, dtype::Float32())
  1354. .set_dtype(4, dtype::Float32())
  1355. .set_rng(0, &rng)
  1356. .set_rng(1, &rng)
  1357. .set_rng(2, &rng)
  1358. .set_epsilon(1e-3)
  1359. .set_param(param)
  1360. .execs({src_tensor_shape,
  1361. weight_tensor_shape,
  1362. bias_tensor_shape,
  1363. {},
  1364. {}});
  1365. }
  1366. static void x86_correctness_fp32_mkldnn(Handle* handle) {
  1367. Checker<ConvBias> checker(handle);
  1368. UniformIntRNG rng{-127, 127};
  1369. checker.set_before_exec_callback(
  1370. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1371. "MKLDNN_CONV_FP32"));
  1372. for (auto bias_mode :
  1373. {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
  1374. megdnn::BiasMode::BIAS})
  1375. for (auto noline_mode : {param::ConvBias::NonlineMode::IDENTITY,
  1376. param::ConvBias::NonlineMode::SIGMOID,
  1377. param::ConvBias::NonlineMode::H_SWISH})
  1378. for (size_t n : {1, 2})
  1379. for (size_t stride : {1, 2})
  1380. for (size_t kernel : {3, 5, 7})
  1381. for (size_t oc : {8, 16})
  1382. for (size_t ic : {3, 8, 16})
  1383. for (size_t h : {22, 33})
  1384. for (size_t w : {22, 33}) {
  1385. for (size_t group = 1;
  1386. group <= std::min(oc, ic);
  1387. ++group) {
  1388. x86_correctness_fp32_mkldnn_run(
  1389. checker, rng, handle,
  1390. bias_mode, noline_mode, n,
  1391. stride, kernel, oc, ic, h,
  1392. w, group);
  1393. }
  1394. }
  1395. }
  1396. TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1397. x86_correctness_fp32_mkldnn(handle());
  1398. }
  1399. TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
  1400. x86_correctness_fp32_mkldnn(handle());
  1401. }
  1402. TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
  1403. using namespace conv_bias;
  1404. std::vector<TestArg> args;
  1405. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1406. size_t p, NonlineMode nonline_mode) {
  1407. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1408. return;
  1409. param::ConvBias param;
  1410. param.stride_h = 1;
  1411. param.stride_w = 1;
  1412. param.pad_h = p;
  1413. param.pad_w = p;
  1414. param.nonlineMode = nonline_mode;
  1415. //! no bias
  1416. args.emplace_back(param, TensorShape{1, ic, h, w},
  1417. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1418. };
  1419. for (size_t kernel : {2, 3, 5, 7})
  1420. for (size_t ic : {1, 2, 3, 4})
  1421. for (size_t oc : {1, 2, 4})
  1422. for (size_t p : {0, 2})
  1423. for (size_t size : {20, 21, 22, 23, 24})
  1424. for (NonlineMode nonline_mode :
  1425. {NonlineMode::IDENTITY}) {
  1426. run(oc, ic, size, size, kernel, p, nonline_mode);
  1427. }
  1428. Checker<ConvBias> checker(handle());
  1429. checker.set_before_exec_callback(
  1430. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1431. "MKLDNN_MATMUL_INT8"));
  1432. checker.set_epsilon(1);
  1433. UniformIntRNG rng{-50, 50};
  1434. checker.set_dtype(0, dtype::Int8())
  1435. .set_dtype(1, dtype::Int8())
  1436. .set_dtype(2, dtype::Int32())
  1437. .set_dtype(4, dtype::Int32())
  1438. .set_rng(0, &rng)
  1439. .set_rng(1, &rng)
  1440. .set_rng(2, &rng);
  1441. for (auto&& arg : args) {
  1442. checker.set_param(arg.param).exec(
  1443. {arg.src, arg.filter, arg.bias, {}, {}});
  1444. }
  1445. }
  1446. TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
  1447. using namespace conv_bias;
  1448. std::vector<TestArg> args;
  1449. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1450. size_t p, NonlineMode nonline_mode) {
  1451. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1452. return;
  1453. param::ConvBias param;
  1454. param.stride_h = 1;
  1455. param.stride_w = 1;
  1456. param.pad_h = p;
  1457. param.pad_w = p;
  1458. param.nonlineMode = nonline_mode;
  1459. //! no bias
  1460. args.emplace_back(param, TensorShape{1, ic, h, w},
  1461. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1462. };
  1463. for (size_t kernel : {2, 3, 5, 7})
  1464. for (size_t ic : {1, 2, 3, 4})
  1465. for (size_t oc : {1, 2, 4})
  1466. for (size_t p : {0, 2})
  1467. for (size_t size : {20, 22, 24})
  1468. for (NonlineMode nonline_mode :
  1469. {NonlineMode::IDENTITY}) {
  1470. run(oc, ic, size, size, kernel, p, nonline_mode);
  1471. }
  1472. Checker<ConvBias> checker(handle());
  1473. checker.set_before_exec_callback(
  1474. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1475. checker.set_epsilon(1);
  1476. UniformIntRNG rng{-50, 50};
  1477. checker.set_dtype(0, dtype::Int8())
  1478. .set_dtype(1, dtype::Int8())
  1479. .set_dtype(2, dtype::Int32())
  1480. .set_dtype(4, dtype::Int32())
  1481. .set_rng(0, &rng)
  1482. .set_rng(1, &rng)
  1483. .set_rng(2, &rng);
  1484. for (auto&& arg : args) {
  1485. checker.set_param(arg.param).exec(
  1486. {arg.src, arg.filter, arg.bias, {}, {}});
  1487. }
  1488. }
  1489. TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
  1490. using namespace conv_bias;
  1491. std::vector<TestArg> args;
  1492. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1493. size_t p, NonlineMode nonline_mode) {
  1494. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1495. return;
  1496. param::ConvBias param;
  1497. param.stride_h = 1;
  1498. param.stride_w = 1;
  1499. param.pad_h = p;
  1500. param.pad_w = p;
  1501. param.nonlineMode = nonline_mode;
  1502. //! no bias
  1503. args.emplace_back(param, TensorShape{1, ic, h, w},
  1504. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  1505. };
  1506. for (size_t kernel : {2, 3, 5, 7})
  1507. for (size_t ic : {1, 2, 3, 4})
  1508. for (size_t oc : {1, 2, 4})
  1509. for (size_t p : {0, 2})
  1510. for (size_t size : {20, 22, 24})
  1511. for (NonlineMode nonline_mode :
  1512. {NonlineMode::IDENTITY}) {
  1513. run(oc, ic, size, size, kernel, p, nonline_mode);
  1514. }
  1515. Checker<ConvBias> checker(handle());
  1516. checker.set_before_exec_callback(
  1517. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
  1518. checker.set_epsilon(1);
  1519. UniformIntRNG rng{-50, 50};
  1520. checker.set_dtype(0, dtype::Int8())
  1521. .set_dtype(1, dtype::Int8())
  1522. .set_dtype(2, dtype::Int32())
  1523. .set_dtype(4, dtype::Int32())
  1524. .set_rng(0, &rng)
  1525. .set_rng(1, &rng)
  1526. .set_rng(2, &rng);
  1527. for (auto&& arg : args) {
  1528. checker.set_param(arg.param).exec(
  1529. {arg.src, arg.filter, arg.bias, {}, {}});
  1530. }
  1531. }
  1532. #endif
  1533. #if MEGDNN_WITH_BENCHMARK
  1534. namespace {
  1535. void benchmark_impl(const param::ConvBias param,
  1536. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1537. shapes_and_computation,
  1538. const std::string algo_name, size_t RUNS,
  1539. TaskExecutorConfig&& multi_thread_config,
  1540. TaskExecutorConfig&& single_thread_config,
  1541. std::vector<DType> dtype_v) {
  1542. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1543. dtype::Float32(), dtype::Float32()};
  1544. std::vector<float> multi_thread_times, single_thread_times;
  1545. {
  1546. auto multi_thread_hanle =
  1547. create_cpu_handle(0, true, &multi_thread_config);
  1548. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1549. benchmarker.set_times(RUNS)
  1550. .set_display(false)
  1551. .set_dtype(0, dtype_v[0])
  1552. .set_dtype(1, dtype_v[1])
  1553. .set_dtype(2, dtype_v[2])
  1554. .set_dtype(4, dtype_v[3])
  1555. .set_param(param)
  1556. .set_before_exec_callback(
  1557. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1558. algo_name.c_str()));
  1559. for (auto shape : shapes_and_computation) {
  1560. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1561. }
  1562. }
  1563. {
  1564. auto single_thread_handle =
  1565. create_cpu_handle(0, true, &single_thread_config);
  1566. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1567. benchmarker.set_times(RUNS)
  1568. .set_display(false)
  1569. .set_dtype(0, dtype_v[0])
  1570. .set_dtype(1, dtype_v[1])
  1571. .set_dtype(2, dtype_v[2])
  1572. .set_dtype(4, dtype_v[3])
  1573. .set_param(param)
  1574. .set_before_exec_callback(
  1575. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1576. algo_name.c_str()));
  1577. for (auto shape : shapes_and_computation) {
  1578. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1579. }
  1580. }
  1581. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  1582. printf("core_ids:");
  1583. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  1584. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  1585. }
  1586. printf(", Single thread core_id %zu\n",
  1587. single_thread_config.affinity_core_set[0]);
  1588. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  1589. auto shapes = shapes_and_computation[i];
  1590. printf("Bench case: ");
  1591. for (auto&& shape : shapes.first) {
  1592. printf("%s ", shape.to_string().c_str());
  1593. }
  1594. float computations = shapes.second;
  1595. printf("%zu threads gflops: %f,\n single thread gflops: "
  1596. "%f. spead up = %f, speedup/cores=%f\n",
  1597. multi_thread_config.nr_thread,
  1598. computations / multi_thread_times[i],
  1599. computations / single_thread_times[i],
  1600. single_thread_times[i] / multi_thread_times[i],
  1601. single_thread_times[i] / multi_thread_times[i] /
  1602. multi_thread_config.nr_thread);
  1603. }
  1604. }
  1605. void benchmark_impl_comp(
  1606. const param::ConvBias param,
  1607. std::vector<std::pair<SmallVector<TensorShape>, float>>&
  1608. shapes_and_computation,
  1609. const std::string algo_name, const std::string algo_name1, size_t RUNS,
  1610. TaskExecutorConfig&& multi_thread_config,
  1611. TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
  1612. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1613. dtype::Float32(), dtype::Float32()};
  1614. std::vector<float> multi_thread_times, single_thread_times;
  1615. {
  1616. auto multi_thread_hanle =
  1617. create_cpu_handle(0, true, &multi_thread_config);
  1618. auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
  1619. benchmarker.set_times(RUNS)
  1620. .set_display(false)
  1621. .set_dtype(0, dtype_v[0])
  1622. .set_dtype(1, dtype_v[1])
  1623. .set_dtype(2, dtype_v[2])
  1624. .set_dtype(4, dtype_v[3])
  1625. .set_param(param)
  1626. .set_before_exec_callback(
  1627. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1628. algo_name.c_str()));
  1629. for (auto shape : shapes_and_computation) {
  1630. multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1631. }
  1632. }
  1633. {
  1634. auto single_thread_handle =
  1635. create_cpu_handle(0, true, &single_thread_config);
  1636. auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
  1637. benchmarker.set_times(RUNS)
  1638. .set_display(false)
  1639. .set_dtype(0, dtype_v[0])
  1640. .set_dtype(1, dtype_v[1])
  1641. .set_dtype(2, dtype_v[2])
  1642. .set_dtype(4, dtype_v[3])
  1643. .set_param(param)
  1644. .set_before_exec_callback(
  1645. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1646. algo_name1.c_str()));
  1647. for (auto shape : shapes_and_computation) {
  1648. single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
  1649. }
  1650. }
  1651. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  1652. printf("core_ids:");
  1653. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  1654. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  1655. }
  1656. for (size_t i = 0; i < shapes_and_computation.size(); i++) {
  1657. auto shapes = shapes_and_computation[i];
  1658. printf("Bench case: ");
  1659. for (auto&& shape : shapes.first) {
  1660. printf("%s ", shape.to_string().c_str());
  1661. }
  1662. float computations = shapes.second;
  1663. printf("algo:%s gflops: %f,\n algo:%s gflops: "
  1664. "%f. spead up = %f\n",
  1665. algo_name.c_str(), computations / multi_thread_times[i],
  1666. algo_name1.c_str(), computations / single_thread_times[i],
  1667. single_thread_times[i] / multi_thread_times[i]);
  1668. }
  1669. }
  1670. } // namespace
  1671. static void benchmark_convbias_chanwise_avx2_int8(uint32_t stride,
  1672. const char* algo) {
  1673. constexpr size_t RUNS = 50;
  1674. param::ConvBias param;
  1675. param.stride_h = stride;
  1676. param.stride_w = stride;
  1677. param.sparse = param::ConvBias::Sparse::GROUP;
  1678. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1679. dtype::Int32(), dtype::Int32()};
  1680. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1681. shapes_and_computation;
  1682. auto bench_case = [&](size_t N, size_t IC, size_t H, size_t W, size_t FS) {
  1683. param.pad_h = FS / 2;
  1684. param.pad_w = FS / 2;
  1685. SmallVector<TensorShape> shapes{
  1686. {N, IC, H, W}, {IC, 1, 1, FS, FS}, {}, {}, {}};
  1687. TensorShape dst{N, IC, (H + 2 * param.pad_h - FS) + 1,
  1688. (W + 2 * param.pad_w - FS) + 1};
  1689. float computations = (FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1690. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1691. };
  1692. bench_case(1, 32, 112, 112, 7);
  1693. bench_case(1, 144, 56, 56, 7);
  1694. bench_case(1, 192, 28, 28, 7);
  1695. bench_case(1, 384, 28, 28, 7);
  1696. bench_case(1, 576, 14, 14, 7);
  1697. bench_case(1, 960, 7, 7, 7);
  1698. bench_case(1, 32, 112, 112, 5);
  1699. bench_case(1, 144, 56, 56, 5);
  1700. bench_case(1, 192, 28, 28, 5);
  1701. bench_case(1, 384, 28, 28, 5);
  1702. bench_case(1, 576, 14, 14, 5);
  1703. bench_case(1, 960, 7, 7, 5);
  1704. bench_case(1, 32, 112, 112, 3);
  1705. bench_case(1, 144, 56, 56, 3);
  1706. bench_case(1, 192, 28, 28, 3);
  1707. bench_case(1, 384, 28, 28, 3);
  1708. bench_case(1, 576, 14, 14, 3);
  1709. bench_case(1, 960, 7, 7, 3);
  1710. bench_case(1, 32, 112, 112, 2);
  1711. bench_case(1, 144, 56, 56, 2);
  1712. bench_case(1, 192, 28, 28, 2);
  1713. bench_case(1, 384, 28, 28, 2);
  1714. bench_case(1, 576, 14, 14, 2);
  1715. bench_case(1, 960, 7, 7, 2);
  1716. std::string algo_name = algo;
  1717. printf("Benchmark %s\n", algo);
  1718. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1719. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1720. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1721. {1, {4}}, data_type);
  1722. shapes_and_computation.clear();
  1723. }
  1724. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S1) {
  1725. benchmark_convbias_chanwise_avx2_int8(
  1726. 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
  1727. }
  1728. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S2) {
  1729. benchmark_convbias_chanwise_avx2_int8(
  1730. 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
  1731. }
  1732. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
  1733. constexpr size_t RUNS = 50;
  1734. param::ConvBias param;
  1735. param.stride_h = 1;
  1736. param.stride_w = 1;
  1737. param.sparse = param::ConvBias::Sparse::DENSE;
  1738. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1739. dtype::Int32(), dtype::Int32()};
  1740. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1741. shapes_and_computation;
  1742. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1743. size_t FS) {
  1744. param.pad_h = FS / 2;
  1745. param.pad_w = FS / 2;
  1746. SmallVector<TensorShape> shapes{
  1747. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  1748. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) + 1,
  1749. (W + 2 * param.pad_w - FS) + 1};
  1750. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1751. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1752. };
  1753. bench_case(1, 32, 32, 200, 200, 7);
  1754. bench_case(1, 32, 64, 200, 200, 7);
  1755. bench_case(1, 32, 32, 128, 128, 7);
  1756. bench_case(1, 32, 64, 128, 128, 7);
  1757. bench_case(1, 32, 32, 100, 100, 7);
  1758. bench_case(1, 32, 64, 100, 100, 7);
  1759. bench_case(1, 32, 32, 80, 80, 7);
  1760. bench_case(1, 32, 64, 80, 80, 7);
  1761. bench_case(1, 32, 32, 200, 200, 5);
  1762. bench_case(1, 32, 64, 200, 200, 5);
  1763. bench_case(1, 32, 32, 128, 128, 5);
  1764. bench_case(1, 32, 64, 128, 128, 5);
  1765. bench_case(1, 32, 32, 100, 100, 5);
  1766. bench_case(1, 32, 64, 100, 100, 5);
  1767. bench_case(1, 32, 32, 80, 80, 5);
  1768. bench_case(1, 32, 64, 80, 80, 5);
  1769. bench_case(1, 32, 32, 200, 200, 3);
  1770. bench_case(1, 32, 64, 200, 200, 3);
  1771. bench_case(1, 32, 32, 128, 128, 3);
  1772. bench_case(1, 32, 64, 128, 128, 3);
  1773. bench_case(1, 32, 32, 100, 100, 3);
  1774. bench_case(1, 32, 64, 100, 100, 3);
  1775. bench_case(1, 32, 32, 80, 80, 3);
  1776. bench_case(1, 32, 64, 80, 80, 3);
  1777. bench_case(1, 32, 32, 200, 200, 2);
  1778. bench_case(1, 32, 64, 200, 200, 2);
  1779. bench_case(1, 32, 32, 128, 128, 2);
  1780. bench_case(1, 32, 64, 128, 128, 2);
  1781. bench_case(1, 32, 32, 100, 100, 2);
  1782. bench_case(1, 32, 64, 100, 100, 2);
  1783. bench_case(1, 32, 32, 80, 80, 2);
  1784. bench_case(1, 32, 64, 80, 80, 2);
  1785. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
  1786. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
  1787. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1788. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1789. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1790. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1791. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1792. {1, {4}}, data_type);
  1793. shapes_and_computation.clear();
  1794. }
  1795. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_8816) {
  1796. constexpr size_t RUNS = 30;
  1797. param::ConvBias param;
  1798. param.stride_h = 1;
  1799. param.stride_w = 1;
  1800. param.sparse = param::ConvBias::Sparse::DENSE;
  1801. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1802. dtype::Int16(), dtype::Int16()};
  1803. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1804. shapes_and_computation;
  1805. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1806. size_t FS) {
  1807. param.pad_h = FS / 2;
  1808. param.pad_w = FS / 2;
  1809. SmallVector<TensorShape> shapes{
  1810. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  1811. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  1812. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  1813. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1814. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1815. };
  1816. bench_case(1, 48, 192, 15, 15, 1);
  1817. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X16_AVX2";
  1818. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1819. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1820. shapes_and_computation.clear();
  1821. }
  1822. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  1823. BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
  1824. constexpr size_t RUNS = 50;
  1825. param::ConvBias param;
  1826. param.stride_h = 2;
  1827. param.stride_w = 2;
  1828. param.sparse = param::ConvBias::Sparse::DENSE;
  1829. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  1830. dtype::Int32(), dtype::Int32()};
  1831. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1832. shapes_and_computation;
  1833. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1834. size_t FS) {
  1835. param.pad_h = FS / 2;
  1836. param.pad_w = FS / 2;
  1837. SmallVector<TensorShape> shapes{
  1838. {N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
  1839. TensorShape dst{N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
  1840. (W + 2 * param.pad_w - FS) / param.stride_w + 1};
  1841. float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
  1842. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1843. };
  1844. bench_case(1, 32, 32, 200, 200, 7);
  1845. bench_case(1, 32, 64, 200, 200, 7);
  1846. bench_case(1, 32, 32, 128, 128, 7);
  1847. bench_case(1, 32, 64, 128, 128, 7);
  1848. bench_case(1, 32, 32, 100, 100, 7);
  1849. bench_case(1, 32, 64, 100, 100, 7);
  1850. bench_case(1, 32, 32, 80, 80, 7);
  1851. bench_case(1, 32, 64, 80, 80, 7);
  1852. bench_case(1, 32, 32, 200, 200, 5);
  1853. bench_case(1, 32, 64, 200, 200, 5);
  1854. bench_case(1, 32, 32, 128, 128, 5);
  1855. bench_case(1, 32, 64, 128, 128, 5);
  1856. bench_case(1, 32, 32, 100, 100, 5);
  1857. bench_case(1, 32, 64, 100, 100, 5);
  1858. bench_case(1, 32, 32, 80, 80, 5);
  1859. bench_case(1, 32, 64, 80, 80, 5);
  1860. bench_case(1, 32, 32, 200, 200, 3);
  1861. bench_case(1, 32, 64, 200, 200, 3);
  1862. bench_case(1, 32, 32, 128, 128, 3);
  1863. bench_case(1, 32, 64, 128, 128, 3);
  1864. bench_case(1, 32, 32, 100, 100, 3);
  1865. bench_case(1, 32, 64, 100, 100, 3);
  1866. bench_case(1, 32, 32, 80, 80, 3);
  1867. bench_case(1, 32, 64, 80, 80, 3);
  1868. bench_case(1, 32, 32, 200, 200, 2);
  1869. bench_case(1, 32, 64, 200, 200, 2);
  1870. bench_case(1, 32, 32, 128, 128, 2);
  1871. bench_case(1, 32, 64, 128, 128, 2);
  1872. bench_case(1, 32, 32, 100, 100, 2);
  1873. bench_case(1, 32, 64, 100, 100, 2);
  1874. bench_case(1, 32, 32, 80, 80, 2);
  1875. bench_case(1, 32, 64, 80, 80, 2);
  1876. std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
  1877. printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
  1878. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1879. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1880. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1881. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1882. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1883. {1, {4}}, data_type);
  1884. shapes_and_computation.clear();
  1885. }
  1886. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
  1887. constexpr size_t RUNS = 50;
  1888. param::ConvBias param;
  1889. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1890. param.pad_h = 1;
  1891. param.pad_w = 1;
  1892. param.stride_h = 1;
  1893. param.stride_w = 1;
  1894. param.sparse = param::ConvBias::Sparse::GROUP;
  1895. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1896. dtype::Float32(), dtype::Float32()};
  1897. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1898. shapes_and_computation;
  1899. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1900. size_t FS, size_t group) {
  1901. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1902. {group, OC / group, IC / group, FS, FS},
  1903. {1, OC, 1, 1},
  1904. {},
  1905. {N, OC, H, W}};
  1906. TensorShape dst{N, OC, H, W};
  1907. float computations =
  1908. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1909. dst.total_nr_elems()) *
  1910. 1e-6;
  1911. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1912. };
  1913. bench_case(1, 32, 32, 200, 200, 3, 4);
  1914. bench_case(1, 32, 32, 200, 200, 3, 32);
  1915. bench_case(1, 32, 32, 128, 128, 3, 4);
  1916. bench_case(1, 32, 32, 128, 128, 3, 32);
  1917. bench_case(1, 32, 32, 100, 100, 3, 4);
  1918. bench_case(1, 32, 32, 100, 100, 3, 32);
  1919. bench_case(1, 32, 32, 80, 80, 3, 4);
  1920. bench_case(1, 32, 32, 80, 80, 3, 32);
  1921. std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
  1922. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP algo\n");
  1923. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1924. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1925. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1926. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1927. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1928. {1, {4}}, data_type);
  1929. shapes_and_computation.clear();
  1930. algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP";
  1931. printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_SMALL_GROUP algo\n");
  1932. bench_case(1, 32, 32, 200, 200, 3, 1);
  1933. bench_case(1, 32, 32, 128, 128, 3, 1);
  1934. bench_case(1, 32, 32, 100, 100, 3, 1);
  1935. bench_case(1, 32, 32, 80, 80, 3, 1);
  1936. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1937. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1938. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1939. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  1940. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  1941. {1, {4}}, data_type);
  1942. }
  1943. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
  1944. constexpr size_t RUNS = 50;
  1945. param::ConvBias param;
  1946. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  1947. param.pad_h = 1;
  1948. param.pad_w = 1;
  1949. param.stride_h = 1;
  1950. param.stride_w = 1;
  1951. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  1952. dtype::Float32(), dtype::Float32()};
  1953. std::vector<std::pair<SmallVector<TensorShape>, float>>
  1954. shapes_and_computation;
  1955. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  1956. size_t FS, size_t group) {
  1957. SmallVector<TensorShape> shapes{{N, IC, H, W},
  1958. {OC / group, IC / group, FS, FS},
  1959. {1, OC, 1, 1},
  1960. {},
  1961. {N, OC, H, W}};
  1962. TensorShape dst{N, OC, H, W};
  1963. float computations =
  1964. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  1965. dst.total_nr_elems()) *
  1966. 1e-6;
  1967. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  1968. };
  1969. bench_case(1, 32, 32, 200, 200, 3, 1);
  1970. bench_case(1, 32, 32, 200, 200, 3, 1);
  1971. bench_case(1, 32, 32, 128, 128, 3, 1);
  1972. bench_case(1, 32, 32, 128, 128, 3, 1);
  1973. bench_case(1, 32, 32, 100, 100, 3, 1);
  1974. bench_case(1, 32, 32, 100, 100, 3, 1);
  1975. bench_case(1, 32, 32, 80, 80, 3, 1);
  1976. bench_case(1, 32, 32, 80, 80, 3, 1);
  1977. bench_case(1, 64, 32, 7, 7, 3, 1);
  1978. bench_case(1, 64, 64, 7, 7, 3, 1);
  1979. bench_case(1, 64, 128, 7, 7, 3, 1);
  1980. bench_case(1, 64, 256, 7, 7, 3, 1);
  1981. bench_case(1, 64, 512, 7, 7, 3, 1);
  1982. bench_case(1, 64, 1024, 7, 7, 3, 1);
  1983. bench_case(1, 64, 32, 14, 14, 3, 1);
  1984. bench_case(1, 64, 64, 14, 14, 3, 1);
  1985. bench_case(1, 64, 128, 14, 14, 3, 1);
  1986. bench_case(1, 64, 256, 14, 14, 3, 1);
  1987. bench_case(1, 64, 512, 14, 14, 3, 1);
  1988. bench_case(1, 64, 1024, 14, 14, 3, 1);
  1989. bench_case(1, 128, 128, 14, 14, 3, 1);
  1990. bench_case(1, 128, 256, 14, 14, 3, 1);
  1991. bench_case(1, 512, 512, 14, 14, 3, 1);
  1992. bench_case(1, 256, 512, 14, 14, 3, 1);
  1993. bench_case(1, 512, 1024, 14, 14, 3, 1);
  1994. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  1995. std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
  1996. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  1997. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  1998. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  1999. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2000. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2001. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2002. {1, {4}}, data_type);
  2003. shapes_and_computation.clear();
  2004. }
  2005. TEST_F(X86_BENCHMARK_MULTI_THREADS,
  2006. BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
  2007. constexpr size_t RUNS = 50;
  2008. param::ConvBias param;
  2009. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2010. param.pad_h = 1;
  2011. param.pad_w = 1;
  2012. param.stride_h = 1;
  2013. param.stride_w = 1;
  2014. std::vector<DType> data_type = {dtype::Float32(), dtype::Float32(),
  2015. dtype::Float32(), dtype::Float32()};
  2016. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2017. shapes_and_computation;
  2018. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2019. size_t FS, size_t group) {
  2020. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2021. {OC / group, IC / group, FS, FS},
  2022. {1, OC, 1, 1},
  2023. {},
  2024. {N, OC, H, W}};
  2025. TensorShape dst{N, OC, H, W};
  2026. float computations =
  2027. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2028. dst.total_nr_elems()) *
  2029. 1e-6;
  2030. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2031. };
  2032. bench_case(1, 32, 32, 200, 200, 3, 1);
  2033. bench_case(1, 32, 32, 200, 200, 3, 1);
  2034. bench_case(1, 32, 32, 128, 128, 3, 1);
  2035. bench_case(1, 32, 32, 128, 128, 3, 1);
  2036. bench_case(1, 32, 32, 100, 100, 3, 1);
  2037. bench_case(1, 32, 32, 100, 100, 3, 1);
  2038. bench_case(1, 32, 32, 80, 80, 3, 1);
  2039. bench_case(1, 32, 32, 80, 80, 3, 1);
  2040. bench_case(1, 64, 32, 7, 7, 3, 1);
  2041. bench_case(1, 64, 64, 7, 7, 3, 1);
  2042. bench_case(1, 64, 128, 7, 7, 3, 1);
  2043. bench_case(1, 64, 256, 7, 7, 3, 1);
  2044. bench_case(1, 64, 512, 7, 7, 3, 1);
  2045. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2046. bench_case(1, 64, 32, 14, 14, 3, 1);
  2047. bench_case(1, 64, 64, 14, 14, 3, 1);
  2048. bench_case(1, 64, 128, 14, 14, 3, 1);
  2049. bench_case(1, 64, 256, 14, 14, 3, 1);
  2050. bench_case(1, 64, 512, 14, 14, 3, 1);
  2051. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2052. bench_case(1, 128, 128, 14, 14, 3, 1);
  2053. bench_case(1, 128, 256, 14, 14, 3, 1);
  2054. bench_case(1, 512, 512, 14, 14, 3, 1);
  2055. bench_case(1, 256, 512, 14, 14, 3, 1);
  2056. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2057. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2058. std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
  2059. std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
  2060. printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
  2061. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2062. RUNS, {1, {4}}, {1, {4}}, data_type);
  2063. benchmark_impl_comp(param, shapes_and_computation, algo_name, algo_name1,
  2064. RUNS, {1, {7}}, {1, {7}}, data_type);
  2065. shapes_and_computation.clear();
  2066. }
  2067. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
  2068. constexpr size_t RUNS = 50;
  2069. param::ConvBias param;
  2070. param.pad_h = 1;
  2071. param.pad_w = 1;
  2072. param.stride_h = 1;
  2073. param.stride_w = 1;
  2074. std::vector<std::pair<SmallVector<TensorShape>, float>>
  2075. shapes_and_computation;
  2076. auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  2077. size_t FS, size_t group) {
  2078. SmallVector<TensorShape> shapes{{N, IC, H, W},
  2079. {OC / group, IC / group, FS, FS},
  2080. {1, OC, 1, 1},
  2081. {},
  2082. {N, OC, H, W}};
  2083. TensorShape dst{N, OC, H, W};
  2084. float computations =
  2085. ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
  2086. dst.total_nr_elems()) *
  2087. 1e-6;
  2088. shapes_and_computation.push_back(std::make_pair(shapes, computations));
  2089. };
  2090. bench_case(1, 32, 32, 200, 200, 3, 1);
  2091. bench_case(1, 32, 32, 200, 200, 3, 1);
  2092. bench_case(1, 32, 32, 128, 128, 3, 1);
  2093. bench_case(1, 32, 32, 128, 128, 3, 1);
  2094. bench_case(1, 32, 32, 100, 100, 3, 1);
  2095. bench_case(1, 32, 32, 100, 100, 3, 1);
  2096. bench_case(1, 32, 32, 80, 80, 3, 1);
  2097. bench_case(1, 32, 32, 80, 80, 3, 1);
  2098. bench_case(1, 64, 32, 7, 7, 3, 1);
  2099. bench_case(1, 64, 64, 7, 7, 3, 1);
  2100. bench_case(1, 64, 128, 7, 7, 3, 1);
  2101. bench_case(1, 64, 256, 7, 7, 3, 1);
  2102. bench_case(1, 64, 512, 7, 7, 3, 1);
  2103. bench_case(1, 64, 1024, 7, 7, 3, 1);
  2104. bench_case(1, 64, 32, 14, 14, 3, 1);
  2105. bench_case(1, 64, 64, 14, 14, 3, 1);
  2106. bench_case(1, 64, 128, 14, 14, 3, 1);
  2107. bench_case(1, 64, 256, 14, 14, 3, 1);
  2108. bench_case(1, 64, 512, 14, 14, 3, 1);
  2109. bench_case(1, 64, 1024, 14, 14, 3, 1);
  2110. bench_case(1, 128, 128, 14, 14, 3, 1);
  2111. bench_case(1, 128, 256, 14, 14, 3, 1);
  2112. bench_case(1, 512, 512, 14, 14, 3, 1);
  2113. bench_case(1, 256, 512, 14, 14, 3, 1);
  2114. bench_case(1, 512, 1024, 14, 14, 3, 1);
  2115. bench_case(1, 1024, 1024, 14, 14, 3, 1);
  2116. std::vector<DType> data_type = {dtype::Int8(), dtype::Int8(),
  2117. dtype::Int32(), dtype::Int32()};
  2118. std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2:192";
  2119. // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
  2120. // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
  2121. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2122. {4, {4, 5, 6, 7}}, {1, {4}}, data_type);
  2123. benchmark_impl(param, shapes_and_computation, algo_name, RUNS,
  2124. {4, {4, 5, 6, 7}}, {1, {7}}, data_type);
  2125. benchmark_impl(param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}},
  2126. {1, {4}}, data_type);
  2127. shapes_and_computation.clear();
  2128. }
  2129. namespace {
  2130. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  2131. size_t pack_size) {
  2132. std::vector<conv_bias::TestArg> args;
  2133. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  2134. size_t p) {
  2135. if (ic % pack_size != 0 || oc % pack_size != 0)
  2136. return;
  2137. if (w + 2 * p < kernel || h + 2 * p < kernel)
  2138. return;
  2139. param::ConvBias param;
  2140. param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  2141. param.format = param::ConvBias::Format::NCHW88;
  2142. param.sparse = param::ConvBias::Sparse::DENSE;
  2143. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  2144. param.stride_h = 1;
  2145. param.stride_w = 1;
  2146. param.pad_h = p;
  2147. param.pad_w = p;
  2148. args.push_back(conv_bias::TestArg{
  2149. param,
  2150. TensorShape{1, ic / 8, h, w, 8},
  2151. TensorShape{oc / 8, ic / 8, kernel, kernel, 8, 8},
  2152. {1, oc / 8, 1, 1, 8}});
  2153. };
  2154. for (size_t ic : {64, 128, 256}) {
  2155. for (size_t oc : {64, 128, 256}) {
  2156. pack(oc, ic, 56, 56, kernel, kernel / 2);
  2157. pack(oc, ic, 14, 14, kernel, kernel / 2);
  2158. pack(oc, ic, 28, 28, kernel, kernel / 2);
  2159. }
  2160. }
  2161. //! conv in vgg16
  2162. pack(512, 512, 15, 15, kernel, kernel / 2);
  2163. pack(512, 256, 15, 15, kernel, kernel / 2);
  2164. pack(256, 256, 29, 29, kernel, kernel / 2);
  2165. pack(256, 128, 29, 29, kernel, kernel / 2);
  2166. pack(128, 128, 57, 57, kernel, kernel / 2);
  2167. pack(128, 64, 57, 57, kernel, kernel / 2);
  2168. pack(64, 64, 56, 56, kernel, kernel / 2);
  2169. pack(128, 128, 28, 28, kernel, kernel / 2);
  2170. pack(512, 512, 14, 14, kernel, kernel / 2);
  2171. return args;
  2172. }
  2173. void benchmark_winograd(const char* algo_name, Handle* handle, size_t kernel,
  2174. size_t pack_size) {
  2175. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  2176. using namespace conv_bias;
  2177. constexpr size_t RUN = 10;
  2178. Benchmarker<ConvBias> benchmark(handle);
  2179. benchmark.set_display(false);
  2180. benchmark.set_times(RUN);
  2181. Benchmarker<ConvBias> benchmark_winograd(handle);
  2182. benchmark_winograd.set_display(false);
  2183. benchmark_winograd.set_times(RUN);
  2184. for (auto&& arg : args) {
  2185. TensorLayout dst_layout;
  2186. auto opr = handle->create_operator<ConvBias>();
  2187. opr->param() = arg.param;
  2188. opr->deduce_layout({arg.src, dtype::Float32()},
  2189. {arg.filter, dtype::Float32()},
  2190. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2191. //! dst.nr_elems * IC * FH * FW * 2
  2192. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2193. arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
  2194. (1024 * 1024 * 1024) * 1e3;
  2195. auto used = benchmark.set_param(arg.param).exec(
  2196. {arg.src, arg.filter, {}, {}, {}}) /
  2197. RUN;
  2198. benchmark_winograd.set_param(arg.param);
  2199. auto used_winograd =
  2200. algo_benchmark<ConvBias>(benchmark_winograd,
  2201. {arg.src, arg.filter, {}, {}, {}},
  2202. algo_name) /
  2203. RUN;
  2204. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  2205. "speedup: "
  2206. "%f\n",
  2207. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2208. used, computations / used, used_winograd,
  2209. computations / used_winograd, used / used_winograd);
  2210. }
  2211. }
  2212. } // namespace
  2213. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
  2214. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
  2215. }
  2216. TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  2217. benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
  2218. }
  2219. #endif
  2220. } // namespace test
  2221. } // namespace megdnn
  2222. // vim: syntax=cpp.doxygen

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