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

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