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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.reserve(conv_algos.size() * 2);
  40. //! add gconv algos by AlgoGroupConvGeneral
  41. size_t algo_size = conv_algos.size();
  42. for (size_t i = 3; i < algo_size; ++i) {
  43. gconv_refhold.emplace_back(new AlgoGroupConvGeneral(conv_algos[i]));
  44. algo2gconv[conv_algos[i]] = gconv_refhold.back().get();
  45. conv_algos.push_back(gconv_refhold.back().get());
  46. }
  47. for (auto&& algo : conv_algos) {
  48. all_algos.push_back(algo);
  49. }
  50. non_cudnn_algos.push_back(all_algos.rbegin()[4]); // group inplace_matmul
  51. non_cudnn_algos.push_back(all_algos.rbegin()[3]); // group matmul
  52. non_cudnn_algos.push_back(all_algos.rbegin()[2]); // group matmul_8x8x32
  53. non_cudnn_algos.push_back(all_algos.rbegin()[1]); // group batched_matmul
  54. non_cudnn_algos.push_back(all_algos.rbegin()[0]); // group 1x1
  55. all_algos.push_back(&bfloat16);
  56. bfloat16_algos.push_back(&bfloat16);
  57. size_t all_algo_size = all_algos.size();
  58. #if CUDA_VERSION >= 10000
  59. fill_imma_algos();
  60. all_algos.push_back(&wmma_quint4x4x32);
  61. for (auto&& algo : int8_nchw4_imma) {
  62. all_algos.push_back(&algo);
  63. }
  64. for (auto&& algo : int8_chwn4_imma) {
  65. all_algos.push_back(&algo);
  66. }
  67. for (auto&& algo : int8_chwn4_imma_reorder_filter) {
  68. all_algos.push_back(&algo);
  69. }
  70. for (auto&& algo : int8_chwn4_imma_unroll_width) {
  71. all_algos.push_back(&algo);
  72. }
  73. #if CUDA_VERSION >= 10020
  74. for (auto&& algo : int8_nchw32_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. 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, o->check_layout_fwd(src, filter, dst), bias,
  100. z, dst, preprocessed_filter) {}
  101. ConvBiasForwardImpl::AlgoBase::SizeArgs::SizeArgs(
  102. ConvBiasForwardImpl* o, const TensorLayout& src,
  103. const TensorLayout& filter, const CanonizedFilterMeta& filter_meta,
  104. const TensorLayout& bias, const TensorLayout& z,
  105. const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter)
  106. : BiasForwardSizeArgs{concrete_handle(o->handle()),
  107. &src,
  108. &filter,
  109. &bias,
  110. &z,
  111. filter_meta,
  112. &dst,
  113. o->param().nonlineMode},
  114. opr{o},
  115. preprocessed_filter{preprocessed_filter} {}
  116. ConvBiasForwardImpl::AlgoBase::ExecArgs::ExecArgs(
  117. ConvBiasForwardImpl* opr, _megdnn_tensor_in src,
  118. _megdnn_tensor_in filter, _megdnn_tensor_in bias, _megdnn_tensor_in z,
  119. _megdnn_tensor_out dst, _megdnn_workspace workspace,
  120. const PreprocessedFilter* preprocessed_filter)
  121. : SizeArgs(opr, src.layout, filter.layout, bias.layout, z.layout,
  122. dst.layout, preprocessed_filter),
  123. src_tensor{&src},
  124. filter_tensor{&filter},
  125. bias_tensor{&bias},
  126. z_tensor{&z},
  127. dst_tensor{&dst},
  128. workspace{workspace} {}
  129. std::string ConvBiasForwardImpl::AlgoBase::SizeArgs::to_string() const {
  130. auto&& fm = filter_meta;
  131. MEGDNN_MARK_USED_VAR(fm);
  132. std::string nonlinear_mode_str;
  133. switch (nonlinear_mode) {
  134. case param::ConvBias::NonlineMode::RELU:
  135. nonlinear_mode_str = "RELU";
  136. break;
  137. case param::ConvBias::NonlineMode::SIGMOID:
  138. nonlinear_mode_str = "SIGMOID";
  139. break;
  140. case param::ConvBias::NonlineMode::IDENTITY:
  141. nonlinear_mode_str = "IDENTITY";
  142. break;
  143. case param::ConvBias::NonlineMode::H_SWISH:
  144. nonlinear_mode_str = "H_SWISH";
  145. break;
  146. default:
  147. megdnn_throw("invalid conv bias nonlinear mode");
  148. }
  149. return ssprintf(
  150. "src=%s, filter=%u{%u,%u,%u,%u}, bias=%s, z=%s, dst=%s, "
  151. "pad=%ux%u, stride=%ux%u, dilate=%ux%u, xcorr=%d, dtype=%s,%s, "
  152. "nonlinear_mode=%s",
  153. src_layout->to_string().c_str(), fm.group, fm.ocpg, fm.icpg,
  154. fm.spatial[0], fm.spatial[1], bias_layout->to_string().c_str(),
  155. z_layout->to_string().c_str(), dst_layout->to_string().c_str(),
  156. fm.padding[0], fm.padding[1], fm.stride[0], fm.stride[1],
  157. fm.dilation[0], fm.dilation[1], !fm.should_flip,
  158. 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. #endif
  211. }
  212. #endif
  213. void ConvBiasForwardImpl::AlgoPack::fill_dp4a_algos() {
  214. using AlgoParam = AlgoInt8NCHW4DotProdImplicitGemm::AlgoParam;
  215. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 128, 32, 64, 32, 32, 2});
  216. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 64, 32, 64, 32, 32, 2});
  217. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 128, 32, 64, 32, 32, 2});
  218. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 128, 32, 32, 64, 32, 2});
  219. int8_nchw4_dotprod.emplace_back(AlgoParam{128, 32, 32, 64, 32, 32, 2});
  220. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 64, 32, 64, 32, 32, 2});
  221. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 64, 32, 32, 64, 32, 2});
  222. int8_nchw4_dotprod.emplace_back(AlgoParam{64, 32, 32, 64, 32, 32, 2});
  223. int8_nchw4_dotprod.emplace_back(AlgoParam{32, 32, 32, 32, 32, 32, 2});
  224. int8_nchw4_dotprod.emplace_back(AlgoParam{16, 128, 16, 16, 128, 16, 1});
  225. int8_nchw4_dotprod.emplace_back(AlgoParam{16, 64, 8, 16, 64, 8, 2});
  226. }
  227. ConvBiasForwardImpl::AlgoBase*
  228. ConvBiasForwardImpl::AlgoPack::cudnn_conv_from_enum(
  229. cudnnConvolutionFwdAlgo_t algo) {
  230. for (auto&& i : cudnn_convs) {
  231. if (i.cudnn_enum() == algo)
  232. return &i;
  233. }
  234. megdnn_throw(ssprintf("can not find cudnn conv fwd algorithm %d",
  235. static_cast<int>(algo)));
  236. }
  237. ConvBiasForwardImpl::AlgoBase*
  238. ConvBiasForwardImpl::AlgoPack::cudnn_conv_bias_act_from_enum(
  239. cudnnConvolutionFwdAlgo_t algo) {
  240. for (auto&& i : cudnn_conv_bias_activations) {
  241. if (i.cudnn_enum() == algo)
  242. return &i;
  243. }
  244. megdnn_throw(ssprintf("can not find cudnn conv bias act algorithm %d",
  245. static_cast<int>(algo)));
  246. }
  247. // vim: syntax=cpp.doxygen

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