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

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

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