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_pooling_ops.h 30 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef GE_OP_NN_POOLING_OPS_H
  17. #define GE_OP_NN_POOLING_OPS_H
  18. #include "graph/operator_reg.h"
  19. #include "graph/operator.h"
  20. namespace ge {
  21. /**
  22. *@brief Performs pooling on the input.
  23. *@par Inputs:
  24. *@li x: An NCHW tensor of type float16, float32, int8.
  25. *@par Attributes:
  26. *@li mode: An optional int32, specifying the pooling algorithm, either "1" (max pooling) or "0" (avg pooling). Defaults to "0".
  27. *@li global_pooling: An optional bool. Defaults to "false".
  28. *@li window: Optional, including: \n
  29. *window[0]: An optional int32, specifying the window size along in the H dimension. The value range is [1, 32768]. Defaults to "1". \n
  30. *window[1]: An optional int32, specifying the window size along in the W dimension. The value range is [1, 32768]. Defaults to "1". \n
  31. *@li stride: Optional, including: \n
  32. *stride[0]: An optional int32, specifying the stride along in the H dimension. The value range is [1, 63]. Defaults to "1". \n
  33. *stride[1]: An optional int32, specifying the stride along in the W dimension. The value range is [1, 63]. Defaults to "1". \n
  34. *@li pad: Optional, including: \n
  35. *pad[0]: An optional int32, specifying the up padding. Defaults to "0". \n
  36. *pad[1]: An optional int32, specifying the bottom padding. Defaults to "0". \n
  37. *pad[2]: An optional int32, specifying the left padding. Defaults to "0". \n
  38. *pad[3]: An optional int32, specifying the right padding. Defaults to "0". \n
  39. *@li dilation: Optional, including: \n
  40. *dilation[0]: An optional int32, specifying the up dilation. Defaults to "1". \n
  41. *dilation[1]: An optional int32, specifying the bottom dilation. Defaults to "1". \n
  42. *dilation[2]: An optional int32, specifying the left dilation. Defaults to "1". \n
  43. *dilation[3]: An optional int32, specifying the right dilation. Defaults to "1". \n
  44. *@li ceil_mode: An optional int32, either "0" (ceil mode) or "1" (floor mode). Defaults to "0".
  45. *@par Outputs:
  46. *y: An NCHW tensor of type float16, float32, int32.
  47. *@attention Constraints:\n
  48. *@li window[0] * window[1] < 256;
  49. *@li 1<=input_h<=4096,1<=input_w<=4096
  50. *@li If input tensor N is a prime number, it should be less than 65535.
  51. *@par Third-party framework compatibility
  52. *@li Compatible with the Caffe operator Pooling.
  53. *@li Compatible with the TensorFlow operator Pooling.
  54. */
  55. REG_OP(Pooling)
  56. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_INT8}))
  57. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_INT32}))
  58. .ATTR(mode, Int, 0) // 0:max pooling or 1:avg pooling
  59. .ATTR(global_pooling, Bool, false)
  60. .ATTR(window, ListInt, {1,1}) // kernel size
  61. .ATTR(stride, ListInt, {1,1}) // stride size
  62. .ATTR(pad, ListInt, {0,0,0,0}) // pad size
  63. .ATTR(dilation, ListInt, {1,1,1,1})
  64. .ATTR(ceil_mode, Int, 0)
  65. .OP_END_FACTORY_REG(Pooling)
  66. /**
  67. *@brief Performs average pooling on the input.
  68. *@par Inputs:
  69. *x: A tensor of type float16, float32, double.
  70. *@par Attributes:
  71. *@li ksize: A required list of 4 ints, specifying the size (N, C, H, and W) of the sliding window, where N = C = 1, and H and W are positive integers within the range [1, 32768].
  72. *@li strides: A required list of 4 ints, specifying the stride of the sliding window. The strides of the N and C dimensions are 1. The strides of the H and W dimensions are positive integers within the range [1, 63].
  73. *@li padding: A required string, specifying the padding algorithm, either "VALID" or "SAME". With "SAME" means that the outputs will have the same spatial dimensions as its inputs. With "VALID" means no padding.
  74. *@li data_format: An optional string, specifying the data format of "ksize" and "strides", either "NCHW", "NC1HWC0", or "NHWC" (default).
  75. *@par Outputs:
  76. *y: The average pooled output tensor. Has the same type and format as input "x".
  77. *@attention Constraints:
  78. *@li This operator applies only to a TensorFlow network.
  79. *@li Only single input and single output are supported.
  80. *@li Global pooling is supported.
  81. *@li "ksize_H" and "ksize_W" are positive integers within the range [1, 32768]. ksize_H * ksize_W < 256
  82. *@li Due to instruction restrictions, the values of "strides_h" and "strides_w" are positive integers within the range [1, 63].
  83. *@par Third-party framework compatibility
  84. * Compatible with the TensorFlow operator AvgPool.
  85. */
  86. REG_OP(AvgPool)
  87. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  88. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  89. .REQUIRED_ATTR(ksize, ListInt)
  90. .REQUIRED_ATTR(strides, ListInt)
  91. .REQUIRED_ATTR(padding, String)
  92. .ATTR(data_format, String, "NHWC")
  93. .OP_END_FACTORY_REG(AvgPool)
  94. /**
  95. *@brief Performs max_pool_ext2 on the input.
  96. *@par Inputs:
  97. * One input:
  98. *x: An NC1HWC0 Tensor of type float16.
  99. *@par Attributes:
  100. *@li ksize: A required list of int8, int16, int32, or int64 values, specifying the size of the window for each dimension of the input tensor. No default value.
  101. *@li strides: A required list of int8, int16, int32, or int64 values, specifying the stride of the sliding window for each dimension of the input tensor. No default value.
  102. *@li padding: A required string. No default value.
  103. *@li data_format: An optional string. Defaults to "NC1HWC0".
  104. *@par Outputs:
  105. *y: A Tensor. Has the same type and format as input "x".
  106. *@attention Constraints:
  107. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1, ksize[1] * ksize[2] <= 255.
  108. *@li "stride is a list that has length 4: strides[0] = 1 or strides[3] = 1, strides[1] <= 63, strides[0] >= 1, strides[2] <= 63, strides[2] >= 1.
  109. *@li "padding" is either "SAME" or "VALID".
  110. *@par Third-party framework compatibility
  111. * Compatible with the TensorFlow operator MaxPoolV2.
  112. */
  113. REG_OP(MaxPoolExt2)
  114. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  115. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  116. DT_UINT16, DT_QINT8}))
  117. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  118. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  119. DT_UINT16, DT_QINT8}))
  120. .REQUIRED_ATTR(ksize, ListInt)
  121. .REQUIRED_ATTR(strides, ListInt)
  122. .REQUIRED_ATTR(padding, String)
  123. .ATTR(data_format, String, "NHWC")
  124. .OP_END_FACTORY_REG(MaxPoolExt2)
  125. /**
  126. *@brief Performs max pooling on the input.
  127. *@par Inputs:
  128. * One input:
  129. *x: An NC1HWC0 Tensor. Supported type:float16, float32, double, int8, int16,
  130. * int32, int64, uint8, uint16, qint8
  131. *@par Attributes:
  132. *@li ksize: A required list of int8, int16, int32, or int64 values,
  133. * specifying the size of the window for each dimension of the input tensor.
  134. * No default value.
  135. *@li strides: A required list of int8, int16, int32, or int64 values,
  136. * specifying the stride of the sliding window for each dimension of
  137. * the input tensor. No default value.
  138. *@li padding: A required string. No default value.
  139. *@li data_format: An optional string. Defaults to "NHWC".
  140. *@par Outputs:
  141. *y: A Tensor. Has the same type and format as input "x".
  142. *@attention Constraints:
  143. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1,
  144. * ksize[1] * ksize[2] <= 255.
  145. *@li "stride is a list that has length 4: strides[0] = 1 or strides[3] = 1,
  146. * strides[1] <= 63, strides[0] >= 1, strides[2] <= 63, strides[2] >= 1.
  147. *@li "padding" is either "SAME" or "VALID".
  148. *@par Third-party framework compatibility
  149. * Compatible with the TensorFlow operator MaxPool.
  150. */
  151. REG_OP(MaxPool)
  152. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  153. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  154. DT_UINT16, DT_QINT8}))
  155. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  156. DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  157. .REQUIRED_ATTR(ksize, ListInt)
  158. .REQUIRED_ATTR(strides, ListInt)
  159. .REQUIRED_ATTR(padding, String)
  160. .ATTR(data_format, String, "NHWC")
  161. .OP_END_FACTORY_REG(MaxPool)
  162. REG_OP(MaxPool3D)
  163. .INPUT(x, TensorType({DT_FLOAT16}))
  164. .OUTPUT(y, TensorType({DT_FLOAT16}))
  165. .REQUIRED_ATTR(ksize, ListInt)
  166. .REQUIRED_ATTR(strides, ListInt)
  167. .REQUIRED_ATTR(padding, String)
  168. .ATTR(pads, ListInt, {0,0,0})
  169. .ATTR(dilation, ListInt, {0,0,0})
  170. .ATTR(ceil_mode, Int, 0)
  171. .ATTR(data_format, String, "NDHWC")
  172. .OP_END_FACTORY_REG(MaxPool3D)
  173. /**
  174. * @brief Computes gradients of the maxpooling function.
  175. * @par Inputs:
  176. * @li x1: A mutable NC1HWC0 tensor of type RealNumberType.
  177. * @li x2: A mutable NC1HWC0 tensor of type RealNumberTypex.
  178. * @li grad: A mutable NC1HWC0 tensor of type RealNumberType.
  179. * @par Attributes:
  180. * @li ksize: A required tuple or list, specifying the size of the window for
  181. * each dimension of the input tensor.
  182. * @li strides: A required tuple or list, specifying the stride of the sliding
  183. * window for each dimension of the input tensor.
  184. * @li padding: A required string, specifying the type of padding algorithm
  185. * to use.
  186. * @li data_format: An optional string, Specify the data format of the input and
  187. * output data. With the default format "NHWC".
  188. * @par Outputs:
  189. * y: A mutable tensor. Has the same shape and type as "x1".
  190. * @attention Constraints:
  191. * @li Computing gradients of global pooling is not supported, which means
  192. * "ksize < x1".
  193. * @li "ksize" is in the range [1, 255]. "strides" is in the range [1, 63]
  194. * @par Third-party framework compatibility
  195. * Compatible with the TensorFlow operator MaxPoolGrad.
  196. */
  197. REG_OP(MaxPoolGrad)
  198. .INPUT(x1, TensorType::RealNumberType())
  199. .INPUT(x2, TensorType::RealNumberType())
  200. .INPUT(grad, TensorType::RealNumberType())
  201. .OUTPUT(y, TensorType::RealNumberType())
  202. .REQUIRED_ATTR(ksize, ListInt)
  203. .REQUIRED_ATTR(strides, ListInt)
  204. .REQUIRED_ATTR(padding, String)
  205. .ATTR(data_format, String, "NHWC")
  206. .OP_END_FACTORY_REG(MaxPoolGrad)
  207. /**
  208. * @brief Computes second-order gradients of the maxpooling function.
  209. * @par Inputs:
  210. * @li x1: Original forward input tensor of type RealNumberType
  211. * @li x2: Original forward output tensor of type RealNumberType
  212. * @li grad: Gradient tensor of type RealNumberType
  213. * @par Attributes:
  214. * @li ksize: A required list or tuple,
  215. * specifying the size of the sliding window.
  216. * @li strides: A required list or tuple,
  217. * specifying the stride of the sliding window.
  218. * @li padding: A required string, window sliding mode. Either SAME or VALID.
  219. * @li data_format: An optional string.
  220. * Format of the original input, either NCHW or NHWC. Defaults to NHWC.
  221. * @attention Constraints:
  222. * @li Only the Ascend 910 platform is supported.
  223. * @li "x1" and "grads" must have the same shape.
  224. * @li "x2" and "y" must have the same shape. Otherwise, an error is reported.
  225. * @li "x1", "x2", "grads", and "y" must be 5D tensors.
  226. * @li ksize[H] and ksize[W] is in the range [1, 255].
  227. * @li strides[H] and strides[W] is in the range [1, 63].
  228. * @li Other dimensions of ksize and strides is 1.
  229. * @par Outputs:
  230. * @li y: Result tensor of type RealNumberType
  231. * @par Third-party framework compatibility
  232. * @li Compatible with the TensorFlow operator MaxPoolGradGrad.
  233. */
  234. REG_OP(MaxPoolGradGrad)
  235. .INPUT(x1, TensorType::RealNumberType())
  236. .INPUT(x2, TensorType::RealNumberType())
  237. .INPUT(grad, TensorType::RealNumberType())
  238. .OUTPUT(y, TensorType::RealNumberType())
  239. .REQUIRED_ATTR(ksize, ListInt)
  240. .REQUIRED_ATTR(strides, ListInt)
  241. .REQUIRED_ATTR(padding, String)
  242. .ATTR(data_format, String, "NHWC")
  243. .OP_END_FACTORY_REG(MaxPoolGradGrad)
  244. /**
  245. *@brief Performs max_pool_ext2 on the input.
  246. *@par Inputs:
  247. * Two inputs:
  248. *@li x: An NC1HWC0 Tensor of type float16.
  249. *@li strides: A required type of int32 values, specifying the stride of the sliding window for each dimension of the input tensor. No default value.
  250. *@li ksize: A required type of int32 values, specifying the size of the window for each dimension of the input tensor. No default value.
  251. *@par Attributes:
  252. *@li padding: A required string. No default value.
  253. *@li data_format: An optional string. Defaults to "NC1HWC0".
  254. *@par Outputs:
  255. *y: A Tensor. Has the same type and format as input "x".
  256. *@attention Constraints:
  257. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1, ksize[1] * ksize[2] <= 255.
  258. *@li "stride is a list that has length 4: strides[0] = 1 or strides[3] = 1, strides[1] <= 63, strides[0] >= 1, strides[2] <= 63, strides[2] >= 1.
  259. *@li "padding" is either "SAME" or "VALID".
  260. *@par Third-party framework compatibility
  261. * Compatible with the TensorFlow operator MaxPoolV2.
  262. */
  263. REG_OP(MaxPoolV2)
  264. .INPUT(x, TensorType({DT_FLOAT16}))
  265. .INPUT(ksize, TensorType({DT_INT32}))
  266. .INPUT(strides, TensorType({DT_INT32}))
  267. .OUTPUT(y, TensorType({DT_FLOAT16}))
  268. .REQUIRED_ATTR(padding, String)
  269. .ATTR(data_format, String, "NHWC")
  270. .OP_END_FACTORY_REG(MaxPoolV2)
  271. /**
  272. *@brief Performs max pooling on the input and outputs both max values and
  273. * indices.
  274. *@par Inputs:
  275. * One input:
  276. *x: An NC1HWC0 Tensor. Supported type: float, double, int32,
  277. * uint8, int16, int8, int64, uint16, half, uint32, uint64.
  278. *@par Attributes:
  279. *@li ksize: A required list of int8, int16, int32, or int64 values,
  280. * specifying the size of the window for each dimension of the input tensor.
  281. * No default value.
  282. *@li strides: A required list of int8, int16, int32, or int64 values,
  283. * specifying the stride of the sliding window for each dimension of
  284. * the input tensor. No default value.
  285. *@li padding: A required string. No default value.
  286. *@par Outputs:
  287. *y: A Tensor. Has the same type and format as input "x".
  288. *argmax: A Tensor. Has the same type and format as input "x".
  289. *@attention Constraints:
  290. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1,
  291. * ksize[1] * ksize[2] <= 255.
  292. *@li "stride is a list that has length 4: strides[0] = 1 or strides[3] = 1,
  293. * strides[1] <= 63, strides[0] >= 1, strides[2] <= 63, strides[2] >= 1.
  294. *@li "padding" is either "SAME" or "VALID".
  295. *@par Third-party framework compatibility
  296. * Compatible with the TensorFlow operator MaxPoolWithArgmax.
  297. */
  298. REG_OP(MaxPoolWithArgmax)
  299. .INPUT(x, TensorType::RealNumberType())
  300. .OUTPUT(y, TensorType::RealNumberType())
  301. .OUTPUT(argmax, TensorType::IndexNumberType())
  302. .REQUIRED_ATTR(ksize, ListInt)
  303. .REQUIRED_ATTR(strides, ListInt)
  304. .REQUIRED_ATTR(padding, String)
  305. .ATTR(Targmax, Int, 7)
  306. .OP_END_FACTORY_REG(MaxPoolWithArgmax)
  307. /**
  308. *@brief Performs the backpropagation of MaxPoolWithArgmax.
  309. *@par Inputs:
  310. * Three inputs, including:
  311. *@li x: An NC1HWC0 tensor. Supported type: float, double, int32,
  312. * uint8, int16, int8, int64, uint16, half, uint32, uint64.
  313. *@li grad: An NC1HWC0 tensor. Supported type: float, double, int32,
  314. * uint8, int16, int8, int64, uint16, half, uint32, uint64.
  315. *@li argmx: An NC1HWC0 tensor of type int32 or int64.
  316. *@par Attributes:
  317. *@li ksize: A required list of int8, int16, int32, or int64 values,
  318. * specifying the size of the window for each dimension of the input tensor.
  319. * No default value.
  320. *@li strides: A required list of int8, int16, int32, or int64 values,
  321. * specifying the stride of the sliding window for each dimension of
  322. * the input tensor. No default value.
  323. *@li padding: A required string. No default value.
  324. *@par Outputs:
  325. *y: A Tensor. Has the same type and format as input "x".
  326. *@attention Constraints:
  327. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1,
  328. * ksize[1] * ksize[2] <= 255.
  329. *@li "strides" is a list that has length 4: strides[0] = 1 or strides[3] = 1
  330. *@li "padding" is either "SAME" or "VALID".
  331. *@see max_pool_with_argmax
  332. *@par Third-party framework compatibility
  333. * Compatible with the TensorFlow operator MaxPoolGradWithArgmax.
  334. */
  335. REG_OP(MaxPoolGradWithArgmax)
  336. .INPUT(x, TensorType::RealNumberType())
  337. .INPUT(grad, TensorType::RealNumberType())
  338. .INPUT(argmax, TensorType::IndexNumberType())
  339. .OUTPUT(y, TensorType::RealNumberType())
  340. .REQUIRED_ATTR(ksize, ListInt)
  341. .REQUIRED_ATTR(strides, ListInt)
  342. .REQUIRED_ATTR(padding, String)
  343. .OP_END_FACTORY_REG(MaxPoolGradWithArgmax)
  344. /**
  345. * @brief Computes second-order gradients of the maxpooling function.
  346. * @par Inputs:
  347. * @li x: Original forward input tensor of type RealNumberType
  348. * @li grad: Gradient tensor of type RealNumberType
  349. * @li argmax: An tensor of type IndexNumberType
  350. * @par Attributes:
  351. * @li ksize: A required list, specifying the size of the sliding window.
  352. * @li strides: A required list, specifying the stride of the sliding window.
  353. * @li padding: A required string, window sliding mode. Either SAME or VALID.
  354. * @par Outputs:
  355. * @li y:Result tensor of type RealNumberType
  356. * @attention Constraints:
  357. * @li Only the cloud platform is supported.
  358. * @li "x1" and "grads" must have the same shape.
  359. * @li length of the shape of x, grads, argmax, y must be 5.
  360. * @li shape of argmax must be (fmap_n, fmap_c1, kernel_h * kernel_w,
  361. * (shape_max_pool[2] * shape_max_pool[3] + 15) // 16 * 16, 1),
  362. * or (fmap_n, fmap_c1, kernel_h * kernel_w,
  363. * (shape_max_pool[2] * shape_max_pool[3] + 31) // 16, 16), else failed.
  364. * @par Third-party framework compatibility
  365. * @li Compatible with the TensorFlow operator MaxPoolGradGradWithArgmax.
  366. */
  367. REG_OP(MaxPoolGradGradWithArgmax)
  368. .INPUT(x, TensorType::RealNumberType())
  369. .INPUT(grad, TensorType::RealNumberType())
  370. .INPUT(argmax, TensorType::IndexNumberType())
  371. .OUTPUT(y, TensorType::RealNumberType())
  372. .REQUIRED_ATTR(ksize, ListInt)
  373. .REQUIRED_ATTR(strides, ListInt)
  374. .REQUIRED_ATTR(padding, String)
  375. .OP_END_FACTORY_REG(MaxPoolGradGradWithArgmax)
  376. /**
  377. * @brief Computes avgpoograd function.
  378. * @par Inputs:
  379. * @li orig_input_shape: An NHWC tensor of type int32.
  380. * @li input_grad: An NHWC tensor of type float16, float32, or double.
  381. * @par Attributes:
  382. * @li ksize: A required tuple or list, specifying the size of the window for
  383. * each dimension of the input tensor.
  384. * @li strides: A required tuple or list, specifying the stride of the sliding
  385. * window for each dimension of the input tensor.
  386. * @li padding: A required string, specifying the type of
  387. * the padding algorithm to use.
  388. * @li data_format: An optional string. Defaults to "NHWC".
  389. * @par Outputs:
  390. * @out_grad: A mutable tensor with the same shape and type as "orig_input".
  391. * @par Third-party framework compatibility
  392. * @li Compatible with the TensorFlow operator AvgPoolGrad.
  393. */
  394. REG_OP(AvgPoolGrad)
  395. .INPUT(orig_input_shape, TensorType({DT_INT32}))
  396. .INPUT(input_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  397. .OUTPUT(out_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  398. .REQUIRED_ATTR(ksize, ListInt)
  399. .REQUIRED_ATTR(strides, ListInt)
  400. .REQUIRED_ATTR(padding, String)
  401. .ATTR(data_format, String, "NHWC")
  402. .OP_END_FACTORY_REG(AvgPoolGrad)
  403. /**
  404. * @brief Computes gradients of average pooling function.
  405. * @par Inputs:
  406. * @input_grad: An NHWC tensor of type float16, float32, or double.
  407. * @par Attributes:
  408. * @li orig_input_shape: A required Original input dimensions.
  409. * @li ksize: A required tuple or list, specifying the size of the window
  410. * for each dimension of the input tensor.
  411. * @li strides: A required tuple or list, specifying the stride of
  412. * the sliding window for each dimension of the input tensor.
  413. * @li padding: A required string, specifying the type of the padding algorithm
  414. * to use.
  415. * @li data_format: An optional string. Defaults to "NHWC".
  416. * @par Outputs:
  417. * @out_grad: A mutable tensor with the same shape and type as "orig_input".
  418. */
  419. REG_OP(AvgPoolGradD)
  420. .INPUT(input_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  421. .OUTPUT(out_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  422. .REQUIRED_ATTR(orig_input_shape, ListInt)
  423. .REQUIRED_ATTR(ksize, ListInt)
  424. .REQUIRED_ATTR(strides, ListInt)
  425. .REQUIRED_ATTR(padding, String)
  426. .ATTR(data_format, String, "NHWC")
  427. .OP_END_FACTORY_REG(AvgPoolGradD)
  428. REG_OP(MaxPoolWithArgmaxCCE)
  429. .INPUT(x, TensorType::ALL())
  430. .OUTPUT(y, TensorType::ALL())
  431. .OUTPUT(argmax, TensorType::ALL())
  432. .ATTR(mode, Int, 0)
  433. .ATTR(pad_mode, Int, 0)
  434. .ATTR(window, ListInt, {1,1})
  435. .ATTR(stride, ListInt, {1,1})
  436. .ATTR(pad, ListInt, {0,0,0,0})
  437. .ATTR(ceil_mode, Int, 0)
  438. .ATTR(data_mode, Int, 1)
  439. .ATTR(nan_opt, Int, 0)
  440. .OP_END_FACTORY_REG(MaxPoolWithArgmaxCCE)
  441. REG_OP(MaxPoolGradWithArgmaxCCE)
  442. .INPUT(x, TensorType::ALL())
  443. .INPUT(grad,TensorType::ALL())
  444. .INPUT(arg,TensorType::ALL())
  445. .OUTPUT(output,TensorType::ALL())
  446. .ATTR(mode, Int, 0)
  447. .ATTR(max_pool_grad_output_shape, ListInt, {0,0,0,0})
  448. .ATTR(pad_mode, Int, 0)
  449. .ATTR(window, ListInt, {1,1})
  450. .ATTR(stride, ListInt, {1,1})
  451. .ATTR(pad, ListInt, {0,0,0,0})
  452. .ATTR(ceil_mode, Int, 0)
  453. .ATTR(data_mode, Int, 1)
  454. .ATTR(nan_opt, Int, 0)
  455. .OP_END_FACTORY_REG(MaxPoolGradWithArgmaxCCE)
  456. /**
  457. *@brief :upsample the layer
  458. *@par Inputs:
  459. * one input, including:
  460. *@li x: A tensor of type float16 or float32.
  461. *@par Attributes:
  462. *@li scale: A optional float, scale factor of x. Defaults to "1.0".
  463. *@li stride_h: An optional int32, broadcast the axis of h. Defaults to "2".
  464. *@li stride_w: An optional int32, broadcast the axis of w. Defaults to "2".
  465. *@par Outputs:
  466. *y: A tensor of type float16 or float32.
  467. */
  468. REG_OP(Upsample)
  469. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  470. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  471. .ATTR(scale, Float, 1)
  472. .ATTR(stride_h, Int, 2)
  473. .ATTR(stride_w, Int, 2)
  474. .OP_END_FACTORY_REG(Upsample)
  475. /**
  476. *@brief Computes gradient of the FractionalMaxPool function.
  477. *@par Inputs:
  478. *Inputs include: \n
  479. * @li orig_input: A Tensor. Must be one of the following types: float32, float64, int32, int64.
  480. * @li orig_output: A Tensor. Must have the same type as orig_input.
  481. * @li out_backprop: A Tensor. Must have the same type as orig_input. \n
  482. 4-D with shape [batch, height, width, channels].
  483. * @li row_pooling_sequence: A Tensor of type int64.
  484. * @li col_pooling_sequence: A Tensor of type int64.
  485. *@par Attributes:
  486. *overlapping: An optional bool. Defaults to False.
  487. *@par Outputs:
  488. *y: A Tensor. Has the same type as orig_input.
  489. *@attention Constraints:\n
  490. *-The implementation for FractionalMaxPoolGrad on Ascend uses AICPU, with bad performance.\n
  491. *@par Third-party framework compatibility
  492. *@li compatible with tensorflow FractionalMaxPoolGrad operator.
  493. */
  494. REG_OP(FractionalMaxPoolGrad)
  495. .INPUT(orig_input, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  496. .INPUT(orig_output, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  497. .INPUT(out_backprop, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  498. .INPUT(row_pooling_sequence, TensorType({ DT_INT64 }))
  499. .INPUT(col_pooling_sequence, TensorType({ DT_INT64 }))
  500. .OUTPUT(y, TensorType({ DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64 }))
  501. .ATTR(overlapping, Bool, false)
  502. .OP_END_FACTORY_REG(FractionalMaxPoolGrad)
  503. /**
  504. *@brief Performs fractional average pooling on the input.
  505. *@par Inputs:
  506. *Inputs include: \n
  507. *x: A Tensor. Must be one of the following types: float32, float64, int32, int64. \n
  508. 4-D with shape [batch, height, width, channels].
  509. *@par Attributes:
  510. *@li pooling_ratio: A list of floats that has length >= 4.
  511. *@li pseudo_random: An optional bool. Defaults to False.
  512. *@li overlapping: An optional bool. Defaults to False. When set to True, it means when pooling.
  513. *@li deterministic: An optional bool. Defaults to False.
  514. *@li seed: An optional int. Defaults to 0.
  515. *@li seed2: An optional int. Defaults to 0.
  516. *@par Outputs:
  517. *@li y: A Tensor. Has the same type as x.
  518. *@li row_pooling_sequence: A Tensor of type int64.
  519. *@li col_pooling_sequence: A Tensor of type int64.
  520. *@attention Constraints:\n
  521. *-The implementation for FractionalAvgPool on Ascend uses AICPU, with bad performance.\n
  522. *@par Third-party framework compatibility
  523. *@li compatible with tensorflow FractionalAvgPool operator.
  524. */
  525. REG_OP(FractionalAvgPool)
  526. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  527. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  528. .OUTPUT(row_pooling_sequence, TensorType({DT_INT64}))
  529. .OUTPUT(col_pooling_sequence, TensorType({DT_INT64}))
  530. .ATTR(pooling_ratio, ListFloat, {})
  531. .ATTR(pseudo_random, Bool, false)
  532. .ATTR(overlapping, Bool, false)
  533. .ATTR(deterministic, Bool, false)
  534. .ATTR(seed, Int, 0)
  535. .ATTR(seed2, Int, 0)
  536. .OP_END_FACTORY_REG(FractionalAvgPool)
  537. /**
  538. *@brief Performs fractional max pooling on the input.
  539. *@par Inputs:
  540. *Inputs include: \n
  541. *x: A Tensor. Must be one of the following types: float32, float64, int32, int64. \n
  542. 4-D with shape [batch, height, width, channels].
  543. *@par Attributes:
  544. *@li pooling_ratio: A list of floats that has length >= 4. Pooling ratio for each dimension of value.
  545. *@li pseudo_random: An optional bool. Defaults to False.
  546. *@li overlapping: An optional bool. Defaults to False.
  547. *@li deterministic: An optional bool. Defaults to False.
  548. *@li seed: An optional int. Defaults to 0.
  549. *@li seed2: An optional int. Defaults to 0.
  550. *@par Outputs:
  551. *@li y: A Tensor. Has the same type as x.
  552. *@li row_pooling_sequence: A Tensor of type int64.
  553. *@li col_pooling_sequence: A Tensor of type int64.
  554. *@attention Constraints:\n
  555. *-The implementation for FractionalMaxPool on Ascend uses AICPU, with bad performance.\n
  556. *@par Third-party framework compatibility
  557. *@li compatible with tensorflow FractionalMaxPool operator.
  558. */
  559. REG_OP(FractionalMaxPool)
  560. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  561. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  562. .OUTPUT(row_pooling_sequence, TensorType({DT_INT64}))
  563. .OUTPUT(col_pooling_sequence, TensorType({DT_INT64}))
  564. .ATTR(pooling_ratio, ListFloat, {})
  565. .ATTR(pseudo_random, Bool, false)
  566. .ATTR(overlapping, Bool, false)
  567. .ATTR(deterministic, Bool, false)
  568. .ATTR(seed, Int, 0)
  569. .ATTR(seed2, Int, 0)
  570. .OP_END_FACTORY_REG(FractionalMaxPool)
  571. /**
  572. *@brief Finds values of the n-th order statistic for the last dimension.
  573. *@par Inputs:
  574. *Inputs include: \n
  575. * @li x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, \n
  576. int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
  577. * @li n: A Tensor of type int32. 0-D.
  578. *@par Attributes:
  579. *reverse: An optional bool. Defaults to False.
  580. *@par Outputs:
  581. *y: A Tensor. Has the same type as x.
  582. *@attention Constraints:\n
  583. *-The implementation for NthElement on Ascend uses AICPU, with bad performance.\n
  584. *@par Third-party framework compatibility
  585. *@li compatible with tensorflow NthElement operator.
  586. */
  587. REG_OP(NthElement)
  588. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16,
  589. DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_DOUBLE}))
  590. .INPUT(n, TensorType({DT_INT32}))
  591. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16,
  592. DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_DOUBLE}))
  593. .ATTR(reverse, Bool, false)
  594. .OP_END_FACTORY_REG(NthElement)
  595. /**
  596. *@brief Computes gradient of the FractionalAvgPool function.
  597. *@par Inputs:
  598. *Inputs include: \n
  599. * @li orig_input_tensor_shape: A Tensor of type int64.
  600. * @li out_backprop: A Tensor. Must be one of the following types: float32, float64, \n
  601. int32, int64. 4-D with shape [batch, height, width, channels].
  602. * @li row_pooling_sequence: A Tensor of type int64.
  603. * @li col_pooling_sequence: A Tensor of type int64.
  604. *@par Attributes:
  605. *overlapping: An optional bool. Defaults to False.
  606. *@par Outputs:
  607. *y: A Tensor. Has the same type as out_backprop.
  608. *@attention Constraints:\n
  609. *-The implementation for FractionalAvgPoolGrad on Ascend uses AICPU, with bad performance.\n
  610. *@par Third-party framework compatibility
  611. *@li compatible with tensorflow FractionalAvgPoolGrad operator.
  612. */
  613. REG_OP(FractionalAvgPoolGrad)
  614. .INPUT(orig_input_tensor_shape, TensorType({DT_INT64}))
  615. .INPUT(out_backprop, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  616. .INPUT(row_pooling_sequence, TensorType({DT_INT64}))
  617. .INPUT(col_pooling_sequence, TensorType({DT_INT64}))
  618. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  619. .ATTR(overlapping, Bool, false)
  620. .OP_END_FACTORY_REG(FractionalAvgPoolGrad)
  621. /**
  622. *@brief Returns the permuted vector/tensor in the destination data format given the.
  623. *@par Inputs:
  624. *Inputs include: \n
  625. *x: A Tensor. Must be one of the following types: int32, int64. Vector of size 4 \n
  626. or Tensor of shape (4, 2) in source data format.
  627. *@par Attributes:
  628. *@li src_format: An optional string. Defaults to "NHWC". source data format.
  629. *@li dst_format: An optional string. Defaults to "NCHW". destination data format.
  630. *@par Outputs:
  631. *y: A Tensor. Has the same type as x.
  632. *@attention Constraints:\n
  633. *-The implementation for DataFormatVecPermute on Ascend uses AICPU, with bad performance.\n
  634. *@par Third-party framework compatibility
  635. *@li compatible with tensorflow DataFormatVecPermute operator.
  636. */
  637. REG_OP(DataFormatVecPermute)
  638. .INPUT(x, TensorType({ DT_INT32, DT_INT64 }))
  639. .OUTPUT(y, TensorType({ DT_INT32, DT_INT64 }))
  640. .ATTR(src_format, String, "NHWC")
  641. .ATTR(dst_format, String, "NCHW")
  642. .OP_END_FACTORY_REG(DataFormatVecPermute)
  643. } // namespace ge
  644. #endif // GE_OP_NN_POOLING_OPS_H

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示