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 14 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. /**
  64. *@brief: Computes hyperbolic tangent of "x" element-wise.
  65. *@par Inputs:
  66. *One input:
  67. *x: A Tensor. Must be one of the following types: float16, float32, double, complex64, complex128, int32, int64
  68. *@par Outputs:
  69. *y: A Tensor. Has the same type as "x".
  70. */
  71. REG_OP(Tanh)
  72. .INPUT(x, TensorType::UnaryDataType())
  73. .OUTPUT(y, TensorType::UnaryDataType())
  74. .OP_END_FACTORY_REG(Tanh)
  75. /**
  76. * @brief Computes rectified linear: "max(x, 0)".
  77. *
  78. * @par Inputs:
  79. * x: A tensor. Must be one of the following types: float32, float64, int32, uint8,\n
  80. * int16, int8, int64, uint16, float16, qint8.
  81. *
  82. * @par Outputs:
  83. * y: A tensor. Has the same type as "x".
  84. *
  85. */
  86. REG_OP(Relu)
  87. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
  88. DT_INT8, DT_INT32, DT_INT16, DT_INT64,
  89. DT_UINT8, DT_UINT16, DT_QINT8}))
  90. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
  91. DT_INT8, DT_INT32, DT_INT16, DT_INT64,
  92. DT_UINT8, DT_UINT16, DT_QINT8}))
  93. .OP_END_FACTORY_REG(Relu)
  94. /**
  95. * @brief Computes rectified linear 6.
  96. * activations = min(max(x, 0), 6).
  97. * @par Inputs:
  98. * x: A Tensor of type RealNumberType.
  99. * @par Outputs:
  100. * y: A Tensor of type RealNumberType.
  101. */
  102. REG_OP(Relu6)
  103. .INPUT(x, TensorType::RealNumberType())
  104. .OUTPUT(y, TensorType::RealNumberType())
  105. .OP_END_FACTORY_REG(Relu6)
  106. /**
  107. * @brief Computes rectified linear 6 gradients for a Relu6 operation.
  108. * backprops = gradients * (features > 0) * (features < 6).
  109. * @par Inputs:
  110. * @li features: A Tensor of type RealNumberType.
  111. * @li gradients: A Tensor of type RealNumberType.
  112. * @par Outputs:
  113. * backprops: A Tensor of type RealNumberType.
  114. */
  115. REG_OP(Relu6Grad)
  116. .INPUT(gradients, TensorType::RealNumberType())
  117. .INPUT(features, TensorType::RealNumberType())
  118. .OUTPUT(backprops, TensorType::RealNumberType())
  119. .OP_END_FACTORY_REG(Relu6Grad)
  120. /**
  121. * @brief Compute sigmoid of "x" element-wise.
  122. * @par Inputs:
  123. * A Tensor of type UnaryDataType.
  124. * @par Outputs:
  125. * A Tensor. Has the same type as "x".
  126. * @see Relu()
  127. */
  128. REG_OP(Sigmoid)
  129. .INPUT(x, TensorType::UnaryDataType())
  130. .OUTPUT(y, TensorType::UnaryDataType())
  131. .OP_END_FACTORY_REG(Sigmoid)
  132. /**
  133. * @brief Computes z = (y - y*y)*dy.
  134. * @par Inputs:
  135. * @li y: the input is tensor , dtype is UnaryDataType.
  136. * @li dy the input is tensor , dtype is UnaryDataType.
  137. * @par Outputs:
  138. * z: the shape of output, dtype is UnaryDataType.
  139. */
  140. REG_OP(SigmoidGrad)
  141. .INPUT(y, TensorType(UnaryDataType))
  142. .INPUT(dy, TensorType(UnaryDataType))
  143. .OUTPUT(z, TensorType(UnaryDataType))
  144. .OP_END_FACTORY_REG(SigmoidGrad)
  145. REG_OP(Activation)
  146. .INPUT(x, TensorType::ALL())
  147. .OUTPUT(y, TensorType::ALL())
  148. /*
  149. 0: sigmod, 1: relu, 2: tanh, 3: clipped ReLU, 4: Elu,
  150. 5: leaky relu, 6: abs, 7: relu1, 8: softsign, 9: softplus
  151. */
  152. .ATTR(mode, Int, 1)
  153. .ATTR(coef, Float, 0)
  154. .OP_END_FACTORY_REG(Activation)
  155. REG_OP(ActivationGrad)
  156. .INPUT(dy, TensorType{DT_FLOAT})
  157. .INPUT(x, TensorType{DT_FLOAT})
  158. .OUTPUT(dx, TensorType{DT_FLOAT})
  159. .ATTR(mode, Int, 1)
  160. .OP_END_FACTORY_REG(ActivationGrad)
  161. /**
  162. *@brief Computes the binomial normal log likelihood (BNLL) output:\n
  163. *if x>0, x+log(1+exp(-x)); otherwise log(1+exp(x)).
  164. *@par Inputs:
  165. *x: A Tensor of type float16 or float32.
  166. *@par Outputs:
  167. *y: A tensor. Has the same type and format as input "x".
  168. */
  169. REG_OP(BNLL)
  170. .INPUT(x, TensorType::FloatingDataType())
  171. .OUTPUT(y, TensorType::FloatingDataType())
  172. .OP_END_FACTORY_REG(BNLL)
  173. /**
  174. *@brief Computes softplus: log(exp(x) + 1).
  175. *@par Inputs:
  176. * One input:\n
  177. *x: A Tensor of type float16 or float32. Up to 8D.
  178. *@par Outputs:
  179. *y: The activations tensor. Has the same type and format as input "x"
  180. */
  181. REG_OP(Softplus)
  182. .INPUT(x, TensorType::FloatingDataType())
  183. .OUTPUT(y, TensorType::FloatingDataType())
  184. .OP_END_FACTORY_REG(Softplus)
  185. /**
  186. *@brief Computes softplus gradients for a softplus operation.
  187. *@par Inputs:
  188. *Two inputs:
  189. * @li gradients: An NC1HWC0 or ND Tensor of type float16 or float32.
  190. * @li features: An NC1HWC0 or ND Tensor of type float16 or float32.
  191. *@par Outputs:
  192. *backprops: A Tensor. Has the same type and format as input "gradients".
  193. */
  194. REG_OP(SoftplusGrad)
  195. .INPUT(gradients, TensorType::FloatingDataType())
  196. .INPUT(features, TensorType::FloatingDataType())
  197. .OUTPUT(backprops, TensorType::FloatingDataType())
  198. .OP_END_FACTORY_REG(SoftplusGrad)
  199. /**
  200. *@brief Computes softsign: x/(abs(x) + 1).
  201. *@par Inputs:
  202. * One input:\n
  203. *x: A Tensor of type float16 or float32. Up to 8D.
  204. *@par Outputs:
  205. *y: The activations tensor. Has the same type and format as "x"
  206. */
  207. REG_OP(Softsign)
  208. .INPUT(x, TensorType::FloatingDataType())
  209. .OUTPUT(y, TensorType::FloatingDataType())
  210. .OP_END_FACTORY_REG(Softsign)
  211. /**
  212. *@brief Computes scaled exponential linear: scale * alpha * (exp(x) - 1).
  213. *@par Inputs:
  214. * One input: \n
  215. *x: A Tensor. Must be one of the following types: float16, float32, int32, int8.
  216. *@par Outputs:
  217. *y: A Tensor. Has the same type and format as input "x".
  218. *@see Region()
  219. */
  220. REG_OP(Selu)
  221. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  222. DT_INT8,DT_INT32}))
  223. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
  224. DT_INT8,DT_INT32}))
  225. .OP_END_FACTORY_REG(Selu)
  226. /**
  227. *@brief Computes rectified linear gradients for a ReLU operation.
  228. *@par Inputs:
  229. * Two inputs, including:
  230. *@li gradients: A Tensor. Must be one of the following types: float32, double, int32, int8, int16, int8, int64, uint16, float16, uint32, uint64
  231. *@li features: A Tensor. Must be one of the following types: float32, double, int32, int8, int16, int8, int64, uint16, float16, uint32, uint64
  232. *@par Outputs:
  233. *backprops: A Tensor. Must have the same type as"gradients".
  234. *@attention Constraints:
  235. * The corresponding Relu operator needs to be called before using this operator on the network.
  236. *@see Relu
  237. */
  238. REG_OP(ReluGrad)
  239. .INPUT(gradients, TensorType::RealNumberType())
  240. .INPUT(features, TensorType::RealNumberType())
  241. .OUTPUT(backprops, TensorType::RealNumberType())
  242. .OP_END_FACTORY_REG(ReluGrad)
  243. /**
  244. *@brief Computes rectified linear gradients for a ReLU operation.
  245. *@par Inputs:
  246. * Two inputs, including:
  247. *@li gradients: A Tensor. Must be one of the following types: float32, double, int32, int8, int16,\n int8, int64, uint16, float16, uint32, uint64
  248. *@li mask: A Tensor. Must be the following types: uint8
  249. *@par Outputs:
  250. *backprops: A Tensor. Must have the same type as"gradients".
  251. *@attention Constraints:
  252. * The corresponding Relu operator needs to be called before using this operator on the network.
  253. *@see Relu
  254. */
  255. REG_OP(ReluGradV2)
  256. .INPUT(gradients, TensorType::RealNumberType())
  257. .INPUT(mask, TensorType({DT_UINT8}))
  258. .OUTPUT(backprops, TensorType::RealNumberType())
  259. .OP_END_FACTORY_REG(ReluGradV2)
  260. /**
  261. *@brief Computes rectified linear: "max(x, 0)".
  262. *
  263. *@attention Constraints:\n
  264. * The last dimension must be divisible by 8.
  265. * The second output "mask" is "1" (for y >= 0) or "0" ( for y < 0).
  266. *
  267. *@par Inputs:
  268. * x: A tensor. Must be one of the following types: float32, float64, int32, uint8,
  269. * int16, int8, int64, uint16, float16, qint8.
  270. *
  271. *@par Outputs:
  272. *@li y: A tensor. Has the same type as "x".
  273. *@li mask: A tensor of type uint8.
  274. */
  275. REG_OP(ReluV2)
  276. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  277. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
  278. .OUTPUT(mask, TensorType({DT_UINT8}))
  279. .OP_END_FACTORY_REG(ReluV2)
  280. /**
  281. *@brief Performs parametric ReLU.
  282. *@par Inputs:
  283. * Two inputs, including: \n
  284. *@li x: A multi-dimensional Tensor of type float16 or float32.
  285. *@li weight: A Scalar or 1D Tensor of type float16 or float32, specifying the weight, the initial value of "a". The number of dimensions must be the same as the number of channels.
  286. *@par Outputs:
  287. *y: An activated Tensor. Has the same dimensions with "x".
  288. */
  289. REG_OP(PRelu)
  290. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  291. .INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
  292. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  293. .OP_END_FACTORY_REG(PRelu)
  294. /**
  295. *@brief Performs the backpropagation of PRelu for training scenarios.
  296. *@par Inputs:
  297. * Three inputs, including: \n
  298. *@li grads: Input gradient. Multi-dimensional Tensors are supported. The data type can be float16 or float32.
  299. *@li features: A multi-dimensional Tensor of type float16 or float32.
  300. *@li weights: A Scalar or 1D Tensor of type float16 or float32, specifying the weight. The number of dimensions must be the same as the number of channels.
  301. *@par Outputs:
  302. *@li dx: Reverse gradient of "features". Has the same dimensions and type as "features".
  303. *@li da: Reverse gradient of "weight". Has the same dimensions and type as "features".
  304. */
  305. REG_OP(PReluGrad)
  306. .INPUT(grads, TensorType({DT_FLOAT16, DT_FLOAT}))
  307. .INPUT(features, TensorType({DT_FLOAT16, DT_FLOAT}))
  308. .INPUT(weights, TensorType({DT_FLOAT16, DT_FLOAT}))
  309. .OUTPUT(dx, TensorType({DT_FLOAT16, DT_FLOAT}))
  310. .OUTPUT(da, TensorType({DT_FLOAT16, DT_FLOAT}))
  311. .OP_END_FACTORY_REG(PReluGrad)
  312. /**
  313. *@brief Activation function fused from sigmoid and ReLU, with soft saturation on the left and no saturation on the right.
  314. *@par Inputs:
  315. *x: A float16 or float32, for the input data type.
  316. *@par Attributes:
  317. *alpha: A float. Defines at which negative value the ELU saturates. Defaults to "1.0".
  318. *@par Outputs:
  319. *y: A float16 or float32, for the normalized result.
  320. *@attention Constraints:
  321. *@li The input is of type float16 or float32.
  322. *@par Multiple batches supported or not
  323. *Supported
  324. *
  325. *@since V100R001C33
  326. */
  327. REG_OP(Elu)
  328. .INPUT(x, TensorType::FloatingDataType())
  329. .OUTPUT(y, TensorType::FloatingDataType())
  330. .ATTR(alpha, Float, 1.0)
  331. .OP_END_FACTORY_REG(Elu)
  332. /**
  333. *@brief Computes gradients for the exponential linear (Elu) operation.
  334. *
  335. *@par Inputs:
  336. *@li grads: A tensor. Must be one of the following types: float16, float32, float64.
  337. * The backpropagated gradients to the corresponding Elu operation.
  338. *@li activations: A tensor. Has the same type as "grads".
  339. * The outputs of the corresponding Elu operation.
  340. *
  341. *@par Outputs:
  342. * y: A tensor. Has the same type as "grads".
  343. *
  344. */
  345. REG_OP(EluGrad)
  346. .INPUT(grads, TensorType::FloatingDataType())
  347. .INPUT(activations, TensorType::FloatingDataType())
  348. .OUTPUT(y, TensorType::FloatingDataType())
  349. .OP_END_FACTORY_REG(EluGrad)
  350. /**
  351. *@brief Computes the output as x if x > 0 and negative_slope * x if x <= 0.
  352. *@par Inputs:
  353. * One input:
  354. * x: A Tensor. Must be one of the following types: float32, float16, int32, int8, double.
  355. *
  356. *@par Attributes:
  357. *negative_slope: A float32. Defaults to "0.0".
  358. *
  359. *@par Outputs:
  360. *y: A Tensor. Has the same type as "x".
  361. */
  362. REG_OP(LeakyRelu)
  363. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8, DT_DOUBLE}))
  364. .ATTR(negative_slope, Float, 0.0)
  365. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8, DT_DOUBLE}))
  366. .OP_END_FACTORY_REG(LeakyRelu)
  367. /**
  368. *@brief Computes the output as g if x > 0 and negative_slope * g if x <= 0.
  369. *@par Inputs:
  370. * Two inputs, including:
  371. * @li g: A Tensor. Must be one of the following types: float16, float32, double.
  372. * @li x: A Tensor. Has the same type as "g".
  373. *@par Attributes:
  374. *negative_slope: A float32. Defaults to "0.0".
  375. *@par Outputs:
  376. *y: A Tensor. Has the same type as "g".
  377. */
  378. REG_OP(LeakyReluGrad)
  379. .INPUT(g, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  380. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  381. .ATTR(negative_slope, Float, 0.0)
  382. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  383. .OP_END_FACTORY_REG(LeakyReluGrad)
  384. } // namespace ge
  385. #endif // GE_OP_NONLINEAR_FUC_OPS_H

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