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_norm_ops.h 33 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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_NORM_OPS_H
  17. #define GE_OP_NN_NORM_OPS_H
  18. #include "graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. *@brief Computes the gradient for log softmax activations.
  22. *@par Inputs:
  23. *@li grad: A Tensor. Must be one of the following types: float16, float32.
  24. *@li x: A Tensor. Must be one of the following types: float16, float32.
  25. *@par Attributes:
  26. * axis: An optional list of ints. Defaults to "{-1}".
  27. *@par Outputs:
  28. * y: A Tensor. Has the same type as "grad".
  29. */
  30. REG_OP(LogSoftmaxGrad)
  31. .INPUT(grad, TensorType({DT_FLOAT16, DT_FLOAT}))
  32. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  33. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  34. .ATTR(axis, ListInt, {-1})
  35. .OP_END_FACTORY_REG(LogSoftmaxGrad)
  36. REG_OP(SparseSoftmaxCrossEntropyWithLogitsCCE)
  37. .INPUT(features, TensorType{DT_FLOAT})
  38. .INPUT(labels, TensorType{DT_FLOAT})
  39. .OUTPUT(out, TensorType{DT_FLOAT})
  40. .OUTPUT(non, TensorType{DT_FLOAT})
  41. .ATTR(cross_entropy_is_grad, Bool, 0)
  42. .ATTR(cross_entropy_mode, Int, 1)
  43. .ATTR(softmax_cross_entropy_lossscale_div_batch, Float, 1.0)
  44. .OP_END_FACTORY_REG(SparseSoftmaxCrossEntropyWithLogitsCCE)
  45. /**
  46. *@brief Computes sparse softmax cross entropy cost and gradients to backpropagate.
  47. *@par Inputs:
  48. *Two inputs, including:
  49. * @li features: A Tensor. Must be one of the following types: half, float32, double.
  50. * A "batch_size * num_classes" matrix.
  51. * @li labels: A Tensor of the same type as "features". batch_size vector with values in [0, num_classes).
  52. *@par Outputs:
  53. *loss: A Tensor for per example loss (a "batch_size" vector). Has the same type as "features".
  54. *backprop: A Tensor for the backpropagated gradients (a batch_size * num_classes matrix). Has the same type as "features".
  55. */
  56. REG_OP(SparseSoftmaxCrossEntropyWithLogits)
  57. .INPUT(features, TensorType({DT_FLOAT16,DT_FLOAT}))
  58. .INPUT(labels, TensorType({DT_INT32, DT_INT64}))
  59. .OUTPUT(loss, TensorType({DT_FLOAT16,DT_FLOAT}))
  60. .OUTPUT(backprop, TensorType({DT_FLOAT16,DT_FLOAT}))
  61. .OP_END_FACTORY_REG(SparseSoftmaxCrossEntropyWithLogits)
  62. /**
  63. *@brief Computes softmax cross entropy cost and gradients to backpropagate.
  64. *@par Inputs:
  65. *Two inputs, including:
  66. * @li features: A Tensor. Must be one of the following types: half, float32, double.
  67. * A "batch_size * num_classes" matrix.
  68. * @li labels: A Tensor of the same type as "features". A "batch_size * num_classes" matrix.
  69. *@par Outputs:
  70. *loss: A Tensor for per example loss (a "batch_size" vector). Has the same type as "features".
  71. *backprop: A Tensor for the backpropagated gradients (a batch_size * num_classes matrix). Has the same type as "features".
  72. */
  73. REG_OP(SoftmaxCrossEntropyWithLogits)
  74. .INPUT(features, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  75. .INPUT(labels, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  76. .OUTPUT(loss, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  77. .OUTPUT(backprop, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  78. .OP_END_FACTORY_REG(SoftmaxCrossEntropyWithLogits)
  79. /**
  80. *@brief Computes gradients for a softmax operation.
  81. *@par Inputs:
  82. * Two inputs, including: \n
  83. * @li softmax: Output of the softmax operator. Must be one of the following types: float16, float31, int32, int8, uint8. The format is NC1HWC0 or DN.
  84. * @li grad_softmax: A Tensor. Has the same shape and type as "softmax". The format is NC1HWC0 or DN.
  85. *@par Outputs:
  86. *grad_x: A Tensor. Has the same shape and type as "softmax".
  87. */
  88. REG_OP(SoftmaxGrad)
  89. .INPUT(softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  90. .INPUT(grad_softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  91. .OUTPUT(grad_x, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  92. .OP_END_FACTORY_REG(SoftmaxGrad)
  93. /**
  94. *@brief Computes the sigmoid cross entropy loss of "predict" and "target".
  95. *@par Inputs:
  96. * Two inputs, including: \n
  97. *@li predict: A multi-dimensional Tensor of type float16 or float32, specifying the predictive value.
  98. *@li target: A multi-dimensional Tensor of type float16 or float32, specifying the target value.
  99. *@par Outputs:
  100. *loss: Sigmoid cross entropy between the predictive value and target value. Has the same dimensions as "predict".
  101. */
  102. REG_OP(SigmoidCrossEntropyWithLogitsGrad)
  103. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  104. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  105. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  106. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  107. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogitsGrad)
  108. /**
  109. *@brief Performs the backpropagation of SigmoidCrossEntropyWithLogits for training scenarios.
  110. *@par Inputs:
  111. * Three inputs, including: \n
  112. *@li predict: A multi-dimensional Tensor of type float16 or float32, specifying the predictive value.
  113. *@li target: A multi-dimensional Tensor of type float16 or float32, specifying the target value.
  114. *@li dout: A multi-dimensional Tensor of float16 or float32, specifying the gradient transferred from the upper layer.
  115. *@par Outputs: \n
  116. *gradient: Return gradient. Has the same dimensions and type as "predict".
  117. */
  118. REG_OP(SigmoidCrossEntropyWithLogits)
  119. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  120. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  121. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  122. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogits)
  123. /**
  124. *@brief Computes the regression box of the RPN. It is a FasterRCNN operator.
  125. *@par Inputs:
  126. * Two inputs, including: \n
  127. *@li predict: A multi-dimensional Tensor of type float16 or float32, specifying the predictive value.
  128. *@li label: A multi-dimensional Tensor of type float16 or float32, specifying the target value.
  129. *@par Attributes:
  130. * sigma: Must be a floating point number. Defaults to "1.0".
  131. *@par Outputs:
  132. *loss: Indicates the loss between the predictive value and target value. Has the same dimensions as "predict".
  133. *@attention Constraints:
  134. * This operator does not perform the "reduce" operation on the loss value. Call other reduce operators to perform "reduce" operation on the loss if required.
  135. */
  136. REG_OP(SmoothL1Loss)
  137. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  138. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  139. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  140. .ATTR(sigma, Float, 1.0)
  141. .OP_END_FACTORY_REG(SmoothL1Loss)
  142. /**
  143. *@brief Performs the backpropagation of SmoothL1Loss for training scenarios.
  144. *@par Inputs:
  145. * Three inputs, including: \n
  146. *@li predict: A multi-dimensional Tensor of type float16 or float32, specifying the predictive value.
  147. *@li label: A multi-dimensional Tensor of float16 or float32, specifying the target value.
  148. *@li dout: A multi-dimensional Tensor of float16 or float32, specifying the gradient transferred from the upper layer.
  149. *@par Attributes:
  150. * sigma: Must be a floating point number. Defaults to "1.0".
  151. *@par Outputs:
  152. *gradient: Return gradient. Has the same dimensions and type as "predict".
  153. */
  154. REG_OP(SmoothL1LossGrad)
  155. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  156. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  157. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  158. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  159. .ATTR(sigma, Float, 1.0)
  160. .OP_END_FACTORY_REG(SmoothL1LossGrad)
  161. /**
  162. *@brief Creates a criterion that measures the Binary Cross Entropy between the target and the output.
  163. *@par Inputs:
  164. * Three inputs, including: \n
  165. *@li x: A 1D or 2D Tensor of type float16 or float32, specifying a predictive value.
  166. *@li y: A 1D or 2D Tensor of type float16 or float32, indicating a tag.
  167. *@li weight: An optional 1D or 2D Tensor, specifying the weight.
  168. *@par Attributes:
  169. *reduction: A character string from "none", "mean", and "sum", specifying the reduction type to be applied to the output. Defaults to "mean".
  170. *@par Outputs:
  171. *output: Output loss. Has the same dimension with the inputs. When "reduction" is set to "none", a Tensor with the same size as "x" is output. Otherwise, a Scalar is output.
  172. *@attention Constraints:
  173. *@li The value of "x" must range from 0 to 1.
  174. *@li The value of "y" must be "0" or "1".
  175. */
  176. REG_OP(BinaryCrossEntropy)
  177. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  178. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  179. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  180. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  181. .ATTR(reduction, String, "mean")
  182. .OP_END_FACTORY_REG(BinaryCrossEntropy)
  183. /**
  184. *@brief Performs the backpropagation of BinaryCrossEntropy for training scenarios.
  185. *@par Inputs:
  186. * Four inputs, including: \n
  187. *@li x: A 1D or 2D Tensor of type float16 or float32, specifying a predictive value.
  188. *@li y: A 1D or 2D Tensor of type float16 or float32, indicating a tag.
  189. *@li grad_output: A 1D or 2D Tensor of type float16 or float32, specifying the backpropagation gradient.
  190. *@li weight: An optional 1D or 2D Tensor, specifying the weight.
  191. *@par Attributes: \n
  192. *reduction: A character string from "none", "mean", and "sum", specifying the gradient output mode. Defaults to "mean".
  193. *@par Outputs: \n
  194. *output: A 1D or 2D Tensor. When "reduction" is set to "none", a Tensor with the same size as "x" is output. Otherwise, a Scalar is output.
  195. *@attention Constraints:
  196. *@li The value of "x" must range from 0 to 1.
  197. *@li The value of "y" must be "0" or "1".
  198. */
  199. REG_OP(BinaryCrossEntropyGrad)
  200. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  201. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  202. .INPUT(grad_output, TensorType({DT_FLOAT, DT_FLOAT16}))
  203. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  204. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  205. .ATTR(reduction, String, "mean")
  206. .OP_END_FACTORY_REG(BinaryCrossEntropyGrad)
  207. /**
  208. *@brief Applies the Softmax function to an n-dimensional input Tensor rescaling them \n so
  209. that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.
  210. *@par Inputs:
  211. *One input:
  212. *x: A mutable Tensor. Must be one of the following types: float16,
  213. *float32, double. Should be a Variable Tensor.
  214. *@par Attributes:
  215. *axes: A list of ints. The dimension softmax would be performed on.
  216. *@par Outputs:
  217. *y: A Tensor. Has the same dimensionality and shape as the "x" with values in the range [0, 1]. Must be one of the following types: float16, float32, int32.
  218. */
  219. REG_OP(SoftmaxV2)
  220. .INPUT(x, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  221. .OUTPUT(y, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  222. .ATTR(axes, ListInt, {-1})
  223. .OP_END_FACTORY_REG(SoftmaxV2)
  224. /**
  225. *@brief Computes log softmax activations.
  226. *@par Inputs:
  227. *One input:
  228. * logits: A Tensor. Must be one of the following types: double, float16, float32.
  229. *@par Attributes:
  230. * axes: An optional list of ints. Defaults to "{-1}".
  231. *@par Outputs:
  232. * logsoftmax: A Tensor. Has the same type as "logits".
  233. */
  234. REG_OP(LogSoftmaxV2)
  235. .INPUT(logits, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  236. .OUTPUT(logsoftmax, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  237. .ATTR(axes, ListInt, {-1})
  238. .OP_END_FACTORY_REG(LogSoftmaxV2)
  239. REG_OP(FusedBatchNormV2)
  240. .INPUT(x, TensorType{DT_FLOAT}) /* Input data tensor from the previous operator"" */
  241. .INPUT(scale, TensorType{DT_FLOAT}) /* If spatial is true, the dimension of bias is (C) If spatial is false, the dimensions of scale are (C x D1 x ... x Dn)*/
  242. .INPUT(b, TensorType{DT_FLOAT}) /* If spatial is true, the dimension of bias is (C) If spatial is false, the dimensions of scale are (C x D1 x ... x Dn)*/
  243. .OPTIONAL_INPUT(mean, TensorType{DT_FLOAT}) /* If spatial is true, the dimension of the running mean (training) or the estimated mean (testing) is (C).If spatial is false, the dimensions of the running mean (training) or the estimated mean (testing) are (C x D1 x ... x Dn)*/
  244. .OPTIONAL_INPUT(variance, TensorType{DT_FLOAT}) /* If spatial is true, the dimension of the running variance(training) or the estimated variance (testing) is (C). If spatial is false, the dimensions of the running variance(training) or the estimated variance (testing) are (C x D1 x ... x Dn).*/
  245. .OUTPUT(y, TensorType{DT_FLOAT}) /* The output tensor of the same shape as X */
  246. .ATTR(momentum, Float, 0.9) // Factor used in computing the running mean and variance.
  247. .ATTR(epsilon, Float, 1e-5f) // The epsilon value to use to avoid division by zero
  248. .ATTR(mode, Int, 1) // 1 means using "CC_BATCHNORM_SPATIAL"; 0 means using "CC_BATCHNORM_PER_ACTIVATION"; only support 1 now
  249. .ATTR(use_global_stats, Bool, true)
  250. .ATTR(alpha, Float, 1)
  251. .ATTR(beta, Float, 0)
  252. .OP_END_FACTORY_REG(FusedBatchNormV2)
  253. /**
  254. *@brief Confuse mul, sum and sub.
  255. *@par Inputs:
  256. *Two inputs, including:
  257. * @li grad: A Tensor. Must be one of the following types: float16, float32.
  258. * @li x: A Tensor. Must be one of the following types: float16, float32.
  259. *@par Outputs:
  260. * y: A Tensor of the same type as "grad".
  261. */
  262. REG_OP(ConfusionSoftmaxGrad)
  263. .INPUT(grad, TensorType({DT_FLOAT16,DT_FLOAT}))
  264. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  265. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  266. .OP_END_FACTORY_REG(ConfusionSoftmaxGrad)
  267. REG_OP(SoftmaxGradExt)
  268. .INPUT(grad, TensorType({DT_FLOAT16,DT_FLOAT}))
  269. .INPUT(x1, TensorType({DT_FLOAT16,DT_FLOAT}))
  270. .INPUT(x2, TensorType({DT_FLOAT16,DT_FLOAT}))
  271. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  272. .ATTR(axes, Int, 1)
  273. .ATTR(keep_dims, Bool, false)
  274. .OP_END_FACTORY_REG(SoftmaxGradExt)
  275. /**
  276. *@brief Normalizes the input.
  277. *@par Inputs:
  278. * One input:
  279. *x: An NCHW tensor of type float16 or float32.
  280. *@par Attributes:
  281. *@li normalize_variance: An optional bool specifying whether to normalize the variance, either "true" (default) or "false"
  282. * the value "false" indicates only to subtract the mean.
  283. *@li across_channels: An optional bool specifying whether to perform across-channel MVN, either "true" or "false" (default)
  284. * The value "true" indicates "CHW" is treated as a vector.
  285. *@li eps: An optional float32 epsilon for not dividing by zero. Defaults to "1e-9".
  286. *@par Outputs:
  287. *y: An NCHW tensor of type float16 or float32.
  288. *@attention Constraints:\n
  289. * The input tensor must have the NCHW format, whose shape length must be 4.
  290. */
  291. REG_OP(MVN)
  292. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16})) /* "First operand." */
  293. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16})) /* "Result, has same element type as inputs" */
  294. .ATTR(normalize_variance, Bool, true)
  295. .ATTR(across_channels, Bool, false)
  296. .ATTR(eps, Float, 1e-9)
  297. .OP_END_FACTORY_REG(MVN)
  298. /**
  299. *@brief Normalizes the input "x1".
  300. *@par Inputs:
  301. * Two inputs, including:
  302. *@li x1: A required NCHW or NHWC tensor of type float32, float16, or int8.
  303. *@li x2: A required ND tensor of type float32, float16, or int8, specifying
  304. * the scaling factor. If "channel_shared" is "true", "x2" is a [1]-dimensional
  305. * vector. If "channel_shared" is "false", "x2" is a [C]-dimensional vector.
  306. *@par Attributes:
  307. *@li across_spatial: An optional bool, specifying the dimension of input "x1"
  308. * to be summed. The value "true" (default) indicates dimensions C, H, W, and
  309. * the value "false" indicates dimension C.
  310. *@li channel_shared: An optional bool, specifying the dimension count of input
  311. * "x2". The value "true" (default) indicates 1, and the value "false" indicates
  312. * dimension C of "x1".
  313. *@li eps: An optional float32, specifying the bias when "across_spatial" is
  314. * "true". Defaults to "1e-10".
  315. *@par Outputs:
  316. *y: A Tensor. Has the same type and format as "x1".
  317. */
  318. REG_OP(Normalize)
  319. .INPUT(x1, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8}))
  320. .INPUT(x2, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8}))
  321. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8}))
  322. .ATTR(across_spatial, Bool, true)
  323. .ATTR(channel_shared, Bool, true)
  324. .ATTR(eps, Float, 1e-10)
  325. .OP_END_FACTORY_REG(Normalize);
  326. /**
  327. *@brief Layernorm operator interface implementation
  328. * calculating: x, gamma, beta
  329. * mean = np.mean(x, reduce_axis, keepdims=True)
  330. * variance = np.mean(np.power((x - mean),2), reduce_axis, keepdims=True)
  331. * y = gamma*((x - mean) / np.sqrt(variance + 0.001)) + beta
  332. *@par Inputs:
  333. *Three inputs, including:
  334. * @li x: A Tensor. Must be one of the following types: float16, float32.
  335. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  336. * @li beta: A Tensor. Must be one of the following types: float16, float32.
  337. *@par Attributes:
  338. * @li begin_norm_axis: A required attribute, the type is int32.
  339. * @li begin_params_axis: A required attribute,the type is int32.
  340. *@par Outputs:
  341. *Three outputs, including:
  342. * @li y: A Tensor. Must be one of the following types: float16, float32.
  343. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  344. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  345. */
  346. REG_OP(LayerNorm)
  347. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  348. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  349. .INPUT(beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  350. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  351. .OUTPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  352. .OUTPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  353. .ATTR(begin_norm_axis, Int, 0)
  354. .ATTR(begin_params_axis, Int, 0)
  355. .ATTR(epsilon, Float, 0.0000001)
  356. .OP_END_FACTORY_REG(LayerNorm)
  357. /**
  358. *@brief LayerNormGrad operator interface implementation
  359. * calculating: dy, x, variance, mean, gamma
  360. * pd_xl = data_dy*data_gamma
  361. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  362. * np.power((data_variance + EPSLON), (-1.5))),
  363. * reduce_axis, keepdims=True)
  364. * pd_mean = np.sum(((-1.0)*pd_xl
  365. * np.power((data_variance + EPSLON), (-0.5))),
  366. * reduce_axis, keepdims=True)
  367. * + pd_var*(1.0/m)
  368. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  369. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  370. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  371. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  372. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  373. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  374. *@par Inputs:
  375. *Three inputs, including:
  376. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  377. * @li x: A Tensor. Must be one of the following types: float16, float32.
  378. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  379. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  380. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  381. *@par Outputs:
  382. *Three outputs, including:
  383. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  384. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  385. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  386. */
  387. REG_OP(LayerNormGrad)
  388. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  389. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  390. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  391. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  392. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  393. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  394. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  395. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  396. .OP_END_FACTORY_REG(LayerNormGrad)
  397. /**
  398. *@brief LayerNormXBackprop operator interface implementation
  399. * calculating: dy, x, variance, mean, gamma
  400. * pd_xl = data_dy*data_gamma
  401. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  402. * np.power((data_variance + EPSLON), (-1.5))),
  403. * reduce_axis, keepdims=True)
  404. * pd_mean = np.sum(((-1.0)*pd_xl
  405. * np.power((data_variance + EPSLON), (-0.5))),
  406. * reduce_axis, keepdims=True)
  407. * + pd_var*(1.0/m)
  408. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  409. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  410. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  411. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  412. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  413. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  414. *@par Inputs:
  415. *Three inputs, including:
  416. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  417. * @li x: A Tensor. Must be one of the following types: float16, float32.
  418. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  419. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  420. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  421. *@par Outputs:
  422. *Three outputs, including:
  423. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  424. */
  425. REG_OP(LayerNormXBackprop)
  426. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  427. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  428. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  429. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  430. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  431. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  432. .OP_END_FACTORY_REG(LayerNormXBackprop)
  433. /**
  434. *@brief LayerNormBetaGammaBackprop operator interface implementation
  435. * calculating: dy, x, variance, mean
  436. * pd_xl = data_dy*data_gamma
  437. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  438. * np.power((data_variance + EPSLON), (-1.5))),
  439. * reduce_axis, keepdims=True)
  440. * pd_mean = np.sum(((-1.0)*pd_xl
  441. * np.power((data_variance + EPSLON), (-0.5))),
  442. * reduce_axis, keepdims=True)
  443. * + pd_var*(1.0/m)
  444. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  445. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  446. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  447. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  448. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  449. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  450. *@par Inputs:
  451. *Three inputs, including:
  452. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  453. * @li x: A Tensor. Must be one of the following types: float16, float32.
  454. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  455. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  456. *@par Outputs:
  457. *Three outputs, including:
  458. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  459. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  460. */
  461. REG_OP(LayerNormBetaGammaBackprop)
  462. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  463. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  464. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  465. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  466. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  467. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  468. .REQUIRED_ATTR(shape_gamma, ListInt)
  469. .OP_END_FACTORY_REG(LayerNormBetaGammaBackprop)
  470. /**
  471. *@brief Return "output" according to the algorithm of dropout_do_mask: \n
  472. * scale_x = x *(1 / keep_prob)
  473. * output = select(mask == 1, scale_x, 0)
  474. *@par Inputs:
  475. *Three inputs, including: \n
  476. * @li x: A mutable Tensor. Must be one of the following types:
  477. * float16, float32
  478. * @li mask: A mutable Tensor. Must met all of the following rules:
  479. * shape of mask should be 1D.
  480. * dtype of mask should be uint8.
  481. * value of shape should met the following algorithm:
  482. * value = (size(x) + 128 - 1) // 128 * 128 //8
  483. * @li keep_prob: A mutable Tensor. Must met all of the following rules:
  484. * shape of "keep_prob" should be (1,) or [1,].
  485. * Has the same type as "x".
  486. *@par Output:
  487. *y: A mutable Tensor. Has the same type as "x".
  488. */
  489. REG_OP(DropOutDoMask)
  490. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  491. .INPUT(mask, TensorType({DT_UINT8}))
  492. .INPUT(keep_prob, TensorType({DT_FLOAT, DT_FLOAT16}))
  493. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  494. .OP_END_FACTORY_REG(DropOutDoMask)
  495. /**
  496. *@brief Scales the input.
  497. *@par Inputs:
  498. * Three inputs, including:
  499. *@li x: An ND tensor of type float16 or float32.
  500. *@li scale: An ND tensor of type float16 or float32.
  501. *@li bias: An ND tensor of type float16 or float32.
  502. *@par Attributes:
  503. *@li axis: An optional int32 used to compute the shape of scale and bias input from the online bottoms. Defaults to "1".
  504. *@par Outputs:
  505. *y: An ND tensor of type float16 or float32.
  506. *@attention Constraints:\n
  507. * Assume that the shape length of "x" is "n" and that of "scale" is "m".
  508. *@li "axis" is within the range [-n, n-1]. num_axes >= -1.
  509. *@li If "scale_from_blob = true", "num_axes = -1", and "axis >= 0", the ith axis of "scale" and the (i+"axis")th axis of "x" must have the same size (0 <= i < n-axis).\n
  510. * If "axis < 0", the ith axis of "scale" and the (i+n+"axis")th axis of "x" must have the same size (0 <= i < -axis).
  511. *@li If "scale_from_blob = true" and "num_axes = 0", "scale" is a scalar with shape length 1 and dimension size 1.
  512. *@li If "scale_from_blob = true", "num_axes > 0, and "axis >= 0", "axis + num_axes" must be less than or equal to "n" and the ith axis of "scale" and the (i+"axis")th axis of "x" must have the same size (0 <= i < num_axes).\n
  513. * If "axis < 0", "n + axis + num_axes" must be less than or equal to "n" and the ith axis of "scale" and the (i+n+"axis")th axis of "x" must have the same size (0 <= i < num_axes).
  514. *@li If "scale_from_blob = false", "scale" is not a scalar, and "axis >= 0","axis + m" must be less than or equal to "n" and the ith axis of "scale" and the (i+"axis")th axis of "x" must have the same size (0 <= i < m).\n
  515. * If "axis < 0", "n + axis + m" must be less than or equal to "n" and the ith axis of "scale" and the (i+n+"axis")th axis of "x" must have the same size (0 <= i < m).
  516. *@li If "bias" is not None, the constraints for "bias" is the same as that for "scale".
  517. */
  518. REG_OP(Scale)
  519. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16})) /* "First operand." */
  520. .INPUT(scale, TensorType({DT_FLOAT, DT_FLOAT16})) /* "Second operand." */
  521. .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT, DT_FLOAT16})) /* "Third operand." */
  522. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16})) /* "Result, has same element type as x" */
  523. .ATTR(axis, Int, 1)
  524. .ATTR(num_axes, Int, 1)
  525. .ATTR(scale_from_blob, Bool, true)
  526. .OP_END_FACTORY_REG(Scale)
  527. /**
  528. *@brief Local Response Normalization.
  529. *@par Inputs:
  530. *One input, including:
  531. *@li x: A Tensor. Must be 4-D shape, and only support the following types: float16, float32.
  532. *@par Attributes:
  533. * depth_radius = (local_size + 1) / 2. Defaults to "5".
  534. *@li bias: An optional float32. An offset, usually > 0 to avoid dividing by 0.
  535. * Defaults to "1".
  536. *@li alpha: An optional float32. A scaling factor, usually positive.
  537. * Defaults to "1".
  538. *@li norm_region: An optional string. A mode option. "ACROSS_CHANNELS":0, "WITHIN_CHANNEL":1. Defaults to "ACROSS_CHANNELS".
  539. *@par Outputs:
  540. *y: A Tensor. Has the same data type and shape as "x".
  541. */
  542. REG_OP(LRN)
  543. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  544. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  545. .ATTR(depth_radius, Int, 5)
  546. .ATTR(bias, Float, 1.0)
  547. .ATTR(alpha, Float, 1.0)
  548. .ATTR(beta, Float, 0.5)
  549. .ATTR(norm_region, String, "ACROSS_CHANNELS")
  550. .OP_END_FACTORY_REG(LRN)
  551. /**
  552. * @brief Computes the gradient for Local Response Normalization.
  553. * @par Inputs:
  554. * @li grads: A 4D Tensor of type float16 or float32.
  555. * @li x: A 4D Tensor of type float16 or float32.
  556. * @li y: A 4D Tensor of type float16 or float32.
  557. * @par Attributes:
  558. * @li depth_radius: An optional int, specifying the half-width of the
  559. * normalization window. Defaults to "5".
  560. * @li bias: An optional float32. An offset, usually > 0 to avoid dividing by 0.
  561. * Defaults to "1".
  562. * @li alpha: An optional float32. A scaling factor, usually positive.
  563. * Defaults to "1".
  564. * @li beta: An optional float32. An exponent. Defaults to "0.5".
  565. * @par Outputs:
  566. * z: A Tensor. Has the same type and shape as "grads".
  567. * @attention Constraints:
  568. * "x" and "y" must have the same shape and type as "grads".
  569. */
  570. REG_OP(LRNGrad)
  571. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  572. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  573. .INPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  574. .OUTPUT(z, TensorType({DT_FLOAT16,DT_FLOAT}))
  575. .ATTR(depth_radius, Int, 5)
  576. .ATTR(bias, Float, 1.0)
  577. .ATTR(alpha, Float, 1.0)
  578. .ATTR(beta, Float, 0.5)
  579. .OP_END_FACTORY_REG(LRNGrad)
  580. /**
  581. *@brief Calculates the RNNT Loss (log probability) for each batch entry. \n
  582. Also calculates the gradient.
  583. *@par Inputs:
  584. *@li acts: 4-D, shape: `(batch x seqLength x labelLength x outputDim)`, the logits.
  585. *@li labels: 2-D Tensor containing all the targets of the batch with zero padded.
  586. *@li input_lengths: Tensor of size (batch) containing size of each output sequence.
  587. *@li label_lengths: Tensor of (batch) containing label length of each example.
  588. *@par Outputs:
  589. *@li costs: 1-D Tensor, the cost of each example in the batch.
  590. *@li grads: A Tensor. Has the same type as acts.
  591. *@par Attributes:
  592. *@li blank_label: An optional attribute. Defaults to 0.
  593. */
  594. REG_OP(RNNTLoss)
  595. .INPUT(acts, TensorType({DT_FLOAT}))
  596. .INPUT(labels, TensorType({DT_INT32}))
  597. .INPUT(input_lengths, TensorType({DT_INT32}))
  598. .INPUT(label_lengths, TensorType({DT_INT32}))
  599. .ATTR(blank_label, Int, 0)
  600. .OUTPUT(costs, TensorType({DT_FLOAT}))
  601. .OUTPUT(grads, TensorType({DT_FLOAT}))
  602. .OP_END_FACTORY_REG(RNNTLoss)
  603. /**
  604. *@brief Performs group normalization.
  605. *@par Inputs:\n
  606. * Five inputs, including: (NHWC, NCHW supported)
  607. *@li x: A 4D Tensor of type float16 or float32, with format NHWC or \n
  608. NCHW for 4D.
  609. *@li scale: A Tensor of type float32. Must be 1D if input "x" is with format \n
  610. NHWC or NCHW. Specifies the scaling factor.
  611. *@li offset: A Tensor of type float32. Must be 1D if input "x" is with \n
  612. format NHWC or NCHW. Specifies the offset.
  613. *@li mean: A Tensor of type float32. Must be 1D if input "x" is with format \n
  614. NHWC or NCHW. Reserved. Mu
  615. st be "None" if the operation is used for training.
  616. *@li variance: A Tensor of type float32. Must be 1D if input "x" is with \n
  617. format NHWC or NCHW. Specifies the variance used for inference. Reserved.
  618. *@par Attributes:
  619. *@li epsilon: An optional float32, specifying the small value added to \n
  620. variance to avoid dividing by zero. Defaults to "0.0001".
  621. *@li data_format: An optional string, specifying the format of "x". \n
  622. Defaults to "NHWC".
  623. *@li is_training: An optional bool, specifying if the operation is used for \n
  624. training or inference. Defaults to "True".
  625. *@par Outputs:\n
  626. * Five outputs, including: (NHWC, NCHW supported)
  627. *@li y: A 4D Tensor of type float16 or float32 for the normalized "x", \n
  628. with format NHWC or NCHW for 4D.
  629. *@li batch_mean: A Tensor of type float32. Must be 1D if input "x" is with \n
  630. format NHWC or NCHW. Specifies the mean of "x".
  631. *@li batch_variance: A Tensor of type float32. Must be 1D if input "x" is \n
  632. with format NHWC or NCHW. Specifies the variance of "x".
  633. *@li reserve_space_1: An optional Tensor of type float32. Must be 1D if \n
  634. input "x" is with format NHWC or NCHW. Specifies the mean o
  635. f "x" for gradient computation. Pass "None" to skip this output.
  636. *@li reserve_space_2: An optional Tensor of type float32. Must be 1D if \n
  637. input "x" is with format NHWC or NCHW. Specifies the varian
  638. ce of "x" for gradient computation. Pass "None" to skip this output.
  639. *@attention Constraints:
  640. *@li If the operation is used for inference and outputs "reserve_space_1" \n
  641. and "reserve_space_2" are available, then "reserve_space_1" has the same \n
  642. value as "mean" and "reserve_spa
  643. ce_2" has the same value as "variance".
  644. *@li For Ascend 310, the result accuracy fails due to the square root \n
  645. instruction.
  646. */
  647. REG_OP(GroupNorm)
  648. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  649. .INPUT(scale, TensorType({DT_FLOAT,}))
  650. .INPUT(offset, TensorType({DT_FLOAT,}))
  651. .OPTIONAL_INPUT(mean, TensorType({DT_FLOAT}))
  652. .OPTIONAL_INPUT(variance, TensorType({DT_FLOAT}))
  653. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  654. .OUTPUT(batch_mean, TensorType({DT_FLOAT}))
  655. .OUTPUT(batch_variance, TensorType({DT_FLOAT}))
  656. .OUTPUT(reserve_space_1, TensorType({DT_FLOAT}))
  657. .OUTPUT(reserve_space_2, TensorType({DT_FLOAT}))
  658. .ATTR(epsilon, Float, 0.0001)
  659. .ATTR(data_format, String, "NHWC")
  660. .ATTR(is_training, Bool, true)
  661. .ATTR(num_groups, Int, 2)
  662. .OP_END_FACTORY_REG(GroupNorm)
  663. } // namespace ge
  664. #endif //GE_OP_NN_NORM_OPS_H

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