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

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

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