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.

algos.cpp 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * \file dnn/src/aarch64/conv_bias/fp32/algos.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/aarch64/conv_bias/fp32/algos.h"
  12. #include "src/aarch64/conv_bias/fp32/stride2_kern.h"
  13. #include "src/arm_common/conv_bias/direct/multi_thread_common.h"
  14. #include "src/arm_common/conv_bias/postprocess_helper.h"
  15. #include "src/fallback/conv_bias/common.h"
  16. #include "midout.h"
  17. using namespace megdnn;
  18. using namespace aarch64;
  19. MIDOUT_DECL(megdnn_aarch64_conv_bias_stride2_conv2357_fp32)
  20. bool ConvBiasImpl::AlgoF32DirectStride2::usable(
  21. const NCBKernSizeParam& param, AlgoSelectionStrategy) const {
  22. MIDOUT_BEGIN(megdnn_aarch64_conv_bias_stride2_conv2357_fp32, 0, 0) {
  23. auto&& fm = param.filter_meta;
  24. auto FH = fm.spatial[0];
  25. return param.filter_meta.format == param::ConvBias::Format::NCHW &&
  26. param.src_type.enumv() == DTypeEnum::Float32 &&
  27. param.filter_type.enumv() == DTypeEnum::Float32 &&
  28. param.dst_type.enumv() == DTypeEnum::Float32 && !fm.should_flip &&
  29. fm.spatial_ndim == 2 && fm.dilation[0] == 1 && fm.dilation[1] == 1 &&
  30. fm.stride[0] == 2 && fm.stride[1] == 2 && FH == fm.spatial[1] &&
  31. (FH == 2 || FH == 3 || FH == 5 || FH == 7);
  32. }
  33. MIDOUT_END();
  34. return false;
  35. }
  36. size_t ConvBiasImpl::AlgoF32DirectStride2::get_workspace(
  37. const NCBKernSizeParam& param) const {
  38. MIDOUT_BEGIN(megdnn_aarch64_conv_bias_stride2_conv2357_fp32, 0, 1) {
  39. bool large_group = param.filter_meta.group >= param.nr_threads;
  40. auto wbundle = arm_common::MultithreadDirectConvCommon<
  41. float, float>::get_bundle_stride(param, large_group);
  42. return wbundle.total_size_in_bytes();
  43. }
  44. MIDOUT_END();
  45. return 0;
  46. }
  47. SmallVector<ConvBiasImpl::NCBKern> ConvBiasImpl::AlgoF32DirectStride2::dispatch_kerns(
  48. const NCBKernSizeParam& param) const {
  49. MIDOUT_BEGIN(megdnn_aarch64_conv_bias_stride2_conv2357_fp32, 0, 2) {
  50. return get_kimpls(param);
  51. }
  52. MIDOUT_END();
  53. return {};
  54. }
  55. SmallVector<ConvBiasImpl::NCBKern> ConvBiasImpl::AlgoF32DirectStride2::get_kimpls(
  56. const NCBKernSizeParam& param) const {
  57. auto fm = param.filter_meta;
  58. auto FH = fm.spatial[0];
  59. size_t N = param.n;
  60. size_t IC = param.filter_meta.icpg;
  61. size_t OC = param.filter_meta.ocpg;
  62. size_t group = fm.group;
  63. bool large_group = group >= param.nr_threads;
  64. using Func = std::function<void(
  65. const float*, const float*, float*, size_t, size_t, size_t, size_t,
  66. size_t)>;
  67. Func conv = nullptr;
  68. if (FH == 2) {
  69. conv = fp32::conv_stride2::do_conv_2x2_stride2;
  70. } else if (FH == 3) {
  71. conv = fp32::conv_stride2::do_conv_3x3_stride2;
  72. } else if (FH == 5) {
  73. conv = fp32::conv_stride2::do_conv_5x5_stride2;
  74. } else if (FH == 7) {
  75. conv = fp32::conv_stride2::do_conv_7x7_stride2;
  76. }
  77. WorkspaceBundle bundle =
  78. arm_common::MultithreadDirectConvCommon<float, float>::get_bundle_stride(
  79. param, large_group);
  80. SmallVector<NCBKern> ret_kerns;
  81. //! Dense conv and small group
  82. if (large_group) {
  83. //! Channel wise conv and big groups
  84. auto exec_one_group = [bundle, conv](
  85. const NCBKernParam& kern_param,
  86. const NCBKernIndex& ncb_index) mutable {
  87. auto fm = kern_param.filter_meta;
  88. size_t IC = fm.icpg;
  89. size_t OC = fm.ocpg;
  90. bundle.set(kern_param.workspace_ptr);
  91. for (size_t ic = 0; ic < IC; ic++) {
  92. arm_common::MultithreadDirectConvCommon<float, float>::
  93. copy_padding_kern_stride(
  94. bundle, kern_param, ncb_index,
  95. {ncb_index.thread_id, 0, ic});
  96. }
  97. for (size_t oc = 0; oc < OC; oc++) {
  98. arm_common::MultithreadDirectConvCommon<float, float>::
  99. do_conv_kern_stride(
  100. bundle, kern_param, ncb_index, conv,
  101. {ncb_index.thread_id, 0, oc});
  102. }
  103. };
  104. ret_kerns.push_back({exec_one_group, {group, N, 1_z}});
  105. } else {
  106. auto copy_padding = [bundle](
  107. const NCBKernParam& kern_param,
  108. const NCBKernIndex& ncb_index) mutable {
  109. bundle.set(kern_param.workspace_ptr);
  110. arm_common::MultithreadDirectConvCommon<float, float>::
  111. copy_padding_kern_stride(
  112. bundle, kern_param, ncb_index, ncb_index.ndrange_id);
  113. };
  114. ret_kerns.push_back({copy_padding, {group, N, IC}});
  115. auto do_conv = [bundle, conv](
  116. const NCBKernParam& kern_param,
  117. const NCBKernIndex& ncb_index) mutable {
  118. bundle.set(kern_param.workspace_ptr);
  119. arm_common::MultithreadDirectConvCommon<float, float>::do_conv_kern_stride(
  120. bundle, kern_param, ncb_index, conv, ncb_index.ndrange_id);
  121. };
  122. ret_kerns.push_back({do_conv, {group, N, OC}});
  123. }
  124. return ret_kerns;
  125. }

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