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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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. protected:
  246. CanonizedFilterMeta check_exec(
  247. const TensorLayout& src, const TensorLayout& filter,
  248. const TensorLayout& dst, size_t workspace_in_bytes,
  249. const PreprocessedFilter* preprocessed_filter);
  250. };
  251. using Convolution = ConvolutionForward;
  252. /**
  253. * \brief ConvolutionBackwardData operator.
  254. *
  255. * Calculating the gradient wrt. convolution input data.
  256. */
  257. class ConvolutionBackwardData
  258. : public ConvolutionBase<param::Convolution>,
  259. public detail::MultiAlgoOpr<ConvolutionBackwardData, 3> {
  260. DEF_OPR_IMPL(ConvolutionBackwardData, ConvolutionBase, 2, 1);
  261. public:
  262. /**
  263. * \param[in] filter (oc, ic, fh, fw)
  264. * \param[in] diff (n, oc, oh, ow)
  265. * \param[out] grad (n, ic, ih, iw)
  266. */
  267. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  268. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  269. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  270. const TensorLayout& diff,
  271. const TensorLayout& grad) = 0;
  272. void deduce_dtype(DType filter, DType diff, DType& grad);
  273. void deduce_layout(const TensorLayout& filter, const TensorLayout& diff,
  274. TensorLayout& grad);
  275. protected:
  276. CanonizedFilterMeta check_exec(const TensorLayout& filter,
  277. const TensorLayout& diff,
  278. const TensorLayout& grad,
  279. size_t workspace_in_bytes);
  280. };
  281. /**
  282. * \brief ConvolutionBackwardFilter operator.
  283. *
  284. * Calculating the gradient wrt. convolution filter.
  285. */
  286. class ConvolutionBackwardFilter
  287. : public ConvolutionBase<param::Convolution>,
  288. public detail::MultiAlgoOpr<ConvolutionBackwardFilter, 3> {
  289. DEF_OPR_IMPL(ConvolutionBackwardFilter, ConvolutionBase, 2, 1);
  290. public:
  291. /**
  292. * \param[in] src (n, ic, ih, iw)
  293. * \param[in] diff (n, oc, oh, ow)
  294. * \param[out] grad (oc, ic, fh, fw)
  295. */
  296. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  297. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  298. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  299. const TensorLayout& diff,
  300. const TensorLayout& grad) = 0;
  301. protected:
  302. CanonizedFilterMeta check_exec(const TensorLayout& src,
  303. const TensorLayout& diff,
  304. const TensorLayout& grad,
  305. size_t workspace_in_bytes);
  306. };
  307. /**
  308. * \brief ConvolutionBias operator
  309. */
  310. class ConvBiasForward : public ConvolutionBase<param::ConvBias>,
  311. public detail::MultiAlgoOpr<ConvBiasForward, 5> {
  312. DEF_OPR_IMPL(ConvBiasForward, ConvolutionBase, 4, 1);
  313. public:
  314. /**
  315. * \param[in] src (n, ic, ih, iw) or (n, ih, iw, ic)
  316. * \param[in] filter (oc, ic, fh, fw) or (oc, fh, fw, ic) or (oc/4, fh, fw,
  317. * 4 * ic)
  318. * \param[in] bias (1, oc, 1, 1)
  319. * \param[in] z same as dst
  320. * \param[in] preprocessed_filter if weight no preprocessed it will be
  321. * nullptr, else the preprocessed weights store in the tensors of
  322. * preprocessed_filter.
  323. * \param[in] workspace if weight no preprocessed
  324. * (preprocessed_filter == nullptr), The size of the workspace satisfies the
  325. * situation that weights is not processed, other wise the size of workspace
  326. * satisfies the situation that weights is preprocessed
  327. * \param[out] dst (n, oc, oh, ow) or (n, oh, ow, oc)
  328. *
  329. * \note if the format is NCHW_WINOGRAD, the filter layout is (alphah,
  330. * alphaw, oc, ic)
  331. */
  332. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  333. _megdnn_tensor_in bias, _megdnn_tensor_in z,
  334. _megdnn_tensor_out dst,
  335. const PreprocessedFilter* preprocessed_filter,
  336. _megdnn_workspace workspace) = 0;
  337. /**
  338. * \brief execute weight preprocessing, read weights form filter and bias,
  339. * write to preprocessed_filter after preprocessed.
  340. *
  341. * \praram[in] workspace the needed tmp workspace when exec_preprocess
  342. * running, the size is got by get_preprocess_workspace_in_bytes
  343. */
  344. virtual void exec_preprocess(const TensorLayout& src_layout,
  345. _megdnn_tensor_in filter,
  346. _megdnn_tensor_in bias,
  347. const TensorLayout& z_layout,
  348. const TensorLayout& dst_layout,
  349. PreprocessedFilter* preprocessed_filter,
  350. _megdnn_workspace workspace) = 0;
  351. void deduce_dtype(DType src, DType filter, DType bias, DType z, DType& dst);
  352. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  353. const TensorLayout& bias, const TensorLayout& z,
  354. TensorLayout& dst);
  355. /**
  356. * \brief query the workspace needed when executing the opr, if the weights
  357. * are preprocessed the preprocessed_filter will not be nullptr, else it
  358. * will be nullptr, the workspace size maybe different whether weights are
  359. * preprocessed
  360. *
  361. * \return the size of workspace needed when executing
  362. */
  363. virtual size_t get_workspace_in_bytes(
  364. const TensorLayout& src, const TensorLayout& filter,
  365. const TensorLayout& bias, const TensorLayout& z,
  366. const TensorLayout& dst,
  367. const PreprocessedFilter* preprocessed_filter) = 0;
  368. /**
  369. * \brief query the workspace needed when pre-processing the weights,
  370. * according to the return size, a _megdnn_workspace will be created and
  371. * passed through exec_preprocess
  372. *
  373. * \return the size of workspace needed when pre-processing
  374. */
  375. virtual size_t get_preprocess_workspace_in_bytes(
  376. const TensorLayout& src, const TensorLayout& filter,
  377. const TensorLayout& bias, const TensorLayout& z,
  378. const TensorLayout& dst) = 0;
  379. /**
  380. * \brief deduce the pre-processed filter layouts according to the src,
  381. * filter and dst layout, which may contain multi layouts when the weights
  382. * is not one
  383. *
  384. * \return SmallVector<TensorLayout> Derive the layouts of weight
  385. * preprocessing, return empty if preprocessing is not needed.
  386. */
  387. virtual SmallVector<TensorLayout> deduce_preprocessed_filter_layout(
  388. const TensorLayout& src, const TensorLayout& filter,
  389. const TensorLayout& bias, const TensorLayout& z,
  390. const TensorLayout& dst) = 0;
  391. enum class BiasMode : uint32_t {
  392. NO_BIAS = 0, //!< no bias
  393. BROADCAST_CHANNEL_BIAS, //!< broadcast channel bias, [1, c, 1, 1]
  394. BIAS //!< [N, C, H, W]
  395. };
  396. //! param for winograd algos.
  397. struct WinogradParam {
  398. uint32_t channel_block_size;
  399. uint32_t output_block_size;
  400. uint32_t tile_size;
  401. bool operator==(const WinogradParam& rhs) const {
  402. return channel_block_size == rhs.channel_block_size &&
  403. output_block_size == rhs.output_block_size &&
  404. tile_size == rhs.tile_size;
  405. }
  406. std::string to_string() const;
  407. };
  408. static constexpr WinogradParam INVALID_WINOGRAD_PARAM = {0, 0, 0};
  409. struct DirectParam {
  410. std::string to_string() const { return ""; }
  411. };
  412. struct MatmulParam {
  413. std::string to_string() const { return ""; }
  414. };
  415. struct DefaultParam {
  416. std::string to_string() const { return ""; }
  417. };
  418. //! get algo name, the format is ParamTrait<T>::category:base:p.to_string()
  419. //! \warning: base must not contain :.
  420. template <typename T>
  421. static std::string algo_name(
  422. const std::string& base, const T& p,
  423. param::ConvBias::Format format = param::ConvBias::Format::NCHW);
  424. /*!
  425. * \brief parse algo_name and get WinogradParam from algo name.
  426. *
  427. * \param algo name string
  428. * \return WinogradParam parsed from algo name, use pattern
  429. * winograd:base:m:tile_size.
  430. *
  431. * \warning: INVALID_WINOGRAD_PARAM returns if the algo_name is not matched.
  432. */
  433. static WinogradParam parse_winograd_name(const std::string& algo_name);
  434. /**
  435. * @brief find if there is nchw_nchwxx conv kernel optimized for argment,
  436. * nchw44 used for arm, nchw88 used for x86
  437. *
  438. * @param src_dtype conv feature map data type
  439. * @param filter_dtype conv filter or weight data type
  440. * @param dst_dtype output data type
  441. * @param fm filter meta param
  442. * @param bias_mode bias mode, no_bias or broadcast or bias
  443. * @param nonline_mode identity or relu or h_swish or sigmoid
  444. * @return true, found a kernel
  445. * @return false, can`t found any kernel
  446. */
  447. static bool is_nchw_nchwxx_optimized(
  448. const DTypeEnum src_dtype, const DTypeEnum filter_dtype,
  449. const DTypeEnum dst_dtype,
  450. const ConvolutionBase<param::Convolution>::CanonizedFilterMeta& fm,
  451. const ConvBiasForward::BiasMode bias_mode,
  452. const param::ConvBias::NonlineMode nonline_mode);
  453. protected:
  454. CanonizedFilterMeta check_exec(
  455. const TensorLayout& src, const TensorLayout& filter,
  456. const TensorLayout& bias, const TensorLayout& z,
  457. const TensorLayout& dst, size_t workspace_in_bytes,
  458. const PreprocessedFilter* preprocessed_filter);
  459. CanonizedFilterMeta check_exec_allow_noncontiguous(
  460. const TensorLayout& src, const TensorLayout& filter,
  461. const TensorLayout& bias, const TensorLayout& z,
  462. const TensorLayout& dst, size_t workspace_in_bytes,
  463. const PreprocessedFilter* preprocessed_filter);
  464. };
  465. using ConvBias = ConvBiasForward;
  466. /**
  467. * \brief base class for Conv - Nonline - Pooling
  468. */
  469. class ConvPoolingBase : public OperatorBase {
  470. DEF_OPR_IMPL_CTOR(ConvPoolingBase, OperatorBase);
  471. /**
  472. * \ Param::Method: Two methods to fetch the input data.
  473. * Default methods is WITH_TEXTURE_OBJ.
  474. * If you want to use WITH_SHARED_MEM mode,
  475. * please make sure that the size of
  476. * [ all of the fliter kernels + a channel
  477. * of input data + a channel of output data]
  478. * should be no large than 38KB.
  479. * And the pooling mode should not be "MAX".
  480. */
  481. DEF_OPR_PARAM(ConvPooling);
  482. protected:
  483. virtual void deduce_layout(const TensorLayout& src,
  484. const TensorLayout& filter,
  485. const TensorLayout& bias, TensorLayout& dst) = 0;
  486. virtual void check_layout(const TensorLayout& src,
  487. const TensorLayout& filter,
  488. const TensorLayout& bias, TensorLayout& dst,
  489. size_t workspace_limit_in_bytes) = 0;
  490. };
  491. class ConvPoolingForward : public ConvPoolingBase {
  492. DEF_OPR_IMPL(ConvPoolingForward, ConvPoolingBase, 2, 1);
  493. public:
  494. /**
  495. * \param[in] src input tensor
  496. * \param[out] dst output tensor
  497. */
  498. virtual void exec(const _megdnn_in TensorND src,
  499. const _megdnn_in TensorND filter,
  500. const _megdnn_in TensorND bias, _megdnn_out TensorND dst,
  501. _megdnn_out Workspace workspace) = 0;
  502. virtual void deduce_layout(const TensorLayout& src,
  503. const TensorLayout& filter,
  504. const TensorLayout& bias, TensorLayout& dst) = 0;
  505. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  506. const TensorLayout& filter,
  507. const TensorLayout& bias,
  508. const TensorLayout& dst) = 0;
  509. protected:
  510. virtual void check_layout(const TensorLayout& src,
  511. const TensorLayout& filter,
  512. const TensorLayout& bias, TensorLayout& dst,
  513. size_t workspace_limit_in_bytes) = 0;
  514. };
  515. using ConvPooling = ConvPoolingForward;
  516. class GroupLocalBase : public OperatorBase {
  517. DEF_OPR_IMPL_CTOR(GroupLocalBase, OperatorBase);
  518. DEF_OPR_PARAM(Convolution);
  519. public:
  520. using Mode = Param::Mode;
  521. protected:
  522. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  523. TensorLayout& dst);
  524. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  525. const TensorLayout& dst);
  526. };
  527. class GroupLocalForward : public GroupLocalBase {
  528. DEF_OPR_IMPL(GroupLocalForward, GroupLocalBase, 2, 1);
  529. public:
  530. /**
  531. * \param[in] src (N, IC, IH, IW)
  532. * \param[in] filter (G, OH, OW, IC/G, FH, FW, OC/G)
  533. * \param[out] dst (N, OC, OH, OW)
  534. **/
  535. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  536. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  537. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  538. TensorLayout& dst) {
  539. deduce_layout_fwd(src, filter, dst);
  540. }
  541. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  542. const TensorLayout& filter,
  543. const TensorLayout& dst) = 0;
  544. protected:
  545. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  546. const TensorLayout& dst, size_t workspace_in_bytes);
  547. };
  548. using GroupLocal = GroupLocalForward;
  549. class GroupLocalBackwardData : public GroupLocalBase {
  550. DEF_OPR_IMPL(GroupLocalBackwardData, GroupLocalBase, 2, 1);
  551. public:
  552. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  553. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  554. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  555. const TensorLayout& diff,
  556. const TensorLayout& grad) = 0;
  557. protected:
  558. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  559. const TensorLayout& grad, size_t workspace_in_bytes);
  560. };
  561. class GroupLocalBackwardFilter : public GroupLocalBase {
  562. DEF_OPR_IMPL(GroupLocalBackwardFilter, GroupLocalBase, 2, 1);
  563. public:
  564. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  565. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  566. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  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 Images2NeibsBase : public OperatorBase {
  574. DEF_OPR_IMPL_CTOR(Images2NeibsBase, OperatorBase);
  575. DEF_OPR_PARAM(Images2Neibs);
  576. protected:
  577. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  578. void check_layout_fwd(const TensorLayout& filter, const TensorLayout& dst);
  579. };
  580. class Images2NeibsForward : public Images2NeibsBase {
  581. DEF_OPR_IMPL(Images2NeibsForward, Images2NeibsBase, 1, 1);
  582. public:
  583. /**
  584. * \param[in] src (N, C, IH, IW)
  585. * \param[out] dst (N, C, OH, OW, window_h, window_w)
  586. *
  587. * \see
  588. * http://deeplearning.net/software/theano/library/tensor/nnet/neighbours.html
  589. *
  590. * \f$ dst_{n, c, oh, ow, wh, ww} = src_{n, c, ih+wh, iw+fw}\f$,
  591. * where \f$ ih=-pad_h+oh*stride_h, iw=-pad_w+ow*stride_w\f$.
  592. */
  593. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  594. _megdnn_workspace workspace) = 0;
  595. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  596. const TensorLayout& dst) = 0;
  597. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  598. protected:
  599. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  600. size_t workspace_in_bytes);
  601. };
  602. using Images2Neibs = Images2NeibsForward;
  603. class Images2NeibsBackward : public Images2NeibsBase {
  604. DEF_OPR_IMPL(Images2NeibsBackward, Images2NeibsBase, 1, 1);
  605. public:
  606. /**
  607. * \param[in] diff the backpropagated gradient wrt. dst
  608. * \param[out] grad the backpropagated gradient wrt. src
  609. */
  610. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_out grad,
  611. _megdnn_workspace workspace) = 0;
  612. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  613. const TensorLayout& grad) = 0;
  614. protected:
  615. void check_exec(const TensorLayout& diff, const TensorLayout& grad,
  616. size_t workspace_in_bytes);
  617. };
  618. /**
  619. * \brief base class for Pooling
  620. */
  621. class PoolingBase : public OperatorBase {
  622. DEF_OPR_IMPL_CTOR(PoolingBase, OperatorBase);
  623. DEF_OPR_PARAM(Pooling);
  624. public:
  625. using Mode = Param::Mode;
  626. protected:
  627. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  628. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  629. };
  630. class PoolingForward : public PoolingBase,
  631. public detail::MultiAlgoOpr<PoolingForward, 2> {
  632. DEF_OPR_IMPL(PoolingForward, PoolingBase, 1, 1);
  633. public:
  634. /**
  635. * \param[in] src input tensor
  636. * \param[out] dst output tensor
  637. */
  638. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  639. _megdnn_workspace workspace) = 0;
  640. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  641. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  642. const TensorLayout& dst) = 0;
  643. protected:
  644. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  645. size_t workspace_in_bytes);
  646. };
  647. using Pooling = PoolingForward;
  648. class PoolingBackward : public PoolingBase,
  649. public detail::MultiAlgoOpr<PoolingBackward, 4> {
  650. DEF_OPR_IMPL(PoolingBackward, PoolingBase, 3, 1);
  651. public:
  652. /**
  653. * \param[in] src the `src' parameter in PoolingForward::exec
  654. * \param[in] dst the `dst' parameter in PoolingForward::exec
  655. * \param[in] diff the backpropagated gradient wrt. dst
  656. * \param[out] grad the backpropagated gradient wrt. src
  657. */
  658. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  659. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  660. _megdnn_workspace workspace) = 0;
  661. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  662. const TensorLayout& dst,
  663. const TensorLayout& diff,
  664. const TensorLayout& grad) = 0;
  665. protected:
  666. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  667. const TensorLayout& diff, const TensorLayout& grad,
  668. size_t workspace_in_bytes);
  669. };
  670. /**
  671. * \brief base class for AdaptivePooling
  672. */
  673. class AdaptivePoolingBase : public OperatorBase {
  674. DEF_OPR_IMPL_CTOR(AdaptivePoolingBase, OperatorBase);
  675. DEF_OPR_PARAM(AdaptivePooling);
  676. protected:
  677. param::Pooling deduce_pooling_param(const TensorLayout& src,
  678. const TensorLayout& dst);
  679. };
  680. class AdaptivePoolingForward : public AdaptivePoolingBase {
  681. DEF_OPR_IMPL(AdaptivePoolingForward, AdaptivePoolingBase, 1, 1);
  682. public:
  683. /**
  684. * \param[in] src input tensor
  685. * \param[out] dst output tensor
  686. */
  687. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  688. _megdnn_workspace workspace) = 0;
  689. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  690. const TensorLayout& dst) = 0;
  691. };
  692. using AdaptivePooling = AdaptivePoolingForward;
  693. class AdaptivePoolingBackward : public AdaptivePoolingBase {
  694. DEF_OPR_IMPL(AdaptivePoolingBackward, AdaptivePoolingBase, 3, 1);
  695. public:
  696. /**
  697. * \param[in] src the `src' parameter in AdaptivePoolingForward::exec
  698. * \param[in] dst the `dst' parameter in AdaptivePoolingForward::exec
  699. * \param[in] diff the backpropagated gradient wrt. dst
  700. * \param[out] grad the backpropagated gradient wrt. src
  701. */
  702. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  703. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  704. _megdnn_workspace workspace) = 0;
  705. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  706. const TensorLayout& dst,
  707. const TensorLayout& diff,
  708. const TensorLayout& grad) = 0;
  709. };
  710. /**
  711. * \brief base class for Local
  712. */
  713. class LocalBase : public OperatorBase {
  714. DEF_OPR_IMPL_CTOR(LocalBase, OperatorBase);
  715. DEF_OPR_PARAM(Convolution);
  716. public:
  717. using Mode = Param::Mode;
  718. protected:
  719. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  720. TensorLayout& dst);
  721. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  722. const TensorLayout& dst);
  723. };
  724. class LocalForward : public LocalBase {
  725. DEF_OPR_IMPL(LocalForward, LocalBase, 2, 1);
  726. public:
  727. /**
  728. * \param[in] src (n, ic, ih, iw)
  729. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  730. * \param[out] dst (n, oc, oh, ow)
  731. */
  732. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  733. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  734. /**
  735. * \brief Deducing output tensor layouts from input tensor layouts.
  736. *
  737. * Be aware that the first and second dimension of `filter' are ignored
  738. * when deducing `dst' layout.
  739. */
  740. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  741. TensorLayout& dst);
  742. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  743. const TensorLayout& filter,
  744. const TensorLayout& dst) = 0;
  745. protected:
  746. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  747. const TensorLayout& dst, size_t workspace_in_bytes);
  748. };
  749. using Local = LocalForward;
  750. class LocalBackwardData : public LocalBase {
  751. DEF_OPR_IMPL(LocalBackwardData, LocalBase, 2, 1);
  752. public:
  753. /**
  754. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  755. * \param[in] diff (n, oc, oh, ow)
  756. * \param[out] grad (n, ic, ih, iw)
  757. */
  758. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  759. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  760. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  761. const TensorLayout& diff,
  762. const TensorLayout& grad) = 0;
  763. protected:
  764. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  765. const TensorLayout& grad, size_t workspace_in_bytes);
  766. };
  767. class LocalBackwardFilter : public LocalBase {
  768. DEF_OPR_IMPL(LocalBackwardFilter, LocalBase, 2, 1);
  769. public:
  770. /**
  771. * \param[in] src (n, ic, ih, iw)
  772. * \param[in] diff (n, oc, oh, ow)
  773. * \param[out] grad (oh, ow, ic, fh, fw, oc)
  774. */
  775. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  776. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  777. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  778. const TensorLayout& diff,
  779. const TensorLayout& grad) = 0;
  780. protected:
  781. void check_exec(const TensorLayout& src, const TensorLayout& diff,
  782. const TensorLayout& grad, size_t workspace_in_bytes);
  783. };
  784. class BNBase : public OperatorBase {
  785. DEF_OPR_IMPL_CTOR(BNBase, OperatorBase);
  786. DEF_OPR_PARAM(BN);
  787. protected:
  788. void check_param();
  789. };
  790. class BNForward : public BNBase {
  791. DEF_OPR_IMPL(BNForward, BNBase, 6, 5);
  792. public:
  793. /**
  794. * \dst[i] = gemma
  795. * *(x[i]-estimatedMean[k])/sqrt(epsilon+estimatedVariance[k]) + beta \where
  796. * epsilon is a very small value to avoid a "divide by zero" error.
  797. * \param[in] src (n, c, h, w)
  798. * \param[out] dst (n, c, h, w)
  799. * \param[out] mean (see m_param.ParamDim) Global mean.
  800. * \param[out] variance (see m_param.ParamDim) Global variance.
  801. * \Param[out] batch_mean (see m_param.ParamDim)
  802. * Optionally cached intermediate mean from forward pass
  803. * \Param[out] batch_inv_variance (see m_param.ParamDim)
  804. * Optionally cached intermediate variance from forward pass
  805. * src and dst must have the same shape.
  806. * src and dst must be contiguous.
  807. */
  808. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in bn_scale,
  809. _megdnn_tensor_in bn_bias, _megdnn_tensor_inout mean,
  810. _megdnn_tensor_inout variance,
  811. _megdnn_tensor_out batch_mean,
  812. _megdnn_tensor_out batch_inv_variance,
  813. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  814. void deduce_layout(const TensorLayout& src, TensorLayout& bn_scale,
  815. TensorLayout& bn_bias, TensorLayout& mean,
  816. TensorLayout& variance, TensorLayout& batch_mean,
  817. TensorLayout& batch_inv_variance, TensorLayout& dst);
  818. virtual size_t get_workspace_in_bytes(
  819. const TensorLayout& src, const TensorLayout& bn_scale,
  820. const TensorLayout& bn_bias, const TensorLayout& mean,
  821. const TensorLayout& variance, const TensorLayout& batch_mean,
  822. const TensorLayout& batch_inv_variance,
  823. const TensorLayout& dst) = 0;
  824. protected:
  825. void check_exec(const TensorLayout& src, const TensorLayout& bn_scale,
  826. const TensorLayout& bn_bias, const TensorLayout& mean,
  827. const TensorLayout& variance,
  828. const TensorLayout& batch_mean,
  829. const TensorLayout& batch_inv_variance,
  830. const TensorLayout& dst, size_t workspace_in_bytes);
  831. };
  832. using BN = BNForward;
  833. class BNBackward : public BNBase {
  834. DEF_OPR_IMPL(BNBackward, BNBase, 5, 3);
  835. public:
  836. /**
  837. * \param[in] input data of forwarding propagate.
  838. * \param[in] dy the backpropagated gradient of y.
  839. * \param[out] dx the backpropagated gradient of x.
  840. * \param[out] d_bn_scale, the backpropagated gradient of bn_scale.
  841. * \param[out] d_bn_bias, the backpropagated gradient of bn_bias.
  842. * Optionally cached intermediate results from forward pass
  843. * \param[in] saved_batch_mean mean of the input batch.
  844. Calculated in the forwardpropagation.
  845. * \param[in] saved_batch_variance of the input batch.
  846. Calculated in the forwardpropagation.
  847. */
  848. virtual void exec(_megdnn_tensor_in x, _megdnn_tensor_in dy,
  849. _megdnn_tensor_in saved_batch_mean,
  850. _megdnn_tensor_in saved_batch_variance,
  851. _megdnn_tensor_in bn_scale, _megdnn_tensor_out d_bn_scale,
  852. _megdnn_tensor_out d_bn_bias, _megdnn_tensor_out dx,
  853. _megdnn_workspace workspace) = 0;
  854. virtual size_t get_workspace_in_bytes(
  855. const TensorLayout& x, const TensorLayout& dy,
  856. const TensorLayout& saved_batch_mean,
  857. const TensorLayout& saved_batch_variance,
  858. const TensorLayout& bn_scale, const TensorLayout& d_bn_scale,
  859. const TensorLayout& d_bn_bias, const TensorLayout& dx) = 0;
  860. protected:
  861. void check_exec(const TensorLayout& x, const TensorLayout& dy,
  862. const TensorLayout& saved_batch_mean,
  863. const TensorLayout& saved_batch_variance,
  864. const TensorLayout& bn_scale,
  865. const TensorLayout& d_bn_scale,
  866. const TensorLayout& d_bn_bias, const TensorLayout& dx,
  867. size_t workspace_in_bytes);
  868. };
  869. class LRNBase : public OperatorBase {
  870. DEF_OPR_IMPL_CTOR(LRNBase, OperatorBase);
  871. DEF_OPR_PARAM(LRN);
  872. protected:
  873. void check_param();
  874. };
  875. class LRNForward : public LRNBase {
  876. DEF_OPR_IMPL(LRNForward, LRNBase, 1, 1);
  877. public:
  878. /**
  879. * \see ImageNet Classification with Deep Convolutional Neural Networks
  880. * \param[in] src (n, c, h, w)
  881. * \param[out] dst (n, c, h, w)
  882. *
  883. * src and dst must have the same shape.
  884. * src and dst must be contiguous.
  885. */
  886. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  887. _megdnn_workspace workspace) = 0;
  888. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  889. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  890. const TensorLayout& dst) = 0;
  891. protected:
  892. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  893. size_t workspace_in_bytes);
  894. };
  895. using LRN = LRNForward;
  896. class LRNBackward : public LRNBase {
  897. DEF_OPR_IMPL(LRNBackward, LRNBase, 3, 1);
  898. public:
  899. /**
  900. * \param[in] src the `src' parameter in LRNForward::exec
  901. * \param[in] dst the `dst' parameter in LRNForward::exec
  902. * \param[in] diff the backpropagated gradient wrt. dst
  903. * \param[out] grad the backpropagated gradient wrt. src
  904. *
  905. * All tensors should be contiguous and of the same shape.
  906. */
  907. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  908. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  909. _megdnn_workspace workspace) = 0;
  910. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  911. const TensorLayout& dst,
  912. const TensorLayout& diff,
  913. const TensorLayout& grad) = 0;
  914. protected:
  915. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  916. const TensorLayout& diff, const TensorLayout& grad,
  917. size_t workspace_in_bytes);
  918. };
  919. class ROIPoolingBase : public OperatorBase {
  920. DEF_OPR_IMPL_CTOR(ROIPoolingBase, OperatorBase);
  921. DEF_OPR_PARAM(ROIPooling);
  922. protected:
  923. void check_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  924. const TensorLayout& dst, const TensorLayout& index);
  925. };
  926. class ROIPoolingForward : public ROIPoolingBase {
  927. DEF_OPR_IMPL(ROIPoolingForward, ROIPoolingBase, 2, 2);
  928. public:
  929. /**
  930. * \param[in] src (n, c, ih, iw)
  931. * \param[in] rois (m, 5)
  932. * \param[out] dst (m, c, oh, ow)
  933. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  934. *
  935. * The internal implementation is akin to
  936. * https://github.com/rbgirshick/caffe-fast-rcnn .d
  937. * Note that rois(, 0) denotes the input image index. We store it as
  938. * a float, but it should be an integer instead.
  939. *
  940. * index is a temporary tensor to facilitate its backward operator.
  941. * It is used to store argmax indicex in MAX mode, and it is not used
  942. * in AVERAGE mode.
  943. */
  944. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in rois,
  945. _megdnn_tensor_out dst, _megdnn_tensor_out index,
  946. _megdnn_workspace workspace) = 0;
  947. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  948. const TensorLayout& rois,
  949. const TensorLayout& dst,
  950. const TensorLayout& index) = 0;
  951. protected:
  952. void check_exec(const TensorLayout& src, const TensorLayout& rois,
  953. const TensorLayout& dst, const TensorLayout& index,
  954. size_t workspace_in_bytes);
  955. };
  956. using ROIPooling = ROIPoolingForward;
  957. class ROIPoolingBackward : public ROIPoolingBase {
  958. DEF_OPR_IMPL(ROIPoolingBackward, ROIPoolingBase, 4, 1);
  959. public:
  960. /**
  961. * \param[in] diff the backpropagated gradient wrt. dst
  962. * \param[in] src the `src' parameter in ROIPoolingForward::exec
  963. * \param[in] rois the `rois' parameter in ROIPoolingForward::exec
  964. * \param[in] index the `index' parameter in ROIPoolingForward::exec
  965. * \param[out] grad the backpropagated gradient wrt. src
  966. */
  967. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in src,
  968. _megdnn_tensor_in rois, _megdnn_tensor_in index,
  969. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  970. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  971. const TensorLayout& src,
  972. const TensorLayout& rois,
  973. const TensorLayout& index,
  974. const TensorLayout& grad) = 0;
  975. protected:
  976. void check_exec(const TensorLayout& diff, const TensorLayout& src,
  977. const TensorLayout& rois, const TensorLayout& index,
  978. const TensorLayout& grad, size_t workspace_in_bytes);
  979. };
  980. class Convolution3DBase : public OperatorBase {
  981. DEF_OPR_IMPL_CTOR(Convolution3DBase, OperatorBase);
  982. DEF_OPR_PARAM(Convolution3D);
  983. public:
  984. static constexpr size_t MAX_SPATIAL_DIM = 3;
  985. using Mode = Param::Mode;
  986. struct CanonizedFilterMeta {
  987. DTypeEnum dtype_enum;
  988. Param::Format format;
  989. uint32_t
  990. //! whether filter should be flipped (i.e. is CONVOLUTION)
  991. should_flip,
  992. group, //!< number of groups
  993. icpg, //!< input channels per group
  994. ocpg, //!< output channels per group
  995. spatial_ndim, stride[MAX_SPATIAL_DIM], padding[MAX_SPATIAL_DIM],
  996. //! spatial dim
  997. spatial[MAX_SPATIAL_DIM], dilation[MAX_SPATIAL_DIM],
  998. //! spatial dim with dilation applied
  999. dilated_spatial[MAX_SPATIAL_DIM];
  1000. } MEGDNN_PACKED;
  1001. protected:
  1002. CanonizedFilterMeta deduce_layout_fwd(const TensorLayout& src,
  1003. const TensorLayout& filter,
  1004. TensorLayout& dst) const;
  1005. CanonizedFilterMeta check_layout_fwd(const TensorLayout& src,
  1006. const TensorLayout& filter,
  1007. const TensorLayout& dst) const;
  1008. CanonizedFilterMeta make_canonized_filter_meta(
  1009. size_t src_ndim, const TensorLayout& filter) const;
  1010. };
  1011. class Convolution3DForward
  1012. : public Convolution3DBase,
  1013. public detail::MultiAlgoOpr<Convolution3DForward, 3> {
  1014. DEF_OPR_IMPL(Convolution3DForward, Convolution3DBase, 2, 1);
  1015. public:
  1016. /**
  1017. * \param[in] src (n, ic, id, ih, iw)
  1018. * \param[in] filter (oc, ic, fd, fh, fw)
  1019. * \param[out] dst (n, oc, od, oh, ow)
  1020. */
  1021. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1022. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1023. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1024. TensorLayout& dst);
  1025. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1026. const TensorLayout& filter,
  1027. const TensorLayout& dst) = 0;
  1028. protected:
  1029. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1030. const TensorLayout& filter,
  1031. const TensorLayout& dst,
  1032. size_t workspace_in_bytes);
  1033. };
  1034. using Convolution3D = Convolution3DForward;
  1035. class Convolution3DBackwardData
  1036. : public Convolution3DBase,
  1037. public detail::MultiAlgoOpr<Convolution3DBackwardData, 3> {
  1038. DEF_OPR_IMPL(Convolution3DBackwardData, Convolution3DBase, 2, 1);
  1039. public:
  1040. /**
  1041. * \param[in] filter (oc, ic, fd, fh, fw)
  1042. * \param[in] diff (n, oc, od, oh, ow)
  1043. * \param[out] grad (n, ic, id, ih, iw)
  1044. */
  1045. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  1046. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1047. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  1048. const TensorLayout& diff,
  1049. const TensorLayout& grad) = 0;
  1050. void deduce_layout(const TensorLayout& filter, const TensorLayout& diff,
  1051. TensorLayout& grad);
  1052. protected:
  1053. CanonizedFilterMeta check_exec(const TensorLayout& filter,
  1054. const TensorLayout& diff,
  1055. const TensorLayout& grad,
  1056. size_t workspace_in_bytes);
  1057. };
  1058. class Convolution3DBackwardFilter
  1059. : public Convolution3DBase,
  1060. public detail::MultiAlgoOpr<Convolution3DBackwardFilter, 3> {
  1061. DEF_OPR_IMPL(Convolution3DBackwardFilter, Convolution3DBase, 2, 1);
  1062. public:
  1063. /**
  1064. * \param[in] src (n, ic, id, ih, iw)
  1065. * \param[in] diff (n, oc, od, oh, ow)
  1066. * \param[out] grad (oc, ic, fd, fh, fw)
  1067. */
  1068. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  1069. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1070. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1071. const TensorLayout& diff,
  1072. const TensorLayout& grad) = 0;
  1073. protected:
  1074. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1075. const TensorLayout& diff,
  1076. const TensorLayout& grad,
  1077. size_t workspace_in_bytes);
  1078. };
  1079. class LocalShareBase : public OperatorBase {
  1080. DEF_OPR_IMPL_CTOR(LocalShareBase, OperatorBase);
  1081. DEF_OPR_PARAM(LocalShare);
  1082. protected:
  1083. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1084. TensorLayout& dst);
  1085. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1086. const TensorLayout& dst);
  1087. };
  1088. class LocalShareForward : public LocalShareBase,
  1089. public detail::MultiAlgoOpr<LocalShareForward, 3> {
  1090. DEF_OPR_IMPL(LocalShareForward, LocalShareBase, 2, 1);
  1091. public:
  1092. /**
  1093. * \param[in] src (N, IC, IH, IW)
  1094. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1095. * FH, FW, OC / G)
  1096. * \param[out] dst (N, OC, OH, OW)
  1097. */
  1098. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1099. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1100. /**
  1101. * \brief deduce layout of the ouput tensor
  1102. */
  1103. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1104. TensorLayout& dst);
  1105. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1106. const TensorLayout& filter,
  1107. const TensorLayout& dst) = 0;
  1108. protected:
  1109. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  1110. const TensorLayout& dst, size_t workspace_in_bytes);
  1111. };
  1112. using LocalShare = LocalShareForward;
  1113. class LocalShareBackwardData
  1114. : public LocalShareBase,
  1115. public detail::MultiAlgoOpr<LocalShareBackwardData, 3> {
  1116. DEF_OPR_IMPL(LocalShareBackwardData, LocalShareBase, 2, 1);
  1117. public:
  1118. /**
  1119. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1120. * FH, FW, OC / G)
  1121. * \param[in] diff (N, OC, OH, OW)
  1122. * \param[out] grad (N, IC, IH, IW)
  1123. */
  1124. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  1125. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1126. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  1127. const TensorLayout& diff,
  1128. const TensorLayout& grad) = 0;
  1129. void deduce_layout(const TensorLayout& filter, const TensorLayout& diff,
  1130. TensorLayout& grad);
  1131. protected:
  1132. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  1133. const TensorLayout& grad, size_t workspace_in_bytes);
  1134. };
  1135. class LocalShareBackwardFilter
  1136. : public LocalShareBase,
  1137. public detail::MultiAlgoOpr<LocalShareBackwardFilter, 3> {
  1138. DEF_OPR_IMPL(LocalShareBackwardFilter, LocalShareBase, 2, 1);
  1139. public:
  1140. /**
  1141. * \param[in] src (N, IC, IH, IW)
  1142. * \param[in] diff (N, OC, OH, OW)
  1143. * \param[out] grad (G, spatial_groups_h, spatial_groups_w, IC / G,
  1144. * FH, FW, OC / G)
  1145. */
  1146. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  1147. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1148. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1149. const TensorLayout& diff,
  1150. const TensorLayout& grad) = 0;
  1151. protected:
  1152. void check_exec(const TensorLayout& src, const TensorLayout& diff,
  1153. const TensorLayout& grad, size_t workspace_in_bytes);
  1154. };
  1155. class ROIAlignBase : public OperatorBase {
  1156. DEF_OPR_IMPL_CTOR(ROIAlignBase, OperatorBase);
  1157. DEF_OPR_PARAM(ROIAlign);
  1158. protected:
  1159. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  1160. TensorLayout& dst, TensorLayout& index);
  1161. void check_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  1162. const TensorLayout& dst, const TensorLayout& index);
  1163. };
  1164. class ROIAlignForward : public ROIAlignBase {
  1165. DEF_OPR_IMPL(ROIAlignForward, ROIAlignBase, 2, 2);
  1166. public:
  1167. /**
  1168. * \param[in] src (n, c, ih, iw)
  1169. * \param[in] rois (m, 5)
  1170. * \param[out] dst (m, c, oh, ow)
  1171. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  1172. *
  1173. * Note that rois(, 0) denotes the input image index. We store it as
  1174. * a float, but it should be an integer instead.
  1175. *
  1176. * index is a temporary tensor to facilitate its backward operator.
  1177. * It is used to store argmax indicex in MAX mode, and it is not used
  1178. * in AVERAGE mode.
  1179. */
  1180. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in rois,
  1181. _megdnn_tensor_out dst, _megdnn_tensor_out index,
  1182. _megdnn_workspace workspace) = 0;
  1183. void deduce_layout(const TensorLayout& src, const TensorLayout& rois,
  1184. TensorLayout& dst, TensorLayout& index);
  1185. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1186. const TensorLayout& rois,
  1187. const TensorLayout& dst,
  1188. const TensorLayout& index) = 0;
  1189. protected:
  1190. void check_exec(const TensorLayout& src, const TensorLayout& rois,
  1191. const TensorLayout& dst, const TensorLayout& index,
  1192. size_t workspace_in_bytes);
  1193. };
  1194. using ROIAlign = ROIAlignForward;
  1195. class ROIAlignBackward : public ROIAlignBase {
  1196. DEF_OPR_IMPL(ROIAlignBackward, ROIAlignBase, 3, 1);
  1197. public:
  1198. /**
  1199. * \param[in] diff the backpropagated gradient wrt. dst
  1200. * \param[in] rois the `rois' parameter in ROIAlignForward::exec
  1201. * \param[in] index the `index' parameter in ROIAlignForward::exec
  1202. * \param[out] grad the backpropagated gradient wrt. src
  1203. */
  1204. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in rois,
  1205. _megdnn_tensor_in index, _megdnn_tensor_out grad,
  1206. _megdnn_workspace workspace) = 0;
  1207. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1208. const TensorLayout& rois,
  1209. const TensorLayout& index,
  1210. const TensorLayout& grad) = 0;
  1211. protected:
  1212. void check_exec(const TensorLayout& diff, const TensorLayout& rois,
  1213. const TensorLayout& index, const TensorLayout& grad,
  1214. size_t workspace_in_bytes);
  1215. };
  1216. class DeformableConvBase : public OperatorBase {
  1217. DEF_OPR_IMPL_CTOR(DeformableConvBase, OperatorBase);
  1218. DEF_OPR_PARAM(Convolution);
  1219. public:
  1220. static constexpr size_t MAX_SPATIAL_DIM = 2;
  1221. struct CanonizedFilterMeta : Convolution::CanonizedFilterMeta {
  1222. uint32_t deformable_group;
  1223. };
  1224. protected:
  1225. CanonizedFilterMeta make_canonized_filter_meta(
  1226. size_t src_ndim, const TensorLayout& filter,
  1227. const TensorLayout& offset) const;
  1228. void deduce_layout_fwd(const TensorLayout& im, const TensorLayout& filter,
  1229. const TensorLayout& mask, const TensorLayout& offset,
  1230. TensorLayout& dst);
  1231. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1232. const TensorLayout& mask, const TensorLayout& offset,
  1233. const TensorLayout& dst);
  1234. };
  1235. class DeformableConvForward
  1236. : public DeformableConvBase,
  1237. public detail::MultiAlgoOpr<DeformableConvForward, 5> {
  1238. DEF_OPR_IMPL(DeformableConvForward, DeformableConvBase, 4, 1);
  1239. public:
  1240. /**
  1241. * \param[in] im (n, ic, ih, iw)
  1242. * \param[in] filter (oc, ic, fh, fw)
  1243. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1244. * \param[in] mask (dg, fh, fw, oh, ow)
  1245. * \param[out] dst (n, oc, oh, ow)
  1246. */
  1247. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in filter,
  1248. _megdnn_tensor_in offset, _megdnn_tensor_in mask,
  1249. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1250. void deduce_layout(const TensorLayout& im, const TensorLayout& filter,
  1251. const TensorLayout& offset, const TensorLayout& mask,
  1252. TensorLayout& dst);
  1253. virtual size_t get_workspace_in_bytes(const TensorLayout& im,
  1254. const TensorLayout& filter,
  1255. const TensorLayout& offset,
  1256. const TensorLayout& mask,
  1257. const TensorLayout& dst) = 0;
  1258. protected:
  1259. CanonizedFilterMeta check_exec(const TensorLayout& im,
  1260. const TensorLayout& filter,
  1261. const TensorLayout& offset,
  1262. const TensorLayout& mask,
  1263. const TensorLayout& dst,
  1264. size_t workspace_in_bytes);
  1265. };
  1266. using DeformableConv = DeformableConvForward;
  1267. /**
  1268. * \brief DeformableConvBackwardFilter operator.
  1269. *
  1270. * Calculating the gradient wrt. convolution filter.
  1271. */
  1272. class DeformableConvBackwardFilter
  1273. : public DeformableConvBase,
  1274. public detail::MultiAlgoOpr<DeformableConvBackwardFilter, 5> {
  1275. DEF_OPR_IMPL(DeformableConvBackwardFilter, DeformableConvBase, 4, 1);
  1276. public:
  1277. /**
  1278. * \param[in] im (oc, ic, fh, fw)
  1279. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1280. * \param[in] mask (dg, fh, fw, oh, ow)
  1281. * \param[in] out_grad (n, oc, oh, ow)
  1282. * \param[out] filter_grad (oc, ic, ih, iw)
  1283. */
  1284. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in offset,
  1285. _megdnn_tensor_in mask, _megdnn_tensor_in out_grad,
  1286. _megdnn_tensor_out filter_grad,
  1287. _megdnn_workspace workspace) = 0;
  1288. virtual size_t get_workspace_in_bytes(const TensorLayout& im,
  1289. const TensorLayout& offset,
  1290. const TensorLayout& mask,
  1291. const TensorLayout& out_grad,
  1292. const TensorLayout& filter_grad) = 0;
  1293. void deduce_layout(const TensorLayout& im, const TensorLayout& offset,
  1294. const TensorLayout& mask, const TensorLayout& out_grad,
  1295. TensorLayout& filter_grad);
  1296. protected:
  1297. CanonizedFilterMeta check_exec(const TensorLayout& im,
  1298. const TensorLayout& offset,
  1299. const TensorLayout& mask,
  1300. const TensorLayout& out_grad,
  1301. const TensorLayout& filter_grad,
  1302. size_t workspace_in_bytes);
  1303. };
  1304. /**
  1305. * \brief DeformableConvBackwardData operator.
  1306. *
  1307. * Calculating the gradient wrt. convolution input data, offset and mask.
  1308. */
  1309. class DeformableConvBackwardData
  1310. : public DeformableConvBase,
  1311. public detail::MultiAlgoOpr<DeformableConvBackwardData, 8> {
  1312. DEF_OPR_IMPL(DeformableConvBackwardData, DeformableConvBase, 5, 3);
  1313. public:
  1314. /**
  1315. * \param[in] im (oc, ic, fh, fw)
  1316. * \param[in] filter (oc, ic, fh, fw)
  1317. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1318. * \param[in] mask (dg, fh, fw, oh, ow)
  1319. * \param[in] out_grad (n, oc, oh, ow)
  1320. * \param[out] im_grad (n, ic, ih, iw)
  1321. * \param[out] offset_grad (dg, 2, fh, fw, oh, ow)
  1322. * \param[out] mask_grad (dg, fh, fw, oh, ow)
  1323. */
  1324. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in filter,
  1325. _megdnn_tensor_in offset, _megdnn_tensor_in mask,
  1326. _megdnn_tensor_in out_grad, _megdnn_tensor_out im_grad,
  1327. _megdnn_tensor_out offset_grad,
  1328. _megdnn_tensor_out mask_grad,
  1329. _megdnn_workspace workspace) = 0;
  1330. virtual size_t get_workspace_in_bytes(
  1331. const TensorLayout& im, const TensorLayout& filter,
  1332. const TensorLayout& offset, const TensorLayout& mask,
  1333. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1334. const TensorLayout& offset_grad, const TensorLayout& mask_grad) = 0;
  1335. void deduce_layout(const TensorLayout& im, const TensorLayout& filter,
  1336. const TensorLayout& offset, const TensorLayout& mask,
  1337. const TensorLayout& out_grad, TensorLayout& im_grad,
  1338. TensorLayout& offset_grad, TensorLayout& mask_grad);
  1339. protected:
  1340. CanonizedFilterMeta check_exec(
  1341. const TensorLayout& im, const TensorLayout& filter,
  1342. const TensorLayout& offset, const TensorLayout& mask,
  1343. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1344. const TensorLayout& offset_grad, const TensorLayout& mask_grad,
  1345. size_t workspace_in_bytes);
  1346. };
  1347. class DeformablePSROIPoolingBase : public OperatorBase {
  1348. DEF_OPR_IMPL_CTOR(DeformablePSROIPoolingBase, OperatorBase);
  1349. DEF_OPR_PARAM(DeformablePSROIPooling);
  1350. protected:
  1351. void deduce_layout_fwd(const TensorLayout& data, const TensorLayout& trans,
  1352. const TensorLayout& rois, TensorLayout& out_data,
  1353. TensorLayout& out_count);
  1354. void check_layout_fwd(const TensorLayout& data, const TensorLayout& trans,
  1355. const TensorLayout& rois,
  1356. const TensorLayout& out_data,
  1357. const TensorLayout& out_count,
  1358. size_t workspace_in_bytes);
  1359. };
  1360. class DeformablePSROIPoolingForward : public DeformablePSROIPoolingBase {
  1361. DEF_OPR_IMPL(DeformablePSROIPoolingForward, DeformablePSROIPoolingBase, 3,
  1362. 2);
  1363. public:
  1364. /**
  1365. * \param[in] data (oc, ic, ih, iw)
  1366. * \param[in] rois (xx, xx, xx, xx)
  1367. * \param[in] trans (oc, ic, fh, fw)
  1368. * \param[out] out_data ( n, ic, ih, iw)
  1369. * \param[out] out_count ( n, ic, ih, iw)
  1370. */
  1371. virtual size_t get_workspace_in_bytes(const TensorLayout& data,
  1372. const TensorLayout& rois,
  1373. const TensorLayout& trans,
  1374. const TensorLayout& out_data,
  1375. const TensorLayout& out_count) = 0;
  1376. virtual void exec(_megdnn_tensor_in data, _megdnn_tensor_in rois,
  1377. _megdnn_tensor_in trans, _megdnn_tensor_out out_data,
  1378. _megdnn_tensor_out out_count,
  1379. _megdnn_workspace workspace) = 0;
  1380. void deduce_layout(const TensorLayout& data, const TensorLayout& rois,
  1381. const TensorLayout& trans, TensorLayout& out_data,
  1382. TensorLayout& out_count);
  1383. void check_exec(const TensorLayout& data, const TensorLayout& rois,
  1384. const TensorLayout& trans, const TensorLayout& out_data,
  1385. const TensorLayout& out_count, size_t workspace_in_bytes);
  1386. };
  1387. using DeformablePSROIPooling = DeformablePSROIPoolingForward;
  1388. class DeformablePSROIPoolingBackward : public DeformablePSROIPoolingBase {
  1389. DEF_OPR_IMPL(DeformablePSROIPoolingBackward, DeformablePSROIPoolingBase, 5,
  1390. 2);
  1391. public:
  1392. /**
  1393. * \param[in] data (oc, ic, ih, iw)
  1394. * \param[in] rois (xx, xx, xx, xx)
  1395. * \param[in] trans (oc, ic, fh, fw)
  1396. * \param[in] out_diff (xx, xx, xx, xx)
  1397. * \param[in] out_count (xx, xx, xx, xx)
  1398. * \param[out] data_diff ( n, ic, ih, iw)
  1399. * \param[out] trans_diff ( n, ic, ih, iw)
  1400. */
  1401. virtual void exec(_megdnn_tensor_in data, _megdnn_tensor_in rois,
  1402. _megdnn_tensor_in trans, _megdnn_tensor_in out_diff,
  1403. _megdnn_tensor_in out_count, _megdnn_tensor_out data_diff,
  1404. _megdnn_tensor_out trans_diff,
  1405. _megdnn_workspace workspace) = 0;
  1406. virtual size_t get_workspace_in_bytes(const TensorLayout& data,
  1407. const TensorLayout& rois,
  1408. const TensorLayout& trans,
  1409. const TensorLayout& out_diff,
  1410. const TensorLayout& out_count,
  1411. const TensorLayout& data_diff,
  1412. const TensorLayout& trans_diff) = 0;
  1413. void check_exec(const TensorLayout& data, const TensorLayout& rois,
  1414. const TensorLayout& trans, const TensorLayout& out_diff,
  1415. const TensorLayout& out_count,
  1416. const TensorLayout& data_diff,
  1417. const TensorLayout& trans_diff, size_t workspace_in_bytes);
  1418. };
  1419. class BatchConvBiasForward
  1420. : public ConvolutionBase<param::BatchConvBias>,
  1421. public detail::MultiAlgoOpr<BatchConvBiasForward, 5> {
  1422. DEF_OPR_IMPL(BatchConvBiasForward, ConvolutionBase, 4, 1);
  1423. public:
  1424. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1425. _megdnn_tensor_in bias, _megdnn_tensor_in z,
  1426. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1427. void deduce_dtype(DType src, DType filter, DType bias, DType z, DType& dst);
  1428. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1429. const TensorLayout& bias, const TensorLayout& z,
  1430. TensorLayout& dst);
  1431. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1432. const TensorLayout& filter,
  1433. const TensorLayout& bias,
  1434. const TensorLayout& z,
  1435. const TensorLayout& dst) = 0;
  1436. protected:
  1437. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1438. const TensorLayout& filter,
  1439. const TensorLayout& bias,
  1440. const TensorLayout& z,
  1441. const TensorLayout& dst,
  1442. size_t workspace_in_bytes);
  1443. };
  1444. using BatchConvBias = BatchConvBiasForward;
  1445. class FakeQuantBase : public OperatorBase {
  1446. DEF_OPR_IMPL_CTOR(FakeQuantBase, OperatorBase);
  1447. DEF_OPR_PARAM(FakeQuant);
  1448. protected:
  1449. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1450. void check_layout_fwd(const TensorLayout& input, const TensorLayout& scale,
  1451. const TensorLayout& zero_point,
  1452. const TensorLayout& output);
  1453. };
  1454. class FakeQuantForward : public FakeQuantBase {
  1455. DEF_OPR_IMPL(FakeQuantForward, FakeQuantBase, 3, 1);
  1456. public:
  1457. virtual void exec(_megdnn_tensor_in input, _megdnn_tensor_in scale,
  1458. _megdnn_tensor_in zero_point, _megdnn_tensor_out output,
  1459. _megdnn_workspace workspace) = 0;
  1460. void deduce_layout(const TensorLayout& input, const TensorLayout& scale,
  1461. const TensorLayout& zero_point, TensorLayout& output);
  1462. virtual size_t get_workspace_in_bytes(const TensorLayout& input,
  1463. const TensorLayout& scale,
  1464. const TensorLayout& zero_point,
  1465. const TensorLayout& output) = 0;
  1466. protected:
  1467. void check_exec(const TensorLayout& input, const TensorLayout& scale,
  1468. const TensorLayout& zero_point, const TensorLayout& output,
  1469. size_t workspace_in_bytes);
  1470. };
  1471. using FakeQuant = FakeQuantForward;
  1472. class FakeQuantBackward : public FakeQuantBase {
  1473. DEF_OPR_IMPL(FakeQuantBackward, FakeQuantBase, 4, 1);
  1474. public:
  1475. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in input,
  1476. _megdnn_tensor_in scale, _megdnn_tensor_in zero_point,
  1477. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1478. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1479. const TensorLayout& input,
  1480. const TensorLayout& scale,
  1481. const TensorLayout& zero_point,
  1482. const TensorLayout& grad) = 0;
  1483. protected:
  1484. void check_exec(const TensorLayout& diff, const TensorLayout& input,
  1485. const TensorLayout& scale, const TensorLayout& zero_point,
  1486. const TensorLayout& grad, size_t workspace_in_bytes);
  1487. };
  1488. class TQTBase : public OperatorBase {
  1489. DEF_OPR_IMPL_CTOR(TQTBase, OperatorBase);
  1490. DEF_OPR_PARAM(TQT);
  1491. protected:
  1492. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1493. void check_layout_fwd(const TensorLayout& input, const TensorLayout& scale,
  1494. const TensorLayout& output);
  1495. };
  1496. class TQTForward : public TQTBase {
  1497. DEF_OPR_IMPL(TQTForward, TQTBase, 2, 1);
  1498. public:
  1499. virtual void exec(_megdnn_tensor_in input, _megdnn_tensor_in scale,
  1500. _megdnn_tensor_out output,
  1501. _megdnn_workspace workspace) = 0;
  1502. void deduce_layout(const TensorLayout& input, const TensorLayout& scale,
  1503. TensorLayout& output);
  1504. virtual size_t get_workspace_in_bytes(const TensorLayout& input,
  1505. const TensorLayout& scale,
  1506. const TensorLayout& output) = 0;
  1507. protected:
  1508. void check_exec(const TensorLayout& input, const TensorLayout& scale,
  1509. const TensorLayout& output, size_t workspace_in_bytes);
  1510. };
  1511. using TQT = TQTForward;
  1512. class TQTBackward : public TQTBase {
  1513. DEF_OPR_IMPL(TQTBackward, TQTBase, 3, 2);
  1514. public:
  1515. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in input,
  1516. _megdnn_tensor_in scale, _megdnn_tensor_out grad_x,
  1517. _megdnn_tensor_out grad_s,
  1518. _megdnn_workspace workspace) = 0;
  1519. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1520. const TensorLayout& input,
  1521. const TensorLayout& scale,
  1522. const TensorLayout& grad_x,
  1523. const TensorLayout& grad_s) = 0;
  1524. protected:
  1525. void check_exec(const TensorLayout& diff, const TensorLayout& input,
  1526. const TensorLayout& scale, const TensorLayout& grad_x,
  1527. const TensorLayout& grad_s, size_t workspace_in_bytes);
  1528. };
  1529. class LSQBase : public OperatorBase {
  1530. DEF_OPR_IMPL_CTOR(LSQBase, OperatorBase);
  1531. DEF_OPR_PARAM(LSQ);
  1532. protected:
  1533. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1534. void check_layout_fwd(const TensorLayout& input, const TensorLayout& scale,
  1535. const TensorLayout& zero_point,
  1536. const TensorLayout& grad_scale,
  1537. const TensorLayout& output);
  1538. };
  1539. class LSQForward : public LSQBase {
  1540. DEF_OPR_IMPL(LSQForward, LSQBase, 4, 1);
  1541. public:
  1542. virtual void exec(_megdnn_tensor_in input, _megdnn_tensor_in scale,
  1543. _megdnn_tensor_in zero_point,
  1544. _megdnn_tensor_in grad_scale, _megdnn_tensor_out output,
  1545. _megdnn_workspace workspace) = 0;
  1546. void deduce_layout(const TensorLayout& input, const TensorLayout& scale,
  1547. const TensorLayout& zero_point,
  1548. const TensorLayout& grad_scale, 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& grad_scale,
  1553. const TensorLayout& output) = 0;
  1554. protected:
  1555. void check_exec(const TensorLayout& input, const TensorLayout& scale,
  1556. const TensorLayout& zero_point,
  1557. const TensorLayout& grad_scale, const TensorLayout& output,
  1558. size_t workspace_in_bytes);
  1559. };
  1560. using LSQ = LSQForward;
  1561. class LSQBackward : public LSQBase {
  1562. DEF_OPR_IMPL(LSQBackward, LSQBase, 5, 2);
  1563. public:
  1564. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in input,
  1565. _megdnn_tensor_in scale, _megdnn_tensor_in zero_point,
  1566. _megdnn_tensor_in grad_scale, _megdnn_tensor_out grad_x,
  1567. _megdnn_tensor_out grad_s,
  1568. _megdnn_workspace workspace) = 0;
  1569. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1570. const TensorLayout& input,
  1571. const TensorLayout& scale,
  1572. const TensorLayout& zero_point,
  1573. const TensorLayout& grad_scale,
  1574. const TensorLayout& grad_x,
  1575. const TensorLayout& grad_s) = 0;
  1576. protected:
  1577. void check_exec(const TensorLayout& diff, const TensorLayout& input,
  1578. const TensorLayout& scale, const TensorLayout& zero_point,
  1579. const TensorLayout& grad_scale, const TensorLayout& grad_x,
  1580. const TensorLayout& grad_s, size_t workspace_in_bytes);
  1581. };
  1582. } // namespace megdnn
  1583. #include "megdnn/internal/opr_header_epilogue.h"
  1584. // vim: syntax=cpp.doxygen

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