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.

base.h 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * \file dnn/include/megdnn/oprs/base.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/basic_types.h"
  13. #include "megdnn/handle.h"
  14. #include "megdnn/internal/visibility_prologue.h"
  15. namespace megdnn {
  16. class Handle;
  17. /**
  18. * \brief base class for all operators
  19. *
  20. * This is an helper class. Users should not use OperatorBase directly.
  21. * Operators should be created by handle->create_opr<>().
  22. *
  23. * Each operator must provides the following constexpr values:
  24. *
  25. * * NR_INPUTS: number of input vars
  26. * * NR_OUTPUTS: number of output vars
  27. * * OPERATOR_TYPE: operator type as an enum
  28. *
  29. * If the operator has dynamic inputs or in_out param, the corresponding
  30. * NR_INPUTS is -1.
  31. *
  32. * For an operator whose NR_INPUTS >= 0 and NR_OUTPUTS >= 0, the operator must
  33. * also provide following methods:
  34. *
  35. * * void exec(_megdnn_in inputs..., _megdnn_tensor_out outputs...,
  36. * _megdnn_workspace workspace)
  37. * * void deduce_layout(const TensorLayout& inputs...,
  38. * TensorLayout& outputs...)
  39. * * size_t get_workspace_in_bytes(const TensorLayout &inputs...,
  40. * const TensorLayout &outputs)
  41. */
  42. class OperatorBase {
  43. public:
  44. explicit OperatorBase(Handle* handle) : m_handle(handle) {}
  45. virtual ~OperatorBase();
  46. //! get the handle from which this operator is created
  47. Handle* handle() const { return m_handle; }
  48. //! whether this opr guarantees that its exec() is thread-safe
  49. virtual bool is_thread_safe() const { return false; }
  50. /*!
  51. * \brief set the tracker to be used with MegcoreAsyncErrorInfo
  52. *
  53. * Most operators do not have async errors so this function has a
  54. * default empty implementation.
  55. */
  56. virtual void set_error_tracker(void*) {}
  57. private:
  58. Handle* m_handle;
  59. };
  60. namespace detail {
  61. /**
  62. * \brief AlgoSelectionStrategy is the advance information for selecting
  63. * algo
  64. */
  65. enum class AlgoSelectionStrategy {
  66. HEURISTIC = 0, //!< heristic to select the algos
  67. FAST_RUN = 1,
  68. FULL_RUN = 2,
  69. };
  70. /**
  71. * \brief separate algo by datatype for Matmul and conv
  72. */
  73. enum class AlgoDataType : uint32_t {
  74. FLOAT32 = 1 << 0,
  75. FLOAT16 = 1 << 1,
  76. QINT8X8X32 = 1 << 2,
  77. QUINT8X8X32 = 1 << 3,
  78. INT8X8X16 = 1 << 4,
  79. INT16X16X32 = 1 << 5,
  80. };
  81. /*!
  82. * \brief Abstract representation of an algorithm for implementing
  83. * the operator
  84. *
  85. * All pointers to Algorithm should be allocated globally and usable
  86. * across multiple megdnn handles, and they should not be freed by
  87. * the caller.
  88. */
  89. class Algorithm {
  90. public:
  91. /**
  92. * \brief whether the execution result is
  93. * reproducible across multiple runs.
  94. */
  95. virtual bool is_reproducible() const = 0;
  96. virtual const char* name() const = 0;
  97. Handle::HandleType handle_type() const { return m_handle_type; }
  98. protected:
  99. ~Algorithm() = default;
  100. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  101. };
  102. /*!
  103. * \brief define Algorithm and ExecutionPolicy for oprs that have
  104. * multiple impl algos
  105. *
  106. * \tparam Opr the operator class
  107. * \tparam nargs number of arguments
  108. */
  109. template <class Opr, int nargs>
  110. class MultiAlgoOpr;
  111. //! base def
  112. template <class Opr>
  113. class MultiAlgoOpr<Opr, -1> {
  114. public:
  115. using Algorithm = detail::Algorithm;
  116. /*!
  117. * \brief get a string representation for current algorithm set;
  118. *
  119. * get_all_algorithms() may return different algorithms only if
  120. * algorithm set name differs. This is used for checking cache
  121. * validity.
  122. */
  123. virtual const char* get_algorithm_set_name() const = 0;
  124. //! policy for executing the operator
  125. struct ExecutionPolicy {
  126. //! nullptr means using heuristic
  127. Algorithm* algorithm = nullptr;
  128. };
  129. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  130. const ExecutionPolicy& execution_policy() const {
  131. return m_execution_policy;
  132. }
  133. protected:
  134. ~MultiAlgoOpr() = default;
  135. private:
  136. ExecutionPolicy m_execution_policy;
  137. };
  138. //! specialize for nargs == 3
  139. template <class Opr>
  140. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  141. public:
  142. using Algorithm = detail::Algorithm;
  143. //! get all possible algorithms for the specified layouts
  144. virtual std::vector<Algorithm*> get_all_algorithms(
  145. const TensorLayout& p0, const TensorLayout& p1,
  146. const TensorLayout& p2) = 0;
  147. /**
  148. * \brief Returns the best algorithm by heuristic.
  149. *
  150. * The selected algorithm should not use workspace more than
  151. * \p workspace_limit_in_bytes.
  152. */
  153. virtual Algorithm* get_algorithm_heuristic(
  154. const TensorLayout& p0, const TensorLayout& p1,
  155. const TensorLayout& p2,
  156. size_t workspace_limit_in_bytes =
  157. std::numeric_limits<size_t>::max(),
  158. bool reproducible = false) = 0;
  159. protected:
  160. ~MultiAlgoOpr() = default;
  161. };
  162. //! specializae for nargs == 4
  163. template <class Opr>
  164. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  165. public:
  166. using Algorithm = detail::Algorithm;
  167. //! get all possible algorithms for the specified layouts
  168. virtual std::vector<Algorithm*> get_all_algorithms(
  169. const TensorLayout& p0, const TensorLayout& p1,
  170. const TensorLayout& p2, const TensorLayout& p3) = 0;
  171. /**
  172. * \brief Returns the best algorithm by heuristic.
  173. *
  174. * The selected algorithm should not use workspace more than
  175. * \p workspace_limit_in_bytes.
  176. */
  177. virtual Algorithm* get_algorithm_heuristic(
  178. const TensorLayout& p0, const TensorLayout& p1,
  179. const TensorLayout& p2, const TensorLayout& p3,
  180. size_t workspace_limit_in_bytes =
  181. std::numeric_limits<size_t>::max(),
  182. bool reproducible = false) = 0;
  183. protected:
  184. ~MultiAlgoOpr() = default;
  185. };
  186. //! specializae for nargs == 5
  187. template <class Opr>
  188. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  189. public:
  190. using Algorithm = detail::Algorithm;
  191. //! get all possible algorithms for the specified layouts
  192. virtual std::vector<Algorithm*> get_all_algorithms(
  193. const TensorLayout& p0, const TensorLayout& p1,
  194. const TensorLayout& p2, const TensorLayout& p3,
  195. const TensorLayout& p4) = 0;
  196. /**
  197. * \brief Returns the best algorithm by heuristic.
  198. *
  199. * The selected algorithm should not use workspace more than
  200. * \p workspace_limit_in_bytes.
  201. */
  202. virtual Algorithm* get_algorithm_heuristic(
  203. const TensorLayout& p0, const TensorLayout& p1,
  204. const TensorLayout& p2, const TensorLayout& p3,
  205. const TensorLayout& p4,
  206. size_t workspace_limit_in_bytes =
  207. std::numeric_limits<size_t>::max(),
  208. bool reproducible = false) = 0;
  209. protected:
  210. ~MultiAlgoOpr() = default;
  211. };
  212. //! specializae for nargs == 8
  213. template <class Opr>
  214. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  215. public:
  216. using Algorithm = detail::Algorithm;
  217. //! get all possible algorithms for the specified layouts
  218. virtual std::vector<Algorithm*> get_all_algorithms(
  219. const TensorLayout& p0, const TensorLayout& p1,
  220. const TensorLayout& p2, const TensorLayout& p3,
  221. const TensorLayout& p4, const TensorLayout& p5,
  222. const TensorLayout& p6, const TensorLayout& p7) = 0;
  223. /**
  224. * \brief Returns the best algorithm by heuristic.
  225. *
  226. * The selected algorithm should not use workspace more than
  227. * \p workspace_limit_in_bytes.
  228. */
  229. virtual Algorithm* get_algorithm_heuristic(
  230. const TensorLayout& p0, const TensorLayout& p1,
  231. const TensorLayout& p2, const TensorLayout& p3,
  232. const TensorLayout& p4, const TensorLayout& p5,
  233. const TensorLayout& p6, const TensorLayout& p7,
  234. size_t workspace_limit_in_bytes =
  235. std::numeric_limits<size_t>::max(),
  236. bool reproducible = false) = 0;
  237. protected:
  238. ~MultiAlgoOpr() = default;
  239. };
  240. } // namespace detail
  241. } // namespace megdnn
  242. #include "megdnn/internal/visibility_epilogue.h"
  243. // vim: syntax=cpp.doxygen

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