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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. REG_OP(SoftmaxGrad)
  80. .INPUT(softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  81. .INPUT(grad_softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  82. .OUTPUT(grad_x, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  83. .OP_END_FACTORY_REG(SoftmaxGrad)
  84. REG_OP(SigmoidCrossEntropyWithLogitsGrad)
  85. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  86. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  87. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  88. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  89. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogitsGrad)
  90. REG_OP(SigmoidCrossEntropyWithLogits)
  91. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  92. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  93. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  94. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogits)
  95. REG_OP(SmoothL1Loss)
  96. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  97. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  98. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  99. .ATTR(sigma, Float, 1.0)
  100. .OP_END_FACTORY_REG(SmoothL1Loss)
  101. REG_OP(SmoothL1LossGrad)
  102. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  103. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  104. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  105. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  106. .ATTR(sigma, Float, 1.0)
  107. .OP_END_FACTORY_REG(SmoothL1LossGrad)
  108. REG_OP(BinaryCrossEntropy)
  109. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  110. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  111. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  112. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  113. .ATTR(reduction, String, "mean")
  114. .OP_END_FACTORY_REG(BinaryCrossEntropy)
  115. REG_OP(BinaryCrossEntropyGrad)
  116. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  117. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  118. .INPUT(grad_output, TensorType({DT_FLOAT, DT_FLOAT16}))
  119. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  120. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  121. .ATTR(reduction, String, "mean")
  122. .OP_END_FACTORY_REG(BinaryCrossEntropyGrad)
  123. /**
  124. *@brief Applies the Softmax function to an n-dimensional input Tensor rescaling them \n so
  125. that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.
  126. *@par Inputs:
  127. *One input:
  128. *x: A mutable Tensor. Must be one of the following types: float16,
  129. *float32, double. Should be a Variable Tensor.
  130. *@par Attributes:
  131. *axis: A list of ints. The dimension softmax would be performed on.
  132. *@par Outputs:
  133. *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.
  134. */
  135. REG_OP(Softmax)
  136. .INPUT(x, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  137. .OUTPUT(y, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  138. .ATTR(axis, ListInt, {-1})
  139. .OP_END_FACTORY_REG(Softmax)
  140. /**
  141. *@brief Computes log softmax activations.
  142. *@par Inputs:
  143. *One input:
  144. * logits: A Tensor. Must be one of the following types: double, float16, float32.
  145. *@par Attributes:
  146. * axis: An optional list of ints. Defaults to "{-1}".
  147. *@par Outputs:
  148. * logsoftmax: A Tensor. Has the same type as "logits".
  149. */
  150. REG_OP(LogSoftmax)
  151. .INPUT(logits, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  152. .OUTPUT(logsoftmax, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  153. .ATTR(axis, ListInt, {-1})
  154. .OP_END_FACTORY_REG(LogSoftmax)
  155. REG_OP(FusedBatchNormV2)
  156. .INPUT(x, TensorType{DT_FLOAT}) /* Input data tensor from the previous operator"" */
  157. .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)*/
  158. .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)*/
  159. .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)*/
  160. .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).*/
  161. .OUTPUT(y, TensorType{DT_FLOAT}) /* The output tensor of the same shape as X */
  162. .ATTR(momentum, Float, 0.9) // Factor used in computing the running mean and variance.
  163. .ATTR(epsilon, Float, 1e-5f) // The epsilon value to use to avoid division by zero
  164. .ATTR(mode, Int, 1) // 1 means using "CC_BATCHNORM_SPATIAL"; 0 means using "CC_BATCHNORM_PER_ACTIVATION"; only support 1 now
  165. .ATTR(use_global_stats, Bool, true)
  166. .ATTR(alpha, Float, 1)
  167. .ATTR(beta, Float, 0)
  168. .OP_END_FACTORY_REG(FusedBatchNormV2)
  169. REG_OP(Scale)
  170. .INPUT(x, TensorType{DT_FLOAT})
  171. .OPTIONAL_INPUT(w, TensorType{DT_FLOAT})
  172. .OPTIONAL_INPUT(b, TensorType{DT_FLOAT})
  173. .OUTPUT(y, TensorType{DT_FLOAT})
  174. .ATTR(bias_term, Bool, false)
  175. .ATTR(axis, Int, 1)
  176. .ATTR(num_axis, Int, 1)
  177. .ATTR(alpha, Float, 1.0)
  178. .ATTR(beta, Float, 0.0)
  179. .OP_END_FACTORY_REG(Scale)
  180. REG_OP(SoftmaxGradExt)
  181. .INPUT(grad, TensorType({DT_FLOAT16,DT_FLOAT}))
  182. .INPUT(x1, TensorType({DT_FLOAT16,DT_FLOAT}))
  183. .INPUT(x2, TensorType({DT_FLOAT16,DT_FLOAT}))
  184. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  185. .ATTR(axis, ListInt, {-1})
  186. .ATTR(keep_dims, Bool, false)
  187. .OP_END_FACTORY_REG(SoftmaxGradExt)
  188. /**
  189. *@brief Confuse mul, sum and sub.
  190. *@par Inputs:
  191. *Two inputs, including:
  192. * @li grad: A Tensor. Must be one of the following types: float16, float32.
  193. * @li x: A Tensor. Must be one of the following types: float16, float32.
  194. *@par Outputs:
  195. * y: A Tensor of the same type as "grad".
  196. */
  197. REG_OP(ConfusionSoftmaxGrad)
  198. .INPUT(grad, TensorType({DT_FLOAT16,DT_FLOAT}))
  199. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  200. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  201. .OP_END_FACTORY_REG(ConfusionSoftmaxGrad)
  202. /**
  203. *@brief Layernorm operator interface implementation
  204. * calculating: x, gamma, beta
  205. * mean = np.mean(x, reduce_axis, keepdims=True)
  206. * variance = np.mean(np.power((x - mean),2), reduce_axis, keepdims=True)
  207. * y = gamma*((x - mean) / np.sqrt(variance + 0.001)) + beta
  208. *@par Inputs:
  209. *Three inputs, including:
  210. * @li x: A Tensor. Must be one of the following types: float16, float32.
  211. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  212. * @li beta: A Tensor. Must be one of the following types: float16, float32.
  213. *@par Attributes:
  214. * @li begin_norm_axis: A required attribute, the type is int32.
  215. * @li begin_params_axis: A required attribute,the type is int32.
  216. *@par Outputs:
  217. *Three outputs, including:
  218. * @li y: A Tensor. Must be one of the following types: float16, float32.
  219. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  220. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  221. */
  222. REG_OP(LayerNorm)
  223. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  224. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  225. .INPUT(beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  226. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  227. .OUTPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  228. .OUTPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  229. .ATTR(begin_norm_axis, Int, 0)
  230. .ATTR(begin_params_axis, Int, 0)
  231. .ATTR(epsilon, Float, 0.0000001)
  232. .OP_END_FACTORY_REG(LayerNorm)
  233. /**
  234. *@brief LayerNormGrad operator interface implementation
  235. * calculating: dy, x, variance, mean, gamma
  236. * pd_xl = data_dy*data_gamma
  237. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  238. * np.power((data_variance + EPSLON), (-1.5))),
  239. * reduce_axis, keepdims=True)
  240. * pd_mean = np.sum(((-1.0)*pd_xl
  241. * np.power((data_variance + EPSLON), (-0.5))),
  242. * reduce_axis, keepdims=True)
  243. * + pd_var*(1.0/m)
  244. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  245. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  246. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  247. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  248. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  249. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  250. *@par Inputs:
  251. *Three inputs, including:
  252. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  253. * @li x: A Tensor. Must be one of the following types: float16, float32.
  254. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  255. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  256. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  257. *@par Outputs:
  258. *Three outputs, including:
  259. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  260. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  261. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  262. */
  263. REG_OP(LayerNormGrad)
  264. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  265. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  266. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  267. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  268. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  269. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  270. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  271. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  272. .OP_END_FACTORY_REG(LayerNormGrad)
  273. /**
  274. *@brief LayerNormXBackprop operator interface implementation
  275. * calculating: dy, x, variance, mean, gamma
  276. * pd_xl = data_dy*data_gamma
  277. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  278. * np.power((data_variance + EPSLON), (-1.5))),
  279. * reduce_axis, keepdims=True)
  280. * pd_mean = np.sum(((-1.0)*pd_xl
  281. * np.power((data_variance + EPSLON), (-0.5))),
  282. * reduce_axis, keepdims=True)
  283. * + pd_var*(1.0/m)
  284. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  285. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  286. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  287. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  288. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  289. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  290. *@par Inputs:
  291. *Three inputs, including:
  292. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  293. * @li x: A Tensor. Must be one of the following types: float16, float32.
  294. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  295. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  296. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  297. *@par Outputs:
  298. *Three outputs, including:
  299. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  300. */
  301. REG_OP(LayerNormXBackprop)
  302. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  303. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  304. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  305. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  306. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  307. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  308. .OP_END_FACTORY_REG(LayerNormXBackprop)
  309. /**
  310. *@brief LayerNormBetaGammaBackprop operator interface implementation
  311. * calculating: dy, x, variance, mean
  312. * pd_xl = data_dy*data_gamma
  313. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  314. * np.power((data_variance + EPSLON), (-1.5))),
  315. * reduce_axis, keepdims=True)
  316. * pd_mean = np.sum(((-1.0)*pd_xl
  317. * np.power((data_variance + EPSLON), (-0.5))),
  318. * reduce_axis, keepdims=True)
  319. * + pd_var*(1.0/m)
  320. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  321. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  322. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  323. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  324. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  325. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  326. *@par Inputs:
  327. *Three inputs, including:
  328. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  329. * @li x: A Tensor. Must be one of the following types: float16, float32.
  330. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  331. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  332. *@par Outputs:
  333. *Three outputs, including:
  334. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  335. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  336. */
  337. REG_OP(LayerNormBetaGammaBackprop)
  338. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  339. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  340. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  341. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  342. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  343. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  344. .REQUIRED_ATTR(shape_gamma, ListInt)
  345. .OP_END_FACTORY_REG(LayerNormBetaGammaBackprop)
  346. /**
  347. *@brief Return "output" according to the algorithm of dropout_do_mask: \n
  348. * scale_x = x *(1 / keep_prob)
  349. * output = select(mask == 1, scale_x, 0)
  350. *@par Inputs:
  351. *Three inputs, including: \n
  352. * @li x: A mutable Tensor. Must be one of the following types:
  353. * float16, float32
  354. * @li mask: A mutable Tensor. Must met all of the following rules:
  355. * shape of mask should be 1D.
  356. * dtype of mask should be uint8.
  357. * value of shape should met the following algorithm:
  358. * value = (size(x) + 128 - 1) // 128 * 128 //8
  359. * @li keep_prob: A mutable Tensor. Must met all of the following rules:
  360. * shape of "keep_prob" should be (1,) or [1,].
  361. * Has the same type as "x".
  362. *@par Output:
  363. *y: A mutable Tensor. Has the same type as "x".
  364. */
  365. REG_OP(DropOutDoMask)
  366. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  367. .INPUT(mask, TensorType({DT_UINT8}))
  368. .INPUT(keep_prob, TensorType({DT_FLOAT, DT_FLOAT16}))
  369. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  370. .OP_END_FACTORY_REG(DropOutDoMask)
  371. /**
  372. *@brief Local Response Normalization.
  373. *@par Inputs:
  374. *One input, including:
  375. *@li x: A Tensor. Must be 4-D shape, and only support the following types: float16, float32.
  376. *@par Attributes:
  377. *@li depth_radius: An optional int, specifying the half-width of the
  378. * normalization window. Defaults to "5".
  379. *@li bias: An optional float32. An offset, usually > 0 to avoid dividing by 0.
  380. * Defaults to "1".
  381. *@li alpha: An optional float32. A scaling factor, usually positive.
  382. * Defaults to "1".
  383. *@li beta: An optional float32. An exponent. Defaults to "0.5".
  384. *@li norm_region: An optional string. A mode option. Defaults to "ACROSS_CHANNELS".
  385. *@par Outputs:
  386. *y: A Tensor. Has the same data type and shape as "x".
  387. */
  388. REG_OP(LRN)
  389. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  390. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  391. .ATTR(depth_radius, Int, 5)
  392. .ATTR(bias, Float, 1.0)
  393. .ATTR(alpha, Float, 1.0)
  394. .ATTR(beta, Float, 0.5)
  395. .ATTR(norm_region, String, "ACROSS_CHANNELS")
  396. .OP_END_FACTORY_REG(LRN)
  397. /**
  398. * @brief Computes the gradient for Local Response Normalization.
  399. * @par Inputs:
  400. * @li grads: A 4D Tensor of type float16 or float32.
  401. * @li x: A 4D Tensor of type float16 or float32.
  402. * @li y: A 4D Tensor of type float16 or float32.
  403. * @par Attributes:
  404. * @li depth_radius: An optional int, specifying the half-width of the
  405. * normalization window. Defaults to "5".
  406. * @li bias: An optional float32. An offset, usually > 0 to avoid dividing by 0.
  407. * Defaults to "1".
  408. * @li alpha: An optional float32. A scaling factor, usually positive.
  409. * Defaults to "1".
  410. * @li beta: An optional float32. An exponent. Defaults to "0.5".
  411. * @par Outputs:
  412. * z: A Tensor. Has the same type and shape as "grads".
  413. * @attention Constraints:
  414. * "x" and "y" must have the same shape and type as "grads".
  415. */
  416. REG_OP(LRNGrad)
  417. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  418. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  419. .INPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  420. .OUTPUT(z, TensorType({DT_FLOAT16,DT_FLOAT}))
  421. .ATTR(depth_radius, Int, 5)
  422. .ATTR(bias, Float, 1.0)
  423. .ATTR(alpha, Float, 1.0)
  424. .ATTR(beta, Float, 0.5)
  425. .OP_END_FACTORY_REG(LRNGrad)
  426. } // namespace ge
  427. #endif //GE_OP_NN_NORM_OPS_H

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