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.

matrix_mul.cpp 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. * \file dnn/test/arm_common/matrix_mul.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/matrix_mul.h"
  15. #include "test/common/rng.h"
  16. using namespace megdnn;
  17. using namespace test;
  18. TEST_F(ARM_COMMON, MATRIX_MUL_INT8x8x32) {
  19. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  20. handle());
  21. }
  22. TEST_F(ARM_COMMON, MATRIX_MUL_INT8x8x16) {
  23. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  24. handle());
  25. }
  26. TEST_F(ARM_COMMON, MATRIX_MUL_QUINT8) {
  27. matrix_mul::check_matrix_mul(dtype::Quantized8Asymm(1.2f, (uint8_t)127),
  28. dtype::Quantized8Asymm(1.3f, (uint8_t)129),
  29. {},
  30. handle());
  31. }
  32. TEST_F(ARM_COMMON, MATRIX_MUL_FP32) {
  33. Checker<MatrixMul> checker(handle());
  34. using Param = MatrixMul::Param;
  35. auto run = [&](size_t M, size_t K, size_t N) {
  36. Param param;
  37. param.transposeA = false;
  38. param.transposeB = false;
  39. TensorShape A, B;
  40. A = TensorShape{M, K};
  41. B = TensorShape{K, N};
  42. checker.set_param(param)
  43. .set_dtype(0, dtype::Float32())
  44. .set_dtype(1, dtype::Float32())
  45. .set_dtype(2, dtype::Float32())
  46. .execs({A, B, {}});
  47. };
  48. checker.set_before_exec_callback(
  49. AlgoChecker<MatrixMul>("ARM_COMMON_F32_GEMV"));
  50. // M < 8
  51. for (size_t M : {1, 2, 3, 4, 5, 6, 7})
  52. for (size_t K : {7, 1024, 2048})
  53. for (size_t N : {7, 1024, 2056})
  54. run(M, K, N);
  55. // M = 8,K = 1, 2
  56. for (size_t M : {8})
  57. for (size_t K : {1, 2})
  58. for (size_t N : {7, 1024, 2056})
  59. run(M, K, N);
  60. // N = 1
  61. for (size_t M : {1, 2, 3, 4, 5, 6, 7})
  62. for (size_t K : {7, 1024, 2048})
  63. for (size_t N : {1})
  64. run(M, K, N);
  65. }
  66. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  67. TEST_F(ARM_COMMON, MATRIX_MUL_FP16) {
  68. Checker<MatrixMul> checker(handle());
  69. checker.set_epsilon(1e-2);
  70. NormalRNG rng(2.f);
  71. checker.set_rng(0, &rng).set_rng(1, &rng);
  72. using Param = MatrixMul::Param;
  73. auto args = matrix_mul::get_matmul_args_no_mask();
  74. for (auto& arg : args) {
  75. size_t m = arg.m, n = arg.n, k = arg.k;
  76. Param param;
  77. param.transposeA = false;
  78. param.transposeB = false;
  79. TensorShape A, B;
  80. A = TensorShape{m, k};
  81. B = TensorShape{k, n};
  82. checker.set_param(param)
  83. .set_dtype(0, dtype::Float16())
  84. .set_dtype(1, dtype::Float16())
  85. .set_dtype(2, dtype::Float16())
  86. .execs({A, B, {}});
  87. }
  88. }
  89. TEST_F(ARM_COMMON, MATRIX_MUL_FP16_TEST) {
  90. Checker<MatrixMul> checker(handle());
  91. using Param = MatrixMul::Param;
  92. checker.set_epsilon(1e-2);
  93. NormalRNG rng(2.f);
  94. checker.set_rng(0, &rng).set_rng(1, &rng);
  95. auto run = [&](size_t M, size_t K, size_t N) {
  96. Param param;
  97. param.transposeA = false;
  98. param.transposeB = false;
  99. TensorShape A, B;
  100. A = TensorShape{M, K};
  101. B = TensorShape{K, N};
  102. checker.set_param(param)
  103. .set_dtype(0, dtype::Float16())
  104. .set_dtype(1, dtype::Float16())
  105. .set_dtype(2, dtype::Float16())
  106. .execs({A, B, {}});
  107. };
  108. checker.set_before_exec_callback(
  109. AlgoChecker<MatrixMul>("ARM_COMMON_F16_GEMV"));
  110. // M = 1, 2, 3, 4
  111. for (size_t M : {1, 2, 3, 4})
  112. for (size_t K : {7, 512, 1024})
  113. for (size_t N : {13, 1024, 2048})
  114. run(M, K, N);
  115. // N = 1
  116. for (size_t M : {1, 2, 3, 4})
  117. for (size_t K : {7, 512, 1024})
  118. for (size_t N : {1})
  119. run(M, K, N);
  120. }
  121. #endif
  122. TEST_F(ARM_COMMON, QINT8x8x32_GEMV) {
  123. Checker<MatrixMul> checker(handle());
  124. using Param = MatrixMul::Param;
  125. checker.set_before_exec_callback(
  126. AlgoChecker<MatrixMul>("ARM_COMMON_INT8X8X32_GEMV"));
  127. std::unique_ptr<RNG> rng = std::make_unique<UniformIntRNG>(-127, 127);
  128. checker.set_rng(0, rng.get()).set_rng(1, rng.get());
  129. auto run = [&](size_t M, size_t K, size_t N) {
  130. Param param;
  131. param.transposeA = false;
  132. param.transposeB = false;
  133. TensorShape A, B;
  134. A = TensorShape{M, K};
  135. B = TensorShape{K, N};
  136. checker.set_param(param)
  137. .set_dtype(0, dtype::QuantizedS8(2.5f))
  138. .set_dtype(1, dtype::QuantizedS8(2.5f))
  139. .set_dtype(2, dtype::QuantizedS32(6.25f))
  140. .execs({A, B, {}});
  141. };
  142. // N = 1
  143. for (size_t M : {1, 10, 16, 33, 64})
  144. for (size_t K : {7, 512, 1024})
  145. for (size_t N : {1})
  146. run(M, K, N);
  147. }
  148. #if MEGDNN_WITH_BENCHMARK
  149. TEST_F(ARM_COMMON, BENCHMARK_SGEMV) {
  150. int exec_times = 10;
  151. Benchmarker<MatrixMul> benchmarker(handle());
  152. benchmarker.set_times(exec_times);
  153. auto run = [&](size_t M, size_t K, size_t N) {
  154. std::cout << "SGEMV: (" << M << ", " << K << ", " << N << ")"
  155. << std::endl;
  156. benchmarker.set_dtype(0, dtype::Float32())
  157. .set_dtype(1, dtype::Float32());
  158. auto time = benchmarker.exec({{M, K}, {K, N}, {}}) / exec_times;
  159. auto computations = 2.f * M * K * N * 1e-6;
  160. auto perf = computations / time;
  161. std::cout << "gemv fp32, Performance is " << perf << " Gflops"
  162. << std::endl;
  163. };
  164. std::cout << "warm up:\n";
  165. for (int i = 0; i < 50; i++) {
  166. benchmarker.set_dtype(0, dtype::Float32())
  167. .set_dtype(1, dtype::Float32())
  168. .set_display(false)
  169. .exec({{2, 1024}, {1024, 512}, {}});
  170. benchmarker.set_display(true);
  171. }
  172. // run gemv
  173. for (size_t M : {1, 2, 4, 8})
  174. for (size_t K : {1024, 1536, 2048})
  175. for (size_t N : {512, 1024})
  176. run(M, K, N);
  177. }
  178. TEST_F(ARM_COMMON, BENCHMARK_SGEMV_FP16) {
  179. int exec_times = 50;
  180. Benchmarker<MatrixMul> benchmarker(handle());
  181. benchmarker.set_times(exec_times);
  182. benchmarker.set_before_exec_callback(
  183. AlgoChecker<MatrixMul>("ARM_COMMON_F16_GEMV"));
  184. auto run = [&](size_t M, size_t K, size_t N) {
  185. std::cout << "SGEMV: (" << M << ", " << K << ", " << N << ")"
  186. << std::endl;
  187. benchmarker.set_dtype(0, dtype::Float16())
  188. .set_dtype(1, dtype::Float16())
  189. .set_dtype(2, dtype::Float16());
  190. auto time = benchmarker.exec({{M, K}, {K, N}, {}}) / exec_times;
  191. auto computations = 2 * M * K * N * 1e-6;
  192. auto perf = computations / time;
  193. std::cout << "gemv fp16, Performance is " << perf << " Gflops"
  194. << std::endl;
  195. };
  196. std::cout << "warm up:\n";
  197. for (int i = 0; i < 50; i++) {
  198. benchmarker.set_dtype(0, dtype::Float16())
  199. .set_dtype(1, dtype::Float16())
  200. .set_dtype(2, dtype::Float16())
  201. .set_display(false)
  202. .exec({{2, 1024}, {1024, 512}, {}});
  203. benchmarker.set_display(true);
  204. }
  205. // run gemv
  206. for (size_t M : {1, 2, 3, 4})
  207. for (size_t K : {1024, 1536, 2048})
  208. for (size_t N : {512, 1024})
  209. run(M, K, N);
  210. }
  211. TEST_F(ARM_COMMON, BENCHMARK_SGEMM) {
  212. int exec_times = 10;
  213. Benchmarker<MatrixMul> benchmarker(handle());
  214. benchmarker.set_times(exec_times);
  215. float mod = 1000 * exec_times / 1e9;
  216. auto run = [&](size_t M, size_t K, size_t N) {
  217. float time = 1.f, perf = 1.f;
  218. std::cout << "SGEMM: (" << M << ", " << K << ", " << N << ")"
  219. << std::endl;
  220. benchmarker.set_dtype(0, dtype::Float32())
  221. .set_dtype(1, dtype::Float32());
  222. time = benchmarker.exec({{M, K}, {K, N}, {}});
  223. perf = 2.f * M * K * N / time * mod;
  224. std::cout << "gemm fp32, Performance is " << perf << " Gflops"
  225. << std::endl;
  226. };
  227. std::cout << "warm up:\n";
  228. for (int i = 0; i < 50; i++) {
  229. benchmarker.set_dtype(0, dtype::Float32())
  230. .set_dtype(1, dtype::Float32())
  231. .set_display(false)
  232. .exec({{2, 1024}, {1024, 512}, {}});
  233. benchmarker.set_display(true);
  234. }
  235. run(256, 12 * 24, 256);
  236. //////////////////////// gemv //////////////////////////
  237. for (size_t M : {8, 64, 112, 256}) {
  238. for (size_t K : {8, 64, 112, 256}) {
  239. run (M, 1, K);
  240. }
  241. }
  242. //////////////////////// gemm //////////////////////////
  243. for (size_t M : {8, 64, 112, 256}) {
  244. for (size_t K : {8, 16, 32, 64, 112, 256}) {
  245. for (size_t N : {8, 64, 112, 256}) {
  246. run(M, N, K);
  247. }
  248. }
  249. }
  250. }
  251. TEST_F(ARM_COMMON, BENCHMARK_MATRIX_MUL_INT8x8x32) {
  252. constexpr size_t RUNS = 50;
  253. param::MatrixMul param;
  254. Benchmarker<MatrixMul> benchmarker_int(handle());
  255. benchmarker_int.set_times(RUNS)
  256. .set_dtype(0, dtype::Int8{})
  257. .set_dtype(1, dtype::Int8{})
  258. .set_dtype(2, dtype::Int32{})
  259. .set_param(param).set_display(false);
  260. Benchmarker<MatrixMul> benchmarker_float(handle());
  261. benchmarker_float.set_display(false).set_times(RUNS);
  262. auto run = [&](size_t M, size_t N, size_t K) {
  263. auto int_used = benchmarker_int.exec({{M, K}, {K, N}, {}}) / RUNS;
  264. auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
  265. float computations = 2.f * M * K * N * 1e-6;
  266. printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops int: %f ms "
  267. "%f Gflops speedup: %f\n",
  268. M, K, N, float_used, computations / float_used, int_used,
  269. computations / int_used, float_used / int_used);
  270. };
  271. run(256, 12 * 24, 256);
  272. //////////////////////// gemv //////////////////////////
  273. for (size_t M : {8, 64, 112, 256}) {
  274. for (size_t K : {8, 64, 112, 256}) {
  275. run (M, 1, K);
  276. }
  277. }
  278. //////////////////////// gemm //////////////////////////
  279. for (size_t M : {8, 64, 112, 256}) {
  280. for (size_t K : {8, 16, 32, 64, 112, 256}) {
  281. for (size_t N : {8, 64, 112, 256}) {
  282. run(M, N, K);
  283. }
  284. }
  285. }
  286. }
  287. TEST_F(ARM_COMMON, BENCHMARK_MATRIX_MUL_QUINT8) {
  288. constexpr size_t RUNS = 50;
  289. param::MatrixMul param;
  290. Benchmarker<MatrixMul> benchmarker_int(handle());
  291. benchmarker_int.set_times(RUNS)
  292. .set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)127))
  293. .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)129))
  294. .set_dtype(2, {})
  295. .set_param(param)
  296. .set_display(false);
  297. Benchmarker<MatrixMul> benchmarker_float(handle());
  298. benchmarker_float.set_display(false).set_times(RUNS);
  299. auto run = [&](size_t M, size_t N, size_t K) {
  300. auto int_used = benchmarker_int.exec({{M, K}, {K, N}, {}}) / RUNS;
  301. auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
  302. float computations = 2.f * M * K * N * 1e-6;
  303. printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops int: %f ms "
  304. "%f Gflops speedup: %f\n",
  305. M, K, N, float_used, computations / float_used, int_used,
  306. computations / int_used, float_used / int_used);
  307. };
  308. run(256, 12 * 24, 256);
  309. for (size_t M : {8, 64, 112, 256}) {
  310. for (size_t K : {8, 64, 112, 256}) {
  311. for (size_t N : {8, 64, 112, 256}) {
  312. run(M, N, K);
  313. }
  314. }
  315. }
  316. }
  317. TEST_F(ARM_COMMON, BENCHMARK_TRANSPOSED_MATRIX_MUL_QUINT8) {
  318. constexpr size_t RUNS = 50;
  319. param::MatrixMul param;
  320. param.transposeA = param.transposeB = true;
  321. Benchmarker<MatrixMul> benchmarker_int(handle());
  322. benchmarker_int.set_times(RUNS)
  323. .set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)127))
  324. .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)129))
  325. .set_dtype(2, {})
  326. .set_param(param)
  327. .set_display(false);
  328. Benchmarker<MatrixMul> benchmarker_float(handle());
  329. benchmarker_float.set_param(param).set_display(false).set_times(RUNS);
  330. auto run = [&](size_t M, size_t N, size_t K) {
  331. auto int_used = benchmarker_int.exec({{K, M}, {N, K}, {}}) / RUNS;
  332. auto float_used = benchmarker_float.exec({{K, M}, {N, K}, {}}) / RUNS;
  333. float computations = 2.f * M * K * N * 1e-6;
  334. printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops int: %f ms "
  335. "%f Gflops speedup: %f\n",
  336. M, K, N, float_used, computations / float_used, int_used,
  337. computations / int_used, float_used / int_used);
  338. };
  339. run(256, 12 * 24, 256);
  340. for (size_t M : {8, 64, 112, 256}) {
  341. for (size_t K : {8, 64, 112, 256}) {
  342. for (size_t N : {8, 64, 112, 256}) {
  343. run(M, N, K);
  344. }
  345. }
  346. }
  347. }
  348. #endif
  349. // vim: syntax=cpp.doxygen

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