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

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