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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. /**
  2. * \file dnn/test/arm_common/conv_bias.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #include "megdnn/dtype.h"
  13. #include "test/arm_common/fixture.h"
  14. #include "megdnn/opr_param_defs.h"
  15. #include "megdnn/oprs.h"
  16. #include "src/fallback/conv_bias/common.h"
  17. #include "test/common/benchmarker.h"
  18. #include "test/common/checker.h"
  19. #include "test/common/conv_bias.h"
  20. #include "test/common/rng.h"
  21. #include "test/common/tensor.h"
  22. #include "test/common/workspace_wrapper.h"
  23. using namespace megdnn;
  24. using namespace test;
  25. using namespace conv_bias;
  26. //! TODO this algo current does not support multithread
  27. TEST_F(ARM_COMMON, CONVBIAS_INT8_INT8_INT16_STRIDE2F2) {
  28. checker_conv_bias_int8x8x16(get_conv_bias_args({2}, 2, true, true, true),
  29. handle(), "I8816STRD2F2");
  30. }
  31. TEST_F(ARM_COMMON, CONV_BIAS_MATMUL) {
  32. using namespace conv_bias;
  33. std::vector<TestArg> args = get_quantized_args();
  34. Checker<ConvBiasForward> checker(handle());
  35. checker.set_before_exec_callback(
  36. conv_bias::ConvBiasAlgoChecker<ConvBias>("S8MATMUL"));
  37. #if MEGDNN_ARMV7
  38. checker.set_epsilon(1);
  39. #endif
  40. UniformIntRNG rng{-50, 50};
  41. for (auto&& arg : args) {
  42. if (arg.bias.ndim == 4 && arg.bias[2] != 1 && arg.bias[3] != 1)
  43. continue;
  44. checker.set_dtype(0, dtype::QuantizedS8(0.41113496f))
  45. .set_dtype(1, dtype::QuantizedS8(0.01887994f))
  46. .set_dtype(2, dtype::QuantizedS32(0.41113496f * 0.01887994f))
  47. .set_dtype(4, dtype::QuantizedS8(0.49550694f))
  48. .set_rng(0, &rng)
  49. .set_rng(1, &rng)
  50. .set_rng(2, &rng)
  51. .set_param(arg.param)
  52. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  53. }
  54. }
  55. #define CONV_BIAS_MATMUL_QU8_MODE(MODE) \
  56. using namespace conv_bias; \
  57. std::vector<TestArg> args = get_quantized_args_with_nlmode(MODE); \
  58. Checker<ConvBiasForward> checker(handle()); \
  59. checker.set_before_exec_callback( \
  60. conv_bias::ConvBiasAlgoChecker<ConvBias>("QU8MATMUL")); \
  61. UniformIntRNG rng{0, 127}; \
  62. for (auto&& arg : args) { \
  63. if (arg.bias.ndim == 4 && arg.bias[2] != 1 && arg.bias[3] != 1) \
  64. continue; \
  65. checker.set_dtype(0, dtype::Quantized8Asymm( \
  66. 2.5f, static_cast<uint8_t>(127))) \
  67. .set_dtype(1, dtype::Quantized8Asymm( \
  68. 2.7f, static_cast<uint8_t>(126))) \
  69. .set_dtype(2, dtype::QuantizedS32(6.75f)) \
  70. .set_dtype(4, dtype::Quantized8Asymm( \
  71. 60.25f, static_cast<uint8_t>(125))) \
  72. .set_rng(0, &rng) \
  73. .set_rng(1, &rng) \
  74. .set_rng(2, &rng) \
  75. .set_param(arg.param) \
  76. .execs({arg.src, arg.filter, arg.bias, {}, {}}); \
  77. }
  78. #define MODE_STR(mode) param::ConvBias::NonlineMode::mode
  79. #define CB_TEST(MODE) \
  80. TEST_F(ARM_COMMON, CONV_BIAS_MATMUL_QU8_##MODE) { \
  81. CONV_BIAS_MATMUL_QU8_MODE(MODE_STR(MODE)); \
  82. }
  83. CB_TEST(IDENTITY);
  84. CB_TEST(RELU);
  85. CB_TEST(H_SWISH);
  86. #undef MODE_STR
  87. #undef CB_TEST
  88. #undef CONV_BIAS_MATMUL_QU8_MODE
  89. #if MEGDNN_WITH_BENCHMARK
  90. static void benchmark_convbias(Handle* handle, bool is_fp32 = false) {
  91. constexpr size_t RUNS = 30;
  92. Benchmarker<ConvBias> benchmarker_int(handle);
  93. benchmarker_int.set_times(RUNS)
  94. .set_dtype(0, dtype::QuantizedS8(2.5))
  95. .set_dtype(1, dtype::QuantizedS8(2.5))
  96. .set_dtype(2, dtype::QuantizedS32(6.25))
  97. .set_dtype(4, dtype::QuantizedS8(60.25))
  98. .set_display(false);
  99. benchmarker_int.set_before_exec_callback(
  100. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  101. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16:384"));
  102. Benchmarker<ConvBias> benchmarker_float(handle);
  103. benchmarker_float.set_display(false).set_times(RUNS);
  104. benchmarker_float.set_before_exec_callback(
  105. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  106. "IM2COLMATMUL:AARCH64_F32K8X12X1:192"));
  107. Benchmarker<ConvBias> benchmarker_int_nchw44(handle);
  108. if (is_fp32) {
  109. benchmarker_int_nchw44.set_times(RUNS)
  110. .set_dtype(0, dtype::Float32())
  111. .set_dtype(1, dtype::Float32())
  112. .set_dtype(2, dtype::Float32())
  113. .set_dtype(4, dtype::Float32())
  114. .set_display(false);
  115. } else {
  116. benchmarker_int_nchw44.set_times(RUNS)
  117. .set_dtype(0, dtype::QuantizedS8(2.5))
  118. .set_dtype(1, dtype::QuantizedS8(2.5))
  119. .set_dtype(2, dtype::QuantizedS32(6.25))
  120. .set_dtype(4, dtype::QuantizedS8(60.25))
  121. .set_display(false);
  122. }
  123. benchmarker_int_nchw44.set_before_exec_callback(
  124. conv_bias::ConvBiasAlgoChecker<ConvBias>(".+"));
  125. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  126. size_t FS, size_t stride, bool input_nchw = false) {
  127. param::ConvBias param;
  128. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  129. param.stride_h = stride;
  130. param.stride_w = stride;
  131. param.pad_h = FS / 2;
  132. param.pad_w = FS / 2;
  133. auto OH = (H + 2 * param.pad_h - FS) /
  134. static_cast<size_t>(param.stride_h) +
  135. 1;
  136. auto OW = (W + 2 * param.pad_w - FS) /
  137. static_cast<size_t>(param.stride_w) +
  138. 1;
  139. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  140. bias({1, OC, 1, 1}), dst({N, OC, OH, OW});
  141. param.format = param::ConvBias::Format::NCHW;
  142. auto int_used = benchmarker_int.set_param(param).exec(
  143. {src, filter, bias, {}, dst}) /
  144. RUNS;
  145. auto float_used = benchmarker_float.set_param(param).exec(
  146. {src, filter, bias, {}, dst}) /
  147. RUNS;
  148. param.format = param::ConvBias::Format::NCHW44;
  149. src = {N, IC / 4, H, W, 4};
  150. filter = {OC / 4, IC / 4, FS, FS, 4, 4};
  151. if (input_nchw) {
  152. src = {N, IC, H, W};
  153. filter = {OC / 4, FS, FS, IC, 4};
  154. }
  155. bias = {1, OC / 4, 1, 1, 4};
  156. dst = {N, OC / 4, OH, OW, 4};
  157. auto int_nchw44_used = benchmarker_int_nchw44.set_param(param).exec(
  158. {src, filter, bias, {}, dst}) /
  159. RUNS;
  160. float computations = IC * (FS * FS) * dst.total_nr_elems() * 2 * 1e-6;
  161. printf("run: %s %s %s->%s \n", src.to_string().c_str(),
  162. filter.to_string().c_str(), bias.to_string().c_str(),
  163. dst.to_string().c_str());
  164. printf("float: %f ms %f Gflops, ", float_used,
  165. computations / float_used);
  166. printf("int_nchw: %f ms %f Gflops, ", int_used,
  167. computations / int_used);
  168. auto speed_up = int_used / int_nchw44_used;
  169. if (is_fp32) {
  170. speed_up = float_used / int_nchw44_used;
  171. printf("fp32_nchw44: %f ms %f Gflops %f speedup, ", int_nchw44_used,
  172. computations / int_nchw44_used, speed_up);
  173. } else {
  174. printf("int_nchw44: %f ms %f Gflops %f speedup, ", int_nchw44_used,
  175. computations / int_nchw44_used, speed_up);
  176. }
  177. printf("\n");
  178. };
  179. if (is_fp32) {
  180. run(1, 1, 4, 112, 112, 2, 2, true);
  181. run(1, 3, 32, 224, 224, 3, 2, true);
  182. run(1, 3, 64, 224, 224, 7, 2, true);
  183. run(1, 64, 128, 56, 56, 3, 2, false);
  184. run(1, 128, 256, 28, 28, 3, 2, false);
  185. run(1, 256, 512, 14, 14, 3, 2, false);
  186. run(1, 128, 128, 28, 28, 3, 1, false);
  187. run(1, 256, 256, 14, 14, 3, 1, false);
  188. run(1, 512, 512, 7, 7, 3, 1, false);
  189. } else {
  190. for (size_t stride : {1, 2}) {
  191. printf("stride %zu\n", stride);
  192. for (size_t filter_size : {2, 3, 5, 7}) {
  193. for (size_t img_size : {32}) {
  194. for (size_t channel : {8, 16, 32, 64, 128, 256}) {
  195. run(1, channel, channel, img_size, img_size,
  196. filter_size, stride, false);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_NCHW44) {
  204. benchmark_convbias(handle(), true);
  205. }
  206. TEST_F(ARM_COMMON_MULTI_THREADS, BENCHMARK_CONVBIAS_NCHW44) {
  207. benchmark_convbias(handle(), true);
  208. }
  209. #endif
  210. TEST_F(ARM_COMMON, CONV_BIAS_MATMUL_QS8) {
  211. using namespace conv_bias;
  212. std::vector<TestArg> args = get_quantized_args();
  213. Checker<ConvBiasForward> checker(handle());
  214. checker.set_before_exec_callback(
  215. conv_bias::ConvBiasAlgoChecker<ConvBias>("S8MATMUL"));
  216. #if MEGDNN_ARMV7
  217. checker.set_epsilon(1);
  218. #endif
  219. UniformIntRNG rng{0, 255};
  220. for (auto&& arg : args) {
  221. if (arg.bias.ndim == 4 && arg.bias[2] != 1 && arg.bias[3] != 1)
  222. continue;
  223. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  224. .set_dtype(1, dtype::QuantizedS8(2.7f))
  225. .set_dtype(2, dtype::QuantizedS32(6.75f))
  226. .set_dtype(4, dtype::QuantizedS8(60.25f))
  227. .set_rng(0, &rng)
  228. .set_rng(1, &rng)
  229. .set_rng(2, &rng)
  230. .set_param(arg.param)
  231. .execs({arg.src, arg.filter, arg.bias, {}, {}});
  232. }
  233. }
  234. #if MEGDNN_ARMV7
  235. TEST_F(ARM_COMMON, CONV_BIAS_RESCALE_OP) {
  236. using namespace conv_bias;
  237. Checker<ConvBias> checker(handle());
  238. checker.set_before_exec_callback(
  239. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8MATMUL"));
  240. checker.set_epsilon(1).set_max_avg_error(1e-2).set_max_avg_biased_error(
  241. 1e-3);
  242. UniformIntRNG rng{-128, 127};
  243. checker.set_dtype(0, dtype::QuantizedS8(0.41113496f))
  244. .set_dtype(1, dtype::QuantizedS8(0.01887994f))
  245. .set_dtype(2, dtype::QuantizedS32(0.41113496f * 0.01887994f))
  246. .set_dtype(4, dtype::QuantizedS8(0.49550694f))
  247. .set_rng(0, &rng)
  248. .set_rng(1, &rng)
  249. .set_rng(2, &rng);
  250. param::ConvBias param;
  251. param.stride_h = 1;
  252. param.stride_w = 1;
  253. param.pad_h = 0;
  254. param.pad_w = 0;
  255. param.nonlineMode = NonlineMode::IDENTITY;
  256. //! Unary op
  257. checker.set_param(param).exec({TensorShape{2, 1, 128, 128},
  258. TensorShape{16, 1, 2, 2},
  259. TensorShape{},
  260. TensorShape{},
  261. {}});
  262. //! Binary op
  263. checker.set_param(param).exec({TensorShape{2, 1, 128, 128},
  264. TensorShape{16, 1, 2, 2},
  265. TensorShape{1, 16, 1, 1},
  266. TensorShape{},
  267. {}});
  268. }
  269. #endif
  270. #if MEGDNN_WITH_BENCHMARK
  271. void benchmark_im2col(const char* algo_name, const char* im2col_name,
  272. Handle* handle, size_t kernel, size_t pack_size = 1) {
  273. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  274. using namespace conv_bias;
  275. constexpr size_t RUN = 10;
  276. Benchmarker<ConvBias> benchmark(handle);
  277. benchmark.set_display(false);
  278. benchmark.set_times(RUN);
  279. Benchmarker<ConvBias> benchmark_im2col(handle);
  280. benchmark_im2col.set_display(false);
  281. benchmark_im2col.set_times(RUN);
  282. for (auto&& arg : args) {
  283. TensorLayout dst_layout;
  284. auto opr = handle->create_operator<ConvBias>();
  285. opr->param() = arg.param;
  286. opr->deduce_layout({arg.src, dtype::Float32()},
  287. {arg.filter, dtype::Float32()},
  288. {arg.bias, dtype::Float32()}, {}, dst_layout);
  289. //! dst.nr_elems * IC * FH * FW * 2
  290. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  291. arg.filter[2] * arg.filter[3] * 2.0 /
  292. (1024 * 1024 * 1024) * 1e3;
  293. benchmark.set_param(arg.param);
  294. auto used = algo_benchmark<ConvBias>(benchmark,
  295. {arg.src, arg.filter, {}, {}, {}},
  296. algo_name) /
  297. RUN;
  298. benchmark_im2col.set_param(arg.param);
  299. auto used_im2col =
  300. algo_benchmark<ConvBias>(benchmark_im2col,
  301. {arg.src, arg.filter, {}, {}, {}},
  302. im2col_name) /
  303. RUN;
  304. printf("%s %s: normal: %f ms %f Gflops im2col: %f ms %f GFlops "
  305. "speedup: "
  306. "%f\n",
  307. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  308. used, computations / used, used_im2col,
  309. computations / used_im2col, used / used_im2col);
  310. }
  311. }
  312. void benchmark_im2col_single_algo(const char* im2col_name, Handle* handle,
  313. size_t kernel, size_t pack_size = 1) {
  314. std::vector<conv_bias::TestArg> args;
  315. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  316. size_t p) {
  317. if (ic % pack_size != 0 || oc % pack_size != 0)
  318. return;
  319. if (w + 2 * p < kernel || h + 2 * p < kernel)
  320. return;
  321. param::ConvBias param;
  322. param.stride_h = 1;
  323. param.stride_w = 1;
  324. param.pad_h = p;
  325. param.pad_w = p;
  326. args.push_back(conv_bias::TestArg{param,
  327. TensorShape{1, ic, h, w},
  328. TensorShape{oc, ic, kernel, kernel},
  329. {1, oc, 1, 1}});
  330. };
  331. pack(1, 64, 100, 100, kernel, 1);
  332. pack(8, 64, 100, 100, kernel, 1);
  333. pack(16, 64, 100, 100, kernel, 1);
  334. pack(32, 64, 100, 100, kernel, 1);
  335. pack(64, 64, 100, 100, kernel, 1);
  336. pack(128, 64, 100, 100, kernel, 1);
  337. pack(256, 64, 100, 100, kernel, 1);
  338. pack(512, 64, 100, 100, kernel, 1);
  339. pack(1024, 64, 100, 100, kernel, 1);
  340. pack(1, 64, 10, 10, kernel, 1);
  341. pack(8, 64, 10, 10, kernel, 1);
  342. pack(16, 64, 10, 10, kernel, 1);
  343. pack(32, 64, 10, 10, kernel, 1);
  344. pack(64, 64, 10, 10, kernel, 1);
  345. pack(128, 64, 10, 10, kernel, 1);
  346. pack(256, 64, 10, 10, kernel, 1);
  347. pack(512, 64, 10, 10, kernel, 1);
  348. pack(1024, 64, 10, 10, kernel, 1);
  349. pack(1, 16, 10, 10, kernel, 1);
  350. pack(8, 16, 10, 10, kernel, 1);
  351. pack(16, 16, 10, 10, kernel, 1);
  352. pack(32, 16, 10, 10, kernel, 1);
  353. pack(64, 16, 10, 10, kernel, 1);
  354. pack(128, 16, 10, 10, kernel, 1);
  355. pack(256, 16, 10, 10, kernel, 1);
  356. pack(512, 16, 10, 10, kernel, 1);
  357. pack(1024, 16, 10, 10, kernel, 1);
  358. using namespace conv_bias;
  359. constexpr size_t RUN = 20;
  360. Benchmarker<ConvBias> benchmark_im2col(handle);
  361. benchmark_im2col.set_display(false);
  362. benchmark_im2col.set_times(RUN);
  363. for (auto&& arg : args) {
  364. TensorLayout dst_layout;
  365. auto opr = handle->create_operator<ConvBias>();
  366. opr->param() = arg.param;
  367. opr->deduce_layout({arg.src, dtype::Float32()},
  368. {arg.filter, dtype::Float32()},
  369. {arg.bias, dtype::Float32()}, {}, dst_layout);
  370. //! dst.nr_elems * IC * FH * FW * 2
  371. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  372. arg.filter[2] * arg.filter[3] * 2.0 /
  373. (1024 * 1024 * 1024) * 1e3;
  374. benchmark_im2col.set_param(arg.param);
  375. auto used_im2col =
  376. algo_benchmark<ConvBias>(benchmark_im2col,
  377. {arg.src, arg.filter, {}, {}, {}},
  378. im2col_name) /
  379. RUN;
  380. printf("%s %s: im2col: %f ms %f GFlops \n", arg.src.to_string().c_str(),
  381. arg.filter.to_string().c_str(), used_im2col,
  382. computations / used_im2col);
  383. }
  384. }
  385. void BENCHMARK_IM2COL_NCHW44_VS_NCHW(const char* algo_name,
  386. const char* im2col_name, Handle* handle,
  387. size_t kernel, size_t pack_size = 1) {
  388. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  389. using namespace conv_bias;
  390. constexpr size_t RUN = 10;
  391. Benchmarker<ConvBias> benchmark(handle);
  392. benchmark.set_display(false);
  393. benchmark.set_times(RUN);
  394. benchmark.set_dtype(0, dtype::Int8());
  395. benchmark.set_dtype(1, dtype::Int8());
  396. benchmark.set_dtype(2, dtype::Int32());
  397. benchmark.set_dtype(4, dtype::Int32());
  398. Benchmarker<ConvBias> benchmark_im2col(handle);
  399. benchmark_im2col.set_display(false);
  400. benchmark_im2col.set_times(RUN);
  401. benchmark_im2col.set_dtype(0, dtype::Int8());
  402. benchmark_im2col.set_dtype(1, dtype::Int8());
  403. benchmark_im2col.set_dtype(2, dtype::Int32());
  404. benchmark_im2col.set_dtype(4, dtype::Int32());
  405. for (auto&& arg : args) {
  406. TensorLayout dst_layout;
  407. auto opr = handle->create_operator<ConvBias>();
  408. opr->param() = arg.param;
  409. opr->deduce_layout({arg.src, dtype::Float32()},
  410. {arg.filter, dtype::Float32()},
  411. {arg.bias, dtype::Float32()}, {}, dst_layout);
  412. //! dst.nr_elems * IC * FH * FW * 2
  413. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  414. arg.filter[2] * arg.filter[3] * 2.0 /
  415. (1024 * 1024 * 1024) * 1e3;
  416. std::vector<conv_bias::TestArg> nchw44param;
  417. benchmark.set_param(arg.param);
  418. auto used = algo_benchmark<ConvBias>(benchmark,
  419. {arg.src, arg.filter, {}, {}, {}},
  420. algo_name) /
  421. RUN;
  422. arg.param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  423. arg.param.format = param::ConvBias::Format::NCHW44;
  424. benchmark_im2col.set_param(arg.param);
  425. nchw44param.push_back(conv_bias::TestArg{
  426. arg.param,
  427. TensorShape{arg.src.shape[0], arg.src.shape[1] / 4, arg.src[2],
  428. arg.src.shape[3], 4},
  429. TensorShape{arg.filter.shape[0] / 4, arg.filter.shape[1] / 4,
  430. kernel, kernel, 4, 4},
  431. TensorShape{}});
  432. auto used_im2col =
  433. algo_benchmark<ConvBias>(
  434. benchmark_im2col,
  435. {nchw44param[0].src, nchw44param[0].filter, {}, {}, {}},
  436. im2col_name) /
  437. RUN;
  438. printf("nchw44 shape src %s filter %s\n",
  439. nchw44param[0].src.to_string().c_str(),
  440. nchw44param[0].filter.to_string().c_str());
  441. printf("%s %s: normal: %f ms %f Gflops im2col: %f ms %f GFlops "
  442. "speedup: "
  443. "%f\n",
  444. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  445. used, computations / used, used_im2col,
  446. computations / used_im2col, used / used_im2col);
  447. }
  448. }
  449. TEST_F(ARM_COMMON, BENCHMARK_NCHW_VS_NCHW44_INT8x8x32) {
  450. printf("=========================compare "
  451. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16, "
  452. "IM2COLMATMUL:AARCH64_INT8X8X32_MK4_4X4X16 \n");
  453. BENCHMARK_IM2COL_NCHW44_VS_NCHW("IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16",
  454. "IM2COLMATMUL:AARCH64_INT8X8X32_MK4_4X4X16",
  455. handle(), 3, 4);
  456. }
  457. TEST_F(ARM_COMMON, BENCHMARK_GROUP_CONVBIAS_QUANTIZED) {
  458. constexpr size_t RUNS = 50;
  459. param::ConvBias param;
  460. param.sparse = param::ConvBias::Sparse::GROUP;
  461. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  462. Benchmarker<ConvBias> benchmarker_int(handle());
  463. benchmarker_int.set_times(RUNS)
  464. .set_dtype(0, dtype::QuantizedS8(2.5f))
  465. .set_dtype(1, dtype::QuantizedS8(2.5f))
  466. .set_dtype(2, dtype::QuantizedS32(6.25f))
  467. .set_dtype(4, dtype::QuantizedS8(40.25f))
  468. .set_display(false);
  469. Benchmarker<ConvBias> benchmarker_float(handle());
  470. benchmarker_float.set_display(false).set_times(RUNS);
  471. auto run = [&](size_t N, size_t GROUP, size_t IC, size_t OC, size_t H,
  472. size_t W, size_t FS, size_t STRD) {
  473. megdnn_assert(IC % GROUP == 0 && OC % GROUP == 0);
  474. TensorShape src({N, IC, H, W}),
  475. filter({GROUP, OC / GROUP, IC / GROUP, FS, FS}),
  476. bias({1, OC, 1, 1}), dst({N, OC, H / STRD, W / STRD});
  477. param.pad_h = FS / 2;
  478. param.pad_w = FS / 2;
  479. param.stride_h = STRD;
  480. param.stride_w = STRD;
  481. auto int_used = benchmarker_int.set_param(param).exec(
  482. {src, filter, bias, {}, dst}) /
  483. RUNS;
  484. auto float_used = benchmarker_float.set_param(param).exec(
  485. {src, filter, bias, {}, dst}) /
  486. RUNS;
  487. float computations = (IC / GROUP * FS * FS * dst.total_nr_elems() * 2 +
  488. dst.total_nr_elems()) *
  489. 1e-6;
  490. printf("run: %s %s %s->%s \nfloat: %f ms %f Gflops int: %f ms "
  491. "%f Gflops speedup: %f\n",
  492. src.to_string().c_str(), filter.to_string().c_str(),
  493. bias.to_string().c_str(), dst.to_string().c_str(), float_used,
  494. computations / float_used, int_used, computations / int_used,
  495. float_used / int_used);
  496. };
  497. run(1, 1, 28, 28, 28, 28, 3, 1);
  498. run(1, 68, 68, 68, 14, 14, 3, 2);
  499. run(1, 96, 96, 96, 14, 14, 3, 2);
  500. run(1, 100, 100, 100, 7, 7, 3, 1);
  501. }
  502. #endif
  503. #if MEGDNN_WITH_BENCHMARK
  504. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_MATMUL) {
  505. constexpr size_t RUNS = 10;
  506. param::ConvBias param;
  507. param.stride_h = 1;
  508. param.stride_w = 1;
  509. param.nonlineMode = param::ConvBias::NonlineMode::RELU;
  510. Benchmarker<ConvBias> benchmarker(handle()), benchmarker_fused(handle());
  511. benchmarker.set_times(RUNS)
  512. .set_dtype(0, dtype::QuantizedS8(2.5f))
  513. .set_dtype(1, dtype::QuantizedS8(2.5f))
  514. .set_dtype(2, dtype::QuantizedS32(6.25f))
  515. .set_dtype(4, dtype::QuantizedS8(40.25f))
  516. .set_display(false);
  517. benchmarker_fused.set_times(RUNS)
  518. .set_dtype(0, dtype::QuantizedS8(2.5f))
  519. .set_dtype(1, dtype::QuantizedS8(2.5f))
  520. .set_dtype(2, dtype::QuantizedS32(6.25f))
  521. .set_dtype(4, dtype::QuantizedS8(40.25f))
  522. .set_display(false);
  523. benchmarker_fused.set_before_exec_callback(
  524. conv_bias::ConvBiasAlgoChecker<ConvBias>("S8MATMUL"));
  525. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
  526. size_t FS) {
  527. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}),
  528. bias({1, OC, 1, 1}), dst({N, OC, H, W});
  529. param.pad_h = FS / 2;
  530. param.pad_w = FS / 2;
  531. auto default_used = benchmarker.set_param(param).exec(
  532. {src, filter, bias, {}, dst}) /
  533. RUNS;
  534. auto fused_used = benchmarker_fused.set_param(param).exec(
  535. {src, filter, bias, {}, dst}) /
  536. RUNS;
  537. float computations =
  538. IC * (FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  539. printf("run: %s %s %s->%s \ndefault: %f ms %f Gflops fused: %f ms "
  540. "%f Gflops speedup: %f\n",
  541. src.to_string().c_str(), filter.to_string().c_str(),
  542. bias.to_string().c_str(), dst.to_string().c_str(), default_used,
  543. computations / default_used, fused_used,
  544. computations / fused_used, default_used / fused_used);
  545. };
  546. run(1, 128, 128, 32, 32, 3);
  547. for (size_t IC : {36, 48}) {
  548. for (size_t OC : {36, 48, 64}) {
  549. for (size_t size : {56, 128, 256}) {
  550. for (size_t FS : {1, 3, 5}) {
  551. run(1, IC, OC, size, size, FS);
  552. }
  553. }
  554. }
  555. }
  556. }
  557. #endif
  558. #if MEGDNN_WITH_BENCHMARK
  559. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23) {
  560. #if MEGDNN_AARCH64
  561. benchmark_winograd("WINOGRAD:AARCH64_F32:1:2", handle(), 3);
  562. #else
  563. benchmark_winograd("WINOGRAD:ARMV7_F32_:1:2", handle(), 3);
  564. #endif
  565. }
  566. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_4x4) {
  567. #if MEGDNN_AARCH64
  568. benchmark_winograd("WINOGRAD:AARCH64_F32_MK4_4x16:4:2", handle(), 3, 4);
  569. #else
  570. benchmark_winograd("WINOGRAD:ARMV7_F32_MK4_4x8:4:2", handle(), 3, 4);
  571. #endif
  572. }
  573. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63) {
  574. #if MEGDNN_AARCH64
  575. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:6", handle(), 3);
  576. #else
  577. benchmark_winograd("WINOGRAD:ARMV7_F32:1:6", handle(), 3);
  578. #endif
  579. }
  580. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F63_4x4) {
  581. #if MEGDNN_AARCH64
  582. benchmark_winograd("WINOGRAD:AARCH64_F32_MK4_4x16:4:6", handle(), 3, 4);
  583. #else
  584. benchmark_winograd("WINOGRAD:ARMV7_F32_MK4_4x8:4:6", handle(), 3, 4);
  585. #endif
  586. }
  587. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F54) {
  588. #if MEGDNN_AARCH64
  589. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:5", handle(), 4);
  590. #else
  591. benchmark_winograd("WINOGRAD:ARMV7_F32:1:5", handle(), 4);
  592. #endif
  593. }
  594. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F45) {
  595. #if MEGDNN_AARCH64
  596. benchmark_winograd("WINOGRAD:AARCH64_F32K8X12X1:1:4", handle(), 5);
  597. #else
  598. benchmark_winograd("WINOGRAD:ARMV7_F32:1:4", handle(), 5);
  599. #endif
  600. }
  601. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  602. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23) {
  603. #if MEGDNN_AARCH64
  604. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  605. "WINOGRAD:AARCH64_F16_K8X24X1:1:6", handle(), 3, 4);
  606. #else
  607. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:2",
  608. "WINOGRAD:AARCH32_F16_K4X16X1:1:2", handle(), 3);
  609. #endif
  610. }
  611. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F45) {
  612. #if MEGDNN_AARCH64
  613. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32K8X12X1:1:4",
  614. "WINOGRAD:AARCH64_F16_K8X24X1:1:4", handle(), 5);
  615. #else
  616. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:4",
  617. "WINOGRAD:AARCH32_F16_K4X16X1:1:4", handle(), 5);
  618. #endif
  619. }
  620. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F63) {
  621. #if MEGDNN_AARCH64
  622. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32K8X12X1:1:6",
  623. "WINOGRAD:AARCH64_F16_K8X24X1:1:6", handle(), 3);
  624. #else
  625. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32:1:6",
  626. "WINOGRAD:AARCH32_F16_K4X16X1:1:6", handle(), 3);
  627. #endif
  628. }
  629. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F16_F23_8x8) {
  630. #if MEGDNN_AARCH64
  631. benchmark_winograd_fp16("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  632. "WINOGRAD:AARCH64_F16_MK8_8X8:8:2", handle(), 3, 8);
  633. #else
  634. benchmark_winograd_fp16("WINOGRAD:ARMV7_F32_MK4_4x8:4:2",
  635. "WINOGRAD:AARCH32_F16_MK8_4X8:8:2", handle(), 3, 8);
  636. #endif
  637. }
  638. #endif
  639. TEST_F(ARM_COMMON, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
  640. auto benchmark_winograd_quantized = [](const char* algo_name_fp32,
  641. const char* algo_name_quantized,
  642. Handle* handle, size_t kernel) {
  643. auto&& args = get_winograd_benchmark_args(kernel);
  644. using namespace conv_bias;
  645. constexpr size_t RUN = 10;
  646. Benchmarker<ConvBias> benchmark(handle);
  647. benchmark.set_display(false);
  648. benchmark.set_times(RUN);
  649. Benchmarker<ConvBias> benchmark_winograd(handle);
  650. benchmark_winograd.set_display(false).set_times(RUN);
  651. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  652. .set_dtype(1, dtype::QuantizedS8(2.5f))
  653. .set_dtype(2, dtype::QuantizedS32(6.25f))
  654. .set_dtype(4, dtype::QuantizedS8(60.25f));
  655. for (auto&& arg : args) {
  656. TensorLayout dst_layout;
  657. auto opr = handle->create_operator<ConvBias>();
  658. opr->param() = arg.param;
  659. opr->deduce_layout({arg.src, dtype::Float32()},
  660. {arg.filter, dtype::Float32()},
  661. {arg.bias, dtype::Float32()}, {}, dst_layout);
  662. //! dst.nr_elems * IC * FH * FW * 2
  663. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  664. arg.filter[2] * arg.filter[3] * 2.0 /
  665. (1024 * 1024 * 1024) * 1e3;
  666. benchmark.set_param(arg.param);
  667. auto used = algo_benchmark<ConvBias>(
  668. benchmark, {arg.src, arg.filter, {}, {}, {}},
  669. algo_name_fp32) /
  670. RUN;
  671. benchmark_winograd.set_param(arg.param);
  672. auto used_winograd =
  673. algo_benchmark<ConvBias>(benchmark_winograd,
  674. {arg.src, arg.filter, {}, {}, {}},
  675. algo_name_quantized) /
  676. RUN;
  677. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  678. "speedup: "
  679. "%f\n",
  680. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  681. used, computations / used, used_winograd,
  682. computations / used_winograd, used / used_winograd);
  683. }
  684. };
  685. #if MEGDNN_AARCH64
  686. benchmark_winograd_quantized("WINOGRAD:AARCH64_F32_MK4_4x16:4:2",
  687. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2",
  688. handle(), 3);
  689. #else
  690. benchmark_winograd_quantized("WINOGRAD:ARMV7_F32_MK4_4x8:4:2",
  691. "WINOGRAD:ARMV7_INT16X16X32_MK8_4X8:8:2",
  692. handle(), 3);
  693. #endif
  694. }
  695. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1) {
  696. // have to remove preferred restrict in usable func before run the benchmark
  697. using namespace conv_bias;
  698. std::vector<TestArg> args;
  699. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  700. size_t p, NonlineMode nonline_mode) {
  701. if (w + 2 * p < kernel || h + 2 * p < kernel)
  702. return;
  703. param::ConvBias param;
  704. param.stride_h = 1;
  705. param.stride_w = 1;
  706. param.pad_h = p;
  707. param.pad_w = p;
  708. param.nonlineMode = nonline_mode;
  709. //! channel bias
  710. args.emplace_back(param, TensorShape{2, ic, h, w},
  711. TensorShape{oc, ic, kernel, kernel},
  712. TensorShape{1, oc, 1, 1});
  713. };
  714. for (size_t kernel : {2, 3, 5, 7})
  715. for (size_t ic : {1, 8, 16, 32})
  716. for (size_t oc : {1, 8, 16, 32})
  717. for (size_t p : {1})
  718. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  719. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  720. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  721. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  722. }
  723. constexpr size_t RUN = 50;
  724. Benchmarker<ConvBias> benchmark0(handle());
  725. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  726. .set_dtype(1, dtype::QuantizedS8(2.5f))
  727. .set_dtype(2, dtype::QuantizedS32(6.25f))
  728. .set_dtype(4, dtype::QuantizedS8(60.25f));
  729. benchmark0.set_display(false);
  730. benchmark0.set_times(RUN);
  731. benchmark0.set_before_exec_callback(
  732. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD1"));
  733. Benchmarker<ConvBias> benchmark1(handle());
  734. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  735. .set_dtype(1, dtype::QuantizedS8(2.5f))
  736. .set_dtype(2, dtype::QuantizedS32(6.25f))
  737. .set_dtype(4, dtype::QuantizedS8(60.25f));
  738. benchmark1.set_display(false);
  739. benchmark1.set_times(RUN);
  740. for (auto&& arg : args) {
  741. TensorLayout dst_layout;
  742. auto opr = handle()->create_operator<ConvBias>();
  743. opr->param() = arg.param;
  744. opr->deduce_layout({arg.src, dtype::Int8()},
  745. {arg.filter, dtype::Int8()},
  746. {arg.bias, dtype::Int32()}, {}, dst_layout);
  747. //! dst.nr_elems * IC * FH * FW * 2
  748. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  749. arg.filter[2] * arg.filter[3] * 2.0 /
  750. (1024 * 1024 * 1024) * 1e3;
  751. auto used0 = benchmark0.set_param(arg.param).exec(
  752. {arg.src, arg.filter, arg.bias, {}, {}}) /
  753. RUN;
  754. auto used1 = benchmark1.set_param(arg.param).exec(
  755. {arg.src, arg.filter, arg.bias, {}, {}}) /
  756. RUN;
  757. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  758. "speedup: %f\n",
  759. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  760. used0, computations / used0, used1, computations / used1,
  761. used1 / used0);
  762. }
  763. }
  764. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2) {
  765. // have to remove preferred restrict in usable func before run the benchmark
  766. using namespace conv_bias;
  767. std::vector<TestArg> args;
  768. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  769. size_t p, NonlineMode nonline_mode) {
  770. if (w + 2 * p < kernel || h + 2 * p < kernel)
  771. return;
  772. param::ConvBias param;
  773. param.stride_h = 2;
  774. param.stride_w = 2;
  775. param.pad_h = p;
  776. param.pad_w = p;
  777. param.nonlineMode = nonline_mode;
  778. //! channel bias
  779. args.emplace_back(param, TensorShape{2, ic, h, w},
  780. TensorShape{oc, ic, kernel, kernel},
  781. TensorShape{1, oc, 1, 1});
  782. };
  783. for (size_t kernel : {2, 3, 5, 7})
  784. for (size_t ic : {1, 8, 16, 32})
  785. for (size_t oc : {1, 8, 16, 32})
  786. for (size_t p : {1})
  787. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  788. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  789. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  790. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  791. }
  792. constexpr size_t RUN = 50;
  793. Benchmarker<ConvBias> benchmark0(handle());
  794. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  795. .set_dtype(1, dtype::QuantizedS8(2.5f))
  796. .set_dtype(2, dtype::QuantizedS32(6.25f))
  797. .set_dtype(4, dtype::QuantizedS8(60.25f));
  798. benchmark0.set_display(false);
  799. benchmark0.set_times(RUN);
  800. benchmark0.set_before_exec_callback(
  801. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("S8STRD2"));
  802. Benchmarker<ConvBias> benchmark1(handle());
  803. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  804. .set_dtype(1, dtype::QuantizedS8(2.5f))
  805. .set_dtype(2, dtype::QuantizedS32(6.25f))
  806. .set_dtype(4, dtype::QuantizedS8(60.25f));
  807. benchmark1.set_display(false);
  808. benchmark1.set_times(RUN);
  809. for (auto&& arg : args) {
  810. TensorLayout dst_layout;
  811. auto opr = handle()->create_operator<ConvBias>();
  812. opr->param() = arg.param;
  813. opr->deduce_layout({arg.src, dtype::Int8()},
  814. {arg.filter, dtype::Int8()},
  815. {arg.bias, dtype::Int32()}, {}, dst_layout);
  816. //! dst.nr_elems * IC * FH * FW * 2
  817. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  818. arg.filter[2] * arg.filter[3] * 2.0 /
  819. (1024 * 1024 * 1024) * 1e3;
  820. auto used0 = benchmark0.set_param(arg.param).exec(
  821. {arg.src, arg.filter, arg.bias, {}, {}}) /
  822. RUN;
  823. auto used1 = benchmark1.set_param(arg.param).exec(
  824. {arg.src, arg.filter, arg.bias, {}, {}}) /
  825. RUN;
  826. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  827. "speedup: %f\n",
  828. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  829. used0, computations / used0, used1, computations / used1,
  830. used1 / used0);
  831. }
  832. }
  833. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1) {
  834. // have to remove preferred restrict in usable func before run the benchmark
  835. using namespace conv_bias;
  836. std::vector<TestArg> args;
  837. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  838. size_t p, NonlineMode nonline_mode) {
  839. if (w + 2 * p < kernel || h + 2 * p < kernel)
  840. return;
  841. param::ConvBias param;
  842. param.stride_h = 1;
  843. param.stride_w = 1;
  844. param.pad_h = p;
  845. param.pad_w = p;
  846. param.nonlineMode = nonline_mode;
  847. //! channel bias
  848. args.emplace_back(param, TensorShape{2, ic, h, w},
  849. TensorShape{oc, ic, kernel, kernel},
  850. TensorShape{1, oc, 1, 1});
  851. };
  852. for (size_t kernel : {2, 3, 5, 7})
  853. for (size_t ic : {1, 8, 16, 32})
  854. for (size_t oc : {1, 8, 16, 32})
  855. for (size_t p : {1})
  856. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  857. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  858. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  859. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  860. }
  861. constexpr size_t RUN = 50;
  862. Benchmarker<ConvBias> benchmark0(handle());
  863. benchmark0
  864. .set_dtype(0,
  865. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  866. .set_dtype(1,
  867. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  868. .set_dtype(2, dtype::QuantizedS32(0.04f))
  869. .set_dtype(4,
  870. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  871. benchmark0.set_display(false);
  872. benchmark0.set_times(RUN);
  873. benchmark0.set_before_exec_callback(
  874. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD1"));
  875. Benchmarker<ConvBias> benchmark1(handle());
  876. benchmark1
  877. .set_dtype(0,
  878. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  879. .set_dtype(1,
  880. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  881. .set_dtype(2, dtype::QuantizedS32(0.04f))
  882. .set_dtype(4,
  883. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  884. benchmark1.set_display(false);
  885. benchmark1.set_times(RUN);
  886. for (auto&& arg : args) {
  887. TensorLayout dst_layout;
  888. auto opr = handle()->create_operator<ConvBias>();
  889. opr->param() = arg.param;
  890. opr->deduce_layout({arg.src, dtype::Int8()},
  891. {arg.filter, dtype::Int8()},
  892. {arg.bias, dtype::Int32()}, {}, dst_layout);
  893. //! dst.nr_elems * IC * FH * FW * 2
  894. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  895. arg.filter[2] * arg.filter[3] * 2.0 /
  896. (1024 * 1024 * 1024) * 1e3;
  897. auto used0 = benchmark0.set_param(arg.param).exec(
  898. {arg.src, arg.filter, arg.bias, {}, {}}) /
  899. RUN;
  900. auto used1 = benchmark1.set_param(arg.param).exec(
  901. {arg.src, arg.filter, arg.bias, {}, {}}) /
  902. RUN;
  903. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  904. "speedup: %f\n",
  905. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  906. used0, computations / used0, used1, computations / used1,
  907. used1 / used0);
  908. }
  909. }
  910. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2) {
  911. // have to remove preferred restrict in usable func before run the benchmark
  912. using namespace conv_bias;
  913. std::vector<TestArg> args;
  914. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  915. size_t p, NonlineMode nonline_mode) {
  916. if (w + 2 * p < kernel || h + 2 * p < kernel)
  917. return;
  918. param::ConvBias param;
  919. param.stride_h = 2;
  920. param.stride_w = 2;
  921. param.pad_h = p;
  922. param.pad_w = p;
  923. param.nonlineMode = nonline_mode;
  924. //! channel bias
  925. args.emplace_back(param, TensorShape{2, ic, h, w},
  926. TensorShape{oc, ic, kernel, kernel},
  927. TensorShape{1, oc, 1, 1});
  928. };
  929. for (size_t kernel : {2, 3, 5, 7})
  930. for (size_t ic : {1, 8, 16, 32})
  931. for (size_t oc : {1, 8, 16, 32})
  932. for (size_t p : {1})
  933. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  934. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  935. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  936. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  937. }
  938. constexpr size_t RUN = 50;
  939. Benchmarker<ConvBias> benchmark0(handle());
  940. benchmark0
  941. .set_dtype(0,
  942. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  943. .set_dtype(1,
  944. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  945. .set_dtype(2, dtype::QuantizedS32(0.04f))
  946. .set_dtype(4,
  947. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  948. benchmark0.set_display(false);
  949. benchmark0.set_times(RUN);
  950. benchmark0.set_before_exec_callback(
  951. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("QU8STRD2"));
  952. Benchmarker<ConvBias> benchmark1(handle());
  953. benchmark1
  954. .set_dtype(0,
  955. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  956. .set_dtype(1,
  957. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  958. .set_dtype(2, dtype::QuantizedS32(0.04f))
  959. .set_dtype(4,
  960. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  961. benchmark1.set_display(false);
  962. benchmark1.set_times(RUN);
  963. for (auto&& arg : args) {
  964. TensorLayout dst_layout;
  965. auto opr = handle()->create_operator<ConvBias>();
  966. opr->param() = arg.param;
  967. opr->deduce_layout({arg.src, dtype::Int8()},
  968. {arg.filter, dtype::Int8()},
  969. {arg.bias, dtype::Int32()}, {}, dst_layout);
  970. //! dst.nr_elems * IC * FH * FW * 2
  971. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  972. arg.filter[2] * arg.filter[3] * 2.0 /
  973. (1024 * 1024 * 1024) * 1e3;
  974. auto used0 = benchmark0.set_param(arg.param).exec(
  975. {arg.src, arg.filter, arg.bias, {}, {}}) /
  976. RUN;
  977. auto used1 = benchmark1.set_param(arg.param).exec(
  978. {arg.src, arg.filter, arg.bias, {}, {}}) /
  979. RUN;
  980. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  981. "speedup: %f\n",
  982. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  983. used0, computations / used0, used1, computations / used1,
  984. used1 / used0);
  985. }
  986. }
  987. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QINT8_STRIDE1_NCHW44) {
  988. // have to remove preferred restrict in usable func before run the benchmark
  989. using namespace conv_bias;
  990. param::ConvBias param;
  991. param.stride_h = 1;
  992. param.stride_w = 1;
  993. param.pad_h = 1;
  994. param.pad_w = 1;
  995. param.nonlineMode = NonlineMode::RELU;
  996. param.sparse = param::ConvBias::Sparse::GROUP;
  997. constexpr size_t RUN = 50;
  998. Benchmarker<ConvBias> benchmark0(handle());
  999. benchmark0.set_dtype(0, dtype::QuantizedS8(0.2f))
  1000. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1001. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1002. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1003. benchmark0.set_display(false);
  1004. benchmark0.set_param(param);
  1005. benchmark0.set_times(RUN);
  1006. benchmark0.set_before_exec_callback(
  1007. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1008. "S8STRD1_LARGE_GROUP"));
  1009. auto opr = handle()->create_operator<ConvBias>();
  1010. opr->param() = param;
  1011. param.format = param::ConvBias::Format::NCHW44;
  1012. Benchmarker<ConvBias> benchmark1(handle());
  1013. benchmark1.set_dtype(0, dtype::QuantizedS8(0.2f))
  1014. .set_dtype(1, dtype::QuantizedS8(0.2f))
  1015. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1016. .set_dtype(4, dtype::QuantizedS8(1.4f));
  1017. benchmark1.set_display(false);
  1018. benchmark1.set_param(param);
  1019. benchmark1.set_times(RUN);
  1020. benchmark1.set_before_exec_callback(
  1021. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
  1022. "S8_CHAN_WISE_STRD1_NCHW44"));
  1023. auto run = [&](size_t group, size_t w, size_t h, size_t kernel) {
  1024. TensorLayout dst_layout;
  1025. opr->deduce_layout({{1, group * 4, h, w}, dtype::Int8()},
  1026. {{group * 4, 1, 1, kernel, kernel}, dtype::Int8()},
  1027. {{1, group * 4, 1, 1}, dtype::Int32()}, {},
  1028. dst_layout);
  1029. //! dst.nr_elems * IC * FH * FW * 2
  1030. float computations = dst_layout.total_nr_elems() * kernel * kernel *
  1031. 2.0 / (1024 * 1024 * 1024) * 1e3;
  1032. auto used0 = benchmark0.exec({{1, group * 4, h, w},
  1033. {group * 4, 1, 1, kernel, kernel},
  1034. {1, group * 4, 1, 1},
  1035. {},
  1036. {}}) /
  1037. RUN;
  1038. auto used1 = benchmark1.exec({{1, group, h, w, 4},
  1039. {group, 1, 1, kernel, kernel, 4},
  1040. {1, group, 1, 1, 4},
  1041. {},
  1042. {}}) /
  1043. RUN;
  1044. printf("group/h/w/kernel:%zu,%zu,%zu,%zu: nchw: %f ms %f Gflops "
  1045. "nchw44: "
  1046. "%f ms %f GFlops "
  1047. "speedup: %f\n",
  1048. group, h, w, kernel, used0, computations / used0, used1,
  1049. computations / used1, used0 / used1);
  1050. };
  1051. for (size_t group : {8, 16, 32, 64, 128}) {
  1052. for (size_t kerenl : {2, 3, 5}) {
  1053. run(group, 112, 112, kerenl);
  1054. run(group, 56, 56, kerenl);
  1055. run(group, 48, 48, kerenl);
  1056. run(group, 28, 28, kerenl);
  1057. run(group, 14, 14, kerenl);
  1058. }
  1059. }
  1060. }
  1061. #endif
  1062. #if __ARM_FEATURE_DOTPROD
  1063. #if MEGDNN_WITH_BENCHMARK
  1064. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE1_WITHDOTPROD) {
  1065. // have to remove preferred restrict in usable func before run the benchmark
  1066. using namespace conv_bias;
  1067. std::vector<TestArg> args;
  1068. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1069. size_t p, NonlineMode nonline_mode) {
  1070. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1071. return;
  1072. param::ConvBias param;
  1073. param.stride_h = 1;
  1074. param.stride_w = 1;
  1075. param.pad_h = p;
  1076. param.pad_w = p;
  1077. param.nonlineMode = nonline_mode;
  1078. //! channel bias
  1079. args.emplace_back(param, TensorShape{2, ic, h, w},
  1080. TensorShape{oc, ic, kernel, kernel},
  1081. TensorShape{1, oc, 1, 1});
  1082. };
  1083. for (size_t kernel : {2, 3, 5, 7})
  1084. for (size_t ic : {1, 8, 16, 32})
  1085. for (size_t oc : {1, 8, 16, 32})
  1086. for (size_t p : {1})
  1087. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1088. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1089. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1090. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1091. }
  1092. constexpr size_t RUN = 50;
  1093. Benchmarker<ConvBias> benchmark0(handle());
  1094. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1095. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1096. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1097. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1098. benchmark0.set_display(false);
  1099. benchmark0.set_times(RUN);
  1100. benchmark0.set_before_exec_callback(
  1101. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD1"));
  1102. Benchmarker<ConvBias> benchmark1(handle());
  1103. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1104. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1105. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1106. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1107. benchmark1.set_display(false);
  1108. benchmark1.set_times(RUN);
  1109. for (auto&& arg : args) {
  1110. TensorLayout dst_layout;
  1111. auto opr = handle()->create_operator<ConvBias>();
  1112. opr->param() = arg.param;
  1113. opr->deduce_layout({arg.src, dtype::Int8()},
  1114. {arg.filter, dtype::Int8()},
  1115. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1116. //! dst.nr_elems * IC * FH * FW * 2
  1117. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1118. arg.filter[2] * arg.filter[3] * 2.0 /
  1119. (1024 * 1024 * 1024) * 1e3;
  1120. auto used0 = benchmark0.set_param(arg.param).exec(
  1121. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1122. RUN;
  1123. auto used1 = benchmark1.set_param(arg.param).exec(
  1124. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1125. RUN;
  1126. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1127. "speedup: %f\n",
  1128. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1129. used0, computations / used0, used1, computations / used1,
  1130. used1 / used0);
  1131. }
  1132. }
  1133. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_INT8_STRIDE2_WITHDOTPROD) {
  1134. // have to remove preferred restrict in usable func before run the benchmark
  1135. using namespace conv_bias;
  1136. std::vector<TestArg> args;
  1137. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1138. size_t p, NonlineMode nonline_mode) {
  1139. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1140. return;
  1141. param::ConvBias param;
  1142. param.stride_h = 2;
  1143. param.stride_w = 2;
  1144. param.pad_h = p;
  1145. param.pad_w = p;
  1146. param.nonlineMode = nonline_mode;
  1147. //! channel bias
  1148. args.emplace_back(param, TensorShape{2, ic, h, w},
  1149. TensorShape{oc, ic, kernel, kernel},
  1150. TensorShape{1, oc, 1, 1});
  1151. };
  1152. for (size_t kernel : {2, 3, 5, 7})
  1153. for (size_t ic : {1, 8, 16, 32})
  1154. for (size_t oc : {1, 8, 16, 32})
  1155. for (size_t p : {1})
  1156. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1157. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1158. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1159. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1160. }
  1161. constexpr size_t RUN = 50;
  1162. Benchmarker<ConvBias> benchmark0(handle());
  1163. benchmark0.set_dtype(0, dtype::QuantizedS8(2.5f))
  1164. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1165. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1166. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1167. benchmark0.set_display(false);
  1168. benchmark0.set_times(RUN);
  1169. benchmark0.set_before_exec_callback(
  1170. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTS8STRD2"));
  1171. Benchmarker<ConvBias> benchmark1(handle());
  1172. benchmark1.set_dtype(0, dtype::QuantizedS8(2.5f))
  1173. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1174. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1175. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1176. benchmark1.set_display(false);
  1177. benchmark1.set_times(RUN);
  1178. for (auto&& arg : args) {
  1179. TensorLayout dst_layout;
  1180. auto opr = handle()->create_operator<ConvBias>();
  1181. opr->param() = arg.param;
  1182. opr->deduce_layout({arg.src, dtype::Int8()},
  1183. {arg.filter, dtype::Int8()},
  1184. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1185. //! dst.nr_elems * IC * FH * FW * 2
  1186. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1187. arg.filter[2] * arg.filter[3] * 2.0 /
  1188. (1024 * 1024 * 1024) * 1e3;
  1189. auto used0 = benchmark0.set_param(arg.param).exec(
  1190. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1191. RUN;
  1192. auto used1 = benchmark1.set_param(arg.param).exec(
  1193. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1194. RUN;
  1195. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1196. "speedup: %f\n",
  1197. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1198. used0, computations / used0, used1, computations / used1,
  1199. used1 / used0);
  1200. }
  1201. }
  1202. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE1_WITHDOTPROD) {
  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,
  1207. size_t p, NonlineMode nonline_mode) {
  1208. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1209. return;
  1210. param::ConvBias param;
  1211. param.stride_h = 1;
  1212. param.stride_w = 1;
  1213. param.pad_h = p;
  1214. param.pad_w = p;
  1215. param.nonlineMode = nonline_mode;
  1216. //! channel bias
  1217. args.emplace_back(param, TensorShape{2, ic, h, w},
  1218. TensorShape{oc, ic, kernel, kernel},
  1219. TensorShape{1, oc, 1, 1});
  1220. };
  1221. // clang-format off
  1222. for (size_t kernel : {2, 3, 5, 7})
  1223. for (size_t ic : {1, 8, 16, 32})
  1224. for (size_t oc : {1, 8, 16, 32})
  1225. for (size_t p : {1})
  1226. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1227. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1228. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1229. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1230. }
  1231. // clang-format on
  1232. constexpr size_t RUN = 50;
  1233. Benchmarker<ConvBias> benchmark0(handle());
  1234. benchmark0
  1235. .set_dtype(0,
  1236. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1237. .set_dtype(1,
  1238. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1239. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1240. .set_dtype(4,
  1241. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1242. benchmark0.set_display(false);
  1243. benchmark0.set_times(RUN);
  1244. benchmark0.set_before_exec_callback(
  1245. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD1"));
  1246. Benchmarker<ConvBias> benchmark1(handle());
  1247. benchmark1
  1248. .set_dtype(0,
  1249. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1250. .set_dtype(1,
  1251. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1252. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1253. .set_dtype(4,
  1254. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1255. benchmark1.set_display(false);
  1256. benchmark1.set_times(RUN);
  1257. for (auto&& arg : args) {
  1258. TensorLayout dst_layout;
  1259. auto opr = handle()->create_operator<ConvBias>();
  1260. opr->param() = arg.param;
  1261. opr->deduce_layout({arg.src, dtype::Int8()},
  1262. {arg.filter, dtype::Int8()},
  1263. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1264. //! dst.nr_elems * IC * FH * FW * 2
  1265. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1266. arg.filter[2] * arg.filter[3] * 2.0 /
  1267. (1024 * 1024 * 1024) * 1e3;
  1268. auto used0 = benchmark0.set_param(arg.param).exec(
  1269. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1270. RUN;
  1271. auto used1 = benchmark1.set_param(arg.param).exec(
  1272. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1273. RUN;
  1274. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1275. "speedup: %f\n",
  1276. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1277. used0, computations / used0, used1, computations / used1,
  1278. used1 / used0);
  1279. }
  1280. }
  1281. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_QUINT8_STRIDE2_WITHDOTPROD) {
  1282. // have to remove preferred restrict in usable func before run the benchmark
  1283. using namespace conv_bias;
  1284. std::vector<TestArg> args;
  1285. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1286. size_t p, NonlineMode nonline_mode) {
  1287. if (w + 2 * p < kernel || h + 2 * p < kernel)
  1288. return;
  1289. param::ConvBias param;
  1290. param.stride_h = 2;
  1291. param.stride_w = 2;
  1292. param.pad_h = p;
  1293. param.pad_w = p;
  1294. param.nonlineMode = nonline_mode;
  1295. //! channel bias
  1296. args.emplace_back(param, TensorShape{2, ic, h, w},
  1297. TensorShape{oc, ic, kernel, kernel},
  1298. TensorShape{1, oc, 1, 1});
  1299. };
  1300. // clang-format off
  1301. for (size_t kernel : {2, 3, 5, 7})
  1302. for (size_t ic : {1, 8, 16, 32})
  1303. for (size_t oc : {1, 8, 16, 32})
  1304. for (size_t p : {1})
  1305. for (NonlineMode nonline_mode : {NonlineMode::RELU}) {
  1306. run(oc, ic, 56, 56, kernel, p, nonline_mode);
  1307. run(oc, ic, 128, 128, kernel, p, nonline_mode);
  1308. run(oc, ic, 256, 256, kernel, p, nonline_mode);
  1309. }
  1310. // clang-format on
  1311. constexpr size_t RUN = 50;
  1312. Benchmarker<ConvBias> benchmark0(handle());
  1313. benchmark0
  1314. .set_dtype(0,
  1315. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1316. .set_dtype(1,
  1317. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1318. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1319. .set_dtype(4,
  1320. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1321. benchmark0.set_display(false);
  1322. benchmark0.set_times(RUN);
  1323. benchmark0.set_before_exec_callback(
  1324. conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("ARMDOTU8STRD2"));
  1325. Benchmarker<ConvBias> benchmark1(handle());
  1326. benchmark1
  1327. .set_dtype(0,
  1328. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(100)))
  1329. .set_dtype(1,
  1330. dtype::Quantized8Asymm(0.2f, static_cast<uint8_t>(120)))
  1331. .set_dtype(2, dtype::QuantizedS32(0.04f))
  1332. .set_dtype(4,
  1333. dtype::Quantized8Asymm(1.4f, static_cast<uint8_t>(110)));
  1334. benchmark1.set_display(false);
  1335. benchmark1.set_times(RUN);
  1336. for (auto&& arg : args) {
  1337. TensorLayout dst_layout;
  1338. auto opr = handle()->create_operator<ConvBias>();
  1339. opr->param() = arg.param;
  1340. opr->deduce_layout({arg.src, dtype::Int8()},
  1341. {arg.filter, dtype::Int8()},
  1342. {arg.bias, dtype::Int32()}, {}, dst_layout);
  1343. //! dst.nr_elems * IC * FH * FW * 2
  1344. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1345. arg.filter[2] * arg.filter[3] * 2.0 /
  1346. (1024 * 1024 * 1024) * 1e3;
  1347. auto used0 = benchmark0.set_param(arg.param).exec(
  1348. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1349. RUN;
  1350. auto used1 = benchmark1.set_param(arg.param).exec(
  1351. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1352. RUN;
  1353. printf("%s %s: conv_bias: %f ms %f Gflops conv_elem: %f ms %f GFlops "
  1354. "speedup: %f\n",
  1355. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1356. used0, computations / used0, used1, computations / used1,
  1357. used1 / used0);
  1358. }
  1359. }
  1360. #endif
  1361. #endif
  1362. /*====================== BENCHMARK CONV1X1 ===========================*/
  1363. #if MEGDNN_WITH_BENCHMARK
  1364. namespace {
  1365. std::vector<conv_bias::TestArg> get_conv_bias_1x1_benchmark_args(
  1366. size_t pack_size = 1) {
  1367. using namespace conv_bias;
  1368. std::vector<TestArg> args;
  1369. param::ConvBias param;
  1370. param.stride_h = 1;
  1371. param.stride_w = 1;
  1372. param.pad_h = 0;
  1373. param.pad_w = 0;
  1374. param.nonlineMode = param::ConvBias::NonlineMode::IDENTITY;
  1375. auto bench_case = [&](size_t OC, size_t IC, size_t H, size_t W) {
  1376. if (pack_size == 1)
  1377. args.emplace_back(param, TensorShape{1, IC, H, W},
  1378. TensorShape{OC, IC, 1, 1}, TensorShape{});
  1379. else {
  1380. if (pack_size == 4)
  1381. param.format = param::ConvBias::Format::NCHW44;
  1382. args.emplace_back(param,
  1383. TensorShape{1, IC / pack_size, H, W, pack_size},
  1384. TensorShape{OC / pack_size, IC / pack_size, 1, 1,
  1385. pack_size, pack_size},
  1386. TensorShape{});
  1387. }
  1388. };
  1389. //! MobileNetV1
  1390. bench_case(64, 32, 112, 112);
  1391. bench_case(128, 64, 56, 56);
  1392. bench_case(128, 128, 56, 56);
  1393. bench_case(256, 128, 28, 28);
  1394. bench_case(256, 256, 28, 28);
  1395. bench_case(512, 256, 14, 14);
  1396. bench_case(512, 512, 14, 14);
  1397. bench_case(1024, 512, 7, 7);
  1398. bench_case(1024, 1024, 7, 7);
  1399. //! MobileNetV2
  1400. bench_case(16, 32, 112, 112);
  1401. bench_case(96, 16, 112, 112);
  1402. bench_case(144, 24, 56, 56);
  1403. bench_case(192, 32, 28, 28);
  1404. bench_case(384, 64, 28, 28);
  1405. bench_case(576, 96, 14, 14);
  1406. bench_case(960, 160, 7, 7);
  1407. bench_case(320, 960, 7, 7);
  1408. bench_case(1280, 320, 7, 7);
  1409. //! MobileNetV3-Large
  1410. bench_case(64, 16, 112, 112);
  1411. bench_case(72, 24, 56, 56);
  1412. bench_case(120, 40, 28, 28);
  1413. bench_case(240, 40, 28, 28);
  1414. bench_case(200, 80, 14, 14);
  1415. bench_case(184, 80, 14, 14);
  1416. bench_case(480, 80, 14, 14);
  1417. bench_case(672, 112, 14, 14);
  1418. //! MobileNetV3-Small
  1419. bench_case(72, 16, 56, 56);
  1420. bench_case(88, 24, 28, 28);
  1421. bench_case(96, 24, 28, 28);
  1422. bench_case(240, 40, 14, 14);
  1423. bench_case(120, 40, 14, 14);
  1424. bench_case(144, 48, 14, 14);
  1425. bench_case(288, 48, 14, 14);
  1426. bench_case(576, 96, 7, 7);
  1427. //! resnet50
  1428. bench_case(256, 64, 56, 56);
  1429. bench_case(512, 128, 28, 28);
  1430. bench_case(1024, 256, 14, 14);
  1431. bench_case(2048, 512, 7, 7);
  1432. return args;
  1433. }
  1434. void benchmark_conv1x1(const char* matmul_algo_name, Handle* handle,
  1435. DType stype, DType matmul_dtype, DType bias_type,
  1436. DType conv_dtype) {
  1437. using namespace conv_bias;
  1438. std::vector<TestArg> conv_bias_1x1_args =
  1439. get_conv_bias_1x1_benchmark_args();
  1440. constexpr size_t RUNS = 50;
  1441. param::MatrixMul param;
  1442. param.transposeA = false;
  1443. param.transposeB = false;
  1444. Benchmarker<MatrixMul> benchmark_matmul(handle);
  1445. benchmark_matmul.set_before_exec_callback(
  1446. AlgoChecker<MatrixMul>(matmul_algo_name));
  1447. benchmark_matmul.set_times(RUNS)
  1448. .set_dtype(0, stype)
  1449. .set_dtype(1, stype)
  1450. .set_dtype(2, matmul_dtype)
  1451. .set_param(param)
  1452. .set_display(false);
  1453. std::string conv1x1_algo_name = ssprintf("CONV1x1:%s:24", matmul_algo_name);
  1454. Benchmarker<ConvBias> benchmark_conv1x1(handle);
  1455. benchmark_conv1x1.set_before_exec_callback(
  1456. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1457. conv1x1_algo_name.c_str()));
  1458. benchmark_conv1x1.set_times(RUNS)
  1459. .set_dtype(0, stype)
  1460. .set_dtype(1, stype)
  1461. .set_dtype(2, bias_type)
  1462. .set_dtype(4, conv_dtype)
  1463. .set_display(false);
  1464. for (auto&& arg : conv_bias_1x1_args) {
  1465. size_t IC = arg.src[1];
  1466. size_t OH = arg.src[2];
  1467. size_t OW = arg.src[3];
  1468. size_t OC = arg.filter[0];
  1469. size_t M = OC;
  1470. size_t K = IC;
  1471. size_t N = OH * OW;
  1472. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  1473. TensorShape A, B;
  1474. A = TensorShape{M, K};
  1475. B = TensorShape{K, N};
  1476. auto conv1x1_used = benchmark_conv1x1.set_param(arg.param).exec(
  1477. {arg.src, arg.filter, arg.bias, {}, {}}) /
  1478. RUNS;
  1479. auto matmul_used = benchmark_matmul.exec({A, B, {}}) / RUNS;
  1480. printf("\n%s: ", matmul_algo_name);
  1481. printf("%s %s:\n matmul: %f ms %f Gflops\nconv1x1: %f ms %f GFlops "
  1482. "speedup: "
  1483. "%f\n",
  1484. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1485. matmul_used, computations / matmul_used, conv1x1_used,
  1486. computations / conv1x1_used, matmul_used / conv1x1_used);
  1487. }
  1488. }
  1489. } // namespace
  1490. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F32) {
  1491. #if MEGDNN_AARCH64
  1492. benchmark_conv1x1("AARCH64_F32K8X12X1", handle(), dtype::Float32{},
  1493. dtype::Float32{}, dtype::Float32{}, dtype::Float32{});
  1494. #else
  1495. benchmark_conv1x1("ARMV7_F32", handle(), dtype::Float32{}, dtype::Float32{},
  1496. dtype::Float32{}, dtype::Float32{});
  1497. #endif
  1498. }
  1499. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  1500. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_F16) {
  1501. #if MEGDNN_AARCH64
  1502. benchmark_conv1x1("AARCH64_F16_K8X24X1", handle(), dtype::Float16{},
  1503. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1504. #else
  1505. benchmark_conv1x1("AARCH32_F16_K4X16X1", handle(), dtype::Float16{},
  1506. dtype::Float16{}, dtype::Float16{}, dtype::Float16{});
  1507. #endif
  1508. }
  1509. #endif
  1510. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDSYM) {
  1511. dtype::QuantizedS8 stype(2.5f);
  1512. dtype::QuantizedS32 dtype(6.25f);
  1513. #if MEGDNN_AARCH64
  1514. #if __ARM_FEATURE_DOTPROD
  1515. benchmark_conv1x1("AARCH64_INT8X8X32_K8X12X4_DOTPROD", handle(), stype,
  1516. dtype, dtype, dtype);
  1517. #else
  1518. benchmark_conv1x1("AARCH64_INT8X8X32_K8X8X8", handle(), stype, dtype, dtype,
  1519. dtype);
  1520. benchmark_conv1x1("AARCH64_INT8X8X32_K4X4X16", handle(), stype, dtype,
  1521. dtype, dtype);
  1522. #endif
  1523. #elif MEGDNN_ARMV7
  1524. benchmark_conv1x1("ARMV7_INT8X8X32_K4X8X8", handle(), stype, dtype, dtype,
  1525. dtype);
  1526. #endif
  1527. }
  1528. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_QUANTIZEDASYM) {
  1529. dtype::Quantized8Asymm stype(1.2f, (uint8_t)125);
  1530. dtype::QuantizedS32 dtype(1.2 * 1.2);
  1531. #if MEGDNN_AARCH64
  1532. #if __ARM_FEATURE_DOTPROD
  1533. benchmark_conv1x1("AARCH64_QUINT8_K8X8X4_DOTPROD", handle(), stype, dtype,
  1534. dtype, dtype);
  1535. #else
  1536. benchmark_conv1x1("AARCH64_QUINT8_K8X8X8", handle(), stype, dtype, dtype,
  1537. dtype);
  1538. #endif
  1539. #elif MEGDNN_ARMV7
  1540. benchmark_conv1x1("ARMV7_QUINT8_K4X8X8", handle(), stype, dtype, dtype,
  1541. dtype);
  1542. #endif
  1543. }
  1544. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_CONV1X1_S1_INT8x8x16) {
  1545. #if MEGDNN_AARCH64
  1546. benchmark_conv1x1("AARCH64_INT8X8X16_K8X8X8", handle(), dtype::Int8{},
  1547. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1548. benchmark_conv1x1("AARCH64_INT8X8X16_K4X4X16", handle(), dtype::Int8{},
  1549. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1550. #elif MEGDNN_ARMV7
  1551. benchmark_conv1x1("ARMV7_INT8X8X16_K4X8X8", handle(), dtype::Int8{},
  1552. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1553. benchmark_conv1x1("ARMV7_INT8X8X16_K4X2X16", handle(), dtype::Int8{},
  1554. dtype::Int16{}, dtype::Int16{}, dtype::Int16{});
  1555. #endif
  1556. }
  1557. #ifndef __ARM_FEATURE_DOTPROD
  1558. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_1X1_S1_NCHW_VS_NCHW44_INT8x8x32) {
  1559. std::vector<TestArg> conv_bias_1x1_args_nchw44 =
  1560. get_conv_bias_1x1_benchmark_args(4);
  1561. std::vector<TestArg> conv_bias_1x1_args_nchw =
  1562. get_conv_bias_1x1_benchmark_args(1);
  1563. constexpr size_t RUNS = 50;
  1564. Benchmarker<ConvBias> benchmark_conv1x1_nchw44(handle());
  1565. benchmark_conv1x1_nchw44.set_before_exec_callback(
  1566. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1567. "CONV1x1:AARCH64_INT8X8X32_MK4_4X4X16:24"));
  1568. benchmark_conv1x1_nchw44.set_times(RUNS)
  1569. .set_dtype(0, dtype::Int8())
  1570. .set_dtype(1, dtype::Int8())
  1571. .set_dtype(2, dtype::Int32())
  1572. .set_dtype(4, dtype::Int32())
  1573. .set_display(false);
  1574. Benchmarker<ConvBias> benchmark_conv1x1_nchw(handle());
  1575. benchmark_conv1x1_nchw.set_before_exec_callback(
  1576. conv_bias::ConvBiasAlgoChecker<ConvBias>(
  1577. "CONV1x1:AARCH64_INT8X8X32_K4X4X16:24"));
  1578. benchmark_conv1x1_nchw.set_times(RUNS)
  1579. .set_dtype(0, dtype::Int8())
  1580. .set_dtype(1, dtype::Int8())
  1581. .set_dtype(2, dtype::Int32())
  1582. .set_dtype(4, dtype::Int32())
  1583. .set_display(false);
  1584. for (size_t i = 0; i < conv_bias_1x1_args_nchw44.size(); ++i) {
  1585. auto&& arg_nchw = conv_bias_1x1_args_nchw[i];
  1586. auto&& arg_nchw44 = conv_bias_1x1_args_nchw44[i];
  1587. size_t IC = arg_nchw.src[1];
  1588. size_t OH = arg_nchw.src[2];
  1589. size_t OW = arg_nchw.src[3];
  1590. size_t OC = arg_nchw.filter[0];
  1591. size_t M = OC;
  1592. size_t K = IC;
  1593. size_t N = OH * OW;
  1594. float computations = M * N * K * 2.f / (1024 * 1024 * 1024) * 1e3;
  1595. auto conv1x1_nchw = benchmark_conv1x1_nchw.set_param(arg_nchw.param)
  1596. .exec({arg_nchw.src,
  1597. arg_nchw.filter,
  1598. arg_nchw.bias,
  1599. {},
  1600. {}}) /
  1601. RUNS;
  1602. auto conv1x1_nchw44 =
  1603. benchmark_conv1x1_nchw44.set_param(arg_nchw44.param)
  1604. .exec({arg_nchw44.src,
  1605. arg_nchw44.filter,
  1606. arg_nchw44.bias,
  1607. {},
  1608. {}}) /
  1609. RUNS;
  1610. printf("%s %s:\n conv_1x1_nchw: %f ms %f Gflops\nconv1x1_nchw44: %f ms "
  1611. "%f GFlops "
  1612. "speedup: "
  1613. "%f\n",
  1614. arg_nchw.src.to_string().c_str(),
  1615. arg_nchw.filter.to_string().c_str(), conv1x1_nchw,
  1616. computations / conv1x1_nchw, conv1x1_nchw44,
  1617. computations / conv1x1_nchw44, conv1x1_nchw / conv1x1_nchw44);
  1618. }
  1619. }
  1620. #endif
  1621. TEST_F(ARM_COMMON, BENCHMARK_CONV_BIAS_WINOGRAD_VS_IM2COL_INT8) {
  1622. auto&& args = get_winograd_benchmark_args(3, 8);
  1623. using namespace conv_bias;
  1624. constexpr size_t RUN = 10;
  1625. Benchmarker<ConvBias> benchmark_im2col(handle());
  1626. benchmark_im2col.set_display(false);
  1627. benchmark_im2col.set_times(RUN);
  1628. benchmark_im2col.set_dtype(0, dtype::QuantizedS8(2.5f))
  1629. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1630. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1631. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1632. Benchmarker<ConvBias> benchmark_winograd(handle());
  1633. benchmark_winograd.set_display(false);
  1634. benchmark_winograd.set_times(RUN);
  1635. benchmark_winograd.set_dtype(0, dtype::QuantizedS8(2.5f))
  1636. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1637. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1638. .set_dtype(4, dtype::QuantizedS8(60.25f));
  1639. for (auto&& arg : args) {
  1640. TensorLayout dst_layout;
  1641. auto opr = handle()->create_operator<ConvBias>();
  1642. opr->param() = arg.param;
  1643. opr->deduce_layout({arg.src, dtype::Float32()},
  1644. {arg.filter, dtype::Float32()},
  1645. {arg.bias, dtype::Float32()}, {}, dst_layout);
  1646. //! dst.nr_elems * IC * FH * FW * 2
  1647. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  1648. arg.filter[2] * arg.filter[3] * 2.0 /
  1649. (1024 * 1024 * 1024) * 1e3;
  1650. benchmark_im2col.set_param(arg.param);
  1651. auto im2col_used =
  1652. algo_benchmark<ConvBias>(
  1653. benchmark_im2col, {arg.src, arg.filter, {}, {}, {}},
  1654. "IM2COLMATMUL:AARCH64_INT8X8X32_K4X4X16") /
  1655. RUN;
  1656. benchmark_winograd.set_param(arg.param);
  1657. auto winograd_used =
  1658. algo_benchmark<ConvBias>(
  1659. benchmark_winograd, {arg.src, arg.filter, {}, {}, {}},
  1660. "WINOGRAD:AARCH64_INT16X16X32_MK8_8X8:8:2") /
  1661. RUN;
  1662. printf("%s %s: im2col: %f ms %f Gflops winograd: %f ms %f GFlops "
  1663. "speedup: "
  1664. "%f\n",
  1665. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  1666. im2col_used, computations / im2col_used, winograd_used,
  1667. computations / winograd_used, im2col_used / winograd_used);
  1668. }
  1669. }
  1670. #endif
  1671. // vim: syntax=cpp.doxygen

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