You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

nn.h 88 kB

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