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

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

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