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.

algo.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * \file dnn/src/cuda/conv_bias/algo.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
  10. * implied.
  11. */
  12. #include "src/cuda/conv_bias/algo.h"
  13. #include "src/cuda/utils.h"
  14. using namespace megdnn;
  15. using namespace cuda;
  16. ConvBiasForwardImpl::AlgoPack::AlgoPack() {
  17. non_cudnn_algos.push_back(&chanwise);
  18. non_cudnn_algos.push_back(&chanwise_small);
  19. non_cudnn_algos.push_back(&inplace_matmul);
  20. non_cudnn_algos.push_back(&matmul);
  21. non_cudnn_algos.push_back(&matmul8x8x32);
  22. non_cudnn_algos.push_back(&batched_matmul);
  23. fill_cudnn_algos();
  24. for (auto&& algo : cudnn_conv_bias_activations) {
  25. all_algos.push_back(&algo);
  26. }
  27. //! add conv+nonlinear algos
  28. std::vector<AlgoBase*> conv_algos;
  29. conv_algos.push_back(&chanwise);
  30. conv_algos.push_back(&chanwise_small);
  31. conv_algos.push_back(&chanwise8x8x32);
  32. for (auto&& algo : cudnn_convs) {
  33. conv_algos.push_back(&algo);
  34. }
  35. conv_algos.push_back(&inplace_matmul);
  36. conv_algos.push_back(&matmul);
  37. conv_algos.push_back(&matmul8x8x32);
  38. conv_algos.push_back(&batched_matmul);
  39. conv_algos.push_back(&group);
  40. for (auto&& algo : conv_algos) {
  41. all_algos.push_back(algo);
  42. }
  43. all_algos.push_back(&bfloat16);
  44. bfloat16_algos.push_back(&bfloat16);
  45. size_t all_algo_size = all_algos.size();
  46. #if CUDA_VERSION >= 10000
  47. fill_imma_algos();
  48. all_algos.push_back(&wmma_quint4x4x32);
  49. for (auto&& algo : int8_nchw4_imma) {
  50. all_algos.push_back(&algo);
  51. }
  52. for (auto&& algo : int8_chwn4_imma) {
  53. all_algos.push_back(&algo);
  54. }
  55. for (auto&& algo : int8_chwn4_imma_reorder_filter) {
  56. all_algos.push_back(&algo);
  57. }
  58. for (auto&& algo : int8_chwn4_imma_unroll_width) {
  59. all_algos.push_back(&algo);
  60. }
  61. #if CUDA_VERSION >= 10020
  62. for (auto&& algo : int8_nchw32_imma) {
  63. all_algos.push_back(&algo);
  64. }
  65. for (auto&& algo : int4_int4_nchw64_imma) {
  66. all_algos.push_back(&algo);
  67. }
  68. for (auto&& algo : uint4_int4_nchw64_imma) {
  69. all_algos.push_back(&algo);
  70. }
  71. for (auto&& algo : int4_int4_nhwc_imma) {
  72. all_algos.push_back(&algo);
  73. }
  74. for (auto&& algo : uint4_int4_nhwc_imma) {
  75. all_algos.push_back(&algo);
  76. }
  77. #endif
  78. #endif
  79. fill_dp4a_algos();
  80. for (auto&& algo : int8_nchw4_dotprod) {
  81. all_algos.push_back(&algo);
  82. }
  83. all_algos.push_back(&int8_chwn4_dotprod);
  84. all_algos.push_back(&fallback_nchw_qs8);
  85. for (size_t i = all_algo_size; i < all_algos.size(); ++i) {
  86. non_cudnn_algos.push_back(all_algos[i]);
  87. }
  88. for (auto&& algo : all_algos) {
  89. m_all_algos_map.emplace(algo->info().desc, algo);
  90. }
  91. }
  92. ConvBiasForwardImpl::AlgoPack ConvBiasForwardImpl::sm_algo_pack;
  93. MEGDNN_DEF_GET_ALGO_FROM_DESC(ConvBiasForwardImpl)
  94. ConvBiasForwardImpl::AlgoBase::SizeArgs::SizeArgs(
  95. const ConvBiasForwardImpl* o, const TensorLayout& src,
  96. const TensorLayout& filter, const TensorLayout& bias,
  97. const TensorLayout& z, const TensorLayout& dst,
  98. const PreprocessedFilter* preprocessed_filter)
  99. : SizeArgs(o, src, filter,
  100. o->make_canonized_filter_meta(src.ndim, filter), bias, z,
  101. dst, preprocessed_filter) {}
  102. ConvBiasForwardImpl::AlgoBase::SizeArgs::SizeArgs(
  103. const ConvBiasForwardImpl* o, const TensorLayout& src,
  104. const TensorLayout& filter, const CanonizedFilterMeta& filter_meta,
  105. const TensorLayout& bias, const TensorLayout& z,
  106. const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter)
  107. : BiasForwardSizeArgs{concrete_handle(o->handle()),
  108. &src,
  109. &filter,
  110. &bias,
  111. &z,
  112. filter_meta,
  113. &dst,
  114. o->param().nonlineMode},
  115. opr{o},
  116. preprocessed_filter{preprocessed_filter} {}
  117. ConvBiasForwardImpl::AlgoBase::ExecArgs::ExecArgs(
  118. ConvBiasForwardImpl* opr, _megdnn_tensor_in src,
  119. _megdnn_tensor_in filter, _megdnn_tensor_in bias, _megdnn_tensor_in z,
  120. _megdnn_tensor_out dst, _megdnn_workspace workspace,
  121. const PreprocessedFilter* preprocessed_filter)
  122. : SizeArgs(opr, src.layout, filter.layout, bias.layout, z.layout,
  123. dst.layout, preprocessed_filter),
  124. src_tensor{&src},
  125. filter_tensor{&filter},
  126. bias_tensor{&bias},
  127. z_tensor{&z},
  128. dst_tensor{&dst},
  129. workspace{workspace} {}
  130. std::string ConvBiasForwardImpl::AlgoBase::SizeArgs::to_string() const {
  131. auto&& fm = filter_meta;
  132. MEGDNN_MARK_USED_VAR(fm);
  133. std::string nonlinear_mode_str;
  134. switch (nonlinear_mode) {
  135. case param::ConvBias::NonlineMode::RELU:
  136. nonlinear_mode_str = "RELU";
  137. break;
  138. case param::ConvBias::NonlineMode::SIGMOID:
  139. nonlinear_mode_str = "SIGMOID";
  140. break;
  141. case param::ConvBias::NonlineMode::IDENTITY:
  142. nonlinear_mode_str = "IDENTITY";
  143. break;
  144. case param::ConvBias::NonlineMode::H_SWISH:
  145. nonlinear_mode_str = "H_SWISH";
  146. break;
  147. default:
  148. megdnn_throw("invalid conv bias nonlinear mode");
  149. }
  150. return ssprintf(
  151. "src=%s, filter=%s, bias=%s, z=%s, dst=%s, "
  152. "pad=%ux%u, stride=%ux%u, dilate=%ux%u, xcorr=%d, dtype=%s,%s, "
  153. "nonlinear_mode=%s",
  154. src_layout->to_string().c_str(), filter_layout->to_string().c_str(),
  155. bias_layout->to_string().c_str(), z_layout->to_string().c_str(),
  156. dst_layout->to_string().c_str(), fm.padding[0], fm.padding[1],
  157. fm.stride[0], fm.stride[1], fm.dilation[0], fm.dilation[1],
  158. !fm.should_flip, src_layout->dtype.name(), dst_layout->dtype.name(),
  159. nonlinear_mode_str.c_str());
  160. }
  161. void ConvBiasForwardImpl::AlgoPack::fill_cudnn_algos() {
  162. for (auto&& algo : CudnnAlgoPack::conv_fwd_algos()) {
  163. cudnn_conv_bias_activations.push_back(algo.first);
  164. cudnn_convs.push_back(algo.first);
  165. }
  166. }
  167. #if CUDA_VERSION >= 10000
  168. void ConvBiasForwardImpl::AlgoPack::fill_imma_algos() {
  169. int8_chwn4_imma.push_back(
  170. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA16x16x16});
  171. int8_chwn4_imma.push_back(
  172. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA32x8x16});
  173. int8_chwn4_imma.push_back(
  174. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA8x32x16});
  175. int8_nchw4_imma.push_back(
  176. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA16x16x16});
  177. int8_nchw4_imma.push_back(
  178. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA32x8x16});
  179. int8_nchw4_imma.push_back(
  180. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA8x32x16});
  181. int8_chwn4_imma_reorder_filter.push_back(
  182. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  183. IMMA16x16x16});
  184. int8_chwn4_imma_reorder_filter.push_back(
  185. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  186. IMMA32x8x16});
  187. int8_chwn4_imma_reorder_filter.push_back(
  188. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  189. IMMA8x32x16});
  190. int8_chwn4_imma_unroll_width.push_back(
  191. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  192. IMMA16x16x16});
  193. int8_chwn4_imma_unroll_width.push_back(
  194. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  195. IMMA32x8x16});
  196. int8_chwn4_imma_unroll_width.push_back(
  197. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  198. IMMA8x32x16});
  199. #if CUDA_VERSION >= 10020
  200. {
  201. using AlgoParam = AlgoInt8NCHW32IMMAImplicitGemm::AlgoParam;
  202. int8_nchw32_imma.emplace_back(AlgoParam{128, 256, 64, 64, 64, 64});
  203. int8_nchw32_imma.emplace_back(AlgoParam{256, 128, 64, 64, 64, 64});
  204. int8_nchw32_imma.emplace_back(AlgoParam{128, 128, 64, 64, 64, 64});
  205. int8_nchw32_imma.emplace_back(AlgoParam{64, 128, 64, 32, 64, 64});
  206. int8_nchw32_imma.emplace_back(AlgoParam{128, 64, 64, 64, 32, 64});
  207. int8_nchw32_imma.emplace_back(AlgoParam{64, 64, 64, 32, 32, 64});
  208. int8_nchw32_imma.emplace_back(AlgoParam{32, 64, 64, 32, 16, 64});
  209. }
  210. {
  211. using AlgoParam = AlgoInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  212. int4_int4_nchw64_imma.emplace_back(
  213. AlgoParam{128, 128, 128, 64, 64, 128});
  214. int4_int4_nchw64_imma.emplace_back(
  215. AlgoParam{256, 128, 128, 64, 64, 128});
  216. }
  217. {
  218. using AlgoParam = AlgoUInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  219. uint4_int4_nchw64_imma.emplace_back(
  220. AlgoParam{128, 128, 128, 64, 64, 128});
  221. uint4_int4_nchw64_imma.emplace_back(
  222. AlgoParam{256, 128, 128, 64, 64, 128});
  223. }
  224. {
  225. using AlgoParam = AlgoInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  226. int4_int4_nhwc_imma.emplace_back(
  227. AlgoParam{128, 32, 64, 64, 32, 64, 32});
  228. int4_int4_nhwc_imma.emplace_back(
  229. AlgoParam{128, 32, 64, 64, 32, 64, 16});
  230. int4_int4_nhwc_imma.emplace_back(AlgoParam{128, 32, 64, 64, 32, 64, 8});
  231. int4_int4_nhwc_imma.emplace_back(
  232. AlgoParam{128, 64, 64, 64, 64, 64, 32});
  233. int4_int4_nhwc_imma.emplace_back(
  234. AlgoParam{128, 64, 64, 64, 64, 64, 16});
  235. int4_int4_nhwc_imma.emplace_back(AlgoParam{128, 64, 64, 64, 64, 64, 8});
  236. }
  237. {
  238. using AlgoParam = AlgoUInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  239. uint4_int4_nhwc_imma.emplace_back(
  240. AlgoParam{128, 32, 64, 64, 32, 64, 32});
  241. uint4_int4_nhwc_imma.emplace_back(
  242. AlgoParam{128, 32, 64, 64, 32, 64, 16});
  243. uint4_int4_nhwc_imma.emplace_back(
  244. AlgoParam{128, 32, 64, 64, 32, 64, 8});
  245. uint4_int4_nhwc_imma.emplace_back(
  246. AlgoParam{128, 64, 64, 64, 64, 64, 32});
  247. uint4_int4_nhwc_imma.emplace_back(
  248. AlgoParam{128, 64, 64, 64, 64, 64, 16});
  249. uint4_int4_nhwc_imma.emplace_back(
  250. AlgoParam{128, 64, 64, 64, 64, 64, 8});
  251. }
  252. #endif
  253. }
  254. #endif
  255. void ConvBiasForwardImpl::AlgoPack::fill_dp4a_algos() {
  256. using AlgoParam = AlgoInt8NCHW4DotProdImplicitGemm::AlgoParam;
  257. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 128, 32, 64, 32, 32, 2});
  258. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 64, 32, 64, 32, 32, 2});
  259. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 128, 32, 64, 32, 32, 2});
  260. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 128, 32, 32, 64, 32, 2});
  261. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 32, 32, 64, 32, 32, 2});
  262. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 64, 32, 64, 32, 32, 2});
  263. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 64, 32, 32, 64, 32, 2});
  264. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 32, 32, 64, 32, 32, 2});
  265. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 32, 32, 32, 32, 32, 2});
  266. int8_nchw4_dotprod.emplace_back(AlgoParam{16, 128, 16, 16, 128, 16, 1});
  267. int8_nchw4_dotprod.emplace_back(AlgoParam{16, 64, 8, 16, 64, 8, 2});
  268. }
  269. ConvBiasForwardImpl::AlgoBase*
  270. ConvBiasForwardImpl::AlgoPack::cudnn_conv_from_enum(
  271. cudnnConvolutionFwdAlgo_t algo) {
  272. for (auto&& i : cudnn_convs) {
  273. if (i.cudnn_enum() == algo)
  274. return &i;
  275. }
  276. megdnn_throw(ssprintf("can not find cudnn conv fwd algorithm %d",
  277. static_cast<int>(algo)));
  278. }
  279. ConvBiasForwardImpl::AlgoBase*
  280. ConvBiasForwardImpl::AlgoPack::cudnn_conv_bias_act_from_enum(
  281. cudnnConvolutionFwdAlgo_t algo) {
  282. for (auto&& i : cudnn_conv_bias_activations) {
  283. if (i.cudnn_enum() == algo)
  284. return &i;
  285. }
  286. megdnn_throw(ssprintf("can not find cudnn conv bias act algorithm %d",
  287. static_cast<int>(algo)));
  288. }
  289. // vim: syntax=cpp.doxygen

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