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.h 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * \file dnn/src/fallback/conv_bias/im2col/algos.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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. #pragma once
  12. #include "megdnn/thin/small_vector.h"
  13. #include "src/common/utils.h"
  14. #include "src/fallback/conv_bias/opr_impl.h"
  15. #include "src/fallback/matrix_mul/opr_impl.h"
  16. #include "src/common/opr_delegate.h"
  17. namespace megdnn {
  18. namespace fallback {
  19. class ConvBiasImpl::AlgoIm2col final : public AlgoBase {
  20. //! calculate m_oc_tile_size in choice_ohw_oc_block() fucntion,
  21. //! when m_oc_tile_size < this value m_oc_tile_size = ohw
  22. static constexpr size_t DEFAULT_OHW_MIN_TILE_SIZE = 32;
  23. //! when nr_threads > 1 and round(ohw,nr_threads)>nr_threads,
  24. //! m_oc_tile_size = DEFAULT_OC_TILE_SIZE
  25. static constexpr size_t DEFAULT_OC_TILE_SIZE = 512;
  26. //! when m_oc_tile_size > this value m_oc_tile_size =
  27. //! DEFAULT_OC_MAX_TILE_SIZE
  28. static constexpr size_t DEFAULT_OC_MAX_TILE_SIZE = 1024;
  29. //! when m_oc_tile_size < this value m_oc_tile_size =
  30. //! DEFAULT_OC_MIN_TILE_SIZE the purpose is aligning the calculation
  31. static constexpr size_t DEFAULT_OC_MIN_TILE_SIZE = 128;
  32. fallback::MatrixMulImpl::KernSizeParam get_matmul_kern_param(
  33. const NCBKernSizeParam& param, size_t ohw_tile_size,
  34. size_t oc_tile_size) const;
  35. WorkspaceBundle get_bundle(const NCBKernSizeParam& param) const;
  36. void choice_ohw_oc_block(
  37. const NCBKernSizeParam& param, size_t& oc_tile_size,
  38. size_t& ohw_tile_size, size_t block_m, size_t block_n,
  39. fallback::MatrixMulImpl::AlgoBase::PackMode pack_mode) const;
  40. public:
  41. AlgoIm2col(MatrixMulImpl::AlgoBase* matmul_algo, size_t ohw_tile_size)
  42. : m_matmul_algo(matmul_algo),
  43. m_ohw_tile_size(ohw_tile_size) {}
  44. bool is_reproducible() const override { return true; }
  45. const char* name() const override {
  46. if (m_name.empty()) {
  47. m_name = ssprintf("IM2COLMATMUL:%s:%zu", m_matmul_algo->name(),
  48. m_ohw_tile_size);
  49. }
  50. return m_name.c_str();
  51. }
  52. bool usable(const NCBKernSizeParam& param,
  53. AlgoSelectionStrategy algo_selection_strategy) const override;
  54. size_t get_workspace(const NCBKernSizeParam& param) const override;
  55. SmallVector<NCBKern> dispatch_kerns(
  56. const NCBKernSizeParam& param) const override;
  57. bool is_preferred(
  58. const NCBKernSizeParam& param) const override {
  59. if (param.src_type.category() == DTypeCategory::QUANTIZED) {
  60. static CpuOprDelegationStorage<1> storage;
  61. auto conv_bias_opr = storage.get<ConvBias, 0>();
  62. return static_cast<ConvBiasImpl*>(conv_bias_opr)
  63. ->is_matmul_quantized_prefer(param);
  64. }
  65. auto&& fm = param.filter_meta;
  66. auto OC = fm.ocpg, IC = fm.icpg;
  67. return OC >= 32 || IC >= 32;
  68. }
  69. private:
  70. MatrixMulImpl::AlgoBase* m_matmul_algo;
  71. mutable std::string m_name;
  72. const size_t m_ohw_tile_size;
  73. };
  74. } // namespace fallback
  75. } // namespace megdnn
  76. // vim: syntax=cpp.doxygen

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