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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 : int8_nhwc_imma) {
  66. all_algos.push_back(&algo);
  67. }
  68. for (auto&& algo : int4_int4_nchw64_imma) {
  69. all_algos.push_back(&algo);
  70. }
  71. for (auto&& algo : uint4_int4_nchw64_imma) {
  72. all_algos.push_back(&algo);
  73. }
  74. for (auto&& algo : int4_int4_nhwc_imma) {
  75. all_algos.push_back(&algo);
  76. }
  77. for (auto&& algo : uint4_int4_nhwc_imma) {
  78. all_algos.push_back(&algo);
  79. }
  80. #endif
  81. #endif
  82. fill_dp4a_algos();
  83. for (auto&& algo : int8_nchw4_dotprod) {
  84. all_algos.push_back(&algo);
  85. }
  86. all_algos.push_back(&int8_chwn4_dotprod);
  87. all_algos.push_back(&fallback_nchw_qs8);
  88. for (size_t i = all_algo_size; i < all_algos.size(); ++i) {
  89. non_cudnn_algos.push_back(all_algos[i]);
  90. }
  91. for (auto&& algo : all_algos) {
  92. m_all_algos_map.emplace(algo->info().desc, algo);
  93. }
  94. }
  95. ConvBiasForwardImpl::AlgoPack ConvBiasForwardImpl::sm_algo_pack;
  96. MEGDNN_DEF_GET_ALGO_FROM_DESC(ConvBiasForwardImpl)
  97. ConvBiasForwardImpl::AlgoBase::SizeArgs::SizeArgs(
  98. const ConvBiasForwardImpl* o, const TensorLayout& src,
  99. const TensorLayout& filter, const TensorLayout& bias,
  100. const TensorLayout& z, const TensorLayout& dst,
  101. const PreprocessedFilter* preprocessed_filter)
  102. : SizeArgs(o, src, filter,
  103. o->make_canonized_filter_meta(src.ndim, filter), bias, z,
  104. dst, preprocessed_filter) {}
  105. ConvBiasForwardImpl::AlgoBase::SizeArgs::SizeArgs(
  106. const ConvBiasForwardImpl* o, const TensorLayout& src,
  107. const TensorLayout& filter, const CanonizedFilterMeta& filter_meta,
  108. const TensorLayout& bias, const TensorLayout& z,
  109. const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter)
  110. : BiasForwardSizeArgs{concrete_handle(o->handle()),
  111. &src,
  112. &filter,
  113. &bias,
  114. &z,
  115. filter_meta,
  116. &dst,
  117. o->param().nonlineMode},
  118. opr{o},
  119. preprocessed_filter{preprocessed_filter} {}
  120. ConvBiasForwardImpl::AlgoBase::ExecArgs::ExecArgs(
  121. ConvBiasForwardImpl* opr, _megdnn_tensor_in src,
  122. _megdnn_tensor_in filter, _megdnn_tensor_in bias, _megdnn_tensor_in z,
  123. _megdnn_tensor_out dst, _megdnn_workspace workspace,
  124. const PreprocessedFilter* preprocessed_filter)
  125. : SizeArgs(opr, src.layout, filter.layout, bias.layout, z.layout,
  126. dst.layout, preprocessed_filter),
  127. src_tensor{&src},
  128. filter_tensor{&filter},
  129. bias_tensor{&bias},
  130. z_tensor{&z},
  131. dst_tensor{&dst},
  132. workspace{workspace} {}
  133. std::string ConvBiasForwardImpl::AlgoBase::SizeArgs::to_string() const {
  134. auto&& fm = filter_meta;
  135. MEGDNN_MARK_USED_VAR(fm);
  136. std::string nonlinear_mode_str;
  137. switch (nonlinear_mode) {
  138. case param::ConvBias::NonlineMode::RELU:
  139. nonlinear_mode_str = "RELU";
  140. break;
  141. case param::ConvBias::NonlineMode::SIGMOID:
  142. nonlinear_mode_str = "SIGMOID";
  143. break;
  144. case param::ConvBias::NonlineMode::IDENTITY:
  145. nonlinear_mode_str = "IDENTITY";
  146. break;
  147. case param::ConvBias::NonlineMode::H_SWISH:
  148. nonlinear_mode_str = "H_SWISH";
  149. break;
  150. default:
  151. megdnn_throw("invalid conv bias nonlinear mode");
  152. }
  153. return ssprintf(
  154. "src=%s, filter=%s, bias=%s, z=%s, dst=%s, "
  155. "pad=%ux%u, stride=%ux%u, dilate=%ux%u, xcorr=%d, dtype=%s,%s, "
  156. "nonlinear_mode=%s",
  157. src_layout->to_string().c_str(), filter_layout->to_string().c_str(),
  158. bias_layout->to_string().c_str(), z_layout->to_string().c_str(),
  159. dst_layout->to_string().c_str(), fm.padding[0], fm.padding[1],
  160. fm.stride[0], fm.stride[1], fm.dilation[0], fm.dilation[1],
  161. !fm.should_flip, src_layout->dtype.name(), dst_layout->dtype.name(),
  162. nonlinear_mode_str.c_str());
  163. }
  164. void ConvBiasForwardImpl::AlgoPack::fill_cudnn_algos() {
  165. for (auto&& algo : CudnnAlgoPack::conv_fwd_algos()) {
  166. cudnn_conv_bias_activations.push_back(algo.first);
  167. cudnn_convs.push_back(algo.first);
  168. }
  169. }
  170. #if CUDA_VERSION >= 10000
  171. void ConvBiasForwardImpl::AlgoPack::fill_imma_algos() {
  172. int8_chwn4_imma.push_back(
  173. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA16x16x16});
  174. int8_chwn4_imma.push_back(
  175. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA32x8x16});
  176. int8_chwn4_imma.push_back(
  177. {AlgoInt8CHWN4IMMAImplicitGemm::MMATileSize::IMMA8x32x16});
  178. int8_nchw4_imma.push_back(
  179. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA16x16x16});
  180. int8_nchw4_imma.push_back(
  181. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA32x8x16});
  182. int8_nchw4_imma.push_back(
  183. {AlgoInt8NCHW4IMMAImplicitGemm::MMATileSize::IMMA8x32x16});
  184. int8_chwn4_imma_reorder_filter.push_back(
  185. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  186. IMMA16x16x16});
  187. int8_chwn4_imma_reorder_filter.push_back(
  188. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  189. IMMA32x8x16});
  190. int8_chwn4_imma_reorder_filter.push_back(
  191. {AlgoInt8CHWN4IMMAImplicitGemmReorderFilter::MMATileSize::
  192. IMMA8x32x16});
  193. int8_chwn4_imma_unroll_width.push_back(
  194. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  195. IMMA16x16x16});
  196. int8_chwn4_imma_unroll_width.push_back(
  197. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  198. IMMA32x8x16});
  199. int8_chwn4_imma_unroll_width.push_back(
  200. {AlgoInt8CHWN4IMMAImplicitGemmUnrollWidth::MMATileSize::
  201. IMMA8x32x16});
  202. #if CUDA_VERSION >= 10020
  203. {
  204. using AlgoParam = AlgoInt8NCHW32IMMAImplicitGemm::AlgoParam;
  205. int8_nchw32_imma.emplace_back(
  206. AlgoParam{128, 256, 64, 64, 64, 64, 8, 8, 16, 2});
  207. int8_nchw32_imma.emplace_back(
  208. AlgoParam{256, 128, 64, 64, 64, 64, 8, 8, 16, 2});
  209. int8_nchw32_imma.emplace_back(
  210. AlgoParam{128, 128, 64, 64, 64, 64, 8, 8, 16, 2});
  211. int8_nchw32_imma.emplace_back(
  212. AlgoParam{128, 64, 64, 64, 32, 64, 8, 8, 16, 2});
  213. int8_nchw32_imma.emplace_back(
  214. AlgoParam{64, 128, 64, 32, 64, 64, 8, 8, 16, 2});
  215. int8_nchw32_imma.emplace_back(
  216. AlgoParam{128, 64, 32, 64, 32, 32, 8, 8, 16, 1});
  217. int8_nchw32_imma.emplace_back(
  218. AlgoParam{128, 32, 32, 64, 32, 32, 8, 8, 16, 1});
  219. int8_nchw32_imma.emplace_back(
  220. AlgoParam{64, 128, 32, 32, 64, 32, 8, 8, 16, 1});
  221. int8_nchw32_imma.emplace_back(
  222. AlgoParam{32, 128, 32, 32, 64, 32, 8, 8, 16, 1});
  223. }
  224. {
  225. using AlgoParam = AlgoInt8NHWCIMMAImplicitGemm::AlgoParam;
  226. int8_nhwc_imma.emplace_back(
  227. AlgoParam{64, 16, 32, 64, 16, 32, 8, 8, 16, 2, 16});
  228. int8_nhwc_imma.emplace_back(
  229. AlgoParam{64, 16, 32, 64, 16, 32, 8, 8, 16, 2, 8});
  230. int8_nhwc_imma.emplace_back(
  231. AlgoParam{64, 16, 32, 64, 16, 32, 8, 8, 16, 2, 4});
  232. int8_nhwc_imma.emplace_back(
  233. AlgoParam{128, 32, 32, 64, 32, 32, 8, 8, 16, 1, 16});
  234. int8_nhwc_imma.emplace_back(
  235. AlgoParam{128, 32, 32, 64, 32, 32, 8, 8, 16, 1, 8});
  236. int8_nhwc_imma.emplace_back(
  237. AlgoParam{128, 32, 32, 64, 32, 32, 8, 8, 16, 1, 4});
  238. }
  239. {
  240. using AlgoParam = AlgoInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  241. int4_int4_nchw64_imma.emplace_back(
  242. AlgoParam{128, 128, 128, 64, 64, 128, 8, 8, 32, 2});
  243. int4_int4_nchw64_imma.emplace_back(
  244. AlgoParam{128, 256, 128, 64, 64, 128, 8, 8, 32, 2});
  245. int4_int4_nchw64_imma.emplace_back(
  246. AlgoParam{128, 64, 128, 64, 64, 128, 8, 8, 32, 2});
  247. int4_int4_nchw64_imma.emplace_back(
  248. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1});
  249. }
  250. {
  251. using AlgoParam = AlgoUInt4Int4NCHW64IMMAImplicitGemm::AlgoParam;
  252. uint4_int4_nchw64_imma.emplace_back(
  253. AlgoParam{128, 128, 128, 64, 64, 128, 8, 8, 32, 2});
  254. uint4_int4_nchw64_imma.emplace_back(
  255. AlgoParam{128, 256, 128, 64, 64, 128, 8, 8, 32, 2});
  256. uint4_int4_nchw64_imma.emplace_back(
  257. AlgoParam{128, 64, 128, 64, 64, 128, 8, 8, 32, 2});
  258. uint4_int4_nchw64_imma.emplace_back(
  259. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1});
  260. }
  261. {
  262. using AlgoParam = AlgoInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  263. int4_int4_nhwc_imma.emplace_back(
  264. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 32});
  265. int4_int4_nhwc_imma.emplace_back(
  266. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 16});
  267. int4_int4_nhwc_imma.emplace_back(
  268. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 8});
  269. int4_int4_nhwc_imma.emplace_back(
  270. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 32});
  271. int4_int4_nhwc_imma.emplace_back(
  272. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 16});
  273. int4_int4_nhwc_imma.emplace_back(
  274. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 8});
  275. int4_int4_nhwc_imma.emplace_back(
  276. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 32});
  277. int4_int4_nhwc_imma.emplace_back(
  278. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 16});
  279. int4_int4_nhwc_imma.emplace_back(
  280. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 8});
  281. }
  282. {
  283. using AlgoParam = AlgoUInt4Int4NHWCIMMAImplicitGemm::AlgoParam;
  284. uint4_int4_nhwc_imma.emplace_back(
  285. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 32});
  286. uint4_int4_nhwc_imma.emplace_back(
  287. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 16});
  288. uint4_int4_nhwc_imma.emplace_back(
  289. AlgoParam{128, 16, 64, 128, 16, 64, 8, 8, 32, 2, 8});
  290. uint4_int4_nhwc_imma.emplace_back(
  291. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 32});
  292. uint4_int4_nhwc_imma.emplace_back(
  293. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 16});
  294. uint4_int4_nhwc_imma.emplace_back(
  295. AlgoParam{128, 32, 64, 64, 32, 64, 8, 8, 32, 1, 8});
  296. uint4_int4_nhwc_imma.emplace_back(
  297. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 32});
  298. uint4_int4_nhwc_imma.emplace_back(
  299. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 16});
  300. uint4_int4_nhwc_imma.emplace_back(
  301. AlgoParam{128, 64, 64, 64, 64, 64, 8, 8, 32, 1, 8});
  302. }
  303. #endif
  304. }
  305. #endif
  306. void ConvBiasForwardImpl::AlgoPack::fill_dp4a_algos() {
  307. using AlgoParam = AlgoInt8NCHW4DotProdImplicitGemm::AlgoParam;
  308. int8_nchw4_dotprod.emplace_back(
  309. AlgoParam{128, 128, 32, 64, 32, 32, 1, 1, 4, 2});
  310. int8_nchw4_dotprod.emplace_back(
  311. AlgoParam{128, 64, 32, 64, 32, 32, 1, 1, 4, 2});
  312. int8_nchw4_dotprod.emplace_back(
  313. AlgoParam{64, 128, 32, 64, 32, 32, 1, 1, 4, 2});
  314. int8_nchw4_dotprod.emplace_back(
  315. AlgoParam{32, 128, 32, 32, 64, 32, 1, 1, 4, 2});
  316. int8_nchw4_dotprod.emplace_back(
  317. AlgoParam{128, 32, 32, 64, 32, 32, 1, 1, 4, 2});
  318. int8_nchw4_dotprod.emplace_back(
  319. AlgoParam{32, 64, 32, 32, 64, 32, 1, 1, 4, 2});
  320. int8_nchw4_dotprod.emplace_back(
  321. AlgoParam{64, 32, 32, 64, 32, 32, 1, 1, 4, 2});
  322. int8_nchw4_dotprod.emplace_back(
  323. AlgoParam{16, 128, 16, 16, 128, 16, 1, 1, 4, 1});
  324. int8_nchw4_dotprod.emplace_back(
  325. AlgoParam{16, 64, 8, 16, 64, 8, 1, 1, 4, 2});
  326. }
  327. ConvBiasForwardImpl::AlgoBase*
  328. ConvBiasForwardImpl::AlgoPack::cudnn_conv_from_enum(
  329. cudnnConvolutionFwdAlgo_t algo) {
  330. for (auto&& i : cudnn_convs) {
  331. if (i.cudnn_enum() == algo)
  332. return &i;
  333. }
  334. megdnn_throw(ssprintf("can not find cudnn conv fwd algorithm %d",
  335. static_cast<int>(algo)));
  336. }
  337. ConvBiasForwardImpl::AlgoBase*
  338. ConvBiasForwardImpl::AlgoPack::cudnn_conv_bias_act_from_enum(
  339. cudnnConvolutionFwdAlgo_t algo) {
  340. for (auto&& i : cudnn_conv_bias_activations) {
  341. if (i.cudnn_enum() == algo)
  342. return &i;
  343. }
  344. megdnn_throw(ssprintf("can not find cudnn conv bias act algorithm %d",
  345. static_cast<int>(algo)));
  346. }
  347. // vim: syntax=cpp.doxygen

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