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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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-2020 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 to
  200. * 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. /**
  392. * \brief deduce the origin filter layout and conv_bias param after winograd
  393. * transform, this used in fast-run to construct the origin cache-key
  394. */
  395. static void deduce_winograd_origin_layout_and_param(
  396. const Param::Format format, const size_t output_block_size,
  397. const TensorLayout& src_layout,
  398. const TensorLayout& winograd_filter_layout,
  399. TensorLayout& origin_layout, Param& origin_param);
  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. protected:
  463. CanonizedFilterMeta check_exec(
  464. const TensorLayout& src, const TensorLayout& filter,
  465. const TensorLayout& bias, const TensorLayout& z,
  466. const TensorLayout& dst, size_t workspace_in_bytes,
  467. const PreprocessedFilter* preprocessed_filter);
  468. };
  469. using ConvBias = ConvBiasForward;
  470. /**
  471. * \brief base class for Conv - Nonline - Pooling
  472. */
  473. class ConvPoolingBase : public OperatorBase {
  474. DEF_OPR_IMPL_CTOR(ConvPoolingBase, OperatorBase);
  475. /**
  476. * \ Param::Method: Two methods to fetch the input data.
  477. * Default methods is WITH_TEXTURE_OBJ.
  478. * If you want to use WITH_SHARED_MEM mode,
  479. * please make sure that the size of
  480. * [ all of the fliter kernels + a channel
  481. * of input data + a channel of output data]
  482. * should be no large than 38KB.
  483. * And the pooling mode should not be "MAX".
  484. */
  485. DEF_OPR_PARAM(ConvPooling);
  486. protected:
  487. virtual void deduce_layout(const TensorLayout& src,
  488. const TensorLayout& filter,
  489. const TensorLayout& bias, TensorLayout& dst) = 0;
  490. virtual void check_layout(const TensorLayout& src,
  491. const TensorLayout& filter,
  492. const TensorLayout& bias, TensorLayout& dst,
  493. size_t workspace_limit_in_bytes) = 0;
  494. };
  495. class ConvPoolingForward : public ConvPoolingBase {
  496. DEF_OPR_IMPL(ConvPoolingForward, ConvPoolingBase, 2, 1);
  497. public:
  498. /**
  499. * \param[in] src input tensor
  500. * \param[out] dst output tensor
  501. */
  502. virtual void exec(const _megdnn_in TensorND src,
  503. const _megdnn_in TensorND filter,
  504. const _megdnn_in TensorND bias, _megdnn_out TensorND dst,
  505. _megdnn_out Workspace workspace) = 0;
  506. virtual void deduce_layout(const TensorLayout& src,
  507. const TensorLayout& filter,
  508. const TensorLayout& bias, TensorLayout& dst) = 0;
  509. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  510. const TensorLayout& filter,
  511. const TensorLayout& bias,
  512. const TensorLayout& dst) = 0;
  513. protected:
  514. virtual void check_layout(const TensorLayout& src,
  515. const TensorLayout& filter,
  516. const TensorLayout& bias, TensorLayout& dst,
  517. size_t workspace_limit_in_bytes) = 0;
  518. };
  519. using ConvPooling = ConvPoolingForward;
  520. class GroupLocalBase : public OperatorBase {
  521. DEF_OPR_IMPL_CTOR(GroupLocalBase, OperatorBase);
  522. DEF_OPR_PARAM(Convolution);
  523. public:
  524. using Mode = Param::Mode;
  525. protected:
  526. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  527. TensorLayout& dst);
  528. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  529. const TensorLayout& dst);
  530. };
  531. class GroupLocalForward : public GroupLocalBase {
  532. DEF_OPR_IMPL(GroupLocalForward, GroupLocalBase, 2, 1);
  533. public:
  534. /**
  535. * \param[in] src (N, IC, IH, IW)
  536. * \param[in] filter (G, OH, OW, IC/G, FH, FW, OC/G)
  537. * \param[out] dst (N, OC, OH, OW)
  538. **/
  539. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  540. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  541. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  542. TensorLayout& dst) {
  543. deduce_layout_fwd(src, filter, dst);
  544. }
  545. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  546. const TensorLayout& filter,
  547. const TensorLayout& dst) = 0;
  548. protected:
  549. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  550. const TensorLayout& dst, size_t workspace_in_bytes);
  551. };
  552. using GroupLocal = GroupLocalForward;
  553. class GroupLocalBackwardData : public GroupLocalBase {
  554. DEF_OPR_IMPL(GroupLocalBackwardData, GroupLocalBase, 2, 1);
  555. public:
  556. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  557. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  558. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  559. const TensorLayout& diff,
  560. const TensorLayout& grad) = 0;
  561. protected:
  562. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  563. const TensorLayout& grad, size_t workspace_in_bytes);
  564. };
  565. class GroupLocalBackwardFilter : public GroupLocalBase {
  566. DEF_OPR_IMPL(GroupLocalBackwardFilter, GroupLocalBase, 2, 1);
  567. public:
  568. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  569. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  570. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  571. const TensorLayout& diff,
  572. const TensorLayout& grad) = 0;
  573. protected:
  574. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  575. const TensorLayout& grad, size_t workspace_in_bytes);
  576. };
  577. class Images2NeibsBase : public OperatorBase {
  578. DEF_OPR_IMPL_CTOR(Images2NeibsBase, OperatorBase);
  579. DEF_OPR_PARAM(Images2Neibs);
  580. protected:
  581. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  582. void check_layout_fwd(const TensorLayout& filter, const TensorLayout& dst);
  583. };
  584. class Images2NeibsForward : public Images2NeibsBase {
  585. DEF_OPR_IMPL(Images2NeibsForward, Images2NeibsBase, 1, 1);
  586. public:
  587. /**
  588. * \param[in] src (N, C, IH, IW)
  589. * \param[out] dst (N, C, OH, OW, window_h, window_w)
  590. *
  591. * \see
  592. * http://deeplearning.net/software/theano/library/tensor/nnet/neighbours.html
  593. *
  594. * \f$ dst_{n, c, oh, ow, wh, ww} = src_{n, c, ih+wh, iw+fw}\f$,
  595. * where \f$ ih=-pad_h+oh*stride_h, iw=-pad_w+ow*stride_w\f$.
  596. */
  597. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  598. _megdnn_workspace workspace) = 0;
  599. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  600. const TensorLayout& dst) = 0;
  601. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  602. protected:
  603. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  604. size_t workspace_in_bytes);
  605. };
  606. using Images2Neibs = Images2NeibsForward;
  607. class Images2NeibsBackward : public Images2NeibsBase {
  608. DEF_OPR_IMPL(Images2NeibsBackward, Images2NeibsBase, 1, 1);
  609. public:
  610. /**
  611. * \param[in] diff the backpropagated gradient wrt. dst
  612. * \param[out] grad the backpropagated gradient wrt. src
  613. */
  614. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_out grad,
  615. _megdnn_workspace workspace) = 0;
  616. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  617. const TensorLayout& grad) = 0;
  618. protected:
  619. void check_exec(const TensorLayout& diff, const TensorLayout& grad,
  620. size_t workspace_in_bytes);
  621. };
  622. /**
  623. * \brief base class for Pooling
  624. */
  625. class PoolingBase : public OperatorBase {
  626. DEF_OPR_IMPL_CTOR(PoolingBase, OperatorBase);
  627. DEF_OPR_PARAM(Pooling);
  628. public:
  629. using Mode = Param::Mode;
  630. protected:
  631. void deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst);
  632. void check_layout_fwd(const TensorLayout& src, const TensorLayout& dst);
  633. };
  634. class PoolingForward : public PoolingBase {
  635. DEF_OPR_IMPL(PoolingForward, PoolingBase, 1, 1);
  636. public:
  637. /**
  638. * \param[in] src input tensor
  639. * \param[out] dst output tensor
  640. */
  641. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  642. _megdnn_workspace workspace) = 0;
  643. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  644. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  645. const TensorLayout& dst) = 0;
  646. protected:
  647. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  648. size_t workspace_in_bytes);
  649. };
  650. using Pooling = PoolingForward;
  651. class PoolingBackward : public PoolingBase {
  652. DEF_OPR_IMPL(PoolingBackward, PoolingBase, 3, 1);
  653. public:
  654. /**
  655. * \param[in] src the `src' parameter in PoolingForward::exec
  656. * \param[in] dst the `dst' parameter in PoolingForward::exec
  657. * \param[in] diff the backpropagated gradient wrt. dst
  658. * \param[out] grad the backpropagated gradient wrt. src
  659. */
  660. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  661. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  662. _megdnn_workspace workspace) = 0;
  663. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  664. const TensorLayout& dst,
  665. const TensorLayout& diff,
  666. const TensorLayout& grad) = 0;
  667. protected:
  668. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  669. const TensorLayout& diff, const TensorLayout& grad,
  670. size_t workspace_in_bytes);
  671. };
  672. /**
  673. * \brief base class for AdaptivePooling
  674. */
  675. class AdaptivePoolingBase : public OperatorBase {
  676. DEF_OPR_IMPL_CTOR(AdaptivePoolingBase, OperatorBase);
  677. DEF_OPR_PARAM(AdaptivePooling);
  678. protected:
  679. param::Pooling deduce_pooling_param(const TensorLayout& src,
  680. const TensorLayout& dst);
  681. };
  682. class AdaptivePoolingForward : public AdaptivePoolingBase {
  683. DEF_OPR_IMPL(AdaptivePoolingForward, AdaptivePoolingBase, 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. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  692. const TensorLayout& dst) = 0;
  693. };
  694. using AdaptivePooling = AdaptivePoolingForward;
  695. class AdaptivePoolingBackward : public AdaptivePoolingBase {
  696. DEF_OPR_IMPL(AdaptivePoolingBackward, AdaptivePoolingBase, 3, 1);
  697. public:
  698. /**
  699. * \param[in] src the `src' parameter in AdaptivePoolingForward::exec
  700. * \param[in] dst the `dst' parameter in AdaptivePoolingForward::exec
  701. * \param[in] diff the backpropagated gradient wrt. dst
  702. * \param[out] grad the backpropagated gradient wrt. src
  703. */
  704. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  705. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  706. _megdnn_workspace workspace) = 0;
  707. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  708. const TensorLayout& dst,
  709. const TensorLayout& diff,
  710. const TensorLayout& grad) = 0;
  711. };
  712. /**
  713. * \brief base class for Local
  714. */
  715. class LocalBase : public OperatorBase {
  716. DEF_OPR_IMPL_CTOR(LocalBase, OperatorBase);
  717. DEF_OPR_PARAM(Convolution);
  718. public:
  719. using Mode = Param::Mode;
  720. protected:
  721. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  722. TensorLayout& dst);
  723. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  724. const TensorLayout& dst);
  725. };
  726. class LocalForward : public LocalBase {
  727. DEF_OPR_IMPL(LocalForward, LocalBase, 2, 1);
  728. public:
  729. /**
  730. * \param[in] src (n, ic, ih, iw)
  731. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  732. * \param[out] dst (n, oc, oh, ow)
  733. */
  734. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  735. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  736. /**
  737. * \brief Deducing output tensor layouts from input tensor layouts.
  738. *
  739. * Be aware that the first and second dimension of `filter' are ignored
  740. * when deducing `dst' layout.
  741. */
  742. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  743. TensorLayout& dst);
  744. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  745. const TensorLayout& filter,
  746. const TensorLayout& dst) = 0;
  747. protected:
  748. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  749. const TensorLayout& dst, size_t workspace_in_bytes);
  750. };
  751. using Local = LocalForward;
  752. class LocalBackwardData : public LocalBase {
  753. DEF_OPR_IMPL(LocalBackwardData, LocalBase, 2, 1);
  754. public:
  755. /**
  756. * \param[in] filter (oh, ow, ic, fh, fw, oc)
  757. * \param[in] diff (n, oc, oh, ow)
  758. * \param[out] grad (n, ic, ih, iw)
  759. */
  760. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  761. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  762. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  763. const TensorLayout& diff,
  764. const TensorLayout& grad) = 0;
  765. protected:
  766. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  767. const TensorLayout& grad, size_t workspace_in_bytes);
  768. };
  769. class LocalBackwardFilter : public LocalBase {
  770. DEF_OPR_IMPL(LocalBackwardFilter, LocalBase, 2, 1);
  771. public:
  772. /**
  773. * \param[in] src (n, ic, ih, iw)
  774. * \param[in] diff (n, oc, oh, ow)
  775. * \param[out] grad (oh, ow, ic, fh, fw, oc)
  776. */
  777. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  778. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  779. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  780. const TensorLayout& diff,
  781. const TensorLayout& grad) = 0;
  782. protected:
  783. void check_exec(const TensorLayout& src, const TensorLayout& diff,
  784. const TensorLayout& grad, size_t workspace_in_bytes);
  785. };
  786. class BNBase : public OperatorBase {
  787. DEF_OPR_IMPL_CTOR(BNBase, OperatorBase);
  788. DEF_OPR_PARAM(BN);
  789. protected:
  790. void check_param();
  791. };
  792. class BNForward : public BNBase {
  793. DEF_OPR_IMPL(BNForward, BNBase, 6, 5);
  794. public:
  795. /**
  796. * \dst[i] = gemma
  797. * *(x[i]-estimatedMean[k])/sqrt(epsilon+estimatedVariance[k]) + beta \where
  798. * epsilon is a very small value to avoid a "divide by zero" error.
  799. * \param[in] src (n, c, h, w)
  800. * \param[out] dst (n, c, h, w)
  801. * \param[out] mean (see m_param.ParamDim) Global mean.
  802. * \param[out] variance (see m_param.ParamDim) Global variance.
  803. * \Param[out] batch_mean (see m_param.ParamDim)
  804. * Optionally cached intermediate mean from forward pass
  805. * \Param[out] batch_inv_variance (see m_param.ParamDim)
  806. * Optionally cached intermediate variance from forward pass
  807. * src and dst must have the same shape.
  808. * src and dst must be contiguous.
  809. */
  810. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in bn_scale,
  811. _megdnn_tensor_in bn_bias, _megdnn_tensor_inout mean,
  812. _megdnn_tensor_inout variance,
  813. _megdnn_tensor_out batch_mean,
  814. _megdnn_tensor_out batch_inv_variance,
  815. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  816. void deduce_layout(const TensorLayout& src, TensorLayout& bn_scale,
  817. TensorLayout& bn_bias, TensorLayout& mean,
  818. TensorLayout& variance, TensorLayout& batch_mean,
  819. TensorLayout& batch_inv_variance, TensorLayout& dst);
  820. virtual size_t get_workspace_in_bytes(
  821. const TensorLayout& src, const TensorLayout& bn_scale,
  822. const TensorLayout& bn_bias, const TensorLayout& mean,
  823. const TensorLayout& variance, const TensorLayout& batch_mean,
  824. const TensorLayout& batch_inv_variance,
  825. const TensorLayout& dst) = 0;
  826. protected:
  827. void check_exec(const TensorLayout& src, const TensorLayout& bn_scale,
  828. const TensorLayout& bn_bias, const TensorLayout& mean,
  829. const TensorLayout& variance,
  830. const TensorLayout& batch_mean,
  831. const TensorLayout& batch_inv_variance,
  832. const TensorLayout& dst, size_t workspace_in_bytes);
  833. };
  834. using BN = BNForward;
  835. class BNBackward : public BNBase {
  836. DEF_OPR_IMPL(BNBackward, BNBase, 5, 3);
  837. public:
  838. /**
  839. * \param[in] input data of forwarding propagate.
  840. * \param[in] dy the backpropagated gradient of y.
  841. * \param[out] dx the backpropagated gradient of x.
  842. * \param[out] d_bn_scale, the backpropagated gradient of bn_scale.
  843. * \param[out] d_bn_bias, the backpropagated gradient of bn_bias.
  844. * Optionally cached intermediate results from forward pass
  845. * \param[in] saved_batch_mean mean of the input batch.
  846. Calculated in the forwardpropagation.
  847. * \param[in] saved_batch_variance of the input batch.
  848. Calculated in the forwardpropagation.
  849. */
  850. virtual void exec(_megdnn_tensor_in x, _megdnn_tensor_in dy,
  851. _megdnn_tensor_in saved_batch_mean,
  852. _megdnn_tensor_in saved_batch_variance,
  853. _megdnn_tensor_in bn_scale, _megdnn_tensor_out d_bn_scale,
  854. _megdnn_tensor_out d_bn_bias, _megdnn_tensor_out dx,
  855. _megdnn_workspace workspace) = 0;
  856. virtual size_t get_workspace_in_bytes(
  857. const TensorLayout& x, const TensorLayout& dy,
  858. const TensorLayout& saved_batch_mean,
  859. const TensorLayout& saved_batch_variance,
  860. const TensorLayout& bn_scale, const TensorLayout& d_bn_scale,
  861. const TensorLayout& d_bn_bias, const TensorLayout& dx) = 0;
  862. protected:
  863. void check_exec(const TensorLayout& x, const TensorLayout& dy,
  864. const TensorLayout& saved_batch_mean,
  865. const TensorLayout& saved_batch_variance,
  866. const TensorLayout& bn_scale,
  867. const TensorLayout& d_bn_scale,
  868. const TensorLayout& d_bn_bias, const TensorLayout& dx,
  869. size_t workspace_in_bytes);
  870. };
  871. class LRNBase : public OperatorBase {
  872. DEF_OPR_IMPL_CTOR(LRNBase, OperatorBase);
  873. DEF_OPR_PARAM(LRN);
  874. protected:
  875. void check_param();
  876. };
  877. class LRNForward : public LRNBase {
  878. DEF_OPR_IMPL(LRNForward, LRNBase, 1, 1);
  879. public:
  880. /**
  881. * \see ImageNet Classification with Deep Convolutional Neural Networks
  882. * \param[in] src (n, c, h, w)
  883. * \param[out] dst (n, c, h, w)
  884. *
  885. * src and dst must have the same shape.
  886. * src and dst must be contiguous.
  887. */
  888. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_out dst,
  889. _megdnn_workspace workspace) = 0;
  890. void deduce_layout(const TensorLayout& src, TensorLayout& dst);
  891. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  892. const TensorLayout& dst) = 0;
  893. protected:
  894. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  895. size_t workspace_in_bytes);
  896. };
  897. using LRN = LRNForward;
  898. class LRNBackward : public LRNBase {
  899. DEF_OPR_IMPL(LRNBackward, LRNBase, 3, 1);
  900. public:
  901. /**
  902. * \param[in] src the `src' parameter in LRNForward::exec
  903. * \param[in] dst the `dst' parameter in LRNForward::exec
  904. * \param[in] diff the backpropagated gradient wrt. dst
  905. * \param[out] grad the backpropagated gradient wrt. src
  906. *
  907. * All tensors should be contiguous and of the same shape.
  908. */
  909. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in dst,
  910. _megdnn_tensor_in diff, _megdnn_tensor_out grad,
  911. _megdnn_workspace workspace) = 0;
  912. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  913. const TensorLayout& dst,
  914. const TensorLayout& diff,
  915. const TensorLayout& grad) = 0;
  916. protected:
  917. void check_exec(const TensorLayout& src, const TensorLayout& dst,
  918. const TensorLayout& diff, const TensorLayout& grad,
  919. size_t workspace_in_bytes);
  920. };
  921. class ROIPoolingBase : public OperatorBase {
  922. DEF_OPR_IMPL_CTOR(ROIPoolingBase, OperatorBase);
  923. DEF_OPR_PARAM(ROIPooling);
  924. protected:
  925. void check_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  926. const TensorLayout& dst, const TensorLayout& index);
  927. };
  928. class ROIPoolingForward : public ROIPoolingBase {
  929. DEF_OPR_IMPL(ROIPoolingForward, ROIPoolingBase, 2, 2);
  930. public:
  931. /**
  932. * \param[in] src (n, c, ih, iw)
  933. * \param[in] rois (m, 5)
  934. * \param[out] dst (m, c, oh, ow)
  935. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  936. *
  937. * The internal implementation is akin to
  938. * https://github.com/rbgirshick/caffe-fast-rcnn .d
  939. * Note that rois(, 0) denotes the input image index. We store it as
  940. * a float, but it should be an integer instead.
  941. *
  942. * index is a temporary tensor to facilitate its backward operator.
  943. * It is used to store argmax indicex in MAX mode, and it is not used
  944. * in AVERAGE mode.
  945. */
  946. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in rois,
  947. _megdnn_tensor_out dst, _megdnn_tensor_out index,
  948. _megdnn_workspace workspace) = 0;
  949. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  950. const TensorLayout& rois,
  951. const TensorLayout& dst,
  952. const TensorLayout& index) = 0;
  953. protected:
  954. void check_exec(const TensorLayout& src, const TensorLayout& rois,
  955. const TensorLayout& dst, const TensorLayout& index,
  956. size_t workspace_in_bytes);
  957. };
  958. using ROIPooling = ROIPoolingForward;
  959. class ROIPoolingBackward : public ROIPoolingBase {
  960. DEF_OPR_IMPL(ROIPoolingBackward, ROIPoolingBase, 4, 1);
  961. public:
  962. /**
  963. * \param[in] diff the backpropagated gradient wrt. dst
  964. * \param[in] src the `src' parameter in ROIPoolingForward::exec
  965. * \param[in] rois the `rois' parameter in ROIPoolingForward::exec
  966. * \param[in] index the `index' parameter in ROIPoolingForward::exec
  967. * \param[out] grad the backpropagated gradient wrt. src
  968. */
  969. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in src,
  970. _megdnn_tensor_in rois, _megdnn_tensor_in index,
  971. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  972. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  973. const TensorLayout& src,
  974. const TensorLayout& rois,
  975. const TensorLayout& index,
  976. const TensorLayout& grad) = 0;
  977. protected:
  978. void check_exec(const TensorLayout& diff, const TensorLayout& src,
  979. const TensorLayout& rois, const TensorLayout& index,
  980. const TensorLayout& grad, size_t workspace_in_bytes);
  981. };
  982. class Convolution3DBase : public OperatorBase {
  983. DEF_OPR_IMPL_CTOR(Convolution3DBase, OperatorBase);
  984. DEF_OPR_PARAM(Convolution3D);
  985. public:
  986. static constexpr size_t MAX_SPATIAL_DIM = 3;
  987. using Mode = Param::Mode;
  988. struct CanonizedFilterMeta {
  989. DTypeEnum dtype_enum;
  990. Param::Format format;
  991. uint32_t
  992. //! whether filter should be flipped (i.e. is CONVOLUTION)
  993. should_flip,
  994. group, //!< number of groups
  995. icpg, //!< input channels per group
  996. ocpg, //!< output channels per group
  997. spatial_ndim, stride[MAX_SPATIAL_DIM], padding[MAX_SPATIAL_DIM],
  998. //! spatial dim
  999. spatial[MAX_SPATIAL_DIM], dilation[MAX_SPATIAL_DIM],
  1000. //! spatial dim with dilation applied
  1001. dilated_spatial[MAX_SPATIAL_DIM];
  1002. } MEGDNN_PACKED;
  1003. protected:
  1004. CanonizedFilterMeta deduce_layout_fwd(const TensorLayout& src,
  1005. const TensorLayout& filter,
  1006. TensorLayout& dst) const;
  1007. CanonizedFilterMeta check_layout_fwd(const TensorLayout& src,
  1008. const TensorLayout& filter,
  1009. const TensorLayout& dst) const;
  1010. CanonizedFilterMeta make_canonized_filter_meta(
  1011. size_t src_ndim, const TensorLayout& filter) const;
  1012. };
  1013. class Convolution3DForward
  1014. : public Convolution3DBase,
  1015. public detail::MultiAlgoOpr<Convolution3DForward, 3> {
  1016. DEF_OPR_IMPL(Convolution3DForward, Convolution3DBase, 2, 1);
  1017. public:
  1018. /**
  1019. * \param[in] src (n, ic, id, ih, iw)
  1020. * \param[in] filter (oc, ic, fd, fh, fw)
  1021. * \param[out] dst (n, oc, od, oh, ow)
  1022. */
  1023. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1024. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1025. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1026. TensorLayout& dst);
  1027. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1028. const TensorLayout& filter,
  1029. const TensorLayout& dst) = 0;
  1030. protected:
  1031. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1032. const TensorLayout& filter,
  1033. const TensorLayout& dst,
  1034. size_t workspace_in_bytes);
  1035. };
  1036. using Convolution3D = Convolution3DForward;
  1037. class Convolution3DBackwardData
  1038. : public Convolution3DBase,
  1039. public detail::MultiAlgoOpr<Convolution3DBackwardData, 3> {
  1040. DEF_OPR_IMPL(Convolution3DBackwardData, Convolution3DBase, 2, 1);
  1041. public:
  1042. /**
  1043. * \param[in] filter (oc, ic, fd, fh, fw)
  1044. * \param[in] diff (n, oc, od, oh, ow)
  1045. * \param[out] grad (n, ic, id, ih, iw)
  1046. */
  1047. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  1048. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1049. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  1050. const TensorLayout& diff,
  1051. const TensorLayout& grad) = 0;
  1052. void deduce_layout(const TensorLayout& filter, const TensorLayout& diff,
  1053. TensorLayout& grad);
  1054. protected:
  1055. CanonizedFilterMeta check_exec(const TensorLayout& filter,
  1056. const TensorLayout& diff,
  1057. const TensorLayout& grad,
  1058. size_t workspace_in_bytes);
  1059. };
  1060. class Convolution3DBackwardFilter
  1061. : public Convolution3DBase,
  1062. public detail::MultiAlgoOpr<Convolution3DBackwardFilter, 3> {
  1063. DEF_OPR_IMPL(Convolution3DBackwardFilter, Convolution3DBase, 2, 1);
  1064. public:
  1065. /**
  1066. * \param[in] src (n, ic, id, ih, iw)
  1067. * \param[in] diff (n, oc, od, oh, ow)
  1068. * \param[out] grad (oc, ic, fd, fh, fw)
  1069. */
  1070. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  1071. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1072. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1073. const TensorLayout& diff,
  1074. const TensorLayout& grad) = 0;
  1075. protected:
  1076. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1077. const TensorLayout& diff,
  1078. const TensorLayout& grad,
  1079. size_t workspace_in_bytes);
  1080. };
  1081. class LocalShareBase : public OperatorBase {
  1082. DEF_OPR_IMPL_CTOR(LocalShareBase, OperatorBase);
  1083. DEF_OPR_PARAM(LocalShare);
  1084. protected:
  1085. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1086. TensorLayout& dst);
  1087. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1088. const TensorLayout& dst);
  1089. };
  1090. class LocalShareForward : public LocalShareBase,
  1091. public detail::MultiAlgoOpr<LocalShareForward, 3> {
  1092. DEF_OPR_IMPL(LocalShareForward, LocalShareBase, 2, 1);
  1093. public:
  1094. /**
  1095. * \param[in] src (N, IC, IH, IW)
  1096. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1097. * FH, FW, OC / G)
  1098. * \param[out] dst (N, OC, OH, OW)
  1099. */
  1100. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1101. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1102. /**
  1103. * \brief deduce layout of the ouput tensor
  1104. */
  1105. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1106. TensorLayout& dst);
  1107. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1108. const TensorLayout& filter,
  1109. const TensorLayout& dst) = 0;
  1110. protected:
  1111. void check_exec(const TensorLayout& src, const TensorLayout& filter,
  1112. const TensorLayout& dst, size_t workspace_in_bytes);
  1113. };
  1114. using LocalShare = LocalShareForward;
  1115. class LocalShareBackwardData
  1116. : public LocalShareBase,
  1117. public detail::MultiAlgoOpr<LocalShareBackwardData, 3> {
  1118. DEF_OPR_IMPL(LocalShareBackwardData, LocalShareBase, 2, 1);
  1119. public:
  1120. /**
  1121. * \param[in] filter (G, spatial_groups_h, spatial_groups_w, IC / G,
  1122. * FH, FW, OC / G)
  1123. * \param[in] diff (N, OC, OH, OW)
  1124. * \param[out] grad (N, IC, IH, IW)
  1125. */
  1126. virtual void exec(_megdnn_tensor_in filter, _megdnn_tensor_in diff,
  1127. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1128. virtual size_t get_workspace_in_bytes(const TensorLayout& filter,
  1129. const TensorLayout& diff,
  1130. const TensorLayout& grad) = 0;
  1131. void deduce_layout(const TensorLayout& filter, const TensorLayout& diff,
  1132. TensorLayout& grad);
  1133. protected:
  1134. void check_exec(const TensorLayout& filter, const TensorLayout& diff,
  1135. const TensorLayout& grad, size_t workspace_in_bytes);
  1136. };
  1137. class LocalShareBackwardFilter
  1138. : public LocalShareBase,
  1139. public detail::MultiAlgoOpr<LocalShareBackwardFilter, 3> {
  1140. DEF_OPR_IMPL(LocalShareBackwardFilter, LocalShareBase, 2, 1);
  1141. public:
  1142. /**
  1143. * \param[in] src (N, IC, IH, IW)
  1144. * \param[in] diff (N, OC, OH, OW)
  1145. * \param[out] grad (G, spatial_groups_h, spatial_groups_w, IC / G,
  1146. * FH, FW, OC / G)
  1147. */
  1148. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in diff,
  1149. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1150. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1151. const TensorLayout& diff,
  1152. const TensorLayout& grad) = 0;
  1153. protected:
  1154. void check_exec(const TensorLayout& src, const TensorLayout& diff,
  1155. const TensorLayout& grad, size_t workspace_in_bytes);
  1156. };
  1157. class ROIAlignBase : public OperatorBase {
  1158. DEF_OPR_IMPL_CTOR(ROIAlignBase, OperatorBase);
  1159. DEF_OPR_PARAM(ROIAlign);
  1160. protected:
  1161. void deduce_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  1162. TensorLayout& dst, TensorLayout& index);
  1163. void check_layout_fwd(const TensorLayout& src, const TensorLayout& rois,
  1164. const TensorLayout& dst, const TensorLayout& index);
  1165. };
  1166. class ROIAlignForward : public ROIAlignBase {
  1167. DEF_OPR_IMPL(ROIAlignForward, ROIAlignBase, 2, 2);
  1168. public:
  1169. /**
  1170. * \param[in] src (n, c, ih, iw)
  1171. * \param[in] rois (m, 5)
  1172. * \param[out] dst (m, c, oh, ow)
  1173. * \param[out] index (m, c, oh, ow) if mode is MAX, (0) if mode is AVERAGE
  1174. *
  1175. * Note that rois(, 0) denotes the input image index. We store it as
  1176. * a float, but it should be an integer instead.
  1177. *
  1178. * index is a temporary tensor to facilitate its backward operator.
  1179. * It is used to store argmax indicex in MAX mode, and it is not used
  1180. * in AVERAGE mode.
  1181. */
  1182. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in rois,
  1183. _megdnn_tensor_out dst, _megdnn_tensor_out index,
  1184. _megdnn_workspace workspace) = 0;
  1185. void deduce_layout(const TensorLayout& src, const TensorLayout& rois,
  1186. TensorLayout& dst, TensorLayout& index);
  1187. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1188. const TensorLayout& rois,
  1189. const TensorLayout& dst,
  1190. const TensorLayout& index) = 0;
  1191. protected:
  1192. void check_exec(const TensorLayout& src, const TensorLayout& rois,
  1193. const TensorLayout& dst, const TensorLayout& index,
  1194. size_t workspace_in_bytes);
  1195. };
  1196. using ROIAlign = ROIAlignForward;
  1197. class ROIAlignBackward : public ROIAlignBase {
  1198. DEF_OPR_IMPL(ROIAlignBackward, ROIAlignBase, 3, 1);
  1199. public:
  1200. /**
  1201. * \param[in] diff the backpropagated gradient wrt. dst
  1202. * \param[in] rois the `rois' parameter in ROIAlignForward::exec
  1203. * \param[in] index the `index' parameter in ROIAlignForward::exec
  1204. * \param[out] grad the backpropagated gradient wrt. src
  1205. */
  1206. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in rois,
  1207. _megdnn_tensor_in index, _megdnn_tensor_out grad,
  1208. _megdnn_workspace workspace) = 0;
  1209. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1210. const TensorLayout& rois,
  1211. const TensorLayout& index,
  1212. const TensorLayout& grad) = 0;
  1213. protected:
  1214. void check_exec(const TensorLayout& diff, const TensorLayout& rois,
  1215. const TensorLayout& index, const TensorLayout& grad,
  1216. size_t workspace_in_bytes);
  1217. };
  1218. class DeformableConvBase : public OperatorBase {
  1219. DEF_OPR_IMPL_CTOR(DeformableConvBase, OperatorBase);
  1220. DEF_OPR_PARAM(Convolution);
  1221. public:
  1222. static constexpr size_t MAX_SPATIAL_DIM = 2;
  1223. struct CanonizedFilterMeta : Convolution::CanonizedFilterMeta {
  1224. uint32_t deformable_group;
  1225. };
  1226. protected:
  1227. CanonizedFilterMeta make_canonized_filter_meta(
  1228. size_t src_ndim, const TensorLayout& filter,
  1229. const TensorLayout& offset) const;
  1230. void deduce_layout_fwd(const TensorLayout& im, const TensorLayout& filter,
  1231. const TensorLayout& mask, const TensorLayout& offset,
  1232. TensorLayout& dst);
  1233. void check_layout_fwd(const TensorLayout& src, const TensorLayout& filter,
  1234. const TensorLayout& mask, const TensorLayout& offset,
  1235. const TensorLayout& dst);
  1236. };
  1237. class DeformableConvForward
  1238. : public DeformableConvBase,
  1239. public detail::MultiAlgoOpr<DeformableConvForward, 5> {
  1240. DEF_OPR_IMPL(DeformableConvForward, DeformableConvBase, 4, 1);
  1241. public:
  1242. /**
  1243. * \param[in] im (n, ic, ih, iw)
  1244. * \param[in] filter (oc, ic, fh, fw)
  1245. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1246. * \param[in] mask (dg, fh, fw, oh, ow)
  1247. * \param[out] dst (n, oc, oh, ow)
  1248. */
  1249. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in filter,
  1250. _megdnn_tensor_in offset, _megdnn_tensor_in mask,
  1251. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1252. void deduce_layout(const TensorLayout& im, const TensorLayout& filter,
  1253. const TensorLayout& offset, const TensorLayout& mask,
  1254. TensorLayout& dst);
  1255. virtual size_t get_workspace_in_bytes(const TensorLayout& im,
  1256. const TensorLayout& filter,
  1257. const TensorLayout& offset,
  1258. const TensorLayout& mask,
  1259. const TensorLayout& dst) = 0;
  1260. protected:
  1261. CanonizedFilterMeta check_exec(const TensorLayout& im,
  1262. const TensorLayout& filter,
  1263. const TensorLayout& offset,
  1264. const TensorLayout& mask,
  1265. const TensorLayout& dst,
  1266. size_t workspace_in_bytes);
  1267. };
  1268. using DeformableConv = DeformableConvForward;
  1269. /**
  1270. * \brief DeformableConvBackwardFilter operator.
  1271. *
  1272. * Calculating the gradient wrt. convolution filter.
  1273. */
  1274. class DeformableConvBackwardFilter
  1275. : public DeformableConvBase,
  1276. public detail::MultiAlgoOpr<DeformableConvBackwardFilter, 5> {
  1277. DEF_OPR_IMPL(DeformableConvBackwardFilter, DeformableConvBase, 4, 1);
  1278. public:
  1279. /**
  1280. * \param[in] im (oc, ic, fh, fw)
  1281. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1282. * \param[in] mask (dg, fh, fw, oh, ow)
  1283. * \param[in] out_grad (n, oc, oh, ow)
  1284. * \param[out] filter_grad (oc, ic, ih, iw)
  1285. */
  1286. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in offset,
  1287. _megdnn_tensor_in mask, _megdnn_tensor_in out_grad,
  1288. _megdnn_tensor_out filter_grad,
  1289. _megdnn_workspace workspace) = 0;
  1290. virtual size_t get_workspace_in_bytes(const TensorLayout& im,
  1291. const TensorLayout& offset,
  1292. const TensorLayout& mask,
  1293. const TensorLayout& out_grad,
  1294. const TensorLayout& filter_grad) = 0;
  1295. void deduce_layout(const TensorLayout& im, const TensorLayout& offset,
  1296. const TensorLayout& mask, const TensorLayout& out_grad,
  1297. TensorLayout& filter_grad);
  1298. protected:
  1299. CanonizedFilterMeta check_exec(const TensorLayout& im,
  1300. const TensorLayout& offset,
  1301. const TensorLayout& mask,
  1302. const TensorLayout& out_grad,
  1303. const TensorLayout& filter_grad,
  1304. size_t workspace_in_bytes);
  1305. };
  1306. /**
  1307. * \brief DeformableConvBackwardData operator.
  1308. *
  1309. * Calculating the gradient wrt. convolution input data, offset and mask.
  1310. */
  1311. class DeformableConvBackwardData
  1312. : public DeformableConvBase,
  1313. public detail::MultiAlgoOpr<DeformableConvBackwardData, 8> {
  1314. DEF_OPR_IMPL(DeformableConvBackwardData, DeformableConvBase, 5, 3);
  1315. public:
  1316. /**
  1317. * \param[in] im (oc, ic, fh, fw)
  1318. * \param[in] filter (oc, ic, fh, fw)
  1319. * \param[in] offset (dg, 2, fh, fw, oh, ow)
  1320. * \param[in] mask (dg, fh, fw, oh, ow)
  1321. * \param[in] out_grad (n, oc, oh, ow)
  1322. * \param[out] im_grad (n, ic, ih, iw)
  1323. * \param[out] offset_grad (dg, 2, fh, fw, oh, ow)
  1324. * \param[out] mask_grad (dg, fh, fw, oh, ow)
  1325. */
  1326. virtual void exec(_megdnn_tensor_in im, _megdnn_tensor_in filter,
  1327. _megdnn_tensor_in offset, _megdnn_tensor_in mask,
  1328. _megdnn_tensor_in out_grad, _megdnn_tensor_out im_grad,
  1329. _megdnn_tensor_out offset_grad,
  1330. _megdnn_tensor_out mask_grad,
  1331. _megdnn_workspace workspace) = 0;
  1332. virtual size_t get_workspace_in_bytes(
  1333. const TensorLayout& im, const TensorLayout& filter,
  1334. const TensorLayout& offset, const TensorLayout& mask,
  1335. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1336. const TensorLayout& offset_grad, const TensorLayout& mask_grad) = 0;
  1337. void deduce_layout(const TensorLayout& im, const TensorLayout& filter,
  1338. const TensorLayout& offset, const TensorLayout& mask,
  1339. const TensorLayout& out_grad, TensorLayout& im_grad,
  1340. TensorLayout& offset_grad, TensorLayout& mask_grad);
  1341. protected:
  1342. CanonizedFilterMeta check_exec(
  1343. const TensorLayout& im, const TensorLayout& filter,
  1344. const TensorLayout& offset, const TensorLayout& mask,
  1345. const TensorLayout& out_grad, const TensorLayout& im_grad,
  1346. const TensorLayout& offset_grad, const TensorLayout& mask_grad,
  1347. size_t workspace_in_bytes);
  1348. };
  1349. class DeformablePSROIPoolingBase : public OperatorBase {
  1350. DEF_OPR_IMPL_CTOR(DeformablePSROIPoolingBase, OperatorBase);
  1351. DEF_OPR_PARAM(DeformablePSROIPooling);
  1352. protected:
  1353. void deduce_layout_fwd(const TensorLayout& data, const TensorLayout& trans,
  1354. const TensorLayout& rois, TensorLayout& out_data,
  1355. TensorLayout& out_count);
  1356. void check_layout_fwd(const TensorLayout& data, const TensorLayout& trans,
  1357. const TensorLayout& rois,
  1358. const TensorLayout& out_data,
  1359. const TensorLayout& out_count,
  1360. size_t workspace_in_bytes);
  1361. };
  1362. class DeformablePSROIPoolingForward : public DeformablePSROIPoolingBase {
  1363. DEF_OPR_IMPL(DeformablePSROIPoolingForward, DeformablePSROIPoolingBase, 3,
  1364. 2);
  1365. public:
  1366. /**
  1367. * \param[in] data (oc, ic, ih, iw)
  1368. * \param[in] rois (xx, xx, xx, xx)
  1369. * \param[in] trans (oc, ic, fh, fw)
  1370. * \param[out] out_data ( n, ic, ih, iw)
  1371. * \param[out] out_count ( n, ic, ih, iw)
  1372. */
  1373. virtual size_t get_workspace_in_bytes(const TensorLayout& data,
  1374. const TensorLayout& rois,
  1375. const TensorLayout& trans,
  1376. const TensorLayout& out_data,
  1377. const TensorLayout& out_count) = 0;
  1378. virtual void exec(_megdnn_tensor_in data, _megdnn_tensor_in rois,
  1379. _megdnn_tensor_in trans, _megdnn_tensor_out out_data,
  1380. _megdnn_tensor_out out_count,
  1381. _megdnn_workspace workspace) = 0;
  1382. void deduce_layout(const TensorLayout& data, const TensorLayout& rois,
  1383. const TensorLayout& trans, TensorLayout& out_data,
  1384. TensorLayout& out_count);
  1385. void check_exec(const TensorLayout& data, const TensorLayout& rois,
  1386. const TensorLayout& trans, const TensorLayout& out_data,
  1387. const TensorLayout& out_count, size_t workspace_in_bytes);
  1388. };
  1389. using DeformablePSROIPooling = DeformablePSROIPoolingForward;
  1390. class DeformablePSROIPoolingBackward : public DeformablePSROIPoolingBase {
  1391. DEF_OPR_IMPL(DeformablePSROIPoolingBackward, DeformablePSROIPoolingBase, 5,
  1392. 2);
  1393. public:
  1394. /**
  1395. * \param[in] data (oc, ic, ih, iw)
  1396. * \param[in] rois (xx, xx, xx, xx)
  1397. * \param[in] trans (oc, ic, fh, fw)
  1398. * \param[in] out_diff (xx, xx, xx, xx)
  1399. * \param[in] out_count (xx, xx, xx, xx)
  1400. * \param[out] data_diff ( n, ic, ih, iw)
  1401. * \param[out] trans_diff ( n, ic, ih, iw)
  1402. */
  1403. virtual void exec(_megdnn_tensor_in data, _megdnn_tensor_in rois,
  1404. _megdnn_tensor_in trans, _megdnn_tensor_in out_diff,
  1405. _megdnn_tensor_in out_count, _megdnn_tensor_out data_diff,
  1406. _megdnn_tensor_out trans_diff,
  1407. _megdnn_workspace workspace) = 0;
  1408. virtual size_t get_workspace_in_bytes(const TensorLayout& data,
  1409. const TensorLayout& rois,
  1410. const TensorLayout& trans,
  1411. const TensorLayout& out_diff,
  1412. const TensorLayout& out_count,
  1413. const TensorLayout& data_diff,
  1414. const TensorLayout& trans_diff) = 0;
  1415. void check_exec(const TensorLayout& data, const TensorLayout& rois,
  1416. const TensorLayout& trans, const TensorLayout& out_diff,
  1417. const TensorLayout& out_count,
  1418. const TensorLayout& data_diff,
  1419. const TensorLayout& trans_diff, size_t workspace_in_bytes);
  1420. };
  1421. class BatchConvBiasForward
  1422. : public ConvolutionBase<param::BatchConvBias>,
  1423. public detail::MultiAlgoOpr<BatchConvBiasForward, 5> {
  1424. DEF_OPR_IMPL(BatchConvBiasForward, ConvolutionBase, 4, 1);
  1425. public:
  1426. virtual void exec(_megdnn_tensor_in src, _megdnn_tensor_in filter,
  1427. _megdnn_tensor_in bias, _megdnn_tensor_in z,
  1428. _megdnn_tensor_out dst, _megdnn_workspace workspace) = 0;
  1429. void deduce_dtype(DType src, DType filter, DType bias, DType z, DType& dst);
  1430. void deduce_layout(const TensorLayout& src, const TensorLayout& filter,
  1431. const TensorLayout& bias, const TensorLayout& z,
  1432. TensorLayout& dst);
  1433. virtual size_t get_workspace_in_bytes(const TensorLayout& src,
  1434. const TensorLayout& filter,
  1435. const TensorLayout& bias,
  1436. const TensorLayout& z,
  1437. const TensorLayout& dst) = 0;
  1438. protected:
  1439. CanonizedFilterMeta check_exec(const TensorLayout& src,
  1440. const TensorLayout& filter,
  1441. const TensorLayout& bias,
  1442. const TensorLayout& z,
  1443. const TensorLayout& dst,
  1444. size_t workspace_in_bytes);
  1445. };
  1446. using BatchConvBias = BatchConvBiasForward;
  1447. class FakeQuantBase : public OperatorBase {
  1448. DEF_OPR_IMPL_CTOR(FakeQuantBase, OperatorBase);
  1449. DEF_OPR_PARAM(FakeQuant);
  1450. protected:
  1451. void deduce_layout_fwd(const TensorLayout& input, TensorLayout& output);
  1452. void check_layout_fwd(const TensorLayout& input, const TensorLayout& scale,
  1453. const TensorLayout& zero_point,
  1454. const TensorLayout& output);
  1455. };
  1456. class FakeQuantForward : public FakeQuantBase {
  1457. DEF_OPR_IMPL(FakeQuantForward, FakeQuantBase, 3, 1);
  1458. public:
  1459. virtual void exec(_megdnn_tensor_in input, _megdnn_tensor_in scale,
  1460. _megdnn_tensor_in zero_point, _megdnn_tensor_out output,
  1461. _megdnn_workspace workspace) = 0;
  1462. void deduce_layout(const TensorLayout& input, const TensorLayout& scale,
  1463. const TensorLayout& zero_point, TensorLayout& output);
  1464. virtual size_t get_workspace_in_bytes(const TensorLayout& input,
  1465. const TensorLayout& scale,
  1466. const TensorLayout& zero_point,
  1467. const TensorLayout& output) = 0;
  1468. protected:
  1469. void check_exec(const TensorLayout& input, const TensorLayout& scale,
  1470. const TensorLayout& zero_point, const TensorLayout& output,
  1471. size_t workspace_in_bytes);
  1472. };
  1473. using FakeQuant = FakeQuantForward;
  1474. class FakeQuantBackward : public FakeQuantBase {
  1475. DEF_OPR_IMPL(FakeQuantBackward, FakeQuantBase, 4, 1);
  1476. public:
  1477. virtual void exec(_megdnn_tensor_in diff, _megdnn_tensor_in input,
  1478. _megdnn_tensor_in scale, _megdnn_tensor_in zero_point,
  1479. _megdnn_tensor_out grad, _megdnn_workspace workspace) = 0;
  1480. virtual size_t get_workspace_in_bytes(const TensorLayout& diff,
  1481. const TensorLayout& input,
  1482. const TensorLayout& scale,
  1483. const TensorLayout& zero_point,
  1484. const TensorLayout& grad) = 0;
  1485. protected:
  1486. void check_exec(const TensorLayout& diff, const TensorLayout& input,
  1487. const TensorLayout& scale, const TensorLayout& zero_point,
  1488. const TensorLayout& grad, size_t workspace_in_bytes);
  1489. };
  1490. } // namespace megdnn
  1491. #include "megdnn/internal/opr_header_epilogue.h"
  1492. // vim: syntax=cpp.doxygen

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