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

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

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