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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. #pragma once
  2. #include <type_traits>
  3. #include "megdnn/basic_types.h"
  4. #include "megdnn/handle.h"
  5. #include "megdnn/internal/visibility_prologue.h"
  6. namespace megdnn {
  7. class Handle;
  8. /**
  9. * \brief base class for all operators
  10. *
  11. * This is an helper class. Users should not use OperatorBase directly.
  12. * Operators should be created by handle->create_opr<>().
  13. *
  14. * Each operator must provides the following constexpr values:
  15. *
  16. * * NR_INPUTS: number of input vars
  17. * * NR_OUTPUTS: number of output vars
  18. * * OPERATOR_TYPE: operator type as an enum
  19. *
  20. * If the operator has dynamic inputs or in_out param, the corresponding
  21. * NR_INPUTS is -1.
  22. *
  23. * For an operator whose NR_INPUTS >= 0 and NR_OUTPUTS >= 0, the operator must
  24. * also provide following methods:
  25. *
  26. * * void exec(_megdnn_in inputs..., _megdnn_tensor_out outputs...,
  27. * _megdnn_workspace workspace)
  28. * * void deduce_layout(const TensorLayout& inputs...,
  29. * TensorLayout& outputs...)
  30. * * size_t get_workspace_in_bytes(const TensorLayout &inputs...,
  31. * const TensorLayout &outputs)
  32. */
  33. class OperatorBase {
  34. public:
  35. explicit OperatorBase(Handle* handle) : m_handle(handle) {}
  36. virtual ~OperatorBase();
  37. //! get the handle from which this operator is created
  38. Handle* handle() const { return m_handle; }
  39. //! whether this opr guarantees that its exec() is thread-safe
  40. virtual bool is_thread_safe() const { return false; }
  41. /*!
  42. * \brief set the tracker to be used with MegcoreAsyncErrorInfo
  43. *
  44. * Most operators do not have async errors so this function has a
  45. * default empty implementation.
  46. */
  47. virtual void set_error_tracker(void*) {}
  48. private:
  49. Handle* m_handle;
  50. };
  51. namespace detail {
  52. /**
  53. * \brief AlgoSelectionStrategy is the advance information for selecting
  54. * algo
  55. */
  56. enum class AlgoSelectionStrategy {
  57. HEURISTIC = 0, //!< heristic to select the algos
  58. FAST_RUN = 1,
  59. FULL_RUN = 2,
  60. };
  61. /**
  62. * \brief separate algo by datatype for Matmul and conv
  63. */
  64. enum class AlgoDataType : uint32_t {
  65. FLOAT32 = 1 << 0,
  66. FLOAT16 = 1 << 1,
  67. QINT8X8X32 = 1 << 2,
  68. QUINT8X8X32 = 1 << 3,
  69. INT8X8X16 = 1 << 4,
  70. INT16X16X32 = 1 << 5,
  71. INT4X4X16 = 1 << 6,
  72. QINT4x4x32 = 1 << 7,
  73. };
  74. /*!
  75. * \brief Abstract representation of an algorithm for implementing
  76. * the operator
  77. */
  78. class Algorithm {
  79. public:
  80. static constexpr uint32_t INVALID_ALGO_TYPE = static_cast<uint32_t>(-1);
  81. /**
  82. * \brief the attribe of the algo, such as REPRODUCIBLE, NAIVE
  83. *
  84. */
  85. enum class Attribute : uint32_t {
  86. /**
  87. * \brief general algo.
  88. */
  89. DEFAULT = 0,
  90. /**
  91. * \brief whether the execution result is
  92. * reproducible across multiple runs.
  93. */
  94. REPRODUCIBLE = 1 << 0,
  95. /**
  96. * \brief whether the algo is naive
  97. * Mark algorithms with simple implementation as NAIVE, so we can filter
  98. * these algorithms to speed up fastrun.
  99. * */
  100. NAIVE = 1 << 1,
  101. /**
  102. * \brief whether the algo is usable once shape changed.
  103. * */
  104. USABLE_DEPEND_ON_SHAPE = 1 << 2,
  105. /**
  106. * \brief whether the accuracy of the algo is dependent with respect
  107. * to batch
  108. * In the case of using algorithm with this attribute, even if the
  109. * content of each batch is the same, the output under multiple batch
  110. * input and single batch input may not equal
  111. * */
  112. ACCURACY_DEPEND_ON_BATCH = 1 << 3,
  113. };
  114. /**
  115. * \brief Algorithm information, we can get real algo from
  116. * AlgorithmInfo::Info::Desc
  117. */
  118. struct Info {
  119. struct Desc {
  120. //! backend of the algo belonging to
  121. Handle::HandleType handle_type;
  122. //! indicate the real algo implementation
  123. uint32_t type = INVALID_ALGO_TYPE;
  124. //! serialized param of the algo type
  125. std::string param;
  126. //! algorithm name
  127. std::string name;
  128. bool valid() const { return type != INVALID_ALGO_TYPE; }
  129. void reset() { type = INVALID_ALGO_TYPE; }
  130. bool operator==(const Desc& rhs) const {
  131. return handle_type == rhs.handle_type && type == rhs.type &&
  132. param == rhs.param && name == rhs.name;
  133. }
  134. } desc;
  135. Attribute attribute;
  136. bool valid() const { return desc.valid(); }
  137. void reset() { desc.reset(); }
  138. //! desc donate the algo
  139. bool operator==(const Info& rhs) const { return desc == rhs.desc; }
  140. };
  141. virtual ~Algorithm() = default;
  142. /**
  143. * \brief get the attribute of the algo
  144. */
  145. virtual Attribute attribute() const = 0;
  146. virtual const char* name() const = 0;
  147. //! serialized param
  148. virtual std::string param() const { return {}; }
  149. virtual uint32_t type() const = 0;
  150. //! if algo contain all of the attribute in attr
  151. bool contain_attribute_all(const Attribute& attr) const;
  152. //! if algo contain any attribute in attr
  153. bool contain_attribute_any(const Attribute& attr) const;
  154. void check_attribute(
  155. const Attribute& positive_attr = Attribute::DEFAULT,
  156. const Attribute& negative_attr = Attribute::DEFAULT) const;
  157. static std::string attribute_str(const Attribute& attr);
  158. Handle::HandleType handle_type() const { return m_handle_type; }
  159. Info::Desc desc() const { return {handle_type(), type(), param(), name()}; }
  160. Info info() const { return {desc(), attribute()}; }
  161. template <typename T>
  162. static void serialize_write_pod(const T& val, std::string& result) {
  163. static_assert(
  164. std::is_trivially_copyable<T>::value,
  165. "type should be trivially copyable");
  166. static_assert(
  167. !std::is_pointer<T>::value,
  168. "serialize pointer is unsafe in eager execution mode");
  169. result.append(reinterpret_cast<const char*>(&val), sizeof(T));
  170. }
  171. static void serialize_write_pod(const char* val, std::string& result) {
  172. result.append(val, strlen(val));
  173. }
  174. static void serialize_write_pod(const std::string& val, std::string& result) {
  175. result.append(val.data(), val.size());
  176. }
  177. template <typename T>
  178. static T deserialize_read_pod(const std::string& data, size_t offset = 0) {
  179. static_assert(std::is_trivially_copyable<T>::value, "invalid type");
  180. T ret;
  181. //! A pointer to an object or incomplete type may be converted to a
  182. //! pointer to a different object or incomplete type. If the resulting
  183. //! pointer is not correctly aligned for the pointed-to type, the
  184. //! behavior is undefined.
  185. //!
  186. //! so here we should use memcpy instead of
  187. //! *reinterpret_cast<const T*>(&data[offset]);
  188. memcpy(&ret, data.data() + offset, sizeof(T));
  189. return ret;
  190. }
  191. static std::string deserialize_read_pod(
  192. const std::string& data, size_t offset = 0, size_t size = 0) {
  193. return std::string(data.data() + offset, size);
  194. }
  195. template <typename T>
  196. static T deserialize_read_pod(const char* data, size_t offset = 0) {
  197. static_assert(std::is_trivially_copyable<T>::value, "invalid type");
  198. T ret;
  199. //! A pointer to an object or incomplete type may be converted to a
  200. //! pointer to a different object or incomplete type. If the resulting
  201. //! pointer is not correctly aligned for the pointed-to type, the
  202. //! behavior is undefined.
  203. //!
  204. //! so here we should use memcpy instead of
  205. //! *reinterpret_cast<const T*>(&data[offset]);
  206. memcpy(&ret, data + offset, sizeof(T));
  207. return ret;
  208. }
  209. enum class OprType : uint32_t {
  210. MATRIX_MUL_FORWARD,
  211. BATCHED_MATRIX_MUL_FORWARD,
  212. CONVOLUTION_FORWARD,
  213. CONVOLUTION_BACKWARD_DATA,
  214. CONVOLUTION_BACKWARD_FILTER,
  215. CONVOLUTION3D_FORWARD,
  216. CONVOLUTION3D_BACKWARD_DATA,
  217. CONVOLUTION3D_BACKWARD_FILTER,
  218. LOCAL_SHARE_FORWARD,
  219. LOCAL_SHARE_BACKWARD_DATA,
  220. LOCAL_SHARE_BACKWARD_FILTER,
  221. DEFORMABLE_CONV_FORWARD,
  222. DEFORMABLE_CONV_BACKWARD_DATA,
  223. DEFORMABLE_CONV_BACKWARD_FILTER,
  224. CONVBIAS_FORWARD,
  225. BATCH_CONV_FORWARD,
  226. POOLING_FORWARD,
  227. POOLING_BACKWARD,
  228. REGIONRESTRICTEDCONVOLUTION_FORWARD,
  229. REGIONRESTRICTEDCONVOLUTION_BACKWARD_DATA,
  230. REGIONRESTRICTEDCONVOLUTION_BACKWARD_FILTER,
  231. };
  232. struct SearchItem {
  233. OprType opr_type;
  234. //! serialized param
  235. std::string param;
  236. TensorLayoutArray layouts;
  237. };
  238. /**
  239. * \brief get subopr list of the algo
  240. *
  241. * \param layouts origin layouts of the parent opr
  242. * \param opr parent opr
  243. */
  244. virtual std::vector<SearchItem> get_subopr_list(
  245. const TensorLayoutArray&, const OperatorBase*) const {
  246. return {};
  247. }
  248. protected:
  249. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  250. };
  251. MEGDNN_DEF_ENUM_CLASS_BIT_OPR(Algorithm::Attribute)
  252. //! policy for executing the operator
  253. struct ExecutionPolicy {
  254. //! INVALID_ALGO_TYPE algo_type means using heuristic
  255. Algorithm::Info::Desc algo;
  256. std::vector<ExecutionPolicy> sub_policy;
  257. };
  258. /*!
  259. * \brief define Algorithm and ExecutionPolicy for oprs that have
  260. * multiple impl algos
  261. *
  262. * \tparam Opr the operator class
  263. * \tparam nargs number of arguments
  264. */
  265. template <class Opr, int nargs>
  266. class MultiAlgoOpr;
  267. //! base def
  268. template <class Opr>
  269. class MultiAlgoOpr<Opr, -1> {
  270. public:
  271. using AlgorithmInfo = detail::Algorithm::Info;
  272. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  273. using Algorithm = detail::Algorithm;
  274. /*!
  275. * \brief get a string representation for current algorithm set;
  276. *
  277. * get_all_algorithms_safe() may return different algorithms only if
  278. * algorithm set name differs. This is used for checking cache
  279. * validity.
  280. */
  281. virtual const char* get_algorithm_set_name() const = 0;
  282. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  283. const ExecutionPolicy& execution_policy() const { return m_execution_policy; }
  284. virtual Algorithm* get_algorithm_from_desc(const AlgorithmDesc&) = 0;
  285. protected:
  286. ~MultiAlgoOpr() = default;
  287. private:
  288. ExecutionPolicy m_execution_policy;
  289. };
  290. //! specialize for nargs == 2
  291. template <class Opr>
  292. class MultiAlgoOpr<Opr, 2> : public MultiAlgoOpr<Opr, -1> {
  293. public:
  294. using Algorithm = detail::Algorithm;
  295. using AlgorithmInfo = detail::Algorithm::Info;
  296. using AlgoAttribute = detail::Algorithm::Attribute;
  297. //! get all possible algorithm decriptions for the specified layouts
  298. std::vector<AlgorithmInfo> get_all_algorithms_info(
  299. const TensorLayout& p0, const TensorLayout& p1) {
  300. std::vector<AlgorithmInfo> ret;
  301. for (auto&& algo : get_all_algorithms(p0, p1)) {
  302. ret.emplace_back(algo->info());
  303. }
  304. return ret;
  305. }
  306. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  307. const TensorLayout& p0, const TensorLayout& p1) {
  308. std::vector<AlgorithmInfo> ret;
  309. for (auto&& algo : get_all_algorithms_safe(p0, p1)) {
  310. ret.emplace_back(algo->info());
  311. }
  312. return ret;
  313. }
  314. /**
  315. * \brief Returns the best algorithm information which indicate the
  316. * algorithm by heuristic.
  317. *
  318. * The selected algorithm should not use workspace more than
  319. * \p workspace_limit_in_bytes.
  320. */
  321. AlgorithmInfo get_algorithm_info_heuristic(
  322. const TensorLayout& p0, const TensorLayout& p1,
  323. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  324. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  325. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  326. return get_algorithm_heuristic(
  327. p0, p1, workspace_limit_in_bytes, positive_attr, negative_attr)
  328. ->info();
  329. }
  330. protected:
  331. ~MultiAlgoOpr() = default;
  332. //! get all possible algorithms for the specified layouts
  333. virtual std::vector<Algorithm*> get_all_algorithms(
  334. const TensorLayout& p0, const TensorLayout& p1) = 0;
  335. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  336. const TensorLayout& p0, const TensorLayout& p1) = 0;
  337. /**
  338. * \brief Returns the best algorithm by heuristic.
  339. *
  340. * The selected algorithm should not use workspace more than
  341. * \p workspace_limit_in_bytes.
  342. */
  343. virtual Algorithm* get_algorithm_heuristic(
  344. const TensorLayout& p0, const TensorLayout& p1,
  345. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  346. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  347. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  348. };
  349. //! specialize for nargs == 3
  350. template <class Opr>
  351. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  352. public:
  353. using Algorithm = detail::Algorithm;
  354. using AlgorithmInfo = detail::Algorithm::Info;
  355. using AlgoAttribute = detail::Algorithm::Attribute;
  356. //! get all possible algorithm decriptions for the specified layouts
  357. std::vector<AlgorithmInfo> get_all_algorithms_info(
  358. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) {
  359. std::vector<AlgorithmInfo> ret;
  360. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  361. ret.emplace_back(algo->info());
  362. }
  363. return ret;
  364. }
  365. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  366. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) {
  367. std::vector<AlgorithmInfo> ret;
  368. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2)) {
  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. * \p workspace_limit_in_bytes.
  379. */
  380. AlgorithmInfo get_algorithm_info_heuristic(
  381. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  382. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  383. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  384. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  385. return get_algorithm_heuristic(
  386. p0, p1, p2, workspace_limit_in_bytes, positive_attr,
  387. negative_attr)
  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, const TensorLayout& p2) = 0;
  395. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  396. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) = 0;
  397. /**
  398. * \brief Returns the best algorithm by heuristic.
  399. *
  400. * The selected algorithm should not use workspace more than
  401. * \p workspace_limit_in_bytes.
  402. */
  403. virtual Algorithm* get_algorithm_heuristic(
  404. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  405. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  406. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  407. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  408. };
  409. //! specializae for nargs == 4
  410. template <class Opr>
  411. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  412. public:
  413. using Algorithm = detail::Algorithm;
  414. using AlgorithmInfo = detail::Algorithm::Info;
  415. using AlgoAttribute = detail::Algorithm::Attribute;
  416. //! get all possible algorithm decriptions for the specified layouts
  417. std::vector<AlgorithmInfo> get_all_algorithms_info(
  418. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  419. const TensorLayout& p3) {
  420. std::vector<AlgorithmInfo> ret;
  421. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  422. ret.emplace_back(algo->info());
  423. }
  424. return ret;
  425. }
  426. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  427. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  428. const TensorLayout& p3) {
  429. std::vector<AlgorithmInfo> ret;
  430. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3)) {
  431. ret.emplace_back(algo->info());
  432. }
  433. return ret;
  434. }
  435. /**
  436. * \brief Returns the best algorithm information which indicate the
  437. * algorithm by heuristic.
  438. *
  439. * The selected algorithm should not use workspace more than
  440. * \p workspace_limit_in_bytes.
  441. */
  442. AlgorithmInfo get_algorithm_info_heuristic(
  443. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  444. const TensorLayout& p3,
  445. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  446. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  447. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  448. return get_algorithm_heuristic(
  449. p0, p1, p2, p3, workspace_limit_in_bytes, positive_attr,
  450. negative_attr)
  451. ->info();
  452. }
  453. protected:
  454. ~MultiAlgoOpr() = default;
  455. //! get all possible algorithms for the specified layouts
  456. virtual std::vector<Algorithm*> get_all_algorithms(
  457. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  458. const TensorLayout& p3) = 0;
  459. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  460. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  461. const TensorLayout& p3) = 0;
  462. /**
  463. * \brief Returns the best algorithm by heuristic.
  464. *
  465. * The selected algorithm should not use workspace more than
  466. * \p workspace_limit_in_bytes.
  467. */
  468. virtual Algorithm* get_algorithm_heuristic(
  469. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  470. const TensorLayout& p3,
  471. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  472. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  473. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  474. };
  475. //! specializae for nargs == 5
  476. template <class Opr>
  477. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  478. public:
  479. using Algorithm = detail::Algorithm;
  480. using AlgorithmInfo = detail::Algorithm::Info;
  481. using AlgoAttribute = detail::Algorithm::Attribute;
  482. //! get all possible algorithm decriptions for the specified layouts
  483. std::vector<AlgorithmInfo> get_all_algorithms_info(
  484. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  485. const TensorLayout& p3, const TensorLayout& p4) {
  486. std::vector<AlgorithmInfo> ret;
  487. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  488. ret.emplace_back(algo->info());
  489. }
  490. return ret;
  491. }
  492. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  493. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  494. const TensorLayout& p3, const TensorLayout& p4) {
  495. std::vector<AlgorithmInfo> ret;
  496. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4)) {
  497. ret.emplace_back(algo->info());
  498. }
  499. return ret;
  500. }
  501. /**
  502. * \brief Returns the best algorithm information which indicate the
  503. * algorithm by heuristic.
  504. *
  505. * The selected algorithm should not use workspace more than
  506. * \p workspace_limit_in_bytes.
  507. */
  508. AlgorithmInfo get_algorithm_info_heuristic(
  509. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  510. const TensorLayout& p3, const TensorLayout& p4,
  511. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  512. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  513. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  514. return get_algorithm_heuristic(
  515. p0, p1, p2, p3, p4, workspace_limit_in_bytes, positive_attr,
  516. negative_attr)
  517. ->info();
  518. }
  519. protected:
  520. ~MultiAlgoOpr() = default;
  521. //! get all possible algorithms for the specified layouts
  522. virtual std::vector<Algorithm*> get_all_algorithms(
  523. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  524. const TensorLayout& p3, const TensorLayout& p4) = 0;
  525. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  526. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  527. const TensorLayout& p3, const TensorLayout& p4) = 0;
  528. /**
  529. * \brief Returns the best algorithm by heuristic.
  530. *
  531. * The selected algorithm should not use workspace more than
  532. * \p workspace_limit_in_bytes.
  533. */
  534. virtual Algorithm* get_algorithm_heuristic(
  535. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  536. const TensorLayout& p3, const TensorLayout& p4,
  537. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  538. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  539. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  540. };
  541. //! specializae for nargs == 8
  542. template <class Opr>
  543. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  544. public:
  545. using Algorithm = detail::Algorithm;
  546. using AlgorithmInfo = detail::Algorithm::Info;
  547. using AlgoAttribute = detail::Algorithm::Attribute;
  548. //! get all possible algorithm decriptions for the specified layouts
  549. std::vector<AlgorithmInfo> get_all_algorithms_info(
  550. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  551. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  552. const TensorLayout& p6, const TensorLayout& p7) {
  553. std::vector<AlgorithmInfo> ret;
  554. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  555. ret.emplace_back(algo->info());
  556. }
  557. return ret;
  558. }
  559. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  560. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  561. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  562. const TensorLayout& p6, const TensorLayout& p7) {
  563. std::vector<AlgorithmInfo> ret;
  564. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4, p5, p6, p7)) {
  565. ret.emplace_back(algo->info());
  566. }
  567. return ret;
  568. }
  569. /**
  570. * \brief Returns the best algorithm information which indicate the
  571. * algorithm by heuristic.
  572. *
  573. * The selected algorithm should not use workspace more than
  574. */
  575. AlgorithmInfo get_algorithm_info_heuristic(
  576. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  577. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  578. const TensorLayout& p6, const TensorLayout& p7,
  579. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  580. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  581. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  582. return get_algorithm_heuristic(
  583. p0, p1, p2, p3, p4, p5, p6, p7, workspace_limit_in_bytes,
  584. positive_attr, negative_attr)
  585. ->info();
  586. }
  587. protected:
  588. ~MultiAlgoOpr() = default;
  589. //! get all possible algorithms for the specified layouts
  590. virtual std::vector<Algorithm*> get_all_algorithms(
  591. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  592. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  593. const TensorLayout& p6, const TensorLayout& p7) = 0;
  594. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  595. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  596. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  597. const TensorLayout& p6, const TensorLayout& p7) = 0;
  598. /**
  599. * \brief Returns the best algorithm by heuristic.
  600. *
  601. * The selected algorithm should not use workspace more than
  602. * \p workspace_limit_in_bytes.
  603. */
  604. virtual Algorithm* get_algorithm_heuristic(
  605. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  606. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  607. const TensorLayout& p6, const TensorLayout& p7,
  608. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  609. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  610. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  611. };
  612. } // namespace detail
  613. using Algorithm = detail::Algorithm;
  614. using AlgoAttribute = Algorithm::Attribute;
  615. using ExecutionPolicy = detail::ExecutionPolicy;
  616. } // namespace megdnn
  617. #include "megdnn/internal/visibility_epilogue.h"
  618. // vim: syntax=cpp.doxygen