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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) {
  46. Checker<MatrixMul> checker(handle());
  47. checker.set_before_exec_callback(AlgoChecker<MatrixMul>("FB_NAIVE"));
  48. using Param = MatrixMul::Param;
  49. auto args = matrix_mul::get_matmul_args();
  50. for (auto arg : args) {
  51. auto m = arg.m, n = arg.n, k = arg.k;
  52. auto mask = arg.mask;
  53. Param param;
  54. param.transposeA = mask & 1;
  55. param.transposeB = mask & 2;
  56. TensorShape AS, BS, CS;
  57. if (param.transposeA)
  58. AS = TensorShape{k, m};
  59. else
  60. AS = TensorShape{m, k};
  61. if (param.transposeB)
  62. BS = TensorShape{n, k};
  63. else
  64. BS = TensorShape{k, n};
  65. CS = TensorShape{m, n};
  66. TensorLayout AL, BL, CL;
  67. AL = TensorLayout(AS, dtype::Float32());
  68. BL = TensorLayout(BS, dtype::Float32());
  69. CL = TensorLayout(CS, dtype::Float32());
  70. checker.set_param(param);
  71. checker.execl({AL, BL, CL});
  72. }
  73. }
  74. TEST_F(FALLBACK, BATCHED_MATRIX_MUL) {
  75. Checker<BatchedMatrixMul> checker(handle());
  76. using Param = MatrixMul::Param;
  77. auto args = matrix_mul::get_batched_matmul_args();
  78. for (auto arg : args) {
  79. auto b = arg.b, m = arg.m, n = arg.n, k = arg.k;
  80. auto mask = arg.mask;
  81. Param param;
  82. param.transposeA = mask & 1;
  83. param.transposeB = mask & 2;
  84. TensorShape AS, BS, CS;
  85. if (param.transposeA)
  86. AS = TensorShape{b, k, m};
  87. else
  88. AS = TensorShape{b, m, k};
  89. if (param.transposeB)
  90. BS = TensorShape{b, n, k};
  91. else
  92. BS = TensorShape{b, k, n};
  93. TensorLayout AL, BL;
  94. AL = TensorLayout(AS, dtype::Float32());
  95. BL = TensorLayout(BS, dtype::Float32());
  96. checker.set_param(param);
  97. checker.execs({AL, BL, {}});
  98. }
  99. }
  100. } // namespace test
  101. } // namespace megdnn
  102. // vim: syntax=cpp.doxygen

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