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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366
  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>("F32STRD1"));
  1187. auto opr = handle()->create_operator<ConvBias>();
  1188. opr->param() = param;
  1189. param.format = param::ConvBias::Format::NCHW44;
  1190. Benchmarker<ConvBias> benchmark1(handle());
  1191. benchmark1.set_display(false);
  1192. benchmark1.set_param(param);
  1193. benchmark1.set_times(RUN);
  1194. benchmark1.set_before_exec_callback(
  1195. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1196. "F32_CHANNEL_WISE_NCHW44"));
  1197. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1198. TensorLayout dst_layout;
  1199. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1200. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1201. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1202. dst_layout);
  1203. //! dst.nr_elems * IC * FH * FW * 2
  1204. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1205. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1206. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1207. {group * 4, 1, 1, kernel, kernel},
  1208. {1, group * 4, 1, 1},
  1209. {},
  1210. {}}) /
  1211. RUN;
  1212. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1213. {group, 1, 1, kernel, kernel, 4},
  1214. {1, group, 1, 1, 4},
  1215. {},
  1216. {}}) /
  1217. RUN;
  1218. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1219. "nchw44: "
  1220. "%f ms %f GFlops "
  1221. "speedup: %f\n",
  1222. group, h, w, kernel, used0, computations / used0, used1,
  1223. computations / used1, used0 / used1);
  1224. };
  1225. for (size_t group : {8, 16, 32, 64}) {
  1226. for (size_t kerenl : {2, 3, 5}) {
  1227. run(group, 112, 112, kerenl);
  1228. run(group, 56, 56, kerenl);
  1229. run(group, 48, 48, kerenl);
  1230. run(group, 28, 28, kerenl);
  1231. run(group, 14, 14, kerenl);
  1232. }
  1233. }
  1234. run(8, 112, 112, 3);
  1235. run(32, 56, 56, 3);
  1236. run(64, 28, 28, 3);
  1237. run(128, 14, 14, 3);
  1238. }
  1239. TEST_F(ARM_COMMON, BENCHMARK_CHANNEL_WISE_F32_STRIDE2_NCHW44) {
  1240. // have to remove preferred restrict in usable func before run the benchmark
  1241. using namespace conv_bias;
  1242. param::ConvBias param;
  1243. param.stride_h = 2;
  1244. param.stride_w = 2;
  1245. param.pad_h = 1;
  1246. param.pad_w = 1;
  1247. param.nonlineMode = NonlineMode::RELU;
  1248. param.sparse = param::ConvBias::Sparse::GROUP;
  1249. constexpr size_t RUN = 50;
  1250. Benchmarker<ConvBias> benchmark0(handle());
  1251. benchmark0.set_display(false);
  1252. benchmark0.set_param(param);
  1253. benchmark0.set_times(RUN);
  1254. benchmark0.set_before_exec_callback(
  1255. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("F32STRD2"));
  1256. auto opr = handle()->create_operator<ConvBias>();
  1257. opr->param() = param;
  1258. param.format = param::ConvBias::Format::NCHW44;
  1259. Benchmarker<ConvBias> benchmark1(handle());
  1260. benchmark1.set_display(false);
  1261. benchmark1.set_param(param);
  1262. benchmark1.set_times(RUN);
  1263. benchmark1.set_before_exec_callback(
  1264. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1265. "F32_CHANNEL_WISE_NCHW44"));
  1266. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1267. TensorLayout dst_layout;
  1268. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1269. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1270. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1271. dst_layout);
  1272. //! dst.nr_elems * IC * FH * FW * 2
  1273. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1274. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1275. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1276. {group * 4, 1, 1, kernel, kernel},
  1277. {1, group * 4, 1, 1},
  1278. {},
  1279. {}}) /
  1280. RUN;
  1281. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1282. {group, 1, 1, kernel, kernel, 4},
  1283. {1, group, 1, 1, 4},
  1284. {},
  1285. {}}) /
  1286. RUN;
  1287. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1288. "nchw44: "
  1289. "%f ms %f GFlops "
  1290. "speedup: %f\n",
  1291. group, h, w, kernel, used0, computations / used0, used1,
  1292. computations / used1, used0 / used1);
  1293. };
  1294. for (size_t group : {8, 16, 32, 64}) {
  1295. for (size_t kerenl : {2, 3, 5}) {
  1296. run(group, 112, 112, kerenl);
  1297. run(group, 56, 56, kerenl);
  1298. run(group, 48, 48, kerenl);
  1299. run(group, 28, 28, kerenl);
  1300. run(group, 14, 14, kerenl);
  1301. }
  1302. }
  1303. run(8, 112, 112, 3);
  1304. run(32, 56, 56, 3);
  1305. run(64, 28, 28, 3);
  1306. run(128, 14, 14, 3);
  1307. }
  1308. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QINT8_STRIDE1_NCHW44) {
  1309. // have to remove preferred restrict in usable func before run the benchmark
  1310. using namespace conv_bias;
  1311. param::ConvBias param;
  1312. param.stride_h = 1;
  1313. param.stride_w = 1;
  1314. param.pad_h = 1;
  1315. param.pad_w = 1;
  1316. param.nonlineMode = NonlineMode::RELU;
  1317. param.sparse = param::ConvBias::Sparse::GROUP;
  1318. constexpr size_t RUN = 50;
  1319. Benchmarker<ConvBias> benchmark0(handle());
  1320. benchmark0.set_dtype(0, dtype::QuantizedS8(0.2f))
  1321. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1322. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1323. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1324. benchmark0.set_display(false);
  1325. benchmark0.set_param(param);
  1326. benchmark0.set_times(RUN);
  1327. benchmark0.set_before_exec_callback(
  1328. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD1"));
  1329. auto opr = handle()->create_operator<ConvBias>();
  1330. opr->param() = param;
  1331. param.format = param::ConvBias::Format::NCHW44;
  1332. Benchmarker<ConvBias> benchmark1(handle());
  1333. benchmark1.set_dtype(0, dtype::QuantizedS8(0.2f))
  1334. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1335. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1336. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1337. benchmark1.set_display(false);
  1338. benchmark1.set_param(param);
  1339. benchmark1.set_times(RUN);
  1340. benchmark1.set_before_exec_callback(
  1341. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1342. "S8_CHAN_WISE_STRD1_NCHW44"));
  1343. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1344. TensorLayout dst_layout;
  1345. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1346. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1347. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1348. dst_layout);
  1349. //! dst.nr_elems * IC * FH * FW * 2
  1350. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1351. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1352. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1353. {group * 4, 1, 1, kernel, kernel},
  1354. {1, group * 4, 1, 1},
  1355. {},
  1356. {}}) /
  1357. RUN;
  1358. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1359. {group, 1, 1, kernel, kernel, 4},
  1360. {1, group, 1, 1, 4},
  1361. {},
  1362. {}}) /
  1363. RUN;
  1364. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1365. "nchw44: "
  1366. "%f ms %f GFlops "
  1367. "speedup: %f\n",
  1368. group, h, w, kernel, used0, computations / used0, used1,
  1369. computations / used1, used0 / used1);
  1370. };
  1371. for (size_t group : {8, 16, 32, 64, 128}) {
  1372. for (size_t kerenl : {2, 3, 5}) {
  1373. run(group, 112, 112, kerenl);
  1374. run(group, 56, 56, kerenl);
  1375. run(group, 48, 48, kerenl);
  1376. run(group, 28, 28, kerenl);
  1377. run(group, 14, 14, kerenl);
  1378. }
  1379. }
  1380. }
  1381. #endif
  1382. #if __ARM_FEATURE_DOTPROD
  1383. #if MEGDNN_WITH_BENCHMARK
  1384. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD) {
  1385. // have to remove preferred restrict in usable func before run the benchmark
  1386. using namespace conv_bias;
  1387. std::vector<TestArg> args;
  1388. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1389. size_t p, NonlineMode nonline_mode) {
  1390. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1391. return;
  1392. param::ConvBias param;
  1393. param.stride_h = 1;
  1394. param.stride_w = 1;
  1395. param.pad_h = p;
  1396. param.pad_w = p;
  1397. param.nonlineMode = nonline_mode;
  1398. //! channel bias
  1399. args.emplace_back(param, TensorShape{2, ic, h, w},
  1400. TensorShape{oc, ic, kernel, kernel},
  1401. TensorShape{1, oc, 1, 1});
  1402. };
  1403. for (size_t kernel : {2, 3, 5, 7})
  1404. for (size_t ic : {1, 8, 16, 32})
  1405. for (size_t oc : {1, 8, 16, 32})
  1406. for (size_t p : {1})
  1407. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1408. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1409. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1410. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1411. }
  1412. constexpr size_t RUN = 50;
  1413. Benchmarker<ConvBias> benchmark0(handle());
  1414. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1415. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1416. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1417. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1418. benchmark0.set_display(false);
  1419. benchmark0.set_times(RUN);
  1420. benchmark0.set_before_exec_callback(
  1421. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD1"));
  1422. Benchmarker<ConvBias> benchmark1(handle());
  1423. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1424. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1425. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1426. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1427. benchmark1.set_display(false);
  1428. benchmark1.set_times(RUN);
  1429. for (auto&& arg : args) {
  1430. TensorLayout dst_layout;
  1431. auto opr = handle()->create_operator<ConvBias>();
  1432. opr->param() = arg.param;
  1433. opr->deduce_layout({arg.src, dtype::Int8()},
  1434. {arg.filter, dtype::Int8()},
  1435. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1436. //! dst.nr_elems * IC * FH * FW * 2
  1437. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1438. arg.filter[2] * arg.filter[3] * 2.0 /
  1439. (1024 * 1024 * 1024) * 1e3;
  1440. auto used0 = benchmark0.set_param(arg.param).exec(
  1441. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1442. RUN;
  1443. auto used1 = benchmark1.set_param(arg.param).exec(
  1444. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1445. RUN;
  1446. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1447. "speedup: %f\n",
  1448. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1449. used0, computations / used0, used1, computations / used1,
  1450. used1 / used0);
  1451. }
  1452. }
  1453. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2_WITHDOTPROD) {
  1454. // have to remove preferred restrict in usable func before run the benchmark
  1455. using namespace conv_bias;
  1456. std::vector<TestArg> args;
  1457. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1458. size_t p, NonlineMode nonline_mode) {
  1459. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1460. return;
  1461. param::ConvBias param;
  1462. param.stride_h = 2;
  1463. param.stride_w = 2;
  1464. param.pad_h = p;
  1465. param.pad_w = p;
  1466. param.nonlineMode = nonline_mode;
  1467. //! channel bias
  1468. args.emplace_back(param, TensorShape{2, ic, h, w},
  1469. TensorShape{oc, ic, kernel, kernel},
  1470. TensorShape{1, oc, 1, 1});
  1471. };
  1472. for (size_t kernel : {2, 3, 5, 7})
  1473. for (size_t ic : {1, 8, 16, 32})
  1474. for (size_t oc : {1, 8, 16, 32})
  1475. for (size_t p : {1})
  1476. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1477. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1478. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1479. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1480. }
  1481. constexpr size_t RUN = 50;
  1482. Benchmarker<ConvBias> benchmark0(handle());
  1483. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1484. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1485. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1486. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1487. benchmark0.set_display(false);
  1488. benchmark0.set_times(RUN);
  1489. benchmark0.set_before_exec_callback(
  1490. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD2"));
  1491. Benchmarker<ConvBias> benchmark1(handle());
  1492. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1493. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1494. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1495. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1496. benchmark1.set_display(false);
  1497. benchmark1.set_times(RUN);
  1498. for (auto&& arg : args) {
  1499. TensorLayout dst_layout;
  1500. auto opr = handle()->create_operator<ConvBias>();
  1501. opr->param() = arg.param;
  1502. opr->deduce_layout({arg.src, dtype::Int8()},
  1503. {arg.filter, dtype::Int8()},
  1504. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1505. //! dst.nr_elems * IC * FH * FW * 2
  1506. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1507. arg.filter[2] * arg.filter[3] * 2.0 /
  1508. (1024 * 1024 * 1024) * 1e3;
  1509. auto used0 = benchmark0.set_param(arg.param).exec(
  1510. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1511. RUN;
  1512. auto used1 = benchmark1.set_param(arg.param).exec(
  1513. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1514. RUN;
  1515. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1516. "speedup: %f\n",
  1517. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1518. used0, computations / used0, used1, computations / used1,
  1519. used1 / used0);
  1520. }
  1521. }
  1522. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1_WITHDOTPROD) {
  1523. // have to remove preferred restrict in usable func before run the benchmark
  1524. using namespace conv_bias;
  1525. std::vector<TestArg> args;
  1526. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1527. size_t p, NonlineMode nonline_mode) {
  1528. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1529. return;
  1530. param::ConvBias param;
  1531. param.stride_h = 1;
  1532. param.stride_w = 1;
  1533. param.pad_h = p;
  1534. param.pad_w = p;
  1535. param.nonlineMode = nonline_mode;
  1536. //! channel bias
  1537. args.emplace_back(param, TensorShape{2, ic, h, w},
  1538. TensorShape{oc, ic, kernel, kernel},
  1539. TensorShape{1, oc, 1, 1});
  1540. };
  1541. // clang-format off
  1542. for (size_t kernel : {2, 3, 5, 7})
  1543. for (size_t ic : {1, 8, 16, 32})
  1544. for (size_t oc : {1, 8, 16, 32})
  1545. for (size_t p : {1})
  1546. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1547. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1548. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1549. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1550. }
  1551. // clang-format on
  1552. constexpr size_t RUN = 50;
  1553. Benchmarker<ConvBias> benchmark0(handle());
  1554. benchmark0
  1555. .set_dtype(0,
  1556. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1557. .set_dtype(1,
  1558. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1559. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1560. .set_dtype(4,
  1561. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1562. benchmark0.set_display(false);
  1563. benchmark0.set_times(RUN);
  1564. benchmark0.set_before_exec_callback(
  1565. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD1"));
  1566. Benchmarker<ConvBias> benchmark1(handle());
  1567. benchmark1
  1568. .set_dtype(0,
  1569. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1570. .set_dtype(1,
  1571. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1572. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1573. .set_dtype(4,
  1574. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1575. benchmark1.set_display(false);
  1576. benchmark1.set_times(RUN);
  1577. for (auto&& arg : args) {
  1578. TensorLayout dst_layout;
  1579. auto opr = handle()->create_operator<ConvBias>();
  1580. opr->param() = arg.param;
  1581. opr->deduce_layout({arg.src, dtype::Int8()},
  1582. {arg.filter, dtype::Int8()},
  1583. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1584. //! dst.nr_elems * IC * FH * FW * 2
  1585. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1586. arg.filter[2] * arg.filter[3] * 2.0 /
  1587. (1024 * 1024 * 1024) * 1e3;
  1588. auto used0 = benchmark0.set_param(arg.param).exec(
  1589. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1590. RUN;
  1591. auto used1 = benchmark1.set_param(arg.param).exec(
  1592. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1593. RUN;
  1594. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1595. "speedup: %f\n",
  1596. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1597. used0, computations / used0, used1, computations / used1,
  1598. used1 / used0);
  1599. }
  1600. }
  1601. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2_WITHDOTPROD) {
  1602. // have to remove preferred restrict in usable func before run the benchmark
  1603. using namespace conv_bias;
  1604. std::vector<TestArg> args;
  1605. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1606. size_t p, NonlineMode nonline_mode) {
  1607. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1608. return;
  1609. param::ConvBias param;
  1610. param.stride_h = 2;
  1611. param.stride_w = 2;
  1612. param.pad_h = p;
  1613. param.pad_w = p;
  1614. param.nonlineMode = nonline_mode;
  1615. //! channel bias
  1616. args.emplace_back(param, TensorShape{2, ic, h, w},
  1617. TensorShape{oc, ic, kernel, kernel},
  1618. TensorShape{1, oc, 1, 1});
  1619. };
  1620. // clang-format off
  1621. for (size_t kernel : {2, 3, 5, 7})
  1622. for (size_t ic : {1, 8, 16, 32})
  1623. for (size_t oc : {1, 8, 16, 32})
  1624. for (size_t p : {1})
  1625. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1626. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1627. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1628. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1629. }
  1630. // clang-format on
  1631. constexpr size_t RUN = 50;
  1632. Benchmarker<ConvBias> benchmark0(handle());
  1633. benchmark0
  1634. .set_dtype(0,
  1635. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1636. .set_dtype(1,
  1637. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1638. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1639. .set_dtype(4,
  1640. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1641. benchmark0.set_display(false);
  1642. benchmark0.set_times(RUN);
  1643. benchmark0.set_before_exec_callback(
  1644. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD2"));
  1645. Benchmarker<ConvBias> benchmark1(handle());
  1646. benchmark1
  1647. .set_dtype(0,
  1648. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1649. .set_dtype(1,
  1650. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1651. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1652. .set_dtype(4,
  1653. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1654. benchmark1.set_display(false);
  1655. benchmark1.set_times(RUN);
  1656. for (auto&& arg : args) {
  1657. TensorLayout dst_layout;
  1658. auto opr = handle()->create_operator<ConvBias>();
  1659. opr->param() = arg.param;
  1660. opr->deduce_layout({arg.src, dtype::Int8()},
  1661. {arg.filter, dtype::Int8()},
  1662. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1663. //! dst.nr_elems * IC * FH * FW * 2
  1664. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1665. arg.filter[2] * arg.filter[3] * 2.0 /
  1666. (1024 * 1024 * 1024) * 1e3;
  1667. auto used0 = benchmark0.set_param(arg.param).exec(
  1668. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1669. RUN;
  1670. auto used1 = benchmark1.set_param(arg.param).exec(
  1671. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1672. RUN;
  1673. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1674. "speedup: %f\n",
  1675. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1676. used0, computations / used0, used1, computations / used1,
  1677. used1 / used0);
  1678. }
  1679. }
  1680. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD_NCHW44_DOT) {
  1681. using namespace conv_bias;
  1682. std::vector<TestArg> args;
  1683. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1684. size_t p, size_t stride, NonlineMode nonline_mode) {
  1685. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1686. return;
  1687. param::ConvBias param;
  1688. param.stride_h = stride;
  1689. param.stride_w = stride;
  1690. param.pad_h = p;
  1691. param.pad_w = p;
  1692. param.nonlineMode = nonline_mode;
  1693. param.format = param::ConvBias::Format::NCHW44_DOT;
  1694. //! channel bias
  1695. args.emplace_back(param, TensorShape{1, ic / 4, h, w, 4},
  1696. TensorShape{oc / 4, ic / 4, kernel, kernel, 4, 4},
  1697. TensorShape{1, oc / 4, 1, 1, 4});
  1698. };
  1699. for (size_t stride : {1, 2})
  1700. for (size_t kernel : {2, 3, 5, 7})
  1701. for (size_t oc : {64})
  1702. for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
  1703. run(oc, oc, 56, 56, kernel, kernel / 2, stride,
  1704. nonline_mode);
  1705. }
  1706. constexpr size_t RUN = 50;
  1707. Benchmarker<ConvBias> benchmark0(handle());
  1708. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1709. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1710. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1711. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1712. benchmark0.set_display(false);
  1713. benchmark0.set_times(RUN);
  1714. benchmark0.set_before_exec_callback(
  1715. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1716. "ARMDOTS8DIRECT_NCHW44"));
  1717. Benchmarker<ConvBias> benchmark1(handle());
  1718. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1719. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1720. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1721. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1722. benchmark1.set_display(false);
  1723. benchmark1.set_times(RUN);
  1724. for (auto&& arg : args) {
  1725. TensorLayout dst_layout;
  1726. auto opr = handle()->create_operator<ConvBias>();
  1727. opr->param() = arg.param;
  1728. opr->deduce_layout({arg.src, dtype::Int8()},
  1729. {arg.filter, dtype::Int8()},
  1730. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1731. //! dst.nr_elems * IC * FH * FW * 2
  1732. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1733. arg.filter[2] * arg.filter[3] * 8.0 /
  1734. (1024 * 1024 * 1024) * 1e3;
  1735. auto used0 = benchmark0.set_param(arg.param).exec(
  1736. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1737. RUN;
  1738. auto used1 = benchmark1.set_param(arg.param).exec(
  1739. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1740. RUN;
  1741. printf("%s %s: Direct use: %f ms %f Gflops normal: %f ms %f GFlops "
  1742. "speedup: %f\n",
  1743. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1744. used0, computations / used0, used1, computations / used1,
  1745. used1 / used0);
  1746. }
  1747. }
  1748. #endif
  1749. #endif
  1750. /*====================== BENCHMARK CONV1X1 ===========================*/
  1751. #if MEGDNN_WITH_BENCHMARK
  1752. namespace {
  1753. std::vector<conv_bias::TestArg> get_conv_bias_1x1_benchmark_args(
  1754. size_t pack_size = 1) {
  1755. using namespace conv_bias;
  1756. std::vector<TestArg> args;
  1757. param::ConvBias param;
  1758. param.stride_h = 1;
  1759. param.stride_w = 1;
  1760. param.pad_h = 0;
  1761. param.pad_w = 0;
  1762. param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1763. auto bench_case = [&](size_t OC, size_t IC, size_t H, size_t W) {
  1764. if (pack_size == 1)
  1765. args.emplace_back(param, TensorShape{1, IC, H, W},
  1766. TensorShape{OC, IC, 1, 1}, TensorShape{});
  1767. else {
  1768. if (pack_size == 4)
  1769. param.format = param::ConvBias::Format::NCHW44;
  1770. args.emplace_back(param,
  1771. TensorShape{1, IC / pack_size, H, W, pack_size},
  1772. TensorShape{OC / pack_size, IC / pack_size, 1, 1,
  1773. pack_size, pack_size},
  1774. TensorShape{});
  1775. }
  1776. };
  1777. //! MobileNetV1
  1778. bench_case(64, 32, 112, 112);
  1779. bench_case(128, 64, 56, 56);
  1780. bench_case(128, 128, 56, 56);
  1781. bench_case(256, 128, 28, 28);
  1782. bench_case(256, 256, 28, 28);
  1783. bench_case(512, 256, 14, 14);
  1784. bench_case(512, 512, 14, 14);
  1785. bench_case(1024, 512, 7, 7);
  1786. bench_case(1024, 1024, 7, 7);
  1787. //! MobileNetV2
  1788. bench_case(16, 32, 112, 112);
  1789. bench_case(96, 16, 112, 112);
  1790. bench_case(144, 24, 56, 56);
  1791. bench_case(192, 32, 28, 28);
  1792. bench_case(384, 64, 28, 28);
  1793. bench_case(576, 96, 14, 14);
  1794. bench_case(960, 160, 7, 7);
  1795. bench_case(320, 960, 7, 7);
  1796. bench_case(1280, 320, 7, 7);
  1797. //! MobileNetV3-Large
  1798. bench_case(64, 16, 112, 112);
  1799. bench_case(72, 24, 56, 56);
  1800. bench_case(120, 40, 28, 28);
  1801. bench_case(240, 40, 28, 28);
  1802. bench_case(200, 80, 14, 14);
  1803. bench_case(184, 80, 14, 14);
  1804. bench_case(480, 80, 14, 14);
  1805. bench_case(672, 112, 14, 14);
  1806. //! MobileNetV3-Small
  1807. bench_case(72, 16, 56, 56);
  1808. bench_case(88, 24, 28, 28);
  1809. bench_case(96, 24, 28, 28);
  1810. bench_case(240, 40, 14, 14);
  1811. bench_case(120, 40, 14, 14);
  1812. bench_case(144, 48, 14, 14);
  1813. bench_case(288, 48, 14, 14);
  1814. bench_case(576, 96, 7, 7);
  1815. //! resnet50
  1816. bench_case(256, 64, 56, 56);
  1817. bench_case(512, 128, 28, 28);
  1818. bench_case(1024, 256, 14, 14);
  1819. bench_case(2048, 512, 7, 7);
  1820. return args;
  1821. }
  1822. void benchmark_conv1x1(const char* matmul_algo_name, Handle* handle,
  1823. DType stype, DType matmul_dtype, DType bias_type,
  1824. DType conv_dtype, bool is_mk4 = false) {
  1825. using namespace conv_bias;
  1826. int pack_size = is_mk4 ? 4 : 1;
  1827. std::vector<TestArg> conv_bias_1x1_args =
  1828. get_conv_bias_1x1_benchmark_args(pack_size);
  1829. constexpr size_t RUNS = 50;
  1830. param::MatrixMul param;
  1831. param.transposeA = false;
  1832. param.transposeB = false;
  1833. if (is_mk4) {
  1834. param.format = MatrixMul::Param::Format::MK4;
  1835. }
  1836. Benchmarker<MatrixMul> benchmark_matmul(handle);
  1837. benchmark_matmul.set_before_exec_callback(
  1838. AlgoChecker<MatrixMul>(matmul_algo_name));
  1839. benchmark_matmul.set_times(RUNS)
  1840. .set_dtype(0, stype)
  1841. .set_dtype(1, stype)
  1842. .set_dtype(2, matmul_dtype)
  1843. .set_param(param)
  1844. .set_display(false);
  1845. std::string conv1x1_algo_name = ssprintf("CONV1x1:%s:24", matmul_algo_name);
  1846. Benchmarker<ConvBias> benchmark_conv1x1(handle);
  1847. benchmark_conv1x1.set_before_exec_callback(
  1848. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1849. conv1x1_algo_name.c_str()));
  1850. benchmark_conv1x1.set_times(RUNS)
  1851. .set_dtype(0, stype)
  1852. .set_dtype(1, stype)
  1853. .set_dtype(2, bias_type)
  1854. .set_dtype(4, conv_dtype)
  1855. .set_display(false);
  1856. for (auto&& arg : conv_bias_1x1_args) {
  1857. size_t IC = arg.src[1];
  1858. size_t OH = arg.src[2];
  1859. size_t OW = arg.src[3];
  1860. size_t OC = arg.filter[0];
  1861. size_t M = OC * pack_size;
  1862. size_t K = IC * pack_size;
  1863. size_t N = OH * OW;
  1864. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  1865. TensorShape A, B;
  1866. A = TensorShape{M, K};
  1867. B = TensorShape{K, N};
  1868. if (is_mk4) {
  1869. A = TensorShape{M / 4, K / 4, 4, 4};
  1870. B = TensorShape{K / 4, N, 4};
  1871. }
  1872. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  1873. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1874. RUNS;
  1875. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  1876. printf("%s %s:\n matmul: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  1877. "speedup: "
  1878. "%f\n",
  1879. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1880. matmul_used, computations / matmul_used, conv1x1_used,
  1881. computations / conv1x1_used, matmul_used / conv1x1_used);
  1882. }
  1883. }
  1884. } // namespace
  1885. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F32) {
  1886. #if MEGDNN_AARCH64
  1887. benchmark_conv1x1("AARCH64_F32K8X12X1", handle(), dtype::Float32{},
  1888. dtype::Float32{}, dtype::Float32{}, dtype::Float32{});
  1889. #else
  1890. benchmark_conv1x1("ARMV7_F32", handle(), dtype::Float32{}, dtype::Float32{},
  1891. dtype::Float32{}, dtype::Float32{});
  1892. #endif
  1893. }
  1894. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  1895. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F16) {
  1896. #if MEGDNN_AARCH64
  1897. benchmark_conv1x1("AARCH64_F16_K8X24X1", handle(), dtype::Float16{},
  1898. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1899. #else
  1900. benchmark_conv1x1("AARCH32_F16_K4X16X1", handle(), dtype::Float16{},
  1901. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1902. #endif
  1903. }
  1904. #endif
  1905. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDSYM) {
  1906. dtype::QuantizedS8 stype(2.5f);
  1907. dtype::QuantizedS32 dtype(6.25f);
  1908. #if MEGDNN_AARCH64
  1909. #if __ARM_FEATURE_DOTPROD
  1910. benchmark_conv1x1("AARCH64_INT8X8X32_K8X12X4_DOTPROD", handle(), stype,
  1911. dtype, dtype, dtype);
  1912. #else
  1913. benchmark_conv1x1("AARCH64_INT8X8X32_K8X8X8", handle(), stype, dtype, dtype,
  1914. dtype);
  1915. benchmark_conv1x1("AARCH64_INT8X8X32_K4X4X16", handle(), stype, dtype,
  1916. dtype, dtype);
  1917. #endif
  1918. #elif MEGDNN_ARMV7
  1919. benchmark_conv1x1("ARMV7_INT8X8X32_K4X8X8", handle(), stype, dtype, dtype,
  1920. dtype);
  1921. #endif
  1922. }
  1923. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDASYM) {
  1924. dtype::Quantized8Asymm stype(1.2f, (uint8_t)125);
  1925. dtype::QuantizedS32 dtype(1.2 * 1.2);
  1926. #if MEGDNN_AARCH64
  1927. #if __ARM_FEATURE_DOTPROD
  1928. benchmark_conv1x1("AARCH64_QUINT8_K8X8X4_DOTPROD", handle(), stype, dtype,
  1929. dtype, dtype);
  1930. #else
  1931. benchmark_conv1x1("AARCH64_QUINT8_K8X8X8", handle(), stype, dtype, dtype,
  1932. dtype);
  1933. #endif
  1934. #elif MEGDNN_ARMV7
  1935. benchmark_conv1x1("ARMV7_QUINT8_K4X8X8", handle(), stype, dtype, dtype,
  1936. dtype);
  1937. #endif
  1938. }
  1939. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_INT8x8x16) {
  1940. #if MEGDNN_AARCH64
  1941. benchmark_conv1x1("AARCH64_INT8X8X16_K8X8X8", handle(), dtype::Int8{},
  1942. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1943. benchmark_conv1x1("AARCH64_INT8X8X16_K4X4X16", handle(), dtype::Int8{},
  1944. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1945. #elif MEGDNN_ARMV7
  1946. benchmark_conv1x1("ARMV7_INT8X8X16_K4X8X8", handle(), dtype::Int8{},
  1947. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1948. benchmark_conv1x1("ARMV7_INT8X8X16_K4X2X16", handle(), dtype::Int8{},
  1949. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1950. benchmark_conv1x1("ARMV7_INT8X8X16_MK4_K8X8X4", handle(), dtype::Int8{},
  1951. dtype::Int16{}, dtype::Int16{}, dtype::Int16{}, true);
  1952. #endif
  1953. }
  1954. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_GEMV_FP32) {
  1955. using namespace conv_bias;
  1956. std::vector<conv_bias::TestArg> args;
  1957. param::ConvBias conv_param;
  1958. conv_param.stride_h = 1;
  1959. conv_param.stride_w = 1;
  1960. conv_param.pad_h = 0;
  1961. conv_param.pad_w = 0;
  1962. conv_param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1963. auto run = [&](size_t M, size_t K) {
  1964. args.emplace_back(conv_param, TensorShape{1, K, 1, 1},
  1965. TensorShape{M, K, 1, 1}, TensorShape{});
  1966. };
  1967. for (size_t M : {4, 64, 1024, 4096})
  1968. for (size_t K : {128, 256, 1024, 4096})
  1969. run(M, K);
  1970. constexpr size_t RUNS = 50;
  1971. param::MatrixMul param;
  1972. param.transposeA = false;
  1973. param.transposeB = false;
  1974. Benchmarker<MatrixMul> benchmark_matmul(handle());
  1975. benchmark_matmul.set_before_exec_callback(
  1976. AlgoChecker<MatrixMul>("ARM_COMMON_F32_GEMV"));
  1977. benchmark_matmul.set_times(RUNS)
  1978. .set_dtype(0, dtype::Float32{})
  1979. .set_dtype(1, dtype::Float32{})
  1980. .set_dtype(2, dtype::Float32{})
  1981. .set_param(param)
  1982. .set_display(false);
  1983. Benchmarker<ConvBias> benchmark_conv1x1(handle());
  1984. benchmark_conv1x1.set_before_exec_callback(
  1985. conv_bias::ConvBiasAlgoChecker<ConvBias>("CONV1x1_GEMV"));
  1986. benchmark_conv1x1.set_times(RUNS)
  1987. .set_dtype(0, dtype::Float32{})
  1988. .set_dtype(1, dtype::Float32{})
  1989. .set_dtype(2, dtype::Float32{})
  1990. .set_dtype(4, dtype::Float32{})
  1991. .set_display(false);
  1992. std::cout << "warm up:\n";
  1993. for (int i = 0; i < 50; i++) {
  1994. benchmark_matmul.exec({{1, 1024}, {1024, 512}, {}});
  1995. benchmark_matmul.set_display(true);
  1996. }
  1997. for (auto&& arg : args) {
  1998. size_t IC = arg.src[1];
  1999. size_t OH = arg.src[2];
  2000. size_t OW = arg.src[3];
  2001. size_t OC = arg.filter[0];
  2002. size_t M = OC;
  2003. size_t K = IC;
  2004. size_t N = OH * OW;
  2005. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2006. TensorShape A, B;
  2007. A = TensorShape{M, K};
  2008. B = TensorShape{K, N};
  2009. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  2010. {arg.src, arg.filter, arg.bias, {}, {}}) /
  2011. RUNS;
  2012. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  2013. printf("%s %s:\n gemv: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  2014. "speedup: "
  2015. "%f\n",
  2016. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2017. matmul_used, computations / matmul_used, conv1x1_used,
  2018. computations / conv1x1_used, matmul_used / conv1x1_used);
  2019. }
  2020. }
  2021. #ifndef __ARM_FEATURE_DOTPROD
  2022. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_1X1_S1_NCHW_VS_NCHW44_INT8x8x32) {
  2023. std::vector<TestArg> conv_bias_1x1_args_nchw44 =
  2024. get_conv_bias_1x1_benchmark_args(4);
  2025. std::vector<TestArg> conv_bias_1x1_args_nchw =
  2026. get_conv_bias_1x1_benchmark_args(1);
  2027. constexpr size_t RUNS = 50;
  2028. Benchmarker<ConvBias> benchmark_conv1x1_nchw44(handle());
  2029. benchmark_conv1x1_nchw44.set_before_exec_callback(
  2030. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2031. "CONV1x1:AARCH64_INT8X8X32_MK4_4X4X16:24"));
  2032. benchmark_conv1x1_nchw44.set_times(RUNS)
  2033. .set_dtype(0, dtype::Int8())
  2034. .set_dtype(1, dtype::Int8())
  2035. .set_dtype(2, dtype::Int32())
  2036. .set_dtype(4, dtype::Int32())
  2037. .set_display(false);
  2038. Benchmarker<ConvBias> benchmark_conv1x1_nchw(handle());
  2039. benchmark_conv1x1_nchw.set_before_exec_callback(
  2040. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  2041. "CONV1x1:AARCH64_INT8X8X32_K4X4X16:24"));
  2042. benchmark_conv1x1_nchw.set_times(RUNS)
  2043. .set_dtype(0, dtype::Int8())
  2044. .set_dtype(1, dtype::Int8())
  2045. .set_dtype(2, dtype::Int32())
  2046. .set_dtype(4, dtype::Int32())
  2047. .set_display(false);
  2048. for (size_t i = 0; i < conv_bias_1x1_args_nchw44.size(); ++i) {
  2049. auto&& arg_nchw = conv_bias_1x1_args_nchw[i];
  2050. auto&& arg_nchw44 = conv_bias_1x1_args_nchw44[i];
  2051. size_t IC = arg_nchw.src[1];
  2052. size_t OH = arg_nchw.src[2];
  2053. size_t OW = arg_nchw.src[3];
  2054. size_t OC = arg_nchw.filter[0];
  2055. size_t M = OC;
  2056. size_t K = IC;
  2057. size_t N = OH * OW;
  2058. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  2059. auto conv1x1_nchw = benchmark_conv1x1_nchw.set_param(arg_nchw.param)
  2060. .exec({arg_nchw.src,
  2061. arg_nchw.filter,
  2062. arg_nchw.bias,
  2063. {},
  2064. {}}) /
  2065. RUNS;
  2066. auto conv1x1_nchw44 =
  2067. benchmark_conv1x1_nchw44.set_param(arg_nchw44.param)
  2068. .exec({arg_nchw44.src,
  2069. arg_nchw44.filter,
  2070. arg_nchw44.bias,
  2071. {},
  2072. {}}) /
  2073. RUNS;
  2074. printf("%s %s:\n conv_1x1_nchw: %f ms %f Gflops\nconv1x1_nchw44: %f ms "
  2075. "%f GFlops "
  2076. "speedup: "
  2077. "%f\n",
  2078. arg_nchw.src.to_string().c_str(),
  2079. arg_nchw.filter.to_string().c_str(), conv1x1_nchw,
  2080. computations / conv1x1_nchw, conv1x1_nchw44,
  2081. computations / conv1x1_nchw44, conv1x1_nchw / conv1x1_nchw44);
  2082. }
  2083. }
  2084. #endif
  2085. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_WINOGRAD_VS_IM2COL_INT8) {
  2086. auto&& args = get_winograd_benchmark_args(3, 8);
  2087. using namespace conv_bias;
  2088. constexpr size_t RUN = 10;
  2089. Benchmarker<ConvBias> benchmark_im2col(handle());
  2090. benchmark_im2col.set_display(false);
  2091. benchmark_im2col.set_times(RUN);
  2092. benchmark_im2col.set_dtype(0, dtype::QuantizedS8(2.5f))
  2093. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2094. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2095. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2096. Benchmarker<ConvBias> benchmark_winograd(handle());
  2097. benchmark_winograd.set_display(false);
  2098. benchmark_winograd.set_times(RUN);
  2099. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  2100. .set_dtype(1, dtype::QuantizedS8(2.5f))
  2101. .set_dtype(2, dtype::QuantizedS32(6.25f))
  2102. .set_dtype(4, dtype::QuantizedS8(60.25f));
  2103. for (auto&& arg : args) {
  2104. TensorLayout dst_layout;
  2105. auto opr = handle()->create_operator<ConvBias>();
  2106. opr->param() = arg.param;
  2107. opr->deduce_layout({arg.src, dtype::Float32()},
  2108. {arg.filter, dtype::Float32()},
  2109. {arg.bias, dtype::Float32()}, {}, dst_layout);
  2110. //! dst.nr_elems * IC * FH * FW * 2
  2111. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  2112. arg.filter[2] * arg.filter[3] * 2.0 /
  2113. (1024 * 1024 * 1024) * 1e3;
  2114. benchmark_im2col.set_param(arg.param);
  2115. auto im2col_used =
  2116. algo_benchmark<ConvBias>(
  2117. benchmark_im2col, {arg.src, arg.filter, {}, {}, {}},
  2118. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16") /
  2119. RUN;
  2120. benchmark_winograd.set_param(arg.param);
  2121. auto winograd_used =
  2122. algo_benchmark<ConvBias>(
  2123. benchmark_winograd, {arg.src, arg.filter, {}, {}, {}},
  2124. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2") /
  2125. RUN;
  2126. printf("%s %s: im2col: %f ms %f Gflops winograd: %f ms %f GFlops "
  2127. "speedup: "
  2128. "%f\n",
  2129. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  2130. im2col_used, computations / im2col_used, winograd_used,
  2131. computations / winograd_used, im2col_used / winograd_used);
  2132. }
  2133. }
  2134. #endif
  2135. // vim: syntax=cpp.doxygen

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