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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #if MEGDNN_WITH_BENCHMARK
  83. TEST_F(X86, BENCHMARK_MATRIX_MUL_AVX2_MK8_8X8) {
  84. auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
  85. matrix_mul::benchmark_with_contrast(
  86. handle(), args, dtype::Float32{}, dtype::Float32{},
  87. dtype::Float32{}, "X86_F32MK8_8X8", param::MatrixMul::Format::MK8,
  88. dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
  89. "X86_F32_BLAS");
  90. }
  91. TEST_F(X86, BENCHMARK_MATRIX_MUL_8X8X32) {
  92. constexpr size_t RUNS = 50;
  93. auto rng = std::make_unique<UniformIntRNG>(-127, 127);
  94. #if MEGDNN_X86_WITH_VNNI
  95. Benchmarker<MatrixMul> benchmarker_vnni(handle());
  96. benchmarker_vnni.set_times(RUNS)
  97. .set_dtype(0, dtype::Int8{})
  98. .set_dtype(1, dtype::Int8{})
  99. .set_dtype(2, dtype::Int32{})
  100. .set_display(false)
  101. .set_rng(0, rng.get())
  102. .set_rng(1, rng.get());
  103. benchmarker_vnni.set_before_exec_callback(
  104. AlgoChecker<MatrixMul>("X86_INT8X8X32_VNNI"));
  105. #endif
  106. #if MEGDNN_X86_WITH_MKL_DNN
  107. Benchmarker<MatrixMul> benchmarker_mkldnn(handle());
  108. benchmarker_mkldnn.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_mkldnn.set_before_exec_callback(
  116. AlgoChecker<MatrixMul>("X86_INT8X8X32_MKLDNN"));
  117. #endif
  118. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2(handle());
  119. benchmarker_avx2_4x16x2.set_display(false)
  120. .set_times(RUNS)
  121. .set_dtype(0, dtype::Int8{})
  122. .set_dtype(1, dtype::Int8{})
  123. .set_dtype(2, dtype::Int32{})
  124. .set_rng(0, rng.get())
  125. .set_rng(1, rng.get());
  126. benchmarker_avx2_4x16x2.set_before_exec_callback(
  127. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_4X16X2"));
  128. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2_8816(handle());
  129. benchmarker_avx2_4x16x2_8816.set_display(false)
  130. .set_times(RUNS)
  131. .set_dtype(0, dtype::Int8{})
  132. .set_dtype(1, dtype::Int8{})
  133. .set_dtype(2, dtype::Int16{})
  134. .set_rng(0, rng.get())
  135. .set_rng(1, rng.get());
  136. benchmarker_avx2_4x16x2_8816.set_before_exec_callback(
  137. AlgoChecker<MatrixMul>("X86_INT8X8X16_AVX2"));
  138. Benchmarker<MatrixMul> benchmarker_sse_4x8x2_8816(handle());
  139. benchmarker_sse_4x8x2_8816.set_display(false)
  140. .set_times(RUNS)
  141. .set_dtype(0, dtype::Int8{})
  142. .set_dtype(1, dtype::Int8{})
  143. .set_dtype(2, dtype::Int16{})
  144. .set_rng(0, rng.get())
  145. .set_rng(1, rng.get());
  146. benchmarker_sse_4x8x2_8816.set_before_exec_callback(
  147. AlgoChecker<MatrixMul>("X86_INT8X8X16_SSE"));
  148. Benchmarker<MatrixMul> benchmarker_avx2_2x4x16(handle());
  149. benchmarker_avx2_2x4x16.set_display(false)
  150. .set_times(RUNS)
  151. .set_dtype(0, dtype::Int8{})
  152. .set_dtype(1, dtype::Int8{})
  153. .set_dtype(2, dtype::Int32{})
  154. .set_rng(0, rng.get())
  155. .set_rng(1, rng.get());
  156. benchmarker_avx2_2x4x16.set_before_exec_callback(
  157. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_2X4X16"));
  158. Benchmarker<MatrixMul> benchmarker_sse_4x8x2(handle());
  159. benchmarker_sse_4x8x2.set_display(false)
  160. .set_times(RUNS)
  161. .set_dtype(0, dtype::Int8{})
  162. .set_dtype(1, dtype::Int8{})
  163. .set_dtype(2, dtype::Int32{})
  164. .set_rng(0, rng.get())
  165. .set_rng(1, rng.get());
  166. benchmarker_sse_4x8x2.set_before_exec_callback(
  167. AlgoChecker<MatrixMul>("X86_INT8X8X32_SSE_4X8X2"));
  168. Benchmarker<MatrixMul> benchmarker_float(handle());
  169. benchmarker_float.set_display(false)
  170. .set_times(RUNS)
  171. .set_rng(0, rng.get())
  172. .set_rng(1, rng.get());
  173. benchmarker_float.set_before_exec_callback(
  174. AlgoChecker<MatrixMul>("X86_F32_BLAS"));
  175. auto run = [&](size_t M, size_t N, size_t K) {
  176. const float computations = 2.f * M * K * N * 1e-6;
  177. std::cout << "run : {" << M << "," << N << "," << K << "} ";
  178. auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
  179. std::cout << "float: " << float_used << " ms, "
  180. << computations / float_used << " Gflops, ";
  181. #if MEGDNN_X86_WITH_VNNI
  182. if (is_supported(SIMDType::VNNI)) {
  183. auto vnni_used = benchmarker_vnni.exec({{M, K}, {K, N}, {}}) / RUNS;
  184. std::cout << "vnni: " << vnni_used << " ms, "
  185. << computations / vnni_used << " Gflops, "
  186. << "speed_up " << float_used / vnni_used << ", ";
  187. }
  188. #endif
  189. #if MEGDNN_X86_WITH_MKL_DNN
  190. if (is_supported(SIMDType::VNNI)) {
  191. auto mkldnn_used =
  192. benchmarker_mkldnn.exec({{M, K}, {K, N}, {}}) / RUNS;
  193. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  194. << computations / mkldnn_used << " Gflops, "
  195. << "speed_up " << float_used / mkldnn_used << ", ";
  196. }
  197. #endif
  198. if (is_supported(SIMDType::AVX2)) {
  199. auto avx2_used_4x16x2 =
  200. benchmarker_avx2_4x16x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  201. auto avx2_used_2x4x16 =
  202. benchmarker_avx2_2x4x16.exec({{M, K}, {K, N}, {}}) / RUNS;
  203. std::cout << "avx2_k2: " << avx2_used_4x16x2
  204. << " ms, k2 throughput "
  205. << computations / avx2_used_4x16x2 << " Gflops, "
  206. << "k2_speed_up " << float_used / avx2_used_4x16x2
  207. << ", k16_speed_up " << float_used / avx2_used_2x4x16
  208. << ",";
  209. auto avx2_used_4x16x2_8816 =
  210. benchmarker_avx2_4x16x2_8816.exec({{M, K}, {K, N}, {}}) /
  211. RUNS;
  212. std::cout << "avx2_8816: " << avx2_used_4x16x2_8816
  213. << " ms, 8816 throughput "
  214. << computations / avx2_used_4x16x2_8816 << " Gflops,";
  215. }
  216. if (is_supported(SIMDType::SSE4_1)) {
  217. auto sse_used =
  218. benchmarker_sse_4x8x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  219. std::cout << "sse: " << sse_used << " ms, "
  220. << computations / sse_used << " Gflops, "
  221. << "speed_up " << float_used / sse_used << ", ";
  222. auto sse_used_8816 =
  223. benchmarker_sse_4x8x2_8816.exec({{M, K}, {K, N}, {}}) /
  224. RUNS;
  225. std::cout << "sse_8816: " << sse_used_8816 << " ms, "
  226. << computations / sse_used_8816 << " Gflops, ";
  227. }
  228. std::cout << std::endl;
  229. };
  230. run(256, 256, 256);
  231. for (size_t M : {8, 64, 112, 256, 512}) {
  232. for (size_t K : {8, 16, 32, 64, 112, 256, 512}) {
  233. for (size_t N : {8, 64, 112, 256, 512}) {
  234. run(M, N, K);
  235. }
  236. }
  237. }
  238. }
  239. #endif // MEGDNN_WITH_BENCHMARK
  240. // vim: syntax=cpp.doxygen

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