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.

convolution.cpp 49 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. /**
  2. * \file dnn/test/arm_common/convolution.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 implied.
  10. */
  11. #include "test/arm_common/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/convolution.h"
  15. #include "test/common/timer.h"
  16. using namespace megdnn;
  17. using namespace test;
  18. using Param = param::Convolution;
  19. #if __ARM_FEATURE_DOTPROD
  20. TEST_F(ARM_COMMON, CONVOLUTION_BACKWARD_DATA_INT8_INT8_INT32) {
  21. Checker<ConvolutionBackwardData> checker(handle());
  22. using Param = ConvolutionBackwardData::Param;
  23. Param param;
  24. auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
  25. size_t fh, size_t fw, size_t stride, size_t ph, size_t pw,
  26. size_t group = 1) {
  27. param.pad_h = ph;
  28. param.pad_w = pw;
  29. param.stride_h = param.stride_w = stride;
  30. TensorLayout diff =
  31. TensorLayout{{n, oc * group, oh, ow}, dtype::Int8()};
  32. TensorLayout grad;
  33. TensorLayout filter;
  34. if (group == 1) {
  35. param.sparse = Param::Sparse::DENSE;
  36. filter = {{oc, ic, fh, fw}, dtype::Int8()};
  37. } else {
  38. param.sparse = Param::Sparse::GROUP;
  39. filter = {{group, oc, ic, fh, fw}, dtype::Int8()};
  40. }
  41. // TensorLayout grad;
  42. {
  43. auto opr = handle()->create_operator<ConvolutionBackwardData>();
  44. opr->param() = param;
  45. opr->deduce_layout(filter, diff, grad);
  46. }
  47. if(stride == 1 ){
  48. checker.set_before_exec_callback(AlgoChecker<
  49. ConvolutionBackwardData>(
  50. "AARCH32_I8x8x32_DECONV_STRIDE1"));
  51. } else {
  52. checker.set_before_exec_callback(AlgoChecker<
  53. ConvolutionBackwardData>(
  54. "AARCH32_I8x8x32_DECONV_STRIDE2"));
  55. }
  56. checker.set_param(param)
  57. .set_dtype(0, dtype::Int8())
  58. .set_dtype(1, dtype::Int8())
  59. .set_dtype(2, dtype::Int32());
  60. checker.exec(TensorLayoutArray{filter, diff, grad});
  61. };
  62. // clang-format off
  63. for (size_t f : {2, 3, 5, 7})
  64. for (size_t ih = 1; ih < f+1; ++ih)
  65. for (size_t iw = 1; iw < 8*f+1; ++iw)
  66. for (size_t s : {1, 2})
  67. for (size_t ph : {f/2, f-1})
  68. for (size_t pw : {f / 2, f - 1})
  69. if (f >= ph + 1 && f >= pw + 1 && (ih - 1) * s + f > 2 * ph &&
  70. (iw - 1) * s + f > 2 * pw) {
  71. run(2, 3, ih, iw, 2, f, f, s, ph, pw, 1);
  72. }
  73. // clang-format on
  74. }
  75. TEST_F(ARM_COMMON, CONVOLUTION_BACKWARD_DATA_QUINT8) {
  76. Checker<ConvolutionBackwardData> checker(handle());
  77. using Param = ConvolutionBackwardData::Param;
  78. Param param;
  79. auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
  80. size_t fh, size_t fw, size_t stride, size_t ph, size_t pw,
  81. size_t group = 1) {
  82. param.pad_h = ph;
  83. param.pad_w = pw;
  84. param.stride_h = param.stride_w = stride;
  85. TensorLayout diff =
  86. TensorLayout{{n, oc * group, oh, ow}, dtype::Quantized8Asymm(1.3f, (uint8_t)129)};
  87. TensorLayout grad;
  88. TensorLayout filter;
  89. if (group == 1) {
  90. param.sparse = Param::Sparse::DENSE;
  91. filter = {{oc, ic, fh, fw}, dtype::Quantized8Asymm(1.2f, (uint8_t)127)};
  92. } else {
  93. param.sparse = Param::Sparse::GROUP;
  94. filter = {{group, oc, ic, fh, fw}, dtype::Quantized8Asymm(1.2f, (uint8_t)127)};
  95. }
  96. // TensorLayout grad;
  97. {
  98. auto opr = handle()->create_operator<ConvolutionBackwardData>();
  99. opr->param() = param;
  100. opr->deduce_layout(filter, diff, grad);
  101. }
  102. NormalRNG rng(128.f);
  103. if(stride == 1 ){
  104. checker.set_before_exec_callback(
  105. AlgoChecker<ConvolutionBackwardData>(
  106. "ARM_COMMON_QUINT8_DIRECT_"
  107. "DECONV_STRIDE1"));
  108. } else {
  109. checker.set_before_exec_callback(
  110. AlgoChecker<ConvolutionBackwardData>(
  111. "ARM_COMMON_QUINT8_DIRECT_"
  112. "DECONV_STRIDE2"));
  113. }
  114. checker.set_param(param)
  115. .set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)127))
  116. .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)129))
  117. .set_dtype(2, {});
  118. checker.set_rng(0, &rng).set_rng(1, &rng);
  119. checker.exec(TensorLayoutArray{filter, diff, grad});
  120. };
  121. // clang-format off
  122. for (size_t f : {2, 3, 5, 7})
  123. for (size_t ih = 1; ih < f+1; ++ih)
  124. for (size_t iw = 1; iw < 8*f+1; ++iw)
  125. for (size_t s : {1, 2})
  126. for (size_t ph : {f/2, f-1})
  127. for (size_t pw : {f/2, f-1})
  128. if (f >= ph + 1 && f >= pw + 1 && (ih - 1) * s + f > 2 * ph &&
  129. (iw - 1) * s + f > 2 * pw) {
  130. run(2, 2, ih, iw, 2, f, f, s, ph, pw, 1);
  131. }
  132. // clang-format on
  133. }
  134. #endif
  135. #if MEGDNN_WITH_BENCHMARK
  136. #if __ARM_FEATURE_DOTPROD
  137. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_STRIDE1_I8x8x32_WITHDOTPROD) {
  138. using namespace convolution;
  139. using Param = param::Convolution;
  140. std::vector<TestArg> args;
  141. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  142. size_t stride) {
  143. Param param;
  144. param.stride_h = stride;
  145. param.stride_w = stride;
  146. param.pad_h = kernel / 2;
  147. param.pad_w = kernel / 2;
  148. args.emplace_back(param, TensorShape{1, ic, h, w},
  149. TensorShape{oc, ic, kernel, kernel});
  150. };
  151. for (size_t kernel : {2, 3, 5, 7}) {
  152. for (size_t ic : {1, 8, 16, 32, 64}) {
  153. for (size_t oc : {1, 8, 16, 32, 64}) {
  154. run(oc, ic, 56, 56, kernel, 1);
  155. run(oc, ic, 128, 128, kernel, 1);
  156. run(oc, ic, 256, 256, kernel, 1);
  157. }
  158. }
  159. }
  160. constexpr size_t RUN = 50;
  161. Benchmarker<Convolution> benchmark(handle());
  162. benchmark.set_before_exec_callback(
  163. AlgoChecker<Convolution>("CONVOLUTION_DEFAULT_ARMDOTS8STRD1"));
  164. benchmark.set_dtype(0, dtype::Int8())
  165. .set_dtype(1, dtype::Int8())
  166. .set_dtype(2, dtype::Int32());
  167. benchmark.set_display(false);
  168. benchmark.set_times(RUN);
  169. Benchmarker<Convolution> benchmark_float(handle());
  170. benchmark_float.set_display(false);
  171. benchmark_float.set_times(RUN);
  172. for (auto&& arg : args) {
  173. TensorLayout dst_layout;
  174. auto opr = handle()->create_operator<Convolution>();
  175. opr->param() = arg.param;
  176. opr->deduce_layout({arg.src, dtype::Float32()},
  177. {arg.filter, dtype::Float32()}, dst_layout);
  178. //! dst.nr_elems * IC * FH * FW * 2
  179. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  180. arg.filter[2] * arg.filter[3] * 2.0 /
  181. (1024 * 1024 * 1024) * 1e3;
  182. auto used_int = benchmark.set_param(arg.param).exec(
  183. {arg.src, arg.filter, {}}) /
  184. RUN;
  185. auto used_float = benchmark_float.set_param(arg.param).exec(
  186. {arg.src, arg.filter, {}}) /
  187. RUN;
  188. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  189. "%f\n",
  190. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  191. used_int, computations / used_int, used_float,
  192. computations / used_float, used_float / used_int);
  193. }
  194. }
  195. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_STRIDE2_I8x8x32_WITHDOTPROD) {
  196. using namespace convolution;
  197. using Param = param::Convolution;
  198. std::vector<TestArg> args;
  199. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  200. size_t stride) {
  201. Param param;
  202. param.stride_h = stride;
  203. param.stride_w = stride;
  204. param.pad_h = kernel / 2;
  205. param.pad_w = kernel / 2;
  206. args.emplace_back(param, TensorShape{1, ic, h, w},
  207. TensorShape{oc, ic, kernel, kernel});
  208. };
  209. for (size_t kernel : {2, 3, 5, 7}) {
  210. for (size_t ic : {1, 8, 16, 32, 64}) {
  211. for (size_t oc : {1, 8, 16, 32, 64}) {
  212. run(oc, ic, 56, 56, kernel, 2);
  213. run(oc, ic, 128, 128, kernel, 2);
  214. run(oc, ic, 256, 256, kernel, 2);
  215. }
  216. }
  217. }
  218. constexpr size_t RUN = 10;
  219. Benchmarker<Convolution> benchmark(handle());
  220. benchmark.set_before_exec_callback(
  221. AlgoChecker<Convolution>("CONVOLUTION_DEFAULT_ARMDOTS8STRD2"));
  222. benchmark.set_dtype(0, dtype::Int8())
  223. .set_dtype(1, dtype::Int8())
  224. .set_dtype(2, dtype::Int32());
  225. benchmark.set_display(false);
  226. benchmark.set_times(RUN);
  227. Benchmarker<Convolution> benchmark_float(handle());
  228. benchmark_float.set_display(false);
  229. benchmark_float.set_times(RUN);
  230. for (auto&& arg : args) {
  231. TensorLayout dst_layout;
  232. auto opr = handle()->create_operator<Convolution>();
  233. opr->param() = arg.param;
  234. opr->deduce_layout({arg.src, dtype::Float32()},
  235. {arg.filter, dtype::Float32()}, dst_layout);
  236. //! dst.nr_elems * IC * FH * FW * 2
  237. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  238. arg.filter[2] * arg.filter[3] * 2.0 /
  239. (1024 * 1024 * 1024) * 1e3;
  240. auto used_int =
  241. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  242. RUN;
  243. auto used_float = benchmark_float.set_param(arg.param).exec(
  244. {arg.src, arg.filter, {}}) /
  245. RUN;
  246. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  247. "%f\n",
  248. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  249. used_int, computations / used_int, used_float,
  250. computations / used_float, used_float / used_int);
  251. }
  252. }
  253. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_STRIDE1_QUINT8_WITHDOTPROD) {
  254. using namespace convolution;
  255. using Param = param::Convolution;
  256. std::vector<TestArg> args;
  257. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  258. size_t stride) {
  259. Param param;
  260. param.stride_h = stride;
  261. param.stride_w = stride;
  262. param.pad_h = kernel / 2;
  263. param.pad_w = kernel / 2;
  264. args.emplace_back(param, TensorShape{1, ic, h, w},
  265. TensorShape{oc, ic, kernel, kernel});
  266. };
  267. for (size_t kernel : {2, 3, 5, 7}) {
  268. for (size_t ic : {1, 8, 16, 32, 64}) {
  269. for (size_t oc : {1, 8, 16, 32, 64}) {
  270. run(oc, ic, 56, 56, kernel, 1);
  271. run(oc, ic, 128, 128, kernel, 1);
  272. run(oc, ic, 256, 256, kernel, 1);
  273. }
  274. }
  275. }
  276. constexpr size_t RUN = 50;
  277. Benchmarker<Convolution> benchmark(handle());
  278. benchmark.set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)129))
  279. .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)127))
  280. .set_dtype(2, {});
  281. benchmark.set_display(false);
  282. benchmark.set_times(RUN);
  283. benchmark.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  284. "CONVOLUTION_DEFAULT_ARMDOTU8STRD1"));
  285. Benchmarker<Convolution> benchmark_float(handle());
  286. benchmark_float.set_display(false);
  287. benchmark_float.set_times(RUN);
  288. for (auto&& arg : args) {
  289. TensorLayout dst_layout;
  290. auto opr = handle()->create_operator<Convolution>();
  291. opr->param() = arg.param;
  292. opr->deduce_layout({arg.src, dtype::Float32()},
  293. {arg.filter, dtype::Float32()}, dst_layout);
  294. //! dst.nr_elems * IC * FH * FW * 2
  295. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  296. arg.filter[2] * arg.filter[3] * 2.0 /
  297. (1024 * 1024 * 1024) * 1e3;
  298. auto used_int = benchmark.set_param(arg.param).exec(
  299. {arg.src, arg.filter, {}}) /
  300. RUN;
  301. auto used_float = benchmark_float.set_param(arg.param).exec(
  302. {arg.src, arg.filter, {}}) /
  303. RUN;
  304. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  305. "%f\n",
  306. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  307. used_int, computations / used_int, used_float,
  308. computations / used_float, used_float / used_int);
  309. }
  310. }
  311. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_STRIDE2_QUINT8_WITHDOTPROD) {
  312. using namespace convolution;
  313. using Param = param::Convolution;
  314. std::vector<TestArg> args;
  315. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  316. size_t stride) {
  317. Param param;
  318. param.stride_h = stride;
  319. param.stride_w = stride;
  320. param.pad_h = kernel / 2;
  321. param.pad_w = kernel / 2;
  322. args.emplace_back(param, TensorShape{1, ic, h, w},
  323. TensorShape{oc, ic, kernel, kernel});
  324. };
  325. for (size_t kernel : {2, 3, 5, 7}) {
  326. for (size_t ic : {1, 8, 16, 32, 64}) {
  327. for (size_t oc : {1, 8, 16, 32, 64}) {
  328. run(oc, ic, 56, 56, kernel, 2);
  329. run(oc, ic, 128, 128, kernel, 2);
  330. run(oc, ic, 256, 256, kernel, 2);
  331. }
  332. }
  333. }
  334. constexpr size_t RUN = 50;
  335. Benchmarker<Convolution> benchmark(handle());
  336. benchmark.set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)129))
  337. .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)127))
  338. .set_dtype(2, {});
  339. benchmark.set_display(false);
  340. benchmark.set_times(RUN);
  341. benchmark.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  342. "CONVOLUTION_DEFAULT_ARMDOTU8STRD2"));
  343. Benchmarker<Convolution> benchmark_float(handle());
  344. benchmark_float.set_display(false);
  345. benchmark_float.set_times(RUN);
  346. for (auto&& arg : args) {
  347. TensorLayout dst_layout;
  348. auto opr = handle()->create_operator<Convolution>();
  349. opr->param() = arg.param;
  350. opr->deduce_layout({arg.src, dtype::Float32()},
  351. {arg.filter, dtype::Float32()}, dst_layout);
  352. //! dst.nr_elems * IC * FH * FW * 2
  353. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  354. arg.filter[2] * arg.filter[3] * 2.0 /
  355. (1024 * 1024 * 1024) * 1e3;
  356. auto used_int = benchmark.set_param(arg.param).exec(
  357. {arg.src, arg.filter, {}}) /
  358. RUN;
  359. auto used_float = benchmark_float.set_param(arg.param).exec(
  360. {arg.src, arg.filter, {}}) /
  361. RUN;
  362. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  363. "%f\n",
  364. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  365. used_int, computations / used_int, used_float,
  366. computations / used_float, used_float / used_int);
  367. }
  368. }
  369. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_BACKWARD_DATA_INT8_INT8_INT32) {
  370. using Param = ConvolutionBackwardData::Param;
  371. auto run = [&](const TensorLayoutArray& tensors, Param param) {
  372. Benchmarker<ConvolutionBackwardData> benchmarker(handle());
  373. size_t RUN = 50;
  374. auto time = benchmarker.set_display(false)
  375. .set_dtype(0, dtype::Int8{})
  376. .set_dtype(1, dtype::Int8{})
  377. .set_dtype(2, dtype::Int32{})
  378. .set_times(RUN)
  379. .set_param(param)
  380. .exec(tensors);
  381. size_t OC = tensors[0][0];
  382. size_t FH = tensors[0][2];
  383. size_t FW = tensors[0][3];
  384. float computations = tensors[2].total_nr_elems() * OC * FH * FW * 2.0 /
  385. (1024 * 1024 * 1024) * 1e3;
  386. printf("time = %f \n perf= %f gops\n", time, computations * RUN / time);
  387. };
  388. auto profile = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
  389. size_t fh, size_t fw, size_t s) {
  390. Param param;
  391. param.stride_h = param.stride_w = s;
  392. printf("oc: %zd ic: %zd w: %zd h: %zd kernel_size: %zd sreide: %zd\n",
  393. oc, ic, ow, oh, fh, s);
  394. TensorLayout diff = TensorLayout{{n, oc, oh, ow}, dtype::Int8()};
  395. TensorLayout filter = TensorLayout{{oc, ic, fh, fw}, dtype::Int8()};
  396. TensorLayout grad;
  397. {
  398. auto opr = handle()->create_operator<ConvolutionBackwardData>();
  399. opr->param() = param;
  400. opr->deduce_layout(filter, diff, grad);
  401. }
  402. run(TensorLayoutArray{filter, diff, grad}, param);
  403. };
  404. profile(1, 3, 120, 120, 2, 3, 3, 1);
  405. profile(1, 3, 60, 60, 2, 3, 3, 2);
  406. profile(1, 3, 224, 224, 2, 5, 5, 1);
  407. profile(1, 3, 112, 112, 2, 5, 5, 2);
  408. profile(1, 3, 224, 224, 2, 7, 7, 1);
  409. profile(1, 3, 112, 112, 2, 7, 7, 2);
  410. }
  411. #endif
  412. TEST_F(ARM_COMMON, BENCHMARK_CHANWISE_CONVOLUTION) {
  413. auto run = [&](const TensorShapeArray& shapes, Param param) {
  414. auto handle_naive = create_cpu_handle(2);
  415. Benchmarker<Convolution> benchmarker_naive(handle_naive.get()),
  416. benchmarker_float(handle()), benchmarker_int(handle());
  417. benchmarker_int.set_dtype(0, dtype::Int8());
  418. benchmarker_int.set_dtype(1, dtype::Int8());
  419. benchmarker_int.set_dtype(2, dtype::Int16());
  420. size_t RUN = 10;
  421. auto tfloat = benchmarker_float.set_display(false)
  422. .set_times(RUN)
  423. .set_param(param)
  424. .exec(shapes);
  425. auto tnaive = benchmarker_naive.set_display(false)
  426. .set_times(RUN)
  427. .set_param(param)
  428. .exec(shapes);
  429. auto iparam = param;
  430. auto tint = benchmarker_int.set_display(false)
  431. .set_times(RUN)
  432. .set_param(iparam)
  433. .exec(shapes);
  434. float int_float_ratio = static_cast<float>(tfloat) / tint;
  435. printf("naive=%.3fms float=%.3fms int=%.3fms, int/float=%.3f\n",
  436. tnaive / RUN, tfloat / RUN, tint / RUN, int_float_ratio);
  437. EXPECT_GE(int_float_ratio, 1.5);
  438. };
  439. Param param;
  440. param.mode = Param::Mode::CROSS_CORRELATION;
  441. param.sparse = Param::Sparse::GROUP;
  442. run({{2, 12, 200, 100}, {12, 2, 1, 5, 5}, {}}, param);
  443. run({{10, 24, 28, 28}, {24, 1, 1, 3, 3}, {}}, param);
  444. param.stride_h = 2;
  445. param.stride_w = 2;
  446. param.pad_h = 1;
  447. param.pad_w = 1;
  448. run({{2, 12, 200, 100}, {12, 2, 1, 5, 5}, {}}, param);
  449. run({{10, 24, 28, 28}, {24, 1, 1, 3, 3}, {}}, param);
  450. }
  451. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_INT8X8X32_STRD1_WITHOUT_DOTPROD) {
  452. // have to remove preferred restrict in usable func before run the benchmark
  453. using namespace convolution;
  454. std::vector<TestArg> args;
  455. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  456. size_t p) {
  457. if (w + 2 * p < kernel || h + 2 * p < kernel)
  458. return;
  459. param::Convolution param;
  460. param.stride_h = 1;
  461. param.stride_w = 1;
  462. param.pad_h = p;
  463. param.pad_w = p;
  464. args.emplace_back(param, TensorShape{1, ic, h, w},
  465. TensorShape{oc, ic, kernel, kernel});
  466. };
  467. // compare to float direct conv here,
  468. // but float direct conv don't support 7x7.
  469. for (size_t kernel : {2, 3, 5})
  470. for (size_t ic : {1, 8, 16, 32, 64})
  471. for (size_t oc : {1, 8, 16, 32, 64})
  472. for (size_t p : {0, 1, 2, 3}) {
  473. run(oc, ic, 56, 56, kernel, p);
  474. run(oc, ic, 128, 128, kernel, p);
  475. run(oc, ic, 256, 256, kernel, p);
  476. }
  477. constexpr size_t RUN = 50;
  478. Benchmarker<Convolution> benchmark(handle());
  479. benchmark.set_dtype(0, dtype::Int8())
  480. .set_dtype(1, dtype::Int8())
  481. .set_dtype(2, dtype::Int32());
  482. benchmark.set_display(false);
  483. benchmark.set_times(RUN);
  484. benchmark.set_before_exec_callback(
  485. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_S8STRD1"));
  486. Benchmarker<Convolution> benchmark_float(handle());
  487. benchmark_float.set_display(false);
  488. benchmark_float.set_times(RUN);
  489. benchmark_float.set_before_exec_callback(
  490. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_F32STRD1"));
  491. for (auto&& arg : args) {
  492. TensorLayout dst_layout;
  493. auto opr = handle()->create_operator<Convolution>();
  494. opr->param() = arg.param;
  495. opr->deduce_layout({arg.src, dtype::Float32()},
  496. {arg.filter, dtype::Float32()}, dst_layout);
  497. //! dst.nr_elems * IC * FH * FW * 2
  498. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  499. arg.filter[2] * arg.filter[3] * 2.0 /
  500. (1024 * 1024 * 1024) * 1e3;
  501. auto used_int =
  502. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  503. RUN;
  504. auto used_float = benchmark_float.set_param(arg.param).exec(
  505. {arg.src, arg.filter, {}}) /
  506. RUN;
  507. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  508. "%f\n",
  509. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  510. used_int, computations / used_int, used_float,
  511. computations / used_float, used_float / used_int);
  512. }
  513. }
  514. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_INT8X8X32_STRD2_WITHOUT_DOTPROD) {
  515. // have to remove preferred restrict in usable func before run the benchmark
  516. using namespace convolution;
  517. std::vector<TestArg> args;
  518. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  519. size_t p) {
  520. if (w + 2 * p < kernel || h + 2 * p < kernel)
  521. return;
  522. param::Convolution param;
  523. param.stride_h = 2;
  524. param.stride_w = 2;
  525. param.pad_h = p;
  526. param.pad_w = p;
  527. args.emplace_back(param, TensorShape{1, ic, h, w},
  528. TensorShape{oc, ic, kernel, kernel});
  529. };
  530. for (size_t kernel : {2, 3, 5, 7})
  531. for (size_t ic : {1, 8, 16, 32, 64})
  532. for (size_t oc : {1, 8, 16, 32, 64})
  533. for (size_t p : {0, 1, 2, 3}) {
  534. run(oc, ic, 56, 56, kernel, p);
  535. run(oc, ic, 128, 128, kernel, p);
  536. run(oc, ic, 256, 256, kernel, p);
  537. }
  538. constexpr size_t RUN = 50;
  539. Benchmarker<Convolution> benchmark(handle());
  540. benchmark.set_dtype(0, dtype::Int8())
  541. .set_dtype(1, dtype::Int8())
  542. .set_dtype(2, dtype::Int32());
  543. benchmark.set_display(false);
  544. benchmark.set_times(RUN);
  545. benchmark.set_before_exec_callback(
  546. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_S8STRD2"));
  547. Benchmarker<Convolution> benchmark_float(handle());
  548. benchmark_float.set_display(false);
  549. benchmark_float.set_times(RUN);
  550. #if MEGDNN_AARCH64
  551. benchmark_float.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  552. "CONVOLUTION_DEFAULT_ARMV8F32STRD2"));
  553. #else
  554. benchmark_float.set_before_exec_callback(
  555. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_F32STRD2"));
  556. #endif
  557. for (auto&& arg : args) {
  558. TensorLayout dst_layout;
  559. auto opr = handle()->create_operator<Convolution>();
  560. opr->param() = arg.param;
  561. opr->deduce_layout({arg.src, dtype::Float32()},
  562. {arg.filter, dtype::Float32()}, dst_layout);
  563. //! dst.nr_elems * IC * FH * FW * 2
  564. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  565. arg.filter[2] * arg.filter[3] * 2.0 /
  566. (1024 * 1024 * 1024) * 1e3;
  567. auto used_int =
  568. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  569. RUN;
  570. auto used_float = benchmark_float.set_param(arg.param).exec(
  571. {arg.src, arg.filter, {}}) /
  572. RUN;
  573. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  574. "%f\n",
  575. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  576. used_int, computations / used_int, used_float,
  577. computations / used_float, used_float / used_int);
  578. }
  579. }
  580. TEST_F(ARM_COMMON,
  581. BENCHMARK_CONVOLUTION_INT8X8X32_STRD1_WITHOUT_DOTPROD_TO_MATMUL) {
  582. // have to remove preferred restrict in usable func before run the benchmark
  583. using namespace convolution;
  584. std::vector<TestArg> args;
  585. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  586. size_t p) {
  587. if (w + 2 * p < kernel || h + 2 * p < kernel)
  588. return;
  589. param::Convolution param;
  590. param.stride_h = 1;
  591. param.stride_w = 1;
  592. param.pad_h = p;
  593. param.pad_w = p;
  594. args.emplace_back(param, TensorShape{1, ic, h, w},
  595. TensorShape{oc, ic, kernel, kernel});
  596. };
  597. for (size_t kernel : {2, 3, 5, 7})
  598. for (size_t p : {0, 1, 2})
  599. for (size_t ic : {1, 3, 4, 8, 12, 16, 32, 48, 64})
  600. for (size_t oc : {1, 3, 4, 8, 12, 16, 32, 48, 64})
  601. for (size_t size : {56, 128, 256}) {
  602. run(oc, ic, size, size, kernel, p);
  603. }
  604. constexpr size_t RUN = 50;
  605. Benchmarker<Convolution> benchmark_conv(handle());
  606. benchmark_conv.set_dtype(0, dtype::Int8())
  607. .set_dtype(1, dtype::Int8())
  608. .set_dtype(2, dtype::Int32());
  609. benchmark_conv.set_display(false);
  610. benchmark_conv.set_times(RUN);
  611. benchmark_conv.set_before_exec_callback(
  612. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_S8STRD1"));
  613. Benchmarker<Convolution> benchmark_matmul(handle());
  614. benchmark_matmul.set_dtype(0, dtype::Int8())
  615. .set_dtype(1, dtype::Int8())
  616. .set_dtype(2, dtype::Int32());
  617. benchmark_matmul.set_display(false);
  618. benchmark_matmul.set_times(RUN);
  619. for (auto&& arg : args) {
  620. TensorLayout dst_layout;
  621. auto opr = handle()->create_operator<Convolution>();
  622. opr->param() = arg.param;
  623. opr->deduce_layout({arg.src, dtype::Float32()},
  624. {arg.filter, dtype::Float32()}, dst_layout);
  625. //! dst.nr_elems * IC * FH * FW * 2
  626. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  627. arg.filter[2] * arg.filter[3] * 2.0 /
  628. (1024 * 1024 * 1024) * 1e3;
  629. auto used_conv = benchmark_conv.set_param(arg.param).exec(
  630. {arg.src, arg.filter, {}}) /
  631. RUN;
  632. auto used_matmul = benchmark_matmul.set_param(arg.param).exec(
  633. {arg.src, arg.filter, {}}) /
  634. RUN;
  635. printf("%s %s: conv: %f ms %f Gflops matmul: %f ms %f GFlops speedup: "
  636. "%f\n",
  637. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  638. used_conv, computations / used_conv, used_matmul,
  639. computations / used_matmul, used_matmul / used_conv);
  640. }
  641. }
  642. TEST_F(ARM_COMMON,
  643. BENCHMARK_CONVOLUTION_INT8X8X32_STRD2_WITHOUT_DOTPROD_TO_MATMUL) {
  644. // have to remove preferred restrict in usable func before run the benchmark
  645. using namespace convolution;
  646. std::vector<TestArg> args;
  647. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  648. size_t p) {
  649. if (w + 2 * p < kernel || h + 2 * p < kernel)
  650. return;
  651. param::Convolution param;
  652. param.stride_h = 2;
  653. param.stride_w = 2;
  654. param.pad_h = p;
  655. param.pad_w = p;
  656. args.emplace_back(param, TensorShape{1, ic, h, w},
  657. TensorShape{oc, ic, kernel, kernel});
  658. };
  659. for (size_t kernel : {2, 3, 5, 7})
  660. for (size_t p : {0, 1, 2})
  661. for (size_t ic : {1, 3, 4, 8, 12, 16, 32, 48, 64})
  662. for (size_t oc : {1, 3, 4, 8, 12, 16, 32, 48, 64})
  663. for (size_t size : {56, 128, 256}) {
  664. run(oc, ic, size, size, kernel, p);
  665. }
  666. constexpr size_t RUN = 50;
  667. Benchmarker<Convolution> benchmark_conv(handle());
  668. benchmark_conv.set_dtype(0, dtype::Int8())
  669. .set_dtype(1, dtype::Int8())
  670. .set_dtype(2, dtype::Int32());
  671. benchmark_conv.set_display(false);
  672. benchmark_conv.set_times(RUN);
  673. benchmark_conv.set_before_exec_callback(
  674. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_S8STRD2"));
  675. Benchmarker<Convolution> benchmark_matmul(handle());
  676. benchmark_matmul.set_dtype(0, dtype::Int8())
  677. .set_dtype(1, dtype::Int8())
  678. .set_dtype(2, dtype::Int32());
  679. benchmark_matmul.set_display(false);
  680. benchmark_matmul.set_times(RUN);
  681. for (auto&& arg : args) {
  682. TensorLayout dst_layout;
  683. auto opr = handle()->create_operator<Convolution>();
  684. opr->param() = arg.param;
  685. opr->deduce_layout({arg.src, dtype::Float32()},
  686. {arg.filter, dtype::Float32()}, dst_layout);
  687. //! dst.nr_elems * IC * FH * FW * 2
  688. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  689. arg.filter[2] * arg.filter[3] * 2.0 /
  690. (1024 * 1024 * 1024) * 1e3;
  691. auto used_conv = benchmark_conv.set_param(arg.param).exec(
  692. {arg.src, arg.filter, {}}) /
  693. RUN;
  694. auto used_matmul = benchmark_matmul.set_param(arg.param).exec(
  695. {arg.src, arg.filter, {}}) /
  696. RUN;
  697. printf("%s %s: conv: %f ms %f Gflops matmul: %f ms %f GFlops speedup: "
  698. "%f\n",
  699. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  700. used_conv, computations / used_conv, used_matmul,
  701. computations / used_matmul, used_matmul / used_conv);
  702. }
  703. }
  704. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_QUINT8X8X32_STRD1_WITHOUT_DOTPROD) {
  705. // have to remove preferred restrict in usable func before run the benchmark
  706. using namespace convolution;
  707. std::vector<TestArg> args;
  708. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  709. size_t p) {
  710. if (w + 2 * p < kernel || h + 2 * p < kernel)
  711. return;
  712. param::Convolution param;
  713. param.stride_h = 1;
  714. param.stride_w = 1;
  715. param.pad_h = p;
  716. param.pad_w = p;
  717. args.emplace_back(param, TensorShape{1, ic, h, w},
  718. TensorShape{oc, ic, kernel, kernel});
  719. };
  720. // compare to float direct conv here,
  721. // but float direct conv don't support 7x7.
  722. for (size_t kernel : {2, 3, 5})
  723. for (size_t ic : {1, 8, 16, 32, 64})
  724. for (size_t oc : {1, 8, 16, 32, 64})
  725. for (size_t p : {0, 1, 2, 3}) {
  726. run(oc, ic, 56, 56, kernel, p);
  727. run(oc, ic, 128, 128, kernel, p);
  728. run(oc, ic, 256, 256, kernel, p);
  729. }
  730. constexpr size_t RUN = 50;
  731. Benchmarker<Convolution> benchmark(handle());
  732. benchmark.set_dtype(0, dtype::Quantized8Asymm(0.1f, static_cast<uint8_t>(120)))
  733. .set_dtype(1, dtype::Quantized8Asymm(0.1f, static_cast<uint8_t>(120)))
  734. .set_dtype(2, dtype::QuantizedS32(0.01f));
  735. benchmark.set_display(false);
  736. benchmark.set_times(RUN);
  737. benchmark.set_before_exec_callback(
  738. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_QU8STRD1"));
  739. Benchmarker<Convolution> benchmark_float(handle());
  740. benchmark_float.set_display(false);
  741. benchmark_float.set_times(RUN);
  742. benchmark_float.set_before_exec_callback(
  743. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_F32STRD1"));
  744. for (auto&& arg : args) {
  745. TensorLayout dst_layout;
  746. auto opr = handle()->create_operator<Convolution>();
  747. opr->param() = arg.param;
  748. opr->deduce_layout({arg.src, dtype::Float32()},
  749. {arg.filter, dtype::Float32()}, dst_layout);
  750. //! dst.nr_elems * IC * FH * FW * 2
  751. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  752. arg.filter[2] * arg.filter[3] * 2.0 /
  753. (1024 * 1024 * 1024) * 1e3;
  754. auto used_int =
  755. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  756. RUN;
  757. auto used_float = benchmark_float.set_param(arg.param).exec(
  758. {arg.src, arg.filter, {}}) /
  759. RUN;
  760. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  761. "%f\n",
  762. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  763. used_int, computations / used_int, used_float,
  764. computations / used_float, used_float / used_int);
  765. }
  766. }
  767. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_QUINT8X8X32_STRD2_WITHOUT_DOTPROD) {
  768. // have to remove preferred restrict in usable func before run the benchmark
  769. using namespace convolution;
  770. std::vector<TestArg> args;
  771. auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  772. size_t p) {
  773. if (w + 2 * p < kernel || h + 2 * p < kernel)
  774. return;
  775. param::Convolution param;
  776. param.stride_h = 2;
  777. param.stride_w = 2;
  778. param.pad_h = p;
  779. param.pad_w = p;
  780. args.emplace_back(param, TensorShape{1, ic, h, w},
  781. TensorShape{oc, ic, kernel, kernel});
  782. };
  783. for (size_t kernel : {2, 3, 5, 7})
  784. for (size_t ic : {1, 8, 16, 32, 64})
  785. for (size_t oc : {1, 8, 16, 32, 64})
  786. for (size_t p : {0, 1, 2, 3}) {
  787. run(oc, ic, 56, 56, kernel, p);
  788. run(oc, ic, 128, 128, kernel, p);
  789. run(oc, ic, 256, 256, kernel, p);
  790. }
  791. constexpr size_t RUN = 50;
  792. Benchmarker<Convolution> benchmark(handle());
  793. benchmark.set_dtype(0, dtype::Quantized8Asymm(0.1f, static_cast<uint8_t>(120)))
  794. .set_dtype(1, dtype::Quantized8Asymm(0.1f, static_cast<uint8_t>(120)))
  795. .set_dtype(2, dtype::QuantizedS32(0.01f));
  796. benchmark.set_display(false);
  797. benchmark.set_times(RUN);
  798. benchmark.set_before_exec_callback(
  799. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_QU8STRD2"));
  800. Benchmarker<Convolution> benchmark_float(handle());
  801. benchmark_float.set_display(false);
  802. benchmark_float.set_times(RUN);
  803. #if MEGDNN_AARCH64
  804. benchmark_float.set_before_exec_callback(AlgoChecker<ConvolutionForward>(
  805. "CONVOLUTION_DEFAULT_ARMV8F32STRD2"));
  806. #else
  807. benchmark_float.set_before_exec_callback(
  808. AlgoChecker<ConvolutionForward>("CONVOLUTION_DEFAULT_F32STRD2"));
  809. #endif
  810. for (auto&& arg : args) {
  811. TensorLayout dst_layout;
  812. auto opr = handle()->create_operator<Convolution>();
  813. opr->param() = arg.param;
  814. opr->deduce_layout({arg.src, dtype::Float32()},
  815. {arg.filter, dtype::Float32()}, 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 used_int =
  821. benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}}) /
  822. RUN;
  823. auto used_float = benchmark_float.set_param(arg.param).exec(
  824. {arg.src, arg.filter, {}}) /
  825. RUN;
  826. printf("%s %s: int: %f ms %f Gflops float: %f ms %f GFlops speedup: "
  827. "%f\n",
  828. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  829. used_int, computations / used_int, used_float,
  830. computations / used_float, used_float / used_int);
  831. }
  832. }
  833. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_INT8_INT8_INT16) {
  834. using Param = param::Convolution;
  835. auto run = [&](const TensorShapeArray& shapes, Param param) {
  836. TensorLayoutArray layouts;
  837. layouts.emplace_back(shapes[0], dtype::Int8());
  838. layouts.emplace_back(shapes[1], dtype::Int8());
  839. layouts.emplace_back(shapes[2], dtype::Int16());
  840. Benchmarker<Convolution> benchmarker_cpu(handle()),
  841. benchmarker_float(handle());
  842. benchmarker_cpu.set_dtype(0, dtype::Int8());
  843. benchmarker_cpu.set_dtype(1, dtype::Int8());
  844. benchmarker_cpu.set_dtype(2, dtype::Int16());
  845. auto iparam = param;
  846. size_t RUN = 10;
  847. auto t2 = benchmarker_cpu.set_display(false)
  848. .set_times(RUN)
  849. .set_param(iparam)
  850. .execl(layouts);
  851. auto t4 = benchmarker_float.set_display(false)
  852. .set_times(RUN)
  853. .set_param(param)
  854. .exec(shapes);
  855. auto speedup = t4 / t2;
  856. std::cout << "src=" << shapes[0].to_string()
  857. << " filter=" << shapes[1].to_string()
  858. << " stride=" << param.stride_h << " float=" << t4 << "ms"
  859. << " int=" << t2 << "ms"
  860. << " speedup=" << speedup << std::endl;
  861. ASSERT_GE(speedup, 1);
  862. };
  863. /*
  864. for (size_t s: {1, 2})
  865. for (size_t k: {3})
  866. for (size_t c: {16})
  867. for (size_t h = 20; h <= 60; ++h)
  868. {
  869. Param param;
  870. param.stride_h = param.stride_w = s;
  871. run({{1, c, h, h}, {c, c, k, k}, {}}, param);
  872. }
  873. for (size_t s: {1})
  874. for (size_t k: {1})
  875. for (size_t c: {16})
  876. for (size_t h = 16; h <= 1024; h*=2)
  877. {
  878. Param param;
  879. param.stride_h = param.stride_w = s;
  880. run({{1, c, h, h}, {c, c, k, k}, {}}, param);
  881. }
  882. */
  883. for (size_t s : {1}) {
  884. Param param;
  885. param.stride_h = param.stride_w = s;
  886. run({{2, 3, 480, 270}, {12, 3, 1, 1}, {}}, param);
  887. run({{2, 12, 240, 135}, {48, 12, 1, 1}, {}}, param);
  888. run({{2, 16, 240, 135}, {4, 16, 1, 1}, {}}, param);
  889. run({{2, 4, 240, 135}, {16, 4, 1, 1}, {}}, param);
  890. run({{2, 16, 240, 135}, {8, 16, 1, 1}, {}}, param);
  891. run({{2, 8, 120, 68}, {32, 8, 1, 1}, {}}, param);
  892. run({{2, 32, 120, 68}, {8, 32, 1, 1}, {}}, param);
  893. run({{2, 64, 60, 34}, {16, 64, 1, 1}, {}}, param);
  894. }
  895. }
  896. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_INT8_INT8_INT32) {
  897. using Param = param::Convolution;
  898. auto run = [&](const TensorShapeArray& shapes, Param param) {
  899. TensorLayoutArray layouts;
  900. layouts.emplace_back(shapes[0], dtype::Int8());
  901. layouts.emplace_back(shapes[1], dtype::Int8());
  902. layouts.emplace_back(shapes[2], dtype::Int32());
  903. Benchmarker<Convolution> benchmarker_cpu(handle()),
  904. benchmarker_float(handle());
  905. benchmarker_cpu.set_dtype(0, dtype::Int8());
  906. benchmarker_cpu.set_dtype(1, dtype::Int8());
  907. benchmarker_cpu.set_dtype(2, dtype::Int32());
  908. auto iparam = param;
  909. size_t RUN = 10;
  910. auto t2 = benchmarker_cpu.set_display(false)
  911. .set_times(RUN)
  912. .set_param(iparam)
  913. .execl(layouts);
  914. auto t4 = benchmarker_float.set_display(false)
  915. .set_times(RUN)
  916. .set_param(param)
  917. .exec(shapes);
  918. auto speedup = t4 / t2;
  919. std::cout << "src=" << shapes[0].to_string()
  920. << " filter=" << shapes[1].to_string()
  921. << " stride=" << param.stride_h << " float=" << t4 << "ms"
  922. << " int=" << t2 << "ms"
  923. << " speedup=" << speedup << std::endl;
  924. ASSERT_GE(speedup, 1);
  925. };
  926. for (size_t s : {1, 2})
  927. for (size_t k : {3})
  928. for (size_t c : {16})
  929. for (size_t h = 20; h <= 60; ++h) {
  930. Param param;
  931. param.stride_h = param.stride_w = s;
  932. run({{1, c, h, h}, {c, c, k, k}, {}}, param);
  933. }
  934. for (size_t s : {1})
  935. for (size_t k : {1})
  936. for (size_t c : {16})
  937. for (size_t h = 16; h <= 1024; h *= 2) {
  938. Param param;
  939. param.stride_h = param.stride_w = s;
  940. run({{1, c, h, h}, {c, c, k, k}, {}}, param);
  941. }
  942. for (size_t s : {1}) {
  943. Param param;
  944. param.stride_h = param.stride_w = s;
  945. run({{2, 3, 480, 270}, {12, 3, 1, 1}, {}}, param);
  946. run({{2, 12, 240, 135}, {48, 12, 1, 1}, {}}, param);
  947. run({{2, 16, 240, 135}, {4, 16, 1, 1}, {}}, param);
  948. run({{2, 4, 240, 135}, {16, 4, 1, 1}, {}}, param);
  949. run({{2, 16, 240, 135}, {8, 16, 1, 1}, {}}, param);
  950. run({{2, 8, 120, 68}, {32, 8, 1, 1}, {}}, param);
  951. run({{2, 32, 120, 68}, {8, 32, 1, 1}, {}}, param);
  952. run({{2, 64, 60, 34}, {16, 64, 1, 1}, {}}, param);
  953. }
  954. }
  955. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_DIRECT) {
  956. using Param = param::Convolution;
  957. Benchmarker<Convolution> benchmarker_float(handle());
  958. Benchmarker<Convolution> benchmarker_half(handle());
  959. const size_t RUNS = 10;
  960. benchmarker_float.set_display(false)
  961. .set_times(RUNS)
  962. .set_dtype(0, dtype::Float32{})
  963. .set_dtype(1, dtype::Float32{})
  964. .set_dtype(2, dtype::Float32{})
  965. .set_before_exec_callback(
  966. AlgoChecker<Convolution>("CONVOLUTION_DEFAULT_F32DIRECT"));
  967. benchmarker_half.set_display(false)
  968. .set_times(RUNS)
  969. .set_dtype(0, dtype::Float16{})
  970. .set_dtype(1, dtype::Float16{})
  971. .set_dtype(2, dtype::Float16{})
  972. .set_before_exec_callback(
  973. AlgoChecker<Convolution>("CONVOLUTION_DEFAULT_F16DIRECT"));
  974. auto run = [&](const TensorShapeArray& shapes, Param param) {
  975. auto tfloat = benchmarker_float.set_param(param).exec(shapes) / RUNS;
  976. auto thalf = benchmarker_half.set_param(param).exec(shapes) / RUNS;
  977. TensorLayout dst_layout;
  978. auto opr = handle()->create_operator<Convolution>();
  979. opr->param() = param;
  980. opr->deduce_layout({shapes[0], dtype::Float32()},
  981. {shapes[1], dtype::Float32()}, dst_layout);
  982. //! dst.nr_elems * IC * FH * FW * 2
  983. float computations = dst_layout.total_nr_elems() * shapes[1][1] *
  984. shapes[1][2] * shapes[1][3] * 2.0 /
  985. (1024 * 1024 * 1024);
  986. printf("run:%s %s float: %f ms %f Gflops VS half: %f ms %f Gflops "
  987. "speepup: %f\n",
  988. shapes[0].to_string().c_str(), shapes[1].to_string().c_str(),
  989. tfloat, computations / tfloat * 1e3, thalf,
  990. computations / thalf * 1e3, tfloat / thalf);
  991. };
  992. auto profile = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h,
  993. size_t kernel, size_t stride) {
  994. Param param;
  995. param.stride_h = stride;
  996. param.stride_w = stride;
  997. param.pad_h = kernel / 2;
  998. param.pad_w = kernel / 2;
  999. run({{n, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
  1000. };
  1001. for (size_t kernel : {1, 2, 3, 4, 5, 6, 7}) {
  1002. for (size_t ic : {12}) {
  1003. for (size_t oc : {4}) {
  1004. for (size_t size : {17, 28, 32, 34, 64, 112, 256}) {
  1005. profile(1, oc, ic, size, size, kernel, 1);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. for (auto k : {1, 2, 3, 4, 5, 6, 7}) {
  1011. profile(2, 12, 3, 480, 270, k, 1);
  1012. profile(2, 48, 12, 240, 135, k, 1);
  1013. profile(2, 4, 16, 240, 135, k, 1);
  1014. profile(2, 16, 4, 240, 135, k, 1);
  1015. profile(2, 8, 16, 240, 135, k, 1);
  1016. profile(2, 32, 8, 240, 135, k, 1);
  1017. profile(2, 8, 32, 120, 68, k, 1);
  1018. profile(2, 16, 64, 60, 34, k, 1);
  1019. }
  1020. }
  1021. TEST_F(ARM_COMMON, BENCHMARK_CONVOLUTION_STRIDE1) {
  1022. using Param = param::Convolution;
  1023. auto run_fp32 = [&](const TensorShapeArray& shapes, Param param) {
  1024. Benchmarker<Convolution> benchmarker_float(handle());
  1025. size_t RUN = 50;
  1026. auto tfloat =
  1027. benchmarker_float.set_display(false)
  1028. .set_dtype(0, dtype::Float32())
  1029. .set_dtype(1, dtype::Float32())
  1030. .set_dtype(2, dtype::Float32())
  1031. .set_before_exec_callback(AlgoChecker<Convolution>(
  1032. "CONVOLUTION_DEFAULT_F32STRD1"))
  1033. .set_times(RUN)
  1034. .set_param(param)
  1035. .exec(shapes);
  1036. size_t IC = shapes[1][1];
  1037. size_t FH = shapes[1][2];
  1038. size_t FW = shapes[1][3];
  1039. TensorLayout dst_layout;
  1040. auto opr = handle()->create_operator<Convolution>();
  1041. opr->param() = param;
  1042. opr->deduce_layout({shapes[0], dtype::Float32()},
  1043. {shapes[1], dtype::Float32()}, dst_layout);
  1044. printf("fp32 flops: %.3f mflops\n",
  1045. (IC * dst_layout.total_nr_elems() * FH * FW * 2) /
  1046. (tfloat / RUN * 1000));
  1047. };
  1048. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  1049. auto run_fp16 = [&](const TensorShapeArray& shapes, Param param) {
  1050. Benchmarker<Convolution> benchmarker_float(handle());
  1051. size_t RUN = 50;
  1052. auto tfloat =
  1053. benchmarker_float.set_display(false)
  1054. .set_dtype(0, dtype::Float16())
  1055. .set_dtype(1, dtype::Float16())
  1056. .set_dtype(2, dtype::Float16())
  1057. .set_before_exec_callback(AlgoChecker<Convolution>(
  1058. "CONVOLUTION_DEFAULT_F16STRD1"))
  1059. .set_times(RUN)
  1060. .set_param(param)
  1061. .exec(shapes);
  1062. size_t IC = shapes[1][1];
  1063. size_t FH = shapes[1][2];
  1064. size_t FW = shapes[1][3];
  1065. TensorLayout dst_layout;
  1066. auto opr = handle()->create_operator<Convolution>();
  1067. opr->param() = param;
  1068. opr->deduce_layout({shapes[0], dtype::Float16()},
  1069. {shapes[1], dtype::Float16()}, dst_layout);
  1070. printf("fp16 flops: %.3f mflops\n",
  1071. (IC * dst_layout.total_nr_elems() * FH * FW * 2) /
  1072. (tfloat / RUN * 1000));
  1073. };
  1074. #endif
  1075. auto profile = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  1076. size_t stride) {
  1077. Param param;
  1078. param.stride_h = stride;
  1079. param.stride_w = stride;
  1080. param.pad_h = kernel / 2;
  1081. param.pad_w = kernel / 2;
  1082. printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n",
  1083. oc, ic, w, h, stride, kernel);
  1084. run_fp32({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
  1085. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  1086. run_fp16({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
  1087. #endif
  1088. };
  1089. for (size_t kernel : {2, 3, 5}) {
  1090. for (size_t ic : {3, 6, 12, 24}) {
  1091. for (size_t oc : {3, 6, 12, 24}) {
  1092. for (size_t size : {4, 7, 8, 14, 16, 17, 28, 32, 34, 64, 112}) {
  1093. profile(oc, ic, size, size, kernel, 1);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. }
  1099. #endif
  1100. // vim: syntax=cpp.doxygen

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