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 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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
  10. * implied.
  11. */
  12. #pragma once
  13. #include "megdnn/basic_types.h"
  14. #include "megdnn/handle.h"
  15. #include "megdnn/internal/visibility_prologue.h"
  16. namespace megdnn {
  17. class Handle;
  18. /**
  19. * \brief base class for all operators
  20. *
  21. * This is an helper class. Users should not use OperatorBase directly.
  22. * Operators should be created by handle->create_opr<>().
  23. *
  24. * Each operator must provides the following constexpr values:
  25. *
  26. * * NR_INPUTS: number of input vars
  27. * * NR_OUTPUTS: number of output vars
  28. * * OPERATOR_TYPE: operator type as an enum
  29. *
  30. * If the operator has dynamic inputs or in_out param, the corresponding
  31. * NR_INPUTS is -1.
  32. *
  33. * For an operator whose NR_INPUTS >= 0 and NR_OUTPUTS >= 0, the operator must
  34. * also provide following methods:
  35. *
  36. * * void exec(_megdnn_in inputs..., _megdnn_tensor_out outputs...,
  37. * _megdnn_workspace workspace)
  38. * * void deduce_layout(const TensorLayout& inputs...,
  39. * TensorLayout& outputs...)
  40. * * size_t get_workspace_in_bytes(const TensorLayout &inputs...,
  41. * const TensorLayout &outputs)
  42. */
  43. class OperatorBase {
  44. public:
  45. explicit OperatorBase(Handle* handle) : m_handle(handle) {}
  46. virtual ~OperatorBase();
  47. //! get the handle from which this operator is created
  48. Handle* handle() const { return m_handle; }
  49. //! whether this opr guarantees that its exec() is thread-safe
  50. virtual bool is_thread_safe() const { return false; }
  51. /*!
  52. * \brief set the tracker to be used with MegcoreAsyncErrorInfo
  53. *
  54. * Most operators do not have async errors so this function has a
  55. * default empty implementation.
  56. */
  57. virtual void set_error_tracker(void*) {}
  58. private:
  59. Handle* m_handle;
  60. };
  61. namespace detail {
  62. /**
  63. * \brief AlgoSelectionStrategy is the advance information for selecting
  64. * algo
  65. */
  66. enum class AlgoSelectionStrategy {
  67. HEURISTIC = 0, //!< heristic to select the algos
  68. FAST_RUN = 1,
  69. FULL_RUN = 2,
  70. };
  71. /**
  72. * \brief separate algo by datatype for Matmul and conv
  73. */
  74. enum class AlgoDataType : uint32_t {
  75. FLOAT32 = 1 << 0,
  76. FLOAT16 = 1 << 1,
  77. QINT8X8X32 = 1 << 2,
  78. QUINT8X8X32 = 1 << 3,
  79. INT8X8X16 = 1 << 4,
  80. INT16X16X32 = 1 << 5,
  81. INT4X4X16 = 1 << 6,
  82. };
  83. /*!
  84. * \brief Abstract representation of an algorithm for implementing
  85. * the operator
  86. */
  87. class Algorithm {
  88. public:
  89. static constexpr uint32_t INVALID_ALGO_TYPE = static_cast<uint32_t>(-1);
  90. /**
  91. * \brief Algorithm information, we can get real algo from
  92. * AlgorithmInfo::Info::Desc
  93. */
  94. struct Info {
  95. struct Desc {
  96. //! backend of the algo belonging to
  97. Handle::HandleType handle_type;
  98. //! indicate the real algo implementation
  99. uint32_t type = INVALID_ALGO_TYPE;
  100. //! serialized param of the algo type
  101. std::string param;
  102. bool valid() const { return type != INVALID_ALGO_TYPE; }
  103. void reset() { type = INVALID_ALGO_TYPE; }
  104. bool operator==(const Desc& rhs) const {
  105. return handle_type == rhs.handle_type && type == rhs.type &&
  106. param == rhs.param;
  107. }
  108. } desc;
  109. //! algorithm name
  110. std::string name;
  111. bool is_reproducible;
  112. bool valid() const { return desc.valid(); }
  113. void reset() { desc.reset(); }
  114. //! desc donate the algo
  115. bool operator==(const Info& rhs) const { return desc == rhs.desc; }
  116. };
  117. virtual ~Algorithm() = default;
  118. /**
  119. * \brief whether the execution result is
  120. * reproducible across multiple runs.
  121. */
  122. virtual bool is_reproducible() const = 0;
  123. virtual const char* name() const = 0;
  124. //! serialized param
  125. virtual std::string param() const { return {}; }
  126. virtual uint32_t type() const = 0;
  127. Handle::HandleType handle_type() const { return m_handle_type; }
  128. Info info() const {
  129. return {{handle_type(), type(), param()}, name(), is_reproducible()};
  130. }
  131. template <typename T>
  132. static void serialize_write_pod(const T& val, std::string& result) {
  133. result.append(reinterpret_cast<const char*>(&val), sizeof(T));
  134. }
  135. static void serialize_write_pod(const char* val, std::string& result) {
  136. result.append(val, strlen(val));
  137. }
  138. template <typename T>
  139. static T deserialize_read_pod(const std::string& data, size_t offset = 0) {
  140. T ret = *reinterpret_cast<const T*>(&data[offset]);
  141. return ret;
  142. }
  143. protected:
  144. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  145. };
  146. /*!
  147. * \brief define Algorithm and ExecutionPolicy for oprs that have
  148. * multiple impl algos
  149. *
  150. * \tparam Opr the operator class
  151. * \tparam nargs number of arguments
  152. */
  153. template <class Opr, int nargs>
  154. class MultiAlgoOpr;
  155. //! base def
  156. template <class Opr>
  157. class MultiAlgoOpr<Opr, -1> {
  158. public:
  159. using AlgorithmInfo = detail::Algorithm::Info;
  160. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  161. using Algorithm = detail::Algorithm;
  162. /*!
  163. * \brief get a string representation for current algorithm set;
  164. *
  165. * get_all_algorithms() may return different algorithms only if
  166. * algorithm set name differs. This is used for checking cache
  167. * validity.
  168. */
  169. virtual const char* get_algorithm_set_name() const = 0;
  170. //! policy for executing the operator
  171. struct ExecutionPolicy {
  172. //! INVALID_ALGO_TYPE algo_type means using heuristic
  173. AlgorithmInfo algo;
  174. };
  175. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  176. const ExecutionPolicy& execution_policy() const {
  177. return m_execution_policy;
  178. }
  179. protected:
  180. ~MultiAlgoOpr() = default;
  181. private:
  182. ExecutionPolicy m_execution_policy;
  183. };
  184. //! specialize for nargs == 3
  185. template <class Opr>
  186. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  187. public:
  188. using Algorithm = detail::Algorithm;
  189. using AlgorithmInfo = detail::Algorithm::Info;
  190. //! get all possible algorithm decriptions for the specified layouts
  191. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  192. const TensorLayout& p1,
  193. const TensorLayout& p2) {
  194. std::vector<AlgorithmInfo> ret;
  195. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  196. ret.emplace_back(algo->info());
  197. }
  198. return ret;
  199. }
  200. /**
  201. * \brief Returns the best algorithm information which indicate the
  202. * algorithm by heuristic.
  203. *
  204. * The selected algorithm should not use workspace more than
  205. * \p workspace_limit_in_bytes.
  206. */
  207. AlgorithmInfo get_algorithm_info_heuristic(
  208. const TensorLayout& p0, const TensorLayout& p1,
  209. const TensorLayout& p2,
  210. size_t workspace_limit_in_bytes =
  211. std::numeric_limits<size_t>::max(),
  212. bool reproducible = false) {
  213. return get_algorithm_heuristic(p0, p1, p2, workspace_limit_in_bytes,
  214. reproducible)
  215. ->info();
  216. }
  217. protected:
  218. ~MultiAlgoOpr() = default;
  219. //! get all possible algorithms for the specified layouts
  220. virtual std::vector<Algorithm*> get_all_algorithms(
  221. const TensorLayout& p0, const TensorLayout& p1,
  222. const TensorLayout& p2) = 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,
  232. size_t workspace_limit_in_bytes =
  233. std::numeric_limits<size_t>::max(),
  234. bool reproducible = false) = 0;
  235. };
  236. //! specializae for nargs == 4
  237. template <class Opr>
  238. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  239. public:
  240. using Algorithm = detail::Algorithm;
  241. using AlgorithmInfo = detail::Algorithm::Info;
  242. //! get all possible algorithm decriptions for the specified layouts
  243. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  244. const TensorLayout& p1,
  245. const TensorLayout& p2,
  246. const TensorLayout& p3) {
  247. std::vector<AlgorithmInfo> ret;
  248. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  249. ret.emplace_back(algo->info());
  250. }
  251. return ret;
  252. }
  253. /**
  254. * \brief Returns the best algorithm information which indicate the
  255. * algorithm by heuristic.
  256. *
  257. * The selected algorithm should not use workspace more than
  258. * \p workspace_limit_in_bytes.
  259. */
  260. AlgorithmInfo get_algorithm_info_heuristic(
  261. const TensorLayout& p0, const TensorLayout& p1,
  262. const TensorLayout& p2, const TensorLayout& p3,
  263. size_t workspace_limit_in_bytes =
  264. std::numeric_limits<size_t>::max(),
  265. bool reproducible = false) {
  266. return get_algorithm_heuristic(p0, p1, p2, p3, workspace_limit_in_bytes,
  267. reproducible)
  268. ->info();
  269. }
  270. protected:
  271. ~MultiAlgoOpr() = default;
  272. //! get all possible algorithms for the specified layouts
  273. virtual std::vector<Algorithm*> get_all_algorithms(
  274. const TensorLayout& p0, const TensorLayout& p1,
  275. const TensorLayout& p2, const TensorLayout& p3) = 0;
  276. /**
  277. * \brief Returns the best algorithm by heuristic.
  278. *
  279. * The selected algorithm should not use workspace more than
  280. * \p workspace_limit_in_bytes.
  281. */
  282. virtual Algorithm* get_algorithm_heuristic(
  283. const TensorLayout& p0, const TensorLayout& p1,
  284. const TensorLayout& p2, const TensorLayout& p3,
  285. size_t workspace_limit_in_bytes =
  286. std::numeric_limits<size_t>::max(),
  287. bool reproducible = false) = 0;
  288. };
  289. //! specializae for nargs == 5
  290. template <class Opr>
  291. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  292. public:
  293. using Algorithm = detail::Algorithm;
  294. using AlgorithmInfo = detail::Algorithm::Info;
  295. //! get all possible algorithm decriptions for the specified layouts
  296. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  297. const TensorLayout& p1,
  298. const TensorLayout& p2,
  299. const TensorLayout& p3,
  300. const TensorLayout& p4) {
  301. std::vector<AlgorithmInfo> ret;
  302. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  303. ret.emplace_back(algo->info());
  304. }
  305. return ret;
  306. }
  307. /**
  308. * \brief Returns the best algorithm information which indicate the
  309. * algorithm by heuristic.
  310. *
  311. * The selected algorithm should not use workspace more than
  312. * \p workspace_limit_in_bytes.
  313. */
  314. AlgorithmInfo get_algorithm_info_heuristic(
  315. const TensorLayout& p0, const TensorLayout& p1,
  316. const TensorLayout& p2, const TensorLayout& p3,
  317. const TensorLayout& p4,
  318. size_t workspace_limit_in_bytes =
  319. std::numeric_limits<size_t>::max(),
  320. bool reproducible = false) {
  321. return get_algorithm_heuristic(p0, p1, p2, p3, p4,
  322. workspace_limit_in_bytes, reproducible)
  323. ->info();
  324. }
  325. protected:
  326. ~MultiAlgoOpr() = default;
  327. //! get all possible algorithms for the specified layouts
  328. virtual std::vector<Algorithm*> get_all_algorithms(
  329. const TensorLayout& p0, const TensorLayout& p1,
  330. const TensorLayout& p2, const TensorLayout& p3,
  331. const TensorLayout& p4) = 0;
  332. /**
  333. * \brief Returns the best algorithm by heuristic.
  334. *
  335. * The selected algorithm should not use workspace more than
  336. * \p workspace_limit_in_bytes.
  337. */
  338. virtual Algorithm* get_algorithm_heuristic(
  339. const TensorLayout& p0, const TensorLayout& p1,
  340. const TensorLayout& p2, const TensorLayout& p3,
  341. const TensorLayout& p4,
  342. size_t workspace_limit_in_bytes =
  343. std::numeric_limits<size_t>::max(),
  344. bool reproducible = false) = 0;
  345. };
  346. //! specializae for nargs == 8
  347. template <class Opr>
  348. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  349. public:
  350. using Algorithm = detail::Algorithm;
  351. using AlgorithmInfo = detail::Algorithm::Info;
  352. //! get all possible algorithm decriptions for the specified layouts
  353. std::vector<AlgorithmInfo> get_all_algorithms_info(
  354. const TensorLayout& p0, const TensorLayout& p1,
  355. const TensorLayout& p2, const TensorLayout& p3,
  356. const TensorLayout& p4, const TensorLayout& p5,
  357. const TensorLayout& p6, const TensorLayout& p7) {
  358. std::vector<AlgorithmInfo> ret;
  359. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  360. ret.emplace_back(algo->info());
  361. }
  362. return ret;
  363. }
  364. /**
  365. * \brief Returns the best algorithm information which indicate the
  366. * algorithm by heuristic.
  367. *
  368. * The selected algorithm should not use workspace more than
  369. */
  370. AlgorithmInfo get_algorithm_info_heuristic(
  371. const TensorLayout& p0, const TensorLayout& p1,
  372. const TensorLayout& p2, const TensorLayout& p3,
  373. const TensorLayout& p4, const TensorLayout& p5,
  374. const TensorLayout& p6, const TensorLayout& p7,
  375. size_t workspace_limit_in_bytes =
  376. std::numeric_limits<size_t>::max(),
  377. bool reproducible = false) {
  378. return get_algorithm_heuristic(p0, p1, p2, p3, p4, p5, p6, p7,
  379. workspace_limit_in_bytes, reproducible)
  380. ->info();
  381. }
  382. protected:
  383. ~MultiAlgoOpr() = default;
  384. //! get all possible algorithms for the specified layouts
  385. virtual std::vector<Algorithm*> get_all_algorithms(
  386. const TensorLayout& p0, const TensorLayout& p1,
  387. const TensorLayout& p2, const TensorLayout& p3,
  388. const TensorLayout& p4, const TensorLayout& p5,
  389. const TensorLayout& p6, const TensorLayout& p7) = 0;
  390. /**
  391. * \brief Returns the best algorithm by heuristic.
  392. *
  393. * The selected algorithm should not use workspace more than
  394. * \p workspace_limit_in_bytes.
  395. */
  396. virtual Algorithm* get_algorithm_heuristic(
  397. const TensorLayout& p0, const TensorLayout& p1,
  398. const TensorLayout& p2, const TensorLayout& p3,
  399. const TensorLayout& p4, const TensorLayout& p5,
  400. const TensorLayout& p6, const TensorLayout& p7,
  401. size_t workspace_limit_in_bytes =
  402. std::numeric_limits<size_t>::max(),
  403. bool reproducible = false) = 0;
  404. };
  405. } // namespace detail
  406. } // namespace megdnn
  407. #include "megdnn/internal/visibility_epilogue.h"
  408. // vim: syntax=cpp.doxygen

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