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

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台