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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. REG_OP(LogSoftmaxGrad)
  21. .INPUT(grad, TensorType({DT_FLOAT16, DT_FLOAT}))
  22. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  23. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  24. .ATTR(axis, ListInt, {-1})
  25. .OP_END_FACTORY_REG(LogSoftmaxGrad)
  26. REG_OP(SparseSoftmaxCrossEntropyWithLogitsCCE)
  27. .INPUT(features, TensorType{DT_FLOAT})
  28. .INPUT(labels, TensorType{DT_FLOAT})
  29. .OUTPUT(out, TensorType{DT_FLOAT})
  30. .OUTPUT(non, TensorType{DT_FLOAT})
  31. .ATTR(cross_entropy_is_grad, Bool, 0)
  32. .ATTR(cross_entropy_mode, Int, 1)
  33. .ATTR(softmax_cross_entropy_lossscale_div_batch, Float, 1.0)
  34. .OP_END_FACTORY_REG(SparseSoftmaxCrossEntropyWithLogitsCCE)
  35. /**
  36. *@brief Computes sparse softmax cross entropy cost and gradients to backpropagate.
  37. *@par Inputs:
  38. *Two inputs, including:
  39. * @li features: A Tensor. Must be one of the following types: half, float32, double.
  40. * A "batch_size * num_classes" matrix.
  41. * @li labels: A Tensor of the same type as "features". batch_size vector with values in [0, num_classes).
  42. *@par Outputs:
  43. *loss: A Tensor for per example loss (a "batch_size" vector). Has the same type as "features".
  44. *backprop: A Tensor for the backpropagated gradients (a batch_size * num_classes matrix). Has the same type as "features".
  45. */
  46. REG_OP(SparseSoftmaxCrossEntropyWithLogits)
  47. .INPUT(features, TensorType({DT_FLOAT16,DT_FLOAT}))
  48. .INPUT(labels, TensorType({DT_INT32, DT_INT64}))
  49. .OUTPUT(loss, TensorType({DT_FLOAT16,DT_FLOAT}))
  50. .OUTPUT(backprop, TensorType({DT_FLOAT16,DT_FLOAT}))
  51. .OP_END_FACTORY_REG(SparseSoftmaxCrossEntropyWithLogits)
  52. /**
  53. *@brief Computes softmax cross entropy cost and gradients to backpropagate.
  54. *@par Inputs:
  55. *Two inputs, including:
  56. * @li features: A Tensor. Must be one of the following types: half, float32, double.
  57. * A "batch_size * num_classes" matrix.
  58. * @li labels: A Tensor of the same type as "features". A "batch_size * num_classes" matrix.
  59. *@par Outputs:
  60. *loss: A Tensor for per example loss (a "batch_size" vector). Has the same type as "features".
  61. *backprop: A Tensor for the backpropagated gradients (a batch_size * num_classes matrix). Has the same type as "features".
  62. */
  63. REG_OP(SoftmaxCrossEntropyWithLogits)
  64. .INPUT(features, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  65. .INPUT(labels, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  66. .OUTPUT(loss, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  67. .OUTPUT(backprop, TensorType({DT_DOUBLE,DT_FLOAT16,DT_FLOAT}))
  68. .OP_END_FACTORY_REG(SoftmaxCrossEntropyWithLogits)
  69. REG_OP(SoftmaxGrad)
  70. .INPUT(softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  71. .INPUT(grad_softmax, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  72. .OUTPUT(grad_x, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  73. .OP_END_FACTORY_REG(SoftmaxGrad)
  74. REG_OP(SigmoidCrossEntropyWithLogitsGrad)
  75. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  76. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  77. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  78. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  79. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogitsGrad)
  80. REG_OP(SigmoidCrossEntropyWithLogits)
  81. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  82. .INPUT(target, TensorType({DT_FLOAT16, DT_FLOAT}))
  83. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  84. .OP_END_FACTORY_REG(SigmoidCrossEntropyWithLogits)
  85. REG_OP(SmoothL1Loss)
  86. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  87. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  88. .OUTPUT(loss, TensorType({DT_FLOAT16, DT_FLOAT}))
  89. .ATTR(sigma, Float, 1.0)
  90. .OP_END_FACTORY_REG(SmoothL1Loss)
  91. REG_OP(SmoothL1LossGrad)
  92. .INPUT(predict, TensorType({DT_FLOAT16, DT_FLOAT}))
  93. .INPUT(label, TensorType({DT_FLOAT16, DT_FLOAT}))
  94. .INPUT(dout, TensorType({DT_FLOAT16, DT_FLOAT}))
  95. .OUTPUT(gradient, TensorType({DT_FLOAT16, DT_FLOAT}))
  96. .ATTR(sigma, Float, 1.0)
  97. .OP_END_FACTORY_REG(SmoothL1LossGrad)
  98. REG_OP(BinaryCrossEntropy)
  99. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  100. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  101. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  102. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  103. .ATTR(reduction, String, "mean")
  104. .OP_END_FACTORY_REG(BinaryCrossEntropy)
  105. REG_OP(BinaryCrossEntropyGrad)
  106. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  107. .INPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  108. .INPUT(grad_output, TensorType({DT_FLOAT, DT_FLOAT16}))
  109. .OPTIONAL_INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  110. .OUTPUT(output, TensorType({DT_FLOAT, DT_FLOAT16}))
  111. .ATTR(reduction, String, "mean")
  112. .OP_END_FACTORY_REG(BinaryCrossEntropyGrad)
  113. /**
  114. *@brief Applies the Softmax function to an n-dimensional input Tensor rescaling them \n so
  115. that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.
  116. *@par Inputs:
  117. *One input:
  118. *x: A mutable Tensor. Must be one of the following types: float16,
  119. *float32, double. Should be a Variable Tensor.
  120. *@par Attributes:
  121. *axis: A list of ints. The dimension softmax would be performed on.
  122. *@par Outputs:
  123. *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.
  124. */
  125. REG_OP(Softmax)
  126. .INPUT(x, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  127. .OUTPUT(y, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  128. .ATTR(axis, ListInt, {-1})
  129. .OP_END_FACTORY_REG(Softmax)
  130. /**
  131. *@brief Computes log softmax activations.
  132. *@par Inputs:
  133. *One input:
  134. * logits: A Tensor. Must be one of the following types: double, float16, float32.
  135. *@par Attributes:
  136. * axis: An optional list of ints. Defaults to "{-1}".
  137. *@par Outputs:
  138. * logsoftmax: A Tensor. Has the same type as "logits".
  139. */
  140. REG_OP(LogSoftmax)
  141. .INPUT(logits, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  142. .OUTPUT(logsoftmax, TensorType({DT_DOUBLE, DT_FLOAT16, DT_FLOAT}))
  143. .ATTR(axis, ListInt, {-1})
  144. .OP_END_FACTORY_REG(LogSoftmax)
  145. REG_OP(FusedBatchNormV2)
  146. .INPUT(x, TensorType{DT_FLOAT}) /* Input data tensor from the previous operator"" */
  147. .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)*/
  148. .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)*/
  149. .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)*/
  150. .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).*/
  151. .OUTPUT(y, TensorType{DT_FLOAT}) /* The output tensor of the same shape as X */
  152. .ATTR(momentum, Float, 0.9) // Factor used in computing the running mean and variance.
  153. .ATTR(epsilon, Float, 1e-5f) // The epsilon value to use to avoid division by zero
  154. .ATTR(mode, Int, 1) // 1 means using "CC_BATCHNORM_SPATIAL"; 0 means using "CC_BATCHNORM_PER_ACTIVATION"; only support 1 now
  155. .ATTR(use_global_stats, Bool, true)
  156. .ATTR(alpha, Float, 1)
  157. .ATTR(beta, Float, 0)
  158. .OP_END_FACTORY_REG(FusedBatchNormV2)
  159. REG_OP(Scale)
  160. .INPUT(x, TensorType{DT_FLOAT})
  161. .OPTIONAL_INPUT(w, TensorType{DT_FLOAT})
  162. .OPTIONAL_INPUT(b, TensorType{DT_FLOAT})
  163. .OUTPUT(y, TensorType{DT_FLOAT})
  164. .ATTR(bias_term, Bool, false)
  165. .ATTR(axis, Int, 1)
  166. .ATTR(num_axis, Int, 1)
  167. .ATTR(alpha, Float, 1.0)
  168. .ATTR(beta, Float, 0.0)
  169. .OP_END_FACTORY_REG(Scale)
  170. REG_OP(ConfusionSoftmaxGrad)
  171. .INPUT(grad, TensorType({DT_FLOAT16,DT_FLOAT}))
  172. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  173. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  174. .OP_END_FACTORY_REG(ConfusionSoftmaxGrad)
  175. } // namespace ge
  176. #endif //GE_OP_NN_NORM_OPS_H

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