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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * \file dnn/test/fallback/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 implied.
  10. */
  11. #include "test/fallback/fixture.h"
  12. #include "test/common/rng.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/matrix_mul.h"
  15. namespace megdnn {
  16. namespace test {
  17. TEST_F(FALLBACK, MATRIX_MUL) {
  18. Checker<MatrixMul> checker(handle());
  19. using Param = MatrixMul::Param;
  20. auto args = matrix_mul::get_matmul_args();
  21. for (auto arg : args) {
  22. auto m = arg.m, n = arg.n, k = arg.k;
  23. auto mask = arg.mask;
  24. Param param;
  25. param.transposeA = mask & 1;
  26. param.transposeB = mask & 2;
  27. TensorShape AS, BS, CS;
  28. if (param.transposeA)
  29. AS = TensorShape{k, m};
  30. else
  31. AS = TensorShape{m, k};
  32. if (param.transposeB)
  33. BS = TensorShape{n, k};
  34. else
  35. BS = TensorShape{k, n};
  36. CS = TensorShape{m, n};
  37. TensorLayout AL, BL, CL;
  38. AL = TensorLayout(AS, dtype::Float32());
  39. BL = TensorLayout(BS, dtype::Float32());
  40. CL = TensorLayout(CS, dtype::Float32());
  41. checker.set_param(param);
  42. checker.execl({AL, BL, CL});
  43. }
  44. }
  45. TEST_F(FALLBACK, MATRIX_MUL_NAIVE_MK4) {
  46. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  47. dtype::Float32{}, handle(), "FB_NAIVE",
  48. param::MatrixMul::Format::MK4, 1);
  49. }
  50. TEST_F(FALLBACK, MATRIX_MUL_NAIVE_MK8) {
  51. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  52. dtype::Float32{}, handle(), "FB_NAIVE",
  53. param::MatrixMul::Format::MK8, 1);
  54. }
  55. TEST_F(FALLBACK, MATRIX_MUL_NAIVE_MK4_DOT) {
  56. matrix_mul::check_matrix_mul(dtype::Float32{}, dtype::Float32{},
  57. dtype::Float32{}, handle(), "FB_NAIVE",
  58. param::MatrixMul::Format::MK4_DOT, 1);
  59. }
  60. TEST_F(FALLBACK, MATRIX_MUL_NAIVE) {
  61. Checker<MatrixMul> checker(handle());
  62. checker.set_before_exec_callback(AlgoChecker<MatrixMul>("FB_NAIVE"));
  63. using Param = MatrixMul::Param;
  64. auto args = matrix_mul::get_matmul_args();
  65. for (auto arg : args) {
  66. auto m = arg.m, n = arg.n, k = arg.k;
  67. auto mask = arg.mask;
  68. Param param;
  69. param.transposeA = mask & 1;
  70. param.transposeB = mask & 2;
  71. TensorShape AS, BS, CS;
  72. if (param.transposeA)
  73. AS = TensorShape{k, m};
  74. else
  75. AS = TensorShape{m, k};
  76. if (param.transposeB)
  77. BS = TensorShape{n, k};
  78. else
  79. BS = TensorShape{k, n};
  80. CS = TensorShape{m, n};
  81. TensorLayout AL, BL, CL;
  82. AL = TensorLayout(AS, dtype::Float32());
  83. BL = TensorLayout(BS, dtype::Float32());
  84. CL = TensorLayout(CS, dtype::Float32());
  85. checker.set_param(param);
  86. checker.execl({AL, BL, CL});
  87. }
  88. }
  89. TEST_F(FALLBACK, BATCHED_MATRIX_MUL) {
  90. Checker<BatchedMatrixMul> checker(handle());
  91. using Param = MatrixMul::Param;
  92. auto args = matrix_mul::get_batched_matmul_args();
  93. for (auto arg : args) {
  94. auto b = arg.b, m = arg.m, n = arg.n, k = arg.k;
  95. auto mask = arg.mask;
  96. Param param;
  97. param.transposeA = mask & 1;
  98. param.transposeB = mask & 2;
  99. TensorShape AS, BS, CS;
  100. if (param.transposeA)
  101. AS = TensorShape{b, k, m};
  102. else
  103. AS = TensorShape{b, m, k};
  104. if (param.transposeB)
  105. BS = TensorShape{b, n, k};
  106. else
  107. BS = TensorShape{b, k, n};
  108. TensorLayout AL, BL;
  109. AL = TensorLayout(AS, dtype::Float32());
  110. BL = TensorLayout(BS, dtype::Float32());
  111. checker.set_param(param);
  112. checker.execs({AL, BL, {}});
  113. }
  114. }
  115. } // namespace test
  116. } // namespace megdnn
  117. // vim: syntax=cpp.doxygen

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