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

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

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