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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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-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
  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. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  46. handle(), "X86_INT8X8X32_AVX2_4X16X2");
  47. }
  48. TEST_F(X86, MATRIX_MUL_AVX2_8X8X16) {
  49. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
  50. handle(), "X86_INT8X8X16_AVX2");
  51. }
  52. TEST_F(X86, MATRIX_MUL_SSE_8X8X32) {
  53. matrix_mul::check_matrix_mul(dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
  54. handle(), "X86_INT8X8X32_SSE_4X8X2");
  55. }
  56. #if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
  57. TEST_F(X86, MATRIX_MUL_MKL_PACKA) {
  58. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  59. dtype::Float32{}, handle(),
  60. "X86_F32_MKL_PACKA");
  61. }
  62. #endif
  63. TEST_F(X86, MATRIX_MUL_AVX2_MK8_8X8) {
  64. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  65. dtype::Float32{}, handle(), "X86_F32MK8_8X8",
  66. param::MatrixMul::Format::MK8, 1);
  67. }
  68. #if MEGDNN_WITH_BENCHMARK
  69. TEST_F(X86, BENCHMARK_MATRIX_MUL_AVX2_MK8_8X8) {
  70. auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
  71. matrix_mul::benchmark_with_contrast(
  72. handle(), args, dtype::Float32{}, dtype::Float32{},
  73. dtype::Float32{}, "X86_F32MK8_8X8", param::MatrixMul::Format::MK8,
  74. dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
  75. "X86_F32_BLAS");
  76. }
  77. TEST_F(X86, BENCHMARK_MATRIX_MUL_8X8X32) {
  78. constexpr size_t RUNS = 50;
  79. auto rng = std::make_unique<UniformIntRNG>(-127, 127);
  80. #if MEGDNN_X86_WITH_VNNI
  81. Benchmarker<MatrixMul> benchmarker_vnni(handle());
  82. benchmarker_vnni.set_times(RUNS)
  83. .set_dtype(0, dtype::Int8{})
  84. .set_dtype(1, dtype::Int8{})
  85. .set_dtype(2, dtype::Int32{})
  86. .set_display(false)
  87. .set_rng(0, rng.get())
  88. .set_rng(1, rng.get());
  89. benchmarker_vnni.set_before_exec_callback(
  90. AlgoChecker<MatrixMul>("X86_INT8X8X32_VNNI"));
  91. #endif
  92. #if MEGDNN_X86_WITH_MKL_DNN
  93. Benchmarker<MatrixMul> benchmarker_mkldnn(handle());
  94. benchmarker_mkldnn.set_times(RUNS)
  95. .set_dtype(0, dtype::Int8{})
  96. .set_dtype(1, dtype::Int8{})
  97. .set_dtype(2, dtype::Int32{})
  98. .set_display(false)
  99. .set_rng(0, rng.get())
  100. .set_rng(1, rng.get());
  101. benchmarker_mkldnn.set_before_exec_callback(
  102. AlgoChecker<MatrixMul>("X86_INT8X8X32_MKLDNN"));
  103. #endif
  104. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2(handle());
  105. benchmarker_avx2_4x16x2.set_display(false)
  106. .set_times(RUNS)
  107. .set_dtype(0, dtype::Int8{})
  108. .set_dtype(1, dtype::Int8{})
  109. .set_dtype(2, dtype::Int32{})
  110. .set_rng(0, rng.get())
  111. .set_rng(1, rng.get());
  112. benchmarker_avx2_4x16x2.set_before_exec_callback(
  113. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_4X16X2"));
  114. Benchmarker<MatrixMul> benchmarker_avx2_4x16x2_8816(handle());
  115. benchmarker_avx2_4x16x2_8816.set_display(false)
  116. .set_times(RUNS)
  117. .set_dtype(0, dtype::Int8{})
  118. .set_dtype(1, dtype::Int8{})
  119. .set_dtype(2, dtype::Int16{})
  120. .set_rng(0, rng.get())
  121. .set_rng(1, rng.get());
  122. benchmarker_avx2_4x16x2_8816.set_before_exec_callback(
  123. AlgoChecker<MatrixMul>("X86_INT8X8X16_AVX2"));
  124. Benchmarker<MatrixMul> benchmarker_avx2_2x4x16(handle());
  125. benchmarker_avx2_2x4x16.set_display(false)
  126. .set_times(RUNS)
  127. .set_dtype(0, dtype::Int8{})
  128. .set_dtype(1, dtype::Int8{})
  129. .set_dtype(2, dtype::Int32{})
  130. .set_rng(0, rng.get())
  131. .set_rng(1, rng.get());
  132. benchmarker_avx2_2x4x16.set_before_exec_callback(
  133. AlgoChecker<MatrixMul>("X86_INT8X8X32_AVX2_2X4X16"));
  134. Benchmarker<MatrixMul> benchmarker_sse_4x8x2(handle());
  135. benchmarker_sse_4x8x2.set_display(false)
  136. .set_times(RUNS)
  137. .set_dtype(0, dtype::Int8{})
  138. .set_dtype(1, dtype::Int8{})
  139. .set_dtype(2, dtype::Int32{})
  140. .set_rng(0, rng.get())
  141. .set_rng(1, rng.get());
  142. benchmarker_sse_4x8x2.set_before_exec_callback(
  143. AlgoChecker<MatrixMul>("X86_INT8X8X32_SSE_4X8X2"));
  144. Benchmarker<MatrixMul> benchmarker_float(handle());
  145. benchmarker_float.set_display(false)
  146. .set_times(RUNS)
  147. .set_rng(0, rng.get())
  148. .set_rng(1, rng.get());
  149. benchmarker_float.set_before_exec_callback(
  150. AlgoChecker<MatrixMul>("X86_F32_BLAS"));
  151. auto run = [&](size_t M, size_t N, size_t K) {
  152. const float computations = 2.f * M * K * N * 1e-6;
  153. std::cout << "run : {" << M << "," << N << "," << K << "} ";
  154. auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
  155. std::cout << "float: " << float_used << " ms, "
  156. << computations / float_used << " Gflops, ";
  157. #if MEGDNN_X86_WITH_VNNI
  158. if (is_supported(SIMDType::VNNI)) {
  159. auto vnni_used = benchmarker_vnni.exec({{M, K}, {K, N}, {}}) / RUNS;
  160. std::cout << "vnni: " << vnni_used << " ms, "
  161. << computations / vnni_used << " Gflops, "
  162. << "speed_up " << float_used / vnni_used << ", ";
  163. }
  164. #endif
  165. #if MEGDNN_X86_WITH_MKL_DNN
  166. if (is_supported(SIMDType::VNNI)) {
  167. auto mkldnn_used =
  168. benchmarker_mkldnn.exec({{M, K}, {K, N}, {}}) / RUNS;
  169. std::cout << "mkldnn: " << mkldnn_used << " ms, "
  170. << computations / mkldnn_used << " Gflops, "
  171. << "speed_up " << float_used / mkldnn_used << ", ";
  172. }
  173. #endif
  174. if (is_supported(SIMDType::AVX2)) {
  175. auto avx2_used_4x16x2 =
  176. benchmarker_avx2_4x16x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  177. auto avx2_used_2x4x16 =
  178. benchmarker_avx2_2x4x16.exec({{M, K}, {K, N}, {}}) / RUNS;
  179. std::cout << "avx2_k2: " << avx2_used_4x16x2
  180. << " ms, k2 throughput "
  181. << computations / avx2_used_4x16x2 << " Gflops, "
  182. << "k2_speed_up " << float_used / avx2_used_4x16x2
  183. << ", k16_speed_up " << float_used / avx2_used_2x4x16
  184. << ",";
  185. auto avx2_used_4x16x2_8816 =
  186. benchmarker_avx2_4x16x2_8816.exec({{M, K}, {K, N}, {}}) /
  187. RUNS;
  188. std::cout << "avx2_8816: " << avx2_used_4x16x2_8816
  189. << " ms, 8816 throughput "
  190. << computations / avx2_used_4x16x2_8816 << " Gflops,";
  191. }
  192. if (is_supported(SIMDType::SSE4_1)) {
  193. auto sse_used =
  194. benchmarker_sse_4x8x2.exec({{M, K}, {K, N}, {}}) / RUNS;
  195. std::cout << "sse: " << sse_used << " ms, "
  196. << computations / sse_used << " Gflops, "
  197. << "speed_up " << float_used / sse_used << ", ";
  198. }
  199. std::cout << std::endl;
  200. };
  201. for (size_t M : {8, 64, 112, 256, 512}) {
  202. for (size_t K : {8, 16, 32, 64, 112, 256, 512}) {
  203. for (size_t N : {8, 64, 112, 256, 512}) {
  204. run(M, N, K);
  205. }
  206. }
  207. }
  208. }
  209. #endif // MEGDNN_WITH_BENCHMARK
  210. // vim: syntax=cpp.doxygen

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