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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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-2021 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;
  141. //! A pointer to an object or incomplete type may be converted to a
  142. //! pointer to a different object or incomplete type. If the resulting
  143. //! pointer is not correctly aligned for the pointed-to type, the
  144. //! behavior is undefined.
  145. //!
  146. //! so here we should use memcpy instead of
  147. //! *reinterpret_cast<const T*>(&data[offset]);
  148. memcpy(&ret, data.data() + offset, sizeof(T));
  149. return ret;
  150. }
  151. protected:
  152. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  153. };
  154. /*!
  155. * \brief define Algorithm and ExecutionPolicy for oprs that have
  156. * multiple impl algos
  157. *
  158. * \tparam Opr the operator class
  159. * \tparam nargs number of arguments
  160. */
  161. template <class Opr, int nargs>
  162. class MultiAlgoOpr;
  163. //! base def
  164. template <class Opr>
  165. class MultiAlgoOpr<Opr, -1> {
  166. public:
  167. using AlgorithmInfo = detail::Algorithm::Info;
  168. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  169. using Algorithm = detail::Algorithm;
  170. /*!
  171. * \brief get a string representation for current algorithm set;
  172. *
  173. * get_all_algorithms() may return different algorithms only if
  174. * algorithm set name differs. This is used for checking cache
  175. * validity.
  176. */
  177. virtual const char* get_algorithm_set_name() const = 0;
  178. //! policy for executing the operator
  179. struct ExecutionPolicy {
  180. //! INVALID_ALGO_TYPE algo_type means using heuristic
  181. AlgorithmInfo algo;
  182. };
  183. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  184. const ExecutionPolicy& execution_policy() const {
  185. return m_execution_policy;
  186. }
  187. protected:
  188. ~MultiAlgoOpr() = default;
  189. private:
  190. ExecutionPolicy m_execution_policy;
  191. };
  192. //! specialize for nargs == 3
  193. template <class Opr>
  194. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  195. public:
  196. using Algorithm = detail::Algorithm;
  197. using AlgorithmInfo = detail::Algorithm::Info;
  198. //! get all possible algorithm decriptions for the specified layouts
  199. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  200. const TensorLayout& p1,
  201. const TensorLayout& p2) {
  202. std::vector<AlgorithmInfo> ret;
  203. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  204. ret.emplace_back(algo->info());
  205. }
  206. return ret;
  207. }
  208. /**
  209. * \brief Returns the best algorithm information which indicate the
  210. * algorithm by heuristic.
  211. *
  212. * The selected algorithm should not use workspace more than
  213. * \p workspace_limit_in_bytes.
  214. */
  215. AlgorithmInfo get_algorithm_info_heuristic(
  216. const TensorLayout& p0, const TensorLayout& p1,
  217. const TensorLayout& p2,
  218. size_t workspace_limit_in_bytes =
  219. std::numeric_limits<size_t>::max(),
  220. bool reproducible = false) {
  221. return get_algorithm_heuristic(p0, p1, p2, workspace_limit_in_bytes,
  222. reproducible)
  223. ->info();
  224. }
  225. protected:
  226. ~MultiAlgoOpr() = default;
  227. //! get all possible algorithms for the specified layouts
  228. virtual std::vector<Algorithm*> get_all_algorithms(
  229. const TensorLayout& p0, const TensorLayout& p1,
  230. const TensorLayout& p2) = 0;
  231. /**
  232. * \brief Returns the best algorithm by heuristic.
  233. *
  234. * The selected algorithm should not use workspace more than
  235. * \p workspace_limit_in_bytes.
  236. */
  237. virtual Algorithm* get_algorithm_heuristic(
  238. const TensorLayout& p0, const TensorLayout& p1,
  239. const TensorLayout& p2,
  240. size_t workspace_limit_in_bytes =
  241. std::numeric_limits<size_t>::max(),
  242. bool reproducible = false) = 0;
  243. };
  244. //! specializae for nargs == 4
  245. template <class Opr>
  246. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  247. public:
  248. using Algorithm = detail::Algorithm;
  249. using AlgorithmInfo = detail::Algorithm::Info;
  250. //! get all possible algorithm decriptions for the specified layouts
  251. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  252. const TensorLayout& p1,
  253. const TensorLayout& p2,
  254. const TensorLayout& p3) {
  255. std::vector<AlgorithmInfo> ret;
  256. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  257. ret.emplace_back(algo->info());
  258. }
  259. return ret;
  260. }
  261. /**
  262. * \brief Returns the best algorithm information which indicate the
  263. * algorithm by heuristic.
  264. *
  265. * The selected algorithm should not use workspace more than
  266. * \p workspace_limit_in_bytes.
  267. */
  268. AlgorithmInfo get_algorithm_info_heuristic(
  269. const TensorLayout& p0, const TensorLayout& p1,
  270. const TensorLayout& p2, const TensorLayout& p3,
  271. size_t workspace_limit_in_bytes =
  272. std::numeric_limits<size_t>::max(),
  273. bool reproducible = false) {
  274. return get_algorithm_heuristic(p0, p1, p2, p3, workspace_limit_in_bytes,
  275. reproducible)
  276. ->info();
  277. }
  278. protected:
  279. ~MultiAlgoOpr() = default;
  280. //! get all possible algorithms for the specified layouts
  281. virtual std::vector<Algorithm*> get_all_algorithms(
  282. const TensorLayout& p0, const TensorLayout& p1,
  283. const TensorLayout& p2, const TensorLayout& p3) = 0;
  284. /**
  285. * \brief Returns the best algorithm by heuristic.
  286. *
  287. * The selected algorithm should not use workspace more than
  288. * \p workspace_limit_in_bytes.
  289. */
  290. virtual Algorithm* get_algorithm_heuristic(
  291. const TensorLayout& p0, const TensorLayout& p1,
  292. const TensorLayout& p2, const TensorLayout& p3,
  293. size_t workspace_limit_in_bytes =
  294. std::numeric_limits<size_t>::max(),
  295. bool reproducible = false) = 0;
  296. };
  297. //! specializae for nargs == 5
  298. template <class Opr>
  299. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  300. public:
  301. using Algorithm = detail::Algorithm;
  302. using AlgorithmInfo = detail::Algorithm::Info;
  303. //! get all possible algorithm decriptions for the specified layouts
  304. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  305. const TensorLayout& p1,
  306. const TensorLayout& p2,
  307. const TensorLayout& p3,
  308. const TensorLayout& p4) {
  309. std::vector<AlgorithmInfo> ret;
  310. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  311. ret.emplace_back(algo->info());
  312. }
  313. return ret;
  314. }
  315. /**
  316. * \brief Returns the best algorithm information which indicate the
  317. * algorithm by heuristic.
  318. *
  319. * The selected algorithm should not use workspace more than
  320. * \p workspace_limit_in_bytes.
  321. */
  322. AlgorithmInfo get_algorithm_info_heuristic(
  323. const TensorLayout& p0, const TensorLayout& p1,
  324. const TensorLayout& p2, const TensorLayout& p3,
  325. const TensorLayout& p4,
  326. size_t workspace_limit_in_bytes =
  327. std::numeric_limits<size_t>::max(),
  328. bool reproducible = false) {
  329. return get_algorithm_heuristic(p0, p1, p2, p3, p4,
  330. workspace_limit_in_bytes, reproducible)
  331. ->info();
  332. }
  333. protected:
  334. ~MultiAlgoOpr() = default;
  335. //! get all possible algorithms for the specified layouts
  336. virtual std::vector<Algorithm*> get_all_algorithms(
  337. const TensorLayout& p0, const TensorLayout& p1,
  338. const TensorLayout& p2, const TensorLayout& p3,
  339. const TensorLayout& p4) = 0;
  340. /**
  341. * \brief Returns the best algorithm by heuristic.
  342. *
  343. * The selected algorithm should not use workspace more than
  344. * \p workspace_limit_in_bytes.
  345. */
  346. virtual Algorithm* get_algorithm_heuristic(
  347. const TensorLayout& p0, const TensorLayout& p1,
  348. const TensorLayout& p2, const TensorLayout& p3,
  349. const TensorLayout& p4,
  350. size_t workspace_limit_in_bytes =
  351. std::numeric_limits<size_t>::max(),
  352. bool reproducible = false) = 0;
  353. };
  354. //! specializae for nargs == 8
  355. template <class Opr>
  356. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  357. public:
  358. using Algorithm = detail::Algorithm;
  359. using AlgorithmInfo = detail::Algorithm::Info;
  360. //! get all possible algorithm decriptions for the specified layouts
  361. std::vector<AlgorithmInfo> get_all_algorithms_info(
  362. const TensorLayout& p0, const TensorLayout& p1,
  363. const TensorLayout& p2, const TensorLayout& p3,
  364. const TensorLayout& p4, const TensorLayout& p5,
  365. const TensorLayout& p6, const TensorLayout& p7) {
  366. std::vector<AlgorithmInfo> ret;
  367. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  368. ret.emplace_back(algo->info());
  369. }
  370. return ret;
  371. }
  372. /**
  373. * \brief Returns the best algorithm information which indicate the
  374. * algorithm by heuristic.
  375. *
  376. * The selected algorithm should not use workspace more than
  377. */
  378. AlgorithmInfo get_algorithm_info_heuristic(
  379. const TensorLayout& p0, const TensorLayout& p1,
  380. const TensorLayout& p2, const TensorLayout& p3,
  381. const TensorLayout& p4, const TensorLayout& p5,
  382. const TensorLayout& p6, const TensorLayout& p7,
  383. size_t workspace_limit_in_bytes =
  384. std::numeric_limits<size_t>::max(),
  385. bool reproducible = false) {
  386. return get_algorithm_heuristic(p0, p1, p2, p3, p4, p5, p6, p7,
  387. workspace_limit_in_bytes, reproducible)
  388. ->info();
  389. }
  390. protected:
  391. ~MultiAlgoOpr() = default;
  392. //! get all possible algorithms for the specified layouts
  393. virtual std::vector<Algorithm*> get_all_algorithms(
  394. const TensorLayout& p0, const TensorLayout& p1,
  395. const TensorLayout& p2, const TensorLayout& p3,
  396. const TensorLayout& p4, const TensorLayout& p5,
  397. const TensorLayout& p6, const TensorLayout& p7) = 0;
  398. /**
  399. * \brief Returns the best algorithm by heuristic.
  400. *
  401. * The selected algorithm should not use workspace more than
  402. * \p workspace_limit_in_bytes.
  403. */
  404. virtual Algorithm* get_algorithm_heuristic(
  405. const TensorLayout& p0, const TensorLayout& p1,
  406. const TensorLayout& p2, const TensorLayout& p3,
  407. const TensorLayout& p4, const TensorLayout& p5,
  408. const TensorLayout& p6, const TensorLayout& p7,
  409. size_t workspace_limit_in_bytes =
  410. std::numeric_limits<size_t>::max(),
  411. bool reproducible = false) = 0;
  412. };
  413. } // namespace detail
  414. } // namespace megdnn
  415. #include "megdnn/internal/visibility_epilogue.h"
  416. // vim: syntax=cpp.doxygen

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