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_int_8x8x16.cpp 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * \file dnn/test/cpu/matrix_mul_int_8x8x16.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 implied.
  10. */
  11. #include "test/cpu/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/convolution.h"
  15. namespace megdnn {
  16. namespace test {
  17. TEST_F(CPU, MATRIX_MUL_INT_8_8_16) {
  18. Checker<MatrixMul> checker(handle());
  19. param::MatrixMul param;
  20. checker.set_dtype(0, dtype::Int8());
  21. checker.set_dtype(1, dtype::Int8());
  22. checker.set_dtype(2, dtype::Int16());
  23. checker.set_param(param);
  24. for (size_t b : {1, 2, 3})
  25. for (size_t i : {10, 20})
  26. for (size_t o : {11, 22}) {
  27. checker.exec({{b, i}, {i, o}, {}});
  28. }
  29. for (size_t m = 16; m <= 512; m *= 4)
  30. for (size_t n = 16; n <= 512; n *= 4)
  31. for (size_t k = 16; k <= 512; k *= 4) {
  32. checker.exec({{m, k}, {k, n}, {}});
  33. checker.exec({{m + 1, k}, {k, n}, {}});
  34. checker.exec({{m + 5, k}, {k, n}, {}});
  35. checker.exec({{m + 7, k}, {k, n}, {}});
  36. checker.exec({{m, k}, {k, n + 15}, {}});
  37. checker.exec({{m, k}, {k, n + 9}, {}});
  38. checker.exec({{m, k}, {k, n + 8}, {}});
  39. checker.exec({{m, k}, {k, n + 7}, {}});
  40. checker.exec({{m, k}, {k, n + 1}, {}});
  41. checker.exec({{m + 1, k}, {k, n + 9}, {}});
  42. checker.exec({{m + 7, k}, {k, n + 15}, {}});
  43. checker.exec({{m + 7, k}, {k, n + 7}, {}});
  44. }
  45. // test transpose scenerio
  46. {
  47. for (int mask = 0; mask < 4; ++mask) {
  48. param::MatrixMul param;
  49. param.transposeA = (mask & 1);
  50. param.transposeB = (mask & 2);
  51. checker.set_param(param);
  52. size_t m = 100, n = 101, k = 102;
  53. TensorShape A =
  54. param.transposeA ? TensorShape({k, m}) : TensorShape({m, k});
  55. TensorShape B =
  56. param.transposeB ? TensorShape({n, k}) : TensorShape({k, n});
  57. checker.exec({A, B, {}});
  58. }
  59. }
  60. }
  61. #if MEGDNN_WITH_BENCHMARK
  62. TEST_F(CPU, BENCHMARK_MATRIX_MUL_INT8_INT8_INT16) {
  63. bool verbose = getenv("MEGDNN_BENCH_VERBOSE");
  64. using Param = param::MatrixMul;
  65. double speedup_sum = 0, speedup_wsum = 0;
  66. auto run = [&](const TensorShapeArray& shapes, const Param& param) {
  67. TensorLayoutArray layouts;
  68. layouts.emplace_back(shapes[0], dtype::Int8());
  69. layouts.emplace_back(shapes[1], dtype::Int8());
  70. layouts.emplace_back(shapes[2], dtype::Int16());
  71. Benchmarker<MatrixMul> benchmarker_cpu(handle());
  72. param::MatrixMul param_int(param);
  73. benchmarker_cpu.set_param(param_int);
  74. Benchmarker<MatrixMul> benchmarker_float(handle());
  75. benchmarker_float.set_param(param);
  76. auto t2 = benchmarker_cpu.set_display(false).set_adaptive_benchmark(0.01).execl(
  77. layouts);
  78. auto t4 =
  79. benchmarker_float.set_display(false).set_adaptive_benchmark(0.01).exec(
  80. shapes);
  81. if (t2 > t4 || verbose) {
  82. std::cout << "MatA=" << shapes[0].to_string()
  83. << " MatB=" << shapes[1].to_string() << " float=" << t4 << "ms"
  84. << " int=" << t2 << "ms"
  85. << " speedup=" << t4 / t2 << std::endl;
  86. }
  87. speedup_sum += t4 / t2;
  88. speedup_wsum += 1;
  89. };
  90. for (size_t m = 16; m <= 256; m *= 4)
  91. for (size_t k = 16; k <= 256; k *= 4)
  92. for (size_t n = 16; n <= 1024; n *= 4) {
  93. Param param;
  94. run({{m, k}, {k, n}, {}}, param);
  95. run({{m, k}, {k, n + 8}, {}}, param);
  96. run({{m, k}, {k, n + 15}, {}}, param);
  97. run({{m + 5, k}, {k, n}, {}}, param);
  98. run({{m + 7, k}, {k, n}, {}}, param);
  99. }
  100. printf("average speedup: %.3f\n", speedup_sum / speedup_wsum);
  101. }
  102. #endif
  103. } // namespace test
  104. } // namespace megdnn
  105. // vim: syntax=cpp.doxygen