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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. };
  229. struct SearchItem {
  230. OprType opr_type;
  231. //! serialized param
  232. std::string param;
  233. TensorLayoutArray layouts;
  234. };
  235. /**
  236. * \brief get subopr list of the algo
  237. *
  238. * \param layouts origin layouts of the parent opr
  239. * \param opr parent opr
  240. */
  241. virtual std::vector<SearchItem> get_subopr_list(
  242. const TensorLayoutArray&, const OperatorBase*) const {
  243. return {};
  244. }
  245. protected:
  246. Handle::HandleType m_handle_type = Handle::HandleType::NAIVE;
  247. };
  248. MEGDNN_DEF_ENUM_CLASS_BIT_OPR(Algorithm::Attribute)
  249. //! policy for executing the operator
  250. struct ExecutionPolicy {
  251. //! INVALID_ALGO_TYPE algo_type means using heuristic
  252. Algorithm::Info::Desc algo;
  253. std::vector<ExecutionPolicy> sub_policy;
  254. };
  255. /*!
  256. * \brief define Algorithm and ExecutionPolicy for oprs that have
  257. * multiple impl algos
  258. *
  259. * \tparam Opr the operator class
  260. * \tparam nargs number of arguments
  261. */
  262. template <class Opr, int nargs>
  263. class MultiAlgoOpr;
  264. //! base def
  265. template <class Opr>
  266. class MultiAlgoOpr<Opr, -1> {
  267. public:
  268. using AlgorithmInfo = detail::Algorithm::Info;
  269. using AlgorithmDesc = detail::Algorithm::Info::Desc;
  270. using Algorithm = detail::Algorithm;
  271. /*!
  272. * \brief get a string representation for current algorithm set;
  273. *
  274. * get_all_algorithms_safe() may return different algorithms only if
  275. * algorithm set name differs. This is used for checking cache
  276. * validity.
  277. */
  278. virtual const char* get_algorithm_set_name() const = 0;
  279. ExecutionPolicy& execution_policy() { return m_execution_policy; }
  280. const ExecutionPolicy& execution_policy() const { return m_execution_policy; }
  281. virtual Algorithm* get_algorithm_from_desc(const AlgorithmDesc&) = 0;
  282. protected:
  283. ~MultiAlgoOpr() = default;
  284. private:
  285. ExecutionPolicy m_execution_policy;
  286. };
  287. //! specialize for nargs == 2
  288. template <class Opr>
  289. class MultiAlgoOpr<Opr, 2> : public MultiAlgoOpr<Opr, -1> {
  290. public:
  291. using Algorithm = detail::Algorithm;
  292. using AlgorithmInfo = detail::Algorithm::Info;
  293. using AlgoAttribute = detail::Algorithm::Attribute;
  294. //! get all possible algorithm decriptions for the specified layouts
  295. std::vector<AlgorithmInfo> get_all_algorithms_info(
  296. const TensorLayout& p0, const TensorLayout& p1) {
  297. std::vector<AlgorithmInfo> ret;
  298. for (auto&& algo : get_all_algorithms(p0, p1)) {
  299. ret.emplace_back(algo->info());
  300. }
  301. return ret;
  302. }
  303. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  304. const TensorLayout& p0, const TensorLayout& p1) {
  305. std::vector<AlgorithmInfo> ret;
  306. for (auto&& algo : get_all_algorithms_safe(p0, p1)) {
  307. ret.emplace_back(algo->info());
  308. }
  309. return ret;
  310. }
  311. /**
  312. * \brief Returns the best algorithm information which indicate the
  313. * algorithm by heuristic.
  314. *
  315. * The selected algorithm should not use workspace more than
  316. * \p workspace_limit_in_bytes.
  317. */
  318. AlgorithmInfo get_algorithm_info_heuristic(
  319. const TensorLayout& p0, const TensorLayout& p1,
  320. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  321. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  322. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  323. return get_algorithm_heuristic(
  324. p0, p1, workspace_limit_in_bytes, positive_attr, negative_attr)
  325. ->info();
  326. }
  327. protected:
  328. ~MultiAlgoOpr() = default;
  329. //! get all possible algorithms for the specified layouts
  330. virtual std::vector<Algorithm*> get_all_algorithms(
  331. const TensorLayout& p0, const TensorLayout& p1) = 0;
  332. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  333. const TensorLayout& p0, const TensorLayout& p1) = 0;
  334. /**
  335. * \brief Returns the best algorithm by heuristic.
  336. *
  337. * The selected algorithm should not use workspace more than
  338. * \p workspace_limit_in_bytes.
  339. */
  340. virtual Algorithm* get_algorithm_heuristic(
  341. const TensorLayout& p0, const TensorLayout& p1,
  342. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  343. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  344. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  345. };
  346. //! specialize for nargs == 3
  347. template <class Opr>
  348. class MultiAlgoOpr<Opr, 3> : public MultiAlgoOpr<Opr, -1> {
  349. public:
  350. using Algorithm = detail::Algorithm;
  351. using AlgorithmInfo = detail::Algorithm::Info;
  352. using AlgoAttribute = detail::Algorithm::Attribute;
  353. //! get all possible algorithm decriptions for the specified layouts
  354. std::vector<AlgorithmInfo> get_all_algorithms_info(
  355. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) {
  356. std::vector<AlgorithmInfo> ret;
  357. for (auto&& algo : get_all_algorithms(p0, p1, p2)) {
  358. ret.emplace_back(algo->info());
  359. }
  360. return ret;
  361. }
  362. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  363. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) {
  364. std::vector<AlgorithmInfo> ret;
  365. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2)) {
  366. ret.emplace_back(algo->info());
  367. }
  368. return ret;
  369. }
  370. /**
  371. * \brief Returns the best algorithm information which indicate the
  372. * algorithm by heuristic.
  373. *
  374. * The selected algorithm should not use workspace more than
  375. * \p workspace_limit_in_bytes.
  376. */
  377. AlgorithmInfo get_algorithm_info_heuristic(
  378. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  379. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  380. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  381. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  382. return get_algorithm_heuristic(
  383. p0, p1, p2, workspace_limit_in_bytes, positive_attr,
  384. negative_attr)
  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, const TensorLayout& p2) = 0;
  392. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  393. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2) = 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, const TensorLayout& p2,
  402. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  403. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  404. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  405. };
  406. //! specializae for nargs == 4
  407. template <class Opr>
  408. class MultiAlgoOpr<Opr, 4> : public MultiAlgoOpr<Opr, -1> {
  409. public:
  410. using Algorithm = detail::Algorithm;
  411. using AlgorithmInfo = detail::Algorithm::Info;
  412. using AlgoAttribute = detail::Algorithm::Attribute;
  413. //! get all possible algorithm decriptions for the specified layouts
  414. std::vector<AlgorithmInfo> get_all_algorithms_info(
  415. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  416. const TensorLayout& p3) {
  417. std::vector<AlgorithmInfo> ret;
  418. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3)) {
  419. ret.emplace_back(algo->info());
  420. }
  421. return ret;
  422. }
  423. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  424. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  425. const TensorLayout& p3) {
  426. std::vector<AlgorithmInfo> ret;
  427. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3)) {
  428. ret.emplace_back(algo->info());
  429. }
  430. return ret;
  431. }
  432. /**
  433. * \brief Returns the best algorithm information which indicate the
  434. * algorithm by heuristic.
  435. *
  436. * The selected algorithm should not use workspace more than
  437. * \p workspace_limit_in_bytes.
  438. */
  439. AlgorithmInfo get_algorithm_info_heuristic(
  440. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  441. const TensorLayout& p3,
  442. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  443. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  444. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  445. return get_algorithm_heuristic(
  446. p0, p1, p2, p3, workspace_limit_in_bytes, positive_attr,
  447. negative_attr)
  448. ->info();
  449. }
  450. protected:
  451. ~MultiAlgoOpr() = default;
  452. //! get all possible algorithms for the specified layouts
  453. virtual std::vector<Algorithm*> get_all_algorithms(
  454. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  455. const TensorLayout& p3) = 0;
  456. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  457. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  458. const TensorLayout& p3) = 0;
  459. /**
  460. * \brief Returns the best algorithm by heuristic.
  461. *
  462. * The selected algorithm should not use workspace more than
  463. * \p workspace_limit_in_bytes.
  464. */
  465. virtual Algorithm* get_algorithm_heuristic(
  466. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  467. const TensorLayout& p3,
  468. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  469. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  470. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  471. };
  472. //! specializae for nargs == 5
  473. template <class Opr>
  474. class MultiAlgoOpr<Opr, 5> : public MultiAlgoOpr<Opr, -1> {
  475. public:
  476. using Algorithm = detail::Algorithm;
  477. using AlgorithmInfo = detail::Algorithm::Info;
  478. using AlgoAttribute = detail::Algorithm::Attribute;
  479. //! get all possible algorithm decriptions for the specified layouts
  480. std::vector<AlgorithmInfo> get_all_algorithms_info(
  481. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  482. const TensorLayout& p3, const TensorLayout& p4) {
  483. std::vector<AlgorithmInfo> ret;
  484. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4)) {
  485. ret.emplace_back(algo->info());
  486. }
  487. return ret;
  488. }
  489. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  490. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  491. const TensorLayout& p3, const TensorLayout& p4) {
  492. std::vector<AlgorithmInfo> ret;
  493. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4)) {
  494. ret.emplace_back(algo->info());
  495. }
  496. return ret;
  497. }
  498. /**
  499. * \brief Returns the best algorithm information which indicate the
  500. * algorithm by heuristic.
  501. *
  502. * The selected algorithm should not use workspace more than
  503. * \p workspace_limit_in_bytes.
  504. */
  505. AlgorithmInfo get_algorithm_info_heuristic(
  506. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  507. const TensorLayout& p3, const TensorLayout& p4,
  508. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  509. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  510. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  511. return get_algorithm_heuristic(
  512. p0, p1, p2, p3, p4, workspace_limit_in_bytes, positive_attr,
  513. negative_attr)
  514. ->info();
  515. }
  516. protected:
  517. ~MultiAlgoOpr() = default;
  518. //! get all possible algorithms for the specified layouts
  519. virtual std::vector<Algorithm*> get_all_algorithms(
  520. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  521. const TensorLayout& p3, const TensorLayout& p4) = 0;
  522. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  523. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  524. const TensorLayout& p3, const TensorLayout& p4) = 0;
  525. /**
  526. * \brief Returns the best algorithm by heuristic.
  527. *
  528. * The selected algorithm should not use workspace more than
  529. * \p workspace_limit_in_bytes.
  530. */
  531. virtual Algorithm* get_algorithm_heuristic(
  532. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  533. const TensorLayout& p3, const TensorLayout& p4,
  534. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  535. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  536. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  537. };
  538. //! specializae for nargs == 8
  539. template <class Opr>
  540. class MultiAlgoOpr<Opr, 8> : public MultiAlgoOpr<Opr, -1> {
  541. public:
  542. using Algorithm = detail::Algorithm;
  543. using AlgorithmInfo = detail::Algorithm::Info;
  544. using AlgoAttribute = detail::Algorithm::Attribute;
  545. //! get all possible algorithm decriptions for the specified layouts
  546. std::vector<AlgorithmInfo> get_all_algorithms_info(
  547. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  548. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  549. const TensorLayout& p6, const TensorLayout& p7) {
  550. std::vector<AlgorithmInfo> ret;
  551. for (auto&& algo : get_all_algorithms(p0, p1, p2, p3, p4, p5, p6, p7)) {
  552. ret.emplace_back(algo->info());
  553. }
  554. return ret;
  555. }
  556. std::vector<AlgorithmInfo> get_all_algorithms_info_safe(
  557. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  558. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  559. const TensorLayout& p6, const TensorLayout& p7) {
  560. std::vector<AlgorithmInfo> ret;
  561. for (auto&& algo : get_all_algorithms_safe(p0, p1, p2, p3, p4, p5, p6, p7)) {
  562. ret.emplace_back(algo->info());
  563. }
  564. return ret;
  565. }
  566. /**
  567. * \brief Returns the best algorithm information which indicate the
  568. * algorithm by heuristic.
  569. *
  570. * The selected algorithm should not use workspace more than
  571. */
  572. AlgorithmInfo get_algorithm_info_heuristic(
  573. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  574. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  575. const TensorLayout& p6, const TensorLayout& p7,
  576. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  577. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  578. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  579. return get_algorithm_heuristic(
  580. p0, p1, p2, p3, p4, p5, p6, p7, workspace_limit_in_bytes,
  581. positive_attr, negative_attr)
  582. ->info();
  583. }
  584. protected:
  585. ~MultiAlgoOpr() = default;
  586. //! get all possible algorithms for the specified layouts
  587. virtual std::vector<Algorithm*> get_all_algorithms(
  588. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  589. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  590. const TensorLayout& p6, const TensorLayout& p7) = 0;
  591. virtual std::vector<Algorithm*> get_all_algorithms_safe(
  592. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  593. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  594. const TensorLayout& p6, const TensorLayout& p7) = 0;
  595. /**
  596. * \brief Returns the best algorithm by heuristic.
  597. *
  598. * The selected algorithm should not use workspace more than
  599. * \p workspace_limit_in_bytes.
  600. */
  601. virtual Algorithm* get_algorithm_heuristic(
  602. const TensorLayout& p0, const TensorLayout& p1, const TensorLayout& p2,
  603. const TensorLayout& p3, const TensorLayout& p4, const TensorLayout& p5,
  604. const TensorLayout& p6, const TensorLayout& p7,
  605. size_t workspace_limit_in_bytes = std::numeric_limits<size_t>::max(),
  606. const AlgoAttribute& positive_attr = AlgoAttribute::DEFAULT,
  607. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) = 0;
  608. };
  609. } // namespace detail
  610. using Algorithm = detail::Algorithm;
  611. using AlgoAttribute = Algorithm::Attribute;
  612. using ExecutionPolicy = detail::ExecutionPolicy;
  613. } // namespace megdnn
  614. #include "megdnn/internal/visibility_epilogue.h"
  615. // vim: syntax=cpp.doxygen