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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /**
  20. *@brief Performs pooling on the input.
  21. *@par Inputs:
  22. *@li x: An NCHW tensor of type float16.
  23. *@par Attributes:
  24. *@li mode: An optional int32, specifying the pooling algorithm, either "1" (max pooling) or "0" (avg pooling). Defaults to "0".
  25. *@li global_pooling: An optional bool. Defaults to "false".
  26. *@li window: Optional, including: \n
  27. *window[0]: An optional int32, specifying the window size along in the H dimension. The value range is [1, 32768]. Defaults to "1". \n
  28. *window[1]: An optional int32, specifying the window size along in the W dimension. The value range is [1, 32768]. Defaults to "1". \n
  29. *@li stride: Optional, including: \n
  30. *stride[0]: An optional int32, specifying the stride along in the H dimension. The value range is [1, 63]. Defaults to "1". \n
  31. *stride[1]: An optional int32, specifying the stride along in the W dimension. The value range is [1, 63]. Defaults to "1". \n
  32. *@li pad: Optional, including: \n
  33. *pad[0]: An optional int32, specifying the up padding. Defaults to "0". \n
  34. *pad[1]: An optional int32, specifying the bottom padding. Defaults to "0". \n
  35. *pad[2]: An optional int32, specifying the left padding. Defaults to "0". \n
  36. *pad[3]: An optional int32, specifying the right padding. Defaults to "0". \n
  37. *@li ceil_mode: An optional int32, either "0" (ceil mode) or "1" (floor mode). Defaults to "0".
  38. *@par Outputs:
  39. *y: An NCHW tensor of type float16.
  40. *@attention Constraints:\n
  41. *@li window[0] * window[1] < 256;
  42. */
  43. namespace ge {
  44. REG_OP(Pooling)
  45. .INPUT(x, TensorType({DT_FLOAT16}))
  46. .OUTPUT(y, TensorType({DT_FLOAT16}))
  47. .ATTR(mode, Int, 0) // 0:max pooling or 1:avg pooling
  48. .ATTR(global_pooling, Bool, false)
  49. .ATTR(window, ListInt, {1,1}) // kernel size
  50. .ATTR(stride, ListInt, {1,1}) // stride size
  51. .ATTR(pad, ListInt, {0,0,0,0}) // pad size
  52. .ATTR(ceil_mode, Int, 0)
  53. .OP_END_FACTORY_REG(Pooling)
  54. /**
  55. *@brief Performs average pooling on the input.
  56. *@par Inputs:
  57. *x: A tensor of type float16.
  58. *@par Attributes:
  59. *@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].
  60. *@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].
  61. *@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.
  62. *@li data_format: An optional string, specifying the data format of "ksize" and "strides", either "NCHW", "NC1HWC0", or "NHWC" (default).
  63. *@par Outputs:
  64. *y: The average pooled output tensor.
  65. *@attention Constraints:\n
  66. *@li Only single input and single output are supported.
  67. *@li Global pooling is supported.
  68. *@li "ksize_H" and "ksize_W" are positive integers within the range [1, 32768]. ksize_H * ksize_W < 256
  69. *@li Due to instruction restrictions, the values of "strides_h" and "strides_w" are positive integers within the range [1, 63].
  70. */
  71. REG_OP(AvgPool)
  72. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  73. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  74. .REQUIRED_ATTR(ksize, ListInt)
  75. .REQUIRED_ATTR(strides, ListInt)
  76. .REQUIRED_ATTR(padding, String)
  77. .ATTR(data_format, String, "NHWC")
  78. .OP_END_FACTORY_REG(AvgPool)
  79. REG_OP(MaxPoolExt2)
  80. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  81. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  82. DT_UINT16, DT_QINT8}))
  83. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  84. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  85. DT_UINT16, DT_QINT8}))
  86. .REQUIRED_ATTR(ksize, ListInt)
  87. .REQUIRED_ATTR(strides, ListInt)
  88. .REQUIRED_ATTR(padding, String)
  89. .ATTR(data_format, String, "NHWC")
  90. .OP_END_FACTORY_REG(MaxPoolExt2)
  91. REG_OP(MaxPool)
  92. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  93. DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
  94. DT_UINT16, DT_QINT8}))
  95. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE, DT_INT8,
  96. DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  97. .REQUIRED_ATTR(ksize, ListInt)
  98. .REQUIRED_ATTR(strides, ListInt)
  99. .REQUIRED_ATTR(padding, String)
  100. .ATTR(data_format, String, "NHWC")
  101. .OP_END_FACTORY_REG(MaxPool)
  102. /**
  103. * @brief Computes gradients of the maxpooling function.
  104. * @par Inputs:
  105. * @li x1: A mutable NC1HWC0 tensor of type RealNumberType.
  106. * @li x2: A mutable NC1HWC0 tensor of type RealNumberTypex.
  107. * @li grad: A mutable NC1HWC0 tensor of type RealNumberType.
  108. * @par Attributes:
  109. * @li ksize: A tuple or list, specifying the size of the window for each
  110. * dimension of the input tensor.
  111. * @li strides: A tuple or list, specifying the stride of the sliding window for
  112. * each dimension of the input tensor.
  113. * @li padding: A string, specifying the type of padding algorithm to use.
  114. * @par Outputs:
  115. * y: A mutable tensor. Has the same shape and type as "x1.
  116. * @attention Constraints:
  117. * @li Computing gradients of global pooling is not supported, which means
  118. * "ksize < x1".
  119. * @li "ksiez" is in the range [1, 255]. "strides" is in the range [1, 63]
  120. */
  121. REG_OP(MaxPoolGrad)
  122. .INPUT(x1, TensorType::RealNumberType())
  123. .INPUT(x2, TensorType::RealNumberType())
  124. .INPUT(grad, TensorType::RealNumberType())
  125. .OUTPUT(y, TensorType::RealNumberType())
  126. .REQUIRED_ATTR(ksize, ListInt)
  127. .REQUIRED_ATTR(strides, ListInt)
  128. .REQUIRED_ATTR(padding, String)
  129. .OP_END_FACTORY_REG(MaxPoolGrad)
  130. /**
  131. * @brief Computes second-order gradients of the maxpooling function.
  132. * @par Inputs:
  133. * @li x1: Original forward input tensor of type float16
  134. * @li x2: Original forward output tensor of type float16
  135. * @li grad: Gradient tensor of type float16
  136. * @par Attributes:
  137. * @li ksize: A required list, specifying the size of the sliding window.
  138. * @li strides: A required list, specifying the stride of the sliding window.
  139. * @li padding: window sliding mode. Either SAME or VALID.
  140. * @li data_format: Format of the original input, either NCHW or NHWC. Defaults
  141. * to NHWC.
  142. * @attention Constraints:
  143. * @li Only the cloud platform is supported.
  144. * @li "x1" and "grads" must have the same shape.
  145. * @li "x2" and "y" must have the same shape. Otherwise, an error is reported.
  146. * @li "x1", "x2", "grads", and "y" must be 5D tensors.
  147. * @par Outputs:
  148. * @li y: Result tensor of type float16
  149. */
  150. REG_OP(MaxPoolGradGrad)
  151. .INPUT(x1, TensorType::RealNumberType())
  152. .INPUT(x2, TensorType::RealNumberType())
  153. .INPUT(grad, TensorType::RealNumberType())
  154. .OUTPUT(y, TensorType::RealNumberType())
  155. .REQUIRED_ATTR(ksize, ListInt)
  156. .REQUIRED_ATTR(strides, ListInt)
  157. .REQUIRED_ATTR(padding, String)
  158. .ATTR(data_format, String, "NHWC")
  159. .OP_END_FACTORY_REG(MaxPoolGradGrad)
  160. /**
  161. *@brief Performs max_pool_ext2 on the input.
  162. *@par Inputs:
  163. * Two inputs:
  164. *@li x: An NC1HWC0 Tensor of type float16.
  165. *@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.
  166. *@li ksize: A required type of int32 values, specifying the size of the window for each dimension of the input tensor. No default value.
  167. *@par Attributes:
  168. *@li padding: A required string. No default value.
  169. *@li data_format: An optional string. Defaults to "NC1HWC0".
  170. *@par Outputs:
  171. *y: A Tensor. Has the same type and format as input "x".
  172. *@attention Constraints:
  173. *@li "ksize" is a list that has length 4: ksize[0] = 1 or ksize[3] = 1, ksize[1] * ksize[2] <= 255.
  174. *@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.
  175. *@li "padding" is either "SAME" or "VALID".
  176. */
  177. REG_OP(MaxPoolV2)
  178. .INPUT(x, TensorType({DT_FLOAT16}))
  179. .INPUT(ksize, TensorType({DT_INT32}))
  180. .INPUT(strides, TensorType({DT_INT32}))
  181. .OUTPUT(y, TensorType({DT_FLOAT16}))
  182. .REQUIRED_ATTR(padding, String)
  183. .ATTR(data_format, String, "NHWC")
  184. .OP_END_FACTORY_REG(MaxPoolV2)
  185. REG_OP(MaxPoolWithArgmax)
  186. .INPUT(x, TensorType::RealNumberType())
  187. .OUTPUT(y, TensorType::RealNumberType())
  188. .OUTPUT(argmax, TensorType::IndexNumberType())
  189. .REQUIRED_ATTR(ksize, ListInt)
  190. .REQUIRED_ATTR(strides, ListInt)
  191. .REQUIRED_ATTR(padding, String)
  192. .ATTR(Targmax, Int, 7)
  193. .OP_END_FACTORY_REG(MaxPoolWithArgmax)
  194. REG_OP(MaxPoolGradWithArgmax)
  195. .INPUT(x, TensorType::RealNumberType())
  196. .INPUT(grad, TensorType::RealNumberType())
  197. .INPUT(argmax, TensorType::IndexNumberType())
  198. .OUTPUT(y, TensorType::RealNumberType())
  199. .REQUIRED_ATTR(ksize, ListInt)
  200. .REQUIRED_ATTR(strides, ListInt)
  201. .REQUIRED_ATTR(padding, String)
  202. .OP_END_FACTORY_REG(MaxPoolGradWithArgmax)
  203. /**
  204. * @brief Computes second-order gradients of the maxpooling function.
  205. * @par Inputs:
  206. * @li x:Original forward input tensor of type float16
  207. * @li grad:Gradient tensor of type float16
  208. * @li argmax:An tensor of type uint16
  209. * @par Attributes:
  210. * @li ksize: A required list, specifying the size of the sliding window.
  211. * @li strides: A required list, specifying the stride of the sliding window.
  212. * @li padding: window sliding mode. Either SAME or VALID.
  213. * @par Outputs:
  214. * @li y:Result tensor of type float16
  215. * @attention Constraints:
  216. * @li Only the cloud platform is supported.
  217. * @li "x1" and "grads" must have the same shape.
  218. * @li length of the shape of x, grads, argmax, y must be 5.
  219. * @li shape of argmax must be (fmap_n, fmap_c1, kernel_h * kernel_w,
  220. * (shape_max_pool[2] * shape_max_pool[3] + 15) // 16 * 16, 1),
  221. * or (fmap_n, fmap_c1, kernel_h * kernel_w,
  222. * (shape_max_pool[2] * shape_max_pool[3] + 31) // 16, 16), else failed.
  223. */
  224. REG_OP(MaxPoolGradGradWithArgmax)
  225. .INPUT(x, TensorType::RealNumberType())
  226. .INPUT(grad, TensorType::RealNumberType())
  227. .INPUT(argmax, TensorType::IndexNumberType())
  228. .OUTPUT(y, TensorType::RealNumberType())
  229. .REQUIRED_ATTR(ksize, ListInt)
  230. .REQUIRED_ATTR(strides, ListInt)
  231. .REQUIRED_ATTR(padding, String)
  232. .OP_END_FACTORY_REG(MaxPoolGradGradWithArgmax)
  233. /**
  234. * @brief Computes avgpoograd function.
  235. * @par Inputs:
  236. * @li orig_input_shape: An NHWC tensor of type int32.
  237. * @li input_grad: An NHWC tensor of type float16, float32, or double.
  238. * @par Attributes:
  239. * @li ksize: A tuple or list, specifying the size of the window for each
  240. * dimension of the input tensor.
  241. * @li strides: A tuple or list, specifying the stride of the sliding window for\n
  242. * each dimension of the input tensor.
  243. * @li padding: A string, specifying the type of the padding algorithm to use.
  244. * @li data_format: A string. Defaults to "NHWC".
  245. * @par Outputs:
  246. * @out_grad: A mutable tensor with the same shape and type as "orig_input".
  247. */
  248. REG_OP(AvgPoolGrad)
  249. .INPUT(orig_input_shape, TensorType({DT_INT32}))
  250. .INPUT(input_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  251. .OUTPUT(out_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  252. .REQUIRED_ATTR(ksize, ListInt)
  253. .REQUIRED_ATTR(strides, ListInt)
  254. .REQUIRED_ATTR(padding, String)
  255. .ATTR(data_format, String, "NHWC")
  256. .OP_END_FACTORY_REG(AvgPoolGrad)
  257. /**
  258. * @brief Computes gradients of average pooling function.
  259. * @par Inputs:
  260. * @input_grad: An NHWC tensor of type float16, float32, or double.
  261. * @par Attributes:
  262. * @li orig_input_shape: Original input dimensions.
  263. * @li ksize: A tuple or list, specifying the size of the window for each
  264. * dimension of the input tensor.
  265. * @li strides: A tuple or list, specifying the stride of the sliding window for\n
  266. * each dimension of the input tensor.
  267. * @li padding: A string, specifying the type of the padding algorithm to use.
  268. * @li data_format: A string. Defaults to "NHWC".
  269. * @par Outputs:
  270. * @out_grad: A mutable tensor with the same shape and type as "orig_input".
  271. */
  272. REG_OP(AvgPoolGradD)
  273. .INPUT(input_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  274. .OUTPUT(out_grad, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE}))
  275. .REQUIRED_ATTR(orig_input_shape, ListInt)
  276. .REQUIRED_ATTR(ksize, ListInt)
  277. .REQUIRED_ATTR(strides, ListInt)
  278. .REQUIRED_ATTR(padding, String)
  279. .ATTR(data_format, String, "NHWC")
  280. .OP_END_FACTORY_REG(AvgPoolGradD)
  281. REG_OP(MaxPoolWithArgmaxCCE)
  282. .INPUT(x, TensorType::ALL())
  283. .OUTPUT(y, TensorType::ALL())
  284. .OUTPUT(argmax, TensorType::ALL())
  285. .ATTR(mode, Int, 0)
  286. .ATTR(pad_mode, Int, 0)
  287. .ATTR(window, ListInt, {1,1})
  288. .ATTR(stride, ListInt, {1,1})
  289. .ATTR(pad, ListInt, {0,0,0,0})
  290. .ATTR(ceil_mode, Int, 0)
  291. .ATTR(data_mode, Int, 1)
  292. .ATTR(nan_opt, Int, 0)
  293. .OP_END_FACTORY_REG(MaxPoolWithArgmaxCCE)
  294. REG_OP(MaxPoolGradWithArgmaxCCE)
  295. .INPUT(x, TensorType::ALL())
  296. .INPUT(grad,TensorType::ALL())
  297. .INPUT(arg,TensorType::ALL())
  298. .OUTPUT(output,TensorType::ALL())
  299. .ATTR(mode, Int, 0)
  300. .ATTR(max_pool_grad_output_shape, ListInt, {0,0,0,0})
  301. .ATTR(pad_mode, Int, 0)
  302. .ATTR(window, ListInt, {1,1})
  303. .ATTR(stride, ListInt, {1,1})
  304. .ATTR(pad, ListInt, {0,0,0,0})
  305. .ATTR(ceil_mode, Int, 0)
  306. .ATTR(data_mode, Int, 1)
  307. .ATTR(nan_opt, Int, 0)
  308. .OP_END_FACTORY_REG(MaxPoolGradWithArgmaxCCE)
  309. REG_OP(Upsample)
  310. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8}))
  311. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8}))
  312. .ATTR(scale, Float, 1)
  313. .ATTR(stride, Int, 2)
  314. .OP_END_FACTORY_REG(Upsample)
  315. } // namespace ge
  316. #endif // GE_OP_NN_POOLING_OPS_H

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