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

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

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