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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  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. #if MEGDNN_AARCH64
  507. TEST_F(ARM_COMMON, BENCHMARK_NCHW_VS_NCHW44_INT8x8x32) {
  508. printf("=========================compare "
  509. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16, "
  510. "IM2COLMATMUL:AARCH64_INT8X8X32_MK4_4X4X16 \n");
  511. BENCHMARK_IM2COL_NCHW44_VS_NCHW("IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16",
  512. "IM2COLMATMUL:AARCH64_INT8X8X32_MK4_4X4X16",
  513. handle(), 3, dtype::Int8(), dtype::Int32());
  514. }
  515. #endif
  516. #if MEGDNN_ARMV7
  517. TEST_F(ARM_COMMON, BENCHMARK_NCHW_VS_NCHW44_INT8x8x16) {
  518. const char* default_algo = "IM2COLMATMUL:ARMV7_INT8X8X16_K4X8X8";
  519. const char* mk4_algo = "IM2COLMATMUL:ARMV7_INT8X8X16_MK4_K8X8X4";
  520. printf("compare %s vs %s \n", default_algo, mk4_algo);
  521. BENCHMARK_IM2COL_NCHW44_VS_NCHW(default_algo, mk4_algo, handle(), 3,
  522. dtype::Int8(), dtype::Int16());
  523. }
  524. #endif
  525. TEST_F(ARM_COMMON, BENCHMARK_GROUP_CONVBIAS_QUANTIZED) {
  526. constexpr size_t RUNS = 50;
  527. param::ConvBias param;
  528. param.sparse = param::ConvBias::Sparse::GROUP;
  529. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  530. Benchmarker<ConvBias> benchmarker_int(handle());
  531. benchmarker_int.set_times(RUNS)
  532. .set_dtype(0, dtype::QuantizedS8(2.5f))
  533. .set_dtype(1, dtype::QuantizedS8(2.5f))
  534. .set_dtype(2, dtype::QuantizedS32(6.25f))
  535. .set_dtype(4, dtype::QuantizedS8(40.25f))
  536. .set_display(false);
  537. Benchmarker<ConvBias> benchmarker_float(handle());
  538. benchmarker_float.set_display(false).set_times(RUNS);
  539. auto run = [&](size_t N, size_t GROUP, size_t IC, size_t OC, size_t H,
  540. size_t W, size_t FS, size_t STRD) {
  541. megdnn_assert(IC % GROUP == 0 && OC % GROUP == 0);
  542. TensorShape src({N, IC, H, W}),
  543. filter({GROUP, OC / GROUP, IC / GROUP, FS, FS}),
  544. bias({1, OC, 1, 1}), dst({N, OC, H / STRD, W / STRD});
  545. param.pad_h = FS / 2;
  546. param.pad_w = FS / 2;
  547. param.stride_h = STRD;
  548. param.stride_w = STRD;
  549. auto int_used = benchmarker_int.set_param(param).exec(
  550. {src, filter, bias, {}, dst}) /
  551. RUNS;
  552. auto float_used = benchmarker_float.set_param(param).exec(
  553. {src, filter, bias, {}, dst}) /
  554. RUNS;
  555. float computations = (IC / GROUP * FS * FS * dst.total_nr_elems() * 2 +
  556. dst.total_nr_elems()) *
  557. 1e-6;
  558. printf("run: %s %s %s->%s \nfloat: %f ms %f Gflops int: %f ms "
  559. "%f Gflops speedup: %f\n",
  560. src.to_string().c_str(), filter.to_string().c_str(),
  561. bias.to_string().c_str(), dst.to_string().c_str(), float_used,
  562. computations / float_used, int_used, computations / int_used,
  563. float_used / int_used);
  564. };
  565. run(1, 1, 28, 28, 28, 28, 3, 1);
  566. run(1, 68, 68, 68, 14, 14, 3, 2);
  567. run(1, 96, 96, 96, 14, 14, 3, 2);
  568. run(1, 100, 100, 100, 7, 7, 3, 1);
  569. }
  570. #endif
  571. #if MEGDNN_WITH_BENCHMARK
  572. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_MATMUL) {
  573. constexpr size_t RUNS = 10;
  574. param::ConvBias param;
  575. param.stride_h = 1;
  576. param.stride_w = 1;
  577. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  578. Benchmarker<ConvBias> benchmarker(handle()), benchmarker_fused(handle());
  579. benchmarker.set_times(RUNS)
  580. .set_dtype(0, dtype::QuantizedS8(2.5f))
  581. .set_dtype(1, dtype::QuantizedS8(2.5f))
  582. .set_dtype(2, dtype::QuantizedS32(6.25f))
  583. .set_dtype(4, dtype::QuantizedS8(40.25f))
  584. .set_display(false);
  585. benchmarker_fused.set_times(RUNS)
  586. .set_dtype(0, dtype::QuantizedS8(2.5f))
  587. .set_dtype(1, dtype::QuantizedS8(2.5f))
  588. .set_dtype(2, dtype::QuantizedS32(6.25f))
  589. .set_dtype(4, dtype::QuantizedS8(40.25f))
  590. .set_display(false);
  591. benchmarker_fused.set_before_exec_callback(
  592. conv_bias::ConvBiasAlgoChecker<ConvBias>("S8MATMUL"));
  593. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  594. size_t FS) {
  595. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  596. bias({1, OC, 1, 1}), dst({N, OC, H, W});
  597. param.pad_h = FS / 2;
  598. param.pad_w = FS / 2;
  599. auto default_used = benchmarker.set_param(param).exec(
  600. {src, filter, bias, {}, dst}) /
  601. RUNS;
  602. auto fused_used = benchmarker_fused.set_param(param).exec(
  603. {src, filter, bias, {}, dst}) /
  604. RUNS;
  605. float computations =
  606. IC * (FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  607. printf("run: %s %s %s->%s \ndefault: %f ms %f Gflops fused: %f ms "
  608. "%f Gflops speedup: %f\n",
  609. src.to_string().c_str(), filter.to_string().c_str(),
  610. bias.to_string().c_str(), dst.to_string().c_str(), default_used,
  611. computations / default_used, fused_used,
  612. computations / fused_used, default_used / fused_used);
  613. };
  614. run(1, 128, 128, 32, 32, 3);
  615. for (size_t IC : {36, 48}) {
  616. for (size_t OC : {36, 48, 64}) {
  617. for (size_t size : {56, 128, 256}) {
  618. for (size_t FS : {1, 3, 5}) {
  619. run(1, IC, OC, size, size, FS);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. #endif
  626. #if MEGDNN_WITH_BENCHMARK
  627. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23) {
  628. #if MEGDNN_AARCH64
  629. benchmark_winograd("WINOGRAD:AARCH64_F32:1:2", handle(), 3);
  630. #else
  631. benchmark_winograd("WINOGRAD:ARMV7_F32_:1:2", handle(), 3);
  632. #endif
  633. }
  634. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_4x4) {
  635. #if MEGDNN_AARCH64
  636. benchmark_winograd("WINOGRAD:AARCH64_F32_MK4_4x16:4:2", handle(), 3, 4);
  637. #else
  638. benchmark_winograd("WINOGRAD:ARMV7_F32_MK4_4x8:4:2", handle(), 3, 4);
  639. #endif
  640. }
  641. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63) {
  642. #if MEGDNN_AARCH64
  643. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:6", handle(), 3);
  644. #else
  645. benchmark_winograd("WINOGRAD:ARMV7_F32:1:6", handle(), 3);
  646. #endif
  647. }
  648. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63_4x4) {
  649. #if MEGDNN_AARCH64
  650. benchmark_winograd("WINOGRAD:AARCH64_F32_MK4_4x16:4:6", handle(), 3, 4);
  651. #else
  652. benchmark_winograd("WINOGRAD:ARMV7_F32_MK4_4x8:4:6", handle(), 3, 4);
  653. #endif
  654. }
  655. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F54) {
  656. #if MEGDNN_AARCH64
  657. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:5", handle(), 4);
  658. #else
  659. benchmark_winograd("WINOGRAD:ARMV7_F32:1:5", handle(), 4);
  660. #endif
  661. }
  662. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F45) {
  663. #if MEGDNN_AARCH64
  664. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:4", handle(), 5);
  665. #else
  666. benchmark_winograd("WINOGRAD:ARMV7_F32:1:4", handle(), 5);
  667. #endif
  668. }
  669. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  670. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23) {
  671. #if MEGDNN_AARCH64
  672. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  673. "WINOGRAD:AARCH64_F16_K8X24X1:1:6", handle(), 3, 4);
  674. #else
  675. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:2",
  676. "WINOGRAD:AARCH32_F16_K4X16X1:1:2", handle(), 3);
  677. #endif
  678. }
  679. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F45) {
  680. #if MEGDNN_AARCH64
  681. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32K8X12X1:1:4",
  682. "WINOGRAD:AARCH64_F16_K8X24X1:1:4", handle(), 5);
  683. #else
  684. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:4",
  685. "WINOGRAD:AARCH32_F16_K4X16X1:1:4", handle(), 5);
  686. #endif
  687. }
  688. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F63) {
  689. #if MEGDNN_AARCH64
  690. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32K8X12X1:1:6",
  691. "WINOGRAD:AARCH64_F16_K8X24X1:1:6", handle(), 3);
  692. #else
  693. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:6",
  694. "WINOGRAD:AARCH32_F16_K4X16X1:1:6", handle(), 3);
  695. #endif
  696. }
  697. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23_8x8) {
  698. #if MEGDNN_AARCH64
  699. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  700. "WINOGRAD:AARCH64_F16_MK8_8X8:8:2", handle(), 3, 8);
  701. #else
  702. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32_MK4_4x8:4:2",
  703. "WINOGRAD:AARCH32_F16_MK8_4X8:8:2", handle(), 3, 8);
  704. #endif
  705. }
  706. #endif
  707. void benchmark_winograd_nchw_vs_nchw44(const char* algo_name, Handle* handle) {
  708. using namespace conv_bias;
  709. using NLMode = param::ConvBias::NonlineMode;
  710. std::vector<conv_bias::TestArg> args_nchw44;
  711. std::vector<conv_bias::TestArg> args_nchw;
  712. auto pack = [&](size_t n, size_t oc, size_t ic, size_t h, size_t w,
  713. size_t group, NLMode nlmode) {
  714. param::ConvBias param;
  715. param.format = param::ConvBias::Format::NCHW44;
  716. param.stride_h = 1;
  717. param.stride_w = 1;
  718. param.pad_h = 1;
  719. param.pad_w = 1;
  720. param.nonlineMode = nlmode;
  721. if (group == 1) {
  722. param.sparse = param::ConvBias::Sparse::DENSE;
  723. args_nchw44.emplace_back(param, TensorShape{n, ic / 4, h, w, 4},
  724. TensorShape{oc / 4, ic / 4, 3, 3, 4, 4},
  725. TensorShape{});
  726. param.format = param::ConvBias::Format::NCHW;
  727. args_nchw.emplace_back(param, TensorShape{n, ic, h, w},
  728. TensorShape{oc, ic, 3, 3}, TensorShape{});
  729. } else {
  730. auto oc_per_group = oc / group;
  731. auto ic_per_group = ic / group;
  732. param.sparse = param::ConvBias::Sparse::GROUP;
  733. args_nchw44.emplace_back(param,
  734. TensorShape{n, ic_per_group / 4, h, w, 4},
  735. TensorShape{group, oc_per_group / 4,
  736. ic_per_group / 4, 3, 3, 4, 4},
  737. TensorShape{});
  738. param.format = param::ConvBias::Format::NCHW;
  739. args_nchw.emplace_back(
  740. param, TensorShape{n, ic, h, w},
  741. TensorShape{group, oc_per_group, ic_per_group, 3, 3},
  742. TensorShape{});
  743. }
  744. };
  745. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  746. for (auto nlmode : nonlinemode)
  747. for (size_t n : {1})
  748. for (size_t group = 1; group <= 1; ++group) {
  749. pack(n, 512, 512, 15, 15, group, nlmode);
  750. pack(n, 512, 256, 15, 15, group, nlmode);
  751. pack(n, 256, 256, 29, 29, group, nlmode);
  752. pack(n, 256, 128, 29, 29, group, nlmode);
  753. pack(n, 128, 128, 57, 57, group, nlmode);
  754. pack(n, 128, 64, 57, 57, group, nlmode);
  755. pack(n, 24, 24, 224, 224, group, nlmode);
  756. pack(n, 64, 24, 123, 123, group, nlmode);
  757. pack(n, 64, 64, 56, 56, group, nlmode);
  758. pack(n, 128, 128, 28, 28, group, nlmode);
  759. pack(n, 256, 256, 14, 14, group, nlmode);
  760. pack(n, 512, 512, 7, 7, group, nlmode);
  761. }
  762. using namespace conv_bias;
  763. constexpr size_t RUN = 10;
  764. Benchmarker<ConvBias> benchmark_winograd_nchw(handle);
  765. benchmark_winograd_nchw.set_display(false);
  766. benchmark_winograd_nchw.set_times(RUN);
  767. Benchmarker<ConvBias> benchmark_winograd_nchw44(handle);
  768. benchmark_winograd_nchw44.set_display(false);
  769. benchmark_winograd_nchw44.set_times(RUN);
  770. std::string winograd_nchw_algo_name = ssprintf("WINOGRAD:%s", algo_name);
  771. std::string winograd_nchw44_algo_name =
  772. ssprintf("WINOGRAD_NCHW44:%s", algo_name);
  773. for (size_t i = 0; i < args_nchw.size(); ++i) {
  774. auto arg_nchw = args_nchw[i];
  775. auto arg_nchw44 = args_nchw44[i];
  776. TensorLayout dst_layout;
  777. auto opr = handle->create_operator<ConvBias>();
  778. opr->param() = arg_nchw.param;
  779. opr->deduce_layout({arg_nchw.src, dtype::Float32()},
  780. {arg_nchw.filter, dtype::Float32()},
  781. {arg_nchw.bias, dtype::Float32()}, {}, dst_layout);
  782. //! dst.nr_elems * IC * FH * FW * 2
  783. float computations = dst_layout.total_nr_elems() * arg_nchw.filter[1] *
  784. arg_nchw.filter[2] * arg_nchw.filter[3] * 2.0 /
  785. (1024 * 1024 * 1024) * 1e3;
  786. benchmark_winograd_nchw.set_param(arg_nchw.param);
  787. auto nchw_used = algo_benchmark<ConvBias>(
  788. benchmark_winograd_nchw,
  789. {arg_nchw.src, arg_nchw.filter, {}, {}, {}},
  790. winograd_nchw_algo_name.c_str()) /
  791. RUN;
  792. benchmark_winograd_nchw44.set_param(arg_nchw44.param);
  793. auto nchw44_used =
  794. algo_benchmark<ConvBias>(
  795. benchmark_winograd_nchw44,
  796. {arg_nchw44.src, arg_nchw44.filter, {}, {}, {}},
  797. winograd_nchw44_algo_name.c_str()) /
  798. RUN;
  799. printf("%s %s: nchw: %f ms %f Gflops nchw44: %f ms %f GFlops "
  800. "speedup: "
  801. "%f\n",
  802. arg_nchw.src.to_string().c_str(),
  803. arg_nchw.filter.to_string().c_str(), nchw_used,
  804. computations / nchw_used, nchw44_used,
  805. computations / nchw44_used, nchw_used / nchw44_used);
  806. }
  807. }
  808. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_MK4_NCHW_VS_NCHW44) {
  809. #if MEGDNN_AARCH64
  810. benchmark_winograd_nchw_vs_nchw44("AARCH64_F32_MK4_4x16:4:2", handle());
  811. #else
  812. benchmark_winograd_nchw_vs_nchw44("ARMV7_F32_MK4_4x8:4:2", handle());
  813. #endif
  814. }
  815. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63_MK4_NCHW_VS_NCHW44) {
  816. #if MEGDNN_AARCH64
  817. benchmark_winograd_nchw_vs_nchw44("AARCH64_F32_MK4_4x16:4:6", handle());
  818. #else
  819. benchmark_winograd_nchw_vs_nchw44("ARMV7_F32_MK4_4x8:4:6", handle());
  820. #endif
  821. }
  822. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  823. auto benchmark_winograd_quantized = [](const char* algo_name_fp32,
  824. const char* algo_name_quantized,
  825. Handle* handle, size_t kernel) {
  826. auto&& args = get_winograd_benchmark_args(kernel);
  827. using namespace conv_bias;
  828. constexpr size_t RUN = 10;
  829. Benchmarker<ConvBias> benchmark(handle);
  830. benchmark.set_display(false);
  831. benchmark.set_times(RUN);
  832. Benchmarker<ConvBias> benchmark_winograd(handle);
  833. benchmark_winograd.set_display(false).set_times(RUN);
  834. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  835. .set_dtype(1, dtype::QuantizedS8(2.5f))
  836. .set_dtype(2, dtype::QuantizedS32(6.25f))
  837. .set_dtype(4, dtype::QuantizedS8(60.25f));
  838. for (auto&& arg : args) {
  839. TensorLayout dst_layout;
  840. auto opr = handle->create_operator<ConvBias>();
  841. opr->param() = arg.param;
  842. opr->deduce_layout({arg.src, dtype::Float32()},
  843. {arg.filter, dtype::Float32()},
  844. {arg.bias, dtype::Float32()}, {}, dst_layout);
  845. //! dst.nr_elems * IC * FH * FW * 2
  846. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  847. arg.filter[2] * arg.filter[3] * 2.0 /
  848. (1024 * 1024 * 1024) * 1e3;
  849. benchmark.set_param(arg.param);
  850. auto used = algo_benchmark<ConvBias>(
  851. benchmark, {arg.src, arg.filter, {}, {}, {}},
  852. algo_name_fp32) /
  853. RUN;
  854. benchmark_winograd.set_param(arg.param);
  855. auto used_winograd =
  856. algo_benchmark<ConvBias>(benchmark_winograd,
  857. {arg.src, arg.filter, {}, {}, {}},
  858. algo_name_quantized) /
  859. RUN;
  860. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  861. "speedup: "
  862. "%f\n",
  863. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  864. used, computations / used, used_winograd,
  865. computations / used_winograd, used / used_winograd);
  866. }
  867. };
  868. #if MEGDNN_AARCH64
  869. benchmark_winograd_quantized("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  870. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2",
  871. handle(), 3);
  872. #else
  873. benchmark_winograd_quantized("WINOGRAD:ARMV7_F32_MK4_4x8:4:2",
  874. "WINOGRAD:ARMV7_INT16X16X32_MK8_4X8:8:2",
  875. handle(), 3);
  876. #endif
  877. }
  878. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1) {
  879. // have to remove preferred restrict in usable func before run the benchmark
  880. using namespace conv_bias;
  881. std::vector<TestArg> args;
  882. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  883. size_t p, NonlineMode nonline_mode) {
  884. if (w + 2 * p < kernel || h + 2 * p < kernel)
  885. return;
  886. param::ConvBias param;
  887. param.stride_h = 1;
  888. param.stride_w = 1;
  889. param.pad_h = p;
  890. param.pad_w = p;
  891. param.nonlineMode = nonline_mode;
  892. //! channel bias
  893. args.emplace_back(param, TensorShape{2, ic, h, w},
  894. TensorShape{oc, ic, kernel, kernel},
  895. TensorShape{1, oc, 1, 1});
  896. };
  897. for (size_t kernel : {2, 3, 5, 7})
  898. for (size_t ic : {1, 8, 16, 32})
  899. for (size_t oc : {1, 8, 16, 32})
  900. for (size_t p : {1})
  901. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  902. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  903. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  904. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  905. }
  906. constexpr size_t RUN = 50;
  907. Benchmarker<ConvBias> benchmark0(handle());
  908. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  909. .set_dtype(1, dtype::QuantizedS8(2.5f))
  910. .set_dtype(2, dtype::QuantizedS32(6.25f))
  911. .set_dtype(4, dtype::QuantizedS8(60.25f));
  912. benchmark0.set_display(false);
  913. benchmark0.set_times(RUN);
  914. benchmark0.set_before_exec_callback(
  915. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD1"));
  916. Benchmarker<ConvBias> benchmark1(handle());
  917. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  918. .set_dtype(1, dtype::QuantizedS8(2.5f))
  919. .set_dtype(2, dtype::QuantizedS32(6.25f))
  920. .set_dtype(4, dtype::QuantizedS8(60.25f));
  921. benchmark1.set_display(false);
  922. benchmark1.set_times(RUN);
  923. for (auto&& arg : args) {
  924. TensorLayout dst_layout;
  925. auto opr = handle()->create_operator<ConvBias>();
  926. opr->param() = arg.param;
  927. opr->deduce_layout({arg.src, dtype::Int8()},
  928. {arg.filter, dtype::Int8()},
  929. {arg.bias, dtype::Int32()}, {}, dst_layout);
  930. //! dst.nr_elems * IC * FH * FW * 2
  931. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  932. arg.filter[2] * arg.filter[3] * 2.0 /
  933. (1024 * 1024 * 1024) * 1e3;
  934. auto used0 = benchmark0.set_param(arg.param).exec(
  935. {arg.src, arg.filter, arg.bias, {}, {}}) /
  936. RUN;
  937. auto used1 = benchmark1.set_param(arg.param).exec(
  938. {arg.src, arg.filter, arg.bias, {}, {}}) /
  939. RUN;
  940. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  941. "speedup: %f\n",
  942. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  943. used0, computations / used0, used1, computations / used1,
  944. used1 / used0);
  945. }
  946. }
  947. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2) {
  948. // have to remove preferred restrict in usable func before run the benchmark
  949. using namespace conv_bias;
  950. std::vector<TestArg> args;
  951. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  952. size_t p, NonlineMode nonline_mode) {
  953. if (w + 2 * p < kernel || h + 2 * p < kernel)
  954. return;
  955. param::ConvBias param;
  956. param.stride_h = 2;
  957. param.stride_w = 2;
  958. param.pad_h = p;
  959. param.pad_w = p;
  960. param.nonlineMode = nonline_mode;
  961. //! channel bias
  962. args.emplace_back(param, TensorShape{2, ic, h, w},
  963. TensorShape{oc, ic, kernel, kernel},
  964. TensorShape{1, oc, 1, 1});
  965. };
  966. for (size_t kernel : {2, 3, 5, 7})
  967. for (size_t ic : {1, 8, 16, 32})
  968. for (size_t oc : {1, 8, 16, 32})
  969. for (size_t p : {1})
  970. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  971. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  972. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  973. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  974. }
  975. constexpr size_t RUN = 50;
  976. Benchmarker<ConvBias> benchmark0(handle());
  977. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  978. .set_dtype(1, dtype::QuantizedS8(2.5f))
  979. .set_dtype(2, dtype::QuantizedS32(6.25f))
  980. .set_dtype(4, dtype::QuantizedS8(60.25f));
  981. benchmark0.set_display(false);
  982. benchmark0.set_times(RUN);
  983. benchmark0.set_before_exec_callback(
  984. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD2"));
  985. Benchmarker<ConvBias> benchmark1(handle());
  986. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  987. .set_dtype(1, dtype::QuantizedS8(2.5f))
  988. .set_dtype(2, dtype::QuantizedS32(6.25f))
  989. .set_dtype(4, dtype::QuantizedS8(60.25f));
  990. benchmark1.set_display(false);
  991. benchmark1.set_times(RUN);
  992. for (auto&& arg : args) {
  993. TensorLayout dst_layout;
  994. auto opr = handle()->create_operator<ConvBias>();
  995. opr->param() = arg.param;
  996. opr->deduce_layout({arg.src, dtype::Int8()},
  997. {arg.filter, dtype::Int8()},
  998. {arg.bias, dtype::Int32()}, {}, dst_layout);
  999. //! dst.nr_elems * IC * FH * FW * 2
  1000. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1001. arg.filter[2] * arg.filter[3] * 2.0 /
  1002. (1024 * 1024 * 1024) * 1e3;
  1003. auto used0 = benchmark0.set_param(arg.param).exec(
  1004. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1005. RUN;
  1006. auto used1 = benchmark1.set_param(arg.param).exec(
  1007. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1008. RUN;
  1009. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1010. "speedup: %f\n",
  1011. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1012. used0, computations / used0, used1, computations / used1,
  1013. used1 / used0);
  1014. }
  1015. }
  1016. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1) {
  1017. // have to remove preferred restrict in usable func before run the benchmark
  1018. using namespace conv_bias;
  1019. std::vector<TestArg> args;
  1020. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1021. size_t p, NonlineMode nonline_mode) {
  1022. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1023. return;
  1024. param::ConvBias param;
  1025. param.stride_h = 1;
  1026. param.stride_w = 1;
  1027. param.pad_h = p;
  1028. param.pad_w = p;
  1029. param.nonlineMode = nonline_mode;
  1030. //! channel bias
  1031. args.emplace_back(param, TensorShape{2, ic, h, w},
  1032. TensorShape{oc, ic, kernel, kernel},
  1033. TensorShape{1, oc, 1, 1});
  1034. };
  1035. for (size_t kernel : {2, 3, 5, 7})
  1036. for (size_t ic : {1, 8, 16, 32})
  1037. for (size_t oc : {1, 8, 16, 32})
  1038. for (size_t p : {1})
  1039. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1040. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1041. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1042. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1043. }
  1044. constexpr size_t RUN = 50;
  1045. Benchmarker<ConvBias> benchmark0(handle());
  1046. benchmark0
  1047. .set_dtype(0,
  1048. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1049. .set_dtype(1,
  1050. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1051. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1052. .set_dtype(4,
  1053. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1054. benchmark0.set_display(false);
  1055. benchmark0.set_times(RUN);
  1056. benchmark0.set_before_exec_callback(
  1057. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD1"));
  1058. Benchmarker<ConvBias> benchmark1(handle());
  1059. benchmark1
  1060. .set_dtype(0,
  1061. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1062. .set_dtype(1,
  1063. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1064. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1065. .set_dtype(4,
  1066. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1067. benchmark1.set_display(false);
  1068. benchmark1.set_times(RUN);
  1069. for (auto&& arg : args) {
  1070. TensorLayout dst_layout;
  1071. auto opr = handle()->create_operator<ConvBias>();
  1072. opr->param() = arg.param;
  1073. opr->deduce_layout({arg.src, dtype::Int8()},
  1074. {arg.filter, dtype::Int8()},
  1075. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1076. //! dst.nr_elems * IC * FH * FW * 2
  1077. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1078. arg.filter[2] * arg.filter[3] * 2.0 /
  1079. (1024 * 1024 * 1024) * 1e3;
  1080. auto used0 = benchmark0.set_param(arg.param).exec(
  1081. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1082. RUN;
  1083. auto used1 = benchmark1.set_param(arg.param).exec(
  1084. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1085. RUN;
  1086. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1087. "speedup: %f\n",
  1088. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1089. used0, computations / used0, used1, computations / used1,
  1090. used1 / used0);
  1091. }
  1092. }
  1093. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2) {
  1094. // have to remove preferred restrict in usable func before run the benchmark
  1095. using namespace conv_bias;
  1096. std::vector<TestArg> args;
  1097. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1098. size_t p, NonlineMode nonline_mode) {
  1099. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1100. return;
  1101. param::ConvBias param;
  1102. param.stride_h = 2;
  1103. param.stride_w = 2;
  1104. param.pad_h = p;
  1105. param.pad_w = p;
  1106. param.nonlineMode = nonline_mode;
  1107. //! channel bias
  1108. args.emplace_back(param, TensorShape{2, ic, h, w},
  1109. TensorShape{oc, ic, kernel, kernel},
  1110. TensorShape{1, oc, 1, 1});
  1111. };
  1112. for (size_t kernel : {2, 3, 5, 7})
  1113. for (size_t ic : {1, 8, 16, 32})
  1114. for (size_t oc : {1, 8, 16, 32})
  1115. for (size_t p : {1})
  1116. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1117. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1118. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1119. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1120. }
  1121. constexpr size_t RUN = 50;
  1122. Benchmarker<ConvBias> benchmark0(handle());
  1123. benchmark0
  1124. .set_dtype(0,
  1125. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1126. .set_dtype(1,
  1127. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1128. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1129. .set_dtype(4,
  1130. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1131. benchmark0.set_display(false);
  1132. benchmark0.set_times(RUN);
  1133. benchmark0.set_before_exec_callback(
  1134. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD2"));
  1135. Benchmarker<ConvBias> benchmark1(handle());
  1136. benchmark1
  1137. .set_dtype(0,
  1138. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1139. .set_dtype(1,
  1140. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1141. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1142. .set_dtype(4,
  1143. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1144. benchmark1.set_display(false);
  1145. benchmark1.set_times(RUN);
  1146. for (auto&& arg : args) {
  1147. TensorLayout dst_layout;
  1148. auto opr = handle()->create_operator<ConvBias>();
  1149. opr->param() = arg.param;
  1150. opr->deduce_layout({arg.src, dtype::Int8()},
  1151. {arg.filter, dtype::Int8()},
  1152. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1153. //! dst.nr_elems * IC * FH * FW * 2
  1154. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1155. arg.filter[2] * arg.filter[3] * 2.0 /
  1156. (1024 * 1024 * 1024) * 1e3;
  1157. auto used0 = benchmark0.set_param(arg.param).exec(
  1158. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1159. RUN;
  1160. auto used1 = benchmark1.set_param(arg.param).exec(
  1161. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1162. RUN;
  1163. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1164. "speedup: %f\n",
  1165. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1166. used0, computations / used0, used1, computations / used1,
  1167. used1 / used0);
  1168. }
  1169. }
  1170. TEST_F(ARM_COMMON, BENCHMARK_CHANNEL_WISE_F32_STRIDE1_NCHW44) {
  1171. // have to remove preferred restrict in usable func before run the benchmark
  1172. using namespace conv_bias;
  1173. param::ConvBias param;
  1174. param.stride_h = 1;
  1175. param.stride_w = 1;
  1176. param.pad_h = 1;
  1177. param.pad_w = 1;
  1178. param.nonlineMode = NonlineMode::RELU;
  1179. param.sparse = param::ConvBias::Sparse::GROUP;
  1180. constexpr size_t RUN = 50;
  1181. Benchmarker<ConvBias> benchmark0(handle());
  1182. benchmark0.set_display(false);
  1183. benchmark0.set_param(param);
  1184. benchmark0.set_times(RUN);
  1185. benchmark0.set_before_exec_callback(
  1186. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1187. "F32STRD1_LARGE_GROUP"));
  1188. auto opr = handle()->create_operator<ConvBias>();
  1189. opr->param() = param;
  1190. param.format = param::ConvBias::Format::NCHW44;
  1191. Benchmarker<ConvBias> benchmark1(handle());
  1192. benchmark1.set_display(false);
  1193. benchmark1.set_param(param);
  1194. benchmark1.set_times(RUN);
  1195. benchmark1.set_before_exec_callback(
  1196. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1197. "F32_CHANNEL_WISE_NCHW44"));
  1198. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1199. TensorLayout dst_layout;
  1200. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1201. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1202. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1203. dst_layout);
  1204. //! dst.nr_elems * IC * FH * FW * 2
  1205. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1206. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1207. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1208. {group * 4, 1, 1, kernel, kernel},
  1209. {1, group * 4, 1, 1},
  1210. {},
  1211. {}}) /
  1212. RUN;
  1213. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1214. {group, 1, 1, kernel, kernel, 4},
  1215. {1, group, 1, 1, 4},
  1216. {},
  1217. {}}) /
  1218. RUN;
  1219. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1220. "nchw44: "
  1221. "%f ms %f GFlops "
  1222. "speedup: %f\n",
  1223. group, h, w, kernel, used0, computations / used0, used1,
  1224. computations / used1, used0 / used1);
  1225. };
  1226. for (size_t group : {8, 16, 32, 64}) {
  1227. for (size_t kerenl : {2, 3, 5}) {
  1228. run(group, 112, 112, kerenl);
  1229. run(group, 56, 56, kerenl);
  1230. run(group, 48, 48, kerenl);
  1231. run(group, 28, 28, kerenl);
  1232. run(group, 14, 14, kerenl);
  1233. }
  1234. }
  1235. run(8, 112, 112, 3);
  1236. run(32, 56, 56, 3);
  1237. run(64, 28, 28, 3);
  1238. run(128, 14, 14, 3);
  1239. }
  1240. TEST_F(ARM_COMMON, BENCHMARK_CHANNEL_WISE_F32_STRIDE2_NCHW44) {
  1241. // have to remove preferred restrict in usable func before run the benchmark
  1242. using namespace conv_bias;
  1243. param::ConvBias param;
  1244. param.stride_h = 2;
  1245. param.stride_w = 2;
  1246. param.pad_h = 1;
  1247. param.pad_w = 1;
  1248. param.nonlineMode = NonlineMode::RELU;
  1249. param.sparse = param::ConvBias::Sparse::GROUP;
  1250. constexpr size_t RUN = 50;
  1251. Benchmarker<ConvBias> benchmark0(handle());
  1252. benchmark0.set_display(false);
  1253. benchmark0.set_param(param);
  1254. benchmark0.set_times(RUN);
  1255. benchmark0.set_before_exec_callback(
  1256. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1257. "F32STRD2_LARGE_GROUP"));
  1258. auto opr = handle()->create_operator<ConvBias>();
  1259. opr->param() = param;
  1260. param.format = param::ConvBias::Format::NCHW44;
  1261. Benchmarker<ConvBias> benchmark1(handle());
  1262. benchmark1.set_display(false);
  1263. benchmark1.set_param(param);
  1264. benchmark1.set_times(RUN);
  1265. benchmark1.set_before_exec_callback(
  1266. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1267. "F32_CHANNEL_WISE_NCHW44"));
  1268. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1269. TensorLayout dst_layout;
  1270. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1271. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1272. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1273. dst_layout);
  1274. //! dst.nr_elems * IC * FH * FW * 2
  1275. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1276. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1277. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1278. {group * 4, 1, 1, kernel, kernel},
  1279. {1, group * 4, 1, 1},
  1280. {},
  1281. {}}) /
  1282. RUN;
  1283. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1284. {group, 1, 1, kernel, kernel, 4},
  1285. {1, group, 1, 1, 4},
  1286. {},
  1287. {}}) /
  1288. RUN;
  1289. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1290. "nchw44: "
  1291. "%f ms %f GFlops "
  1292. "speedup: %f\n",
  1293. group, h, w, kernel, used0, computations / used0, used1,
  1294. computations / used1, used0 / used1);
  1295. };
  1296. for (size_t group : {8, 16, 32, 64}) {
  1297. for (size_t kerenl : {2, 3, 5}) {
  1298. run(group, 112, 112, kerenl);
  1299. run(group, 56, 56, kerenl);
  1300. run(group, 48, 48, kerenl);
  1301. run(group, 28, 28, kerenl);
  1302. run(group, 14, 14, kerenl);
  1303. }
  1304. }
  1305. run(8, 112, 112, 3);
  1306. run(32, 56, 56, 3);
  1307. run(64, 28, 28, 3);
  1308. run(128, 14, 14, 3);
  1309. }
  1310. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QINT8_STRIDE1_NCHW44) {
  1311. // have to remove preferred restrict in usable func before run the benchmark
  1312. using namespace conv_bias;
  1313. param::ConvBias param;
  1314. param.stride_h = 1;
  1315. param.stride_w = 1;
  1316. param.pad_h = 1;
  1317. param.pad_w = 1;
  1318. param.nonlineMode = NonlineMode::RELU;
  1319. param.sparse = param::ConvBias::Sparse::GROUP;
  1320. constexpr size_t RUN = 50;
  1321. Benchmarker<ConvBias> benchmark0(handle());
  1322. benchmark0.set_dtype(0, dtype::QuantizedS8(0.2f))
  1323. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1324. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1325. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1326. benchmark0.set_display(false);
  1327. benchmark0.set_param(param);
  1328. benchmark0.set_times(RUN);
  1329. benchmark0.set_before_exec_callback(
  1330. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1331. "S8STRD1_LARGE_GROUP"));
  1332. auto opr = handle()->create_operator<ConvBias>();
  1333. opr->param() = param;
  1334. param.format = param::ConvBias::Format::NCHW44;
  1335. Benchmarker<ConvBias> benchmark1(handle());
  1336. benchmark1.set_dtype(0, dtype::QuantizedS8(0.2f))
  1337. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1338. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1339. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1340. benchmark1.set_display(false);
  1341. benchmark1.set_param(param);
  1342. benchmark1.set_times(RUN);
  1343. benchmark1.set_before_exec_callback(
  1344. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1345. "S8_CHAN_WISE_STRD1_NCHW44"));
  1346. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1347. TensorLayout dst_layout;
  1348. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1349. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1350. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1351. dst_layout);
  1352. //! dst.nr_elems * IC * FH * FW * 2
  1353. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1354. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1355. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1356. {group * 4, 1, 1, kernel, kernel},
  1357. {1, group * 4, 1, 1},
  1358. {},
  1359. {}}) /
  1360. RUN;
  1361. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1362. {group, 1, 1, kernel, kernel, 4},
  1363. {1, group, 1, 1, 4},
  1364. {},
  1365. {}}) /
  1366. RUN;
  1367. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1368. "nchw44: "
  1369. "%f ms %f GFlops "
  1370. "speedup: %f\n",
  1371. group, h, w, kernel, used0, computations / used0, used1,
  1372. computations / used1, used0 / used1);
  1373. };
  1374. for (size_t group : {8, 16, 32, 64, 128}) {
  1375. for (size_t kerenl : {2, 3, 5}) {
  1376. run(group, 112, 112, kerenl);
  1377. run(group, 56, 56, kerenl);
  1378. run(group, 48, 48, kerenl);
  1379. run(group, 28, 28, kerenl);
  1380. run(group, 14, 14, kerenl);
  1381. }
  1382. }
  1383. }
  1384. #endif
  1385. #if __ARM_FEATURE_DOTPROD
  1386. #if MEGDNN_WITH_BENCHMARK
  1387. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD) {
  1388. // have to remove preferred restrict in usable func before run the benchmark
  1389. using namespace conv_bias;
  1390. std::vector<TestArg> args;
  1391. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1392. size_t p, NonlineMode nonline_mode) {
  1393. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1394. return;
  1395. param::ConvBias param;
  1396. param.stride_h = 1;
  1397. param.stride_w = 1;
  1398. param.pad_h = p;
  1399. param.pad_w = p;
  1400. param.nonlineMode = nonline_mode;
  1401. //! channel bias
  1402. args.emplace_back(param, TensorShape{2, ic, h, w},
  1403. TensorShape{oc, ic, kernel, kernel},
  1404. TensorShape{1, oc, 1, 1});
  1405. };
  1406. for (size_t kernel : {2, 3, 5, 7})
  1407. for (size_t ic : {1, 8, 16, 32})
  1408. for (size_t oc : {1, 8, 16, 32})
  1409. for (size_t p : {1})
  1410. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1411. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1412. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1413. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1414. }
  1415. constexpr size_t RUN = 50;
  1416. Benchmarker<ConvBias> benchmark0(handle());
  1417. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1418. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1419. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1420. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1421. benchmark0.set_display(false);
  1422. benchmark0.set_times(RUN);
  1423. benchmark0.set_before_exec_callback(
  1424. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD1"));
  1425. Benchmarker<ConvBias> benchmark1(handle());
  1426. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1427. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1428. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1429. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1430. benchmark1.set_display(false);
  1431. benchmark1.set_times(RUN);
  1432. for (auto&& arg : args) {
  1433. TensorLayout dst_layout;
  1434. auto opr = handle()->create_operator<ConvBias>();
  1435. opr->param() = arg.param;
  1436. opr->deduce_layout({arg.src, dtype::Int8()},
  1437. {arg.filter, dtype::Int8()},
  1438. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1439. //! dst.nr_elems * IC * FH * FW * 2
  1440. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1441. arg.filter[2] * arg.filter[3] * 2.0 /
  1442. (1024 * 1024 * 1024) * 1e3;
  1443. auto used0 = benchmark0.set_param(arg.param).exec(
  1444. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1445. RUN;
  1446. auto used1 = benchmark1.set_param(arg.param).exec(
  1447. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1448. RUN;
  1449. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1450. "speedup: %f\n",
  1451. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1452. used0, computations / used0, used1, computations / used1,
  1453. used1 / used0);
  1454. }
  1455. }
  1456. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2_WITHDOTPROD) {
  1457. // have to remove preferred restrict in usable func before run the benchmark
  1458. using namespace conv_bias;
  1459. std::vector<TestArg> args;
  1460. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1461. size_t p, NonlineMode nonline_mode) {
  1462. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1463. return;
  1464. param::ConvBias param;
  1465. param.stride_h = 2;
  1466. param.stride_w = 2;
  1467. param.pad_h = p;
  1468. param.pad_w = p;
  1469. param.nonlineMode = nonline_mode;
  1470. //! channel bias
  1471. args.emplace_back(param, TensorShape{2, ic, h, w},
  1472. TensorShape{oc, ic, kernel, kernel},
  1473. TensorShape{1, oc, 1, 1});
  1474. };
  1475. for (size_t kernel : {2, 3, 5, 7})
  1476. for (size_t ic : {1, 8, 16, 32})
  1477. for (size_t oc : {1, 8, 16, 32})
  1478. for (size_t p : {1})
  1479. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1480. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1481. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1482. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1483. }
  1484. constexpr size_t RUN = 50;
  1485. Benchmarker<ConvBias> benchmark0(handle());
  1486. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1487. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1488. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1489. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1490. benchmark0.set_display(false);
  1491. benchmark0.set_times(RUN);
  1492. benchmark0.set_before_exec_callback(
  1493. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD2"));
  1494. Benchmarker<ConvBias> benchmark1(handle());
  1495. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1496. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1497. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1498. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1499. benchmark1.set_display(false);
  1500. benchmark1.set_times(RUN);
  1501. for (auto&& arg : args) {
  1502. TensorLayout dst_layout;
  1503. auto opr = handle()->create_operator<ConvBias>();
  1504. opr->param() = arg.param;
  1505. opr->deduce_layout({arg.src, dtype::Int8()},
  1506. {arg.filter, dtype::Int8()},
  1507. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1508. //! dst.nr_elems * IC * FH * FW * 2
  1509. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1510. arg.filter[2] * arg.filter[3] * 2.0 /
  1511. (1024 * 1024 * 1024) * 1e3;
  1512. auto used0 = benchmark0.set_param(arg.param).exec(
  1513. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1514. RUN;
  1515. auto used1 = benchmark1.set_param(arg.param).exec(
  1516. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1517. RUN;
  1518. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1519. "speedup: %f\n",
  1520. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1521. used0, computations / used0, used1, computations / used1,
  1522. used1 / used0);
  1523. }
  1524. }
  1525. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1_WITHDOTPROD) {
  1526. // have to remove preferred restrict in usable func before run the benchmark
  1527. using namespace conv_bias;
  1528. std::vector<TestArg> args;
  1529. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1530. size_t p, NonlineMode nonline_mode) {
  1531. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1532. return;
  1533. param::ConvBias param;
  1534. param.stride_h = 1;
  1535. param.stride_w = 1;
  1536. param.pad_h = p;
  1537. param.pad_w = p;
  1538. param.nonlineMode = nonline_mode;
  1539. //! channel bias
  1540. args.emplace_back(param, TensorShape{2, ic, h, w},
  1541. TensorShape{oc, ic, kernel, kernel},
  1542. TensorShape{1, oc, 1, 1});
  1543. };
  1544. // clang-format off
  1545. for (size_t kernel : {2, 3, 5, 7})
  1546. for (size_t ic : {1, 8, 16, 32})
  1547. for (size_t oc : {1, 8, 16, 32})
  1548. for (size_t p : {1})
  1549. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1550. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1551. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1552. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1553. }
  1554. // clang-format on
  1555. constexpr size_t RUN = 50;
  1556. Benchmarker<ConvBias> benchmark0(handle());
  1557. benchmark0
  1558. .set_dtype(0,
  1559. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1560. .set_dtype(1,
  1561. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1562. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1563. .set_dtype(4,
  1564. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1565. benchmark0.set_display(false);
  1566. benchmark0.set_times(RUN);
  1567. benchmark0.set_before_exec_callback(
  1568. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD1"));
  1569. Benchmarker<ConvBias> benchmark1(handle());
  1570. benchmark1
  1571. .set_dtype(0,
  1572. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1573. .set_dtype(1,
  1574. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1575. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1576. .set_dtype(4,
  1577. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1578. benchmark1.set_display(false);
  1579. benchmark1.set_times(RUN);
  1580. for (auto&& arg : args) {
  1581. TensorLayout dst_layout;
  1582. auto opr = handle()->create_operator<ConvBias>();
  1583. opr->param() = arg.param;
  1584. opr->deduce_layout({arg.src, dtype::Int8()},
  1585. {arg.filter, dtype::Int8()},
  1586. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1587. //! dst.nr_elems * IC * FH * FW * 2
  1588. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1589. arg.filter[2] * arg.filter[3] * 2.0 /
  1590. (1024 * 1024 * 1024) * 1e3;
  1591. auto used0 = benchmark0.set_param(arg.param).exec(
  1592. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1593. RUN;
  1594. auto used1 = benchmark1.set_param(arg.param).exec(
  1595. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1596. RUN;
  1597. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1598. "speedup: %f\n",
  1599. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1600. used0, computations / used0, used1, computations / used1,
  1601. used1 / used0);
  1602. }
  1603. }
  1604. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2_WITHDOTPROD) {
  1605. // have to remove preferred restrict in usable func before run the benchmark
  1606. using namespace conv_bias;
  1607. std::vector<TestArg> args;
  1608. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1609. size_t p, NonlineMode nonline_mode) {
  1610. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1611. return;
  1612. param::ConvBias param;
  1613. param.stride_h = 2;
  1614. param.stride_w = 2;
  1615. param.pad_h = p;
  1616. param.pad_w = p;
  1617. param.nonlineMode = nonline_mode;
  1618. //! channel bias
  1619. args.emplace_back(param, TensorShape{2, ic, h, w},
  1620. TensorShape{oc, ic, kernel, kernel},
  1621. TensorShape{1, oc, 1, 1});
  1622. };
  1623. // clang-format off
  1624. for (size_t kernel : {2, 3, 5, 7})
  1625. for (size_t ic : {1, 8, 16, 32})
  1626. for (size_t oc : {1, 8, 16, 32})
  1627. for (size_t p : {1})
  1628. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1629. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1630. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1631. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1632. }
  1633. // clang-format on
  1634. constexpr size_t RUN = 50;
  1635. Benchmarker<ConvBias> benchmark0(handle());
  1636. benchmark0
  1637. .set_dtype(0,
  1638. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1639. .set_dtype(1,
  1640. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1641. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1642. .set_dtype(4,
  1643. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1644. benchmark0.set_display(false);
  1645. benchmark0.set_times(RUN);
  1646. benchmark0.set_before_exec_callback(
  1647. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD2"));
  1648. Benchmarker<ConvBias> benchmark1(handle());
  1649. benchmark1
  1650. .set_dtype(0,
  1651. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1652. .set_dtype(1,
  1653. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1654. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1655. .set_dtype(4,
  1656. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1657. benchmark1.set_display(false);
  1658. benchmark1.set_times(RUN);
  1659. for (auto&& arg : args) {
  1660. TensorLayout dst_layout;
  1661. auto opr = handle()->create_operator<ConvBias>();
  1662. opr->param() = arg.param;
  1663. opr->deduce_layout({arg.src, dtype::Int8()},
  1664. {arg.filter, dtype::Int8()},
  1665. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1666. //! dst.nr_elems * IC * FH * FW * 2
  1667. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1668. arg.filter[2] * arg.filter[3] * 2.0 /
  1669. (1024 * 1024 * 1024) * 1e3;
  1670. auto used0 = benchmark0.set_param(arg.param).exec(
  1671. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1672. RUN;
  1673. auto used1 = benchmark1.set_param(arg.param).exec(
  1674. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1675. RUN;
  1676. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1677. "speedup: %f\n",
  1678. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1679. used0, computations / used0, used1, computations / used1,
  1680. used1 / used0);
  1681. }
  1682. }
  1683. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD_NCHW44_DOT) {
  1684. using namespace conv_bias;
  1685. std::vector<TestArg> args;
  1686. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1687. size_t p, size_t stride, NonlineMode nonline_mode) {
  1688. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1689. return;
  1690. param::ConvBias param;
  1691. param.stride_h = stride;
  1692. param.stride_w = stride;
  1693. param.pad_h = p;
  1694. param.pad_w = p;
  1695. param.nonlineMode = nonline_mode;
  1696. param.format = param::ConvBias::Format::NCHW44_DOT;
  1697. //! channel bias
  1698. args.emplace_back(param, TensorShape{1, ic / 4, h, w, 4},
  1699. TensorShape{oc / 4, ic / 4, kernel, kernel, 4, 4},
  1700. TensorShape{1, oc / 4, 1, 1, 4});
  1701. };
  1702. for (size_t stride : {1, 2})
  1703. for (size_t kernel : {2, 3, 5, 7})
  1704. for (size_t oc : {64})
  1705. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1706. run(oc, oc, 56, 56, kernel, kernel / 2, stride,
  1707. nonline_mode);
  1708. }
  1709. constexpr size_t RUN = 50;
  1710. Benchmarker<ConvBias> benchmark0(handle());
  1711. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1712. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1713. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1714. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1715. benchmark0.set_display(false);
  1716. benchmark0.set_times(RUN);
  1717. benchmark0.set_before_exec_callback(
  1718. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1719. "ARMDOTS8DIRECT_NCHW44"));
  1720. Benchmarker<ConvBias> benchmark1(handle());
  1721. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1722. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1723. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1724. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1725. benchmark1.set_display(false);
  1726. benchmark1.set_times(RUN);
  1727. for (auto&& arg : args) {
  1728. TensorLayout dst_layout;
  1729. auto opr = handle()->create_operator<ConvBias>();
  1730. opr->param() = arg.param;
  1731. opr->deduce_layout({arg.src, dtype::Int8()},
  1732. {arg.filter, dtype::Int8()},
  1733. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1734. //! dst.nr_elems * IC * FH * FW * 2
  1735. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1736. arg.filter[2] * arg.filter[3] * 8.0 /
  1737. (1024 * 1024 * 1024) * 1e3;
  1738. auto used0 = benchmark0.set_param(arg.param).exec(
  1739. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1740. RUN;
  1741. auto used1 = benchmark1.set_param(arg.param).exec(
  1742. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1743. RUN;
  1744. printf("%s %s: Direct use: %f ms %f Gflops normal: %f ms %f GFlops "
  1745. "speedup: %f\n",
  1746. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1747. used0, computations / used0, used1, computations / used1,
  1748. used1 / used0);
  1749. }
  1750. }
  1751. #endif
  1752. #endif
  1753. /*====================== BENCHMARK CONV1X1 ===========================*/
  1754. #if MEGDNN_WITH_BENCHMARK
  1755. namespace {
  1756. std::vector<conv_bias::TestArg> get_conv_bias_1x1_benchmark_args(
  1757. size_t pack_size = 1) {
  1758. using namespace conv_bias;
  1759. std::vector<TestArg> args;
  1760. param::ConvBias param;
  1761. param.stride_h = 1;
  1762. param.stride_w = 1;
  1763. param.pad_h = 0;
  1764. param.pad_w = 0;
  1765. param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1766. auto bench_case = [&](size_t OC, size_t IC, size_t H, size_t W) {
  1767. if (pack_size == 1)
  1768. args.emplace_back(param, TensorShape{1, IC, H, W},
  1769. TensorShape{OC, IC, 1, 1}, TensorShape{});
  1770. else {
  1771. if (pack_size == 4)
  1772. param.format = param::ConvBias::Format::NCHW44;
  1773. args.emplace_back(param,
  1774. TensorShape{1, IC / pack_size, H, W, pack_size},
  1775. TensorShape{OC / pack_size, IC / pack_size, 1, 1,
  1776. pack_size, pack_size},
  1777. TensorShape{});
  1778. }
  1779. };
  1780. //! MobileNetV1
  1781. bench_case(64, 32, 112, 112);
  1782. bench_case(128, 64, 56, 56);
  1783. bench_case(128, 128, 56, 56);
  1784. bench_case(256, 128, 28, 28);
  1785. bench_case(256, 256, 28, 28);
  1786. bench_case(512, 256, 14, 14);
  1787. bench_case(512, 512, 14, 14);
  1788. bench_case(1024, 512, 7, 7);
  1789. bench_case(1024, 1024, 7, 7);
  1790. //! MobileNetV2
  1791. bench_case(16, 32, 112, 112);
  1792. bench_case(96, 16, 112, 112);
  1793. bench_case(144, 24, 56, 56);
  1794. bench_case(192, 32, 28, 28);
  1795. bench_case(384, 64, 28, 28);
  1796. bench_case(576, 96, 14, 14);
  1797. bench_case(960, 160, 7, 7);
  1798. bench_case(320, 960, 7, 7);
  1799. bench_case(1280, 320, 7, 7);
  1800. //! MobileNetV3-Large
  1801. bench_case(64, 16, 112, 112);
  1802. bench_case(72, 24, 56, 56);
  1803. bench_case(120, 40, 28, 28);
  1804. bench_case(240, 40, 28, 28);
  1805. bench_case(200, 80, 14, 14);
  1806. bench_case(184, 80, 14, 14);
  1807. bench_case(480, 80, 14, 14);
  1808. bench_case(672, 112, 14, 14);
  1809. //! MobileNetV3-Small
  1810. bench_case(72, 16, 56, 56);
  1811. bench_case(88, 24, 28, 28);
  1812. bench_case(96, 24, 28, 28);
  1813. bench_case(240, 40, 14, 14);
  1814. bench_case(120, 40, 14, 14);
  1815. bench_case(144, 48, 14, 14);
  1816. bench_case(288, 48, 14, 14);
  1817. bench_case(576, 96, 7, 7);
  1818. //! resnet50
  1819. bench_case(256, 64, 56, 56);
  1820. bench_case(512, 128, 28, 28);
  1821. bench_case(1024, 256, 14, 14);
  1822. bench_case(2048, 512, 7, 7);
  1823. return args;
  1824. }
  1825. void benchmark_conv1x1(const char* matmul_algo_name, Handle* handle,
  1826. DType stype, DType matmul_dtype, DType bias_type,
  1827. DType conv_dtype, bool is_mk4 = false) {
  1828. using namespace conv_bias;
  1829. int pack_size = is_mk4 ? 4 : 1;
  1830. std::vector<TestArg> conv_bias_1x1_args =
  1831. get_conv_bias_1x1_benchmark_args(pack_size);
  1832. constexpr size_t RUNS = 50;
  1833. param::MatrixMul param;
  1834. param.transposeA = false;
  1835. param.transposeB = false;
  1836. if (is_mk4) {
  1837. param.format = MatrixMul::Param::Format::MK4;
  1838. }
  1839. Benchmarker<MatrixMul> benchmark_matmul(handle);
  1840. benchmark_matmul.set_before_exec_callback(
  1841. AlgoChecker<MatrixMul>(matmul_algo_name));
  1842. benchmark_matmul.set_times(RUNS)
  1843. .set_dtype(0, stype)
  1844. .set_dtype(1, stype)
  1845. .set_dtype(2, matmul_dtype)
  1846. .set_param(param)
  1847. .set_display(false);
  1848. std::string conv1x1_algo_name = ssprintf("CONV1x1:%s:24", matmul_algo_name);
  1849. Benchmarker<ConvBias> benchmark_conv1x1(handle);
  1850. benchmark_conv1x1.set_before_exec_callback(
  1851. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1852. conv1x1_algo_name.c_str()));
  1853. benchmark_conv1x1.set_times(RUNS)
  1854. .set_dtype(0, stype)
  1855. .set_dtype(1, stype)
  1856. .set_dtype(2, bias_type)
  1857. .set_dtype(4, conv_dtype)
  1858. .set_display(false);
  1859. for (auto&& arg : conv_bias_1x1_args) {
  1860. size_t IC = arg.src[1];
  1861. size_t OH = arg.src[2];
  1862. size_t OW = arg.src[3];
  1863. size_t OC = arg.filter[0];
  1864. size_t M = OC * pack_size;
  1865. size_t K = IC * pack_size;
  1866. size_t N = OH * OW;
  1867. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  1868. TensorShape A, B;
  1869. A = TensorShape{M, K};
  1870. B = TensorShape{K, N};
  1871. if (is_mk4) {
  1872. A = TensorShape{M / 4, K / 4, 4, 4};
  1873. B = TensorShape{K / 4, N, 4};
  1874. }
  1875. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  1876. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1877. RUNS;
  1878. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  1879. printf("%s %s:\n matmul: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  1880. "speedup: "
  1881. "%f\n",
  1882. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1883. matmul_used, computations / matmul_used, conv1x1_used,
  1884. computations / conv1x1_used, matmul_used / conv1x1_used);
  1885. }
  1886. }
  1887. } // namespace
  1888. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F32) {
  1889. #if MEGDNN_AARCH64
  1890. benchmark_conv1x1("AARCH64_F32K8X12X1", handle(), dtype::Float32{},
  1891. dtype::Float32{}, dtype::Float32{}, dtype::Float32{});
  1892. #else
  1893. benchmark_conv1x1("ARMV7_F32", handle(), dtype::Float32{}, dtype::Float32{},
  1894. dtype::Float32{}, dtype::Float32{});
  1895. #endif
  1896. }
  1897. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  1898. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F16) {
  1899. #if MEGDNN_AARCH64
  1900. benchmark_conv1x1("AARCH64_F16_K8X24X1", handle(), dtype::Float16{},
  1901. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1902. #else
  1903. benchmark_conv1x1("AARCH32_F16_K4X16X1", handle(), dtype::Float16{},
  1904. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1905. #endif
  1906. }
  1907. #endif
  1908. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDSYM) {
  1909. dtype::QuantizedS8 stype(2.5f);
  1910. dtype::QuantizedS32 dtype(6.25f);
  1911. #if MEGDNN_AARCH64
  1912. #if __ARM_FEATURE_DOTPROD
  1913. benchmark_conv1x1("AARCH64_INT8X8X32_K8X12X4_DOTPROD", handle(), stype,
  1914. dtype, dtype, dtype);
  1915. #else
  1916. benchmark_conv1x1("AARCH64_INT8X8X32_K8X8X8", handle(), stype, dtype, dtype,
  1917. dtype);
  1918. benchmark_conv1x1("AARCH64_INT8X8X32_K4X4X16", handle(), stype, dtype,
  1919. dtype, dtype);
  1920. #endif
  1921. #elif MEGDNN_ARMV7
  1922. benchmark_conv1x1("ARMV7_INT8X8X32_K4X8X8", handle(), stype, dtype, dtype,
  1923. dtype);
  1924. #endif
  1925. }
  1926. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDASYM) {
  1927. dtype::Quantized8Asymm stype(1.2f, (uint8_t)125);
  1928. dtype::QuantizedS32 dtype(1.2 * 1.2);
  1929. #if MEGDNN_AARCH64
  1930. #if __ARM_FEATURE_DOTPROD
  1931. benchmark_conv1x1("AARCH64_QUINT8_K8X8X4_DOTPROD", handle(), stype, dtype,
  1932. dtype, dtype);
  1933. #else
  1934. benchmark_conv1x1("AARCH64_QUINT8_K8X8X8", handle(), stype, dtype, dtype,
  1935. dtype);
  1936. #endif
  1937. #elif MEGDNN_ARMV7
  1938. benchmark_conv1x1("ARMV7_QUINT8_K4X8X8", handle(), stype, dtype, dtype,
  1939. dtype);
  1940. #endif
  1941. }
  1942. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_INT8x8x16) {
  1943. #if MEGDNN_AARCH64
  1944. benchmark_conv1x1("AARCH64_INT8X8X16_K8X8X8", handle(), dtype::Int8{},
  1945. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1946. benchmark_conv1x1("AARCH64_INT8X8X16_K4X4X16", handle(), dtype::Int8{},
  1947. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1948. #elif MEGDNN_ARMV7
  1949. benchmark_conv1x1("ARMV7_INT8X8X16_K4X8X8", handle(), dtype::Int8{},
  1950. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1951. benchmark_conv1x1("ARMV7_INT8X8X16_K4X2X16", handle(), dtype::Int8{},
  1952. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1953. benchmark_conv1x1("ARMV7_INT8X8X16_MK4_K8X8X4", handle(), dtype::Int8{},
  1954. dtype::Int16{}, dtype::Int16{}, dtype::Int16{}, true);
  1955. #endif
  1956. }
  1957. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_GEMV_FP32) {
  1958. using namespace conv_bias;
  1959. std::vector<conv_bias::TestArg> args;
  1960. param::ConvBias conv_param;
  1961. conv_param.stride_h = 1;
  1962. conv_param.stride_w = 1;
  1963. conv_param.pad_h = 0;
  1964. conv_param.pad_w = 0;
  1965. conv_param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1966. auto run = [&](size_t M, size_t K) {
  1967. args.emplace_back(conv_param, TensorShape{1, K, 1, 1},
  1968. TensorShape{M, K, 1, 1}, TensorShape{});
  1969. };
  1970. for (size_t M : {4, 64, 1024, 4096})
  1971. for (size_t K : {128, 256, 1024, 4096})
  1972. run(M, K);
  1973. constexpr size_t RUNS = 50;
  1974. param::MatrixMul param;
  1975. param.transposeA = false;
  1976. param.transposeB = false;
  1977. Benchmarker<MatrixMul> benchmark_matmul(handle());
  1978. benchmark_matmul.set_before_exec_callback(
  1979. AlgoChecker<MatrixMul>("ARM_COMMON_F32_GEMV"));
  1980. benchmark_matmul.set_times(RUNS)
  1981. .set_dtype(0, dtype::Float32{})
  1982. .set_dtype(1, dtype::Float32{})
  1983. .set_dtype(2, dtype::Float32{})
  1984. .set_param(param)
  1985. .set_display(false);
  1986. Benchmarker<ConvBias> benchmark_conv1x1(handle());
  1987. benchmark_conv1x1.set_before_exec_callback(
  1988. conv_bias::ConvBiasAlgoChecker<ConvBias>("CONV1x1_GEMV"));
  1989. benchmark_conv1x1.set_times(RUNS)
  1990. .set_dtype(0, dtype::Float32{})
  1991. .set_dtype(1, dtype::Float32{})
  1992. .set_dtype(2, dtype::Float32{})
  1993. .set_dtype(4, dtype::Float32{})
  1994. .set_display(false);
  1995. std::cout << "warm up:\n";
  1996. for (int i = 0; i < 50; i++) {
  1997. benchmark_matmul.exec({{1, 1024}, {1024, 512}, {}});
  1998. benchmark_matmul.set_display(true);
  1999. }
  2000. for (auto&& arg : args) {
  2001. size_t IC = arg.src[1];
  2002. size_t OH = arg.src[2];
  2003. size_t OW = arg.src[3];
  2004. size_t OC = arg.filter[0];
  2005. size_t M = OC;
  2006. size_t K = IC;
  2007. size_t N = OH * OW;
  2008. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2009. TensorShape A, B;
  2010. A = TensorShape{M, K};
  2011. B = TensorShape{K, N};
  2012. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  2013. {arg.src, arg.filter, arg.bias, {}, {}}) /
  2014. RUNS;
  2015. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  2016. printf("%s %s:\n gemv: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  2017. "speedup: "
  2018. "%f\n",
  2019. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2020. matmul_used, computations / matmul_used, conv1x1_used,
  2021. computations / conv1x1_used, matmul_used / conv1x1_used);
  2022. }
  2023. }
  2024. #ifndef __ARM_FEATURE_DOTPROD
  2025. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_1X1_S1_NCHW_VS_NCHW44_INT8x8x32) {
  2026. std::vector<TestArg> conv_bias_1x1_args_nchw44 =
  2027. get_conv_bias_1x1_benchmark_args(4);
  2028. std::vector<TestArg> conv_bias_1x1_args_nchw =
  2029. get_conv_bias_1x1_benchmark_args(1);
  2030. constexpr size_t RUNS = 50;
  2031. Benchmarker<ConvBias> benchmark_conv1x1_nchw44(handle());
  2032. benchmark_conv1x1_nchw44.set_before_exec_callback(
  2033. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2034. "CONV1x1:AARCH64_INT8X8X32_MK4_4X4X16:24"));
  2035. benchmark_conv1x1_nchw44.set_times(RUNS)
  2036. .set_dtype(0, dtype::Int8())
  2037. .set_dtype(1, dtype::Int8())
  2038. .set_dtype(2, dtype::Int32())
  2039. .set_dtype(4, dtype::Int32())
  2040. .set_display(false);
  2041. Benchmarker<ConvBias> benchmark_conv1x1_nchw(handle());
  2042. benchmark_conv1x1_nchw.set_before_exec_callback(
  2043. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2044. "CONV1x1:AARCH64_INT8X8X32_K4X4X16:24"));
  2045. benchmark_conv1x1_nchw.set_times(RUNS)
  2046. .set_dtype(0, dtype::Int8())
  2047. .set_dtype(1, dtype::Int8())
  2048. .set_dtype(2, dtype::Int32())
  2049. .set_dtype(4, dtype::Int32())
  2050. .set_display(false);
  2051. for (size_t i = 0; i < conv_bias_1x1_args_nchw44.size(); ++i) {
  2052. auto&& arg_nchw = conv_bias_1x1_args_nchw[i];
  2053. auto&& arg_nchw44 = conv_bias_1x1_args_nchw44[i];
  2054. size_t IC = arg_nchw.src[1];
  2055. size_t OH = arg_nchw.src[2];
  2056. size_t OW = arg_nchw.src[3];
  2057. size_t OC = arg_nchw.filter[0];
  2058. size_t M = OC;
  2059. size_t K = IC;
  2060. size_t N = OH * OW;
  2061. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2062. auto conv1x1_nchw = benchmark_conv1x1_nchw.set_param(arg_nchw.param)
  2063. .exec({arg_nchw.src,
  2064. arg_nchw.filter,
  2065. arg_nchw.bias,
  2066. {},
  2067. {}}) /
  2068. RUNS;
  2069. auto conv1x1_nchw44 =
  2070. benchmark_conv1x1_nchw44.set_param(arg_nchw44.param)
  2071. .exec({arg_nchw44.src,
  2072. arg_nchw44.filter,
  2073. arg_nchw44.bias,
  2074. {},
  2075. {}}) /
  2076. RUNS;
  2077. printf("%s %s:\n conv_1x1_nchw: %f ms %f Gflops\nconv1x1_nchw44: %f ms "
  2078. "%f GFlops "
  2079. "speedup: "
  2080. "%f\n",
  2081. arg_nchw.src.to_string().c_str(),
  2082. arg_nchw.filter.to_string().c_str(), conv1x1_nchw,
  2083. computations / conv1x1_nchw, conv1x1_nchw44,
  2084. computations / conv1x1_nchw44, conv1x1_nchw / conv1x1_nchw44);
  2085. }
  2086. }
  2087. #endif
  2088. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_WINOGRAD_VS_IM2COL_INT8) {
  2089. auto&& args = get_winograd_benchmark_args(3, 8);
  2090. using namespace conv_bias;
  2091. constexpr size_t RUN = 10;
  2092. Benchmarker<ConvBias> benchmark_im2col(handle());
  2093. benchmark_im2col.set_display(false);
  2094. benchmark_im2col.set_times(RUN);
  2095. benchmark_im2col.set_dtype(0, dtype::QuantizedS8(2.5f))
  2096. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2097. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2098. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2099. Benchmarker<ConvBias> benchmark_winograd(handle());
  2100. benchmark_winograd.set_display(false);
  2101. benchmark_winograd.set_times(RUN);
  2102. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  2103. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2104. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2105. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2106. for (auto&& arg : args) {
  2107. TensorLayout dst_layout;
  2108. auto opr = handle()->create_operator<ConvBias>();
  2109. opr->param() = arg.param;
  2110. opr->deduce_layout({arg.src, dtype::Float32()},
  2111. {arg.filter, dtype::Float32()},
  2112. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2113. //! dst.nr_elems * IC * FH * FW * 2
  2114. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2115. arg.filter[2] * arg.filter[3] * 2.0 /
  2116. (1024 * 1024 * 1024) * 1e3;
  2117. benchmark_im2col.set_param(arg.param);
  2118. auto im2col_used =
  2119. algo_benchmark<ConvBias>(
  2120. benchmark_im2col, {arg.src, arg.filter, {}, {}, {}},
  2121. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16") /
  2122. RUN;
  2123. benchmark_winograd.set_param(arg.param);
  2124. auto winograd_used =
  2125. algo_benchmark<ConvBias>(
  2126. benchmark_winograd, {arg.src, arg.filter, {}, {}, {}},
  2127. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2") /
  2128. RUN;
  2129. printf("%s %s: im2col: %f ms %f Gflops winograd: %f ms %f GFlops "
  2130. "speedup: "
  2131. "%f\n",
  2132. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2133. im2col_used, computations / im2col_used, winograd_used,
  2134. computations / winograd_used, im2col_used / winograd_used);
  2135. }
  2136. }
  2137. #endif
  2138. // vim: syntax=cpp.doxygen

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