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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * z = dy * (y > 0) * (y < 6).
  101. * @par Inputs:
  102. * @li y: A Tensor of type RealNumberType.
  103. * @li dy: A Tensor of type RealNumberType.
  104. * @par Outputs:
  105. * z: 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 "x" is with shape (D1, D2, ..., DK), where, D1 * D2... * Dn <= 2^31-1,
  120. * Di <= 1000000, n <= 8.
  121. * @li Ascend 310 provides only 1?? accuracy for the result.
  122. * @see Relu()
  123. */
  124. REG_OP(Sigmoid)
  125. .INPUT(x, TensorType(UnaryDataType))
  126. .OUTPUT(y, TensorType(UnaryDataType))
  127. .OP_END_FACTORY_REG(Sigmoid)
  128. /**
  129. * @brief Computes z = (y - y*y)*dy.
  130. * @par Inputs:
  131. * @li y: the input is tensor , dtype is UnaryDataType.
  132. * @li dy the input is tensor , dtype is UnaryDataType.
  133. * @par Outputs:
  134. * z: the shape of output, dtype is UnaryDataType.
  135. */
  136. REG_OP(SigmoidGrad)
  137. .INPUT(y, TensorType(UnaryDataType))
  138. .INPUT(dy, TensorType(UnaryDataType))
  139. .OUTPUT(z, TensorType(UnaryDataType))
  140. .OP_END_FACTORY_REG(SigmoidGrad)
  141. REG_OP(Activation)
  142. .INPUT(x, TensorType::ALL())
  143. .OUTPUT(y, TensorType::ALL())
  144. /*
  145. 0:sigmod, 1:relu, 2:tanh, 3:clipped ReLU, 4:Elu,
  146. 5:leaky relu, 6:abs, 7:relu1, 8:softsign, 9:softplus
  147. */
  148. .ATTR(mode, Int, 1)
  149. .ATTR(coef, Float, 0)
  150. .OP_END_FACTORY_REG(Activation)
  151. REG_OP(ActivationGrad)
  152. .INPUT(dy, TensorType{DT_FLOAT})
  153. .INPUT(x, TensorType{DT_FLOAT})
  154. .OUTPUT(dx, TensorType{DT_FLOAT})
  155. .ATTR(mode, Int, 1)
  156. .OP_END_FACTORY_REG(ActivationGrad)
  157. REG_OP(Softplus)
  158. .INPUT(features, TensorType::FloatingDataType())
  159. .OUTPUT(activations, TensorType::FloatingDataType())
  160. .OP_END_FACTORY_REG(Softplus)
  161. REG_OP(SoftplusGrad)
  162. .INPUT(gradients, TensorType::FloatingDataType())
  163. .INPUT(features, TensorType::FloatingDataType())
  164. .OUTPUT(backprops, TensorType::FloatingDataType())
  165. .OP_END_FACTORY_REG(SoftplusGrad)
  166. REG_OP(Softsign)
  167. .INPUT(features, TensorType::FloatingDataType())
  168. .OUTPUT(activations, TensorType::FloatingDataType())
  169. .OP_END_FACTORY_REG(Softsign)
  170. REG_OP(Selu)
  171. .INPUT(features, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  172. DT_INT8,DT_INT32}))
  173. .OUTPUT(activations, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  174. DT_INT8,DT_INT32}))
  175. .OP_END_FACTORY_REG(Selu)
  176. REG_OP(ReluGrad)
  177. .INPUT(gradients, TensorType::RealNumberType())
  178. .INPUT(features, TensorType::RealNumberType())
  179. .OUTPUT(backprops, TensorType::RealNumberType())
  180. .OP_END_FACTORY_REG(ReluGrad)
  181. /**
  182. *@brief Computes rectified linear gradients for a ReLU operation.
  183. *@par Inputs:
  184. * Two inputs, including:
  185. *@li gradients: A Tensor. Must be one of the following types: float32, double, int32, int8, int16,\n int8, int64, uint16, float16, uint32, uint64
  186. *@li mask: A Tensor. Must be the following types: uint8
  187. *@par Outputs:
  188. *backprops: A Tensor. Must have the same type as"gradients".
  189. *@attention Constraints:
  190. * The corresponding Relu operator needs to be called before using this operator on the network.
  191. *@see Relu
  192. */
  193. REG_OP(ReluGradV2)
  194. .INPUT(gradients, TensorType::RealNumberType())
  195. .INPUT(mask, TensorType({DT_UINT8}))
  196. .OUTPUT(backprops, TensorType::RealNumberType())
  197. .OP_END_FACTORY_REG(ReluGradV2)
  198. /**
  199. *@brief Computes rectified linear: `max(x, 0)`.
  200. *
  201. *@attention Constraints:\n
  202. * The last dim must be mutiply of 8
  203. * The second output `mask` is the result of `y` use 'gt' compare with 0.
  204. *
  205. *@par Inputs:
  206. * x: A tensor. Must be one of the following types: float32, float64, int32, uint8,
  207. * int16, int8, int64, uint16, float16, qint8.
  208. *
  209. *@par Outputs:
  210. *@li y : A `Tensor`. Has the same type as `x`.
  211. *@li mask : A `Tensor`. Must be the type : `uint8`.
  212. *
  213. */
  214. REG_OP(ReluV2)
  215. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  216. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  217. .OUTPUT(mask, TensorType({DT_UINT8}))
  218. .OP_END_FACTORY_REG(ReluV2)
  219. REG_OP(PRelu)
  220. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  221. .INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  222. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  223. .OP_END_FACTORY_REG(PRelu)
  224. REG_OP(PReluGrad)
  225. .INPUT(input_gradients, TensorType({DT_FLOAT16, DT_FLOAT}))
  226. .INPUT(input_features, TensorType({DT_FLOAT16, DT_FLOAT}))
  227. .INPUT(input_weights, TensorType({DT_FLOAT16, DT_FLOAT}))
  228. .OUTPUT(output_backprops_dx, TensorType({DT_FLOAT16, DT_FLOAT}))
  229. .OUTPUT(output_backprops_da, TensorType({DT_FLOAT16, DT_FLOAT}))
  230. .OP_END_FACTORY_REG(PReluGrad)
  231. /**
  232. *@brief Computes exponential linear: `exp(x) - 1` if < 0, `x` otherwise.
  233. *
  234. *@par Inputs:
  235. * x : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
  236. *
  237. *@par Outputs:
  238. * y : A `Tensor`. Has the same type as `x`.
  239. *
  240. */
  241. REG_OP(Elu)
  242. .INPUT(x, TensorType::FloatingDataType())
  243. .OUTPUT(y, TensorType::FloatingDataType())
  244. .ATTR(alpha, Float, 1.0)
  245. .OP_END_FACTORY_REG(Elu)
  246. /**
  247. *@brief Computes gradients for the exponential linear (Elu) operation.
  248. *
  249. *@par Inputs:
  250. *@li grads : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
  251. * The backpropagated gradients to the corresponding Elu operation.
  252. *@li activations : A `Tensor`. Must have the same type as `grads`.
  253. * The outputs of the corresponding Elu operation.
  254. *
  255. *@par Outputs:
  256. * y : A `Tensor`. Has the same type as `grads`.
  257. *
  258. */
  259. REG_OP(EluGrad)
  260. .INPUT(grads, TensorType::FloatingDataType())
  261. .INPUT(activations, TensorType::FloatingDataType())
  262. .OUTPUT(y, TensorType::FloatingDataType())
  263. .OP_END_FACTORY_REG(EluGrad)
  264. REG_OP(LeakyRelu)
  265. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8}))
  266. .ATTR(negative_slope, Float, 0.0)
  267. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8}))
  268. .OP_END_FACTORY_REG(LeakyRelu)
  269. } // namespace ge
  270. #endif // GE_OP_NONLINEAR_FUC_OPS_H

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