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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. virtual Algorithm* get_algorithm_from_desc(const AlgorithmDesc&) = 0;
  188. protected:
  189. ~MultiAlgoOpr() = default;
  190. private:
  191. ExecutionPolicy m_execution_policy;
  192. };
  193. //! specialize for nargs == 3
  194. template <class Opr>
  195. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  196. public:
  197. using Algorithm = detail::Algorithm;
  198. using AlgorithmInfo = detail::Algorithm::Info;
  199. //! get all possible algorithm decriptions for the specified layouts
  200. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  201. const TensorLayout& p1,
  202. const TensorLayout& p2) {
  203. std::vector<AlgorithmInfo> ret;
  204. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  205. ret.emplace_back(algo->info());
  206. }
  207. return ret;
  208. }
  209. /**
  210. * \brief Returns the best algorithm information which indicate the
  211. * algorithm by heuristic.
  212. *
  213. * The selected algorithm should not use workspace more than
  214. * \p workspace_limit_in_bytes.
  215. */
  216. AlgorithmInfo get_algorithm_info_heuristic(
  217. const TensorLayout& p0, const TensorLayout& p1,
  218. const TensorLayout& p2,
  219. size_t workspace_limit_in_bytes =
  220. std::numeric_limits<size_t>::max(),
  221. bool reproducible = false) {
  222. return get_algorithm_heuristic(p0, p1, p2, workspace_limit_in_bytes,
  223. reproducible)
  224. ->info();
  225. }
  226. protected:
  227. ~MultiAlgoOpr() = default;
  228. //! get all possible algorithms for the specified layouts
  229. virtual std::vector<Algorithm*> get_all_algorithms(
  230. const TensorLayout& p0, const TensorLayout& p1,
  231. const TensorLayout& p2) = 0;
  232. /**
  233. * \brief Returns the best algorithm by heuristic.
  234. *
  235. * The selected algorithm should not use workspace more than
  236. * \p workspace_limit_in_bytes.
  237. */
  238. virtual Algorithm* get_algorithm_heuristic(
  239. const TensorLayout& p0, const TensorLayout& p1,
  240. const TensorLayout& p2,
  241. size_t workspace_limit_in_bytes =
  242. std::numeric_limits<size_t>::max(),
  243. bool reproducible = false) = 0;
  244. };
  245. //! specializae for nargs == 4
  246. template <class Opr>
  247. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  248. public:
  249. using Algorithm = detail::Algorithm;
  250. using AlgorithmInfo = detail::Algorithm::Info;
  251. //! get all possible algorithm decriptions for the specified layouts
  252. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  253. const TensorLayout& p1,
  254. const TensorLayout& p2,
  255. const TensorLayout& p3) {
  256. std::vector<AlgorithmInfo> ret;
  257. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  258. ret.emplace_back(algo->info());
  259. }
  260. return ret;
  261. }
  262. /**
  263. * \brief Returns the best algorithm information which indicate the
  264. * algorithm by heuristic.
  265. *
  266. * The selected algorithm should not use workspace more than
  267. * \p workspace_limit_in_bytes.
  268. */
  269. AlgorithmInfo get_algorithm_info_heuristic(
  270. const TensorLayout& p0, const TensorLayout& p1,
  271. const TensorLayout& p2, const TensorLayout& p3,
  272. size_t workspace_limit_in_bytes =
  273. std::numeric_limits<size_t>::max(),
  274. bool reproducible = false) {
  275. return get_algorithm_heuristic(p0, p1, p2, p3, workspace_limit_in_bytes,
  276. reproducible)
  277. ->info();
  278. }
  279. protected:
  280. ~MultiAlgoOpr() = default;
  281. //! get all possible algorithms for the specified layouts
  282. virtual std::vector<Algorithm*> get_all_algorithms(
  283. const TensorLayout& p0, const TensorLayout& p1,
  284. const TensorLayout& p2, const TensorLayout& p3) = 0;
  285. /**
  286. * \brief Returns the best algorithm by heuristic.
  287. *
  288. * The selected algorithm should not use workspace more than
  289. * \p workspace_limit_in_bytes.
  290. */
  291. virtual Algorithm* get_algorithm_heuristic(
  292. const TensorLayout& p0, const TensorLayout& p1,
  293. const TensorLayout& p2, const TensorLayout& p3,
  294. size_t workspace_limit_in_bytes =
  295. std::numeric_limits<size_t>::max(),
  296. bool reproducible = false) = 0;
  297. };
  298. //! specializae for nargs == 5
  299. template <class Opr>
  300. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  301. public:
  302. using Algorithm = detail::Algorithm;
  303. using AlgorithmInfo = detail::Algorithm::Info;
  304. //! get all possible algorithm decriptions for the specified layouts
  305. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  306. const TensorLayout& p1,
  307. const TensorLayout& p2,
  308. const TensorLayout& p3,
  309. const TensorLayout& p4) {
  310. std::vector<AlgorithmInfo> ret;
  311. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  312. ret.emplace_back(algo->info());
  313. }
  314. return ret;
  315. }
  316. /**
  317. * \brief Returns the best algorithm information which indicate the
  318. * algorithm by heuristic.
  319. *
  320. * The selected algorithm should not use workspace more than
  321. * \p workspace_limit_in_bytes.
  322. */
  323. AlgorithmInfo get_algorithm_info_heuristic(
  324. const TensorLayout& p0, const TensorLayout& p1,
  325. const TensorLayout& p2, const TensorLayout& p3,
  326. const TensorLayout& p4,
  327. size_t workspace_limit_in_bytes =
  328. std::numeric_limits<size_t>::max(),
  329. bool reproducible = false) {
  330. return get_algorithm_heuristic(p0, p1, p2, p3, p4,
  331. workspace_limit_in_bytes, reproducible)
  332. ->info();
  333. }
  334. protected:
  335. ~MultiAlgoOpr() = default;
  336. //! get all possible algorithms for the specified layouts
  337. virtual std::vector<Algorithm*> get_all_algorithms(
  338. const TensorLayout& p0, const TensorLayout& p1,
  339. const TensorLayout& p2, const TensorLayout& p3,
  340. const TensorLayout& p4) = 0;
  341. /**
  342. * \brief Returns the best algorithm by heuristic.
  343. *
  344. * The selected algorithm should not use workspace more than
  345. * \p workspace_limit_in_bytes.
  346. */
  347. virtual Algorithm* get_algorithm_heuristic(
  348. const TensorLayout& p0, const TensorLayout& p1,
  349. const TensorLayout& p2, const TensorLayout& p3,
  350. const TensorLayout& p4,
  351. size_t workspace_limit_in_bytes =
  352. std::numeric_limits<size_t>::max(),
  353. bool reproducible = false) = 0;
  354. };
  355. //! specializae for nargs == 8
  356. template <class Opr>
  357. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  358. public:
  359. using Algorithm = detail::Algorithm;
  360. using AlgorithmInfo = detail::Algorithm::Info;
  361. //! get all possible algorithm decriptions for the specified layouts
  362. std::vector<AlgorithmInfo> get_all_algorithms_info(
  363. const TensorLayout& p0, const TensorLayout& p1,
  364. const TensorLayout& p2, const TensorLayout& p3,
  365. const TensorLayout& p4, const TensorLayout& p5,
  366. const TensorLayout& p6, const TensorLayout& p7) {
  367. std::vector<AlgorithmInfo> ret;
  368. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  369. ret.emplace_back(algo->info());
  370. }
  371. return ret;
  372. }
  373. /**
  374. * \brief Returns the best algorithm information which indicate the
  375. * algorithm by heuristic.
  376. *
  377. * The selected algorithm should not use workspace more than
  378. */
  379. AlgorithmInfo get_algorithm_info_heuristic(
  380. const TensorLayout& p0, const TensorLayout& p1,
  381. const TensorLayout& p2, const TensorLayout& p3,
  382. const TensorLayout& p4, const TensorLayout& p5,
  383. const TensorLayout& p6, const TensorLayout& p7,
  384. size_t workspace_limit_in_bytes =
  385. std::numeric_limits<size_t>::max(),
  386. bool reproducible = false) {
  387. return get_algorithm_heuristic(p0, p1, p2, p3, p4, p5, p6, p7,
  388. workspace_limit_in_bytes, reproducible)
  389. ->info();
  390. }
  391. protected:
  392. ~MultiAlgoOpr() = default;
  393. //! get all possible algorithms for the specified layouts
  394. virtual std::vector<Algorithm*> get_all_algorithms(
  395. const TensorLayout& p0, const TensorLayout& p1,
  396. const TensorLayout& p2, const TensorLayout& p3,
  397. const TensorLayout& p4, const TensorLayout& p5,
  398. const TensorLayout& p6, const TensorLayout& p7) = 0;
  399. /**
  400. * \brief Returns the best algorithm by heuristic.
  401. *
  402. * The selected algorithm should not use workspace more than
  403. * \p workspace_limit_in_bytes.
  404. */
  405. virtual Algorithm* get_algorithm_heuristic(
  406. const TensorLayout& p0, const TensorLayout& p1,
  407. const TensorLayout& p2, const TensorLayout& p3,
  408. const TensorLayout& p4, const TensorLayout& p5,
  409. const TensorLayout& p6, const TensorLayout& p7,
  410. size_t workspace_limit_in_bytes =
  411. std::numeric_limits<size_t>::max(),
  412. bool reproducible = false) = 0;
  413. };
  414. } // namespace detail
  415. } // namespace megdnn
  416. #include "megdnn/internal/visibility_epilogue.h"
  417. // vim: syntax=cpp.doxygen

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