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

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