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.

general.h 52 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. #pragma once
  2. #include "megdnn/internal/opr_header_prologue.h"
  3. #include "megdnn/thin/small_vector.h"
  4. namespace megdnn {
  5. /*!
  6. * \brief standard element-wise operator
  7. *
  8. * Inputs must have same dtype, and their shapes must broadcastable into a final
  9. * shape. They can have arbitrary layouts, but non-contiguous and non-broadcast
  10. * layouts may harm performance seriously.
  11. *
  12. * Output dtype is the same as input dtype (note that even for compare oprs this
  13. * is true, e.g. float == float returns value of float). Output layout must be
  14. * contiguous.
  15. */
  16. class ElemwiseForward : public OperatorBase {
  17. DEF_OPR_PARAM(Elemwise);
  18. DEF_OPR_IMPL(ElemwiseForward, OperatorBase, -1, 1);
  19. public:
  20. using Mode = Param::Mode;
  21. //! information about a mode
  22. struct ModeTrait {
  23. uint32_t arity; //!< number of inputs needed
  24. bool commutable; //!< whether arity == 2 and inputs commutable
  25. bool allow_int; //!< whether int inputs allowed
  26. bool allow_float; //!< whether float inputs allowed
  27. bool allow_bool; //!< whether bool inputs allowed
  28. const char* name; //!< name of the mode
  29. ModeTrait()
  30. : arity(0),
  31. commutable(0),
  32. allow_int(0),
  33. allow_float(0),
  34. allow_bool(0),
  35. name(NULL) {}
  36. //! get trait from a mode; this function is thread safe
  37. MGE_WIN_DECLSPEC_FUC static const ModeTrait& from_mode(Mode mode);
  38. };
  39. //! get trait of current mode
  40. const ModeTrait& mode_trait() const { return ModeTrait::from_mode(m_param.mode); }
  41. /**
  42. * \param[in] src input tensor
  43. * \param[out] dst output tensor
  44. *
  45. * src and dst should have the same shape;
  46. * layouts should be contiguous;
  47. * the underlying data pointer can point to the same memory region for
  48. * src and dst.
  49. */
  50. virtual void exec(_megdnn_in const TensorNDArray& src, _megdnn_tensor_out dst) = 0;
  51. //! deduce output shape (do not check whether arity matches)
  52. MGE_WIN_DECLSPEC_FUC static void deduce_shape(
  53. const TensorShapeArray& src, TensorShape& dst);
  54. MGE_WIN_DECLSPEC_FUC static void deduce_format(
  55. const TensorFormatArray& src, TensorFormat& dst);
  56. //! deduce output layout
  57. MGE_WIN_DECLSPEC_FUC void deduce_layout(
  58. const TensorLayoutArray& src, TensorLayout& dst);
  59. protected:
  60. //! throw exception if incorrect layout; broadcast input shape to
  61. //! output shape
  62. MGE_WIN_DECLSPEC_FUC void check_layout_and_broadcast(
  63. const TensorLayoutPtrArray& src, const TensorLayout& dst);
  64. private:
  65. void check_dtype(DType dtype);
  66. };
  67. using Elemwise = ElemwiseForward;
  68. /*!
  69. * \brief compute ``x**a`` where ``a`` is a constant from the Param
  70. *
  71. * This opr is usually not directly accessible by the end user and it is created
  72. * by mgb optimizer, aiming to work around numerical stability issues with pow.
  73. * For example ``powf(x, 2.f)`` with ``x < 0`` in fast math mode may return NaN.
  74. *
  75. * Like elemwise, this opr supports arbitrary strides. But it should only be
  76. * used with monotone strides. Input and output should have the same
  77. * float-category dtype.
  78. */
  79. class PowC : public OperatorBase {
  80. DEF_OPR_PARAM(PowC);
  81. DEF_OPR_IMPL(PowC, OperatorBase, 1, 1);
  82. public:
  83. void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst);
  84. //! compatible API for mgb; workspace is not used
  85. void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst, _megdnn_workspace) {
  86. return exec(src, dst);
  87. }
  88. size_t get_workspace_in_bytes(const TensorLayout&, const TensorLayout&) {
  89. // the impls should require no workspace; this can be later changed to a
  90. // virtual function if this situation changes
  91. return 0;
  92. }
  93. void deduce_layout(const TensorLayout& src, TensorLayout& dst) {
  94. dst.dtype = src.dtype;
  95. dst.init_contiguous_stride(src);
  96. }
  97. protected:
  98. /*!
  99. * Perform the computing where layouts have been verified.
  100. *
  101. * \p src can have arbitrary layout, and \p dst is contiguous. They have the
  102. * same shape and dtype.
  103. *
  104. * The implementation should not access param(). It should check \p exp_f
  105. * and \p exp_i for the exponent value. Exactly one of them would be
  106. * non-null.
  107. *
  108. * Note: \p exp_f and \p exp_i must be dereferenced before dispatching any
  109. * kernel. They are allocated on the caller's stack.
  110. */
  111. virtual void do_exec(
  112. _megdnn_tensor_in src, _megdnn_tensor_out dst, const float* exp_f,
  113. const int* exp_i) = 0;
  114. };
  115. /*!
  116. * \brief modify a tensor inplace by adding another tensor to it
  117. *
  118. * dst and delta can have arbitrary layout but must have the same shape.
  119. */
  120. class AddUpdateForward : public OperatorBase {
  121. DEF_OPR_PARAM(AddUpdate);
  122. DEF_OPR_IMPL(AddUpdateForward, OperatorBase, -1, 1);
  123. public:
  124. virtual void exec(_megdnn_tensor_inout dst, _megdnn_tensor_in delta) = 0;
  125. protected:
  126. void check_exec(const TensorLayout& dst, const TensorLayout& delta);
  127. };
  128. using AddUpdate = AddUpdateForward;
  129. class ReduceForward : public OperatorBase {
  130. DEF_OPR_PARAM(Reduce);
  131. DEF_OPR_IMPL(ReduceForward, OperatorBase, 1, 1);
  132. public:
  133. using Mode = Param::Mode;
  134. using DataType = Param::DataType;
  135. /**
  136. * \param[in] src input tensor
  137. * \param[out] dst output tensor
  138. *
  139. * src and dst should be contiguous.
  140. * src and dst should be of the same shape for all dimensions except
  141. * param().axis.
  142. * the param().axis-th dimension shape for dst should be one.
  143. */
  144. virtual void exec(
  145. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  146. _megdnn_workspace workspace) = 0;
  147. MGE_WIN_DECLSPEC_FUC void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  148. virtual size_t get_workspace_in_bytes(
  149. const TensorLayout& src, const TensorLayout& dst) = 0;
  150. protected:
  151. void check_exec(
  152. const TensorLayout& src, const TensorLayout& dst,
  153. size_t workspace_in_bytes);
  154. };
  155. using Reduce = ReduceForward;
  156. class CorrelationBase : public OperatorBase {
  157. DEF_OPR_IMPL_CTOR(CorrelationBase, OperatorBase);
  158. DEF_OPR_PARAM(Correlation);
  159. protected:
  160. void deduce_layout_fwd(
  161. const TensorLayout& data1, const TensorLayout& data2, TensorLayout& dst);
  162. void check_layout_fwd(
  163. const TensorLayout& data1, const TensorLayout& data2,
  164. const TensorLayout& dst);
  165. };
  166. class CorrelationForward : public CorrelationBase {
  167. DEF_OPR_IMPL(CorrelationForward, CorrelationBase, 2, 1);
  168. public:
  169. /**
  170. * \param[in] data1 (n, c, ih, iw)
  171. * \param[in] data2 (n, c, ih, iw)
  172. * \param[out] dst (n, q, oh, ow), q is the number of neighborhood
  173. * */
  174. virtual void exec(
  175. _megdnn_tensor_in data1, _megdnn_tensor_in data2, _megdnn_tensor_out dst,
  176. _megdnn_workspace workspace) = 0;
  177. void deduce_layout(
  178. const TensorLayout& data1, const TensorLayout& data2, TensorLayout& dst);
  179. virtual size_t get_workspace_in_bytes(
  180. const TensorLayout& data1, const TensorLayout& data2,
  181. const TensorLayout& dst) = 0;
  182. protected:
  183. void check_exec(
  184. const TensorLayout& data1, const TensorLayout& data2,
  185. const TensorLayout& dst, size_t workspace_in_bytes);
  186. };
  187. using Correlation = CorrelationForward;
  188. class CorrelationBackwardData1 : public CorrelationBase {
  189. DEF_OPR_IMPL(CorrelationBackwardData1, CorrelationBase, 3, 1);
  190. public:
  191. /**
  192. * \param[in] diff the backpropagated gradient wrt. dst
  193. * \param[in] data1 the `data1' parameter in CorrelationForward::exec
  194. * \param[in] data2 the `data2' parameter in CorrelationForward::exec
  195. * \param[out] grad1 the backpropagated gradient wrt. data1
  196. */
  197. virtual void exec(
  198. _megdnn_tensor_in diff, _megdnn_tensor_in data1, _megdnn_tensor_in data2,
  199. _megdnn_tensor_out grad1, _megdnn_workspace workspace) = 0;
  200. void deduce_layout(
  201. const TensorLayout& diff1, const TensorLayout& data1,
  202. const TensorLayout& data2, TensorLayout& dst);
  203. virtual size_t get_workspace_in_bytes(
  204. const TensorLayout& diff, const TensorLayout& data1,
  205. const TensorLayout& data2, const TensorLayout& grad1) = 0;
  206. protected:
  207. void check_exec(
  208. const TensorLayout& diff, const TensorLayout& data1,
  209. const TensorLayout& data2, const TensorLayout& grad1,
  210. size_t workspace_in_bytes);
  211. };
  212. class CorrelationBackwardData2 : public CorrelationBase {
  213. DEF_OPR_IMPL(CorrelationBackwardData2, CorrelationBase, 3, 1);
  214. public:
  215. /**
  216. * \param[in] diff the backpropagated gradient wrt. dst
  217. * \param[in] data1 the `data1' parameter in CorrelationForward::exec
  218. * \param[in] data2 the `data2' parameter in CorrelationForward::exec
  219. * \param[out] grad2 the backpropagated gradient wrt. data2
  220. */
  221. virtual void exec(
  222. _megdnn_tensor_in diff, _megdnn_tensor_in data1, _megdnn_tensor_in data2,
  223. _megdnn_tensor_out grad2, _megdnn_workspace workspace) = 0;
  224. void deduce_layout(
  225. const TensorLayout& diff1, const TensorLayout& data1,
  226. const TensorLayout& data2, TensorLayout& dst);
  227. virtual size_t get_workspace_in_bytes(
  228. const TensorLayout& diff, const TensorLayout& data1,
  229. const TensorLayout& data2, const TensorLayout& grad2) = 0;
  230. protected:
  231. void check_exec(
  232. const TensorLayout& diff, const TensorLayout& data1,
  233. const TensorLayout& data2, const TensorLayout& grad2,
  234. size_t workspace_in_bytes);
  235. };
  236. class CumsumForward : public OperatorBase {
  237. DEF_OPR_PARAM(Cumsum);
  238. DEF_OPR_IMPL(CumsumForward, OperatorBase, 1, 1);
  239. public:
  240. /**
  241. * \param[in] src input tensor
  242. * \param[out] dst output tensor
  243. *
  244. * src and dst should be contiguous.
  245. * src and dst should have the same shape.
  246. *
  247. * The exclusive flag specifies whether the current element it taken
  248. * into account when calculating results.
  249. *
  250. * The reverse flag specifies whether cumsum is forward (
  251. * from 0 to n) or backward (from n downto 0).
  252. *
  253. * Example:
  254. * exclusive && reverse:
  255. * dst_i = src_{i+1} + src_{i+2} + ... + src_{n-1}
  256. * exclusive && !reverse
  257. * dst_i = src_0 + src_1 + ... + src_{i-1}
  258. * !exclusive && reverse:
  259. * dst_i = src_i + src_{i+1} + ... + src_{n-1}
  260. * !exclusive && !reverse:
  261. * dst_i = src_0 + src_1 + ... + src_i
  262. */
  263. virtual void exec(
  264. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  265. _megdnn_workspace workspace) = 0;
  266. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  267. virtual size_t get_workspace_in_bytes(
  268. const TensorLayout& src, const TensorLayout& dst) = 0;
  269. protected:
  270. void check_exec(
  271. const TensorLayout& src, const TensorLayout& dst,
  272. size_t workspace_in_bytes);
  273. };
  274. using Cumsum = CumsumForward;
  275. // mxx can be max or min
  276. class ArgmxxBase : public OperatorBase {
  277. DEF_OPR_IMPL_CTOR(ArgmxxBase, OperatorBase);
  278. DEF_OPR_PARAM(Axis);
  279. protected:
  280. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  281. };
  282. class ArgmaxForward : public ArgmxxBase {
  283. DEF_OPR_IMPL(ArgmaxForward, ArgmxxBase, 1, 1);
  284. public:
  285. /**
  286. * \param[in] src input tensor
  287. * \param[out] dst output tensor containing the argmax indices
  288. *
  289. * src and dst should be contiguous.
  290. * src and dst should be of the same shape for all dimensions except
  291. * param().axis.
  292. * the param().axis-th dimension shape for dst should be one.
  293. */
  294. virtual void exec(
  295. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  296. _megdnn_workspace workspace) = 0;
  297. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  298. virtual size_t get_workspace_in_bytes(
  299. const TensorLayout& src, const TensorLayout& dst) = 0;
  300. protected:
  301. void check_exec(
  302. const TensorLayout& src, const TensorLayout& dst,
  303. size_t workspace_in_bytes);
  304. };
  305. using Argmax = ArgmaxForward;
  306. class ArgminForward : public ArgmxxBase {
  307. DEF_OPR_IMPL(ArgminForward, ArgmxxBase, 1, 1);
  308. public:
  309. /**
  310. * \param[in] src input tensor
  311. * \param[out] dst output tensor containing the argmax indices
  312. *
  313. * src and dst should be contiguous.
  314. * src and dst should be of the same shape for all dimensions except
  315. * param().axis.
  316. * the param().axis-th dimension shape for dst should be one.
  317. */
  318. virtual void exec(
  319. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  320. _megdnn_workspace workspace) = 0;
  321. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  322. virtual size_t get_workspace_in_bytes(
  323. const TensorLayout& src, const TensorLayout& dst) = 0;
  324. protected:
  325. void check_exec(
  326. const TensorLayout& src, const TensorLayout& dst,
  327. size_t workspace_in_bytes);
  328. };
  329. using Argmin = ArgminForward;
  330. /*!
  331. * \brief take values from input according to given condition
  332. *
  333. * Output two tensors:
  334. * 1. values copied from *data*, with same dtype as *data*
  335. * 2. selected indices with dtype int32; note that it is 1-dimensional and
  336. * based on the flatten input.
  337. *
  338. * Require data and mask to have the same shape and both be contiguous.
  339. */
  340. class CondTake : public OperatorBase {
  341. DEF_OPR_IMPL(CondTake, OperatorBase, 2, 2);
  342. DEF_OPR_PARAM(CondTake);
  343. public:
  344. using Output = std::array<TensorND, 2>;
  345. using OutputDType = std::array<DType, 2>;
  346. OutputDType infer_dtype(DType data, DType mask);
  347. virtual size_t get_workspace_in_bytes(
  348. const TensorLayout& data, const TensorLayout& mask) = 0;
  349. virtual Output exec(
  350. _megdnn_tensor_in data, _megdnn_tensor_in mask, _megdnn_workspace workspace,
  351. DynOutMallocPolicyCall malloc_policy) = 0;
  352. protected:
  353. //! check input layouts and get flattened size
  354. size_t check_exec_get_size(
  355. const TensorLayout& data, const TensorLayout& mask,
  356. size_t workspace_in_bytes);
  357. };
  358. class TransposeForward : public OperatorBase {
  359. DEF_OPR_IMPL(TransposeForward, OperatorBase, 1, 1);
  360. DEF_OPR_PARAM(Empty);
  361. public:
  362. /**
  363. * \param[in] src (m, n) stride[0] >= n && stride[1] == 1
  364. * \param[out] dst (n, m) stride[0] >= m && stride[1] == 1
  365. */
  366. virtual void exec(
  367. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  368. _megdnn_workspace workspace) = 0;
  369. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  370. virtual size_t get_workspace_in_bytes(
  371. const TensorLayout& src, const TensorLayout& dst) = 0;
  372. protected:
  373. void check_exec(
  374. const TensorLayout& src, const TensorLayout& dst,
  375. size_t workspace_in_bytes);
  376. };
  377. using Transpose = TransposeForward;
  378. /**
  379. * Change a tensor to another layout that has the same dtype and total number of
  380. * elements, and non-overlapping stride.
  381. *
  382. * ON CPU:
  383. * This operator is optimized for some cases(e.g. both dst and last dim of src
  384. * are contiguous)
  385. *
  386. * ON CUDA:
  387. * More contiguous the input/output layouts, higher performance. There is also
  388. * special optimization for broadcast case.
  389. */
  390. class RelayoutForward : public OperatorBase {
  391. DEF_OPR_IMPL(RelayoutForward, OperatorBase, 1, 1);
  392. DEF_OPR_PARAM(Empty);
  393. public:
  394. /*!
  395. * \brief execute relayout opr
  396. *
  397. * This operator should be placed on the same computing device of *dst*.
  398. *
  399. * \param src_handle handle of input tensor; for CUDA d2d copy, the
  400. * src handle can be on a different GPU for copy tensor with
  401. * non-contig dims <= 2
  402. */
  403. virtual void exec(
  404. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  405. Handle* src_handle = nullptr) = 0;
  406. protected:
  407. //! check layout and collapse contiguous
  408. void check_layout_and_canonize(TensorLayout& src, TensorLayout& dst);
  409. };
  410. using Relayout = RelayoutForward;
  411. /**
  412. * \brief Base class for Concat and Split operators
  413. */
  414. class ConcatSplitBase : public OperatorBase {
  415. public:
  416. using Param = param::Axis;
  417. ConcatSplitBase(Handle* handle);
  418. const Param& param() const { return m_param; }
  419. Param& param() { return m_param; }
  420. protected:
  421. void check_layout_common(const TensorLayoutArray& srcs, const TensorLayout& dst);
  422. Param m_param;
  423. /**
  424. * \brief a helper function
  425. *
  426. * A = shape[0] * shape[1] * ... * shape[axis-1]
  427. * B = {srcs[0].shape[axis], srcs[1].shape[axis], ...}
  428. * C = shape[axis+1] * shape[axis+2] * ... * shape[ndim-1]
  429. */
  430. void get_ABC(const TensorShapeArray& srcs, size_t& A, size_t* B, size_t& C);
  431. thin_function<TensorLayout(const TensorND& tensor)> m_get_layout;
  432. thin_function<TensorShape(const TensorLayout& layout)> m_get_shape;
  433. };
  434. class ConcatForward : public ConcatSplitBase {
  435. DEF_OPR_IMPL(ConcatForward, ConcatSplitBase, 1, 1);
  436. public:
  437. /**
  438. * \param[in] srcs a vector containing all inputs to be concatenated
  439. * \param[out] dst the output tensor.
  440. *
  441. * All tensors in srcs and dst should be contiguous.
  442. * All tensors should have the same shape for all axes except
  443. * param().axis.
  444. * For the param().axis-th axis, the axis shape for dst should be the
  445. * sum of corresponding axis shapes for all srcs.
  446. */
  447. virtual void exec(
  448. _megdnn_in const TensorNDArray& srcs, _megdnn_tensor_out dst,
  449. _megdnn_workspace workspace) = 0;
  450. MGE_WIN_DECLSPEC_FUC void deduce_layout(
  451. const TensorLayoutArray& srcs, TensorLayout& dst);
  452. virtual size_t get_workspace_in_bytes(
  453. const TensorLayoutArray& srcs, const TensorLayout& dst) = 0;
  454. protected:
  455. void check_exec(
  456. const TensorLayoutArray& srcs, const TensorLayout& dst,
  457. size_t workspace_in_bytes);
  458. };
  459. using Concat = ConcatForward;
  460. class SplitForward : public ConcatSplitBase {
  461. DEF_OPR_IMPL(SplitForward, ConcatSplitBase, 1, 1);
  462. public:
  463. /**
  464. * \param[in] src input tensor
  465. * \param[out] dsts a vector containing all splitted result
  466. *
  467. * All tensors in src and dsts should be contiguous.
  468. * All tensors should have the same shape for all axes except
  469. * param().axis.
  470. * For the param().axis-th axis, the axis shape for src should be the
  471. * sum of corresponding axis shapes for all dsts.
  472. */
  473. virtual void exec(
  474. _megdnn_tensor_in src, const TensorNDArray& dsts,
  475. _megdnn_workspace workspace) = 0;
  476. virtual size_t get_workspace_in_bytes(
  477. const TensorLayout& src, const TensorLayoutArray& dsts) = 0;
  478. protected:
  479. void check_exec(
  480. const TensorLayout& src, const TensorLayoutArray& dsts,
  481. size_t workspace_in_bytes);
  482. };
  483. using Split = SplitForward;
  484. /**
  485. * \brief Base class for ParamPackConcat and ParamPackSplit Operators.
  486. *
  487. * ParamPack oprs act like Concat and Split, but they also are optimized for a
  488. * large number of inputs and can handle alignment requirements. Axis is also
  489. * not supported.
  490. *
  491. * The offsets can be generated by gen_offsets().
  492. */
  493. class ParamPackConcatSplitBase : public OperatorBase {
  494. protected:
  495. void check_exec(
  496. const TensorLayout& concated, const TensorLayout& offsets,
  497. const TensorLayout& parts);
  498. public:
  499. using Param = megdnn::param::Empty;
  500. ParamPackConcatSplitBase(Handle* handle) : OperatorBase(handle) {}
  501. //! generate offsets to be used with ParamPackConcat and ParamPackSplit
  502. MGE_WIN_DECLSPEC_FUC static std::vector<dt_int32> gen_offsets(
  503. const TensorShapeArray& shapes, size_t alignment, size_t dtype_size);
  504. };
  505. /**
  506. * \brief ParamPackConcat, used for calculating gradient of ParamPackSplit
  507. * Combine multiple gradient tensors into a single large tensor, use copy
  508. * strategy due to AddUpdate or other dynamic situation.
  509. */
  510. class ParamPackConcat : public ParamPackConcatSplitBase {
  511. DEF_OPR_IMPL(ParamPackConcat, ParamPackConcatSplitBase, 2, 1);
  512. public:
  513. /*
  514. * \param[in] srcs: TensorND on cpu. srcs[i] corresponding to the
  515. * address of i-th Tensor.
  516. * \param[in] offsets: with size `2 * srcs.shape[0]`.
  517. * offsets[i * 2] and offsets[i * 2 + 1] means
  518. * the begin and the end of srcs[i]'s offsets in dst
  519. * \param[out] dst: output TensorND, live on cpu or gpu
  520. */
  521. virtual void exec(
  522. _megdnn_tensor_in srcs, _megdnn_tensor_in offsets, _megdnn_tensor_out dst,
  523. _megdnn_workspace workspace) = 0;
  524. virtual size_t get_workspace_in_bytes(
  525. const TensorShape& srcs, const TensorShape& offsets,
  526. const TensorShape& dst) = 0;
  527. };
  528. /**
  529. * \brief base class for Tile and Repeat
  530. */
  531. class TileRepeatBase : public OperatorBase {
  532. public:
  533. TileRepeatBase(Handle* handle) : OperatorBase(handle) {}
  534. struct Param {
  535. TensorShape times;
  536. };
  537. Param& param() { return m_param; }
  538. const Param& param() const { return m_param; }
  539. protected:
  540. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  541. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  542. /**
  543. * Assuming src/dst/times are already simplified on entrance.
  544. */
  545. size_t get_workspace_in_bytes_fwd(
  546. const TensorShape& src, const TensorShape& dst, const TensorShape& times,
  547. DType dtype);
  548. Param m_param;
  549. };
  550. class TileBase : public TileRepeatBase {
  551. public:
  552. TileBase(Handle* handle) : TileRepeatBase(handle) {}
  553. protected:
  554. void simplify_shape(
  555. const TensorShape& src, const TensorShape& dst, const TensorShape& times,
  556. TensorShape& src2, TensorShape& dst2, TensorShape& times2);
  557. /**
  558. * This is a helper function that would facilitate other backends'
  559. * implementation.
  560. */
  561. size_t get_workspace_in_bytes_fwd(const TensorLayout& src, const TensorLayout& dst);
  562. };
  563. class TileForward : public TileBase {
  564. DEF_OPR_IMPL(TileForward, TileBase, 1, 1);
  565. public:
  566. /**
  567. * \brief Tile src times to get dst.
  568. * \param[in] src input tensor
  569. * \param[out] dst output tensor
  570. * \param[out] workspace temporary workspace
  571. *
  572. * src and dst must be contiguous.
  573. * dst.shape should be {src.shape[0]*param().times[0],
  574. * src.shape[1]*param().times[1], ...}
  575. *
  576. * \see http://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html
  577. *
  578. * Difference between Tile and Repeat:
  579. * Tiling `abc' twice yields `abcabc', whereas repeating `abc' twice
  580. * yields `aabbcc'.
  581. */
  582. virtual void exec(
  583. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  584. _megdnn_workspace workspace) = 0;
  585. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  586. virtual size_t get_workspace_in_bytes(
  587. const TensorLayout& src, const TensorLayout& dst) = 0;
  588. protected:
  589. void check_exec(
  590. const TensorLayout& src, const TensorLayout& dst,
  591. size_t workspace_in_bytes);
  592. };
  593. using Tile = TileForward;
  594. class TileBackward : public TileBase {
  595. DEF_OPR_IMPL(TileBackward, TileBase, 1, 1);
  596. public:
  597. /**
  598. * \param[in] diff the backpropagated gradient wrt. dst
  599. * \param[out] grad the backpropagated gradient wrt. src
  600. * \param[out] workspace temporary workspace
  601. */
  602. virtual void exec(
  603. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  604. _megdnn_workspace workspace) = 0;
  605. virtual size_t get_workspace_in_bytes(
  606. const TensorLayout& diff, const TensorLayout& grad) = 0;
  607. protected:
  608. void check_exec(
  609. const TensorLayout& diff, const TensorLayout& grad,
  610. size_t workspace_in_bytes);
  611. };
  612. class RepeatBase : public TileRepeatBase {
  613. public:
  614. RepeatBase(Handle* handle) : TileRepeatBase(handle) {}
  615. protected:
  616. void simplify_shape(
  617. const TensorShape& src, const TensorShape& dst, const TensorShape& times,
  618. TensorShape& src2, TensorShape& dst2, TensorShape& times2);
  619. /**
  620. * This is a helper function that would facilitate other backends'
  621. * implementation.
  622. */
  623. size_t get_workspace_in_bytes_fwd(const TensorLayout& src, const TensorLayout& dst);
  624. };
  625. class RepeatForward : public RepeatBase {
  626. DEF_OPR_IMPL(RepeatForward, RepeatBase, 1, 1);
  627. public:
  628. /**
  629. * \brief Repeat src times to get dst.
  630. * \param[in] src input tensor
  631. * \param[out] dst output tensor
  632. * \param[out] workspace temporary workspace
  633. *
  634. * src and dst must be contiguous.
  635. * dst.shape should be {src.shape[0]*param().times[0],
  636. * src.shape[1]*param().times[1], ...}
  637. *
  638. * \see http://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html
  639. * \see TileForward
  640. */
  641. virtual void exec(
  642. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  643. _megdnn_workspace workspace) = 0;
  644. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  645. virtual size_t get_workspace_in_bytes(
  646. const TensorLayout& src, const TensorLayout& dst) = 0;
  647. protected:
  648. void check_exec(
  649. const TensorLayout& src, const TensorLayout& dst,
  650. size_t workspace_in_bytes);
  651. };
  652. using Repeat = RepeatForward;
  653. class RepeatBackward : public RepeatBase {
  654. DEF_OPR_IMPL(RepeatBackward, RepeatBase, 1, 1);
  655. public:
  656. /**
  657. * \param[in] diff the backpropagated gradient wrt. dst
  658. * \param[out] grad the backpropagated gradient wrt. src
  659. * \param[out] workspace temporary workspace
  660. */
  661. virtual void exec(
  662. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  663. _megdnn_workspace workspace) = 0;
  664. virtual size_t get_workspace_in_bytes(
  665. const TensorLayout& diff, const TensorLayout& grad) = 0;
  666. protected:
  667. void check_exec(
  668. const TensorLayout& diff, const TensorLayout& grad,
  669. size_t workspace_in_bytes);
  670. };
  671. class ArgsortForward : public OperatorBase {
  672. DEF_OPR_IMPL(ArgsortForward, OperatorBase, 1, 2);
  673. DEF_OPR_PARAM(Argsort);
  674. public:
  675. using Order = Param::Order;
  676. /**
  677. * \param[in] src (m, n)
  678. * \param[out] dst (m, n)
  679. * \param[out] indices (m, n)
  680. *
  681. * src, dst and indices should be contiguous.
  682. * Performing m independent sorting on m arrays of length n.
  683. * Sorting arrays and storing the resulting array in `dst',
  684. * and the corresponding indices in `indices'.
  685. *
  686. * Indices range from 0 to n-1.
  687. *
  688. * Note that indices is a TensorND of type int.
  689. */
  690. virtual void exec(
  691. _megdnn_tensor_in src, _megdnn_tensor_out dst, _megdnn_tensor_out indices,
  692. _megdnn_workspace workspace) = 0;
  693. void deduce_layout(
  694. const TensorLayout& src, TensorLayout& dst, TensorLayout& indices);
  695. virtual size_t get_workspace_in_bytes(
  696. const TensorLayout& src, const TensorLayout& dst,
  697. const TensorLayout& indices) = 0;
  698. protected:
  699. void check_exec(
  700. const TensorLayout& src, const TensorLayout& dst,
  701. const TensorLayout& indices, size_t workspace_in_bytes);
  702. };
  703. using Argsort = ArgsortForward;
  704. /*!
  705. * \brief backward opr for Argsort
  706. *
  707. * Note: the name is kept for backward compatibility. This opr is actually a
  708. * batched value setter. It is used for gradient computing of Argsort and TopK.
  709. */
  710. class ArgsortBackward : public OperatorBase {
  711. DEF_OPR_IMPL(ArgsortBackward, OperatorBase, 2, 1);
  712. DEF_OPR_PARAM(Empty);
  713. public:
  714. /**
  715. * \param[in] diff (m, k) the backpropagated gradient wrt. dst
  716. * \param[in] indices (m, k) the `indices' parameter in
  717. * ArgsortForward::exec
  718. * \param[out] grad (m, n) the backpropagated gradient wrt. src
  719. *
  720. * Constraint: n >= k. Untouched values would be initialized as zero.
  721. */
  722. virtual void exec(
  723. _megdnn_tensor_in diff, _megdnn_tensor_in indices, _megdnn_tensor_out grad,
  724. _megdnn_workspace workspace) = 0;
  725. virtual size_t get_workspace_in_bytes(
  726. const TensorLayout& diff, const TensorLayout& indices,
  727. const TensorLayout& grad) = 0;
  728. protected:
  729. void check_exec(
  730. const TensorLayout& diff, const TensorLayout& indices,
  731. const TensorLayout& grad, size_t workspace_in_bytes);
  732. };
  733. class TopK : public OperatorBase {
  734. DEF_OPR_IMPL(TopK, OperatorBase, 1, 2);
  735. DEF_OPR_PARAM(TopK);
  736. protected:
  737. //! impl exec; inputs have been validated
  738. virtual void do_exec(
  739. int k, _megdnn_tensor_in data, _megdnn_tensor_out values, int32_t* indices,
  740. _megdnn_workspace workspace) = 0;
  741. public:
  742. /*!
  743. * \param[in] k if positive, compute the smallest top-k values; otherwise
  744. * compute the largest top-k values
  745. * \param[in] data (m, n) input data, where top-k is computed on the
  746. * second axis. The second dimension must be contiguous, and the first
  747. * dimension can have arbitrary stride.
  748. * \param[out] values (m, ) or (m, k) output values; its shape depends
  749. * on mode
  750. * \param[out] indices () or (m, ) or (m, k) output values; its shape
  751. * depends on mode
  752. */
  753. void exec(
  754. int k, _megdnn_tensor_in data, _megdnn_tensor_out values,
  755. _megdnn_tensor_out indices, _megdnn_workspace workspace);
  756. virtual size_t get_workspace_in_bytes(
  757. int k, const TensorLayout& data, const TensorLayout& values,
  758. const TensorLayout& indices) = 0;
  759. void deduce_layout(
  760. int k, const TensorLayout& data, TensorLayout& values,
  761. TensorLayout& indices);
  762. };
  763. /*!
  764. * \brief convert dtype of *src* to match dtype of *dst*; *src* may have
  765. * arbitrary layout and *dst* must be contiguous.
  766. */
  767. class TypeCvtForward : public OperatorBase {
  768. DEF_OPR_PARAM(Empty);
  769. DEF_OPR_IMPL(TypeCvtForward, OperatorBase, 1, 1);
  770. public:
  771. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst) = 0;
  772. protected:
  773. void check_exec(const TensorLayout& src, const TensorLayout& dst);
  774. };
  775. using TypeCvt = TypeCvtForward;
  776. class IndexingRemapBase : public OperatorBase {
  777. public:
  778. using Param = param::IndexingRemap;
  779. IndexingRemapBase(Handle* handle) : OperatorBase(handle) {}
  780. Param& param() { return m_param; }
  781. const Param& param() const { return m_param; }
  782. protected:
  783. Param m_param;
  784. void check_layout_fwd(
  785. const TensorLayout& src, const TensorLayout& map, const TensorLayout& dst);
  786. };
  787. class IndexingRemapForward : public IndexingRemapBase {
  788. DEF_OPR_IMPL(IndexingRemapForward, IndexingRemapBase, 2, 1);
  789. public:
  790. /**
  791. * \param[in] src input tensor
  792. * \param[in] map input map
  793. * \param[out] dst output tensor
  794. *
  795. * Suppose:
  796. * the shape of src is \f$(s_0, s_1, ..., s_{m-1}\f$;
  797. * the shape of dst is \f$(d_0, d_1, ..., d_{n-1})\f$;
  798. * then:
  799. * the shape of map must be \f$(d_0, d_1, ..., d_{n-1}, m)\f$.
  800. *
  801. * The last dimension of map indicates the src indices for the
  802. * corresponding dst entry.
  803. *
  804. * src and dst can be non-contiguous in a non-overlapping manner.
  805. */
  806. virtual void exec(
  807. _megdnn_tensor_in src, _megdnn_tensor_in map, _megdnn_tensor_out dst,
  808. _megdnn_workspace workspace) = 0;
  809. void deduce_layout(
  810. const TensorLayout& src, const TensorLayout& map, TensorLayout& dst);
  811. virtual size_t get_workspace_in_bytes(
  812. const TensorLayout& src, const TensorLayout& map,
  813. const TensorLayout& dst) = 0;
  814. protected:
  815. void check_exec(
  816. const TensorLayout& src, const TensorLayout& map, const TensorLayout& dst,
  817. size_t workspace_in_bytes);
  818. };
  819. using IndexingRemap = IndexingRemapForward;
  820. // The using directives preserve backward compatibility.
  821. using TensorRemapForward = IndexingRemap;
  822. using TensorRemap = TensorRemapForward;
  823. class IndexingRemapBackward : public IndexingRemapBase {
  824. DEF_OPR_IMPL(IndexingRemapBackward, IndexingRemapBase, 2, 1);
  825. public:
  826. /**
  827. * \param[in] diff the backpropagated gradient wrt. dst
  828. * \param[in] map the `map' parameter in IndexingRemapForward::exec
  829. * \param[out] grad the backpropagated gradient wrt. src
  830. */
  831. virtual void exec(
  832. _megdnn_tensor_in diff, _megdnn_tensor_in map, _megdnn_tensor_out grad,
  833. _megdnn_workspace workspace) = 0;
  834. virtual size_t get_workspace_in_bytes(
  835. const TensorLayout& diff, const TensorLayout& map,
  836. const TensorLayout& grad) = 0;
  837. protected:
  838. void check_exec(
  839. const TensorLayout& diff, const TensorLayout& map, const TensorLayout& grad,
  840. size_t workspace_in_bytes);
  841. };
  842. // The using directives preserve backward compatibility.
  843. using TensorRemapBackward = IndexingRemapBackward;
  844. class Linspace : public OperatorBase {
  845. DEF_OPR_IMPL(Linspace, OperatorBase, 0, 1);
  846. DEF_OPR_PARAM(LinspaceFull);
  847. public:
  848. /**
  849. * \param[out] dst must be 1d.
  850. *
  851. * \see http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
  852. */
  853. virtual void exec(_megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  854. virtual size_t get_workspace_in_bytes(const TensorLayout& dst) = 0;
  855. protected:
  856. void check_exec(const TensorLayout& dst, size_t workspace_in_bytes);
  857. };
  858. class Eye : public OperatorBase {
  859. DEF_OPR_IMPL(Eye, OperatorBase, 0, 1);
  860. DEF_OPR_PARAM(Eye);
  861. public:
  862. /**
  863. * \see http://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html
  864. */
  865. virtual void exec(_megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  866. virtual size_t get_workspace_in_bytes(const TensorLayout& dst) = 0;
  867. protected:
  868. void check_exec(const TensorLayout& dst, size_t workspace_in_bytes);
  869. };
  870. class Diag : public OperatorBase {
  871. DEF_OPR_IMPL(Diag, OperatorBase, 1, 1);
  872. DEF_OPR_PARAM(Diag);
  873. public:
  874. /**
  875. * \see http://docs.scipy.org/doc/numpy/reference/generated/numpy.diag.html
  876. */
  877. virtual void exec(
  878. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  879. _megdnn_workspace workspace) = 0;
  880. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  881. virtual size_t get_workspace_in_bytes(
  882. const TensorLayout& src, const TensorLayout& dst) = 0;
  883. protected:
  884. void check_exec(
  885. const TensorLayout& src, const TensorLayout& dst,
  886. size_t workspace_in_bytes);
  887. };
  888. class IndexingOneHotBase : public OperatorBase {
  889. DEF_OPR_IMPL_CTOR(IndexingOneHotBase, OperatorBase);
  890. DEF_OPR_PARAM(Axis);
  891. protected:
  892. MGE_WIN_DECLSPEC_FUC void deduce_layout_fwd(
  893. const TensorLayout& src, const TensorLayout& index, TensorLayout& dst);
  894. void check_layout_fwd(
  895. const TensorLayout& src, const TensorLayout& index,
  896. const TensorLayout& dst);
  897. };
  898. /*!
  899. * \brief Indexing for one-hot encoding
  900. *
  901. * Given src, axis and index,
  902. * for all valid (n-1)-dimensional subscript tuples i iterating through index:
  903. * dst[i[0], ..., i[axis-1], 0, i[axis], ..., i[n-2]] =
  904. * inp[i[0], ..., i[axis-1], index[i], i[axis], ..., i[n-2]]
  905. *
  906. * \param[in] src n-dimensional input data
  907. * \param[in] index (n-1)-dimensional index, must be int
  908. * \param[out] dst n-dimensional output data
  909. */
  910. class IndexingOneHotForward : public IndexingOneHotBase {
  911. DEF_OPR_IMPL(IndexingOneHotForward, IndexingOneHotBase, 2, 1);
  912. public:
  913. void deduce_layout(
  914. const TensorLayout& src, const TensorLayout& index, TensorLayout& dst) {
  915. deduce_layout_fwd(src, index, dst);
  916. }
  917. virtual void exec(
  918. _megdnn_tensor_in src, _megdnn_tensor_in index, _megdnn_tensor_out dst,
  919. _megdnn_workspace workspace) = 0;
  920. virtual size_t get_workspace_in_bytes(
  921. const TensorLayout& src, const TensorLayout& index,
  922. const TensorLayout& dst) = 0;
  923. protected:
  924. void check_exec(
  925. const TensorLayout& src, const TensorLayout& index, const TensorLayout& dst,
  926. size_t workspace_in_bytes);
  927. };
  928. using IndexingOneHot = IndexingOneHotForward;
  929. /*!
  930. * \brief set-subtensor corresponding to IndexingOneHotForward
  931. *
  932. * \param[in,out] data n-dimensional input and output data, whose sub part
  933. * corresponding to *index* would be replaced by *sub*
  934. * \param[in] index (n-1)-dimensional index, must be int
  935. * \param[in] sub n-dimensional sub tensor to be filled in *data*
  936. */
  937. class IndexingSetOneHotForward : public IndexingOneHotBase {
  938. DEF_OPR_IMPL(IndexingSetOneHotForward, IndexingOneHotBase, -1, 1);
  939. public:
  940. virtual void exec(
  941. _megdnn_tensor_inout data, _megdnn_tensor_in index, _megdnn_tensor_in sub,
  942. _megdnn_workspace workspace) = 0;
  943. virtual size_t get_workspace_in_bytes(
  944. const TensorLayout& data, const TensorLayout& index,
  945. const TensorLayout& sub) = 0;
  946. protected:
  947. void check_exec(
  948. const TensorLayout& data, const TensorLayout& index,
  949. const TensorLayout& sub, size_t workspace_in_bytes);
  950. };
  951. using IndexingSetOneHot = IndexingSetOneHotForward;
  952. /*!
  953. * \brief base class for indexing on multiple axes using vector indices
  954. *
  955. * Note that the indexing axes are required to be sorted in ascending order
  956. */
  957. class IndexingMultiAxisVecBase : public OperatorBase {
  958. DEF_OPR_IMPL_CTOR(IndexingMultiAxisVecBase, OperatorBase);
  959. DEF_OPR_PARAM(Empty);
  960. public:
  961. struct AxisIndexer {
  962. size_t axis;
  963. TensorND vec;
  964. };
  965. struct AxisIndexerLayoutOnly {
  966. size_t axis;
  967. TensorLayout layout;
  968. };
  969. using IndexDesc = std::vector<AxisIndexer>;
  970. using IndexDescLayoutOnly = std::vector<AxisIndexerLayoutOnly>;
  971. /*!
  972. * \brief convert IndexDesc to IndexDescLayoutOnly
  973. */
  974. static IndexDescLayoutOnly extract_index_layout(const IndexDesc& index);
  975. /*!
  976. * \brief get the axes on src that are not used in index
  977. * \param[out] out output buffer; suggested size is
  978. * TensorLayout::MAX_NDIM
  979. * \return number of elements written to *out*
  980. */
  981. static size_t get_nonindex_axes(
  982. size_t src_ndim, const IndexDesc& index, size_t* out);
  983. /*!
  984. * \brief get contiguous-collapsed layout for indexing on value
  985. * \param idx_axis indexer axis on value (i.e. ExecInfo::idx_axis)
  986. * \return a tensor layout and an axis to iterate over *value* and also
  987. * access *data*; stride of layout on that axis would be zero, and
  988. * strides on other axes correspond to the strides in *data*
  989. */
  990. static std::tuple<TensorLayout, size_t, TensorShape> get_value_iter_optimized_layout(
  991. const TensorLayout& data, const TensorLayout& value, const IndexDesc& index,
  992. size_t idx_axis);
  993. //! helper info for kernel implementation
  994. struct ExecInfo {
  995. //! axis in value used by indexer
  996. size_t idx_axis;
  997. ptrdiff_t value_stride;
  998. void* error_tracker;
  999. megcore::AsyncErrorInfo* error_info;
  1000. };
  1001. protected:
  1002. /*!
  1003. * \return axis on dst used by indexer (i.e. ExecInfo::idx_axis)
  1004. */
  1005. MGE_WIN_DECLSPEC_FUC static size_t deduce_layout_fwd(
  1006. const TensorLayout& data, const IndexDescLayoutOnly& index,
  1007. TensorLayout& dst);
  1008. static ExecInfo check_exec_noworkspace(
  1009. const TensorLayout& data, const TensorLayout& value, const IndexDesc& index,
  1010. IndexDescLayoutOnly& index_layout);
  1011. };
  1012. /*!
  1013. * \brief compute indexing result, like numpy advanced indexing
  1014. *
  1015. * src can have arbitrary layout, but dst must be dim1-contig
  1016. */
  1017. class IndexingMultiAxisVec : public IndexingMultiAxisVecBase {
  1018. DEF_OPR_IMPL(IndexingMultiAxisVec, IndexingMultiAxisVecBase, 0, 1);
  1019. public:
  1020. virtual void exec(
  1021. _megdnn_tensor_in src, const IndexDesc& index, _megdnn_tensor_out dst,
  1022. _megdnn_workspace workspace) = 0;
  1023. /*!
  1024. * \brief get workspace size based on output shape and indexing axes
  1025. */
  1026. size_t get_workspace_in_bytes(
  1027. const TensorShape& dst, const size_t* axes, size_t nr_axes,
  1028. size_t idx_ndim);
  1029. static void deduce_layout(
  1030. const TensorLayout& data, const IndexDescLayoutOnly& index,
  1031. TensorLayout& dst) {
  1032. deduce_layout_fwd(data, index, dst);
  1033. }
  1034. protected:
  1035. virtual size_t get_workspace_in_bytes(size_t dst_idx_size) = 0;
  1036. ExecInfo check_exec(
  1037. const TensorLayout& src, const IndexDesc& index, const TensorLayout& dst,
  1038. size_t workspace_in_bytes);
  1039. };
  1040. /*!
  1041. * \brief base class for modifying data by given index
  1042. *
  1043. * data can have arbitrary layout, but value must be dim1-contig
  1044. */
  1045. class IndexingModifyMultiAxisVecBase : public IndexingMultiAxisVecBase {
  1046. DEF_OPR_IMPL_CTOR(IndexingModifyMultiAxisVecBase, IndexingMultiAxisVecBase);
  1047. public:
  1048. virtual void exec(
  1049. _megdnn_tensor_inout data, _megdnn_tensor_in value, const IndexDesc& index,
  1050. _megdnn_workspace workspace) = 0;
  1051. /*!
  1052. * \brief get workspace size based on shape of value input and indexing
  1053. * axes
  1054. */
  1055. size_t get_workspace_in_bytes(
  1056. const TensorShape& value, const size_t* axes, size_t nr_axes,
  1057. size_t idx_ndim);
  1058. protected:
  1059. ExecInfo check_exec(
  1060. const TensorLayout& data, const TensorLayout& value, const IndexDesc& index,
  1061. size_t workspace_in_bytes);
  1062. virtual size_t get_workspace_in_bytes(size_t value_idx_size) = 0;
  1063. };
  1064. //! set value to indexed locations; index values must be non-overlapping
  1065. class IndexingSetMultiAxisVec : public IndexingModifyMultiAxisVecBase {
  1066. DEF_OPR_IMPL(IndexingSetMultiAxisVec, IndexingModifyMultiAxisVecBase, 0, 0);
  1067. };
  1068. //! add value to indexed locations; index values must be non-overlapping
  1069. class IndexingIncrMultiAxisVec : public IndexingModifyMultiAxisVecBase {
  1070. DEF_OPR_IMPL(IndexingIncrMultiAxisVec, IndexingModifyMultiAxisVecBase, 0, 0);
  1071. };
  1072. class MeshBase : public OperatorBase {
  1073. DEF_OPR_PARAM(Empty);
  1074. DEF_OPR_IMPL_CTOR(MeshBase, OperatorBase);
  1075. public:
  1076. using AxisIndexer = IndexingMultiAxisVecBase::AxisIndexer;
  1077. using IndexDesc = IndexingMultiAxisVecBase::IndexDesc;
  1078. using AxisIndexerLayoutOnly = IndexingMultiAxisVecBase::AxisIndexerLayoutOnly;
  1079. using IndexDescLayoutOnly = IndexingMultiAxisVecBase::IndexDescLayoutOnly;
  1080. size_t get_workspace_in_bytes(const TensorShape&, const size_t*, size_t, size_t) {
  1081. return 0;
  1082. }
  1083. protected:
  1084. virtual void check_exec(
  1085. const TensorLayout& origin, const TensorLayout& indexed,
  1086. const IndexDesc& desc);
  1087. };
  1088. class NormalMeshBase : public MeshBase {
  1089. DEF_OPR_IMPL(NormalMeshBase, MeshBase, 0, 0);
  1090. protected:
  1091. virtual void check_exec(
  1092. const TensorLayout& origin, const TensorLayout& indexed,
  1093. const IndexDesc& desc) override final;
  1094. };
  1095. class NormalMeshModifyBase : public NormalMeshBase {
  1096. DEF_OPR_IMPL_CTOR(NormalMeshModifyBase, NormalMeshBase);
  1097. public:
  1098. virtual void exec(
  1099. _megdnn_tensor_inout data, _megdnn_tensor_in value, const IndexDesc& desc,
  1100. _megdnn_workspace workspace) = 0;
  1101. };
  1102. class BatchedMeshBase : public MeshBase {
  1103. DEF_OPR_IMPL_CTOR(BatchedMeshBase, MeshBase);
  1104. protected:
  1105. virtual void check_exec(
  1106. const TensorLayout& origin, const TensorLayout& indexed,
  1107. const IndexDesc& desc) override final;
  1108. };
  1109. class BatchedMeshModifyBase : public BatchedMeshBase {
  1110. DEF_OPR_IMPL_CTOR(BatchedMeshModifyBase, BatchedMeshBase);
  1111. public:
  1112. virtual void exec(
  1113. _megdnn_tensor_inout data, _megdnn_tensor_in value, const IndexDesc& desc,
  1114. _megdnn_workspace workspace) = 0;
  1115. };
  1116. class MeshIndexing : public NormalMeshBase {
  1117. DEF_OPR_IMPL(MeshIndexing, NormalMeshBase, 0, 0);
  1118. public:
  1119. virtual void exec(
  1120. _megdnn_tensor_in src, const IndexDesc& desc, _megdnn_tensor_out dst,
  1121. _megdnn_workspace workspace) = 0;
  1122. static void deduce_layout(
  1123. const TensorLayout& inp, const IndexDescLayoutOnly& layouts,
  1124. TensorLayout& out_layout);
  1125. };
  1126. class IncrMeshIndexing : public NormalMeshModifyBase {
  1127. DEF_OPR_IMPL(IncrMeshIndexing, NormalMeshModifyBase, 0, 0);
  1128. };
  1129. class SetMeshIndexing : public NormalMeshModifyBase {
  1130. DEF_OPR_IMPL(SetMeshIndexing, NormalMeshModifyBase, 0, 0);
  1131. };
  1132. class BatchedMeshIndexing : public BatchedMeshBase {
  1133. DEF_OPR_IMPL(BatchedMeshIndexing, BatchedMeshBase, 0, 0);
  1134. public:
  1135. virtual void exec(
  1136. _megdnn_tensor_in src, const IndexDesc& desc, _megdnn_tensor_out dst,
  1137. _megdnn_workspace workspace) = 0;
  1138. static void deduce_layout(
  1139. const TensorLayout& inp, const IndexDescLayoutOnly& layouts,
  1140. TensorLayout& out_layout);
  1141. };
  1142. class BatchedIncrMeshIndexing : public BatchedMeshModifyBase {
  1143. DEF_OPR_IMPL(BatchedIncrMeshIndexing, BatchedMeshModifyBase, 0, 0);
  1144. };
  1145. class BatchedSetMeshIndexing : public BatchedMeshModifyBase {
  1146. DEF_OPR_IMPL(BatchedSetMeshIndexing, BatchedMeshModifyBase, 0, 0);
  1147. };
  1148. class RelayoutFormat : public OperatorBase {
  1149. DEF_OPR_PARAM(RelayoutFormat);
  1150. DEF_OPR_IMPL(RelayoutFormat, OperatorBase, 1, 1);
  1151. public:
  1152. virtual void exec(
  1153. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  1154. _megdnn_workspace workspace) = 0;
  1155. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  1156. void deduce_format(TensorFormat src, TensorFormat& dst);
  1157. virtual size_t get_workspace_in_bytes(
  1158. const TensorLayout& src, const TensorLayout& dst) = 0;
  1159. protected:
  1160. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  1161. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  1162. void check_exec(
  1163. const TensorLayout& src, const TensorLayout& dst,
  1164. size_t workspace_in_bytes);
  1165. void deduce_exec_layout(
  1166. const TensorLayout& src, const TensorLayout& dst,
  1167. TensorLayout& exec_workspace, TensorLayout& exec_src,
  1168. TensorLayout& exec_dst);
  1169. };
  1170. /*!
  1171. * \brief check whether input contains inf or nan value.
  1172. */
  1173. class CheckNonFinite : public OperatorBase {
  1174. DEF_OPR_PARAM(CheckNonFinite);
  1175. DEF_OPR_IMPL(CheckNonFinite, OperatorBase, -1, 1);
  1176. size_t m_size = 0;
  1177. public:
  1178. virtual size_t get_workspace_in_bytes(
  1179. const TensorLayoutArray& srcs, const TensorLayout& dst) = 0;
  1180. MGE_WIN_DECLSPEC_FUC void deduce_layout(
  1181. const TensorLayoutArray& srcs, TensorLayout& dst);
  1182. virtual void exec(
  1183. _megdnn_in const TensorNDArray& srcs, _megdnn_tensor_out dst,
  1184. _megdnn_workspace workspace) = 0;
  1185. protected:
  1186. void check_exec(
  1187. const TensorNDArray& srcs, const TensorND& dst, size_t workspace_in_bytes);
  1188. };
  1189. /*!
  1190. * \brief fill the tensor with a scalar value
  1191. */
  1192. class Fill : public OperatorBase {
  1193. DEF_OPR_PARAM(Fill);
  1194. DEF_OPR_IMPL(Fill, OperatorBase, 0, 1);
  1195. public:
  1196. virtual void exec(_megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1197. virtual size_t get_workspace_in_bytes(const TensorLayout& dst) = 0;
  1198. protected:
  1199. void check_exec(const TensorLayout& dst, size_t workspace_in_bytes);
  1200. };
  1201. /*!
  1202. * \brief standard padding operator
  1203. * Inputs must have the same dtype, and the output tensor shape must greater or equal
  1204. * than input tensor in every dimensions, the extra space will be fulled with m which
  1205. * default to be 0.
  1206. */
  1207. class PaddingBase : public OperatorBase {
  1208. DEF_OPR_PARAM(Padding);
  1209. DEF_OPR_IMPL(PaddingBase, OperatorBase, 1, 1);
  1210. public:
  1211. using Mode = Param::PaddingMode;
  1212. protected:
  1213. SmallVector<size_t> get_offsets();
  1214. MGE_WIN_DECLSPEC_FUC static SmallVector<size_t> get_offsets_impl(const Param& p);
  1215. void check_exec(const TensorLayout& src, const TensorLayout& dst);
  1216. };
  1217. class PaddingForward : public PaddingBase {
  1218. DEF_OPR_IMPL(PaddingForward, PaddingBase, 1, 1);
  1219. public:
  1220. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst) = 0;
  1221. void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst, _megdnn_workspace) {
  1222. return exec(src, dst);
  1223. }
  1224. virtual size_t get_workspace_in_bytes(
  1225. const TensorLayout& src, const TensorLayout& dst) = 0;
  1226. MGE_WIN_DECLSPEC_FUC void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  1227. MGE_WIN_DECLSPEC_FUC static void deduce_layout_impl(
  1228. const TensorLayout& src, TensorLayout& dst, const Param& p);
  1229. protected:
  1230. void forward_check_exec(const TensorLayout& src, const TensorLayout& dst);
  1231. };
  1232. using Padding = PaddingForward;
  1233. class PaddingBackward : public PaddingBase {
  1234. DEF_OPR_IMPL(PaddingBackward, PaddingBase, 1, 1);
  1235. public:
  1236. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst) = 0;
  1237. void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst, _megdnn_workspace) {
  1238. return exec(src, dst);
  1239. }
  1240. virtual size_t get_workspace_in_bytes(
  1241. const TensorLayout& src, const TensorLayout& dst) = 0;
  1242. protected:
  1243. void backward_check_exec(const TensorLayout& src, const TensorLayout& dst);
  1244. };
  1245. class LAMBUpdate : public OperatorBase {
  1246. DEF_OPR_PARAM(LAMBUpdate);
  1247. // input=(m_t-1,v_t-1,lamb_param,grad) , output = (m_t,v_t,new_param)
  1248. DEF_OPR_IMPL(LAMBUpdate, OperatorBase, 4, 3);
  1249. public:
  1250. virtual void exec(
  1251. _megdnn_tensor_in m_t_1, _megdnn_tensor_in v_t_1,
  1252. _megdnn_tensor_in lamb_param, _megdnn_tensor_in grad,
  1253. _megdnn_tensor_out m_t, _megdnn_tensor_out v_t,
  1254. _megdnn_tensor_out new_param, _megdnn_workspace workspace) = 0;
  1255. virtual size_t get_workspace_in_bytes(
  1256. const TensorLayout& m_t_1, const TensorLayout& v_t_1,
  1257. const TensorLayout& lamb_param, const TensorLayout& grad,
  1258. const TensorLayout& m_t, const TensorLayout& v_t,
  1259. const TensorLayout& new_param) = 0;
  1260. MGE_WIN_DECLSPEC_FUC void deduce_layout(
  1261. const TensorLayout& m_t_1, const TensorLayout& v_t_1,
  1262. const TensorLayout& lamb_param, const TensorLayout& grad, TensorLayout& m_t,
  1263. TensorLayout& v_t, TensorLayout& new_param);
  1264. protected:
  1265. void check_exec(
  1266. const TensorLayout& m_t_1, const TensorLayout& v_t_1,
  1267. const TensorLayout& lamb_param, const TensorLayout& grad,
  1268. const TensorLayout& m_t, const TensorLayout& v_t,
  1269. const TensorLayout& new_param, size_t workspace_in_bytes);
  1270. };
  1271. using LAMB = LAMBUpdate;
  1272. class NormBase : public OperatorBase {
  1273. DEF_OPR_PARAM(Norm); // package norm params in Norm keyword from py declaration
  1274. DEF_OPR_IMPL(NormBase, OperatorBase, 1, 1); // constructor and static members
  1275. public:
  1276. virtual void deduce_layout(const TensorLayout& src, TensorLayout& dst) = 0;
  1277. virtual size_t get_workspace_in_bytes(
  1278. const TensorLayout& src, const TensorLayout& dst) = 0;
  1279. protected:
  1280. void check_exec(
  1281. const TensorLayout& src, const TensorLayout& dst,
  1282. size_t workspace_in_bytes);
  1283. };
  1284. class NormForward : public NormBase {
  1285. DEF_OPR_IMPL(NormForward, NormBase, 1, 1);
  1286. using Mode = Param::Mode;
  1287. public:
  1288. virtual void exec(
  1289. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  1290. _megdnn_workspace workspace) = 0;
  1291. virtual void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  1292. virtual size_t get_workspace_in_bytes(
  1293. const TensorLayout& src, const TensorLayout& dst) = 0;
  1294. };
  1295. using Norm = NormForward;
  1296. } // namespace megdnn
  1297. #include "megdnn/internal/opr_header_epilogue.h"
  1298. // vim: syntax=cpp.doxygen