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

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