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 36 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. * @par Quantization supported or not
  253. * Yes
  254. */
  255. REG_OP(DepthwiseConv2D)
  256. .INPUT(x, TensorType({DT_FLOAT16, DT_INT8}))
  257. .INPUT(filter, TensorType({DT_FLOAT16, DT_INT8}))
  258. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_INT32}))
  259. .OPTIONAL_INPUT(offset_w, TensorType({DT_FLOAT16, DT_INT8}))
  260. .OUTPUT(y, TensorType({DT_FLOAT16, DT_INT32}))
  261. .REQUIRED_ATTR(strides, ListInt)
  262. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  263. .REQUIRED_ATTR(pads, ListInt)
  264. .ATTR(data_format, String, "NHWC")
  265. .ATTR(offset_x, Int, 0)
  266. .OP_END_FACTORY_REG(DepthwiseConv2D)
  267. REG_OP(Conv2DCCE)
  268. .INPUT(x, TensorType{DT_FLOAT}) // The input tensor
  269. .INPUT(w, TensorType({DT_FLOAT, DT_INT8})) // The weight tensor ,If QuantType =1 ,shall use type""tensor(int8)
  270. .OPTIONAL_INPUT(b, TensorType{DT_FLOAT}) // Optional 1D bias to be added to the convolution, has size of M.
  271. .OUTPUT(y, TensorType{DT_FLOAT}) // The output tensor
  272. .ATTR(mode, Int, 1)
  273. .ATTR(group, Int, 1) // number of groups input channels and output channels are divided into
  274. .ATTR(num_output, Int, 0) // number of output tensor
  275. .ATTR(pad, ListInt, {0, 0, 0, 0}) // Padding for the beginning and ending along each axis
  276. .ATTR(kernel, ListInt, {0, 0})
  277. .ATTR(stride, ListInt, {1, 1}) // Stride along each axis.
  278. .ATTR(dilation, ListInt, {1, 1}) // dilation value along each axis of the filter.
  279. .ATTR(pad_mode, Int, 0) // pad mode, 0:NOTSET, 1:SAME_UPPER, SAME_LOWER or 2:VALID.defaul default value is 0:NOTSET
  280. .ATTR(algo, Int, 2)
  281. .OP_END_FACTORY_REG(Conv2DCCE)
  282. REG_OP(Conv2DBackpropFilterCCE)
  283. .INPUT(x, TensorType{DT_FLOAT})
  284. .INPUT(filter_sizes, TensorType{DT_INT8})
  285. .INPUT(out_backprop, TensorType{DT_FLOAT})
  286. .OUTPUT(y, TensorType{DT_FLOAT})
  287. .ATTR(conv_grad_filter_output_shape, ListInt, {0, 0, 0, 0})
  288. .ATTR(mode, Int, 1)
  289. .ATTR(group, Int, 1)
  290. .ATTR(pad, ListInt, {0, 0, 0, 0})
  291. .ATTR(stride, ListInt, {1, 1})
  292. .ATTR(dilation, ListInt, {1, 1})
  293. .ATTR(padding, Int, 0) //pad_mode:same valid
  294. .ATTR(algo, Int, 0)
  295. .OP_END_FACTORY_REG(Conv2DBackpropFilterCCE)
  296. REG_OP(Conv2DBackpropInputCCE)
  297. .INPUT(input_sizes, TensorType{DT_INT8})
  298. .INPUT(filter, TensorType{DT_FLOAT})
  299. .INPUT(out_backprop, TensorType{DT_FLOAT})
  300. .OUTPUT(output, TensorType{DT_FLOAT})
  301. .ATTR(conv_grad_input_output_shape, ListInt, {0, 0, 0, 0})
  302. .ATTR(mode, Int, 1)
  303. .ATTR(format, Int, 0)
  304. .ATTR(group, Int, 1)
  305. .ATTR(pad_mode, Int, 0)
  306. .ATTR(stride, ListInt, {1, 1})
  307. .ATTR(dilation, ListInt, {1, 1})
  308. .ATTR(pad, ListInt, {0, 0, 0, 0})
  309. .ATTR(algo, Int, 0)
  310. .OP_END_FACTORY_REG(Conv2DBackpropInputCCE)
  311. /**
  312. *@brief Performs the the backward operation for "BiasAdd" on the "bias" tensor.
  313. * It accumulates all the values from out_backprop into the feature
  314. * dimension. For NHWC data format, the feature dimension is the last.
  315. * For NCHW data format, the feature dimension is the third-to-last.
  316. *@par Inputs:
  317. *x: A Tensor of type TensorType::NumberType().
  318. *@par Attributes:
  319. *data_format: Data format. Defaults to "NHWC".
  320. *@par Outputs:
  321. *y: A Tensor.Has the same type as "x".
  322. */
  323. REG_OP(BiasAddGrad)
  324. .INPUT(x, TensorType::NumberType())
  325. .OUTPUT(y, TensorType::NumberType())
  326. .ATTR(data_format, String, "NHWC")
  327. .OP_END_FACTORY_REG(BiasAddGrad)
  328. /**
  329. *@brief Computes the gradients of convolution with respect to the input.
  330. *@par Inputs:
  331. * Three inputs:
  332. * @li input_size: A Tensor of type int32. An integer vector representing the shape of input,
  333. * where input is a 4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  334. * @li filter: A Tensor. Must be one of the following types: float16, float32, float64.
  335. * 4-D with shape [filter_height, filter_width, in_channels, out_channels]
  336. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  337. * @li out_backprop: A Tensor. Must have the same type as filter. 4-D with shape [batch, out_height, out_width, out_channels]
  338. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  339. *@par Attributes:
  340. * Three attributes:
  341. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  342. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  343. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1]
  344. * @li groups: Number of blocked connections from input channels to output channels.
  345. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  346. *@par Outputs:
  347. * y: A Tensor. Has the same type as filter,and has same format as input_size
  348. */
  349. REG_OP(Conv2DBackpropInput)
  350. .INPUT(input_size, TensorType({DT_INT32}))
  351. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  352. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  353. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  354. .REQUIRED_ATTR(strides, ListInt)
  355. .REQUIRED_ATTR(pads, ListInt)
  356. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  357. .ATTR(groups, Int, 1)
  358. .ATTR(data_format, String, "NHWC")
  359. .OP_END_FACTORY_REG(Conv2DBackpropInput)
  360. /**
  361. *@brief Computes the gradients of convolution with respect to the input.
  362. *@par Inputs:
  363. * Two inputs:
  364. * @li filter: A Tensor. Types is float16.
  365. * 4-D with shape [filter_height, filter_width, in_channels, out_channels] or [out_channels, filter_height, filter_width, in_channels]
  366. * or [out_channels, in_channel, filter_height, filter_width].
  367. * @li out_backprop: A Tensor. Must have the same type as filter. 4-D with shape [batch, out_height, out_width, out_channels]
  368. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  369. *@par Attributes:
  370. * Four attributes:
  371. * @li input_size A Tensor of type int32. An integer vector representing the shape of input,
  372. * where input is a 4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  373. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  374. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  375. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1]
  376. * @li groups: Number of blocked connections from input channels to output channels.
  377. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  378. *@par Outputs:
  379. * y: A Tensor. Has the same type as filter,4-D tensor [batch, height, width, channels] or [batch, channels, height, width].
  380. */
  381. REG_OP(Conv2DBackpropInputD)
  382. .INPUT(filter, TensorType({DT_FLOAT16, DT_INT8}))
  383. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_INT8}))
  384. .OUTPUT(y, TensorType({DT_FLOAT16, DT_INT32}))
  385. .REQUIRED_ATTR(input_size, ListInt)
  386. .REQUIRED_ATTR(strides, ListInt)
  387. .REQUIRED_ATTR(pads, ListInt)
  388. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  389. .ATTR(groups, Int, 1)
  390. .ATTR(data_format, String, "NHWC")
  391. .OP_END_FACTORY_REG(Conv2DBackpropInputD)
  392. /**
  393. *@brief Computes the Deconvolution with respect to the input.
  394. *@par Inputs:
  395. * Three inputs:
  396. * @li x: A Tensor. Must have the same type as "filter". 4D with shape
  397. * [batch, out_height, out_width, out_channels]
  398. * or [batch, out_channels, out_height, out_width]. Gradients with respect
  399. * to the output of the convolution.
  400. * @li filter: A Tensor of type float16.
  401. * 4D with shape [filter_height, filter_width, in_channels, out_channels],
  402. * or [out_channels, filter_height, filter_width, in_channels],
  403. * or [out_channels, in_channel, filter_height, filter_width].
  404. * Two optional inputs:
  405. * @li bias: An optional tensor of type int8
  406. * @li offset_w: An optional 1D tensor for quantized deconvolution. Reserved.\n
  407. *@par Attributes:
  408. * Six attributes:
  409. * @li strides: A tuple or list of 2 integers. The stride of the sliding window
  410. * for H/W dimension.
  411. * @li pads: A tuple or list of 4 integers. The [top, bottom, left, right]
  412. * padding on the feature map
  413. * @li dilations: A tuple or list of 4 integers. The dilation factor for each
  414. * dimension of input. Must be [1, 1, 1, 1].
  415. * @li groups: Number of blocked connections from input channels to \n
  416. output channels.
  417. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC".\n
  418. Specify the data format of the input and output data.
  419. * @li offset_x: An optional integer for quantized deconvolution.
  420. *@par Outputs:
  421. * y: A Tensor. Has the same type as "filter". 4D tensor with shape
  422. * [batch, height, width, channels] or [batch, channels, height, width].
  423. */
  424. REG_OP(Deconvolution)
  425. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  426. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  427. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  428. .OPTIONAL_INPUT(offset_w, TensorType({DT_INT8}))
  429. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  430. .ATTR(strides, ListInt, {1, 1, 1, 1})
  431. .ATTR(pads, ListInt, {0, 0, 0, 0})
  432. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  433. .ATTR(groups, Int, 1)
  434. .ATTR(data_format, String, "NHWC")
  435. .ATTR(offset_x, Int, 0)
  436. .OP_END_FACTORY_REG(Deconvolution)
  437. /**
  438. *@brief Computes the gradients of convolution with respect to the filter
  439. *@par Inputs:
  440. * Three inputs:
  441. * @li x: A Tensor. Must be one of the following types: float16, float32, float64.
  442. * 4-D with shape [batch, in_height, in_width, in_channels] or [batch, in_channels, in_height, in_width].
  443. * @li filter_size: A Tensor of type int32. An integer vector representing the tensor shape of filter,
  444. * where filter is a 4-D tensor [filter_height, filter_width, in_channels, out_channels]
  445. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  446. * @li out_backprop: A Tensor. Must have the same type as x. 4-D with shape [batch, out_height, out_width, out_channels]
  447. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  448. *@par Attributes:
  449. * Three attributes:
  450. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  451. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map.
  452. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1].
  453. * @li groups: Number of blocked connections from input channels to output channels.
  454. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  455. *@par Outputs:
  456. * y: A Tensor. Has the same type as x
  457. */
  458. REG_OP(Conv2DBackpropFilter)
  459. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  460. .INPUT(filter_size, TensorType({DT_INT32}))
  461. .INPUT(out_backprop, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  462. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  463. .REQUIRED_ATTR(strides, ListInt)
  464. .REQUIRED_ATTR(pads, ListInt)
  465. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  466. .ATTR(groups, Int, 1)
  467. .ATTR(data_format, String, "NHWC")
  468. .OP_END_FACTORY_REG(Conv2DBackpropFilter)
  469. /**
  470. *@brief Computes the gradients of convolution with respect to the filter.
  471. *@par Inputs:
  472. * Two inputs:
  473. * @li x: A Tensor. Type is float16.
  474. * 4-D with shape [batch, in_height, in_width, in_channels] or [batch, in_channels, in_height, in_width].
  475. * @li out_backprop: A Tensor. Must have the same type as x. 4-D with shape [batch, out_height, out_width, out_channels]
  476. * or [batch, out_channels, out_height, out_width]. Gradients with respect to the output of the convolution.
  477. *@par Attributes:
  478. * Four attributes:
  479. * @li filter_size: A Tensor of type integers. An integer vector representing the tensor shape of filter,
  480. * where filter is a 4-D tensor [filter_height, filter_width, in_channels, out_channels]
  481. * or [out_channels, filter_height, filter_width, in_channels] or [out_channels, in_channel, filter_height, filter_width].
  482. * @li strides: A tuple/list of 2 integers. The stride of the sliding window for H/W dimension.
  483. * @li pads: A tuple/list of 4 integers, [top, bottom, left, right] pads on feature map
  484. * @li dilations: A tuple/list of 4 integers, The dilation factor for each dimension of input, now only support [1,1,1,1].
  485. * @li groups: Number of blocked connections from input channels to output channels.
  486. * @li data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC". Specify the data format of the input and output data.
  487. *@par Outputs:
  488. * y: A Tensor. Has the same type as x
  489. */
  490. REG_OP(Conv2DBackpropFilterD)
  491. .INPUT(x, TensorType({DT_FLOAT16}))
  492. .INPUT(out_backprop, TensorType({DT_FLOAT16}))
  493. .OUTPUT(y, TensorType({DT_FLOAT}))
  494. .REQUIRED_ATTR(filter_size, ListInt)
  495. .REQUIRED_ATTR(strides, ListInt)
  496. .REQUIRED_ATTR(pads, ListInt)
  497. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  498. .ATTR(groups, Int, 1)
  499. .ATTR(data_format, String, "NHWC")
  500. .OP_END_FACTORY_REG(Conv2DBackpropFilterD)
  501. /**
  502. *@brief Computes a 2D convolution given 4D "x" and "filter" tensors.
  503. *@par Inputs:
  504. * @li x: A 4D tensor of input images.
  505. * @li filter: A 4D tensor of filters.
  506. * @li bias: An optional 1D tensor.
  507. * @li offset_w: An optional 1D tensor for quantized convolution. Reserved.\n
  508. * \n
  509. * The input and output tensor attributes are listed as follows:
  510. * @verbatim
  511. Tensor | x | filter | bias | offset_w | y
  512. -----------|---------|---------|---------|----------|--------
  513. Data Type | float16 | float16 | float16 | _ | float16
  514. |---------|---------|---------|----------|--------
  515. | float32 | float32 | float32 | _ | float32
  516. |---------|---------|---------|----------|--------
  517. | float64 | float64 | float64 | _ | float64
  518. |---------|---------|---------|----------|--------
  519. | int8 | int8 | int32 | int8 | int32
  520. -----------|---------|---------|---------|----------|--------
  521. Format | NCHW | NCHW | ND | ND | NCHW
  522. | NHWC | NHWC | | | NHWC
  523. | | HWCN | | |
  524. @endverbatim
  525. * It should be noted that the data types must correspond to each other, but the
  526. * format does not need to.
  527. *@par Attributes:
  528. * @li strides: A list of 4 integers. Specifying the strides of the
  529. * convolution along the height and width. The dimension order is determined
  530. * by the data format of "x". By default the N and C dimensions are set to 1.
  531. * @li pads: A list of 4 integers. Specifying the top, bottom, left and right
  532. * padding.
  533. * @li dilations: A list of 4 integers. Specifying the dilation rate to use
  534. * for dilated convolution. Has the same dimension order and value as "strides".
  535. * @li groups: Number of blocked connections from input channels to output
  536. * channels. Input channels and output channels must both be divisible by
  537. * "groups". Must be set to 1.
  538. * @li offset_x: An optional integer for quantized convolution.
  539. * @li data_format: An optional string from: "NHWC", "NCHW". Specifying the
  540. * data format of the input and output images. Reserved.
  541. *@par Outputs:
  542. * @li y: A 4D Tensor of output images.
  543. *@attention
  544. * @li The parameter scope is listed as follows:\n
  545. * @verbatim
  546. Name | Field | Scope
  547. ------------------|--------------|----------
  548. Input Image Size | H dimension | [1, 4096]
  549. | W dimension | [1, 4096]
  550. ------------------|--------------|----------
  551. Filter Size | H dimension | [1, 255]
  552. | W dimension | [1, 255]
  553. ------------------|--------------|----------
  554. Stride Size | H dimension | [1, 63]
  555. | W dimension | [1, 63]
  556. ------------------|--------------|----------
  557. Padding Size | top side | [0, 255]
  558. | bottom side | [0, 255]
  559. | left side | [0, 255]
  560. | right side | [0, 255]
  561. ------------------|--------------|----------
  562. Dilation Size | H dimension | [1, 255]
  563. | W dimension | [1, 255]
  564. @endverbatim
  565. * @li There are restrictions for certain scenarios:
  566. * @verbatim
  567. Output | Restrictions
  568. ------------------|----------------------------------------------
  569. W dimension == 1 | HxW(input) == HxW(filter) == 1x1,2x2...11x11.
  570. H dimension == 1 |
  571. ------------------|----------------------------------------------
  572. W dimension == 1 | Not supported
  573. H dimension != 1 |
  574. @endverbatim
  575. * As shown above, "HxW(input)" indicates the image size after padding and
  576. * "HxW(filter)" indicates the filter size after dilation.
  577. *@par Quantization supported or not
  578. * Yes
  579. */
  580. REG_OP(Conv2D)
  581. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  582. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  583. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  584. .OPTIONAL_INPUT(offset_w, TensorType({DT_INT8}))
  585. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  586. .REQUIRED_ATTR(strides, ListInt)
  587. .REQUIRED_ATTR(pads, ListInt)
  588. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  589. .ATTR(groups, Int, 1)
  590. .ATTR(data_format, String, "NHWC")
  591. .ATTR(offset_x, Int, 0)
  592. .OP_END_FACTORY_REG(Conv2D)
  593. REG_OP(Conv2DCompress)
  594. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  595. .INPUT(filter_compress, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT8}))
  596. .INPUT(compress_index, TensorType({DT_INT8}))
  597. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  598. .OPTIONAL_INPUT(offset_w, TensorType({DT_INT8}))
  599. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32}))
  600. .REQUIRED_ATTR(strides, ListInt)
  601. .REQUIRED_ATTR(pads, ListInt)
  602. .ATTR(dilations, ListInt, {1, 1, 1, 1})
  603. .ATTR(groups, Int, 1)
  604. .ATTR(data_format, String, "NHWC")
  605. .ATTR(offset_x, Int, 0)
  606. .OP_END_FACTORY_REG(Conv2DCompress)
  607. /**
  608. *@brief Computes a 3D convolution given 5D "x" and "filter" tensors.
  609. *@par Inputs:
  610. *@li x: A 5D tensor. Must be one of the following types: float16, float32, float64. The format is NCDHW or NDHWC.
  611. *@li filter: A 5D tensor of the same type as "x". The format is NCDHW, NDHWC or DHWCN.
  612. *@li bias: An optional 1D tensor of the same type as "x".
  613. *@par Attributes:
  614. *@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".
  615. *@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.
  616. *@li data_format: An optional string from: "NDHWC", "NCDHW". Defaults to "NDHWC". Specify the data format of the input and output data.
  617. *@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".
  618. *@par Outputs:
  619. *y: A Tensor. Has the same type as "x".
  620. *@attention Constraints:\n
  621. *The image size after padding is greater than the filter size.\n
  622. */
  623. REG_OP(Conv3D)
  624. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  625. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  626. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  627. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  628. .ATTR(strides, ListInt, {1, 1, 1, 1, 1})
  629. .ATTR(pads, ListInt, {0, 0, 0, 0, 0, 0})
  630. .ATTR(data_format, String, "NDHWC")
  631. .ATTR(dilations, ListInt, {1, 1, 1, 1, 1})
  632. .OP_END_FACTORY_REG(Conv3D)
  633. /**
  634. *@brief Computes the gradients of convolution 3d with respect to the input.
  635. *@par Inputs:
  636. * Three inputs:
  637. * @li input_size: A Tensor of type int32, int64. An integer vector representing the shape of input,
  638. * where input is a 5-D tensor [batch, depth, height, width, channels] or [batch, channels, depth, height, width].
  639. * @li filter: A Tensor. Must be one of the following types: float16, float32, float64.
  640. * @li grads: A Tensor. Must have the same type as filter. 5-D with shape [batch, depth, out_height, out_width, out_channels]
  641. * or [batch, out_channels, depth, out_height, out_width]. Gradients with respect to the output of the convolution.
  642. *@par Attributes:
  643. * Four attributes:
  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 6 integers
  646. * @li dilations: A tuple/list of 6 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,and has same format as input_size
  650. */
  651. REG_OP(Conv3DBackpropInput)
  652. .INPUT(input_size, TensorType({DT_INT32, DT_INT64}))
  653. .INPUT(filter, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  654. .INPUT(grads, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  655. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  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(Conv3DBackpropInput)
  661. /**
  662. *@brief Computes the gradients of convolution 3d with respect to the input.
  663. *@par Inputs:
  664. * Two inputs:
  665. * @li filter: A Tensor. Types is float16.
  666. * @li grads: A Tensor. Must have the same type as filter.
  667. *@par Attributes:
  668. * Five attributes:
  669. * @li input_size A Tensor of type int32. An integer vector representing the shape of input,
  670. * @li strides: A tuple/list of 3 integers. The stride of the sliding window for D/H/W dimension.
  671. * @li pads: A tuple/list of 4 integers
  672. * @li dilations: A tuple/list of 5 integers, The dilation factor for each dimension of input, now only support [1,1,1,1,1]
  673. * @li data_format: An optional string from: "NDHWC", "NCHWD". Defaults to "NDHWC". Specify the data format of the input and output data.
  674. *@par Outputs:
  675. * y: A Tensor. Has the same type as filter
  676. */
  677. REG_OP(Conv3DBackpropInputD)
  678. .INPUT(filter, TensorType({DT_FLOAT16}))
  679. .INPUT(grads, TensorType({DT_FLOAT16}))
  680. .OUTPUT(y, TensorType({DT_FLOAT16}))
  681. .REQUIRED_ATTR(input_size, ListInt)
  682. .REQUIRED_ATTR(strides, ListInt)
  683. .ATTR(pads, ListInt, {0, 0, 0, 0, 0, 0})
  684. .ATTR(data_format, String, "NDHWC")
  685. .ATTR(dilations, ListInt, {1, 1, 1, 1, 1})
  686. .OP_END_FACTORY_REG(Conv3DBackpropInputD)
  687. REG_OP(LSTM)
  688. .INPUT(x, TensorType({DT_FLOAT16}))
  689. .INPUT(cont, TensorType({DT_FLOAT32,DT_FLOAT16}))
  690. .INPUT(w_x, TensorType({DT_FLOAT16}))
  691. .INPUT(bias, TensorType({DT_FLOAT16,DT_FLOAT32,DT_INT16,DT_INT32}))
  692. .INPUT(w_h, TensorType({DT_FLOAT16}))
  693. .OPTIONAL_INPUT(x_static, TensorType({DT_FLOAT16}))
  694. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16,DT_FLOAT32}))
  695. .OPTIONAL_INPUT(c_0, TensorType({DT_FLOAT16,DT_FLOAT32}))
  696. .OPTIONAL_INPUT(w_x_static, TensorType({DT_FLOAT16}))
  697. .OUTPUT(h, TensorType({DT_FLOAT16, DT_FLOAT}))
  698. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  699. .OUTPUT(c_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  700. .ATTR(num_output, Int, 0)
  701. .ATTR(expose_hidden, Bool, false)
  702. .OP_END_FACTORY_REG(LSTM)
  703. } // namespace ge
  704. #endif // GE_OP_NN_CALCULATION_OPS_H

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