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.

opr_impl.cpp 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * \file dnn/src/fallback/matrix_mul/opr_impl.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 "src/fallback/matrix_mul/opr_impl.h"
  12. #include <unordered_map>
  13. #include "megdnn/oprs/base.h"
  14. #include "src/common/metahelper.h"
  15. #include "src/common/utils.h"
  16. #include "src/fallback/matrix_mul/algos.h"
  17. #include "src/fallback/matrix_mul/gemm_impl.h"
  18. #include "src/fallback/matrix_mul/generic_strategy.h"
  19. #include "src/naive/handle.h"
  20. #include "src/naive/matrix_mul/opr_impl.h"
  21. #include "src/common/algo_chooser.h"
  22. #if MEGDNN_X86
  23. #include "src/x86/matrix_mul/opr_impl.h"
  24. #elif MEGDNN_AARCH64
  25. #include "src/aarch64/matrix_mul/opr_impl.h"
  26. #elif MEGDNN_ARMV7
  27. #include "src/armv7/matrix_mul/opr_impl.h"
  28. #endif
  29. using namespace megdnn;
  30. using namespace fallback;
  31. class MatrixMulImpl::AlgoPack : NonCopyableObj {
  32. AlgoF32K8x12x1 f32_k8x12x1;
  33. AlgoGemv gemv;
  34. AlgoNaive naive;
  35. SmallVector<AlgoBase*> m_all_algos;
  36. AlgoBase::Mapper m_all_algos_map;
  37. public:
  38. AlgoPack() {
  39. m_all_algos.emplace_back(&gemv);
  40. m_all_algos.emplace_back(&f32_k8x12x1);
  41. m_all_algos.emplace_back(&naive);
  42. for (auto&& algo : m_all_algos) {
  43. m_all_algos_map.emplace(algo->info().desc, algo);
  44. }
  45. }
  46. const SmallVector<AlgoBase*>& all_algos() const { return m_all_algos; }
  47. const AlgoBase::Mapper& all_algos_map() const { return m_all_algos_map; }
  48. };
  49. const MatrixMulImpl::AlgoPack& MatrixMulImpl::algo_pack() {
  50. static AlgoPack algo_pack;
  51. return algo_pack;
  52. }
  53. SmallVector<MatrixMulImpl::AlgoBase*> MatrixMulImpl::get_all_packed_algo() {
  54. return algo_pack().all_algos();
  55. }
  56. SmallVector<MatrixMulImpl::AlgoBase*> MatrixMulImpl::select_algo_type(
  57. AlgoTypePack index) {
  58. megdnn_assert(nr_type_contain(index.data_type),
  59. "Matmul algo selection only support one type");
  60. SmallVector<MatrixMulImpl::AlgoBase*> algos;
  61. for (auto&& algo : get_all_packed_algo()){
  62. auto algo_desc = algo->matmul_description();
  63. if (contain_data_type(algo_desc.algo_type.data_type,
  64. index.data_type) &&
  65. algo_desc.algo_type.format == index.format) {
  66. algos.push_back(algo);
  67. }
  68. }
  69. return algos;
  70. }
  71. std::vector<MatrixMul::Algorithm*> MatrixMulImpl::get_all_algorithms(
  72. const TensorLayout& A, const TensorLayout& B, const TensorLayout& C) {
  73. std::vector<Algorithm*> gemm_algos, gemv_algos;
  74. auto kern_size_param = make_kern_size_param(A, B, C);
  75. for (auto&& algo : get_all_packed_algo()) {
  76. if (algo->usable(kern_size_param)) {
  77. if (algo->algoset() == AlgoBase::AlgoSet::ALGO_TYPE_GEMV) {
  78. // simple gemv
  79. gemv_algos.push_back(algo);
  80. } else {
  81. gemm_algos.push_back(algo);
  82. }
  83. }
  84. }
  85. gemv_algos.insert(gemv_algos.end(), gemm_algos.begin(), gemm_algos.end());
  86. return gemv_algos;
  87. }
  88. MatrixMulImpl::Algorithm* MatrixMulImpl::get_algorithm_from_desc(
  89. const AlgorithmDesc& desc) {
  90. if (!desc.valid()) {
  91. return nullptr;
  92. } else {
  93. switch (desc.handle_type) {
  94. case Handle::HandleType::FALLBACK: {
  95. const auto& map = algo_pack().all_algos_map();
  96. megdnn_assert(map.find(desc) != map.end());
  97. return map.at(desc);
  98. };
  99. #if MEGDNN_X86
  100. case Handle::HandleType::X86:
  101. return x86::MatrixMulImpl::get_algo_from_desc(desc);
  102. #elif MEGDNN_AARCH64 || MEGDNN_ARMV7
  103. case Handle::HandleType::ARM_COMMON:
  104. return arm_common::MatrixMulImpl::get_algo_from_desc(desc);
  105. #if MEGDNN_AARCH64
  106. case Handle::HandleType::AARCH64:
  107. return aarch64::MatrixMulImpl::get_algo_from_desc(desc);
  108. #else
  109. case Handle::HandleType::ARMV7:
  110. return armv7::MatrixMulImpl::get_algo_from_desc(desc);
  111. #endif
  112. #endif
  113. default:
  114. megdnn_throw("Unknown handle type");
  115. return {};
  116. }
  117. }
  118. }
  119. MatrixMul::Algorithm* MatrixMulImpl::get_algorithm_heuristic(
  120. const TensorLayout& A, const TensorLayout& B, const TensorLayout& C,
  121. size_t workspace_limit_in_bytes, const AlgoAttribute& positive_attr,
  122. const AlgoAttribute& negative_attr) {
  123. auto kern_size_param = make_kern_size_param(A, B, C);
  124. if (auto algo = static_cast<AlgoBase*>(
  125. get_algorithm_from_desc(execution_policy().algo))) {
  126. megdnn_assert(algo->get_workspace(kern_size_param) <
  127. workspace_limit_in_bytes);
  128. auto cur = megdnn::get_algo_match_attribute<MatrixMulImpl>(
  129. algo, positive_attr, negative_attr);
  130. if (cur)
  131. return cur;
  132. megdnn_throw(
  133. ssprintf("require algorithm without attribute(%s) with "
  134. "attribute(%s), but given algorithm with "
  135. "attribute(%s)",
  136. Algorithm::attribute_str(negative_attr).c_str(),
  137. Algorithm::attribute_str(positive_attr).c_str(),
  138. Algorithm::attribute_str(algo->attribute()).c_str()));
  139. }
  140. AlgoTypePack algo_type;
  141. algo_type.data_type = kern_size_param.deduce_algo_data_type();
  142. algo_type.format = kern_size_param.format;
  143. auto algos = select_algo_type(algo_type);
  144. Algorithm *heuristic_algo = nullptr;
  145. Algorithm *usable_algo = nullptr;
  146. for (auto&& algo : algos) {
  147. if (static_cast<AlgoBase*>(algo)->usable(kern_size_param) &&
  148. static_cast<AlgoBase*>(algo)->get_workspace(kern_size_param) <=
  149. workspace_limit_in_bytes) {
  150. if (static_cast<AlgoBase*>(algo)->preferred_attribute(
  151. kern_size_param, positive_attr, negative_attr)) {
  152. //! use gemv algo if it's prefered
  153. if (algo->algoset() == AlgoBase::AlgoSet::ALGO_TYPE_GEMV) {
  154. return algo;
  155. } else if (!heuristic_algo) {
  156. heuristic_algo = algo;
  157. }
  158. } else if (!usable_algo) {
  159. usable_algo = algo;
  160. }
  161. }
  162. }
  163. if (!heuristic_algo) heuristic_algo = usable_algo;
  164. megdnn_assert(heuristic_algo, "No usable algorithm found");
  165. return heuristic_algo;
  166. }
  167. MatrixMulImpl::KernSizeParam MatrixMulImpl::make_kern_size_param(
  168. const TensorLayout& A, const TensorLayout& B, const TensorLayout& C) {
  169. KernSizeParam kern_size_param;
  170. kern_size_param.A_type = A.dtype;
  171. kern_size_param.B_type = B.dtype;
  172. kern_size_param.C_type = C.dtype;
  173. kern_size_param.M = C.shape[0];
  174. kern_size_param.N = C.shape[1];
  175. kern_size_param.K = A[1 - param().transposeA];
  176. kern_size_param.LDA = A.stride[0];
  177. kern_size_param.LDB = B.stride[0];
  178. kern_size_param.LDC = C.stride[0];
  179. kern_size_param.trA = param().transposeA;
  180. kern_size_param.trB = param().transposeB;
  181. kern_size_param.compute_mode = param().compute_mode;
  182. kern_size_param.format = param().format;
  183. size_t pack_size = MatrixMulForward::pack_size(param().format);
  184. kern_size_param.K *= pack_size;
  185. kern_size_param.M *= pack_size;
  186. return kern_size_param;
  187. }
  188. MatrixMulImpl::KernParam MatrixMulImpl::make_kern_param(
  189. _megdnn_tensor_in A, _megdnn_tensor_in B, _megdnn_tensor_out C,
  190. _megdnn_workspace workspace) {
  191. KernParam kern_param;
  192. static_cast<KernSizeParam&>(kern_param) =
  193. make_kern_size_param(A.layout, B.layout, C.layout);
  194. kern_param.A_ptr = A.raw_ptr;
  195. kern_param.B_ptr = B.raw_ptr;
  196. kern_param.C_ptr = C.raw_ptr;
  197. kern_param.workspace_ptr = workspace.raw_ptr;
  198. kern_param.workspace_size = workspace.size;
  199. return kern_param;
  200. }
  201. size_t MatrixMulImpl::get_workspace_in_bytes(const TensorLayout& A,
  202. const TensorLayout& B,
  203. const TensorLayout& C) {
  204. if (auto algo = get_algorithm_heuristic(
  205. A, B, C, std::numeric_limits<size_t>::max(),
  206. AlgoAttribute::DEFAULT, AlgoAttribute::DEFAULT)) {
  207. auto kern_size_param = make_kern_size_param(A, B, C);
  208. return static_cast<AlgoBase*>(algo)->get_workspace(kern_size_param);
  209. }
  210. return 0;
  211. }
  212. void MatrixMulImpl::exec(_megdnn_tensor_in A, _megdnn_tensor_in B,
  213. _megdnn_tensor_out C, _megdnn_workspace workspace) {
  214. check_exec(A.layout, B.layout, C.layout, workspace.size);
  215. if (auto algo = get_algorithm_heuristic(A.layout, B.layout, C.layout,
  216. std::numeric_limits<size_t>::max(),
  217. AlgoAttribute::DEFAULT,
  218. AlgoAttribute::DEFAULT)) {
  219. auto kern_param = make_kern_param(A, B, C, workspace);
  220. auto kern = static_cast<AlgoBase*>(algo)->get_kern(kern_param);
  221. auto run = [kern, kern_param]() { kern(kern_param); };
  222. static_cast<naive::HandleImpl*>(handle())->dispatch_kern(run);
  223. return;
  224. }
  225. naive::MatrixMulForwardImpl::exec(A, B, C, workspace);
  226. }
  227. MatrixMulImpl::AlgoDataType
  228. MatrixMulImpl::KernSizeParam::deduce_algo_data_type() const {
  229. megdnn_assert(A_type.enumv() == B_type.enumv(),
  230. "Matmul A type and B type of different ctype\n");
  231. if (A_type.enumv() == DTypeEnum::Float32) {
  232. return MatrixMulImpl::AlgoDataType::FLOAT32;
  233. #if !MEGDNN_DISABLE_FLOAT16
  234. } else if (A_type.enumv() == DTypeEnum::Float16) {
  235. return MatrixMulImpl::AlgoDataType::FLOAT16;
  236. #endif
  237. } else if (A_type.enumv() == DTypeEnum::Int8 ||
  238. A_type.enumv() == DTypeEnum::QuantizedS8) {
  239. if (C_type.enumv() == DTypeEnum::Int16) {
  240. return MatrixMulImpl::AlgoDataType::INT8X8X16;
  241. } else {
  242. megdnn_assert(C_type.enumv() == DTypeEnum::Int32 ||
  243. C_type.enumv() == DTypeEnum::QuantizedS32);
  244. return MatrixMulImpl::AlgoDataType::QINT8X8X32;
  245. }
  246. } else if (A_type.enumv() == DTypeEnum::Quantized8Asymm) {
  247. return MatrixMulImpl::AlgoDataType::QUINT8X8X32;
  248. } else if (A_type.enumv() == DTypeEnum::Int16) {
  249. return MatrixMulImpl::AlgoDataType::INT16X16X32;
  250. } else {
  251. megdnn_throw(ssprintf("matmul not support data type of %s * %s -> %s\n",
  252. A_type.name(), B_type.name(), C_type.name()));
  253. }
  254. }
  255. // vim: syntax=cpp.doxygen

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