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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  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_44_F43_F23) {
  932. #if MEGDNN_AARCH64
  933. benchmark_winograd_compare(
  934. "WINOGRAD:.*:4:4:.*:3", "WINOGRAD:.*:4:2", handle(), 3, 4);
  935. #endif
  936. }
  937. TEST_F(ARM_COMMON, BENCHMARK_WINOGRAD_F43_44) {
  938. #if MEGDNN_AARCH64
  939. benchmark_winograd_weight_preprocess("WINOGRAD:.*:4:4:.*:3", handle(), 3, 4);
  940. #endif
  941. }
  942. TEST_F(ARM_COMMON, BENCHMARK_WINOGRAD_F43_NCHW44) {
  943. #if MEGDNN_AARCH64
  944. benchmark_winograd_weight_preprocess(
  945. "WINOGRAD_NCHW44:.*:4:4:.*:3", handle(), 3, 4, 4);
  946. #endif
  947. }
  948. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63) {
  949. #if MEGDNN_AARCH64
  950. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:6", handle(), 3);
  951. #else
  952. benchmark_winograd("WINOGRAD:ARMV7_F32:1:6", handle(), 3);
  953. #endif
  954. }
  955. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F54) {
  956. #if MEGDNN_AARCH64
  957. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:5", handle(), 4);
  958. #else
  959. benchmark_winograd("WINOGRAD:ARMV7_F32:1:5", handle(), 4);
  960. #endif
  961. }
  962. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F45) {
  963. #if MEGDNN_AARCH64
  964. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:4:.*:5", handle(), 5);
  965. #else
  966. benchmark_winograd("WINOGRAD:ARMV7_F32:1:4:.*:5", handle(), 5);
  967. #endif
  968. }
  969. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  970. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23) {
  971. #if MEGDNN_AARCH64
  972. benchmark_winograd_fp16(
  973. "WINOGRAD:AARCH64_F32_MK4_4x16:4:2", "WINOGRAD:AARCH64_F16_K8X24X1:1:6",
  974. handle(), 3, 4);
  975. #else
  976. benchmark_winograd_fp16(
  977. "WINOGRAD:ARMV7_F32:1:2", "WINOGRAD:AARCH32_F16_K4X16X1:1:2", handle(), 3);
  978. #endif
  979. }
  980. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F45) {
  981. #if MEGDNN_AARCH64
  982. benchmark_winograd_fp16(
  983. "WINOGRAD:AARCH64_F32K8X12X1:1:4:.*:5",
  984. "WINOGRAD:AARCH64_F16_K8X24X1:1:4:.*:5", handle(), 5);
  985. #else
  986. benchmark_winograd_fp16(
  987. "WINOGRAD:ARMV7_F32:1:4:.*:5", "WINOGRAD:AARCH32_F16_K4X16X1:1:4:.*:5",
  988. handle(), 5);
  989. #endif
  990. }
  991. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F63) {
  992. #if MEGDNN_AARCH64
  993. benchmark_winograd_fp16(
  994. "WINOGRAD:AARCH64_F32K8X12X1:1:6", "WINOGRAD:AARCH64_F16_K8X24X1:1:6",
  995. handle(), 3);
  996. #else
  997. benchmark_winograd_fp16(
  998. "WINOGRAD:ARMV7_F32:1:6", "WINOGRAD:AARCH32_F16_K4X16X1:1:6", handle(), 3);
  999. #endif
  1000. }
  1001. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23_8x8) {
  1002. #if MEGDNN_AARCH64
  1003. benchmark_winograd_fp16(
  1004. "WINOGRAD:AARCH64_F32_MK4_4x16:4:2", "WINOGRAD:AARCH64_F16_MK8_8X8:8:2",
  1005. handle(), 3, 8);
  1006. #else
  1007. benchmark_winograd_fp16(
  1008. "WINOGRAD:ARMV7_F32_MK4_4x8:4:2", "WINOGRAD:AARCH32_F16_MK8_4X8:8:2",
  1009. handle(), 3, 8);
  1010. #endif
  1011. }
  1012. #endif
  1013. void benchmark_winograd_nchw_vs_nchw44(
  1014. const char* algo_name0, const char* algo_name1, Handle* handle) {
  1015. using namespace conv_bias;
  1016. using NLMode = param::ConvBias::NonlineMode;
  1017. std::vector<conv_bias::TestArg> args_nchw44;
  1018. std::vector<conv_bias::TestArg> args_nchw;
  1019. auto pack = [&](size_t n, size_t oc, size_t ic, size_t h, size_t w, size_t group,
  1020. NLMode nlmode) {
  1021. param::ConvBias param;
  1022. param.format = param::ConvBias::Format::NCHW44;
  1023. param.stride_h = 1;
  1024. param.stride_w = 1;
  1025. param.pad_h = 1;
  1026. param.pad_w = 1;
  1027. param.nonlineMode = nlmode;
  1028. if (group == 1) {
  1029. param.sparse = param::ConvBias::Sparse::DENSE;
  1030. args_nchw44.emplace_back(
  1031. param, TensorShape{n, ic / 4, h, w, 4},
  1032. TensorShape{oc / 4, ic / 4, 3, 3, 4, 4}, TensorShape{});
  1033. param.format = param::ConvBias::Format::NCHW;
  1034. args_nchw.emplace_back(
  1035. param, TensorShape{n, ic, h, w}, TensorShape{oc, ic, 3, 3},
  1036. TensorShape{});
  1037. } else {
  1038. auto oc_per_group = oc / group;
  1039. auto ic_per_group = ic / group;
  1040. param.sparse = param::ConvBias::Sparse::GROUP;
  1041. args_nchw44.emplace_back(
  1042. param, TensorShape{n, ic_per_group / 4, h, w, 4},
  1043. TensorShape{group, oc_per_group / 4, ic_per_group / 4, 3, 3, 4, 4},
  1044. TensorShape{});
  1045. param.format = param::ConvBias::Format::NCHW;
  1046. args_nchw.emplace_back(
  1047. param, TensorShape{n, ic, h, w},
  1048. TensorShape{group, oc_per_group, ic_per_group, 3, 3},
  1049. TensorShape{});
  1050. }
  1051. };
  1052. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  1053. for (auto nlmode : nonlinemode)
  1054. for (size_t n : {1})
  1055. for (size_t group = 1; group <= 1; ++group) {
  1056. pack(n, 512, 512, 15, 15, group, nlmode);
  1057. pack(n, 512, 256, 15, 15, group, nlmode);
  1058. pack(n, 256, 256, 29, 29, group, nlmode);
  1059. pack(n, 256, 128, 29, 29, group, nlmode);
  1060. pack(n, 128, 128, 57, 57, group, nlmode);
  1061. pack(n, 128, 64, 57, 57, group, nlmode);
  1062. pack(n, 24, 24, 224, 224, group, nlmode);
  1063. pack(n, 64, 24, 123, 123, group, nlmode);
  1064. pack(n, 64, 64, 56, 56, group, nlmode);
  1065. pack(n, 128, 128, 28, 28, group, nlmode);
  1066. pack(n, 256, 256, 14, 14, group, nlmode);
  1067. pack(n, 512, 512, 7, 7, group, nlmode);
  1068. }
  1069. using namespace conv_bias;
  1070. constexpr size_t RUN = 10;
  1071. Benchmarker<ConvBias> benchmark_winograd_nchw(handle);
  1072. benchmark_winograd_nchw.set_display(false);
  1073. benchmark_winograd_nchw.set_times(RUN);
  1074. Benchmarker<ConvBias> benchmark_winograd_nchw44(handle);
  1075. benchmark_winograd_nchw44.set_display(false);
  1076. benchmark_winograd_nchw44.set_times(RUN);
  1077. std::string winograd_nchw_algo_name = ssprintf("WINOGRAD:%s", algo_name0);
  1078. std::string winograd_nchw44_algo_name = ssprintf("WINOGRAD_NCHW44:%s", algo_name1);
  1079. for (size_t i = 0; i < args_nchw.size(); ++i) {
  1080. auto arg_nchw = args_nchw[i];
  1081. auto arg_nchw44 = args_nchw44[i];
  1082. TensorLayout dst_layout;
  1083. auto opr = handle->create_operator<ConvBias>();
  1084. opr->param() = arg_nchw.param;
  1085. opr->deduce_layout(
  1086. {arg_nchw.src, dtype::Float32()}, {arg_nchw.filter, dtype::Float32()},
  1087. {arg_nchw.bias, dtype::Float32()}, {}, dst_layout);
  1088. //! dst.nr_elems * IC * FH * FW * 2
  1089. float computations = dst_layout.total_nr_elems() * arg_nchw.filter[1] *
  1090. arg_nchw.filter[2] * arg_nchw.filter[3] * 2.0 /
  1091. (1024 * 1024 * 1024) * 1e3;
  1092. benchmark_winograd_nchw.set_param(arg_nchw.param);
  1093. auto nchw_used = algo_benchmark<ConvBias>(
  1094. benchmark_winograd_nchw,
  1095. {arg_nchw.src, arg_nchw.filter, {}, {}, {}},
  1096. winograd_nchw_algo_name.c_str()) /
  1097. RUN;
  1098. benchmark_winograd_nchw44.set_param(arg_nchw44.param);
  1099. auto nchw44_used = algo_benchmark<ConvBias>(
  1100. benchmark_winograd_nchw44,
  1101. {arg_nchw44.src, arg_nchw44.filter, {}, {}, {}},
  1102. winograd_nchw44_algo_name.c_str()) /
  1103. RUN;
  1104. printf("%s %s: nchw: %f ms %f Gflops nchw44: %f ms %f GFlops "
  1105. "speedup: "
  1106. "%f\n",
  1107. arg_nchw.src.to_string().c_str(), arg_nchw.filter.to_string().c_str(),
  1108. nchw_used, computations / nchw_used, nchw44_used,
  1109. computations / nchw44_used, nchw_used / nchw44_used);
  1110. }
  1111. }
  1112. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F73_MK4_NCHW_VS_NCHW44) {
  1113. #if MEGDNN_AARCH64
  1114. benchmark_winograd_nchw_vs_nchw44(
  1115. "AARCH64_F32_MK4_4x16:4:6", "FB_GI_F32_GEMV_MK4:4:7", handle());
  1116. #else
  1117. benchmark_winograd_nchw_vs_nchw44(
  1118. "ARMV7_F32_MK4_4x8:4:6", "ARMV7_F32_MK4_4x8:4:7", handle());
  1119. #endif
  1120. }
  1121. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  1122. auto benchmark_winograd_quantized = [](const char* algo_name_fp32,
  1123. const char* algo_name_quantized,
  1124. Handle* handle, size_t kernel) {
  1125. auto&& args = get_winograd_benchmark_args(kernel);
  1126. using namespace conv_bias;
  1127. constexpr size_t RUN = 10;
  1128. Benchmarker<ConvBias> benchmark(handle);
  1129. benchmark.set_display(false);
  1130. benchmark.set_times(RUN);
  1131. Benchmarker<ConvBias> benchmark_winograd(handle);
  1132. benchmark_winograd.set_display(false).set_times(RUN);
  1133. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  1134. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1135. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1136. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1137. for (auto&& arg : args) {
  1138. TensorLayout dst_layout;
  1139. auto opr = handle->create_operator<ConvBias>();
  1140. opr->param() = arg.param;
  1141. opr->deduce_layout(
  1142. {arg.src, dtype::Float32()}, {arg.filter, dtype::Float32()},
  1143. {arg.bias, dtype::Float32()}, {}, dst_layout);
  1144. //! dst.nr_elems * IC * FH * FW * 2
  1145. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1146. arg.filter[2] * arg.filter[3] * 2.0 /
  1147. (1024 * 1024 * 1024) * 1e3;
  1148. benchmark.set_param(arg.param);
  1149. auto used = algo_benchmark<ConvBias>(
  1150. benchmark, {arg.src, arg.filter, {}, {}, {}},
  1151. algo_name_fp32) /
  1152. RUN;
  1153. benchmark_winograd.set_param(arg.param);
  1154. auto used_winograd =
  1155. algo_benchmark<ConvBias>(
  1156. benchmark_winograd, {arg.src, arg.filter, {}, {}, {}},
  1157. algo_name_quantized) /
  1158. RUN;
  1159. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  1160. "speedup: "
  1161. "%f\n",
  1162. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used,
  1163. computations / used, used_winograd, computations / used_winograd,
  1164. used / used_winograd);
  1165. }
  1166. };
  1167. #if MEGDNN_AARCH64
  1168. benchmark_winograd_quantized(
  1169. "WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  1170. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2", handle(), 3);
  1171. #else
  1172. benchmark_winograd_quantized(
  1173. "WINOGRAD:ARMV7_F32_MK4_4x8:4:2", "WINOGRAD:ARMV7_INT16X16X32_MK8_4X8:8:2",
  1174. handle(), 3);
  1175. #endif
  1176. }
  1177. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1) {
  1178. // have to remove preferred restrict in usable func before run the benchmark
  1179. using namespace conv_bias;
  1180. std::vector<TestArg> args;
  1181. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1182. NonlineMode nonline_mode) {
  1183. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1184. return;
  1185. param::ConvBias param;
  1186. param.stride_h = 1;
  1187. param.stride_w = 1;
  1188. param.pad_h = p;
  1189. param.pad_w = p;
  1190. param.nonlineMode = nonline_mode;
  1191. //! channel bias
  1192. args.emplace_back(
  1193. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1194. TensorShape{1, oc, 1, 1});
  1195. };
  1196. for (size_t kernel : {2, 3, 5, 7})
  1197. for (size_t ic : {1, 8, 16, 32})
  1198. for (size_t oc : {1, 8, 16, 32})
  1199. for (size_t p : {1})
  1200. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1201. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1202. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1203. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1204. }
  1205. constexpr size_t RUN = 50;
  1206. Benchmarker<ConvBias> benchmark0(handle());
  1207. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1208. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1209. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1210. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1211. benchmark0.set_display(false);
  1212. benchmark0.set_times(RUN);
  1213. benchmark0.set_before_exec_callback(
  1214. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD1"));
  1215. Benchmarker<ConvBias> benchmark1(handle());
  1216. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1217. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1218. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1219. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1220. benchmark1.set_display(false);
  1221. benchmark1.set_times(RUN);
  1222. for (auto&& arg : args) {
  1223. TensorLayout dst_layout;
  1224. auto opr = handle()->create_operator<ConvBias>();
  1225. opr->param() = arg.param;
  1226. opr->deduce_layout(
  1227. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1228. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1229. //! dst.nr_elems * IC * FH * FW * 2
  1230. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1231. arg.filter[2] * arg.filter[3] * 2.0 /
  1232. (1024 * 1024 * 1024) * 1e3;
  1233. auto used0 = benchmark0.set_param(arg.param).exec(
  1234. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1235. RUN;
  1236. auto used1 = benchmark1.set_param(arg.param).exec(
  1237. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1238. RUN;
  1239. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1240. "speedup: %f\n",
  1241. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1242. computations / used0, used1, computations / used1, used1 / used0);
  1243. }
  1244. }
  1245. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2) {
  1246. // have to remove preferred restrict in usable func before run the benchmark
  1247. using namespace conv_bias;
  1248. std::vector<TestArg> args;
  1249. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1250. NonlineMode nonline_mode) {
  1251. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1252. return;
  1253. param::ConvBias param;
  1254. param.stride_h = 2;
  1255. param.stride_w = 2;
  1256. param.pad_h = p;
  1257. param.pad_w = p;
  1258. param.nonlineMode = nonline_mode;
  1259. //! channel bias
  1260. args.emplace_back(
  1261. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1262. TensorShape{1, oc, 1, 1});
  1263. };
  1264. for (size_t kernel : {2, 3, 5, 7})
  1265. for (size_t ic : {1, 8, 16, 32})
  1266. for (size_t oc : {1, 8, 16, 32})
  1267. for (size_t p : {1})
  1268. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1269. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1270. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1271. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1272. }
  1273. constexpr size_t RUN = 50;
  1274. Benchmarker<ConvBias> benchmark0(handle());
  1275. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1276. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1277. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1278. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1279. benchmark0.set_display(false);
  1280. benchmark0.set_times(RUN);
  1281. benchmark0.set_before_exec_callback(
  1282. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD2"));
  1283. Benchmarker<ConvBias> benchmark1(handle());
  1284. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1285. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1286. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1287. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1288. benchmark1.set_display(false);
  1289. benchmark1.set_times(RUN);
  1290. for (auto&& arg : args) {
  1291. TensorLayout dst_layout;
  1292. auto opr = handle()->create_operator<ConvBias>();
  1293. opr->param() = arg.param;
  1294. opr->deduce_layout(
  1295. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1296. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1297. //! dst.nr_elems * IC * FH * FW * 2
  1298. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1299. arg.filter[2] * arg.filter[3] * 2.0 /
  1300. (1024 * 1024 * 1024) * 1e3;
  1301. auto used0 = benchmark0.set_param(arg.param).exec(
  1302. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1303. RUN;
  1304. auto used1 = benchmark1.set_param(arg.param).exec(
  1305. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1306. RUN;
  1307. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1308. "speedup: %f\n",
  1309. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1310. computations / used0, used1, computations / used1, used1 / used0);
  1311. }
  1312. }
  1313. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1) {
  1314. // have to remove preferred restrict in usable func before run the benchmark
  1315. using namespace conv_bias;
  1316. std::vector<TestArg> args;
  1317. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1318. NonlineMode nonline_mode) {
  1319. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1320. return;
  1321. param::ConvBias param;
  1322. param.stride_h = 1;
  1323. param.stride_w = 1;
  1324. param.pad_h = p;
  1325. param.pad_w = p;
  1326. param.nonlineMode = nonline_mode;
  1327. //! channel bias
  1328. args.emplace_back(
  1329. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1330. TensorShape{1, oc, 1, 1});
  1331. };
  1332. for (size_t kernel : {2, 3, 5, 7})
  1333. for (size_t ic : {1, 8, 16, 32})
  1334. for (size_t oc : {1, 8, 16, 32})
  1335. for (size_t p : {1})
  1336. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1337. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1338. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1339. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1340. }
  1341. constexpr size_t RUN = 50;
  1342. Benchmarker<ConvBias> benchmark0(handle());
  1343. benchmark0.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1344. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1345. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1346. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1347. benchmark0.set_display(false);
  1348. benchmark0.set_times(RUN);
  1349. benchmark0.set_before_exec_callback(
  1350. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD1"));
  1351. Benchmarker<ConvBias> benchmark1(handle());
  1352. benchmark1.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1353. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1354. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1355. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1356. benchmark1.set_display(false);
  1357. benchmark1.set_times(RUN);
  1358. for (auto&& arg : args) {
  1359. TensorLayout dst_layout;
  1360. auto opr = handle()->create_operator<ConvBias>();
  1361. opr->param() = arg.param;
  1362. opr->deduce_layout(
  1363. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1364. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1365. //! dst.nr_elems * IC * FH * FW * 2
  1366. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1367. arg.filter[2] * arg.filter[3] * 2.0 /
  1368. (1024 * 1024 * 1024) * 1e3;
  1369. auto used0 = benchmark0.set_param(arg.param).exec(
  1370. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1371. RUN;
  1372. auto used1 = benchmark1.set_param(arg.param).exec(
  1373. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1374. RUN;
  1375. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1376. "speedup: %f\n",
  1377. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1378. computations / used0, used1, computations / used1, used1 / used0);
  1379. }
  1380. }
  1381. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2) {
  1382. // have to remove preferred restrict in usable func before run the benchmark
  1383. using namespace conv_bias;
  1384. std::vector<TestArg> args;
  1385. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1386. NonlineMode nonline_mode) {
  1387. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1388. return;
  1389. param::ConvBias param;
  1390. param.stride_h = 2;
  1391. param.stride_w = 2;
  1392. param.pad_h = p;
  1393. param.pad_w = p;
  1394. param.nonlineMode = nonline_mode;
  1395. //! channel bias
  1396. args.emplace_back(
  1397. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1398. TensorShape{1, oc, 1, 1});
  1399. };
  1400. for (size_t kernel : {2, 3, 5, 7})
  1401. for (size_t ic : {1, 8, 16, 32})
  1402. for (size_t oc : {1, 8, 16, 32})
  1403. for (size_t p : {1})
  1404. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1405. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1406. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1407. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1408. }
  1409. constexpr size_t RUN = 50;
  1410. Benchmarker<ConvBias> benchmark0(handle());
  1411. benchmark0.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1412. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1413. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1414. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1415. benchmark0.set_display(false);
  1416. benchmark0.set_times(RUN);
  1417. benchmark0.set_before_exec_callback(
  1418. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD2"));
  1419. Benchmarker<ConvBias> benchmark1(handle());
  1420. benchmark1.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1421. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1422. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1423. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1424. benchmark1.set_display(false);
  1425. benchmark1.set_times(RUN);
  1426. for (auto&& arg : args) {
  1427. TensorLayout dst_layout;
  1428. auto opr = handle()->create_operator<ConvBias>();
  1429. opr->param() = arg.param;
  1430. opr->deduce_layout(
  1431. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1432. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1433. //! dst.nr_elems * IC * FH * FW * 2
  1434. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1435. arg.filter[2] * arg.filter[3] * 2.0 /
  1436. (1024 * 1024 * 1024) * 1e3;
  1437. auto used0 = benchmark0.set_param(arg.param).exec(
  1438. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1439. RUN;
  1440. auto used1 = benchmark1.set_param(arg.param).exec(
  1441. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1442. RUN;
  1443. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1444. "speedup: %f\n",
  1445. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1446. computations / used0, used1, computations / used1, used1 / used0);
  1447. }
  1448. }
  1449. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QINT8_STRIDE1_NCHW44) {
  1450. // have to remove preferred restrict in usable func before run the benchmark
  1451. using namespace conv_bias;
  1452. param::ConvBias param;
  1453. param.stride_h = 1;
  1454. param.stride_w = 1;
  1455. param.pad_h = 1;
  1456. param.pad_w = 1;
  1457. param.nonlineMode = NonlineMode::RELU;
  1458. param.sparse = param::ConvBias::Sparse::GROUP;
  1459. constexpr size_t RUN = 50;
  1460. Benchmarker<ConvBias> benchmark0(handle());
  1461. benchmark0.set_dtype(0, dtype::QuantizedS8(0.2f))
  1462. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1463. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1464. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1465. benchmark0.set_display(false);
  1466. benchmark0.set_param(param);
  1467. benchmark0.set_times(RUN);
  1468. benchmark0.set_before_exec_callback(
  1469. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD1"));
  1470. auto opr = handle()->create_operator<ConvBias>();
  1471. opr->param() = param;
  1472. param.format = param::ConvBias::Format::NCHW44;
  1473. Benchmarker<ConvBias> benchmark1(handle());
  1474. benchmark1.set_dtype(0, dtype::QuantizedS8(0.2f))
  1475. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1476. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1477. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1478. benchmark1.set_display(false);
  1479. benchmark1.set_param(param);
  1480. benchmark1.set_times(RUN);
  1481. benchmark1.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1482. "S8_CHAN_WISE_STRD1_NCHW44"));
  1483. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1484. TensorLayout dst_layout;
  1485. opr->deduce_layout(
  1486. {{1, group * 4, h, w}, dtype::Int8()},
  1487. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1488. {{1, group * 4, 1, 1}, dtype::Int32()}, {}, dst_layout);
  1489. //! dst.nr_elems * IC * FH * FW * 2
  1490. float computations = dst_layout.total_nr_elems() * kernel * kernel * 2.0 /
  1491. (1024 * 1024 * 1024) * 1e3;
  1492. auto used0 = benchmark0.exec(
  1493. {{1, group * 4, h, w},
  1494. {group * 4, 1, 1, kernel, kernel},
  1495. {1, group * 4, 1, 1},
  1496. {},
  1497. {}}) /
  1498. RUN;
  1499. auto used1 = benchmark1.exec(
  1500. {{1, group, h, w, 4},
  1501. {group, 1, 1, kernel, kernel, 4},
  1502. {1, group, 1, 1, 4},
  1503. {},
  1504. {}}) /
  1505. RUN;
  1506. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1507. "nchw44: "
  1508. "%f ms %f GFlops "
  1509. "speedup: %f\n",
  1510. group, h, w, kernel, used0, computations / used0, used1,
  1511. computations / used1, used0 / used1);
  1512. };
  1513. for (size_t group : {8, 16, 32, 64, 128}) {
  1514. for (size_t kerenl : {2, 3, 5}) {
  1515. run(group, 112, 112, kerenl);
  1516. run(group, 56, 56, kerenl);
  1517. run(group, 48, 48, kerenl);
  1518. run(group, 28, 28, kerenl);
  1519. run(group, 14, 14, kerenl);
  1520. }
  1521. }
  1522. }
  1523. #endif
  1524. #if MGB_ENABLE_DOT
  1525. #if MEGDNN_WITH_BENCHMARK
  1526. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD) {
  1527. // have to remove preferred restrict in usable func before run the benchmark
  1528. using namespace conv_bias;
  1529. std::vector<TestArg> args;
  1530. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1531. NonlineMode nonline_mode) {
  1532. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1533. return;
  1534. param::ConvBias param;
  1535. param.stride_h = 1;
  1536. param.stride_w = 1;
  1537. param.pad_h = p;
  1538. param.pad_w = p;
  1539. param.nonlineMode = nonline_mode;
  1540. //! channel bias
  1541. args.emplace_back(
  1542. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1543. TensorShape{1, oc, 1, 1});
  1544. };
  1545. for (size_t kernel : {2, 3, 5, 7})
  1546. for (size_t ic : {1, 8, 16, 32})
  1547. for (size_t oc : {1, 8, 16, 32})
  1548. for (size_t p : {1})
  1549. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1550. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1551. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1552. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1553. }
  1554. constexpr size_t RUN = 50;
  1555. Benchmarker<ConvBias> benchmark0(handle());
  1556. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1557. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1558. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1559. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1560. benchmark0.set_display(false);
  1561. benchmark0.set_times(RUN);
  1562. benchmark0.set_before_exec_callback(
  1563. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD1"));
  1564. Benchmarker<ConvBias> benchmark1(handle());
  1565. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1566. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1567. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1568. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1569. benchmark1.set_display(false);
  1570. benchmark1.set_times(RUN);
  1571. for (auto&& arg : args) {
  1572. TensorLayout dst_layout;
  1573. auto opr = handle()->create_operator<ConvBias>();
  1574. opr->param() = arg.param;
  1575. opr->deduce_layout(
  1576. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1577. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1578. //! dst.nr_elems * IC * FH * FW * 2
  1579. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1580. arg.filter[2] * arg.filter[3] * 2.0 /
  1581. (1024 * 1024 * 1024) * 1e3;
  1582. auto used0 = benchmark0.set_param(arg.param).exec(
  1583. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1584. RUN;
  1585. auto used1 = benchmark1.set_param(arg.param).exec(
  1586. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1587. RUN;
  1588. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1589. "speedup: %f\n",
  1590. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1591. computations / used0, used1, computations / used1, used1 / used0);
  1592. }
  1593. }
  1594. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2_WITHDOTPROD) {
  1595. // have to remove preferred restrict in usable func before run the benchmark
  1596. using namespace conv_bias;
  1597. std::vector<TestArg> args;
  1598. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1599. NonlineMode nonline_mode) {
  1600. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1601. return;
  1602. param::ConvBias param;
  1603. param.stride_h = 2;
  1604. param.stride_w = 2;
  1605. param.pad_h = p;
  1606. param.pad_w = p;
  1607. param.nonlineMode = nonline_mode;
  1608. //! channel bias
  1609. args.emplace_back(
  1610. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1611. TensorShape{1, oc, 1, 1});
  1612. };
  1613. for (size_t kernel : {2, 3, 5, 7})
  1614. for (size_t ic : {1, 8, 16, 32})
  1615. for (size_t oc : {1, 8, 16, 32})
  1616. for (size_t p : {1})
  1617. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1618. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1619. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1620. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1621. }
  1622. constexpr size_t RUN = 50;
  1623. Benchmarker<ConvBias> benchmark0(handle());
  1624. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1625. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1626. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1627. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1628. benchmark0.set_display(false);
  1629. benchmark0.set_times(RUN);
  1630. benchmark0.set_before_exec_callback(
  1631. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD2"));
  1632. Benchmarker<ConvBias> benchmark1(handle());
  1633. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1634. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1635. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1636. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1637. benchmark1.set_display(false);
  1638. benchmark1.set_times(RUN);
  1639. for (auto&& arg : args) {
  1640. TensorLayout dst_layout;
  1641. auto opr = handle()->create_operator<ConvBias>();
  1642. opr->param() = arg.param;
  1643. opr->deduce_layout(
  1644. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1645. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1646. //! dst.nr_elems * IC * FH * FW * 2
  1647. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1648. arg.filter[2] * arg.filter[3] * 2.0 /
  1649. (1024 * 1024 * 1024) * 1e3;
  1650. auto used0 = benchmark0.set_param(arg.param).exec(
  1651. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1652. RUN;
  1653. auto used1 = benchmark1.set_param(arg.param).exec(
  1654. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1655. RUN;
  1656. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1657. "speedup: %f\n",
  1658. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1659. computations / used0, used1, computations / used1, used1 / used0);
  1660. }
  1661. }
  1662. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1_WITHDOTPROD) {
  1663. // have to remove preferred restrict in usable func before run the benchmark
  1664. using namespace conv_bias;
  1665. std::vector<TestArg> args;
  1666. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1667. NonlineMode nonline_mode) {
  1668. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1669. return;
  1670. param::ConvBias param;
  1671. param.stride_h = 1;
  1672. param.stride_w = 1;
  1673. param.pad_h = p;
  1674. param.pad_w = p;
  1675. param.nonlineMode = nonline_mode;
  1676. //! channel bias
  1677. args.emplace_back(
  1678. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1679. TensorShape{1, oc, 1, 1});
  1680. };
  1681. // clang-format off
  1682. for (size_t kernel : {2, 3, 5, 7})
  1683. for (size_t ic : {1, 8, 16, 32})
  1684. for (size_t oc : {1, 8, 16, 32})
  1685. for (size_t p : {1})
  1686. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1687. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1688. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1689. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1690. }
  1691. // clang-format on
  1692. constexpr size_t RUN = 50;
  1693. Benchmarker<ConvBias> benchmark0(handle());
  1694. benchmark0.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1695. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1696. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1697. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1698. benchmark0.set_display(false);
  1699. benchmark0.set_times(RUN);
  1700. benchmark0.set_before_exec_callback(
  1701. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD1"));
  1702. Benchmarker<ConvBias> benchmark1(handle());
  1703. benchmark1.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1704. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1705. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1706. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1707. benchmark1.set_display(false);
  1708. benchmark1.set_times(RUN);
  1709. for (auto&& arg : args) {
  1710. TensorLayout dst_layout;
  1711. auto opr = handle()->create_operator<ConvBias>();
  1712. opr->param() = arg.param;
  1713. opr->deduce_layout(
  1714. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1715. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1716. //! dst.nr_elems * IC * FH * FW * 2
  1717. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1718. arg.filter[2] * arg.filter[3] * 2.0 /
  1719. (1024 * 1024 * 1024) * 1e3;
  1720. auto used0 = benchmark0.set_param(arg.param).exec(
  1721. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1722. RUN;
  1723. auto used1 = benchmark1.set_param(arg.param).exec(
  1724. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1725. RUN;
  1726. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1727. "speedup: %f\n",
  1728. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1729. computations / used0, used1, computations / used1, used1 / used0);
  1730. }
  1731. }
  1732. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2_WITHDOTPROD) {
  1733. // have to remove preferred restrict in usable func before run the benchmark
  1734. using namespace conv_bias;
  1735. std::vector<TestArg> args;
  1736. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1737. NonlineMode nonline_mode) {
  1738. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1739. return;
  1740. param::ConvBias param;
  1741. param.stride_h = 2;
  1742. param.stride_w = 2;
  1743. param.pad_h = p;
  1744. param.pad_w = p;
  1745. param.nonlineMode = nonline_mode;
  1746. //! channel bias
  1747. args.emplace_back(
  1748. param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  1749. TensorShape{1, oc, 1, 1});
  1750. };
  1751. // clang-format off
  1752. for (size_t kernel : {2, 3, 5, 7})
  1753. for (size_t ic : {1, 8, 16, 32})
  1754. for (size_t oc : {1, 8, 16, 32})
  1755. for (size_t p : {1})
  1756. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1757. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1758. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1759. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1760. }
  1761. // clang-format on
  1762. constexpr size_t RUN = 50;
  1763. Benchmarker<ConvBias> benchmark0(handle());
  1764. benchmark0.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1765. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1766. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1767. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1768. benchmark0.set_display(false);
  1769. benchmark0.set_times(RUN);
  1770. benchmark0.set_before_exec_callback(
  1771. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD2"));
  1772. Benchmarker<ConvBias> benchmark1(handle());
  1773. benchmark1.set_dtype(0, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1774. .set_dtype(1, dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1775. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1776. .set_dtype(4, dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1777. benchmark1.set_display(false);
  1778. benchmark1.set_times(RUN);
  1779. for (auto&& arg : args) {
  1780. TensorLayout dst_layout;
  1781. auto opr = handle()->create_operator<ConvBias>();
  1782. opr->param() = arg.param;
  1783. opr->deduce_layout(
  1784. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1785. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1786. //! dst.nr_elems * IC * FH * FW * 2
  1787. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1788. arg.filter[2] * arg.filter[3] * 2.0 /
  1789. (1024 * 1024 * 1024) * 1e3;
  1790. auto used0 = benchmark0.set_param(arg.param).exec(
  1791. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1792. RUN;
  1793. auto used1 = benchmark1.set_param(arg.param).exec(
  1794. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1795. RUN;
  1796. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1797. "speedup: %f\n",
  1798. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1799. computations / used0, used1, computations / used1, used1 / used0);
  1800. }
  1801. }
  1802. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD_NCHW44_DOT) {
  1803. using namespace conv_bias;
  1804. std::vector<TestArg> args;
  1805. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
  1806. size_t stride, NonlineMode nonline_mode) {
  1807. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1808. return;
  1809. param::ConvBias param;
  1810. param.stride_h = stride;
  1811. param.stride_w = stride;
  1812. param.pad_h = p;
  1813. param.pad_w = p;
  1814. param.nonlineMode = nonline_mode;
  1815. param.format = param::ConvBias::Format::NCHW44_DOT;
  1816. //! channel bias
  1817. args.emplace_back(
  1818. param, TensorShape{1, ic / 4, h, w, 4},
  1819. TensorShape{oc / 4, ic / 4, kernel, kernel, 4, 4},
  1820. TensorShape{1, oc / 4, 1, 1, 4});
  1821. };
  1822. for (size_t stride : {1, 2})
  1823. for (size_t kernel : {2, 3, 5, 7})
  1824. for (size_t oc : {64})
  1825. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1826. run(oc, oc, 56, 56, kernel, kernel / 2, stride, nonline_mode);
  1827. }
  1828. constexpr size_t RUN = 50;
  1829. Benchmarker<ConvBias> benchmark0(handle());
  1830. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1831. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1832. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1833. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1834. benchmark0.set_display(false);
  1835. benchmark0.set_times(RUN);
  1836. benchmark0.set_before_exec_callback(
  1837. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8DIRECT_NCHW44"));
  1838. Benchmarker<ConvBias> benchmark1(handle());
  1839. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1840. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1841. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1842. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1843. benchmark1.set_display(false);
  1844. benchmark1.set_times(RUN);
  1845. for (auto&& arg : args) {
  1846. TensorLayout dst_layout;
  1847. auto opr = handle()->create_operator<ConvBias>();
  1848. opr->param() = arg.param;
  1849. opr->deduce_layout(
  1850. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1851. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1852. //! dst.nr_elems * IC * FH * FW * 2
  1853. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1854. arg.filter[2] * arg.filter[3] * 8.0 /
  1855. (1024 * 1024 * 1024) * 1e3;
  1856. auto used0 = benchmark0.set_param(arg.param).exec(
  1857. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1858. RUN;
  1859. auto used1 = benchmark1.set_param(arg.param).exec(
  1860. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1861. RUN;
  1862. printf("%s %s: Direct use: %f ms %f Gflops normal: %f ms %f GFlops "
  1863. "speedup: %f\n",
  1864. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used0,
  1865. computations / used0, used1, computations / used1, used1 / used0);
  1866. }
  1867. }
  1868. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_LARGE_KERN_NCHW_DOT) {
  1869. using namespace conv_bias;
  1870. std::vector<TestArg> args;
  1871. auto run = [&](size_t group, size_t w, size_t h, size_t kernel, size_t stride,
  1872. NonlineMode nonline_mode) {
  1873. size_t p = kernel / 2;
  1874. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1875. return;
  1876. param::ConvBias param;
  1877. param.stride_h = stride;
  1878. param.stride_w = stride;
  1879. param.pad_h = p;
  1880. param.pad_w = p;
  1881. param.nonlineMode = nonline_mode;
  1882. param.format = param::ConvBias::Format::NCHW;
  1883. param.sparse = ConvBiasForward::Param::Sparse::GROUP;
  1884. //! channel bias
  1885. args.emplace_back(
  1886. param, TensorShape{1, group, h, w},
  1887. TensorShape{group, 1, 1, kernel, kernel}, TensorShape{1, group, 1, 1});
  1888. };
  1889. run(64, 64, 64, 9, 1, NonlineMode::RELU);
  1890. run(64, 40, 40, 9, 2, NonlineMode::RELU);
  1891. run(64, 20, 20, 9, 1, NonlineMode::RELU);
  1892. constexpr size_t RUN = 120;
  1893. Benchmarker<ConvBias> benchmark0(handle());
  1894. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1895. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1896. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1897. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1898. benchmark0.set_display(false);
  1899. benchmark0.set_times(RUN);
  1900. benchmark0.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1901. "ARMDOTS8_DIRECT_CHANWISE_LARGE"));
  1902. Benchmarker<ConvBias> benchmark1(handle());
  1903. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1904. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1905. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1906. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1907. benchmark1.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1908. "ARMDOTS8_IM2COL_CHANWISE_LARGE"));
  1909. benchmark1.set_display(false);
  1910. benchmark1.set_times(RUN);
  1911. Benchmarker<ConvBias> benchmark2(handle());
  1912. benchmark2.set_dtype(0, dtype::QuantizedS8(2.5f))
  1913. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1914. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1915. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1916. benchmark2.set_before_exec_callback(
  1917. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8"));
  1918. benchmark2.set_display(false);
  1919. benchmark2.set_times(RUN);
  1920. for (auto&& arg : args) {
  1921. TensorLayout dst_layout;
  1922. auto opr = handle()->create_operator<ConvBias>();
  1923. opr->param() = arg.param;
  1924. opr->deduce_layout(
  1925. {arg.src, dtype::Int8()}, {arg.filter, dtype::Int8()},
  1926. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1927. //! dst.nr_elems * FH * FW * 2
  1928. float computations =
  1929. dst_layout.total_nr_elems() * arg.filter[3] * arg.filter[4] * 2.0 / 1e6;
  1930. float computations_5x5 = dst_layout.total_nr_elems() * 5 * 5 * 2.0 / 1e6;
  1931. float computations_11x11 = dst_layout.total_nr_elems() * 11 * 11 * 2.0 / 1e6;
  1932. param::ConvBias param_5x5 = arg.param;
  1933. param_5x5.pad_h = param_5x5.pad_w = 5 / 2;
  1934. param::ConvBias param_11x11 = arg.param;
  1935. param_11x11.pad_h = param_11x11.pad_w = 11 / 2;
  1936. auto used0 = benchmark0.set_param(arg.param).exec(
  1937. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1938. RUN;
  1939. auto used1 = benchmark1.set_param(arg.param).exec(
  1940. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1941. RUN;
  1942. TensorShape flt_5x5_shape = arg.filter;
  1943. flt_5x5_shape[3] = flt_5x5_shape[4] = 5;
  1944. auto used5x5 = benchmark2.set_param(param_5x5).exec(
  1945. {arg.src, flt_5x5_shape, arg.bias, {}, {}}) /
  1946. RUN;
  1947. TensorShape flt_11x11_shape = arg.filter;
  1948. flt_11x11_shape[3] = flt_11x11_shape[4] = 11;
  1949. auto used11x11 = benchmark0.set_param(param_11x11)
  1950. .exec({arg.src, flt_11x11_shape, arg.bias, {}, {}}) /
  1951. RUN;
  1952. printf("%s %s s %u: Direct use: %f ms %f Gflops im2col: %f ms %f GFlops "
  1953. "speedup: %f, compare 5x5 %f ms %f GFlops speedup %f, compare 11x11 %f "
  1954. "ms %f GFops speedup %f\n",
  1955. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1956. arg.param.stride_h, used0, computations / used0, used1,
  1957. computations / used1, used1 / used0, used5x5, computations_5x5 / used5x5,
  1958. used5x5 / used0, used11x11, computations_11x11 / used11x11,
  1959. used11x11 / used0);
  1960. }
  1961. }
  1962. #endif
  1963. #endif
  1964. /*====================== BENCHMARK CONV1X1 ===========================*/
  1965. #if MEGDNN_WITH_BENCHMARK
  1966. namespace {
  1967. std::vector<conv_bias::TestArg> get_conv_bias_1x1_benchmark_args(size_t pack_size = 1) {
  1968. using namespace conv_bias;
  1969. std::vector<TestArg> args;
  1970. param::ConvBias param;
  1971. param.stride_h = 1;
  1972. param.stride_w = 1;
  1973. param.pad_h = 0;
  1974. param.pad_w = 0;
  1975. param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1976. auto bench_case = [&](size_t OC, size_t IC, size_t H, size_t W) {
  1977. if (pack_size == 1)
  1978. args.emplace_back(
  1979. param, TensorShape{1, IC, H, W}, TensorShape{OC, IC, 1, 1},
  1980. TensorShape{});
  1981. else {
  1982. if (pack_size == 4)
  1983. param.format = param::ConvBias::Format::NCHW44;
  1984. args.emplace_back(
  1985. param, TensorShape{1, IC / pack_size, H, W, pack_size},
  1986. TensorShape{
  1987. OC / pack_size, IC / pack_size, 1, 1, pack_size, pack_size},
  1988. TensorShape{});
  1989. }
  1990. };
  1991. //! MobileNetV1
  1992. bench_case(64, 32, 112, 112);
  1993. bench_case(128, 64, 56, 56);
  1994. bench_case(128, 128, 56, 56);
  1995. bench_case(256, 128, 28, 28);
  1996. bench_case(256, 256, 28, 28);
  1997. bench_case(512, 256, 14, 14);
  1998. bench_case(512, 512, 14, 14);
  1999. bench_case(1024, 512, 7, 7);
  2000. bench_case(1024, 1024, 7, 7);
  2001. //! MobileNetV2
  2002. bench_case(16, 32, 112, 112);
  2003. bench_case(96, 16, 112, 112);
  2004. bench_case(144, 24, 56, 56);
  2005. bench_case(192, 32, 28, 28);
  2006. bench_case(384, 64, 28, 28);
  2007. bench_case(576, 96, 14, 14);
  2008. bench_case(960, 160, 7, 7);
  2009. bench_case(320, 960, 7, 7);
  2010. bench_case(1280, 320, 7, 7);
  2011. //! MobileNetV3-Large
  2012. bench_case(64, 16, 112, 112);
  2013. bench_case(72, 24, 56, 56);
  2014. bench_case(120, 40, 28, 28);
  2015. bench_case(240, 40, 28, 28);
  2016. bench_case(200, 80, 14, 14);
  2017. bench_case(184, 80, 14, 14);
  2018. bench_case(480, 80, 14, 14);
  2019. bench_case(672, 112, 14, 14);
  2020. //! MobileNetV3-Small
  2021. bench_case(72, 16, 56, 56);
  2022. bench_case(88, 24, 28, 28);
  2023. bench_case(96, 24, 28, 28);
  2024. bench_case(240, 40, 14, 14);
  2025. bench_case(120, 40, 14, 14);
  2026. bench_case(144, 48, 14, 14);
  2027. bench_case(288, 48, 14, 14);
  2028. bench_case(576, 96, 7, 7);
  2029. //! resnet50
  2030. bench_case(256, 64, 56, 56);
  2031. bench_case(512, 128, 28, 28);
  2032. bench_case(1024, 256, 14, 14);
  2033. bench_case(2048, 512, 7, 7);
  2034. return args;
  2035. }
  2036. void benchmark_conv1x1(
  2037. const char* matmul_algo_name, Handle* handle, DType stype, DType matmul_dtype,
  2038. DType bias_type, DType conv_dtype, bool is_mk4 = false) {
  2039. using namespace conv_bias;
  2040. int pack_size = is_mk4 ? 4 : 1;
  2041. std::vector<TestArg> conv_bias_1x1_args =
  2042. get_conv_bias_1x1_benchmark_args(pack_size);
  2043. constexpr size_t RUNS = 50;
  2044. param::MatrixMul param;
  2045. param.transposeA = false;
  2046. param.transposeB = false;
  2047. if (is_mk4) {
  2048. param.format = MatrixMul::Param::Format::MK4;
  2049. }
  2050. Benchmarker<MatrixMul> benchmark_matmul(handle);
  2051. benchmark_matmul.set_before_exec_callback(AlgoChecker<MatrixMul>(matmul_algo_name));
  2052. benchmark_matmul.set_times(RUNS)
  2053. .set_dtype(0, stype)
  2054. .set_dtype(1, stype)
  2055. .set_dtype(2, matmul_dtype)
  2056. .set_param(param)
  2057. .set_display(false);
  2058. std::string conv1x1_algo_name = ssprintf("CONV1x1:%s:24", matmul_algo_name);
  2059. Benchmarker<ConvBias> benchmark_conv1x1(handle);
  2060. benchmark_conv1x1.set_before_exec_callback(
  2061. conv_bias::ConvBiasAlgoChecker<ConvBias>(conv1x1_algo_name.c_str()));
  2062. benchmark_conv1x1.set_times(RUNS)
  2063. .set_dtype(0, stype)
  2064. .set_dtype(1, stype)
  2065. .set_dtype(2, bias_type)
  2066. .set_dtype(4, conv_dtype)
  2067. .set_display(false);
  2068. for (auto&& arg : conv_bias_1x1_args) {
  2069. size_t IC = arg.src[1];
  2070. size_t OH = arg.src[2];
  2071. size_t OW = arg.src[3];
  2072. size_t OC = arg.filter[0];
  2073. size_t M = OC * pack_size;
  2074. size_t K = IC * pack_size;
  2075. size_t N = OH * OW;
  2076. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2077. TensorShape A, B;
  2078. A = TensorShape{M, K};
  2079. B = TensorShape{K, N};
  2080. if (is_mk4) {
  2081. A = TensorShape{M / 4, K / 4, 4, 4};
  2082. B = TensorShape{K / 4, N, 4};
  2083. }
  2084. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  2085. {arg.src, arg.filter, arg.bias, {}, {}}) /
  2086. RUNS;
  2087. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  2088. printf("%s %s:\n matmul: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  2089. "speedup: "
  2090. "%f\n",
  2091. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), matmul_used,
  2092. computations / matmul_used, conv1x1_used, computations / conv1x1_used,
  2093. matmul_used / conv1x1_used);
  2094. }
  2095. }
  2096. } // namespace
  2097. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F32) {
  2098. #if MEGDNN_AARCH64
  2099. benchmark_conv1x1(
  2100. "AARCH64_F32K8X12X1", handle(), dtype::Float32{}, dtype::Float32{},
  2101. dtype::Float32{}, dtype::Float32{});
  2102. #else
  2103. benchmark_conv1x1(
  2104. "ARMV7_F32", handle(), dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
  2105. dtype::Float32{});
  2106. #endif
  2107. }
  2108. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  2109. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F16) {
  2110. #if MEGDNN_AARCH64
  2111. benchmark_conv1x1(
  2112. "AARCH64_F16_K8X24X1", handle(), dtype::Float16{}, dtype::Float16{},
  2113. dtype::Float16{}, dtype::Float16{});
  2114. #else
  2115. benchmark_conv1x1(
  2116. "AARCH32_F16_K4X16X1", handle(), dtype::Float16{}, dtype::Float16{},
  2117. dtype::Float16{}, dtype::Float16{});
  2118. #endif
  2119. }
  2120. #endif
  2121. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDSYM) {
  2122. dtype::QuantizedS8 stype(2.5f);
  2123. dtype::QuantizedS32 dtype(6.25f);
  2124. #if MEGDNN_AARCH64
  2125. #if MGB_ENABLE_DOT
  2126. benchmark_conv1x1(
  2127. "AARCH64_INT8X8X32_K8X12X4_DOTPROD", handle(), stype, dtype, dtype, dtype);
  2128. #else
  2129. benchmark_conv1x1("AARCH64_INT8X8X32_K8X8X8", handle(), stype, dtype, dtype, dtype);
  2130. benchmark_conv1x1(
  2131. "AARCH64_INT8X8X32_K4X4X16", handle(), stype, dtype, dtype, dtype);
  2132. #endif
  2133. #elif MEGDNN_ARMV7
  2134. benchmark_conv1x1("ARMV7_INT8X8X32_K4X8X8", handle(), stype, dtype, dtype, dtype);
  2135. #endif
  2136. }
  2137. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDASYM) {
  2138. dtype::Quantized8Asymm stype(1.2f, (uint8_t)125);
  2139. dtype::QuantizedS32 dtype(1.2 * 1.2);
  2140. #if MEGDNN_AARCH64
  2141. #if MGB_ENABLE_DOT
  2142. benchmark_conv1x1(
  2143. "AARCH64_QUINT8_K8X8X4_DOTPROD", handle(), stype, dtype, dtype, dtype);
  2144. #else
  2145. benchmark_conv1x1("AARCH64_QUINT8_K8X8X8", handle(), stype, dtype, dtype, dtype);
  2146. #endif
  2147. #elif MEGDNN_ARMV7
  2148. benchmark_conv1x1("ARMV7_QUINT8_K4X8X8", handle(), stype, dtype, dtype, dtype);
  2149. #endif
  2150. }
  2151. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_INT8x8x16) {
  2152. #if MEGDNN_AARCH64
  2153. benchmark_conv1x1(
  2154. "AARCH64_INT8X8X16_K8X8X8", handle(), dtype::Int8{}, dtype::Int16{},
  2155. dtype::Int16{}, dtype::Int16{});
  2156. benchmark_conv1x1(
  2157. "AARCH64_INT8X8X16_K4X4X16", handle(), dtype::Int8{}, dtype::Int16{},
  2158. dtype::Int16{}, dtype::Int16{});
  2159. #elif MEGDNN_ARMV7
  2160. benchmark_conv1x1(
  2161. "ARMV7_INT8X8X16_K4X8X8", handle(), dtype::Int8{}, dtype::Int16{},
  2162. dtype::Int16{}, dtype::Int16{});
  2163. benchmark_conv1x1(
  2164. "ARMV7_INT8X8X16_K4X2X16", handle(), dtype::Int8{}, dtype::Int16{},
  2165. dtype::Int16{}, dtype::Int16{});
  2166. benchmark_conv1x1(
  2167. "ARMV7_INT8X8X16_MK4_K8X8X4", handle(), dtype::Int8{}, dtype::Int16{},
  2168. dtype::Int16{}, dtype::Int16{}, true);
  2169. #endif
  2170. }
  2171. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_GEMV_FP32) {
  2172. using namespace conv_bias;
  2173. std::vector<conv_bias::TestArg> args;
  2174. param::ConvBias conv_param;
  2175. conv_param.stride_h = 1;
  2176. conv_param.stride_w = 1;
  2177. conv_param.pad_h = 0;
  2178. conv_param.pad_w = 0;
  2179. conv_param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  2180. auto run = [&](size_t M, size_t K) {
  2181. args.emplace_back(
  2182. conv_param, TensorShape{1, K, 1, 1}, TensorShape{M, K, 1, 1},
  2183. TensorShape{});
  2184. };
  2185. for (size_t M : {4, 64, 1024, 4096})
  2186. for (size_t K : {128, 256, 1024, 4096})
  2187. run(M, K);
  2188. constexpr size_t RUNS = 50;
  2189. param::MatrixMul param;
  2190. param.transposeA = false;
  2191. param.transposeB = false;
  2192. Benchmarker<MatrixMul> benchmark_matmul(handle());
  2193. benchmark_matmul.set_before_exec_callback(
  2194. AlgoChecker<MatrixMul>("ARM_COMMON_F32_GEMV"));
  2195. benchmark_matmul.set_times(RUNS)
  2196. .set_dtype(0, dtype::Float32{})
  2197. .set_dtype(1, dtype::Float32{})
  2198. .set_dtype(2, dtype::Float32{})
  2199. .set_param(param)
  2200. .set_display(false);
  2201. Benchmarker<ConvBias> benchmark_conv1x1(handle());
  2202. benchmark_conv1x1.set_before_exec_callback(
  2203. conv_bias::ConvBiasAlgoChecker<ConvBias>("CONV1x1_GEMV"));
  2204. benchmark_conv1x1.set_times(RUNS)
  2205. .set_dtype(0, dtype::Float32{})
  2206. .set_dtype(1, dtype::Float32{})
  2207. .set_dtype(2, dtype::Float32{})
  2208. .set_dtype(4, dtype::Float32{})
  2209. .set_display(false);
  2210. std::cout << "warm up:\n";
  2211. for (int i = 0; i < 50; i++) {
  2212. benchmark_matmul.exec({{1, 1024}, {1024, 512}, {}});
  2213. benchmark_matmul.set_display(true);
  2214. }
  2215. for (auto&& arg : args) {
  2216. size_t IC = arg.src[1];
  2217. size_t OH = arg.src[2];
  2218. size_t OW = arg.src[3];
  2219. size_t OC = arg.filter[0];
  2220. size_t M = OC;
  2221. size_t K = IC;
  2222. size_t N = OH * OW;
  2223. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2224. TensorShape A, B;
  2225. A = TensorShape{M, K};
  2226. B = TensorShape{K, N};
  2227. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  2228. {arg.src, arg.filter, arg.bias, {}, {}}) /
  2229. RUNS;
  2230. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  2231. printf("%s %s:\n gemv: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  2232. "speedup: "
  2233. "%f\n",
  2234. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), matmul_used,
  2235. computations / matmul_used, conv1x1_used, computations / conv1x1_used,
  2236. matmul_used / conv1x1_used);
  2237. }
  2238. }
  2239. //! enable none dot algo now
  2240. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_1X1_S1_NCHW_VS_NCHW44_INT8x8x32) {
  2241. std::vector<TestArg> conv_bias_1x1_args_nchw44 =
  2242. get_conv_bias_1x1_benchmark_args(4);
  2243. std::vector<TestArg> conv_bias_1x1_args_nchw = get_conv_bias_1x1_benchmark_args(1);
  2244. constexpr size_t RUNS = 50;
  2245. Benchmarker<ConvBias> benchmark_conv1x1_nchw44(handle());
  2246. benchmark_conv1x1_nchw44.set_before_exec_callback(
  2247. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2248. "CONV1x1:AARCH64_INT8X8X32_MK4_4X4X16:24"));
  2249. benchmark_conv1x1_nchw44.set_times(RUNS)
  2250. .set_dtype(0, dtype::Int8())
  2251. .set_dtype(1, dtype::Int8())
  2252. .set_dtype(2, dtype::Int32())
  2253. .set_dtype(4, dtype::Int32())
  2254. .set_display(false);
  2255. Benchmarker<ConvBias> benchmark_conv1x1_nchw(handle());
  2256. benchmark_conv1x1_nchw.set_before_exec_callback(
  2257. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2258. "CONV1x1:AARCH64_INT8X8X32_K4X4X16:24"));
  2259. benchmark_conv1x1_nchw.set_times(RUNS)
  2260. .set_dtype(0, dtype::Int8())
  2261. .set_dtype(1, dtype::Int8())
  2262. .set_dtype(2, dtype::Int32())
  2263. .set_dtype(4, dtype::Int32())
  2264. .set_display(false);
  2265. for (size_t i = 0; i < conv_bias_1x1_args_nchw44.size(); ++i) {
  2266. auto&& arg_nchw = conv_bias_1x1_args_nchw[i];
  2267. auto&& arg_nchw44 = conv_bias_1x1_args_nchw44[i];
  2268. size_t IC = arg_nchw.src[1];
  2269. size_t OH = arg_nchw.src[2];
  2270. size_t OW = arg_nchw.src[3];
  2271. size_t OC = arg_nchw.filter[0];
  2272. size_t M = OC;
  2273. size_t K = IC;
  2274. size_t N = OH * OW;
  2275. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2276. auto conv1x1_nchw =
  2277. benchmark_conv1x1_nchw.set_param(arg_nchw.param)
  2278. .exec({arg_nchw.src, arg_nchw.filter, arg_nchw.bias, {}, {}}) /
  2279. RUNS;
  2280. auto conv1x1_nchw44 = benchmark_conv1x1_nchw44.set_param(arg_nchw44.param)
  2281. .exec({arg_nchw44.src,
  2282. arg_nchw44.filter,
  2283. arg_nchw44.bias,
  2284. {},
  2285. {}}) /
  2286. RUNS;
  2287. printf("%s %s:\n conv_1x1_nchw: %f ms %f Gflops\nconv1x1_nchw44: %f ms "
  2288. "%f GFlops "
  2289. "speedup: "
  2290. "%f\n",
  2291. arg_nchw.src.to_string().c_str(), arg_nchw.filter.to_string().c_str(),
  2292. conv1x1_nchw, computations / conv1x1_nchw, conv1x1_nchw44,
  2293. computations / conv1x1_nchw44, conv1x1_nchw / conv1x1_nchw44);
  2294. }
  2295. }
  2296. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_WINOGRAD_VS_IM2COL_INT8) {
  2297. auto&& args = get_winograd_benchmark_args(3, 8);
  2298. using namespace conv_bias;
  2299. constexpr size_t RUN = 10;
  2300. Benchmarker<ConvBias> benchmark_im2col(handle());
  2301. benchmark_im2col.set_display(false);
  2302. benchmark_im2col.set_times(RUN);
  2303. benchmark_im2col.set_dtype(0, dtype::QuantizedS8(2.5f))
  2304. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2305. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2306. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2307. Benchmarker<ConvBias> benchmark_winograd(handle());
  2308. benchmark_winograd.set_display(false);
  2309. benchmark_winograd.set_times(RUN);
  2310. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  2311. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2312. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2313. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2314. for (auto&& arg : args) {
  2315. TensorLayout dst_layout;
  2316. auto opr = handle()->create_operator<ConvBias>();
  2317. opr->param() = arg.param;
  2318. opr->deduce_layout(
  2319. {arg.src, dtype::Float32()}, {arg.filter, dtype::Float32()},
  2320. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2321. //! dst.nr_elems * IC * FH * FW * 2
  2322. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2323. arg.filter[2] * arg.filter[3] * 2.0 /
  2324. (1024 * 1024 * 1024) * 1e3;
  2325. benchmark_im2col.set_param(arg.param);
  2326. auto im2col_used = algo_benchmark<ConvBias>(
  2327. benchmark_im2col, {arg.src, arg.filter, {}, {}, {}},
  2328. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16") /
  2329. RUN;
  2330. benchmark_winograd.set_param(arg.param);
  2331. auto winograd_used =
  2332. algo_benchmark<ConvBias>(
  2333. benchmark_winograd, {arg.src, arg.filter, {}, {}, {}},
  2334. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2") /
  2335. RUN;
  2336. printf("%s %s: im2col: %f ms %f Gflops winograd: %f ms %f GFlops "
  2337. "speedup: "
  2338. "%f\n",
  2339. arg.src.to_string().c_str(), arg.filter.to_string().c_str(), im2col_used,
  2340. computations / im2col_used, winograd_used, computations / winograd_used,
  2341. im2col_used / winograd_used);
  2342. }
  2343. }
  2344. #endif
  2345. // vim: syntax=cpp.doxygen