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

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