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.

nonlinear_fuc_ops.h 9.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_NONLINEAR_FUC_OPS_H
  17. #define GE_OP_NONLINEAR_FUC_OPS_H
  18. #include "../graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. *@brief Computes the for the gelu of "x".
  22. *@par Inputs:
  23. *Two inputs, including:
  24. * @li x: A Tensor. Must be one of the following types: float16, float32
  25. *@par Outputs:
  26. *y: A Tensor. Has the same type as "x".
  27. */
  28. REG_OP(Gelu)
  29. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  30. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  31. .OP_END_FACTORY_REG(Gelu)
  32. /**
  33. *@brief Computes the gradient for the gelu of "x".
  34. *@par Inputs:
  35. *Two inputs, including:
  36. * @li dy: A Tensor. Must be one of the following types: float16, float32
  37. * @li x: A Tensor of the same type as "dy".
  38. * @li y: A Tensor of the same type as "dy".
  39. *@par Outputs:
  40. *z: A Tensor. Has the same type as "dy".
  41. */
  42. REG_OP(GeluGrad)
  43. .INPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT}))
  44. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  45. .INPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  46. .OUTPUT(z, TensorType({DT_FLOAT16, DT_FLOAT}))
  47. .OP_END_FACTORY_REG(GeluGrad)
  48. /**
  49. *@brief Computes the gradient for the tanh of "x".
  50. *@par Inputs:
  51. *Two inputs, including:
  52. * @li y: A Tensor. Must be one of the following types: float16, float32,
  53. * double, complex64, complex128.
  54. * @li dy: A Tensor of the same type as "y".
  55. *@par Outputs:
  56. *z: A Tensor. Has the same type as "y".
  57. */
  58. REG_OP(TanhGrad)
  59. .INPUT(y, TensorType::UnaryDataType())
  60. .INPUT(dy, TensorType::UnaryDataType())
  61. .OUTPUT(z, TensorType::UnaryDataType())
  62. .OP_END_FACTORY_REG(TanhGrad)
  63. REG_OP(Tanh)
  64. .INPUT(x, TensorType::UnaryDataType())
  65. .OUTPUT(y, TensorType::UnaryDataType())
  66. .OP_END_FACTORY_REG(Tanh)
  67. /**
  68. * @brief Computes rectified linear: "max(x, 0)".
  69. *
  70. * @par Inputs:
  71. * x: A tensor. Must be one of the following types: float32, float64, int32, uint8,\n
  72. * int16, int8, int64, uint16, float16, qint8.
  73. *
  74. * @par Outputs:
  75. * y: A tensor. Has the same type as "x".
  76. *
  77. */
  78. REG_OP(Relu)
  79. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
  80. DT_INT8, DT_INT32, DT_INT16, DT_INT64,
  81. DT_UINT8, DT_UINT16, DT_QINT8}))
  82. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
  83. DT_INT8, DT_INT32, DT_INT16, DT_INT64,
  84. DT_UINT8, DT_UINT16, DT_QINT8}))
  85. .OP_END_FACTORY_REG(Relu)
  86. /**
  87. * @brief Computes rectified linear 6.
  88. * activations = min(max(features, 0), 6).
  89. * @par Inputs:
  90. * features: A Tensor of type RealNumberType.
  91. * @par Outputs:
  92. * activations: A Tensor of type RealNumberType.
  93. */
  94. REG_OP(Relu6)
  95. .INPUT(features, TensorType::RealNumberType())
  96. .OUTPUT(activations, TensorType::RealNumberType())
  97. .OP_END_FACTORY_REG(Relu6)
  98. /**
  99. * @brief Computes rectified linear 6 gradients for a Relu6 operation.
  100. * backprops = gradients * (features > 0) * (features < 6).
  101. * @par Inputs:
  102. * @li features: A Tensor of type RealNumberType.
  103. * @li gradients: A Tensor of type RealNumberType.
  104. * @par Outputs:
  105. * backprops: A Tensor of type RealNumberType.
  106. */
  107. REG_OP(Relu6Grad)
  108. .INPUT(gradients, TensorType::RealNumberType())
  109. .INPUT(features, TensorType::RealNumberType())
  110. .OUTPUT(backprops, TensorType::RealNumberType())
  111. .OP_END_FACTORY_REG(Relu6Grad)
  112. /**
  113. * @brief Compute sigmoid of "x" element-wise.
  114. * @par Inputs:
  115. * A Tensor of type UnaryDataType.
  116. * @par Outputs:
  117. * A Tensor. Has the same type as "x".
  118. * @attention Constraints:
  119. * @li Ascend 310 provides only 1e-3 accuracy for the result.
  120. * @see Relu()
  121. */
  122. REG_OP(Sigmoid)
  123. .INPUT(x, TensorType(UnaryDataType))
  124. .OUTPUT(y, TensorType(UnaryDataType))
  125. .OP_END_FACTORY_REG(Sigmoid)
  126. /**
  127. * @brief Computes z = (y - y*y)*dy.
  128. * @par Inputs:
  129. * @li y: the input is tensor , dtype is UnaryDataType.
  130. * @li dy the input is tensor , dtype is UnaryDataType.
  131. * @par Outputs:
  132. * z: the shape of output, dtype is UnaryDataType.
  133. */
  134. REG_OP(SigmoidGrad)
  135. .INPUT(y, TensorType(UnaryDataType))
  136. .INPUT(dy, TensorType(UnaryDataType))
  137. .OUTPUT(z, TensorType(UnaryDataType))
  138. .OP_END_FACTORY_REG(SigmoidGrad)
  139. REG_OP(Activation)
  140. .INPUT(x, TensorType::ALL())
  141. .OUTPUT(y, TensorType::ALL())
  142. /*
  143. 0:sigmod, 1:relu, 2:tanh, 3:clipped ReLU, 4:Elu,
  144. 5:leaky relu, 6:abs, 7:relu1, 8:softsign, 9:softplus
  145. */
  146. .ATTR(mode, Int, 1)
  147. .ATTR(coef, Float, 0)
  148. .OP_END_FACTORY_REG(Activation)
  149. REG_OP(ActivationGrad)
  150. .INPUT(dy, TensorType{DT_FLOAT})
  151. .INPUT(x, TensorType{DT_FLOAT})
  152. .OUTPUT(dx, TensorType{DT_FLOAT})
  153. .ATTR(mode, Int, 1)
  154. .OP_END_FACTORY_REG(ActivationGrad)
  155. REG_OP(Softplus)
  156. .INPUT(features, TensorType::FloatingDataType())
  157. .OUTPUT(activations, TensorType::FloatingDataType())
  158. .OP_END_FACTORY_REG(Softplus)
  159. REG_OP(SoftplusGrad)
  160. .INPUT(gradients, TensorType::FloatingDataType())
  161. .INPUT(features, TensorType::FloatingDataType())
  162. .OUTPUT(backprops, TensorType::FloatingDataType())
  163. .OP_END_FACTORY_REG(SoftplusGrad)
  164. REG_OP(Softsign)
  165. .INPUT(features, TensorType::FloatingDataType())
  166. .OUTPUT(activations, TensorType::FloatingDataType())
  167. .OP_END_FACTORY_REG(Softsign)
  168. REG_OP(Selu)
  169. .INPUT(features, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  170. DT_INT8,DT_INT32}))
  171. .OUTPUT(activations, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  172. DT_INT8,DT_INT32}))
  173. .OP_END_FACTORY_REG(Selu)
  174. REG_OP(ReluGrad)
  175. .INPUT(gradients, TensorType::RealNumberType())
  176. .INPUT(features, TensorType::RealNumberType())
  177. .OUTPUT(backprops, TensorType::RealNumberType())
  178. .OP_END_FACTORY_REG(ReluGrad)
  179. /**
  180. *@brief Computes rectified linear gradients for a ReLU operation.
  181. *@par Inputs:
  182. * Two inputs, including:
  183. *@li gradients: A Tensor. Must be one of the following types: float32, double, int32, int8, int16,\n int8, int64, uint16, float16, uint32, uint64
  184. *@li mask: A Tensor. Must be the following types: uint8
  185. *@par Outputs:
  186. *backprops: A Tensor. Must have the same type as"gradients".
  187. *@attention Constraints:
  188. * The corresponding Relu operator needs to be called before using this operator on the network.
  189. *@see Relu
  190. */
  191. REG_OP(ReluGradV2)
  192. .INPUT(gradients, TensorType::RealNumberType())
  193. .INPUT(mask, TensorType({DT_UINT8}))
  194. .OUTPUT(backprops, TensorType::RealNumberType())
  195. .OP_END_FACTORY_REG(ReluGradV2)
  196. /**
  197. *@brief Computes rectified linear: `max(x, 0)`.
  198. *
  199. *@attention Constraints:\n
  200. * The last dim must be mutiply of 8
  201. * The second output `mask` is the result of `y` use 'gt' compare with 0.
  202. *
  203. *@par Inputs:
  204. * x: A tensor. Must be one of the following types: float32, float64, int32, uint8,
  205. * int16, int8, int64, uint16, float16, qint8.
  206. *
  207. *@par Outputs:
  208. *@li y : A `Tensor`. Has the same type as `x`.
  209. *@li mask : A `Tensor`. Must be the type : `uint8`.
  210. *
  211. */
  212. REG_OP(ReluV2)
  213. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  214. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  215. .OUTPUT(mask, TensorType({DT_UINT8}))
  216. .OP_END_FACTORY_REG(ReluV2)
  217. REG_OP(PRelu)
  218. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  219. .INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  220. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  221. .OP_END_FACTORY_REG(PRelu)
  222. REG_OP(PReluGrad)
  223. .INPUT(input_gradients, TensorType({DT_FLOAT16, DT_FLOAT}))
  224. .INPUT(input_features, TensorType({DT_FLOAT16, DT_FLOAT}))
  225. .INPUT(input_weights, TensorType({DT_FLOAT16, DT_FLOAT}))
  226. .OUTPUT(output_backprops_dx, TensorType({DT_FLOAT16, DT_FLOAT}))
  227. .OUTPUT(output_backprops_da, TensorType({DT_FLOAT16, DT_FLOAT}))
  228. .OP_END_FACTORY_REG(PReluGrad)
  229. /**
  230. *@brief Computes exponential linear: `exp(x) - 1` if < 0, `x` otherwise.
  231. *
  232. *@par Inputs:
  233. * x : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
  234. *
  235. *@par Outputs:
  236. * y : A `Tensor`. Has the same type as `x`.
  237. *
  238. */
  239. REG_OP(Elu)
  240. .INPUT(x, TensorType::FloatingDataType())
  241. .OUTPUT(y, TensorType::FloatingDataType())
  242. .ATTR(alpha, Float, 1.0)
  243. .OP_END_FACTORY_REG(Elu)
  244. /**
  245. *@brief Computes gradients for the exponential linear (Elu) operation.
  246. *
  247. *@par Inputs:
  248. *@li grads : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
  249. * The backpropagated gradients to the corresponding Elu operation.
  250. *@li activations : A `Tensor`. Must have the same type as `grads`.
  251. * The outputs of the corresponding Elu operation.
  252. *
  253. *@par Outputs:
  254. * y : A `Tensor`. Has the same type as `grads`.
  255. *
  256. */
  257. REG_OP(EluGrad)
  258. .INPUT(grads, TensorType::FloatingDataType())
  259. .INPUT(activations, TensorType::FloatingDataType())
  260. .OUTPUT(y, TensorType::FloatingDataType())
  261. .OP_END_FACTORY_REG(EluGrad)
  262. } // namespace ge
  263. #endif // GE_OP_NONLINEAR_FUC_OPS_H

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