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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Abstract representation of an algorithm for implementing
  71. * the operator
  72. *
  73. * All pointers to Algorithm should be allocated globally and usable
  74. * across multiple megdnn handles, and they should not be freed by
  75. * the caller.
  76. */
  77. class Algorithm {
  78. public:
  79. /**
  80. * \brief whether the execution result is
  81. * reproducible across multiple runs.
  82. */
  83. virtual bool is_reproducible() const = 0;
  84. virtual const char* name() const = 0;
  85. //! a pointer to represent class type
  86. virtual void* type() const { return nullptr; }
  87. protected:
  88. ~Algorithm() = default;
  89. };
  90. /*!
  91. * \brief define Algorithm and ExecutionPolicy for oprs that have
  92. * multiple impl algos
  93. *
  94. * \tparam Opr the operator class
  95. * \tparam nargs number of arguments
  96. */
  97. template <class Opr, int nargs>
  98. class MultiAlgoOpr;
  99. //! base def
  100. template <class Opr>
  101. class MultiAlgoOpr<Opr, -1> {
  102. public:
  103. using Algorithm = detail::Algorithm;
  104. /*!
  105. * \brief get a string representation for current algorithm set;
  106. *
  107. * get_all_algorithms() may return different algorithms only if
  108. * algorithm set name differs. This is used for checking cache
  109. * validity.
  110. */
  111. virtual const char* get_algorithm_set_name() const = 0;
  112. //! policy for executing the operator
  113. struct ExecutionPolicy {
  114. //! nullptr means using heuristic
  115. Algorithm* algorithm = nullptr;
  116. };
  117. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  118. const ExecutionPolicy& execution_policy() const {
  119. return m_execution_policy;
  120. }
  121. protected:
  122. ~MultiAlgoOpr() = default;
  123. private:
  124. ExecutionPolicy m_execution_policy;
  125. };
  126. //! specialize for nargs == 3
  127. template <class Opr>
  128. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  129. public:
  130. using Algorithm = detail::Algorithm;
  131. //! get all possible algorithms for the specified layouts
  132. virtual std::vector<Algorithm*> get_all_algorithms(
  133. const TensorLayout& p0, const TensorLayout& p1,
  134. const TensorLayout& p2) = 0;
  135. /**
  136. * \brief Returns the best algorithm by heuristic.
  137. *
  138. * The selected algorithm should not use workspace more than
  139. * \p workspace_limit_in_bytes.
  140. */
  141. virtual Algorithm* get_algorithm_heuristic(
  142. const TensorLayout& p0, const TensorLayout& p1,
  143. const TensorLayout& p2,
  144. size_t workspace_limit_in_bytes =
  145. std::numeric_limits<size_t>::max(),
  146. bool reproducible = false) = 0;
  147. protected:
  148. ~MultiAlgoOpr() = default;
  149. };
  150. //! specializae for nargs == 4
  151. template <class Opr>
  152. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  153. public:
  154. using Algorithm = detail::Algorithm;
  155. //! get all possible algorithms for the specified layouts
  156. virtual std::vector<Algorithm*> get_all_algorithms(
  157. const TensorLayout& p0, const TensorLayout& p1,
  158. const TensorLayout& p2, const TensorLayout& p3) = 0;
  159. /**
  160. * \brief Returns the best algorithm by heuristic.
  161. *
  162. * The selected algorithm should not use workspace more than
  163. * \p workspace_limit_in_bytes.
  164. */
  165. virtual Algorithm* get_algorithm_heuristic(
  166. const TensorLayout& p0, const TensorLayout& p1,
  167. const TensorLayout& p2, const TensorLayout& p3,
  168. size_t workspace_limit_in_bytes =
  169. std::numeric_limits<size_t>::max(),
  170. bool reproducible = false) = 0;
  171. protected:
  172. ~MultiAlgoOpr() = default;
  173. };
  174. //! specializae for nargs == 5
  175. template <class Opr>
  176. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  177. public:
  178. using Algorithm = detail::Algorithm;
  179. //! get all possible algorithms for the specified layouts
  180. virtual std::vector<Algorithm*> get_all_algorithms(
  181. const TensorLayout& p0, const TensorLayout& p1,
  182. const TensorLayout& p2, const TensorLayout& p3,
  183. const TensorLayout& p4) = 0;
  184. /**
  185. * \brief Returns the best algorithm by heuristic.
  186. *
  187. * The selected algorithm should not use workspace more than
  188. * \p workspace_limit_in_bytes.
  189. */
  190. virtual Algorithm* get_algorithm_heuristic(
  191. const TensorLayout& p0, const TensorLayout& p1,
  192. const TensorLayout& p2, const TensorLayout& p3,
  193. const TensorLayout& p4,
  194. size_t workspace_limit_in_bytes =
  195. std::numeric_limits<size_t>::max(),
  196. bool reproducible = false) = 0;
  197. protected:
  198. ~MultiAlgoOpr() = default;
  199. };
  200. //! specializae for nargs == 8
  201. template <class Opr>
  202. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  203. public:
  204. using Algorithm = detail::Algorithm;
  205. //! get all possible algorithms for the specified layouts
  206. virtual std::vector<Algorithm*> get_all_algorithms(
  207. const TensorLayout& p0, const TensorLayout& p1,
  208. const TensorLayout& p2, const TensorLayout& p3,
  209. const TensorLayout& p4, const TensorLayout& p5,
  210. const TensorLayout& p6, const TensorLayout& p7) = 0;
  211. /**
  212. * \brief Returns the best algorithm by heuristic.
  213. *
  214. * The selected algorithm should not use workspace more than
  215. * \p workspace_limit_in_bytes.
  216. */
  217. virtual Algorithm* get_algorithm_heuristic(
  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,
  222. size_t workspace_limit_in_bytes =
  223. std::numeric_limits<size_t>::max(),
  224. bool reproducible = false) = 0;
  225. protected:
  226. ~MultiAlgoOpr() = default;
  227. };
  228. } // namespace detail
  229. } // namespace megdnn
  230. #include "megdnn/internal/visibility_epilogue.h"
  231. // vim: syntax=cpp.doxygen

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

Contributors (1)