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

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