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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 <type_traits>
  14. #include "megdnn/basic_types.h"
  15. #include "megdnn/handle.h"
  16. #include "megdnn/internal/visibility_prologue.h"
  17. namespace megdnn {
  18. class Handle;
  19. /**
  20. * \brief base class for all operators
  21. *
  22. * This is an helper class. Users should not use OperatorBase directly.
  23. * Operators should be created by handle->create_opr<>().
  24. *
  25. * Each operator must provides the following constexpr values:
  26. *
  27. * * NR_INPUTS: number of input vars
  28. * * NR_OUTPUTS: number of output vars
  29. * * OPERATOR_TYPE: operator type as an enum
  30. *
  31. * If the operator has dynamic inputs or in_out param, the corresponding
  32. * NR_INPUTS is -1.
  33. *
  34. * For an operator whose NR_INPUTS >= 0 and NR_OUTPUTS >= 0, the operator must
  35. * also provide following methods:
  36. *
  37. * * void exec(_megdnn_in inputs..., _megdnn_tensor_out outputs...,
  38. * _megdnn_workspace workspace)
  39. * * void deduce_layout(const TensorLayout& inputs...,
  40. * TensorLayout& outputs...)
  41. * * size_t get_workspace_in_bytes(const TensorLayout &inputs...,
  42. * const TensorLayout &outputs)
  43. */
  44. class OperatorBase {
  45. public:
  46. explicit OperatorBase(Handle* handle) : m_handle(handle) {}
  47. virtual ~OperatorBase();
  48. //! get the handle from which this operator is created
  49. Handle* handle() const { return m_handle; }
  50. //! whether this opr guarantees that its exec() is thread-safe
  51. virtual bool is_thread_safe() const { return false; }
  52. /*!
  53. * \brief set the tracker to be used with MegcoreAsyncErrorInfo
  54. *
  55. * Most operators do not have async errors so this function has a
  56. * default empty implementation.
  57. */
  58. virtual void set_error_tracker(void*) {}
  59. private:
  60. Handle* m_handle;
  61. };
  62. namespace detail {
  63. /**
  64. * \brief AlgoSelectionStrategy is the advance information for selecting
  65. * algo
  66. */
  67. enum class AlgoSelectionStrategy {
  68. HEURISTIC = 0, //!< heristic to select the algos
  69. FAST_RUN = 1,
  70. FULL_RUN = 2,
  71. };
  72. /**
  73. * \brief separate algo by datatype for Matmul and conv
  74. */
  75. enum class AlgoDataType : uint32_t {
  76. FLOAT32 = 1 << 0,
  77. FLOAT16 = 1 << 1,
  78. QINT8X8X32 = 1 << 2,
  79. QUINT8X8X32 = 1 << 3,
  80. INT8X8X16 = 1 << 4,
  81. INT16X16X32 = 1 << 5,
  82. INT4X4X16 = 1 << 6,
  83. };
  84. /*!
  85. * \brief Abstract representation of an algorithm for implementing
  86. * the operator
  87. */
  88. class Algorithm {
  89. public:
  90. static constexpr uint32_t INVALID_ALGO_TYPE = static_cast<uint32_t>(-1);
  91. /**
  92. * \brief Algorithm information, we can get real algo from
  93. * AlgorithmInfo::Info::Desc
  94. */
  95. struct Info {
  96. struct Desc {
  97. //! backend of the algo belonging to
  98. Handle::HandleType handle_type;
  99. //! indicate the real algo implementation
  100. uint32_t type = INVALID_ALGO_TYPE;
  101. //! serialized param of the algo type
  102. std::string param;
  103. bool valid() const { return type != INVALID_ALGO_TYPE; }
  104. void reset() { type = INVALID_ALGO_TYPE; }
  105. bool operator==(const Desc& rhs) const {
  106. return handle_type == rhs.handle_type && type == rhs.type &&
  107. param == rhs.param;
  108. }
  109. } desc;
  110. //! algorithm name
  111. std::string name;
  112. bool is_reproducible;
  113. bool valid() const { return desc.valid(); }
  114. void reset() { desc.reset(); }
  115. //! desc donate the algo
  116. bool operator==(const Info& rhs) const { return desc == rhs.desc; }
  117. };
  118. virtual ~Algorithm() = default;
  119. /**
  120. * \brief whether the execution result is
  121. * reproducible across multiple runs.
  122. */
  123. virtual bool is_reproducible() const = 0;
  124. virtual const char* name() const = 0;
  125. //! serialized param
  126. virtual std::string param() const { return {}; }
  127. virtual uint32_t type() const = 0;
  128. Handle::HandleType handle_type() const { return m_handle_type; }
  129. Info info() const {
  130. return {{handle_type(), type(), param()}, name(), is_reproducible()};
  131. }
  132. Info::Desc desc() const { return {handle_type(), type(), param()}; }
  133. template <typename T>
  134. static void serialize_write_pod(const T& val, std::string& result) {
  135. static_assert(std::is_standard_layout<T>::value, "invalid type");
  136. result.append(reinterpret_cast<const char*>(&val), sizeof(T));
  137. }
  138. static void serialize_write_pod(const char* val, std::string& result) {
  139. result.append(val, strlen(val));
  140. }
  141. template <typename T>
  142. static T deserialize_read_pod(const std::string& data, size_t offset = 0) {
  143. static_assert(std::is_standard_layout<T>::value, "invalid type");
  144. T ret;
  145. //! A pointer to an object or incomplete type may be converted to a
  146. //! pointer to a different object or incomplete type. If the resulting
  147. //! pointer is not correctly aligned for the pointed-to type, the
  148. //! behavior is undefined.
  149. //!
  150. //! so here we should use memcpy instead of
  151. //! *reinterpret_cast<const T*>(&data[offset]);
  152. memcpy(&ret, data.data() + offset, sizeof(T));
  153. return ret;
  154. }
  155. template <typename T>
  156. static T deserialize_read_pod(const char* data, size_t offset = 0) {
  157. static_assert(std::is_standard_layout<T>::value, "invalid type");
  158. T ret;
  159. //! A pointer to an object or incomplete type may be converted to a
  160. //! pointer to a different object or incomplete type. If the resulting
  161. //! pointer is not correctly aligned for the pointed-to type, the
  162. //! behavior is undefined.
  163. //!
  164. //! so here we should use memcpy instead of
  165. //! *reinterpret_cast<const T*>(&data[offset]);
  166. memcpy(&ret, data + offset, sizeof(T));
  167. return ret;
  168. }
  169. enum class OprType : uint32_t {
  170. MATRIX_MUL_FORWARD,
  171. BATCHED_MATRIX_MUL_FORWARD,
  172. CONVOLUTION_FORWARD,
  173. CONVOLUTION_BACKWARD_DATA,
  174. CONVOLUTION_BACKWARD_FILTER,
  175. CONVOLUTION3D_FORWARD,
  176. CONVOLUTION3D_BACKWARD_DATA,
  177. CONVOLUTION3D_BACKWARD_FILTER,
  178. LOCAL_SHARE_FORWARD,
  179. LOCAL_SHARE_BACKWARD_DATA,
  180. LOCAL_SHARE_BACKWARD_FILTER,
  181. DEFORMABLE_CONV_FORWARD,
  182. DEFORMABLE_CONV_BACKWARD_DATA,
  183. DEFORMABLE_CONV_BACKWARD_FILTER,
  184. CONVBIAS_FORWARD,
  185. BATCH_CONV_FORWARD,
  186. };
  187. struct SearchItem {
  188. OprType opr_type;
  189. //! serialized param
  190. std::string param;
  191. TensorLayoutArray layouts;
  192. };
  193. /**
  194. * \brief get subopr list of the algo
  195. *
  196. * \param layouts origin layouts of the parent opr
  197. * \param opr parent opr
  198. */
  199. virtual std::vector<SearchItem> get_subopr_list(const TensorLayoutArray&,
  200. const OperatorBase*) const {
  201. return {};
  202. }
  203. protected:
  204. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  205. };
  206. //! policy for executing the operator
  207. struct ExecutionPolicy {
  208. //! INVALID_ALGO_TYPE algo_type means using heuristic
  209. Algorithm::Info::Desc algo;
  210. std::vector<ExecutionPolicy> sub_policy;
  211. };
  212. /*!
  213. * \brief define Algorithm and ExecutionPolicy for oprs that have
  214. * multiple impl algos
  215. *
  216. * \tparam Opr the operator class
  217. * \tparam nargs number of arguments
  218. */
  219. template <class Opr, int nargs>
  220. class MultiAlgoOpr;
  221. //! base def
  222. template <class Opr>
  223. class MultiAlgoOpr<Opr, -1> {
  224. public:
  225. using AlgorithmInfo = detail::Algorithm::Info;
  226. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  227. using Algorithm = detail::Algorithm;
  228. /*!
  229. * \brief get a string representation for current algorithm set;
  230. *
  231. * get_all_algorithms() may return different algorithms only if
  232. * algorithm set name differs. This is used for checking cache
  233. * validity.
  234. */
  235. virtual const char* get_algorithm_set_name() const = 0;
  236. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  237. const ExecutionPolicy& execution_policy() const {
  238. return m_execution_policy;
  239. }
  240. virtual Algorithm* get_algorithm_from_desc(const AlgorithmDesc&) = 0;
  241. protected:
  242. ~MultiAlgoOpr() = default;
  243. private:
  244. ExecutionPolicy m_execution_policy;
  245. };
  246. //! specialize for nargs == 3
  247. template <class Opr>
  248. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  249. public:
  250. using Algorithm = detail::Algorithm;
  251. using AlgorithmInfo = detail::Algorithm::Info;
  252. //! get all possible algorithm decriptions for the specified layouts
  253. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  254. const TensorLayout& p1,
  255. const TensorLayout& p2) {
  256. std::vector<AlgorithmInfo> ret;
  257. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  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,
  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, 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) = 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,
  294. size_t workspace_limit_in_bytes =
  295. std::numeric_limits<size_t>::max(),
  296. bool reproducible = false) = 0;
  297. };
  298. //! specializae for nargs == 4
  299. template <class Opr>
  300. class MultiAlgoOpr<Opr, 4> : 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. std::vector<AlgorithmInfo> ret;
  310. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  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. size_t workspace_limit_in_bytes =
  326. std::numeric_limits<size_t>::max(),
  327. bool reproducible = false) {
  328. return get_algorithm_heuristic(p0, p1, p2, p3, workspace_limit_in_bytes,
  329. reproducible)
  330. ->info();
  331. }
  332. protected:
  333. ~MultiAlgoOpr() = default;
  334. //! get all possible algorithms for the specified layouts
  335. virtual std::vector<Algorithm*> get_all_algorithms(
  336. const TensorLayout& p0, const TensorLayout& p1,
  337. const TensorLayout& p2, const TensorLayout& p3) = 0;
  338. /**
  339. * \brief Returns the best algorithm by heuristic.
  340. *
  341. * The selected algorithm should not use workspace more than
  342. * \p workspace_limit_in_bytes.
  343. */
  344. virtual Algorithm* get_algorithm_heuristic(
  345. const TensorLayout& p0, const TensorLayout& p1,
  346. const TensorLayout& p2, const TensorLayout& p3,
  347. size_t workspace_limit_in_bytes =
  348. std::numeric_limits<size_t>::max(),
  349. bool reproducible = false) = 0;
  350. };
  351. //! specializae for nargs == 5
  352. template <class Opr>
  353. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  354. public:
  355. using Algorithm = detail::Algorithm;
  356. using AlgorithmInfo = detail::Algorithm::Info;
  357. //! get all possible algorithm decriptions for the specified layouts
  358. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  359. const TensorLayout& p1,
  360. const TensorLayout& p2,
  361. const TensorLayout& p3,
  362. const TensorLayout& p4) {
  363. std::vector<AlgorithmInfo> ret;
  364. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  365. ret.emplace_back(algo->info());
  366. }
  367. return ret;
  368. }
  369. /**
  370. * \brief Returns the best algorithm information which indicate the
  371. * algorithm by heuristic.
  372. *
  373. * The selected algorithm should not use workspace more than
  374. * \p workspace_limit_in_bytes.
  375. */
  376. AlgorithmInfo get_algorithm_info_heuristic(
  377. const TensorLayout& p0, const TensorLayout& p1,
  378. const TensorLayout& p2, const TensorLayout& p3,
  379. const TensorLayout& p4,
  380. size_t workspace_limit_in_bytes =
  381. std::numeric_limits<size_t>::max(),
  382. bool reproducible = false) {
  383. return get_algorithm_heuristic(p0, p1, p2, p3, p4,
  384. workspace_limit_in_bytes, reproducible)
  385. ->info();
  386. }
  387. protected:
  388. ~MultiAlgoOpr() = default;
  389. //! get all possible algorithms for the specified layouts
  390. virtual std::vector<Algorithm*> get_all_algorithms(
  391. const TensorLayout& p0, const TensorLayout& p1,
  392. const TensorLayout& p2, const TensorLayout& p3,
  393. const TensorLayout& p4) = 0;
  394. /**
  395. * \brief Returns the best algorithm by heuristic.
  396. *
  397. * The selected algorithm should not use workspace more than
  398. * \p workspace_limit_in_bytes.
  399. */
  400. virtual Algorithm* get_algorithm_heuristic(
  401. const TensorLayout& p0, const TensorLayout& p1,
  402. const TensorLayout& p2, const TensorLayout& p3,
  403. const TensorLayout& p4,
  404. size_t workspace_limit_in_bytes =
  405. std::numeric_limits<size_t>::max(),
  406. bool reproducible = false) = 0;
  407. };
  408. //! specializae for nargs == 8
  409. template <class Opr>
  410. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  411. public:
  412. using Algorithm = detail::Algorithm;
  413. using AlgorithmInfo = detail::Algorithm::Info;
  414. //! get all possible algorithm decriptions for the specified layouts
  415. std::vector<AlgorithmInfo> get_all_algorithms_info(
  416. const TensorLayout& p0, const TensorLayout& p1,
  417. const TensorLayout& p2, const TensorLayout& p3,
  418. const TensorLayout& p4, const TensorLayout& p5,
  419. const TensorLayout& p6, const TensorLayout& p7) {
  420. std::vector<AlgorithmInfo> ret;
  421. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  422. ret.emplace_back(algo->info());
  423. }
  424. return ret;
  425. }
  426. /**
  427. * \brief Returns the best algorithm information which indicate the
  428. * algorithm by heuristic.
  429. *
  430. * The selected algorithm should not use workspace more than
  431. */
  432. AlgorithmInfo get_algorithm_info_heuristic(
  433. const TensorLayout& p0, const TensorLayout& p1,
  434. const TensorLayout& p2, const TensorLayout& p3,
  435. const TensorLayout& p4, const TensorLayout& p5,
  436. const TensorLayout& p6, const TensorLayout& p7,
  437. size_t workspace_limit_in_bytes =
  438. std::numeric_limits<size_t>::max(),
  439. bool reproducible = false) {
  440. return get_algorithm_heuristic(p0, p1, p2, p3, p4, p5, p6, p7,
  441. workspace_limit_in_bytes, reproducible)
  442. ->info();
  443. }
  444. protected:
  445. ~MultiAlgoOpr() = default;
  446. //! get all possible algorithms for the specified layouts
  447. virtual std::vector<Algorithm*> get_all_algorithms(
  448. const TensorLayout& p0, const TensorLayout& p1,
  449. const TensorLayout& p2, const TensorLayout& p3,
  450. const TensorLayout& p4, const TensorLayout& p5,
  451. const TensorLayout& p6, const TensorLayout& p7) = 0;
  452. /**
  453. * \brief Returns the best algorithm by heuristic.
  454. *
  455. * The selected algorithm should not use workspace more than
  456. * \p workspace_limit_in_bytes.
  457. */
  458. virtual Algorithm* get_algorithm_heuristic(
  459. const TensorLayout& p0, const TensorLayout& p1,
  460. const TensorLayout& p2, const TensorLayout& p3,
  461. const TensorLayout& p4, const TensorLayout& p5,
  462. const TensorLayout& p6, const TensorLayout& p7,
  463. size_t workspace_limit_in_bytes =
  464. std::numeric_limits<size_t>::max(),
  465. bool reproducible = false) = 0;
  466. };
  467. } // namespace detail
  468. using Algorithm = detail::Algorithm;
  469. using ExecutionPolicy = detail::ExecutionPolicy;
  470. } // namespace megdnn
  471. #include "megdnn/internal/visibility_epilogue.h"
  472. // vim: syntax=cpp.doxygen

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