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.

nn.h 88 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. #pragma once
  2. #include "megdnn/internal/opr_header_prologue.h"
  3. namespace megdnn {
  4. class SeparableConvBase : public OperatorBase {
  5. DEF_OPR_IMPL_CTOR(SeparableConvBase, OperatorBase);
  6. DEF_OPR_PARAM(SeparableConv);
  7. public:
  8. using Mode = Param::Mode;
  9. protected:
  10. void deduce_layout_fwd(
  11. const TensorLayout& src, const TensorLayout& filter_x,
  12. const TensorLayout& filter_y, TensorLayout& dst);
  13. void check_layout_fwd(
  14. const TensorLayout& src, const TensorLayout& filter_x,
  15. const TensorLayout& filter_y, const TensorLayout& dst);
  16. };
  17. class SeparableConvForward : public SeparableConvBase {
  18. DEF_OPR_IMPL(SeparableConvForward, SeparableConvBase, 3, 1);
  19. public:
  20. virtual void exec(
  21. _megdnn_tensor_in src, _megdnn_tensor_in filter_x,
  22. _megdnn_tensor_in filter_y, _megdnn_tensor_out dst,
  23. _megdnn_workspace workspace) = 0;
  24. void deduce_layout(
  25. const TensorLayout& src, const TensorLayout& filter_x,
  26. const TensorLayout& filter_y, TensorLayout& dst);
  27. virtual size_t get_workspace_in_bytes(
  28. const TensorLayout& src, const TensorLayout& filter_x,
  29. const TensorLayout& filter_y, const TensorLayout& dst) = 0;
  30. protected:
  31. void check_exec(
  32. const TensorLayout& src, const TensorLayout& filter_x,
  33. const TensorLayout& filter_y, const TensorLayout& dst,
  34. size_t workspace_in_bytes);
  35. };
  36. using SeparableConv = SeparableConvForward;
  37. namespace detail {
  38. struct PreprocessedFilter {
  39. //! user data; its lifetime should be bound to MegDNN Convolution
  40. //! operator
  41. void* algorithm_id;
  42. TensorNDArray tensors;
  43. };
  44. } // namespace detail
  45. /**
  46. * \brief base class for convolution operation
  47. *
  48. * This operator is supposed to perform convolution on arbitrary input
  49. * dimensions. The input/output format is N, C, dims..., and kernel format can
  50. * take two forms:
  51. * 1. OC, IC, dims..., for conventional dense convolution
  52. * 2. GROUP, OC_PER_GRP, IC_PER_GRP, dims... for sparse group convolution
  53. *
  54. * Currently, only 2D images are supported.
  55. */
  56. template <typename Parameter>
  57. class ConvolutionBase : public OperatorBase {
  58. DEF_OPR_IMPL_CTOR(ConvolutionBase, OperatorBase);
  59. using Param = Parameter;
  60. public:
  61. Param& param() { return m_param; }
  62. const Param& param() const { return m_param; }
  63. protected:
  64. Param m_param;
  65. public:
  66. static constexpr size_t MAX_SPATIAL_DIM = 2;
  67. using Mode = typename Param::Mode;
  68. struct CanonizedFilterMeta {
  69. DType dtype;
  70. typename Param::Format format;
  71. uint32_t
  72. //! whether filter should be flipped (i.e. is CONVOLUTION)
  73. should_flip,
  74. group, //!< number of groups
  75. icpg, //!< input channels per group
  76. ocpg, //!< output channels per group
  77. spatial_ndim, stride[MAX_SPATIAL_DIM], padding[MAX_SPATIAL_DIM],
  78. //! spatial dim
  79. spatial[MAX_SPATIAL_DIM], dilation[MAX_SPATIAL_DIM],
  80. //! spatial dim with dilation applied
  81. dilated_spatial[MAX_SPATIAL_DIM];
  82. //! T should be a ConvolutionBase<Z>::CanonizedFilterMeta
  83. template <typename T>
  84. void copy_from(const T& b) {
  85. dtype = b.dtype;
  86. format = b.format;
  87. should_flip = b.should_flip;
  88. group = b.group;
  89. icpg = b.icpg;
  90. ocpg = b.ocpg;
  91. spatial_ndim = b.spatial_ndim;
  92. memcpy(stride, b.stride, sizeof(stride));
  93. memcpy(padding, b.padding, sizeof(padding));
  94. memcpy(spatial, b.spatial, sizeof(spatial));
  95. memcpy(dilation, b.dilation, sizeof(dilation));
  96. memcpy(dilated_spatial, b.dilated_spatial, sizeof(dilated_spatial));
  97. }
  98. bool operator==(const CanonizedFilterMeta& b) const {
  99. bool flag = true;
  100. flag = flag && (format == b.format);
  101. flag = flag && (dtype == b.dtype);
  102. flag = flag && (should_flip == b.should_flip);
  103. flag = flag && (group == b.group);
  104. flag = flag && (icpg == b.icpg);
  105. flag = flag && (ocpg == b.ocpg);
  106. flag = flag && (spatial_ndim == b.spatial_ndim);
  107. if (flag) {
  108. for (uint32_t i = 0; i < spatial_ndim; ++i) {
  109. flag = flag && (stride[i] == b.stride[i]);
  110. flag = flag && (padding[i] == b.padding[i]);
  111. flag = flag && (spatial[i] == b.spatial[i]);
  112. flag = flag && (dilation[i] == b.dilation[i]);
  113. flag = flag && (dilated_spatial[i] == b.dilated_spatial[i]);
  114. }
  115. }
  116. return flag;
  117. }
  118. };
  119. using PreprocessedFilter = detail::PreprocessedFilter;
  120. protected:
  121. // Check or deduce output DType
  122. void check_or_deduce_dtype_fwd(DType src, DType filter, DType& dst) const;
  123. CanonizedFilterMeta deduce_layout_fwd(
  124. const TensorLayout& src, const TensorLayout& filter,
  125. TensorLayout& dst) const;
  126. CanonizedFilterMeta check_layout_fwd(
  127. const TensorLayout& src, const TensorLayout& filter,
  128. const TensorLayout& dst) const;
  129. CanonizedFilterMeta make_canonized_filter_meta(
  130. size_t src_ndim, const TensorLayout& filter) const;
  131. };
  132. class MaskPropagate : public OperatorBase {
  133. DEF_OPR_IMPL(MaskPropagate, OperatorBase, 1, 1);
  134. DEF_OPR_PARAM(MaskPropagate);
  135. public:
  136. virtual void exec(
  137. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  138. _megdnn_workspace workspace) = 0;
  139. virtual size_t get_workspace_in_bytes(
  140. const TensorLayout& src, const TensorLayout& dst) = 0;
  141. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  142. };
  143. /**
  144. * \brief ConvolutionForward Operator with 0/1 Mask matrix
  145. */
  146. class MaskConvForward : public ConvolutionBase<param::Convolution> {
  147. DEF_OPR_IMPL(MaskConvForward, ConvolutionBase, 3, 1);
  148. public:
  149. virtual void exec(
  150. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_in mask,
  151. _megdnn_tensor_out dst, _megdnn_workspace worksapce) = 0;
  152. virtual size_t get_workspace_in_bytes(
  153. const TensorLayout& src, const TensorLayout& filter,
  154. const TensorLayout& mask, const TensorLayout& dst) = 0;
  155. void deduce_dtype(DType src, DType filter, DType mask, DType& dst);
  156. void deduce_layout(
  157. const TensorLayout& src, const TensorLayout& filter,
  158. const TensorLayout& mask, TensorLayout& dst);
  159. protected:
  160. CanonizedFilterMeta check_exec(
  161. const TensorLayout& src, const TensorLayout& filter,
  162. const TensorLayout& mask, const TensorLayout& dst,
  163. size_t workspace_in_bytes);
  164. };
  165. using MaskConvolution = MaskConvForward;
  166. /**
  167. * \brief ConvolutionForward operator.
  168. */
  169. class ConvolutionForward : public ConvolutionBase<param::Convolution>,
  170. public detail::MultiAlgoOpr<ConvolutionForward, 3> {
  171. DEF_OPR_IMPL(ConvolutionForward, ConvolutionBase, 2, 1);
  172. public:
  173. /**
  174. * \param[in] src (n, ic, ih, iw)
  175. * \param[in] filter (oc, ic, fh, fw)
  176. * \param[in] preprocessed_filter if weight no preprocessed it will be
  177. * nullptr, else the preprocessed weights store in the tensors of
  178. * preprocessed_filter.
  179. * \param[in] workspace if weight no preprocessed
  180. * (preprocessed_filter == nullptr), The size of the workspace satisfies the
  181. * situation that weights is not processed, other wise the size of workspace
  182. * satisfies the situation that weights is preprocessed
  183. * \param[out] dst (n, oc, oh, ow)
  184. */
  185. virtual void exec(
  186. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
  187. const PreprocessedFilter* preprocessed_filter,
  188. _megdnn_workspace workspace) = 0;
  189. /**
  190. * \brief execute weight preprocessing, read weights form filter and write
  191. * to preprocessed_filter after preprocessed.
  192. *
  193. * \praram[in] workspace the needed tmp workspace when exec_preprocess
  194. */
  195. virtual void exec_preprocess(
  196. const TensorLayout& src_layout, _megdnn_tensor_in filter,
  197. const TensorLayout& dst_layout, PreprocessedFilter* preprocessed_filter,
  198. _megdnn_workspace workspace) = 0;
  199. void deduce_dtype(DType src, DType filter, DType& dst);
  200. void deduce_layout(
  201. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  202. /**
  203. * \brief query the workspace needed when executing the opr, if the weights
  204. * are preprocessed the preprocessed_filter will not be nullptr, else it
  205. * will be nullptr, the workspace size maybe different whether weights are
  206. * preprocessed
  207. *
  208. * \return the size of workspace needed when executing
  209. */
  210. virtual size_t get_workspace_in_bytes(
  211. const TensorLayout& src, const TensorLayout& filter,
  212. const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter) = 0;
  213. /**
  214. * \brief deduce the preprocessed filter layouts according to the src,
  215. * filter and dst layout, the result may contain multi layouts when the
  216. * weights is not one
  217. *
  218. * \return SmallVector<TensorLayout> Derive the layouts of weight
  219. * preprocessing, return empty if preprocessing is not needed.
  220. */
  221. virtual SmallVector<TensorLayout> deduce_preprocessed_filter_layout(
  222. const TensorLayout& src, const TensorLayout& filter,
  223. const TensorLayout& dst) = 0;
  224. /**
  225. * \brief query the workspace needed when preprocessing the weights,
  226. * according to the return size, a _megdnn_workspace will be created and
  227. * passed through exec_preprocess
  228. *
  229. * \return the size of workspace needed when preprocessing
  230. */
  231. virtual size_t get_preprocess_workspace_in_bytes(
  232. const TensorLayout& src, const TensorLayout& filter,
  233. const TensorLayout& dst) = 0;
  234. static Algorithm::OprType get_opr_type() {
  235. return Algorithm::OprType::CONVOLUTION_FORWARD;
  236. }
  237. protected:
  238. CanonizedFilterMeta check_exec(
  239. const TensorLayout& src, const TensorLayout& filter,
  240. const TensorLayout& dst, size_t workspace_in_bytes,
  241. const PreprocessedFilter* preprocessed_filter);
  242. };
  243. using Convolution = ConvolutionForward;
  244. /**
  245. * \brief ConvolutionBackwardData operator.
  246. *
  247. * Calculating the gradient wrt. convolution input data.
  248. */
  249. class ConvolutionBackwardData
  250. : public ConvolutionBase<param::Convolution>,
  251. public detail::MultiAlgoOpr<ConvolutionBackwardData, 3> {
  252. DEF_OPR_IMPL(ConvolutionBackwardData, ConvolutionBase, 2, 1);
  253. public:
  254. /**
  255. * \param[in] filter (oc, ic, fh, fw)
  256. * \param[in] diff (n, oc, oh, ow)
  257. * \param[out] grad (n, ic, ih, iw)
  258. */
  259. virtual void exec(
  260. _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  261. _megdnn_workspace workspace) = 0;
  262. virtual size_t get_workspace_in_bytes(
  263. const TensorLayout& filter, const TensorLayout& diff,
  264. const TensorLayout& grad) = 0;
  265. MGE_WIN_DECLSPEC_FUC void deduce_dtype(DType filter, DType diff, DType& grad);
  266. void deduce_layout(
  267. const TensorLayout& filter, const TensorLayout& diff, TensorLayout& grad);
  268. static Algorithm::OprType get_opr_type() {
  269. return Algorithm::OprType::CONVOLUTION_BACKWARD_DATA;
  270. }
  271. protected:
  272. CanonizedFilterMeta check_exec(
  273. const TensorLayout& filter, const TensorLayout& diff,
  274. const TensorLayout& grad, size_t workspace_in_bytes);
  275. };
  276. /**
  277. * \brief ConvolutionBackwardFilter operator.
  278. *
  279. * Calculating the gradient wrt. convolution filter.
  280. */
  281. class ConvolutionBackwardFilter
  282. : public ConvolutionBase<param::Convolution>,
  283. public detail::MultiAlgoOpr<ConvolutionBackwardFilter, 3> {
  284. DEF_OPR_IMPL(ConvolutionBackwardFilter, ConvolutionBase, 2, 1);
  285. public:
  286. /**
  287. * \param[in] src (n, ic, ih, iw)
  288. * \param[in] diff (n, oc, oh, ow)
  289. * \param[out] grad (oc, ic, fh, fw)
  290. */
  291. virtual void exec(
  292. _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  293. _megdnn_workspace workspace) = 0;
  294. virtual size_t get_workspace_in_bytes(
  295. const TensorLayout& src, const TensorLayout& diff,
  296. const TensorLayout& grad) = 0;
  297. static Algorithm::OprType get_opr_type() {
  298. return Algorithm::OprType::CONVOLUTION_BACKWARD_FILTER;
  299. }
  300. protected:
  301. CanonizedFilterMeta check_exec(
  302. const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad,
  303. size_t workspace_in_bytes);
  304. };
  305. /**
  306. * \brief ConvolutionBias operator
  307. */
  308. class ConvBiasForward : public ConvolutionBase<param::ConvBias>,
  309. public detail::MultiAlgoOpr<ConvBiasForward, 5> {
  310. DEF_OPR_IMPL(ConvBiasForward, ConvolutionBase, 4, 1);
  311. public:
  312. /**
  313. * \param[in] src (n, ic, ih, iw) or (n, ih, iw, ic)
  314. * \param[in] filter (oc, ic, fh, fw) or (oc, fh, fw, ic) or (oc/4, fh, fw,
  315. * 4 * ic)
  316. * \param[in] bias (1, oc, 1, 1)
  317. * \param[in] z same as dst
  318. * \param[in] preprocessed_filter if weight no preprocessed it will be
  319. * nullptr, else the preprocessed weights store in the tensors of
  320. * preprocessed_filter.
  321. * \param[in] workspace if weight no preprocessed
  322. * (preprocessed_filter == nullptr), The size of the workspace satisfies the
  323. * situation that weights is not processed, other wise the size of workspace
  324. * satisfies the situation that weights is preprocessed
  325. * \param[out] dst (n, oc, oh, ow) or (n, oh, ow, oc)
  326. *
  327. * \note if the format is NCHW_WINOGRAD, the filter layout is (alphah,
  328. * alphaw, oc, ic)
  329. */
  330. virtual void exec(
  331. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_in bias,
  332. _megdnn_tensor_in z, _megdnn_tensor_out dst,
  333. const PreprocessedFilter* preprocessed_filter,
  334. _megdnn_workspace workspace) = 0;
  335. /**
  336. * \brief execute weight preprocessing, read weights form filter and bias,
  337. * write to preprocessed_filter after preprocessed.
  338. *
  339. * \praram[in] workspace the needed tmp workspace when exec_preprocess
  340. * running, the size is got by get_preprocess_workspace_in_bytes
  341. */
  342. virtual void exec_preprocess(
  343. const TensorLayout& src_layout, _megdnn_tensor_in filter,
  344. _megdnn_tensor_in bias, const TensorLayout& z_layout,
  345. const TensorLayout& dst_layout, PreprocessedFilter* preprocessed_filter,
  346. _megdnn_workspace workspace) = 0;
  347. void deduce_dtype(DType src, DType filter, DType bias, DType z, DType& dst);
  348. void deduce_layout(
  349. const TensorLayout& src, const TensorLayout& filter,
  350. const TensorLayout& bias, const TensorLayout& z, TensorLayout& dst);
  351. /**
  352. * \brief query the workspace needed when executing the opr, if the weights
  353. * are preprocessed the preprocessed_filter will not be nullptr, else it
  354. * will be nullptr, the workspace size maybe different whether weights are
  355. * preprocessed
  356. *
  357. * \return the size of workspace needed when executing
  358. */
  359. virtual size_t get_workspace_in_bytes(
  360. const TensorLayout& src, const TensorLayout& filter,
  361. const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst,
  362. const PreprocessedFilter* preprocessed_filter) = 0;
  363. /**
  364. * \brief query the workspace needed when pre-processing the weights,
  365. * according to the return size, a _megdnn_workspace will be created and
  366. * passed through exec_preprocess
  367. *
  368. * \return the size of workspace needed when pre-processing
  369. */
  370. virtual size_t get_preprocess_workspace_in_bytes(
  371. const TensorLayout& src, const TensorLayout& filter,
  372. const TensorLayout& bias, const TensorLayout& z,
  373. const TensorLayout& dst) = 0;
  374. /**
  375. * \brief deduce the pre-processed filter layouts according to the src,
  376. * filter and dst layout, which may contain multi layouts when the weights
  377. * is not one
  378. *
  379. * \return SmallVector<TensorLayout> Derive the layouts of weight
  380. * preprocessing, return empty if preprocessing is not needed.
  381. */
  382. virtual SmallVector<TensorLayout> deduce_preprocessed_filter_layout(
  383. const TensorLayout& src, const TensorLayout& filter,
  384. const TensorLayout& bias, const TensorLayout& z,
  385. const TensorLayout& dst) = 0;
  386. enum class BiasMode : uint32_t {
  387. NO_BIAS = 0, //!< no bias
  388. BROADCAST_CHANNEL_BIAS, //!< broadcast channel bias, [1, c, 1, 1]
  389. BIAS //!< [N, C, H, W]
  390. };
  391. //! param for winograd algos.
  392. struct WinogradParam {
  393. uint32_t channel_block_size;
  394. uint32_t output_block_size;
  395. uint32_t tile_size;
  396. bool operator==(const WinogradParam& rhs) const {
  397. return channel_block_size == rhs.channel_block_size &&
  398. output_block_size == rhs.output_block_size &&
  399. tile_size == rhs.tile_size;
  400. }
  401. std::string to_string() const;
  402. };
  403. static constexpr WinogradParam INVALID_WINOGRAD_PARAM = {0, 0, 0};
  404. struct DirectParam {
  405. std::string to_string() const { return ""; }
  406. };
  407. struct MatmulParam {
  408. std::string to_string() const { return ""; }
  409. };
  410. struct DefaultParam {
  411. std::string to_string() const { return ""; }
  412. };
  413. //! get algo name, the format is ParamTrait<T>::category:base:p.to_string()
  414. //! \warning: base must not contain :.
  415. template <typename T>
  416. static std::string algo_name(
  417. const std::string& base, const T& p,
  418. param::ConvBias::Format format = param::ConvBias::Format::NCHW);
  419. /*!
  420. * \brief parse algo_name and get WinogradParam from algo name.
  421. *
  422. * \param algo name string
  423. * \return WinogradParam parsed from algo name, use pattern
  424. * winograd:base:m:tile_size.
  425. *
  426. * \warning: INVALID_WINOGRAD_PARAM returns if the algo_name is not matched.
  427. */
  428. static WinogradParam parse_winograd_name(const std::string& algo_name);
  429. /**
  430. * @brief find if there is nchw_nchwxx conv kernel optimized for argment,
  431. * nchw44 used for arm, nchw88 used for x86
  432. *
  433. * @param src_dtype conv feature map data type
  434. * @param filter_dtype conv filter or weight data type
  435. * @param dst_dtype output data type
  436. * @param fm filter meta param
  437. * @param bias_mode bias mode, no_bias or broadcast or bias
  438. * @param nonline_mode identity or relu or h_swish or sigmoid
  439. * @return true, found a kernel
  440. * @return false, can`t found any kernel
  441. */
  442. static bool is_nchw_nchwxx_optimized(
  443. const DTypeEnum src_dtype, const DTypeEnum filter_dtype,
  444. const DTypeEnum dst_dtype,
  445. const ConvolutionBase<param::Convolution>::CanonizedFilterMeta& fm,
  446. const ConvBiasForward::BiasMode bias_mode,
  447. const param::ConvBias::NonlineMode nonline_mode);
  448. static Algorithm::OprType get_opr_type() {
  449. return Algorithm::OprType::CONVBIAS_FORWARD;
  450. }
  451. protected:
  452. CanonizedFilterMeta check_exec(
  453. const TensorLayout& src, const TensorLayout& filter,
  454. const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst,
  455. size_t workspace_in_bytes, const PreprocessedFilter* preprocessed_filter);
  456. CanonizedFilterMeta check_exec_allow_noncontiguous(
  457. const TensorLayout& src, const TensorLayout& filter,
  458. const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst,
  459. size_t workspace_in_bytes, const PreprocessedFilter* preprocessed_filter);
  460. };
  461. using ConvBias = ConvBiasForward;
  462. /**
  463. * \brief base class for Conv - Nonline - Pooling
  464. */
  465. class ConvPoolingBase : public OperatorBase {
  466. DEF_OPR_IMPL_CTOR(ConvPoolingBase, OperatorBase);
  467. /**
  468. * \ Param::Method: Two methods to fetch the input data.
  469. * Default methods is WITH_TEXTURE_OBJ.
  470. * If you want to use WITH_SHARED_MEM mode,
  471. * please make sure that the size of
  472. * [ all of the fliter kernels + a channel
  473. * of input data + a channel of output data]
  474. * should be no large than 38KB.
  475. * And the pooling mode should not be "MAX".
  476. */
  477. DEF_OPR_PARAM(ConvPooling);
  478. protected:
  479. virtual void deduce_layout(
  480. const TensorLayout& src, const TensorLayout& filter,
  481. const TensorLayout& bias, TensorLayout& dst) = 0;
  482. virtual void check_layout(
  483. const TensorLayout& src, const TensorLayout& filter,
  484. const TensorLayout& bias, TensorLayout& dst,
  485. size_t workspace_limit_in_bytes) = 0;
  486. };
  487. class ConvPoolingForward : public ConvPoolingBase {
  488. DEF_OPR_IMPL(ConvPoolingForward, ConvPoolingBase, 2, 1);
  489. public:
  490. /**
  491. * \param[in] src input tensor
  492. * \param[out] dst output tensor
  493. */
  494. virtual void exec(
  495. const _megdnn_in TensorND src, const _megdnn_in TensorND filter,
  496. const _megdnn_in TensorND bias, _megdnn_out TensorND dst,
  497. _megdnn_out Workspace workspace) = 0;
  498. virtual void deduce_layout(
  499. const TensorLayout& src, const TensorLayout& filter,
  500. const TensorLayout& bias, TensorLayout& dst) = 0;
  501. virtual size_t get_workspace_in_bytes(
  502. const TensorLayout& src, const TensorLayout& filter,
  503. const TensorLayout& bias, const TensorLayout& dst) = 0;
  504. protected:
  505. virtual void check_layout(
  506. const TensorLayout& src, const TensorLayout& filter,
  507. const TensorLayout& bias, TensorLayout& dst,
  508. size_t workspace_limit_in_bytes) = 0;
  509. };
  510. using ConvPooling = ConvPoolingForward;
  511. class GroupLocalBase : public OperatorBase {
  512. DEF_OPR_IMPL_CTOR(GroupLocalBase, OperatorBase);
  513. DEF_OPR_PARAM(Convolution);
  514. public:
  515. using Mode = Param::Mode;
  516. protected:
  517. void deduce_layout_fwd(
  518. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  519. void check_layout_fwd(
  520. const TensorLayout& src, const TensorLayout& filter,
  521. const TensorLayout& dst);
  522. };
  523. class GroupLocalForward : public GroupLocalBase {
  524. DEF_OPR_IMPL(GroupLocalForward, GroupLocalBase, 2, 1);
  525. public:
  526. /**
  527. * \param[in] src (N, IC, IH, IW)
  528. * \param[in] filter (G, OH, OW, IC/G, FH, FW, OC/G)
  529. * \param[out] dst (N, OC, OH, OW)
  530. **/
  531. virtual void exec(
  532. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
  533. _megdnn_workspace workspace) = 0;
  534. void deduce_layout(
  535. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst) {
  536. deduce_layout_fwd(src, filter, dst);
  537. }
  538. virtual size_t get_workspace_in_bytes(
  539. const TensorLayout& src, const TensorLayout& filter,
  540. const TensorLayout& dst) = 0;
  541. protected:
  542. void check_exec(
  543. const TensorLayout& src, const TensorLayout& filter,
  544. const TensorLayout& dst, size_t workspace_in_bytes);
  545. };
  546. using GroupLocal = GroupLocalForward;
  547. class GroupLocalBackwardData : public GroupLocalBase {
  548. DEF_OPR_IMPL(GroupLocalBackwardData, GroupLocalBase, 2, 1);
  549. public:
  550. virtual void exec(
  551. _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  552. _megdnn_workspace workspace) = 0;
  553. virtual size_t get_workspace_in_bytes(
  554. const TensorLayout& filter, const TensorLayout& diff,
  555. const TensorLayout& grad) = 0;
  556. protected:
  557. void check_exec(
  558. const TensorLayout& filter, const TensorLayout& diff,
  559. const TensorLayout& grad, size_t workspace_in_bytes);
  560. };
  561. class GroupLocalBackwardFilter : public GroupLocalBase {
  562. DEF_OPR_IMPL(GroupLocalBackwardFilter, GroupLocalBase, 2, 1);
  563. public:
  564. virtual void exec(
  565. _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  566. _megdnn_workspace workspace) = 0;
  567. virtual size_t get_workspace_in_bytes(
  568. const TensorLayout& src, const TensorLayout& diff,
  569. const TensorLayout& grad) = 0;
  570. protected:
  571. void check_exec(
  572. const TensorLayout& filter, const TensorLayout& diff,
  573. const TensorLayout& grad, size_t workspace_in_bytes);
  574. };
  575. class Images2NeibsBase : public OperatorBase {
  576. DEF_OPR_IMPL_CTOR(Images2NeibsBase, OperatorBase);
  577. DEF_OPR_PARAM(Images2Neibs);
  578. protected:
  579. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  580. void check_layout_fwd(const TensorLayout& filter, const TensorLayout& dst);
  581. };
  582. class Images2NeibsForward : public Images2NeibsBase {
  583. DEF_OPR_IMPL(Images2NeibsForward, Images2NeibsBase, 1, 1);
  584. public:
  585. /**
  586. * \param[in] src (N, C, IH, IW)
  587. * \param[out] dst (N, C, OH, OW, window_h, window_w)
  588. *
  589. * \see
  590. * http://deeplearning.net/software/theano/library/tensor/nnet/neighbours.html
  591. *
  592. * \f$ dst_{n, c, oh, ow, wh, ww} = src_{n, c, ih+wh, iw+fw}\f$,
  593. * where \f$ ih=-pad_h+oh*stride_h+(wh-1)*(dilation_h-1),
  594. * iw=-pad_w+ow*stride_w+(ww-1)*(dilation_w-1)\f$.
  595. */
  596. virtual void exec(
  597. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  598. _megdnn_workspace workspace) = 0;
  599. virtual size_t get_workspace_in_bytes(
  600. const TensorLayout& src, const TensorLayout& dst) = 0;
  601. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  602. protected:
  603. void check_exec(
  604. const TensorLayout& src, const TensorLayout& dst,
  605. size_t workspace_in_bytes);
  606. };
  607. using Images2Neibs = Images2NeibsForward;
  608. class Images2NeibsBackward : public Images2NeibsBase {
  609. DEF_OPR_IMPL(Images2NeibsBackward, Images2NeibsBase, 1, 1);
  610. public:
  611. /**
  612. * \param[in] diff the backpropagated gradient wrt. dst
  613. * \param[out] grad the backpropagated gradient wrt. src
  614. */
  615. virtual void exec(
  616. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  617. _megdnn_workspace workspace) = 0;
  618. virtual size_t get_workspace_in_bytes(
  619. const TensorLayout& diff, const TensorLayout& grad) = 0;
  620. protected:
  621. void check_exec(
  622. const TensorLayout& diff, const TensorLayout& grad,
  623. size_t workspace_in_bytes);
  624. };
  625. class SlidingWindowTransposeBase : public OperatorBase {
  626. DEF_OPR_IMPL_CTOR(SlidingWindowTransposeBase, OperatorBase);
  627. DEF_OPR_PARAM(SlidingWindowTranspose);
  628. protected:
  629. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  630. void check_layout_fwd(const TensorLayout& filter, const TensorLayout& dst);
  631. };
  632. class SlidingWindowTransposeForward : public SlidingWindowTransposeBase {
  633. DEF_OPR_IMPL(SlidingWindowTransposeForward, SlidingWindowTransposeBase, 1, 1);
  634. public:
  635. /**
  636. * \param[in] src (N, C, IH, IW, window_h, window_w)
  637. * \param[out] dst (N, C, OH, OW)
  638. */
  639. virtual void exec(
  640. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  641. _megdnn_workspace workspace) = 0;
  642. virtual size_t get_workspace_in_bytes(
  643. const TensorLayout& src, const TensorLayout& dst) = 0;
  644. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  645. protected:
  646. void check_exec(
  647. const TensorLayout& src, const TensorLayout& dst,
  648. size_t workspace_in_bytes);
  649. };
  650. using SlidingWindowTranspose = SlidingWindowTransposeForward;
  651. class SlidingWindowTransposeBackward : public SlidingWindowTransposeBase {
  652. DEF_OPR_IMPL(SlidingWindowTransposeBackward, SlidingWindowTransposeBase, 1, 1);
  653. public:
  654. /**
  655. * \param[in] diff the backpropagated gradient wrt. dst
  656. * \param[out] grad the backpropagated gradient wrt. src
  657. */
  658. virtual void exec(
  659. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  660. _megdnn_workspace workspace) = 0;
  661. virtual size_t get_workspace_in_bytes(
  662. const TensorLayout& diff, const TensorLayout& grad) = 0;
  663. protected:
  664. void check_exec(
  665. const TensorLayout& diff, const TensorLayout& grad,
  666. size_t workspace_in_bytes);
  667. };
  668. /**
  669. * \brief base class for Pooling
  670. */
  671. class PoolingBase : public OperatorBase {
  672. DEF_OPR_IMPL_CTOR(PoolingBase, OperatorBase);
  673. DEF_OPR_PARAM(Pooling);
  674. public:
  675. using Mode = Param::Mode;
  676. protected:
  677. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  678. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  679. public:
  680. MGE_WIN_DECLSPEC_FUC static void deduce_layout_impl(
  681. const TensorLayout& src, const Param& param, TensorLayout& dst);
  682. };
  683. class PoolingForward : public PoolingBase,
  684. public detail::MultiAlgoOpr<PoolingForward, 2> {
  685. DEF_OPR_IMPL(PoolingForward, PoolingBase, 1, 1);
  686. public:
  687. /**
  688. * \param[in] src input tensor
  689. * \param[out] dst output tensor
  690. */
  691. virtual void exec(
  692. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  693. _megdnn_workspace workspace) = 0;
  694. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  695. virtual size_t get_workspace_in_bytes(
  696. const TensorLayout& src, const TensorLayout& dst) = 0;
  697. static Algorithm::OprType get_opr_type() {
  698. return Algorithm::OprType::POOLING_FORWARD;
  699. }
  700. protected:
  701. void check_exec(
  702. const TensorLayout& src, const TensorLayout& dst,
  703. size_t workspace_in_bytes);
  704. };
  705. using Pooling = PoolingForward;
  706. class PoolingBackward : public PoolingBase,
  707. public detail::MultiAlgoOpr<PoolingBackward, 4> {
  708. DEF_OPR_IMPL(PoolingBackward, PoolingBase, 3, 1);
  709. public:
  710. /**
  711. * \param[in] src the `src' parameter in PoolingForward::exec
  712. * \param[in] dst the `dst' parameter in PoolingForward::exec
  713. * \param[in] diff the backpropagated gradient wrt. dst
  714. * \param[out] grad the backpropagated gradient wrt. src
  715. */
  716. virtual void exec(
  717. _megdnn_tensor_in src, _megdnn_tensor_in dst, _megdnn_tensor_in diff,
  718. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  719. virtual size_t get_workspace_in_bytes(
  720. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  721. const TensorLayout& grad) = 0;
  722. static Algorithm::OprType get_opr_type() {
  723. return Algorithm::OprType::POOLING_BACKWARD;
  724. }
  725. protected:
  726. void check_exec(
  727. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  728. const TensorLayout& grad, size_t workspace_in_bytes);
  729. };
  730. /**
  731. * \brief base class for AdaptivePooling
  732. */
  733. class AdaptivePoolingBase : public OperatorBase {
  734. DEF_OPR_IMPL_CTOR(AdaptivePoolingBase, OperatorBase);
  735. DEF_OPR_PARAM(AdaptivePooling);
  736. protected:
  737. param::Pooling deduce_pooling_param(
  738. const TensorLayout& src, const TensorLayout& dst);
  739. };
  740. class AdaptivePoolingForward : public AdaptivePoolingBase {
  741. DEF_OPR_IMPL(AdaptivePoolingForward, AdaptivePoolingBase, 1, 1);
  742. public:
  743. /**
  744. * \param[in] src input tensor
  745. * \param[out] dst output tensor
  746. */
  747. virtual void exec(
  748. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  749. _megdnn_workspace workspace) = 0;
  750. virtual size_t get_workspace_in_bytes(
  751. const TensorLayout& src, const TensorLayout& dst) = 0;
  752. };
  753. using AdaptivePooling = AdaptivePoolingForward;
  754. class AdaptivePoolingBackward : public AdaptivePoolingBase {
  755. DEF_OPR_IMPL(AdaptivePoolingBackward, AdaptivePoolingBase, 3, 1);
  756. public:
  757. /**
  758. * \param[in] src the `src' parameter in AdaptivePoolingForward::exec
  759. * \param[in] dst the `dst' parameter in AdaptivePoolingForward::exec
  760. * \param[in] diff the backpropagated gradient wrt. dst
  761. * \param[out] grad the backpropagated gradient wrt. src
  762. */
  763. virtual void exec(
  764. _megdnn_tensor_in src, _megdnn_tensor_in dst, _megdnn_tensor_in diff,
  765. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  766. virtual size_t get_workspace_in_bytes(
  767. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  768. const TensorLayout& grad) = 0;
  769. };
  770. /**
  771. * \brief base class for Local
  772. */
  773. class LocalBase : public OperatorBase {
  774. DEF_OPR_IMPL_CTOR(LocalBase, OperatorBase);
  775. DEF_OPR_PARAM(Convolution);
  776. public:
  777. using Mode = Param::Mode;
  778. protected:
  779. void deduce_layout_fwd(
  780. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  781. void check_layout_fwd(
  782. const TensorLayout& src, const TensorLayout& filter,
  783. const TensorLayout& dst);
  784. };
  785. class LocalForward : public LocalBase {
  786. DEF_OPR_IMPL(LocalForward, LocalBase, 2, 1);
  787. public:
  788. /**
  789. * \param[in] src (n, ic, ih, iw)
  790. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  791. * \param[out] dst (n, oc, oh, ow)
  792. */
  793. virtual void exec(
  794. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
  795. _megdnn_workspace workspace) = 0;
  796. /**
  797. * \brief Deducing output tensor layouts from input tensor layouts.
  798. *
  799. * Be aware that the first and second dimension of `filter' are ignored
  800. * when deducing `dst' layout.
  801. */
  802. void deduce_layout(
  803. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  804. virtual size_t get_workspace_in_bytes(
  805. const TensorLayout& src, const TensorLayout& filter,
  806. const TensorLayout& dst) = 0;
  807. protected:
  808. void check_exec(
  809. const TensorLayout& src, const TensorLayout& filter,
  810. const TensorLayout& dst, size_t workspace_in_bytes);
  811. };
  812. using Local = LocalForward;
  813. class LocalBackwardData : public LocalBase {
  814. DEF_OPR_IMPL(LocalBackwardData, LocalBase, 2, 1);
  815. public:
  816. /**
  817. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  818. * \param[in] diff (n, oc, oh, ow)
  819. * \param[out] grad (n, ic, ih, iw)
  820. */
  821. virtual void exec(
  822. _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  823. _megdnn_workspace workspace) = 0;
  824. virtual size_t get_workspace_in_bytes(
  825. const TensorLayout& filter, const TensorLayout& diff,
  826. const TensorLayout& grad) = 0;
  827. protected:
  828. void check_exec(
  829. const TensorLayout& filter, const TensorLayout& diff,
  830. const TensorLayout& grad, size_t workspace_in_bytes);
  831. };
  832. class LocalBackwardFilter : public LocalBase {
  833. DEF_OPR_IMPL(LocalBackwardFilter, LocalBase, 2, 1);
  834. public:
  835. /**
  836. * \param[in] src (n, ic, ih, iw)
  837. * \param[in] diff (n, oc, oh, ow)
  838. * \param[out] grad (oh, ow, ic, fh, fw, oc)
  839. */
  840. virtual void exec(
  841. _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  842. _megdnn_workspace workspace) = 0;
  843. virtual size_t get_workspace_in_bytes(
  844. const TensorLayout& src, const TensorLayout& diff,
  845. const TensorLayout& grad) = 0;
  846. protected:
  847. void check_exec(
  848. const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad,
  849. size_t workspace_in_bytes);
  850. };
  851. class BNBase : public OperatorBase {
  852. DEF_OPR_IMPL_CTOR(BNBase, OperatorBase);
  853. DEF_OPR_PARAM(BN);
  854. protected:
  855. void check_param();
  856. };
  857. class BNForward : public BNBase {
  858. DEF_OPR_IMPL(BNForward, BNBase, 6, 6);
  859. public:
  860. /**
  861. * \dst[i] = gemma
  862. * *(x[i]-estimatedMean[k])/sqrt(epsilon+estimatedVariance[k]) + beta \where
  863. * epsilon is a very small value to avoid a "divide by zero" error.
  864. * \param[in] src (n, c, h, w)
  865. * \param[out] dst (n, c, h, w)
  866. * \param[out] mean (see m_param.ParamDim) Global mean.
  867. * \param[out] variance (see m_param.ParamDim) Global variance.
  868. * \param[out] batch_mean (see m_param.ParamDim)
  869. * Optionally cached intermediate mean from forward pass
  870. * \param[out] batch_inv_variance (see m_param.ParamDim)
  871. * Optionally cached intermediate variance from forward pass
  872. * \param[out] reserve (see cudnnBatchNormalizationForwardTrainingEx)
  873. * src and dst must have the same shape.
  874. * src and dst must be contiguous.
  875. */
  876. virtual void exec(
  877. _megdnn_tensor_in src, _megdnn_tensor_in bn_scale,
  878. _megdnn_tensor_in bn_bias, _megdnn_tensor_inout mean,
  879. _megdnn_tensor_inout variance, _megdnn_tensor_out batch_mean,
  880. _megdnn_tensor_out batch_inv_variance, _megdnn_tensor_out reserve,
  881. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  882. void deduce_layout(
  883. const TensorLayout& src, const TensorLayout& bn_scale,
  884. const TensorLayout& bn_bias, TensorLayout& mean, TensorLayout& variance,
  885. TensorLayout& batch_mean, TensorLayout& batch_inv_variance,
  886. TensorLayout& reserve, TensorLayout& dst);
  887. virtual size_t get_workspace_in_bytes(
  888. const TensorLayout& src, const TensorLayout& bn_scale,
  889. const TensorLayout& bn_bias, const TensorLayout& mean,
  890. const TensorLayout& variance, const TensorLayout& batch_mean,
  891. const TensorLayout& batch_inv_variance, const TensorLayout& reserve,
  892. const TensorLayout& dst) = 0;
  893. virtual size_t get_reserve_in_bytes(const TensorLayout& src) = 0;
  894. protected:
  895. void check_exec(
  896. const TensorLayout& src, const TensorLayout& bn_scale,
  897. const TensorLayout& bn_bias, const TensorLayout& mean,
  898. const TensorLayout& variance, const TensorLayout& batch_mean,
  899. const TensorLayout& batch_inv_variance, const TensorLayout& dst,
  900. size_t workspace_in_bytes, size_t reserve_in_bytes = 0);
  901. };
  902. using BN = BNForward;
  903. class BNBackward : public BNBase {
  904. DEF_OPR_IMPL(BNBackward, BNBase, 6, 3);
  905. public:
  906. /**
  907. * \param[in] input data of forwarding propagate.
  908. * \param[in] dy the backpropagated gradient of y.
  909. * \param[out] dx the backpropagated gradient of x.
  910. * \param[out] d_bn_scale, the backpropagated gradient of bn_scale.
  911. * \param[out] d_bn_bias, the backpropagated gradient of bn_bias.
  912. * Optionally cached intermediate results from forward pass
  913. * \param[in] saved_batch_mean mean of the input batch.
  914. Calculated in the forwardpropagation.
  915. * \param[in] saved_batch_variance of the input batch.
  916. Calculated in the forwardpropagation.
  917. * \param[in] reserve (see cudnnBatchNormalizationBackwardEx)
  918. */
  919. virtual void exec(
  920. _megdnn_tensor_in x, _megdnn_tensor_in dy,
  921. _megdnn_tensor_in saved_batch_mean, _megdnn_tensor_in saved_batch_variance,
  922. _megdnn_tensor_in bn_scale, _megdnn_tensor_in reserve,
  923. _megdnn_tensor_out d_bn_scale, _megdnn_tensor_out d_bn_bias,
  924. _megdnn_tensor_out dx, _megdnn_workspace workspace) = 0;
  925. virtual size_t get_workspace_in_bytes(
  926. const TensorLayout& x, const TensorLayout& dy,
  927. const TensorLayout& saved_batch_mean,
  928. const TensorLayout& saved_batch_variance, const TensorLayout& bn_scale,
  929. const TensorLayout& reserve, const TensorLayout& d_bn_scale,
  930. const TensorLayout& d_bn_bias, const TensorLayout& dx) = 0;
  931. virtual size_t get_reserve_in_bytes(const TensorLayout& src) = 0;
  932. protected:
  933. void check_exec(
  934. const TensorLayout& x, const TensorLayout& dy,
  935. const TensorLayout& saved_batch_mean,
  936. const TensorLayout& saved_batch_variance, const TensorLayout& bn_scale,
  937. const TensorLayout& d_bn_scale, const TensorLayout& d_bn_bias,
  938. const TensorLayout& dx, size_t workspace_in_bytes,
  939. size_t reserve_in_bytes = 0);
  940. };
  941. class LRNBase : public OperatorBase {
  942. DEF_OPR_IMPL_CTOR(LRNBase, OperatorBase);
  943. DEF_OPR_PARAM(LRN);
  944. protected:
  945. void check_param();
  946. };
  947. class LRNForward : public LRNBase {
  948. DEF_OPR_IMPL(LRNForward, LRNBase, 1, 1);
  949. public:
  950. /**
  951. * \see ImageNet Classification with Deep Convolutional Neural Networks
  952. * \param[in] src (n, c, h, w)
  953. * \param[out] dst (n, c, h, w)
  954. *
  955. * src and dst must have the same shape.
  956. * src and dst must be contiguous.
  957. */
  958. virtual void exec(
  959. _megdnn_tensor_in src, _megdnn_tensor_out dst,
  960. _megdnn_workspace workspace) = 0;
  961. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  962. virtual size_t get_workspace_in_bytes(
  963. const TensorLayout& src, const TensorLayout& dst) = 0;
  964. protected:
  965. void check_exec(
  966. const TensorLayout& src, const TensorLayout& dst,
  967. size_t workspace_in_bytes);
  968. };
  969. using LRN = LRNForward;
  970. class LRNBackward : public LRNBase {
  971. DEF_OPR_IMPL(LRNBackward, LRNBase, 3, 1);
  972. public:
  973. /**
  974. * \param[in] src the `src' parameter in LRNForward::exec
  975. * \param[in] dst the `dst' parameter in LRNForward::exec
  976. * \param[in] diff the backpropagated gradient wrt. dst
  977. * \param[out] grad the backpropagated gradient wrt. src
  978. *
  979. * All tensors should be contiguous and of the same shape.
  980. */
  981. virtual void exec(
  982. _megdnn_tensor_in src, _megdnn_tensor_in dst, _megdnn_tensor_in diff,
  983. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  984. virtual size_t get_workspace_in_bytes(
  985. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  986. const TensorLayout& grad) = 0;
  987. protected:
  988. void check_exec(
  989. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  990. const TensorLayout& grad, size_t workspace_in_bytes);
  991. };
  992. class ROIPoolingBase : public OperatorBase {
  993. DEF_OPR_IMPL_CTOR(ROIPoolingBase, OperatorBase);
  994. DEF_OPR_PARAM(ROIPooling);
  995. protected:
  996. void check_layout_fwd(
  997. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  998. const TensorLayout& index);
  999. };
  1000. class ROIPoolingForward : public ROIPoolingBase {
  1001. DEF_OPR_IMPL(ROIPoolingForward, ROIPoolingBase, 2, 2);
  1002. public:
  1003. /**
  1004. * \param[in] src (n, c, ih, iw)
  1005. * \param[in] rois (m, 5)
  1006. * \param[out] dst (m, c, oh, ow)
  1007. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  1008. *
  1009. * The internal implementation is akin to
  1010. * https://github.com/rbgirshick/caffe-fast-rcnn .d
  1011. * Note that rois(, 0) denotes the input image index. We store it as
  1012. * a float, but it should be an integer instead.
  1013. *
  1014. * index is a temporary tensor to facilitate its backward operator.
  1015. * It is used to store argmax indicex in MAX mode, and it is not used
  1016. * in AVERAGE mode.
  1017. */
  1018. virtual void exec(
  1019. _megdnn_tensor_in src, _megdnn_tensor_in rois, _megdnn_tensor_out dst,
  1020. _megdnn_tensor_out index, _megdnn_workspace workspace) = 0;
  1021. virtual size_t get_workspace_in_bytes(
  1022. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  1023. const TensorLayout& index) = 0;
  1024. protected:
  1025. void check_exec(
  1026. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  1027. const TensorLayout& index, size_t workspace_in_bytes);
  1028. };
  1029. using ROIPooling = ROIPoolingForward;
  1030. class ROIPoolingBackward : public ROIPoolingBase {
  1031. DEF_OPR_IMPL(ROIPoolingBackward, ROIPoolingBase, 4, 1);
  1032. public:
  1033. /**
  1034. * \param[in] diff the backpropagated gradient wrt. dst
  1035. * \param[in] src the `src' parameter in ROIPoolingForward::exec
  1036. * \param[in] rois the `rois' parameter in ROIPoolingForward::exec
  1037. * \param[in] index the `index' parameter in ROIPoolingForward::exec
  1038. * \param[out] grad the backpropagated gradient wrt. src
  1039. */
  1040. virtual void exec(
  1041. _megdnn_tensor_in diff, _megdnn_tensor_in src, _megdnn_tensor_in rois,
  1042. _megdnn_tensor_in index, _megdnn_tensor_out grad,
  1043. _megdnn_workspace workspace) = 0;
  1044. virtual size_t get_workspace_in_bytes(
  1045. const TensorLayout& diff, const TensorLayout& src, const TensorLayout& rois,
  1046. const TensorLayout& index, const TensorLayout& grad) = 0;
  1047. protected:
  1048. void check_exec(
  1049. const TensorLayout& diff, const TensorLayout& src, const TensorLayout& rois,
  1050. const TensorLayout& index, const TensorLayout& grad,
  1051. size_t workspace_in_bytes);
  1052. };
  1053. class Convolution3DBase : public OperatorBase {
  1054. DEF_OPR_IMPL_CTOR(Convolution3DBase, OperatorBase);
  1055. DEF_OPR_PARAM(Convolution3D);
  1056. public:
  1057. static constexpr size_t MAX_SPATIAL_DIM = 3;
  1058. using Mode = Param::Mode;
  1059. struct CanonizedFilterMeta {
  1060. DTypeEnum dtype_enum;
  1061. Param::Format format;
  1062. uint32_t
  1063. //! whether filter should be flipped (i.e. is CONVOLUTION)
  1064. should_flip,
  1065. group, //!< number of groups
  1066. icpg, //!< input channels per group
  1067. ocpg, //!< output channels per group
  1068. spatial_ndim, stride[MAX_SPATIAL_DIM], padding[MAX_SPATIAL_DIM],
  1069. //! spatial dim
  1070. spatial[MAX_SPATIAL_DIM], dilation[MAX_SPATIAL_DIM],
  1071. //! spatial dim with dilation applied
  1072. dilated_spatial[MAX_SPATIAL_DIM];
  1073. } MEGDNN_PACKED;
  1074. protected:
  1075. CanonizedFilterMeta deduce_layout_fwd(
  1076. const TensorLayout& src, const TensorLayout& filter,
  1077. TensorLayout& dst) const;
  1078. CanonizedFilterMeta check_layout_fwd(
  1079. const TensorLayout& src, const TensorLayout& filter,
  1080. const TensorLayout& dst) const;
  1081. static CanonizedFilterMeta make_canonized_filter_meta_impl(
  1082. size_t src_ndim, const TensorLayout& filter, const Param& param);
  1083. CanonizedFilterMeta make_canonized_filter_meta(
  1084. size_t src_ndim, const TensorLayout& filter) const;
  1085. };
  1086. class Convolution3DForward : public Convolution3DBase,
  1087. public detail::MultiAlgoOpr<Convolution3DForward, 3> {
  1088. DEF_OPR_IMPL(Convolution3DForward, Convolution3DBase, 2, 1);
  1089. public:
  1090. /**
  1091. * \param[in] src (n, ic, id, ih, iw)
  1092. * \param[in] filter (oc, ic, fd, fh, fw)
  1093. * \param[out] dst (n, oc, od, oh, ow)
  1094. */
  1095. virtual void exec(
  1096. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
  1097. _megdnn_workspace workspace) = 0;
  1098. void deduce_layout(
  1099. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  1100. virtual size_t get_workspace_in_bytes(
  1101. const TensorLayout& src, const TensorLayout& filter,
  1102. const TensorLayout& dst) = 0;
  1103. static Algorithm::OprType get_opr_type() {
  1104. return Algorithm::OprType::CONVOLUTION3D_FORWARD;
  1105. }
  1106. protected:
  1107. CanonizedFilterMeta check_exec(
  1108. const TensorLayout& src, const TensorLayout& filter,
  1109. const TensorLayout& dst, size_t workspace_in_bytes);
  1110. };
  1111. using Convolution3D = Convolution3DForward;
  1112. class Convolution3DBackwardData
  1113. : public Convolution3DBase,
  1114. public detail::MultiAlgoOpr<Convolution3DBackwardData, 3> {
  1115. DEF_OPR_IMPL(Convolution3DBackwardData, Convolution3DBase, 2, 1);
  1116. public:
  1117. /**
  1118. * \param[in] filter (oc, ic, fd, fh, fw)
  1119. * \param[in] diff (n, oc, od, oh, ow)
  1120. * \param[out] grad (n, ic, id, ih, iw)
  1121. */
  1122. MGE_WIN_DECLSPEC_FUC static void deduce_layout_impl(
  1123. const TensorLayout& filter, const TensorLayout& diff, const Param& param,
  1124. TensorLayout& grad);
  1125. virtual void exec(
  1126. _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  1127. _megdnn_workspace workspace) = 0;
  1128. virtual size_t get_workspace_in_bytes(
  1129. const TensorLayout& filter, const TensorLayout& diff,
  1130. const TensorLayout& grad) = 0;
  1131. void deduce_layout(
  1132. const TensorLayout& filter, const TensorLayout& diff, TensorLayout& grad);
  1133. static Algorithm::OprType get_opr_type() {
  1134. return Algorithm::OprType::CONVOLUTION3D_BACKWARD_DATA;
  1135. }
  1136. protected:
  1137. CanonizedFilterMeta check_exec(
  1138. const TensorLayout& filter, const TensorLayout& diff,
  1139. const TensorLayout& grad, size_t workspace_in_bytes);
  1140. };
  1141. class Convolution3DBackwardFilter
  1142. : public Convolution3DBase,
  1143. public detail::MultiAlgoOpr<Convolution3DBackwardFilter, 3> {
  1144. DEF_OPR_IMPL(Convolution3DBackwardFilter, Convolution3DBase, 2, 1);
  1145. public:
  1146. /**
  1147. * \param[in] src (n, ic, id, ih, iw)
  1148. * \param[in] diff (n, oc, od, oh, ow)
  1149. * \param[out] grad (oc, ic, fd, fh, fw)
  1150. */
  1151. virtual void exec(
  1152. _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  1153. _megdnn_workspace workspace) = 0;
  1154. virtual size_t get_workspace_in_bytes(
  1155. const TensorLayout& src, const TensorLayout& diff,
  1156. const TensorLayout& grad) = 0;
  1157. static Algorithm::OprType get_opr_type() {
  1158. return Algorithm::OprType::CONVOLUTION3D_BACKWARD_FILTER;
  1159. }
  1160. protected:
  1161. CanonizedFilterMeta check_exec(
  1162. const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad,
  1163. size_t workspace_in_bytes);
  1164. };
  1165. class LocalShareBase : public OperatorBase {
  1166. DEF_OPR_IMPL_CTOR(LocalShareBase, OperatorBase);
  1167. DEF_OPR_PARAM(LocalShare);
  1168. protected:
  1169. void deduce_layout_fwd(
  1170. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  1171. void check_layout_fwd(
  1172. const TensorLayout& src, const TensorLayout& filter,
  1173. const TensorLayout& dst);
  1174. };
  1175. class LocalShareForward : public LocalShareBase,
  1176. public detail::MultiAlgoOpr<LocalShareForward, 3> {
  1177. DEF_OPR_IMPL(LocalShareForward, LocalShareBase, 2, 1);
  1178. public:
  1179. /**
  1180. * \param[in] src (N, IC, IH, IW)
  1181. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1182. * FH, FW, OC / G)
  1183. * \param[out] dst (N, OC, OH, OW)
  1184. */
  1185. virtual void exec(
  1186. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
  1187. _megdnn_workspace workspace) = 0;
  1188. /**
  1189. * \brief deduce layout of the ouput tensor
  1190. */
  1191. void deduce_layout(
  1192. const TensorLayout& src, const TensorLayout& filter, TensorLayout& dst);
  1193. virtual size_t get_workspace_in_bytes(
  1194. const TensorLayout& src, const TensorLayout& filter,
  1195. const TensorLayout& dst) = 0;
  1196. static Algorithm::OprType get_opr_type() {
  1197. return Algorithm::OprType::LOCAL_SHARE_FORWARD;
  1198. }
  1199. protected:
  1200. void check_exec(
  1201. const TensorLayout& src, const TensorLayout& filter,
  1202. const TensorLayout& dst, size_t workspace_in_bytes);
  1203. };
  1204. using LocalShare = LocalShareForward;
  1205. class LocalShareBackwardData : public LocalShareBase,
  1206. public detail::MultiAlgoOpr<LocalShareBackwardData, 3> {
  1207. DEF_OPR_IMPL(LocalShareBackwardData, LocalShareBase, 2, 1);
  1208. public:
  1209. /**
  1210. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1211. * FH, FW, OC / G)
  1212. * \param[in] diff (N, OC, OH, OW)
  1213. * \param[out] grad (N, IC, IH, IW)
  1214. */
  1215. virtual void exec(
  1216. _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  1217. _megdnn_workspace workspace) = 0;
  1218. virtual size_t get_workspace_in_bytes(
  1219. const TensorLayout& filter, const TensorLayout& diff,
  1220. const TensorLayout& grad) = 0;
  1221. void deduce_layout(
  1222. const TensorLayout& filter, const TensorLayout& diff, TensorLayout& grad);
  1223. static Algorithm::OprType get_opr_type() {
  1224. return Algorithm::OprType::LOCAL_SHARE_BACKWARD_DATA;
  1225. }
  1226. protected:
  1227. void check_exec(
  1228. const TensorLayout& filter, const TensorLayout& diff,
  1229. const TensorLayout& grad, size_t workspace_in_bytes);
  1230. };
  1231. class LocalShareBackwardFilter
  1232. : public LocalShareBase,
  1233. public detail::MultiAlgoOpr<LocalShareBackwardFilter, 3> {
  1234. DEF_OPR_IMPL(LocalShareBackwardFilter, LocalShareBase, 2, 1);
  1235. public:
  1236. /**
  1237. * \param[in] src (N, IC, IH, IW)
  1238. * \param[in] diff (N, OC, OH, OW)
  1239. * \param[out] grad (G, spatial_groups_h, spatial_groups_w, IC / G,
  1240. * FH, FW, OC / G)
  1241. */
  1242. virtual void exec(
  1243. _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  1244. _megdnn_workspace workspace) = 0;
  1245. virtual size_t get_workspace_in_bytes(
  1246. const TensorLayout& src, const TensorLayout& diff,
  1247. const TensorLayout& grad) = 0;
  1248. static Algorithm::OprType get_opr_type() {
  1249. return Algorithm::OprType::LOCAL_SHARE_BACKWARD_FILTER;
  1250. }
  1251. protected:
  1252. void check_exec(
  1253. const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad,
  1254. size_t workspace_in_bytes);
  1255. };
  1256. class ROIAlignBase : public OperatorBase {
  1257. DEF_OPR_IMPL_CTOR(ROIAlignBase, OperatorBase);
  1258. DEF_OPR_PARAM(ROIAlign);
  1259. protected:
  1260. void deduce_layout_fwd(
  1261. const TensorLayout& src, const TensorLayout& rois, TensorLayout& dst,
  1262. TensorLayout& index);
  1263. void check_layout_fwd(
  1264. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  1265. const TensorLayout& index);
  1266. };
  1267. class ROIAlignForward : public ROIAlignBase {
  1268. DEF_OPR_IMPL(ROIAlignForward, ROIAlignBase, 2, 2);
  1269. public:
  1270. /**
  1271. * \param[in] src (n, c, ih, iw)
  1272. * \param[in] rois (m, 5)
  1273. * \param[out] dst (m, c, oh, ow)
  1274. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  1275. *
  1276. * Note that rois(, 0) denotes the input image index. We store it as
  1277. * a float, but it should be an integer instead.
  1278. *
  1279. * index is a temporary tensor to facilitate its backward operator.
  1280. * It is used to store argmax indicex in MAX mode, and it is not used
  1281. * in AVERAGE mode.
  1282. */
  1283. virtual void exec(
  1284. _megdnn_tensor_in src, _megdnn_tensor_in rois, _megdnn_tensor_out dst,
  1285. _megdnn_tensor_out index, _megdnn_workspace workspace) = 0;
  1286. void deduce_layout(
  1287. const TensorLayout& src, const TensorLayout& rois, TensorLayout& dst,
  1288. TensorLayout& index);
  1289. virtual size_t get_workspace_in_bytes(
  1290. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  1291. const TensorLayout& index) = 0;
  1292. protected:
  1293. void check_exec(
  1294. const TensorLayout& src, const TensorLayout& rois, const TensorLayout& dst,
  1295. const TensorLayout& index, size_t workspace_in_bytes);
  1296. };
  1297. using ROIAlign = ROIAlignForward;
  1298. class ROIAlignBackward : public ROIAlignBase {
  1299. DEF_OPR_IMPL(ROIAlignBackward, ROIAlignBase, 3, 1);
  1300. public:
  1301. /**
  1302. * \param[in] diff the backpropagated gradient wrt. dst
  1303. * \param[in] rois the `rois' parameter in ROIAlignForward::exec
  1304. * \param[in] index the `index' parameter in ROIAlignForward::exec
  1305. * \param[out] grad the backpropagated gradient wrt. src
  1306. */
  1307. virtual void exec(
  1308. _megdnn_tensor_in diff, _megdnn_tensor_in rois, _megdnn_tensor_in index,
  1309. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1310. virtual size_t get_workspace_in_bytes(
  1311. const TensorLayout& diff, const TensorLayout& rois,
  1312. const TensorLayout& index, const TensorLayout& grad) = 0;
  1313. protected:
  1314. void check_exec(
  1315. const TensorLayout& diff, const TensorLayout& rois,
  1316. const TensorLayout& index, const TensorLayout& grad,
  1317. size_t workspace_in_bytes);
  1318. };
  1319. class DeformableConvBase : public OperatorBase {
  1320. DEF_OPR_IMPL_CTOR(DeformableConvBase, OperatorBase);
  1321. DEF_OPR_PARAM(Convolution);
  1322. public:
  1323. static constexpr size_t MAX_SPATIAL_DIM = 2;
  1324. struct CanonizedFilterMeta : Convolution::CanonizedFilterMeta {
  1325. uint32_t deformable_group;
  1326. };
  1327. protected:
  1328. CanonizedFilterMeta make_canonized_filter_meta(
  1329. size_t src_ndim, const TensorLayout& filter,
  1330. const TensorLayout& offset) const;
  1331. void deduce_layout_fwd(
  1332. const TensorLayout& im, const TensorLayout& filter,
  1333. const TensorLayout& mask, const TensorLayout& offset, TensorLayout& dst);
  1334. void check_layout_fwd(
  1335. const TensorLayout& src, const TensorLayout& filter,
  1336. const TensorLayout& mask, const TensorLayout& offset,
  1337. const TensorLayout& dst);
  1338. };
  1339. class DeformableConvForward : public DeformableConvBase,
  1340. public detail::MultiAlgoOpr<DeformableConvForward, 5> {
  1341. DEF_OPR_IMPL(DeformableConvForward, DeformableConvBase, 4, 1);
  1342. public:
  1343. /**
  1344. * \param[in] im (n, ic, ih, iw)
  1345. * \param[in] filter (oc, ic, fh, fw)
  1346. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1347. * \param[in] mask (dg, fh, fw, oh, ow)
  1348. * \param[out] dst (n, oc, oh, ow)
  1349. */
  1350. virtual void exec(
  1351. _megdnn_tensor_in im, _megdnn_tensor_in filter, _megdnn_tensor_in offset,
  1352. _megdnn_tensor_in mask, _megdnn_tensor_out dst,
  1353. _megdnn_workspace workspace) = 0;
  1354. void deduce_layout(
  1355. const TensorLayout& im, const TensorLayout& filter,
  1356. const TensorLayout& offset, const TensorLayout& mask, TensorLayout& dst);
  1357. virtual size_t get_workspace_in_bytes(
  1358. const TensorLayout& im, const TensorLayout& filter,
  1359. const TensorLayout& offset, const TensorLayout& mask,
  1360. const TensorLayout& dst) = 0;
  1361. static Algorithm::OprType get_opr_type() {
  1362. return Algorithm::OprType::DEFORMABLE_CONV_FORWARD;
  1363. }
  1364. protected:
  1365. CanonizedFilterMeta check_exec(
  1366. const TensorLayout& im, const TensorLayout& filter,
  1367. const TensorLayout& offset, const TensorLayout& mask,
  1368. const TensorLayout& dst, size_t workspace_in_bytes);
  1369. };
  1370. using DeformableConv = DeformableConvForward;
  1371. /**
  1372. * \brief DeformableConvBackwardFilter operator.
  1373. *
  1374. * Calculating the gradient wrt. convolution filter.
  1375. */
  1376. class DeformableConvBackwardFilter
  1377. : public DeformableConvBase,
  1378. public detail::MultiAlgoOpr<DeformableConvBackwardFilter, 5> {
  1379. DEF_OPR_IMPL(DeformableConvBackwardFilter, DeformableConvBase, 4, 1);
  1380. public:
  1381. /**
  1382. * \param[in] im (oc, ic, fh, fw)
  1383. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1384. * \param[in] mask (dg, fh, fw, oh, ow)
  1385. * \param[in] out_grad (n, oc, oh, ow)
  1386. * \param[out] filter_grad (oc, ic, ih, iw)
  1387. */
  1388. virtual void exec(
  1389. _megdnn_tensor_in im, _megdnn_tensor_in offset, _megdnn_tensor_in mask,
  1390. _megdnn_tensor_in out_grad, _megdnn_tensor_out filter_grad,
  1391. _megdnn_workspace workspace) = 0;
  1392. virtual size_t get_workspace_in_bytes(
  1393. const TensorLayout& im, const TensorLayout& offset,
  1394. const TensorLayout& mask, const TensorLayout& out_grad,
  1395. const TensorLayout& filter_grad) = 0;
  1396. void deduce_layout(
  1397. const TensorLayout& im, const TensorLayout& offset,
  1398. const TensorLayout& mask, const TensorLayout& out_grad,
  1399. TensorLayout& filter_grad);
  1400. static Algorithm::OprType get_opr_type() {
  1401. return Algorithm::OprType::DEFORMABLE_CONV_BACKWARD_FILTER;
  1402. }
  1403. protected:
  1404. CanonizedFilterMeta check_exec(
  1405. const TensorLayout& im, const TensorLayout& offset,
  1406. const TensorLayout& mask, const TensorLayout& out_grad,
  1407. const TensorLayout& filter_grad, size_t workspace_in_bytes);
  1408. };
  1409. /**
  1410. * \brief DeformableConvBackwardData operator.
  1411. *
  1412. * Calculating the gradient wrt. convolution input data, offset and mask.
  1413. */
  1414. class DeformableConvBackwardData
  1415. : public DeformableConvBase,
  1416. public detail::MultiAlgoOpr<DeformableConvBackwardData, 8> {
  1417. DEF_OPR_IMPL(DeformableConvBackwardData, DeformableConvBase, 5, 3);
  1418. public:
  1419. /**
  1420. * \param[in] im (oc, ic, fh, fw)
  1421. * \param[in] filter (oc, ic, fh, fw)
  1422. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1423. * \param[in] mask (dg, fh, fw, oh, ow)
  1424. * \param[in] out_grad (n, oc, oh, ow)
  1425. * \param[out] im_grad (n, ic, ih, iw)
  1426. * \param[out] offset_grad (dg, 2, fh, fw, oh, ow)
  1427. * \param[out] mask_grad (dg, fh, fw, oh, ow)
  1428. */
  1429. virtual void exec(
  1430. _megdnn_tensor_in im, _megdnn_tensor_in filter, _megdnn_tensor_in offset,
  1431. _megdnn_tensor_in mask, _megdnn_tensor_in out_grad,
  1432. _megdnn_tensor_out im_grad, _megdnn_tensor_out offset_grad,
  1433. _megdnn_tensor_out mask_grad, _megdnn_workspace workspace) = 0;
  1434. virtual size_t get_workspace_in_bytes(
  1435. const TensorLayout& im, const TensorLayout& filter,
  1436. const TensorLayout& offset, const TensorLayout& mask,
  1437. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1438. const TensorLayout& offset_grad, const TensorLayout& mask_grad) = 0;
  1439. void deduce_layout(
  1440. const TensorLayout& im, const TensorLayout& filter,
  1441. const TensorLayout& offset, const TensorLayout& mask,
  1442. const TensorLayout& out_grad, TensorLayout& im_grad,
  1443. TensorLayout& offset_grad, TensorLayout& mask_grad);
  1444. static Algorithm::OprType get_opr_type() {
  1445. return Algorithm::OprType::DEFORMABLE_CONV_BACKWARD_DATA;
  1446. }
  1447. protected:
  1448. CanonizedFilterMeta check_exec(
  1449. const TensorLayout& im, const TensorLayout& filter,
  1450. const TensorLayout& offset, const TensorLayout& mask,
  1451. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1452. const TensorLayout& offset_grad, const TensorLayout& mask_grad,
  1453. size_t workspace_in_bytes);
  1454. };
  1455. class DeformablePSROIPoolingBase : public OperatorBase {
  1456. DEF_OPR_IMPL_CTOR(DeformablePSROIPoolingBase, OperatorBase);
  1457. DEF_OPR_PARAM(DeformablePSROIPooling);
  1458. protected:
  1459. void deduce_layout_fwd(
  1460. const TensorLayout& data, const TensorLayout& trans,
  1461. const TensorLayout& rois, TensorLayout& out_data, TensorLayout& out_count);
  1462. void check_layout_fwd(
  1463. const TensorLayout& data, const TensorLayout& trans,
  1464. const TensorLayout& rois, const TensorLayout& out_data,
  1465. const TensorLayout& out_count, size_t workspace_in_bytes);
  1466. };
  1467. class DeformablePSROIPoolingForward : public DeformablePSROIPoolingBase {
  1468. DEF_OPR_IMPL(DeformablePSROIPoolingForward, DeformablePSROIPoolingBase, 3, 2);
  1469. public:
  1470. /**
  1471. * \param[in] data (oc, ic, ih, iw)
  1472. * \param[in] rois (xx, xx, xx, xx)
  1473. * \param[in] trans (oc, ic, fh, fw)
  1474. * \param[out] out_data ( n, ic, ih, iw)
  1475. * \param[out] out_count ( n, ic, ih, iw)
  1476. */
  1477. virtual size_t get_workspace_in_bytes(
  1478. const TensorLayout& data, const TensorLayout& rois,
  1479. const TensorLayout& trans, const TensorLayout& out_data,
  1480. const TensorLayout& out_count) = 0;
  1481. virtual void exec(
  1482. _megdnn_tensor_in data, _megdnn_tensor_in rois, _megdnn_tensor_in trans,
  1483. _megdnn_tensor_out out_data, _megdnn_tensor_out out_count,
  1484. _megdnn_workspace workspace) = 0;
  1485. void deduce_layout(
  1486. const TensorLayout& data, const TensorLayout& rois,
  1487. const TensorLayout& trans, TensorLayout& out_data, TensorLayout& out_count);
  1488. void check_exec(
  1489. const TensorLayout& data, const TensorLayout& rois,
  1490. const TensorLayout& trans, const TensorLayout& out_data,
  1491. const TensorLayout& out_count, size_t workspace_in_bytes);
  1492. };
  1493. using DeformablePSROIPooling = DeformablePSROIPoolingForward;
  1494. class DeformablePSROIPoolingBackward : public DeformablePSROIPoolingBase {
  1495. DEF_OPR_IMPL(DeformablePSROIPoolingBackward, DeformablePSROIPoolingBase, 5, 2);
  1496. public:
  1497. /**
  1498. * \param[in] data (oc, ic, ih, iw)
  1499. * \param[in] rois (xx, xx, xx, xx)
  1500. * \param[in] trans (oc, ic, fh, fw)
  1501. * \param[in] out_diff (xx, xx, xx, xx)
  1502. * \param[in] out_count (xx, xx, xx, xx)
  1503. * \param[out] data_diff ( n, ic, ih, iw)
  1504. * \param[out] trans_diff ( n, ic, ih, iw)
  1505. */
  1506. virtual void exec(
  1507. _megdnn_tensor_in data, _megdnn_tensor_in rois, _megdnn_tensor_in trans,
  1508. _megdnn_tensor_in out_diff, _megdnn_tensor_in out_count,
  1509. _megdnn_tensor_out data_diff, _megdnn_tensor_out trans_diff,
  1510. _megdnn_workspace workspace) = 0;
  1511. virtual size_t get_workspace_in_bytes(
  1512. const TensorLayout& data, const TensorLayout& rois,
  1513. const TensorLayout& trans, const TensorLayout& out_diff,
  1514. const TensorLayout& out_count, const TensorLayout& data_diff,
  1515. const TensorLayout& trans_diff) = 0;
  1516. void check_exec(
  1517. const TensorLayout& data, const TensorLayout& rois,
  1518. const TensorLayout& trans, const TensorLayout& out_diff,
  1519. const TensorLayout& out_count, const TensorLayout& data_diff,
  1520. const TensorLayout& trans_diff, size_t workspace_in_bytes);
  1521. };
  1522. class BatchConvBiasForward : public ConvolutionBase<param::BatchConvBias>,
  1523. public detail::MultiAlgoOpr<BatchConvBiasForward, 5> {
  1524. DEF_OPR_IMPL(BatchConvBiasForward, ConvolutionBase, 4, 1);
  1525. public:
  1526. virtual void exec(
  1527. _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_in bias,
  1528. _megdnn_tensor_in z, _megdnn_tensor_out dst,
  1529. _megdnn_workspace workspace) = 0;
  1530. void deduce_dtype(DType src, DType filter, DType bias, DType z, DType& dst);
  1531. void deduce_layout(
  1532. const TensorLayout& src, const TensorLayout& filter,
  1533. const TensorLayout& bias, const TensorLayout& z, TensorLayout& dst);
  1534. virtual size_t get_workspace_in_bytes(
  1535. const TensorLayout& src, const TensorLayout& filter,
  1536. const TensorLayout& bias, const TensorLayout& z,
  1537. const TensorLayout& dst) = 0;
  1538. static Algorithm::OprType get_opr_type() {
  1539. return Algorithm::OprType::BATCH_CONV_FORWARD;
  1540. }
  1541. protected:
  1542. CanonizedFilterMeta check_exec(
  1543. const TensorLayout& src, const TensorLayout& filter,
  1544. const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst,
  1545. size_t workspace_in_bytes);
  1546. };
  1547. using BatchConvBias = BatchConvBiasForward;
  1548. class FakeQuantBase : public OperatorBase {
  1549. DEF_OPR_IMPL_CTOR(FakeQuantBase, OperatorBase);
  1550. DEF_OPR_PARAM(FakeQuant);
  1551. protected:
  1552. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1553. void check_layout_fwd(
  1554. const TensorLayout& input, const TensorLayout& scale,
  1555. const TensorLayout& zero_point, const TensorLayout& output);
  1556. };
  1557. class FakeQuantForward : public FakeQuantBase {
  1558. DEF_OPR_IMPL(FakeQuantForward, FakeQuantBase, 3, 1);
  1559. public:
  1560. virtual void exec(
  1561. _megdnn_tensor_in input, _megdnn_tensor_in scale,
  1562. _megdnn_tensor_in zero_point, _megdnn_tensor_out output,
  1563. _megdnn_workspace workspace) = 0;
  1564. void deduce_layout(
  1565. const TensorLayout& input, const TensorLayout& scale,
  1566. const TensorLayout& zero_point, TensorLayout& output);
  1567. virtual size_t get_workspace_in_bytes(
  1568. const TensorLayout& input, const TensorLayout& scale,
  1569. const TensorLayout& zero_point, const TensorLayout& output) = 0;
  1570. protected:
  1571. void check_exec(
  1572. const TensorLayout& input, const TensorLayout& scale,
  1573. const TensorLayout& zero_point, const TensorLayout& output,
  1574. size_t workspace_in_bytes);
  1575. };
  1576. using FakeQuant = FakeQuantForward;
  1577. class FakeQuantBackward : public FakeQuantBase {
  1578. DEF_OPR_IMPL(FakeQuantBackward, FakeQuantBase, 4, 1);
  1579. public:
  1580. virtual void exec(
  1581. _megdnn_tensor_in diff, _megdnn_tensor_in input, _megdnn_tensor_in scale,
  1582. _megdnn_tensor_in zero_point, _megdnn_tensor_out grad,
  1583. _megdnn_workspace workspace) = 0;
  1584. virtual size_t get_workspace_in_bytes(
  1585. const TensorLayout& diff, const TensorLayout& input,
  1586. const TensorLayout& scale, const TensorLayout& zero_point,
  1587. const TensorLayout& grad) = 0;
  1588. protected:
  1589. void check_exec(
  1590. const TensorLayout& diff, const TensorLayout& input,
  1591. const TensorLayout& scale, const TensorLayout& zero_point,
  1592. const TensorLayout& grad, size_t workspace_in_bytes);
  1593. };
  1594. class TQTBase : public OperatorBase {
  1595. DEF_OPR_IMPL_CTOR(TQTBase, OperatorBase);
  1596. DEF_OPR_PARAM(TQT);
  1597. protected:
  1598. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1599. void check_layout_fwd(
  1600. const TensorLayout& input, const TensorLayout& scale,
  1601. const TensorLayout& output);
  1602. };
  1603. class TQTForward : public TQTBase {
  1604. DEF_OPR_IMPL(TQTForward, TQTBase, 2, 1);
  1605. public:
  1606. virtual void exec(
  1607. _megdnn_tensor_in input, _megdnn_tensor_in scale, _megdnn_tensor_out output,
  1608. _megdnn_workspace workspace) = 0;
  1609. void deduce_layout(
  1610. const TensorLayout& input, const TensorLayout& scale, TensorLayout& output);
  1611. virtual size_t get_workspace_in_bytes(
  1612. const TensorLayout& input, const TensorLayout& scale,
  1613. const TensorLayout& output) = 0;
  1614. protected:
  1615. void check_exec(
  1616. const TensorLayout& input, const TensorLayout& scale,
  1617. const TensorLayout& output, size_t workspace_in_bytes);
  1618. };
  1619. using TQT = TQTForward;
  1620. class TQTBackward : public TQTBase {
  1621. DEF_OPR_IMPL(TQTBackward, TQTBase, 3, 2);
  1622. public:
  1623. virtual void exec(
  1624. _megdnn_tensor_in diff, _megdnn_tensor_in input, _megdnn_tensor_in scale,
  1625. _megdnn_tensor_out grad_x, _megdnn_tensor_out grad_s,
  1626. _megdnn_workspace workspace) = 0;
  1627. virtual size_t get_workspace_in_bytes(
  1628. const TensorLayout& diff, const TensorLayout& input,
  1629. const TensorLayout& scale, const TensorLayout& grad_x,
  1630. const TensorLayout& grad_s) = 0;
  1631. protected:
  1632. void check_exec(
  1633. const TensorLayout& diff, const TensorLayout& input,
  1634. const TensorLayout& scale, const TensorLayout& grad_x,
  1635. const TensorLayout& grad_s, size_t workspace_in_bytes);
  1636. };
  1637. class LSQBase : public OperatorBase {
  1638. DEF_OPR_IMPL_CTOR(LSQBase, OperatorBase);
  1639. DEF_OPR_PARAM(LSQ);
  1640. protected:
  1641. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1642. void check_layout_fwd(
  1643. const TensorLayout& input, const TensorLayout& scale,
  1644. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  1645. const TensorLayout& output);
  1646. };
  1647. class LSQForward : public LSQBase {
  1648. DEF_OPR_IMPL(LSQForward, LSQBase, 4, 1);
  1649. public:
  1650. virtual void exec(
  1651. _megdnn_tensor_in input, _megdnn_tensor_in scale,
  1652. _megdnn_tensor_in zero_point, _megdnn_tensor_in grad_scale,
  1653. _megdnn_tensor_out output, _megdnn_workspace workspace) = 0;
  1654. void deduce_layout(
  1655. const TensorLayout& input, const TensorLayout& scale,
  1656. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  1657. TensorLayout& output);
  1658. virtual size_t get_workspace_in_bytes(
  1659. const TensorLayout& input, const TensorLayout& scale,
  1660. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  1661. const TensorLayout& output) = 0;
  1662. protected:
  1663. void check_exec(
  1664. const TensorLayout& input, const TensorLayout& scale,
  1665. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  1666. const TensorLayout& output, size_t workspace_in_bytes);
  1667. };
  1668. using LSQ = LSQForward;
  1669. class LSQBackward : public LSQBase {
  1670. DEF_OPR_IMPL(LSQBackward, LSQBase, 5, 2);
  1671. public:
  1672. virtual void exec(
  1673. _megdnn_tensor_in diff, _megdnn_tensor_in input, _megdnn_tensor_in scale,
  1674. _megdnn_tensor_in zero_point, _megdnn_tensor_in grad_scale,
  1675. _megdnn_tensor_out grad_x, _megdnn_tensor_out grad_s,
  1676. _megdnn_workspace workspace) = 0;
  1677. virtual size_t get_workspace_in_bytes(
  1678. const TensorLayout& diff, const TensorLayout& input,
  1679. const TensorLayout& scale, const TensorLayout& zero_point,
  1680. const TensorLayout& grad_scale, const TensorLayout& grad_x,
  1681. const TensorLayout& grad_s) = 0;
  1682. protected:
  1683. void check_exec(
  1684. const TensorLayout& diff, const TensorLayout& input,
  1685. const TensorLayout& scale, const TensorLayout& zero_point,
  1686. const TensorLayout& grad_scale, const TensorLayout& grad_x,
  1687. const TensorLayout& grad_s, size_t workspace_in_bytes);
  1688. };
  1689. class LayerNormBase : public OperatorBase {
  1690. DEF_OPR_IMPL_CTOR(LayerNormBase, OperatorBase);
  1691. DEF_OPR_PARAM(LayerNorm);
  1692. protected:
  1693. void deduce_layout_fwd(
  1694. const TensorLayout& data, const TensorLayout& weight,
  1695. const TensorLayout& bias, TensorLayout& dst, TensorLayout& mean,
  1696. TensorLayout& rstd);
  1697. void check_layout_fwd(
  1698. const TensorLayout& data, const TensorLayout& weight,
  1699. const TensorLayout& bias, const TensorLayout& dst, const TensorLayout& mean,
  1700. const TensorLayout& rstd);
  1701. };
  1702. class LayerNormForward : public LayerNormBase {
  1703. DEF_OPR_IMPL(LayerNormForward, LayerNormBase, 3, 3);
  1704. public:
  1705. virtual void exec(
  1706. _megdnn_tensor_in data, _megdnn_tensor_in weight, _megdnn_tensor_in bias,
  1707. _megdnn_tensor_out dst, _megdnn_tensor_out mean, _megdnn_tensor_out rstd,
  1708. _megdnn_workspace workspace) = 0;
  1709. void deduce_layout(
  1710. const TensorLayout& data, const TensorLayout& weight,
  1711. const TensorLayout& bias, TensorLayout& dst, TensorLayout& mean,
  1712. TensorLayout& rstd);
  1713. virtual size_t get_workspace_in_bytes(
  1714. const TensorLayout& data, const TensorLayout& weight,
  1715. const TensorLayout& bias, const TensorLayout& dst, const TensorLayout& mean,
  1716. const TensorLayout& rstd) = 0;
  1717. protected:
  1718. void check_exec(
  1719. const TensorLayout& data, const TensorLayout& weight,
  1720. const TensorLayout& bias, const TensorLayout& dst, const TensorLayout& mean,
  1721. const TensorLayout& rstd, size_t workspace_in_bytes);
  1722. };
  1723. using LayerNorm = LayerNormForward;
  1724. class LayerNormBackward : public LayerNormBase {
  1725. DEF_OPR_IMPL(LayerNormBackward, LayerNormBase, 5, 3);
  1726. public:
  1727. virtual void exec(
  1728. _megdnn_tensor_in diff, _megdnn_tensor_in data, _megdnn_tensor_in weight,
  1729. _megdnn_tensor_in mean, _megdnn_tensor_in rstd, _megdnn_tensor_out ddata,
  1730. _megdnn_tensor_out dweight, _megdnn_tensor_out dbias,
  1731. _megdnn_workspace workspace) = 0;
  1732. void deduce_layout(
  1733. const TensorLayout& diff, const TensorLayout& data,
  1734. const TensorLayout& weight, const TensorLayout& mean,
  1735. const TensorLayout& rstd, TensorLayout& ddata, TensorLayout& dweight,
  1736. TensorLayout& dbias);
  1737. virtual size_t get_workspace_in_bytes(
  1738. const TensorLayout& diff, const TensorLayout& data,
  1739. const TensorLayout& weight, const TensorLayout& mean,
  1740. const TensorLayout& rstd, const TensorLayout& ddata,
  1741. const TensorLayout& dweight, const TensorLayout& dbias) = 0;
  1742. protected:
  1743. void check_exec(
  1744. const TensorLayout& diff, const TensorLayout& data,
  1745. const TensorLayout& weight, const TensorLayout& mean,
  1746. const TensorLayout& rstd, const TensorLayout& ddata,
  1747. const TensorLayout& dweight, const TensorLayout& dbias,
  1748. size_t workspace_in_bytes);
  1749. };
  1750. class DropoutBase : public OperatorBase {
  1751. DEF_OPR_IMPL_CTOR(DropoutBase, OperatorBase);
  1752. DEF_OPR_PARAM(Dropout);
  1753. };
  1754. class DropoutForward : public DropoutBase {
  1755. DEF_OPR_IMPL(DropoutForward, DropoutBase, 1, 2);
  1756. public:
  1757. void deduce_layout(const TensorLayout& inp, TensorLayout& oup, TensorLayout& mask);
  1758. virtual void exec(
  1759. _megdnn_tensor_in inp, _megdnn_tensor_out oup, _megdnn_tensor_out mask,
  1760. _megdnn_workspace workspace) = 0;
  1761. virtual size_t get_workspace_in_bytes(
  1762. const TensorLayout& inp, const TensorLayout& oup,
  1763. const TensorLayout& mask) = 0;
  1764. virtual size_t get_mask_size_in_bytes(const TensorLayout& inp) = 0;
  1765. protected:
  1766. void check_exec(
  1767. const TensorLayout& inp, const TensorLayout& oup, const TensorLayout& mask,
  1768. size_t workspace_in_bytes);
  1769. };
  1770. using Dropout = DropoutForward;
  1771. class DropoutBackward : public DropoutBase {
  1772. DEF_OPR_IMPL(DropoutBackward, DropoutBase, 2, 1);
  1773. public:
  1774. void deduce_layout(
  1775. const TensorLayout& doup, const TensorLayout& mask, TensorLayout& dinp);
  1776. virtual void exec(
  1777. _megdnn_tensor_in doup, _megdnn_tensor_in mask, _megdnn_tensor_out dinp,
  1778. _megdnn_workspace workspace) = 0;
  1779. virtual size_t get_workspace_in_bytes(
  1780. const TensorLayout& doup, const TensorLayout& mask,
  1781. const TensorLayout& dinp) = 0;
  1782. protected:
  1783. void check_exec(
  1784. const TensorLayout& doup, const TensorLayout& mask,
  1785. const TensorLayout& dinp, size_t workspace_in_bytes);
  1786. };
  1787. class SoftmaxBase : public OperatorBase {
  1788. DEF_OPR_IMPL_CTOR(SoftmaxBase, OperatorBase);
  1789. DEF_OPR_PARAM(Softmax);
  1790. protected:
  1791. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1792. void check_layout_fwd(const TensorLayout& input, const TensorLayout& output);
  1793. };
  1794. class SoftmaxForward : public SoftmaxBase {
  1795. DEF_OPR_IMPL(SoftmaxForward, SoftmaxBase, 1, 1);
  1796. public:
  1797. /**
  1798. * \param[in] input input tensor
  1799. * \param[out] output output tensor
  1800. */
  1801. virtual void exec(
  1802. _megdnn_tensor_in input, _megdnn_tensor_out output,
  1803. _megdnn_workspace workspace) = 0;
  1804. void deduce_layout(const TensorLayout& input, TensorLayout& output);
  1805. virtual size_t get_workspace_in_bytes(
  1806. const TensorLayout& input, const TensorLayout& output) = 0;
  1807. protected:
  1808. void check_exec(
  1809. const TensorLayout& input, const TensorLayout& output,
  1810. size_t workspace_in_bytes);
  1811. };
  1812. using Softmax = SoftmaxForward;
  1813. class SoftmaxBackward : public SoftmaxBase {
  1814. DEF_OPR_IMPL(SoftmaxBackward, SoftmaxBase, 2, 1);
  1815. public:
  1816. virtual void exec(
  1817. _megdnn_tensor_in input, _megdnn_tensor_in diff, _megdnn_tensor_out grad_x,
  1818. _megdnn_workspace workspace) = 0;
  1819. virtual size_t get_workspace_in_bytes(
  1820. const TensorLayout& input, const TensorLayout& diff,
  1821. const TensorLayout& grad_x) = 0;
  1822. protected:
  1823. void check_exec(
  1824. const TensorLayout& input, const TensorLayout& diff,
  1825. const TensorLayout& grad_x, size_t workspace_in_bytes);
  1826. };
  1827. class RNNCellForward : public OperatorBase {
  1828. DEF_OPR_PARAM(RNNCell);
  1829. DEF_OPR_IMPL(RNNCellForward, OperatorBase, 6, 1);
  1830. public:
  1831. virtual void exec(
  1832. _megdnn_tensor_in input, _megdnn_tensor_in weight_ih,
  1833. _megdnn_tensor_in bias_ih, _megdnn_tensor_in hx,
  1834. _megdnn_tensor_in weight_hh, _megdnn_tensor_in bias_hh,
  1835. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1836. void deduce_layout(
  1837. const TensorLayout& input, const TensorLayout& weight_ih,
  1838. const TensorLayout& bias_ih, const TensorLayout& hx,
  1839. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1840. TensorLayout& dst);
  1841. virtual size_t get_workspace_in_bytes(
  1842. const TensorLayout& input, const TensorLayout& weight_ih,
  1843. const TensorLayout& bias_ih, const TensorLayout& hx,
  1844. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1845. const TensorLayout& dst) = 0;
  1846. protected:
  1847. void check_exec(
  1848. const TensorLayout& input, const TensorLayout& weight_ih,
  1849. const TensorLayout& bias_ih, const TensorLayout& hx,
  1850. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1851. const TensorLayout& dst, size_t workspace_in_bytes);
  1852. };
  1853. using RNNCell = RNNCellForward;
  1854. class LSTMCellForward : public OperatorBase {
  1855. // DEF_OPR_PARAM(LSTMCell);
  1856. DEF_OPR_PARAM(Empty);
  1857. DEF_OPR_IMPL(LSTMCellForward, OperatorBase, 7, 3);
  1858. public:
  1859. virtual void exec(
  1860. _megdnn_tensor_in input, _megdnn_tensor_in weight_ih,
  1861. _megdnn_tensor_in bias_ih, _megdnn_tensor_in hx,
  1862. _megdnn_tensor_in weight_hh, _megdnn_tensor_in bias_hh,
  1863. _megdnn_tensor_in cx, _megdnn_tensor_out h_new, _megdnn_tensor_out c_new,
  1864. _megdnn_tensor_out gates, _megdnn_workspace workspace) = 0;
  1865. void deduce_layout(
  1866. const TensorLayout& input, const TensorLayout& weight_ih,
  1867. const TensorLayout& bias_ih, const TensorLayout& hx,
  1868. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1869. const TensorLayout& cx, TensorLayout& h_new, TensorLayout& c_new,
  1870. TensorLayout& gates);
  1871. virtual size_t get_workspace_in_bytes(
  1872. const TensorLayout& input, const TensorLayout& weight_ih,
  1873. const TensorLayout& bias_ih, const TensorLayout& hx,
  1874. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1875. const TensorLayout& cx, const TensorLayout& h_new,
  1876. const TensorLayout& c_new, const TensorLayout& gates) = 0;
  1877. protected:
  1878. void check_exec(
  1879. const TensorLayout& input, const TensorLayout& weight_ih,
  1880. const TensorLayout& bias_ih, const TensorLayout& hx,
  1881. const TensorLayout& weight_hh, const TensorLayout& bias_hh,
  1882. const TensorLayout& cx, const TensorLayout& h_new,
  1883. const TensorLayout& c_new, const TensorLayout& gates,
  1884. size_t workspace_in_bytes);
  1885. };
  1886. using LSTMCell = LSTMCellForward;
  1887. class RNNForward : public OperatorBase {
  1888. DEF_OPR_PARAM(RNN);
  1889. DEF_OPR_IMPL(RNNForward, OperatorBase, 3, 3);
  1890. public:
  1891. virtual void exec(
  1892. _megdnn_tensor_in input, _megdnn_tensor_in hx,
  1893. _megdnn_tensor_in flatten_weights, _megdnn_tensor_out output,
  1894. _megdnn_tensor_out hy, _megdnn_tensor_out reserve_space,
  1895. _megdnn_workspace workspace) = 0;
  1896. void deduce_layout(
  1897. const TensorLayout& input, const TensorLayout& hx,
  1898. const TensorLayout& flatten_weights, TensorLayout& output, TensorLayout& hy,
  1899. TensorLayout& reserve_space);
  1900. virtual size_t get_workspace_in_bytes(
  1901. const TensorLayout& input, const TensorLayout& hx,
  1902. const TensorLayout& flatten_weights, const TensorLayout& output,
  1903. const TensorLayout& hy, const TensorLayout& reserve_space) = 0;
  1904. virtual size_t get_reserve_size_in_bytes(const TensorLayout& input) = 0;
  1905. protected:
  1906. void check_exec(
  1907. const TensorLayout& input, const TensorLayout& hx,
  1908. const TensorLayout& flatten_weights, const TensorLayout& output,
  1909. const TensorLayout& hy, const TensorLayout& reserve_space,
  1910. size_t workspace_in_bytes);
  1911. };
  1912. using RNN = RNNForward;
  1913. class RNNBackward : public OperatorBase {
  1914. DEF_OPR_PARAM(RNN);
  1915. DEF_OPR_IMPL(RNNBackward, OperatorBase, 7, 3);
  1916. public:
  1917. virtual void exec(
  1918. _megdnn_tensor_in x, _megdnn_tensor_in y, _megdnn_tensor_in hx,
  1919. _megdnn_tensor_in dy, _megdnn_tensor_in dhy,
  1920. _megdnn_tensor_in flatten_weights, _megdnn_tensor_in reserve_space,
  1921. _megdnn_tensor_out dx, _megdnn_tensor_out dhx, _megdnn_tensor_out dw,
  1922. _megdnn_workspace workspace) = 0;
  1923. void deduce_layout(
  1924. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1925. const TensorLayout& dy, const TensorLayout& dhy,
  1926. const TensorLayout& flatten_weights, const TensorLayout& reserve_space,
  1927. TensorLayout& dx, TensorLayout& dhx, TensorLayout& dw);
  1928. virtual size_t get_workspace_in_bytes(
  1929. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1930. const TensorLayout& dy, const TensorLayout& dhy,
  1931. const TensorLayout& flatten_weights, const TensorLayout& reserve_space,
  1932. const TensorLayout& dx, const TensorLayout& dhx,
  1933. const TensorLayout& dw) = 0;
  1934. protected:
  1935. void check_exec(
  1936. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1937. const TensorLayout& dy, const TensorLayout& dhy,
  1938. const TensorLayout& flatten_weights, const TensorLayout& reserve_space,
  1939. const TensorLayout& dx, const TensorLayout& dhx, const TensorLayout& dw,
  1940. size_t workspace_in_bytes);
  1941. };
  1942. class LSTMForward : public OperatorBase {
  1943. DEF_OPR_PARAM(LSTM);
  1944. DEF_OPR_IMPL(LSTMForward, OperatorBase, 4, 4);
  1945. public:
  1946. virtual void exec(
  1947. _megdnn_tensor_in input, _megdnn_tensor_in hx, _megdnn_tensor_in cx,
  1948. _megdnn_tensor_in flatten_weights, _megdnn_tensor_out output,
  1949. _megdnn_tensor_out hy, _megdnn_tensor_out cy,
  1950. _megdnn_tensor_out reserve_space, _megdnn_workspace workspace) = 0;
  1951. void deduce_layout(
  1952. const TensorLayout& input, const TensorLayout& hx, const TensorLayout& cx,
  1953. const TensorLayout& flatten_weights, TensorLayout& output, TensorLayout& hy,
  1954. TensorLayout& cy, TensorLayout& reserve_space);
  1955. virtual size_t get_workspace_in_bytes(
  1956. const TensorLayout& input, const TensorLayout& hx, const TensorLayout& cx,
  1957. const TensorLayout& flatten_weights, const TensorLayout& output,
  1958. const TensorLayout& hy, const TensorLayout& cy,
  1959. const TensorLayout& reserve_space) = 0;
  1960. virtual size_t get_reserve_size_in_bytes(const TensorLayout& input) = 0;
  1961. protected:
  1962. void check_exec(
  1963. const TensorLayout& input, const TensorLayout& hx, const TensorLayout& cx,
  1964. const TensorLayout& flatten_weights, const TensorLayout& output,
  1965. const TensorLayout& hy, const TensorLayout& cy,
  1966. const TensorLayout& reserve_space, size_t workspace_in_bytes);
  1967. };
  1968. using LSTM = LSTMForward;
  1969. class LSTMBackward : public OperatorBase {
  1970. DEF_OPR_PARAM(LSTM);
  1971. DEF_OPR_IMPL(LSTMBackward, OperatorBase, 9, 4);
  1972. public:
  1973. virtual void exec(
  1974. _megdnn_tensor_in x, _megdnn_tensor_in y, _megdnn_tensor_in hx,
  1975. _megdnn_tensor_in cx, _megdnn_tensor_in dy, _megdnn_tensor_in dhy,
  1976. _megdnn_tensor_in dcy, _megdnn_tensor_in flatten_weights,
  1977. _megdnn_tensor_in reserve_space, _megdnn_tensor_out dx,
  1978. _megdnn_tensor_out dhx, _megdnn_tensor_out dcx, _megdnn_tensor_out dw,
  1979. _megdnn_workspace workspace) = 0;
  1980. void deduce_layout(
  1981. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1982. const TensorLayout& cx, const TensorLayout& dy, const TensorLayout& dhy,
  1983. const TensorLayout& dcy, const TensorLayout& flatten_weights,
  1984. const TensorLayout& reserve_space, TensorLayout& dx, TensorLayout& dhx,
  1985. TensorLayout& dcx, TensorLayout& dw);
  1986. virtual size_t get_workspace_in_bytes(
  1987. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1988. const TensorLayout& cx, const TensorLayout& dy, const TensorLayout& dhy,
  1989. const TensorLayout& dcy, const TensorLayout& flatten_weights,
  1990. const TensorLayout& reserve_space, const TensorLayout& dx,
  1991. const TensorLayout& dhx, const TensorLayout& dcx,
  1992. const TensorLayout& dw) = 0;
  1993. protected:
  1994. void check_exec(
  1995. const TensorLayout& x, const TensorLayout& y, const TensorLayout& hx,
  1996. const TensorLayout& cx, const TensorLayout& dy, const TensorLayout& dhy,
  1997. const TensorLayout& dcy, const TensorLayout& flatten_weights,
  1998. const TensorLayout& reserve_space, const TensorLayout& dx,
  1999. const TensorLayout& dhx, const TensorLayout& dcx, const TensorLayout& dw,
  2000. size_t workspace_in_bytes);
  2001. };
  2002. } // namespace megdnn
  2003. #include "megdnn/internal/opr_header_epilogue.h"
  2004. // vim: syntax=cpp.doxygen