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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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(
  203. AlgoParam{128, 256, 64, 64, 64, 64, 8, 8, 16, 2});
  204. int8_nchw32_imma.emplace_back(
  205. AlgoParam{256, 128, 64, 64, 64, 64, 8, 8, 16, 2});
  206. int8_nchw32_imma.emplace_back(
  207. AlgoParam{128, 128, 64, 64, 64, 64, 8, 8, 16, 2});
  208. int8_nchw32_imma.emplace_back(
  209. AlgoParam{128, 64, 64, 64, 32, 64, 8, 8, 16, 2});
  210. int8_nchw32_imma.emplace_back(
  211. AlgoParam{64, 128, 64, 32, 64, 64, 8, 8, 16, 2});
  212. int8_nchw32_imma.emplace_back(
  213. AlgoParam{128, 64, 32, 64, 32, 32, 8, 8, 16, 1});
  214. int8_nchw32_imma.emplace_back(
  215. AlgoParam{128, 32, 32, 64, 32, 32, 8, 8, 16, 1});
  216. int8_nchw32_imma.emplace_back(
  217. AlgoParam{64, 128, 32, 32, 64, 32, 8, 8, 16, 1});
  218. int8_nchw32_imma.emplace_back(
  219. AlgoParam{32, 128, 32, 32, 64, 32, 8, 8, 16, 1});
  220. }
  221. {
  222. using AlgoParam = AlgoInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  223. int4_int4_nchw64_imma.emplace_back(
  224. AlgoParam{128, 128, 128, 64, 64, 128, 8, 8, 32, 2});
  225. int4_int4_nchw64_imma.emplace_back(
  226. AlgoParam{128, 256, 128, 64, 64, 128, 8, 8, 32, 2});
  227. int4_int4_nchw64_imma.emplace_back(
  228. AlgoParam{128, 64, 128, 64, 64, 128, 8, 8, 32, 2});
  229. int4_int4_nchw64_imma.emplace_back(
  230. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1});
  231. }
  232. {
  233. using AlgoParam = AlgoUInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  234. uint4_int4_nchw64_imma.emplace_back(
  235. AlgoParam{128, 128, 128, 64, 64, 128, 8, 8, 32, 2});
  236. uint4_int4_nchw64_imma.emplace_back(
  237. AlgoParam{128, 256, 128, 64, 64, 128, 8, 8, 32, 2});
  238. uint4_int4_nchw64_imma.emplace_back(
  239. AlgoParam{128, 64, 128, 64, 64, 128, 8, 8, 32, 2});
  240. uint4_int4_nchw64_imma.emplace_back(
  241. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1});
  242. }
  243. {
  244. using AlgoParam = AlgoInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  245. int4_int4_nhwc_imma.emplace_back(
  246. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 32});
  247. int4_int4_nhwc_imma.emplace_back(
  248. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 16});
  249. int4_int4_nhwc_imma.emplace_back(
  250. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 8});
  251. int4_int4_nhwc_imma.emplace_back(
  252. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 32});
  253. int4_int4_nhwc_imma.emplace_back(
  254. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 16});
  255. int4_int4_nhwc_imma.emplace_back(
  256. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 8});
  257. }
  258. {
  259. using AlgoParam = AlgoUInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  260. uint4_int4_nhwc_imma.emplace_back(
  261. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 32});
  262. uint4_int4_nhwc_imma.emplace_back(
  263. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 16});
  264. uint4_int4_nhwc_imma.emplace_back(
  265. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 8});
  266. uint4_int4_nhwc_imma.emplace_back(
  267. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 32});
  268. uint4_int4_nhwc_imma.emplace_back(
  269. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 16});
  270. uint4_int4_nhwc_imma.emplace_back(
  271. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 8});
  272. }
  273. #endif
  274. }
  275. #endif
  276. void ConvBiasForwardImpl::AlgoPack::fill_dp4a_algos() {
  277. using AlgoParam = AlgoInt8NCHW4DotProdImplicitGemm::AlgoParam;
  278. int8_nchw4_dotprod.emplace_back(
  279. AlgoParam{128, 128, 32, 64, 32, 32, 1, 1, 4, 2});
  280. int8_nchw4_dotprod.emplace_back(
  281. AlgoParam{128, 64, 32, 64, 32, 32, 1, 1, 4, 2});
  282. int8_nchw4_dotprod.emplace_back(
  283. AlgoParam{64, 128, 32, 64, 32, 32, 1, 1, 4, 2});
  284. int8_nchw4_dotprod.emplace_back(
  285. AlgoParam{32, 128, 32, 32, 64, 32, 1, 1, 4, 2});
  286. int8_nchw4_dotprod.emplace_back(
  287. AlgoParam{128, 32, 32, 64, 32, 32, 1, 1, 4, 2});
  288. int8_nchw4_dotprod.emplace_back(
  289. AlgoParam{32, 64, 32, 32, 64, 32, 1, 1, 4, 2});
  290. int8_nchw4_dotprod.emplace_back(
  291. AlgoParam{64, 32, 32, 64, 32, 32, 1, 1, 4, 2});
  292. int8_nchw4_dotprod.emplace_back(
  293. AlgoParam{16, 128, 16, 16, 128, 16, 1, 1, 4, 1});
  294. int8_nchw4_dotprod.emplace_back(
  295. AlgoParam{16, 64, 8, 16, 64, 8, 1, 1, 4, 2});
  296. }
  297. ConvBiasForwardImpl::AlgoBase*
  298. ConvBiasForwardImpl::AlgoPack::cudnn_conv_from_enum(
  299. cudnnConvolutionFwdAlgo_t algo) {
  300. for (auto&& i : cudnn_convs) {
  301. if (i.cudnn_enum() == algo)
  302. return &i;
  303. }
  304. megdnn_throw(ssprintf("can not find cudnn conv fwd algorithm %d",
  305. static_cast<int>(algo)));
  306. }
  307. ConvBiasForwardImpl::AlgoBase*
  308. ConvBiasForwardImpl::AlgoPack::cudnn_conv_bias_act_from_enum(
  309. cudnnConvolutionFwdAlgo_t algo) {
  310. for (auto&& i : cudnn_conv_bias_activations) {
  311. if (i.cudnn_enum() == algo)
  312. return &i;
  313. }
  314. megdnn_throw(ssprintf("can not find cudnn conv bias act algorithm %d",
  315. static_cast<int>(algo)));
  316. }
  317. // vim: syntax=cpp.doxygen

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