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

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

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