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

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

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