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_calculation_ops.h 35 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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_CALCULATION_OPS_H
  17. #define GE_OP_NN_CALCULATION_OPS_H
  18. #include "../graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. * @brief Computes the gradients of depthwise convolution with respect to
  22. * the filter.
  23. * @par Inputs:
  24. * Three inputs include: \n
  25. * @li input: 4D origin shape of input tensor [N, C, H, W] or [N, H, W, C],
  26. * support float16, float32, double
  27. * @li filter_size: A 4D tensor of type int32, with shape [H, W, C, K]
  28. * @li out_backprop: 4D tensor with shape [N, C, H, W] or [N, H, W, C].
  29. * Must be one of the following types: float16, float32, double.
  30. * @par Attributes:
  31. * @li strides: A required list or tuple. The stride of the sliding window
  32. * for height and width of input "x" of the convolution.
  33. * Must be with shape [1, 1, stride_height, stride_width] or
  34. * [1, stride_height, stride_width, 1].
  35. * @li dilations: An optional list or tuple. The dilation factor for each
  36. * dimension of input "x".
  37. * If set to k > 1, there will be k-1 skipped cells between each filter element
  38. * on that dimension. Must be with shape [1, 1, dilation_height, dilation_width]
  39. * or [1, dilation_height, dilation_width, 1].
  40. * @li pads: A required list or tuple. Padding added to each dimension of the
  41. * input.
  42. * @li data_format: An optional string. Input data format, either "NHWC" or
  43. * "NCHW".
  44. * @par Outputs:
  45. * filter_grad: Gradient of the deep convolution relative to the filter with
  46. * shape [H, W, C, K]. Must be one of the following types: float16, float32,
  47. * double.
  48. * @attention Constraints:\n
  49. * The feature map is 4D with shape [N, C, Hi, Wi] or [N, Hi, Wi, C], but
  50. * the data is 5D with shape [N, C1, Hi, Wi, C0], where C0 is 16.\n
  51. * The filter is 4D with shape [Hf, Wf, C, K], but the data is 6D with shape
  52. * [C1, Hf, Wf, K, Co, C0],
  53. * where K is fixed at 1, and Co and C0 are 16.\n
  54. * Output backprop is 4D with shape [N, C, Ho, Wo] or [N, Ho, Wo, C], but the
  55. * data is 5D with shape [N, C1, Ho, Wo, C0],
  56. * where C is the same as that of the feature map and C0 is 16.\n
  57. * Limited by Tiling and L1 / L0 buffer memory: 512 * ceil(Wo, 16) + (480 *
  58. * stride_h + 32 * filter_h) * ceil(Wi, 16) ?l1_size and Hf*Wf ?l0b_size/512.\n
  59. */
  60. REG_OP(DepthwiseConv2DBackpropFilter)
  61. .INPUT(input, TensorType({float16}))
  62. .INPUT(filter_size, TensorType({DT_INT32, DT_INT64}))
  63. .INPUT(out_backprop, TensorType({float16}))
  64. .OUTPUT(filter_grad, TensorType({float32}))
  65. .REQUIRED_ATTR(strides, ListInt)
  66. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  67. .REQUIRED_ATTR(pads, ListInt)
  68. .ATTR(data_format, String, "NHWC")
  69. .OP_END_FACTORY_REG(DepthwiseConv2DBackpropFilter)
  70. /**
  71. * @brief Computes the gradients of depthwise convolution with respect to
  72. * the filter.
  73. * @par Inputs:
  74. * Two inputs include: \n
  75. * @li input: 4D tensor with shape [N, C, H, W] or [N, H, W, C], of type float16
  76. * @li out_backprop: 4D tensor with shape [N, C, H, W] or [N, H, W, C],
  77. * of type float16
  78. * @par Attributes:
  79. * @li filter_size: A required list or tuple. Shape of filter.
  80. * @li strides: A required list or tuple. The stride of the sliding window for
  81. * height and width of input "x" of the convolution.
  82. * Must be with shape [1, 1, stride_height, stride_width] or [1, stride_height,
  83. * stride_width, 1].
  84. * @li dilations: An optional list or tuple. The dilation factor for each
  85. * dimension of input "x".
  86. * If set to k > 1, there will be k-1 skipped cells between each filter element
  87. * on that dimension. Must be with shape [1, 1, dilation_height, dilation_width]
  88. * or [1, dilation_height, dilation_width, 1].
  89. * @li pads: A required list or tuple. Padding added to each dimension of the
  90. * input.
  91. * @li data_format: An optional string. Input data format, either "NHWC" or
  92. * "NCHW".
  93. * @par Outputs:
  94. * filter_grad: Gradient of the deep convolution relative to the filter with
  95. * shape [H, W, C, K]. Must be of type float32.
  96. * @attention Constraints:\n
  97. * The feature map is 4D with shape [N, C, Hi, Wi] or [N, Hi, Wi, C], but
  98. * the data is 5D with shape [N, C1, Hi, Wi, C0], where C0 is 16.\n
  99. * The filter is 4D with shape [Hf, Wf, C, K], but the data is 6D with shape
  100. * [C1, Hf, Wf, K, Co, C0],
  101. * where K is fixed at 1, and Co and C0 are 16.\n
  102. * Output backprop is 4D with shape [N, C, Ho, Wo] or [N, Ho, Wo, C], but the
  103. * data is 5D with shape [N, C1, Ho, Wo, C0],
  104. * where C is the same as that of the feature map and C0 is 16.\n
  105. * Limited by Tiling and L1 / L0 buffer memory: 512 * ceil(Wo, 16) + (480 *
  106. * stride_h + 32 * filter_h) * ceil(Wi, 16) ?l1_size and Hf*Wf ?l0b_size/512.\n
  107. */
  108. REG_OP(DepthwiseConv2DBackpropFilterD)
  109. .INPUT(input, TensorType({float16}))
  110. .INPUT(out_backprop, TensorType({float16}))
  111. .OUTPUT(filter_grad, TensorType({float32}))
  112. .REQUIRED_ATTR(filter_size, ListInt)
  113. .REQUIRED_ATTR(strides, ListInt)
  114. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  115. .REQUIRED_ATTR(pads, ListInt)
  116. .ATTR(data_format, String, "NHWC")
  117. .OP_END_FACTORY_REG(DepthwiseConv2DBackpropFilterD)
  118. /**
  119. * @brief Computes the gradients of depthwise convolution with respect to the
  120. * input.
  121. * @par Inputs:
  122. * Three inputs include: \n
  123. * @li input_size: 4D shape of input tensor [N, C, H, W] or [N, H, W, C],
  124. * support int32
  125. * @li filter: 4D filter tensor with shape of [H, W, C, K], support float16,
  126. * float32, double
  127. * @li out_backprop: 4D tensor with shape [N, C, H, W] or [N, H, W, C].
  128. * Must be one of the following types: float16, float32, double.
  129. * @par Attributes:
  130. * @li strides: A required list or tuple. The stride of the sliding window for
  131. * height and width of input "x" of the convolution.
  132. * Must be with shape [1, 1, stride_height, stride_width] or [1, stride_height,
  133. * stride_width, 1].
  134. * @li dilations: An optional list or tuple. The dilation factor for each
  135. * dimension of input "x".
  136. * If set to k > 1, there will be k-1 skipped cells between each filter element
  137. * on that dimension. Must be with shape [1, 1, dilation_height, dilation_width]
  138. * or [1, dilation_height, dilation_width, 1].
  139. * @li pads: A required list or tuple. Padding added to each dimension of the
  140. * input.
  141. * @li data_format: An optional string. Input data format, either "NHWC" or
  142. * "NCHW".
  143. * @par Outputs:
  144. * input_grad: Gradient of the deep convolution relative to the input with shape
  145. * [N, C, H, W] or [N, H, W, C] Must be one of the following types: float16,
  146. * float32, double.
  147. * @attention Constraints:\n
  148. * The feature map is 4D with shape [N, C, Hi, Wi] or [N, Hi, Wi, C], but
  149. * the data is 5D with shape [N, C1, Hi, Wi, C0], where C0 is 16.\n
  150. * The filter is 4D with shape [Hf, Wf, C, K], but the data is 6D with shape
  151. * [C1, Hf, Wf, K, Co, C0],
  152. * where K is fixed at 1, and Co and C0 are 16.\n
  153. * Output backprop is 4D with shape [N, C, Ho, Wo] or [N, Ho, Wo, C], but the
  154. * data is 5D with shape [N, C1, Ho, Wo, C0],
  155. * where C is the same as that of the feature map and C0 is 16.\n
  156. * Limited by Tiling: max_h_in_l1 ?C0, where max_h_in_l1 = (l1_size - Hf *
  157. * Wf * C0 * C0 * 2) / (2 * Wo *C0).\n
  158. */
  159. REG_OP(DepthwiseConv2DBackpropInput)
  160. .INPUT(input_size, TensorType({DT_INT32, DT_INT64}))
  161. .INPUT(filter, TensorType({DT_FLOAT16}))
  162. .INPUT(out_backprop, TensorType({DT_FLOAT16}))
  163. .OUTPUT(input_grad, TensorType({DT_FLOAT16}))
  164. .REQUIRED_ATTR(strides, ListInt)
  165. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  166. .REQUIRED_ATTR(pads, ListInt)
  167. .ATTR(data_format, String, "NHWC")
  168. .OP_END_FACTORY_REG(DepthwiseConv2DBackpropInput)
  169. /**
  170. * @brief Computes the gradients of depthwise convolution with respect to the
  171. * input.
  172. * @par Inputs:
  173. * Two inputs include: \n
  174. * @li filter: A 4D tensor of type float16, with shape [H, W, C, K]
  175. * @li out_backprop: 4D tensor with shape [N, C, H, W] or [N, H, W, C], of
  176. * type float16
  177. * @par Attributes:
  178. * @li input_size: A required list or tuple. The origin shape of input.
  179. * @li strides: A required list or tuple. The stride of the sliding window for
  180. * height and width of input "x" of the convolution.
  181. * Must be with shape [1, 1, stride_height, stride_width] or [1, stride_height,
  182. * stride_width, 1].
  183. * @li dilations: An optional list or tuple. The dilation factor for each
  184. * dimension of input "x".
  185. * If set to k > 1, there will be k-1 skipped cells between each filter element
  186. * on that dimension. Must be with shape [1, 1, dilation_height, dilation_width]
  187. * or [1, dilation_height, dilation_width, 1].
  188. * @li pads: A required list or tuple. Padding added to each dimension of the
  189. * input.
  190. * @li data_format: An optional string. Input data format, either "NHWC" or
  191. * "NCHW".
  192. * @par Outputs:
  193. * input_grad: Gradient of the deep convolution relative to the input with
  194. * shape [N, C, H, W] or [N, H, W, C]. Must be of type float16.
  195. * @attention Constraints:\n
  196. * The feature map is 4D with shape [N, C, Hi, Wi] or [N, Hi, Wi, C], but
  197. * the data is 5D with shape [N, C1, Hi, Wi, C0], where C0 is 16.\n
  198. * The filter is 4D with shape [Hf, Wf, C, K], but the data is 6D with shape
  199. * [C1, Hf, Wf, K, Co, C0],
  200. * where K is fixed at 1, and Co and C0 are 16.\n
  201. * Output backprop is 4D with shape [N, C, Ho, Wo] or [N, Ho, Wo, C], but the
  202. * data is 5D with shape [N, C1, Ho, Wo, C0],
  203. * where C is the same as that of the feature map and C0 is 16.\n
  204. * Limited by Tiling: max_h_in_l1 ?C0, where max_h_in_l1 = (l1_size - Hf *
  205. * Wf * C0 * C0 * 2) / (2 * Wo *C0).\n
  206. */
  207. REG_OP(DepthwiseConv2DBackpropInputD)
  208. .INPUT(filter, TensorType({DT_FLOAT16}))
  209. .INPUT(out_backprop, TensorType({DT_FLOAT16}))
  210. .OUTPUT(input_grad, TensorType({DT_FLOAT16}))
  211. .REQUIRED_ATTR(input_size, ListInt)
  212. .REQUIRED_ATTR(strides, ListInt)
  213. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  214. .REQUIRED_ATTR(pads, ListInt)
  215. .ATTR(data_format, String, "NHWC")
  216. .OP_END_FACTORY_REG(DepthwiseConv2DBackpropInputD)
  217. /**
  218. *@brief Computes a 2D deep convolution given a 4D input tensor and a filter
  219. * tensor.
  220. *@par Inputs:
  221. *Two required inputs and two optional inputs, including: \n
  222. * @li x: A 4D tensor of type float16, with shape [N, C, H, W] or [N, H, W, C]
  223. * @li filter: A 4D tensor of type float16, with shape [H, W, C, K]
  224. * @li bias: An optional tensor of type int8
  225. * @li offset_w: An optional float16, used for quantized inference
  226. * @par Attributes:
  227. * @li strides: A required list or tuple. The stride of the sliding window for
  228. * height and width of input "x" of the convolution.
  229. * Must be with shape [1, 1, stride_height, stride_width] or [1, stride_height,
  230. * stride_width, 1].
  231. * @li dilations: An optional list or tuple. The dilation factor for each
  232. * dimension of input "x".
  233. * If set to k > 1, there will be k-1 skipped cells between each filter element
  234. * on that dimension. Must be with shape [1, 1, dilation_height, dilation_width]
  235. * or [1, dilation_height, dilation_width, 1].
  236. * @li pads: A required list or tuple. Padding added to each dimension of the
  237. * input.
  238. * @li data_format: An optional string. Input data format, either "NHWC" or
  239. * "NCHW".
  240. * @li offset_a: An optional int. Input offset, used for quantized inference.
  241. * @par Outputs:
  242. * y: 4D tensor of type float16, with shape [N, C, H, W] or [N, H, W, C]
  243. * @attention Constraints:\n
  244. * The feature map is 4D with shape [N, C, Hi, Wi] or [N, Hi, Wi, C], but
  245. * the data is 5D with shape [N, C1, Hi, Wi, C0], where C0 is 16.\n
  246. * The filter is 4D with shape [Hf, Wf, C, K], but the data is 6D with shape
  247. * [C1, Hf, Wf, K, Co, C0],
  248. * where K is fixed at 1, and Co and C0 are 16.\n
  249. * Limited by the size of L1 buffer memory: \n
  250. * (l1_size - filter_h*filter_w*BLOCK_SIZE*BLOCK_SIZE*data_size) // (Wi *
  251. * BLOCK_SIZE * data_size) >= (BLOCK_SIZE * strides_h + filter_h - strides_h).\n
  252. */
  253. REG_OP(DepthwiseConv2D)
  254. .INPUT(x, TensorType({DT_FLOAT16}))
  255. .INPUT(filter, TensorType({DT_FLOAT16}))
  256. .OPTIONAL_INPUT(bias, TensorType({DT_INT8}))
  257. .OPTIONAL_INPUT(offset_w, TensorType({DT_FLOAT16}))
  258. .OUTPUT(y, TensorType({DT_FLOAT16}))
  259. .REQUIRED_ATTR(strides, ListInt)
  260. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  261. .REQUIRED_ATTR(pads, ListInt)
  262. .ATTR(data_format, String, "NHWC")
  263. .ATTR(offset_x, Int, 0)
  264. .OP_END_FACTORY_REG(DepthwiseConv2D)
  265. REG_OP(Conv2DCCE)
  266. .INPUT(x, TensorType{DT_FLOAT}) // The input tensor
  267. .INPUT(w, TensorType({DT_FLOAT, DT_INT8})) // The weight tensor ,If QuantType =1 ,shall use type""tensor(int8)
  268. .OPTIONAL_INPUT(b, TensorType{DT_FLOAT}) // Optional 1D bias to be added to the convolution, has size of M.
  269. .OUTPUT(y, TensorType{DT_FLOAT}) // The output tensor
  270. .ATTR(mode, Int, 1)
  271. .ATTR(group, Int, 1) // number of groups input channels and output channels are divided into
  272. .ATTR(num_output, Int, 0) // number of output tensor
  273. .ATTR(pad, ListInt, {0, 0, 0, 0}) // Padding for the beginning and ending along each axis
  274. .ATTR(kernel, ListInt, {0, 0})
  275. .ATTR(stride, ListInt, {1, 1}) // Stride along each axis.
  276. .ATTR(dilation, ListInt, {1, 1}) // dilation value along each axis of the filter.
  277. .ATTR(pad_mode, Int, 0) // pad mode, 0:NOTSET, 1:SAME_UPPER, SAME_LOWER or 2:VALID.defaul default value is 0:NOTSET
  278. .ATTR(algo, Int, 2)
  279. .OP_END_FACTORY_REG(Conv2DCCE)
  280. REG_OP(Conv2DBackpropFilterCCE)
  281. .INPUT(x, TensorType{DT_FLOAT})
  282. .INPUT(filter_sizes, TensorType{DT_INT8})
  283. .INPUT(out_backprop, TensorType{DT_FLOAT})
  284. .OUTPUT(y, TensorType{DT_FLOAT})
  285. .ATTR(conv_grad_filter_output_shape, ListInt, {0, 0, 0, 0})
  286. .ATTR(mode, Int, 1)
  287. .ATTR(group, Int, 1)
  288. .ATTR(pad, ListInt, {0, 0, 0, 0})
  289. .ATTR(stride, ListInt, {1, 1})
  290. .ATTR(dilation, ListInt, {1, 1})
  291. .ATTR(padding, Int, 0) //pad_mode:same valid
  292. .ATTR(algo, Int, 0)
  293. .OP_END_FACTORY_REG(Conv2DBackpropFilterCCE)
  294. REG_OP(Conv2DBackpropInputCCE)
  295. .INPUT(input_sizes, TensorType{DT_INT8})
  296. .INPUT(filter, TensorType{DT_FLOAT})
  297. .INPUT(out_backprop, TensorType{DT_FLOAT})
  298. .OUTPUT(output, TensorType{DT_FLOAT})
  299. .ATTR(conv_grad_input_output_shape, ListInt, {0, 0, 0, 0})
  300. .ATTR(mode, Int, 1)
  301. .ATTR(format, Int, 0)
  302. .ATTR(group, Int, 1)
  303. .ATTR(pad_mode, Int, 0)
  304. .ATTR(stride, ListInt, {1, 1})
  305. .ATTR(dilation, ListInt, {1, 1})
  306. .ATTR(pad, ListInt, {0, 0, 0, 0})
  307. .ATTR(algo, Int, 0)
  308. .OP_END_FACTORY_REG(Conv2DBackpropInputCCE)
  309. /**
  310. *@brief Performs the the backward operation for "BiasAdd" on the "bias" tensor.
  311. * It accumulates all the values from out_backprop into the feature
  312. * dimension. For NHWC data format, the feature dimension is the last.
  313. * For NCHW data format, the feature dimension is the third-to-last.
  314. *@par Inputs:
  315. *x: A Tensor of type TensorType::NumberType().
  316. *@par Attributes:
  317. *data_format: Data format. Defaults to "NHWC".
  318. *@par Outputs:
  319. *y: A Tensor.Has the same type as "x".
  320. */
  321. REG_OP(BiasAddGrad)
  322. .INPUT(x, TensorType::NumberType())
  323. .OUTPUT(y, TensorType::NumberType())
  324. .ATTR(data_format, String, "NHWC")
  325. .OP_END_FACTORY_REG(BiasAddGrad)
  326. /**
  327. *@brief Computes the gradients of convolution with respect to the input.
  328. *@par Inputs:
  329. * Three inputs:
  330. * @li input_size: A Tensor of type int32. An integer vector representing the shape of input,
  331. * where input is a 4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  332. * @li filter: A Tensor. Must be one of the following types: float16, float32, float64.
  333. * 4-D with shape [filter_height, filter_width, in_channels, out_channels]
  334. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  335. * @li out_backprop: A Tensor. Must have the same type as filter. 4-D with shape [batch, out_height, out_width, out_channels]
  336. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  337. *@par Attributes:
  338. * Three attributes:
  339. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  340. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  341. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1]
  342. * @li groups: Number of blocked connections from input channels to output channels.
  343. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  344. *@par Outputs:
  345. * y: A Tensor. Has the same type as filter,and has same format as input_size
  346. */
  347. REG_OP(Conv2DBackpropInput)
  348. .INPUT(input_size, TensorType({DT_INT32}))
  349. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  350. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  351. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  352. .REQUIRED_ATTR(strides, ListInt)
  353. .REQUIRED_ATTR(pads, ListInt)
  354. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  355. .ATTR(groups, Int, 1)
  356. .ATTR(data_format, String, "NHWC")
  357. .OP_END_FACTORY_REG(Conv2DBackpropInput)
  358. /**
  359. *@brief Computes the gradients of convolution with respect to the input.
  360. *@par Inputs:
  361. * Two inputs:
  362. * @li filter: A Tensor. Types is float16.
  363. * 4-D with shape [filter_height, filter_width, in_channels, out_channels] or [out_channels, filter_height, filter_width, in_channels]
  364. * or [out_channels, in_channel, filter_height, filter_width].
  365. * @li out_backprop: A Tensor. Must have the same type as filter. 4-D with shape [batch, out_height, out_width, out_channels]
  366. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  367. *@par Attributes:
  368. * Four attributes:
  369. * @li input_size A Tensor of type int32. An integer vector representing the shape of input,
  370. * where input is a 4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  371. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  372. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  373. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1]
  374. * @li groups: Number of blocked connections from input channels to output channels.
  375. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  376. *@par Outputs:
  377. * y: A Tensor. Has the same type as filter,4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  378. */
  379. REG_OP(Conv2DBackpropInputD)
  380. .INPUT(filter, TensorType({DT_FLOAT16, DT_INT8}))
  381. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_INT8}))
  382. .OUTPUT(y, TensorType({DT_FLOAT16, DT_INT32}))
  383. .REQUIRED_ATTR(input_size, ListInt)
  384. .REQUIRED_ATTR(strides, ListInt)
  385. .REQUIRED_ATTR(pads, ListInt)
  386. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  387. .ATTR(groups, Int, 1)
  388. .ATTR(data_format, String, "NHWC")
  389. .OP_END_FACTORY_REG(Conv2DBackpropInputD)
  390. /**
  391. *@brief Computes the Deconvolution with respect to the input.
  392. *@par Inputs:
  393. * Three inputs:
  394. * @li x: A Tensor. Must have the same type as "filter". 4D with shape
  395. * [batch, out_height, out_width, out_channels]
  396. * or [batch, out_channels, out_height, out_width]. Gradients with respect
  397. * to the output of the convolution.
  398. * @li filter: A Tensor of type float16.
  399. * 4D with shape [filter_height, filter_width, in_channels, out_channels],
  400. * or [out_channels, filter_height, filter_width, in_channels],
  401. * or [out_channels, in_channel, filter_height, filter_width].
  402. * One optional input:
  403. * @li bias: An optional tensor of type int8
  404. *@par Attributes:
  405. * Three attributes:
  406. * @li strides: A tuple or list of 2 integers. The stride of the sliding window
  407. * for H/W dimension.
  408. * @li pads: A tuple or list of 4 integers. The [top, bottom, left, right]
  409. * padding on the feature map
  410. * @li dilations: A tuple or list of 4 integers. The dilation factor for each
  411. * dimension of input. Must be [1, 1, 1, 1].
  412. *@par Outputs:
  413. * y: A Tensor. Has the same type as "filter". 4D tensor with shape
  414. * [batch, height, width, channels] or [batch, channels, height, width].
  415. */
  416. REG_OP(Deconvolution)
  417. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  418. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  419. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  420. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  421. .ATTR(strides, ListInt, {1, 1, 1, 1})
  422. .ATTR(pads, ListInt, {0, 0, 0, 0})
  423. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  424. .OP_END_FACTORY_REG(Deconvolution)
  425. /**
  426. *@brief Computes the gradients of convolution with respect to the filter
  427. *@par Inputs:
  428. * Three inputs:
  429. * @li x: A Tensor. Must be one of the following types: float16, float32, float64.
  430. * 4-D with shape [batch, in_height, in_width, in_channels] or [batch, in_channels, in_height, in_width].
  431. * @li filter_size: A Tensor of type int32. An integer vector representing the tensor shape of filter,
  432. * where filter is a 4-D tensor [filter_height, filter_width, in_channels, out_channels]
  433. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  434. * @li out_backprop: A Tensor. Must have the same type as x. 4-D with shape [batch, out_height, out_width, out_channels]
  435. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  436. *@par Attributes:
  437. * Three attributes:
  438. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  439. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map.
  440. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1].
  441. * @li groups: Number of blocked connections from input channels to output channels.
  442. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  443. *@par Outputs:
  444. * y: A Tensor. Has the same type as x
  445. */
  446. REG_OP(Conv2DBackpropFilter)
  447. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  448. .INPUT(filter_size, TensorType({DT_INT32}))
  449. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  450. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  451. .REQUIRED_ATTR(strides, ListInt)
  452. .REQUIRED_ATTR(pads, ListInt)
  453. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  454. .ATTR(groups, Int, 1)
  455. .ATTR(data_format, String, "NHWC")
  456. .OP_END_FACTORY_REG(Conv2DBackpropFilter)
  457. /**
  458. *@brief Computes the gradients of convolution with respect to the filter.
  459. *@par Inputs:
  460. * Two inputs:
  461. * @li x: A Tensor. Type is float16.
  462. * 4-D with shape [batch, in_height, in_width, in_channels] or [batch, in_channels, in_height, in_width].
  463. * @li out_backprop: A Tensor. Must have the same type as x. 4-D with shape [batch, out_height, out_width, out_channels]
  464. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  465. *@par Attributes:
  466. * Four attributes:
  467. * @li filter_size: A Tensor of type integers. An integer vector representing the tensor shape of filter,
  468. * where filter is a 4-D tensor [filter_height, filter_width, in_channels, out_channels]
  469. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  470. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  471. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  472. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1].
  473. * @li groups: Number of blocked connections from input channels to output channels.
  474. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  475. *@par Outputs:
  476. * y: A Tensor. Has the same type as x
  477. */
  478. REG_OP(Conv2DBackpropFilterD)
  479. .INPUT(x, TensorType({DT_FLOAT16}))
  480. .INPUT(out_backprop, TensorType({DT_FLOAT16}))
  481. .OUTPUT(y, TensorType({DT_FLOAT}))
  482. .REQUIRED_ATTR(filter_size, ListInt)
  483. .REQUIRED_ATTR(strides, ListInt)
  484. .REQUIRED_ATTR(pads, ListInt)
  485. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  486. .ATTR(groups, Int, 1)
  487. .ATTR(data_format, String, "NHWC")
  488. .OP_END_FACTORY_REG(Conv2DBackpropFilterD)
  489. /**
  490. *@brief Computes a 2D convolution given 4D "x" and "filter" tensors.
  491. *@par Inputs:
  492. * @li x: A 4D tensor of input images.
  493. * @li filter: A 4D tensor of filters.
  494. * @li bias: An optional 1D tensor.
  495. * @li offset_w: An optional 1D tensor for quantized convolution. Reserved.\n
  496. * \n
  497. * The input and output tensor attributes are listed as follows:
  498. * @verbatim
  499. Tensor | x | filter | bias | offset_w | y
  500. -----------|---------|---------|---------|----------|--------
  501. Data Type | float16 | float16 | float16 | _ | float16
  502. |---------|---------|---------|----------|--------
  503. | float32 | float32 | float32 | _ | float32
  504. |---------|---------|---------|----------|--------
  505. | float64 | float64 | float64 | _ | float64
  506. |---------|---------|---------|----------|--------
  507. | int8 | int8 | int32 | int8 | int32
  508. -----------|---------|---------|---------|----------|--------
  509. Format | NCHW | NCHW | ND | ND | NCHW
  510. | NHWC | NHWC | | | NHWC
  511. | | HWCN | | |
  512. @endverbatim
  513. * It should be noted that the data types must correspond to each other, but the
  514. * format does not need to.
  515. *@par Attributes:
  516. * @li strides: A list of 4 integers. Specifying the strides of the
  517. * convolution along the height and width. The dimension order is determined
  518. * by the data format of "x". By default the N and C dimensions are set to 1.
  519. * @li pads: A list of 4 integers. Specifying the top, bottom, left and right
  520. * padding.
  521. * @li dilations: A list of 4 integers. Specifying the dilation rate to use
  522. * for dilated convolution. Has the same dimension order and value as "strides".
  523. * @li groups: Number of blocked connections from input channels to output
  524. * channels. Input channels and output channels must both be divisible by
  525. * "groups". Must be set to 1.
  526. * @li offset_x: An optional integer for quantized convolution.
  527. * @li data_format: An optional string from: "NHWC", "NCHW". Specifying the
  528. * data format of the input and output images. Reserved.
  529. *@par Outputs:
  530. * @li y: A 4D Tensor of output images.
  531. *@attention
  532. * @li The parameter scope is listed as follows:\n
  533. * @verbatim
  534. Name | Field | Scope
  535. ------------------|--------------|----------
  536. Input Image Size | H dimension | [1, 4096]
  537. | W dimension | [1, 4096]
  538. ------------------|--------------|----------
  539. Filter Size | H dimension | [1, 255]
  540. | W dimension | [1, 255]
  541. ------------------|--------------|----------
  542. Stride Size | H dimension | [1, 63]
  543. | W dimension | [1, 63]
  544. ------------------|--------------|----------
  545. Padding Size | top side | [0, 255]
  546. | bottom side | [0, 255]
  547. | left side | [0, 255]
  548. | right side | [0, 255]
  549. ------------------|--------------|----------
  550. Dilation Size | H dimension | [1, 255]
  551. | W dimension | [1, 255]
  552. @endverbatim
  553. * @li There are restrictions for certain scenarios:
  554. * @verbatim
  555. Output | Restrictions
  556. ------------------|----------------------------------------------
  557. W dimension == 1 | HxW(input) == HxW(filter) == 1x1,2x2...11x11.
  558. H dimension == 1 |
  559. ------------------|----------------------------------------------
  560. W dimension == 1 | Not supported
  561. H dimension != 1 |
  562. @endverbatim
  563. * As shown above, "HxW(input)" indicates the image size after padding and
  564. * "HxW(filter)" indicates the filter size after dilation.
  565. */
  566. REG_OP(Conv2D)
  567. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  568. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  569. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  570. .OPTIONAL_INPUT(offset_w, TensorType({DT_INT8}))
  571. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  572. .REQUIRED_ATTR(strides, ListInt)
  573. .REQUIRED_ATTR(pads, ListInt)
  574. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  575. .ATTR(groups, Int, 1)
  576. .ATTR(data_format, String, "NHWC")
  577. .ATTR(offset_x, Int, 0)
  578. .OP_END_FACTORY_REG(Conv2D)
  579. /**
  580. *@brief Computes a 3D convolution given 5D "x" and "filter" tensors.
  581. *@par Inputs:
  582. *@li x: A 5D tensor. Must be one of the following types: float16, float32, float64. The format is NCDHW or NDHWC.
  583. *@li filter: A 5D tensor of the same type as "x". The format is NCDHW, NDHWC or DHWCN.
  584. *@li bias: An optional 1D tensor of the same type as "x".
  585. *@par Attributes:
  586. *@li strides: A list of 5 ints. Specifies the stride of the sliding window for each dimension of "x". The N and C dimensions must be 1. Has the same format as "x".
  587. *@li pads: A list of 6 ints. Supports only padding along the D, H and W dimensions in sequence of head, tail, top, bottom, left and right.
  588. *@li padding_mode: An optional string from: "zeros", "circular". Defaults to "zeros".
  589. *@li data_format: An optional string from: "NDHWC", "NCDHW". Defaults to "NDHWC". Specify the data format of the input and output data.
  590. *@li dilations: A list of 5 ints. Specifies the dilation factor for each dimension of "x". The N and C dimensions must be 1. Has the same format as "x".
  591. *@par Outputs:
  592. *y: A Tensor. Has the same type as "x".
  593. *@attention Constraints:\n
  594. *The image size after padding is greater than the filter size.\n
  595. */
  596. REG_OP(Conv3D)
  597. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  598. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  599. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  600. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  601. .ATTR(strides, ListInt, {1, 1, 1, 1, 1})
  602. .ATTR(pads, ListInt, {0, 0, 0, 0, 0, 0})
  603. .ATTR(padding_mode, String, "zeros")
  604. .ATTR(data_format, String, "NDHWC")
  605. .ATTR(dilations, ListInt, {1, 1, 1, 1, 1})
  606. .OP_END_FACTORY_REG(Conv3D)
  607. /**
  608. *@brief Computes the gradients of convolution 3d with respect to the input.
  609. *@par Inputs:
  610. * Three inputs:
  611. * @li input_sizes: A Tensor of type int32, int64. An integer vector representing the shape of input,
  612. * where input is a 5-D tensor [batch, depth, height, width, channels] or [batch, channels, depth, height, width].
  613. * @li filters: A Tensor. Must be one of the following types: float16, float32, float64.
  614. * @li grads: A Tensor. Must have the same type as filter. 5-D with shape [batch, depth, out_height, out_width, out_channels]
  615. * or [batch, out_channels, depth, out_height, out_width]. Gradients with respect to the output of the convolution.
  616. *@par Attributes:
  617. * Four attributes:
  618. * @li strides: A tuple/list of 3 integers. The stride of the sliding window for D/H/W dimension.
  619. * @li pads: A tuple/list of 6 integers
  620. * @li dilations: A tuple/list of 6 integers, The dilation factor for each dimension of input, now only support [1,1,1,1,1]
  621. * @li data_format: An optional string from: "NDHWC", "NCHWD". Defaults to "NDHWC". Specify the data format of the input and output data.
  622. *@par Outputs:
  623. * y: A Tensor. Has the same type as filter,and has same format as input_size
  624. */
  625. REG_OP(Conv3DBackpropInput)
  626. .INPUT(input_sizes, TensorType({DT_INT32, DT_INT64}))
  627. .INPUT(filters, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  628. .INPUT(grads, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  629. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  630. .REQUIRED_ATTR(strides, ListInt)
  631. .ATTR(pads, ListInt, {0, 0, 0, 0, 0, 0})
  632. .ATTR(data_format, String, "NDHWC")
  633. .ATTR(dilations, ListInt, {1, 1, 1, 1, 1})
  634. .OP_END_FACTORY_REG(Conv3DBackpropInput)
  635. /**
  636. *@brief Computes the gradients of convolution 3d with respect to the input.
  637. *@par Inputs:
  638. * Two inputs:
  639. * @li filters: A Tensor. Types is float16.
  640. * @li grads: A Tensor. Must have the same type as filter.
  641. *@par Attributes:
  642. * Five attributes:
  643. * @li input_sizes A Tensor of type int32. An integer vector representing the shape of input,
  644. * @li strides: A tuple/list of 3 integers. The stride of the sliding window for D/H/W dimension.
  645. * @li pads: A tuple/list of 4 integers
  646. * @li dilations: A tuple/list of 5 integers, The dilation factor for each dimension of input, now only support [1,1,1,1,1]
  647. * @li data_format: An optional string from: "NDHWC", "NCHWD". Defaults to "NDHWC". Specify the data format of the input and output data.
  648. *@par Outputs:
  649. * y: A Tensor. Has the same type as filter
  650. */
  651. REG_OP(Conv3DBackpropInputD)
  652. .INPUT(filters, TensorType({DT_FLOAT16}))
  653. .INPUT(grads, TensorType({DT_FLOAT16}))
  654. .OUTPUT(y, TensorType({DT_FLOAT16}))
  655. .REQUIRED_ATTR(input_sizes, ListInt)
  656. .REQUIRED_ATTR(strides, ListInt)
  657. .ATTR(pads, ListInt, {0, 0, 0, 0, 0, 0})
  658. .ATTR(data_format, String, "NDHWC")
  659. .ATTR(dilations, ListInt, {1, 1, 1, 1, 1})
  660. .OP_END_FACTORY_REG(Conv3DBackpropInputD)
  661. REG_OP(LSTMQuant)
  662. .INPUT(x, TensorType({DT_FLOAT16,DT_INT8}))
  663. .INPUT(cont, TensorType({DT_FLOAT32,DT_FLOAT16}))
  664. .OPTIONAL_INPUT(x_static, TensorType({DT_FLOAT16,DT_INT8}))
  665. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16,DT_FLOAT32,DT_INT8}))
  666. .OPTIONAL_INPUT(c_0, TensorType({DT_FLOAT16,DT_FLOAT32}))
  667. .INPUT(w_x, TensorType({DT_FLOAT16,DT_INT8}))
  668. .INPUT(bias, TensorType({DT_FLOAT16,DT_FLOAT32,DT_INT16,DT_INT32}))
  669. .OPTIONAL_INPUT(w_x_static, TensorType({DT_FLOAT16,DT_INT8}))
  670. .INPUT(w_h, TensorType({DT_FLOAT16,DT_INT8}))
  671. .OPTIONAL_INPUT(w_xh_deqscale, TensorType({DT_FLOAT16}))
  672. .OPTIONAL_INPUT(w_x_static_deqscale, TensorType({DT_FLOAT16}))
  673. .OUTPUT(h, TensorType({DT_FLOAT16, DT_FLOAT,DT_INT8}))
  674. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT,DT_INT8}))
  675. .OUTPUT(c_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  676. .ATTR(num_output, Int, 0)
  677. .ATTR(expose_hidden, Bool, false)
  678. .ATTR(xh_scale, Float,0)
  679. .ATTR(sqrt_mode_xh, Bool, false)
  680. .ATTR(sqrt_mode_x_static, Bool, false)
  681. .ATTR(xh_offset, Int,0)
  682. .ATTR(x_static_scale, Float,0.0)
  683. .ATTR(x_static_offset, Int,0)
  684. .ATTR(w_xh_offset,ListInt,{0})
  685. .ATTR(w_x_static_offset,ListInt,{0})
  686. .OP_END_FACTORY_REG(LSTMQuant)
  687. } // namespace ge
  688. #endif // GE_OP_NN_CALCULATION_OPS_H

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