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

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

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