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

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

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