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

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