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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * \file dnn/test/x86/matrix_mul.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #include "test/x86/fixture.h"
  13. #include "src/x86/utils.h"
  14. #include "test/common/benchmarker.h"
  15. #include "test/common/checker.h"
  16. #include "test/common/matrix_mul.h"
  17. #include "test/common/rng.h"
  18. using namespace megdnn;
  19. using namespace test;
  20. using namespace megdnn::x86;
  21. #if MEGDNN_X86_WITH_VNNI
  22. TEST_F(X86, MATRIX_MUL_VNNI_8X8X32) {
  23. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  24. handle(), "X86_INT8X8X32_VNNI");
  25. }
  26. #endif
  27. #if MEGDNN_X86_WITH_MKL_DNN
  28. TEST_F(X86, MATRIX_MUL_MKLDNN_8X8X32) {
  29. if (is_supported(SIMDType::VNNI)) {
  30. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{},
  31. dtype::Int32{}, handle(),
  32. "X86_INT8X8X32_MKLDNN");
  33. } else {
  34. std::cout << "can not do mkldnn matmul check for no vnni support"
  35. << std::endl;
  36. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{},
  37. dtype::Int32{}, handle());
  38. }
  39. }
  40. #endif
  41. //! FIXME: need to add tests of GEMV and QUINT8
  42. TEST_F(X86, MATRIX_MUL_AVX2_8X8X32) {
  43. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  44. handle(), "X86_INT8X8X32_AVX2_2X4X16",
  45. param::MatrixMul::Format::DEFAULT, 8, 1e-3,
  46. false);
  47. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  48. handle(), "X86_INT8X8X32_AVX2_4X16X2",
  49. param::MatrixMul::Format::DEFAULT, 8, 1e-3,
  50. false);
  51. }
  52. TEST_F(X86, MATRIX_MUL_AVX2_8X8X16) {
  53. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  54. handle(), "X86_INT8X8X16_AVX2",
  55. param::MatrixMul::Format::DEFAULT, 8, 1e-3,
  56. false);
  57. }
  58. TEST_F(X86, MATRIX_MUL_SSE_8X8X16) {
  59. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  60. handle(), "X86_INT8X8X16_SSE",
  61. param::MatrixMul::Format::DEFAULT, 8, 1e-3,
  62. false);
  63. }
  64. TEST_F(X86, MATRIX_MUL_SSE_8X8X32) {
  65. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  66. handle(), "X86_INT8X8X32_SSE_4X8X2",
  67. param::MatrixMul::Format::DEFAULT, 8, 1e-3,
  68. false);
  69. }
  70. #if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
  71. TEST_F(X86, MATRIX_MUL_MKL_PACKA) {
  72. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  73. dtype::Float32{}, handle(),
  74. "X86_F32_MKL_PACKA");
  75. }
  76. #endif
  77. TEST_F(X86, MATRIX_MUL_AVX2_MK8_8X8) {
  78. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  79. dtype::Float32{}, handle(), "X86_F32MK8_8X8",
  80. param::MatrixMul::Format::MK8, 1, 1e-3, false);
  81. }
  82. TEST_F(X86, MATRIX_MUL_AVX2_6x16) {
  83. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  84. dtype::Float32{}, handle(), "X86_F32_6x16",
  85. param::MatrixMul::Format::DEFAULT, 1, 1e-3, false);
  86. }
  87. #if MEGDNN_WITH_BENCHMARK
  88. TEST_F(X86, BENCHMARK_MATRIX_MUL_AVX2_MK8_8X8) {
  89. auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
  90. matrix_mul::benchmark_with_contrast(
  91. handle(), args, dtype::Float32{}, dtype::Float32{},
  92. dtype::Float32{}, "X86_F32MK8_8X8", param::MatrixMul::Format::MK8,
  93. dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
  94. "X86_F32_BLAS");
  95. }
  96. TEST_F(X86, BENCHMARK_MATRIX_MUL_AVX2_6x16) {
  97. auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
  98. matrix_mul::benchmark_with_contrast(
  99. handle(), args, dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
  100. "X86_F32_6x16", param::MatrixMul::Format::DEFAULT, dtype::Float32{},
  101. dtype::Float32{}, dtype::Float32{},"X86_F32_BLAS");
  102. }
  103. TEST_F(X86, BENCHMARK_MATRIX_MUL_8X8X32) {
  104. constexpr size_t RUNS = 50;
  105. auto rng = std::make_unique<UniformIntRNG>(-127, 127);
  106. #if MEGDNN_X86_WITH_VNNI
  107. Benchmarker<MatrixMul> benchmarker_vnni(handle());
  108. benchmarker_vnni.set_times(RUNS)
  109. .set_dtype(0, dtype::Int8{})
  110. .set_dtype(1, dtype::Int8{})
  111. .set_dtype(2, dtype::Int32{})
  112. .set_display(false)
  113. .set_rng(0, rng.get())
  114. .set_rng(1, rng.get());
  115. benchmarker_vnni.set_before_exec_callback(
  116. AlgoChecker<MatrixMul>("X86_INT8X8X32_VNNI"));
  117. #endif
  118. #if MEGDNN_X86_WITH_MKL_DNN
  119. Benchmarker<MatrixMul> benchmarker_mkldnn(handle());
  120. benchmarker_mkldnn.set_times(RUNS)
  121. .set_dtype(0, dtype::Int8{})
  122. .set_dtype(1, dtype::Int8{})
  123. .set_dtype(2, dtype::Int32{})
  124. .set_display(false)
  125. .set_rng(0, rng.get())
  126. .set_rng(1, rng.get());
  127. benchmarker_mkldnn.set_before_exec_callback(
  128. AlgoChecker<MatrixMul>("X86_INT8X8X32_MKLDNN"));
  129. #endif
  130. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2(handle());
  131. benchmarker_avx2_4x16x2.set_display(false)
  132. .set_times(RUNS)
  133. .set_dtype(0, dtype::Int8{})
  134. .set_dtype(1, dtype::Int8{})
  135. .set_dtype(2, dtype::Int32{})
  136. .set_rng(0, rng.get())
  137. .set_rng(1, rng.get());
  138. benchmarker_avx2_4x16x2.set_before_exec_callback(
  139. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_4X16X2"));
  140. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2_8816(handle());
  141. benchmarker_avx2_4x16x2_8816.set_display(false)
  142. .set_times(RUNS)
  143. .set_dtype(0, dtype::Int8{})
  144. .set_dtype(1, dtype::Int8{})
  145. .set_dtype(2, dtype::Int16{})
  146. .set_rng(0, rng.get())
  147. .set_rng(1, rng.get());
  148. benchmarker_avx2_4x16x2_8816.set_before_exec_callback(
  149. AlgoChecker<MatrixMul>("X86_INT8X8X16_AVX2"));
  150. Benchmarker<MatrixMul> benchmarker_sse_4x8x2_8816(handle());
  151. benchmarker_sse_4x8x2_8816.set_display(false)
  152. .set_times(RUNS)
  153. .set_dtype(0, dtype::Int8{})
  154. .set_dtype(1, dtype::Int8{})
  155. .set_dtype(2, dtype::Int16{})
  156. .set_rng(0, rng.get())
  157. .set_rng(1, rng.get());
  158. benchmarker_sse_4x8x2_8816.set_before_exec_callback(
  159. AlgoChecker<MatrixMul>("X86_INT8X8X16_SSE"));
  160. Benchmarker<MatrixMul> benchmarker_avx2_2x4x16(handle());
  161. benchmarker_avx2_2x4x16.set_display(false)
  162. .set_times(RUNS)
  163. .set_dtype(0, dtype::Int8{})
  164. .set_dtype(1, dtype::Int8{})
  165. .set_dtype(2, dtype::Int32{})
  166. .set_rng(0, rng.get())
  167. .set_rng(1, rng.get());
  168. benchmarker_avx2_2x4x16.set_before_exec_callback(
  169. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_2X4X16"));
  170. Benchmarker<MatrixMul> benchmarker_sse_4x8x2(handle());
  171. benchmarker_sse_4x8x2.set_display(false)
  172. .set_times(RUNS)
  173. .set_dtype(0, dtype::Int8{})
  174. .set_dtype(1, dtype::Int8{})
  175. .set_dtype(2, dtype::Int32{})
  176. .set_rng(0, rng.get())
  177. .set_rng(1, rng.get());
  178. benchmarker_sse_4x8x2.set_before_exec_callback(
  179. AlgoChecker<MatrixMul>("X86_INT8X8X32_SSE_4X8X2"));
  180. Benchmarker<MatrixMul> benchmarker_float(handle());
  181. benchmarker_float.set_display(false)
  182. .set_times(RUNS)
  183. .set_rng(0, rng.get())
  184. .set_rng(1, rng.get());
  185. benchmarker_float.set_before_exec_callback(
  186. AlgoChecker<MatrixMul>("X86_F32_BLAS"));
  187. auto run = [&](size_t M, size_t N, size_t K) {
  188. const float computations = 2.f * M * K * N * 1e-6;
  189. std::cout << "run : {" << M << "," << N << "," << K << "} ";
  190. auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
  191. std::cout << "float: " << float_used << " ms, "
  192. << computations / float_used << " Gflops, ";
  193. #if MEGDNN_X86_WITH_VNNI
  194. if (is_supported(SIMDType::VNNI)) {
  195. auto vnni_used = benchmarker_vnni.exec({{M, K}, {K, N}, {}}) / RUNS;
  196. std::cout << "vnni: " << vnni_used << " ms, "
  197. << computations / vnni_used << " Gflops, "
  198. << "speed_up " << float_used / vnni_used << ", ";
  199. }
  200. #endif
  201. #if MEGDNN_X86_WITH_MKL_DNN
  202. if (is_supported(SIMDType::VNNI)) {
  203. auto mkldnn_used =
  204. benchmarker_mkldnn.exec({{M, K}, {K, N}, {}}) / RUNS;
  205. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  206. << computations / mkldnn_used << " Gflops, "
  207. << "speed_up " << float_used / mkldnn_used << ", ";
  208. }
  209. #endif
  210. if (is_supported(SIMDType::AVX2)) {
  211. auto avx2_used_4x16x2 =
  212. benchmarker_avx2_4x16x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  213. auto avx2_used_2x4x16 =
  214. benchmarker_avx2_2x4x16.exec({{M, K}, {K, N}, {}}) / RUNS;
  215. std::cout << "avx2_k2: " << avx2_used_4x16x2
  216. << " ms, k2 throughput "
  217. << computations / avx2_used_4x16x2 << " Gflops, "
  218. << "k2_speed_up " << float_used / avx2_used_4x16x2
  219. << ", k16_speed_up " << float_used / avx2_used_2x4x16
  220. << ",";
  221. auto avx2_used_4x16x2_8816 =
  222. benchmarker_avx2_4x16x2_8816.exec({{M, K}, {K, N}, {}}) /
  223. RUNS;
  224. std::cout << "avx2_8816: " << avx2_used_4x16x2_8816
  225. << " ms, 8816 throughput "
  226. << computations / avx2_used_4x16x2_8816 << " Gflops,";
  227. }
  228. if (is_supported(SIMDType::SSE4_1)) {
  229. auto sse_used =
  230. benchmarker_sse_4x8x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  231. std::cout << "sse: " << sse_used << " ms, "
  232. << computations / sse_used << " Gflops, "
  233. << "speed_up " << float_used / sse_used << ", ";
  234. auto sse_used_8816 =
  235. benchmarker_sse_4x8x2_8816.exec({{M, K}, {K, N}, {}}) /
  236. RUNS;
  237. std::cout << "sse_8816: " << sse_used_8816 << " ms, "
  238. << computations / sse_used_8816 << " Gflops, ";
  239. }
  240. std::cout << std::endl;
  241. };
  242. run(256, 256, 256);
  243. for (size_t M : {8, 64, 112, 256, 512}) {
  244. for (size_t K : {8, 16, 32, 64, 112, 256, 512}) {
  245. for (size_t N : {8, 64, 112, 256, 512}) {
  246. run(M, N, K);
  247. }
  248. }
  249. }
  250. }
  251. #endif // MEGDNN_WITH_BENCHMARK
  252. // vim: syntax=cpp.doxygen

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