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

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