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

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

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