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

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

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