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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. QINT4x4x32 = 1 << 7,
  84. };
  85. /*!
  86. * \brief Abstract representation of an algorithm for implementing
  87. * the operator
  88. */
  89. class Algorithm {
  90. public:
  91. static constexpr uint32_t INVALID_ALGO_TYPE = static_cast<uint32_t>(-1);
  92. /**
  93. * \brief the attribe of the algo, such as REPRODUCIBLE, NAIVE
  94. *
  95. */
  96. enum class Attribute : uint32_t {
  97. /**
  98. * \brief general algo.
  99. */
  100. DEFAULT = 0,
  101. /**
  102. * \brief whether the execution result is
  103. * reproducible across multiple runs.
  104. */
  105. REPRODUCIBLE = 1 << 0,
  106. /**
  107. * \brief whether the algo is naive
  108. * Mark algorithms with simple implementation as NAIVE, so we can filter
  109. * these algorithms to speed up fastrun.
  110. * */
  111. NAIVE = 1 << 1,
  112. /**
  113. * \brief whether the algo is usable once shape changed.
  114. * */
  115. USABLE_DEPEND_ON_SHAPE = 1 << 2,
  116. /**
  117. * \brief whether the accuracy of the algo is dependent with respect
  118. * to batch
  119. * In the case of using algorithm with this attribute, even if the
  120. * content of each batch is the same, the output under multiple batch
  121. * input and single batch input may not equal
  122. * */
  123. ACCURACY_DEPEND_ON_BATCH = 1 << 3,
  124. };
  125. /**
  126. * \brief Algorithm information, we can get real algo from
  127. * AlgorithmInfo::Info::Desc
  128. */
  129. struct Info {
  130. struct Desc {
  131. //! backend of the algo belonging to
  132. Handle::HandleType handle_type;
  133. //! indicate the real algo implementation
  134. uint32_t type = INVALID_ALGO_TYPE;
  135. //! serialized param of the algo type
  136. std::string param;
  137. //! algorithm name
  138. std::string name;
  139. bool valid() const { return type != INVALID_ALGO_TYPE; }
  140. void reset() { type = INVALID_ALGO_TYPE; }
  141. bool operator==(const Desc& rhs) const {
  142. return handle_type == rhs.handle_type && type == rhs.type &&
  143. param == rhs.param && name == rhs.name;
  144. }
  145. } desc;
  146. Attribute attribute;
  147. bool valid() const { return desc.valid(); }
  148. void reset() { desc.reset(); }
  149. //! desc donate the algo
  150. bool operator==(const Info& rhs) const { return desc == rhs.desc; }
  151. };
  152. virtual ~Algorithm() = default;
  153. /**
  154. * \brief get the attribute of the algo
  155. */
  156. virtual Attribute attribute() const = 0;
  157. virtual const char* name() const = 0;
  158. //! serialized param
  159. virtual std::string param() const { return {}; }
  160. virtual uint32_t type() const = 0;
  161. //! if algo contain all of the attribute in attr
  162. bool contain_attribute_all(const Attribute& attr) const;
  163. //! if algo contain any attribute in attr
  164. bool contain_attribute_any(const Attribute& attr) const;
  165. void check_attribute(
  166. const Attribute& positive_attr = Attribute::DEFAULT,
  167. const Attribute& negative_attr = Attribute::DEFAULT) const;
  168. static std::string attribute_str(const Attribute& attr);
  169. Handle::HandleType handle_type() const { return m_handle_type; }
  170. Info::Desc desc() const { return {handle_type(), type(), param(), name()}; }
  171. Info info() const {
  172. return {desc(), attribute()};
  173. }
  174. template <typename T>
  175. static void serialize_write_pod(const T& val, std::string& result) {
  176. static_assert(std::is_trivially_copyable<T>::value,
  177. "type should be trivially copyable");
  178. static_assert(!std::is_pointer<T>::value,
  179. "serialize pointer is unsafe in eager execution mode");
  180. result.append(reinterpret_cast<const char*>(&val), sizeof(T));
  181. }
  182. static void serialize_write_pod(const char* val, std::string& result) {
  183. result.append(val, strlen(val));
  184. }
  185. template <typename T>
  186. static T deserialize_read_pod(const std::string& data, size_t offset = 0) {
  187. static_assert(std::is_trivially_copyable<T>::value, "invalid type");
  188. T ret;
  189. //! A pointer to an object or incomplete type may be converted to a
  190. //! pointer to a different object or incomplete type. If the resulting
  191. //! pointer is not correctly aligned for the pointed-to type, the
  192. //! behavior is undefined.
  193. //!
  194. //! so here we should use memcpy instead of
  195. //! *reinterpret_cast<const T*>(&data[offset]);
  196. memcpy(&ret, data.data() + offset, sizeof(T));
  197. return ret;
  198. }
  199. template <typename T>
  200. static T deserialize_read_pod(const char* data, size_t offset = 0) {
  201. static_assert(std::is_trivially_copyable<T>::value, "invalid type");
  202. T ret;
  203. //! A pointer to an object or incomplete type may be converted to a
  204. //! pointer to a different object or incomplete type. If the resulting
  205. //! pointer is not correctly aligned for the pointed-to type, the
  206. //! behavior is undefined.
  207. //!
  208. //! so here we should use memcpy instead of
  209. //! *reinterpret_cast<const T*>(&data[offset]);
  210. memcpy(&ret, data + offset, sizeof(T));
  211. return ret;
  212. }
  213. enum class OprType : uint32_t {
  214. MATRIX_MUL_FORWARD,
  215. BATCHED_MATRIX_MUL_FORWARD,
  216. CONVOLUTION_FORWARD,
  217. CONVOLUTION_BACKWARD_DATA,
  218. CONVOLUTION_BACKWARD_FILTER,
  219. CONVOLUTION3D_FORWARD,
  220. CONVOLUTION3D_BACKWARD_DATA,
  221. CONVOLUTION3D_BACKWARD_FILTER,
  222. LOCAL_SHARE_FORWARD,
  223. LOCAL_SHARE_BACKWARD_DATA,
  224. LOCAL_SHARE_BACKWARD_FILTER,
  225. DEFORMABLE_CONV_FORWARD,
  226. DEFORMABLE_CONV_BACKWARD_DATA,
  227. DEFORMABLE_CONV_BACKWARD_FILTER,
  228. CONVBIAS_FORWARD,
  229. BATCH_CONV_FORWARD,
  230. POOLING_FORWARD,
  231. POOLING_BACKWARD,
  232. };
  233. struct SearchItem {
  234. OprType opr_type;
  235. //! serialized param
  236. std::string param;
  237. TensorLayoutArray layouts;
  238. };
  239. /**
  240. * \brief get subopr list of the algo
  241. *
  242. * \param layouts origin layouts of the parent opr
  243. * \param opr parent opr
  244. */
  245. virtual std::vector<SearchItem> get_subopr_list(const TensorLayoutArray&,
  246. const OperatorBase*) const {
  247. return {};
  248. }
  249. protected:
  250. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  251. };
  252. MEGDNN_DEF_ENUM_CLASS_BIT_OPR(Algorithm::Attribute)
  253. //! policy for executing the operator
  254. struct ExecutionPolicy {
  255. //! INVALID_ALGO_TYPE algo_type means using heuristic
  256. Algorithm::Info::Desc algo;
  257. std::vector<ExecutionPolicy> sub_policy;
  258. };
  259. /*!
  260. * \brief define Algorithm and ExecutionPolicy for oprs that have
  261. * multiple impl algos
  262. *
  263. * \tparam Opr the operator class
  264. * \tparam nargs number of arguments
  265. */
  266. template <class Opr, int nargs>
  267. class MultiAlgoOpr;
  268. //! base def
  269. template <class Opr>
  270. class MultiAlgoOpr<Opr, -1> {
  271. public:
  272. using AlgorithmInfo = detail::Algorithm::Info;
  273. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  274. using Algorithm = detail::Algorithm;
  275. /*!
  276. * \brief get a string representation for current algorithm set;
  277. *
  278. * get_all_algorithms_safe() may return different algorithms only if
  279. * algorithm set name differs. This is used for checking cache
  280. * validity.
  281. */
  282. virtual const char* get_algorithm_set_name() const = 0;
  283. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  284. const ExecutionPolicy& execution_policy() const {
  285. return m_execution_policy;
  286. }
  287. virtual Algorithm* get_algorithm_from_desc(const AlgorithmDesc&) = 0;
  288. protected:
  289. ~MultiAlgoOpr() = default;
  290. private:
  291. ExecutionPolicy m_execution_policy;
  292. };
  293. //! specialize for nargs == 2
  294. template <class Opr>
  295. class MultiAlgoOpr<Opr, 2> : public MultiAlgoOpr<Opr, -1> {
  296. public:
  297. using Algorithm = detail::Algorithm;
  298. using AlgorithmInfo = detail::Algorithm::Info;
  299. using AlgoAttribute = detail::Algorithm::Attribute;
  300. //! get all possible algorithm decriptions for the specified layouts
  301. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  302. const TensorLayout& p1) {
  303. std::vector<AlgorithmInfo> ret;
  304. for (auto&& algo : get_all_algorithms(p0, p1)) {
  305. ret.emplace_back(algo->info());
  306. }
  307. return ret;
  308. }
  309. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(const TensorLayout& p0,
  310. const TensorLayout& p1) {
  311. std::vector<AlgorithmInfo> ret;
  312. for (auto&& algo : get_all_algorithms_safe(p0, p1)) {
  313. ret.emplace_back(algo->info());
  314. }
  315. return ret;
  316. }
  317. /**
  318. * \brief Returns the best algorithm information which indicate the
  319. * algorithm by heuristic.
  320. *
  321. * The selected algorithm should not use workspace more than
  322. * \p workspace_limit_in_bytes.
  323. */
  324. AlgorithmInfo get_algorithm_info_heuristic(
  325. const TensorLayout& p0, const TensorLayout& p1,
  326. size_t workspace_limit_in_bytes =
  327. std::numeric_limits<size_t>::max(),
  328. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  329. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  330. return get_algorithm_heuristic(p0, p1, workspace_limit_in_bytes,
  331. positive_attr, negative_attr)
  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) = 0;
  339. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  340. const TensorLayout& p0, const TensorLayout& p1) = 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. size_t workspace_limit_in_bytes =
  350. std::numeric_limits<size_t>::max(),
  351. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  352. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  353. };
  354. //! specialize for nargs == 3
  355. template <class Opr>
  356. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  357. public:
  358. using Algorithm = detail::Algorithm;
  359. using AlgorithmInfo = detail::Algorithm::Info;
  360. using AlgoAttribute = detail::Algorithm::Attribute;
  361. //! get all possible algorithm decriptions for the specified layouts
  362. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  363. const TensorLayout& p1,
  364. const TensorLayout& p2) {
  365. std::vector<AlgorithmInfo> ret;
  366. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  367. ret.emplace_back(algo->info());
  368. }
  369. return ret;
  370. }
  371. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(const TensorLayout& p0,
  372. const TensorLayout& p1,
  373. const TensorLayout& p2) {
  374. std::vector<AlgorithmInfo> ret;
  375. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2)) {
  376. ret.emplace_back(algo->info());
  377. }
  378. return ret;
  379. }
  380. /**
  381. * \brief Returns the best algorithm information which indicate the
  382. * algorithm by heuristic.
  383. *
  384. * The selected algorithm should not use workspace more than
  385. * \p workspace_limit_in_bytes.
  386. */
  387. AlgorithmInfo get_algorithm_info_heuristic(
  388. const TensorLayout& p0, const TensorLayout& p1,
  389. const TensorLayout& p2,
  390. size_t workspace_limit_in_bytes =
  391. std::numeric_limits<size_t>::max(),
  392. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  393. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  394. return get_algorithm_heuristic(p0, p1, p2, workspace_limit_in_bytes,
  395. positive_attr, negative_attr)
  396. ->info();
  397. }
  398. protected:
  399. ~MultiAlgoOpr() = default;
  400. //! get all possible algorithms for the specified layouts
  401. virtual std::vector<Algorithm*> get_all_algorithms(
  402. const TensorLayout& p0, const TensorLayout& p1,
  403. const TensorLayout& p2) = 0;
  404. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  405. const TensorLayout& p0, const TensorLayout& p1,
  406. const TensorLayout& p2) = 0;
  407. /**
  408. * \brief Returns the best algorithm by heuristic.
  409. *
  410. * The selected algorithm should not use workspace more than
  411. * \p workspace_limit_in_bytes.
  412. */
  413. virtual Algorithm* get_algorithm_heuristic(
  414. const TensorLayout& p0, const TensorLayout& p1,
  415. const TensorLayout& p2,
  416. size_t workspace_limit_in_bytes =
  417. std::numeric_limits<size_t>::max(),
  418. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  419. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  420. };
  421. //! specializae for nargs == 4
  422. template <class Opr>
  423. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  424. public:
  425. using Algorithm = detail::Algorithm;
  426. using AlgorithmInfo = detail::Algorithm::Info;
  427. using AlgoAttribute = detail::Algorithm::Attribute;
  428. //! get all possible algorithm decriptions for the specified layouts
  429. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  430. const TensorLayout& p1,
  431. const TensorLayout& p2,
  432. const TensorLayout& p3) {
  433. std::vector<AlgorithmInfo> ret;
  434. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  435. ret.emplace_back(algo->info());
  436. }
  437. return ret;
  438. }
  439. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(const TensorLayout& p0,
  440. const TensorLayout& p1,
  441. const TensorLayout& p2,
  442. const TensorLayout& p3) {
  443. std::vector<AlgorithmInfo> ret;
  444. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3)) {
  445. ret.emplace_back(algo->info());
  446. }
  447. return ret;
  448. }
  449. /**
  450. * \brief Returns the best algorithm information which indicate the
  451. * algorithm by heuristic.
  452. *
  453. * The selected algorithm should not use workspace more than
  454. * \p workspace_limit_in_bytes.
  455. */
  456. AlgorithmInfo get_algorithm_info_heuristic(
  457. const TensorLayout& p0, const TensorLayout& p1,
  458. const TensorLayout& p2, const TensorLayout& p3,
  459. size_t workspace_limit_in_bytes =
  460. std::numeric_limits<size_t>::max(),
  461. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  462. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  463. return get_algorithm_heuristic(p0, p1, p2, p3, workspace_limit_in_bytes,
  464. positive_attr, negative_attr)
  465. ->info();
  466. }
  467. protected:
  468. ~MultiAlgoOpr() = default;
  469. //! get all possible algorithms for the specified layouts
  470. virtual std::vector<Algorithm*> get_all_algorithms(
  471. const TensorLayout& p0, const TensorLayout& p1,
  472. const TensorLayout& p2, const TensorLayout& p3) = 0;
  473. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  474. const TensorLayout& p0, const TensorLayout& p1,
  475. const TensorLayout& p2, const TensorLayout& p3) = 0;
  476. /**
  477. * \brief Returns the best algorithm by heuristic.
  478. *
  479. * The selected algorithm should not use workspace more than
  480. * \p workspace_limit_in_bytes.
  481. */
  482. virtual Algorithm* get_algorithm_heuristic(
  483. const TensorLayout& p0, const TensorLayout& p1,
  484. const TensorLayout& p2, const TensorLayout& p3,
  485. size_t workspace_limit_in_bytes =
  486. std::numeric_limits<size_t>::max(),
  487. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  488. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  489. };
  490. //! specializae for nargs == 5
  491. template <class Opr>
  492. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  493. public:
  494. using Algorithm = detail::Algorithm;
  495. using AlgorithmInfo = detail::Algorithm::Info;
  496. using AlgoAttribute = detail::Algorithm::Attribute;
  497. //! get all possible algorithm decriptions for the specified layouts
  498. std::vector<AlgorithmInfo> get_all_algorithms_info(const TensorLayout& p0,
  499. const TensorLayout& p1,
  500. const TensorLayout& p2,
  501. const TensorLayout& p3,
  502. const TensorLayout& p4) {
  503. std::vector<AlgorithmInfo> ret;
  504. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  505. ret.emplace_back(algo->info());
  506. }
  507. return ret;
  508. }
  509. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(const TensorLayout& p0,
  510. const TensorLayout& p1,
  511. const TensorLayout& p2,
  512. const TensorLayout& p3,
  513. const TensorLayout& p4) {
  514. std::vector<AlgorithmInfo> ret;
  515. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4)) {
  516. ret.emplace_back(algo->info());
  517. }
  518. return ret;
  519. }
  520. /**
  521. * \brief Returns the best algorithm information which indicate the
  522. * algorithm by heuristic.
  523. *
  524. * The selected algorithm should not use workspace more than
  525. * \p workspace_limit_in_bytes.
  526. */
  527. AlgorithmInfo get_algorithm_info_heuristic(
  528. const TensorLayout& p0, const TensorLayout& p1,
  529. const TensorLayout& p2, const TensorLayout& p3,
  530. const TensorLayout& p4,
  531. size_t workspace_limit_in_bytes =
  532. std::numeric_limits<size_t>::max(),
  533. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  534. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  535. return get_algorithm_heuristic(p0, p1, p2, p3, p4,
  536. workspace_limit_in_bytes, positive_attr,
  537. negative_attr)
  538. ->info();
  539. }
  540. protected:
  541. ~MultiAlgoOpr() = default;
  542. //! get all possible algorithms for the specified layouts
  543. virtual std::vector<Algorithm*> get_all_algorithms(
  544. const TensorLayout& p0, const TensorLayout& p1,
  545. const TensorLayout& p2, const TensorLayout& p3,
  546. const TensorLayout& p4) = 0;
  547. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  548. const TensorLayout& p0, const TensorLayout& p1,
  549. const TensorLayout& p2, const TensorLayout& p3,
  550. const TensorLayout& p4) = 0;
  551. /**
  552. * \brief Returns the best algorithm by heuristic.
  553. *
  554. * The selected algorithm should not use workspace more than
  555. * \p workspace_limit_in_bytes.
  556. */
  557. virtual Algorithm* get_algorithm_heuristic(
  558. const TensorLayout& p0, const TensorLayout& p1,
  559. const TensorLayout& p2, const TensorLayout& p3,
  560. const TensorLayout& p4,
  561. size_t workspace_limit_in_bytes =
  562. std::numeric_limits<size_t>::max(),
  563. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  564. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  565. };
  566. //! specializae for nargs == 8
  567. template <class Opr>
  568. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  569. public:
  570. using Algorithm = detail::Algorithm;
  571. using AlgorithmInfo = detail::Algorithm::Info;
  572. using AlgoAttribute = detail::Algorithm::Attribute;
  573. //! get all possible algorithm decriptions for the specified layouts
  574. std::vector<AlgorithmInfo> get_all_algorithms_info(
  575. const TensorLayout& p0, const TensorLayout& p1,
  576. const TensorLayout& p2, const TensorLayout& p3,
  577. const TensorLayout& p4, const TensorLayout& p5,
  578. const TensorLayout& p6, const TensorLayout& p7) {
  579. std::vector<AlgorithmInfo> ret;
  580. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  581. ret.emplace_back(algo->info());
  582. }
  583. return ret;
  584. }
  585. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  586. const TensorLayout& p0, const TensorLayout& p1,
  587. const TensorLayout& p2, const TensorLayout& p3,
  588. const TensorLayout& p4, const TensorLayout& p5,
  589. const TensorLayout& p6, const TensorLayout& p7) {
  590. std::vector<AlgorithmInfo> ret;
  591. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4, p5, p6, p7)) {
  592. ret.emplace_back(algo->info());
  593. }
  594. return ret;
  595. }
  596. /**
  597. * \brief Returns the best algorithm information which indicate the
  598. * algorithm by heuristic.
  599. *
  600. * The selected algorithm should not use workspace more than
  601. */
  602. AlgorithmInfo get_algorithm_info_heuristic(
  603. const TensorLayout& p0, const TensorLayout& p1,
  604. const TensorLayout& p2, const TensorLayout& p3,
  605. const TensorLayout& p4, const TensorLayout& p5,
  606. const TensorLayout& p6, const TensorLayout& p7,
  607. size_t workspace_limit_in_bytes =
  608. std::numeric_limits<size_t>::max(),
  609. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  610. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  611. return get_algorithm_heuristic(p0, p1, p2, p3, p4, p5, p6, p7,
  612. workspace_limit_in_bytes, positive_attr,
  613. negative_attr)
  614. ->info();
  615. }
  616. protected:
  617. ~MultiAlgoOpr() = default;
  618. //! get all possible algorithms for the specified layouts
  619. virtual std::vector<Algorithm*> get_all_algorithms(
  620. const TensorLayout& p0, const TensorLayout& p1,
  621. const TensorLayout& p2, const TensorLayout& p3,
  622. const TensorLayout& p4, const TensorLayout& p5,
  623. const TensorLayout& p6, const TensorLayout& p7) = 0;
  624. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  625. const TensorLayout& p0, const TensorLayout& p1,
  626. const TensorLayout& p2, const TensorLayout& p3,
  627. const TensorLayout& p4, const TensorLayout& p5,
  628. const TensorLayout& p6, const TensorLayout& p7) = 0;
  629. /**
  630. * \brief Returns the best algorithm by heuristic.
  631. *
  632. * The selected algorithm should not use workspace more than
  633. * \p workspace_limit_in_bytes.
  634. */
  635. virtual Algorithm* get_algorithm_heuristic(
  636. const TensorLayout& p0, const TensorLayout& p1,
  637. const TensorLayout& p2, const TensorLayout& p3,
  638. const TensorLayout& p4, const TensorLayout& p5,
  639. const TensorLayout& p6, const TensorLayout& p7,
  640. size_t workspace_limit_in_bytes =
  641. std::numeric_limits<size_t>::max(),
  642. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  643. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  644. };
  645. } // namespace detail
  646. using Algorithm = detail::Algorithm;
  647. using AlgoAttribute = Algorithm::Attribute;
  648. using ExecutionPolicy = detail::ExecutionPolicy;
  649. } // namespace megdnn
  650. #include "megdnn/internal/visibility_epilogue.h"
  651. // vim: syntax=cpp.doxygen

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