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_other_ops.h 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_OTHER_OPS_H
  17. #define GE_OP_NN_OTHER_OPS_H
  18. #include "../graph/operator_reg.h"
  19. namespace ge {
  20. REG_OP(Erf)
  21. .INPUT(x, TensorType::FloatingDataType())
  22. .OUTPUT(y, TensorType::FloatingDataType())
  23. .OP_END_FACTORY_REG(Erf)
  24. REG_OP(Erfc)
  25. .INPUT(x, TensorType::FloatingDataType())
  26. .OUTPUT(y, TensorType::FloatingDataType())
  27. .OP_END_FACTORY_REG(Erfc)
  28. /**
  29. *@brief This operation returns a rank 1 histogram counting the number of entries in `values` \n
  30. * that fell into every bin.The bins are equal width and determined by the arguments \n
  31. * 'value_range' and 'nbins'. \n
  32. *@par Inputs:
  33. *Three inputs, including: \n
  34. *@li x: A Tensor of type float32,float16,int32.
  35. *@li range: A Tensor of type float32,float16,int32.
  36. *@li nbins: A Tensor of type int32.
  37. *@par Attributes:
  38. * dtype: An optional attribute. Defaults to "int32".
  39. *@par Outputs:
  40. *y: A Tensor. A Tensor of type int32.
  41. */
  42. REG_OP(HistogramFixedWidth)
  43. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT32}))
  44. .INPUT(range, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT32}))
  45. .INPUT(nbins, TensorType({DT_INT32}))
  46. .OUTPUT(y, TensorType({DT_INT32}))
  47. .ATTR(dtype, String, "int32")
  48. .OP_END_FACTORY_REG(HistogramFixedWidth)
  49. /**
  50. *@brief This operation returns a rank 1 histogram counting the number of entries in `values` \n
  51. * that fell into every bin.The bins are equal width and determined by the arguments \n
  52. * 'value_range' and 'nbins'. \n
  53. *@par Inputs:
  54. *Two inputs, including: \n
  55. *@li x: A Tensor of type float32,float16,int32.
  56. *@li range: A Tensor of type float32,float16,int32.
  57. *@par Attributes:
  58. *@li dtype: An optional attribute. Defaults to "int32".
  59. *@li nbins: A required attribute,the type is int32.
  60. *@par Outputs:
  61. *y: A Tensor. A Tensor of type int32.
  62. */
  63. REG_OP(HistogramFixedWidthD)
  64. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT32}))
  65. .INPUT(range, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT32}))
  66. .OUTPUT(y, TensorType({DT_INT32}))
  67. .REQUIRED_ATTR(nbins, Int)
  68. .ATTR(dtype, String, "int32")
  69. .OP_END_FACTORY_REG(HistogramFixedWidthD)
  70. /**
  71. *@brief Layernorm operator interface implementation
  72. * calculating: x, gamma, beta
  73. * mean = np.mean(x, reduce_axis, keepdims=True)
  74. * variance = np.mean(np.power((x - mean),2), reduce_axis, keepdims=True)
  75. * y = gamma*((x - mean) / np.sqrt(variance + 0.001)) + beta
  76. *@par Inputs:
  77. *Three inputs, including:
  78. * @li x: A Tensor. Must be one of the following types: float16, float32.
  79. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  80. * @li beta: A Tensor. Must be one of the following types: float16, float32.
  81. *@par Attributes:
  82. * @li begin_norm_axis: A required attribute, the type is int32.
  83. * @li begin_params_axis: A required attribute,the type is int32.
  84. *@par Outputs:
  85. *Three outputs, including:
  86. * @li y: A Tensor. Must be one of the following types: float16, float32.
  87. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  88. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  89. */
  90. REG_OP(LayerNorm)
  91. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  92. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  93. .INPUT(beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  94. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  95. .OUTPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  96. .OUTPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  97. .ATTR(begin_norm_axis, Int, 0)
  98. .ATTR(begin_params_axis, Int, 0)
  99. .OP_END_FACTORY_REG(LayerNorm)
  100. /**
  101. *@brief LayerNormGrad operator interface implementation
  102. * calculating: dy, x, variance, mean, gamma
  103. * pd_xl = data_dy*data_gamma
  104. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  105. * np.power((data_variance + EPSLON), (-1.5))),
  106. * reduce_axis, keepdims=True)
  107. * pd_mean = np.sum(((-1.0)*pd_xl
  108. * np.power((data_variance + EPSLON), (-0.5))),
  109. * reduce_axis, keepdims=True)
  110. * + pd_var*(1.0/m)
  111. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  112. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  113. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  114. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  115. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  116. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  117. *@par Inputs:
  118. *Three inputs, including:
  119. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  120. * @li x: A Tensor. Must be one of the following types: float16, float32.
  121. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  122. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  123. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  124. *@par Outputs:
  125. *Three outputs, including:
  126. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  127. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  128. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  129. */
  130. REG_OP(LayerNormGrad)
  131. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  132. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  133. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  134. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  135. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  136. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  137. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  138. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  139. .OP_END_FACTORY_REG(LayerNormGrad)
  140. /**
  141. *@brief LayerNormXBackprop operator interface implementation
  142. * calculating: dy, x, variance, mean, gamma
  143. * pd_xl = data_dy*data_gamma
  144. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  145. * np.power((data_variance + EPSLON), (-1.5))),
  146. * reduce_axis, keepdims=True)
  147. * pd_mean = np.sum(((-1.0)*pd_xl
  148. * np.power((data_variance + EPSLON), (-0.5))),
  149. * reduce_axis, keepdims=True)
  150. * + pd_var*(1.0/m)
  151. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  152. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  153. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  154. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  155. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  156. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  157. *@par Inputs:
  158. *Three inputs, including:
  159. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  160. * @li x: A Tensor. Must be one of the following types: float16, float32.
  161. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  162. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  163. * @li gamma: A Tensor. Must be one of the following types: float16, float32.
  164. *@par Outputs:
  165. *Three outputs, including:
  166. * @li pd_x: A Tensor. Must be one of the following types: float16, float32.
  167. */
  168. REG_OP(LayerNormXBackprop)
  169. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  170. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  171. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  172. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  173. .INPUT(gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  174. .OUTPUT(pd_x, TensorType({DT_FLOAT, DT_FLOAT16}))
  175. .OP_END_FACTORY_REG(LayerNormXBackprop)
  176. /**
  177. *@brief LayerNormBetaGammaBackprop operator interface implementation
  178. * calculating: dy, x, variance, mean
  179. * pd_xl = data_dy*data_gamma
  180. * pd_var = np.sum(((-0.5)*pd_xl*(data_x - data_mean)
  181. * np.power((data_variance + EPSLON), (-1.5))),
  182. * reduce_axis, keepdims=True)
  183. * pd_mean = np.sum(((-1.0)*pd_xl
  184. * np.power((data_variance + EPSLON), (-0.5))),
  185. * reduce_axis, keepdims=True)
  186. * + pd_var*(1.0/m)
  187. * np.sum(((-2.0)*(data_x - data_mean)), reduce_axis, keepdims=True)
  188. * pd_x = pd_xl*np.power((data_variance + EPSLON), (-0.5)) +
  189. * pd_var*(2.0/m)*(data_x - data_mean) + pd_mean*(1.0/m)
  190. * pd_gamma = np.sum((data_dy*(data_x - data_mean)
  191. * np.power((data_variance + EPSLON), (-0.5))), param_axis, keepdims=True)
  192. * pd_beta = np.sum(data_dy, param_axis, keepdims=True)
  193. *@par Inputs:
  194. *Three inputs, including:
  195. * @li dy: A Tensor. Must be one of the following types: float16, float32.
  196. * @li x: A Tensor. Must be one of the following types: float16, float32.
  197. * @li variance: A Tensor. Must be one of the following types: float16, float32.
  198. * @li mean: A Tensor. Must be one of the following types: float16, float32.
  199. *@par Outputs:
  200. *Three outputs, including:
  201. * @li pd_gamma: A Tensor. Must be one of the following types: float16, float32.
  202. * @li pd_beta: A Tensor. Must be one of the following types: float16, float32.
  203. */
  204. REG_OP(LayerNormBetaGammaBackprop)
  205. .INPUT(dy, TensorType({DT_FLOAT, DT_FLOAT16}))
  206. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  207. .INPUT(variance, TensorType({DT_FLOAT, DT_FLOAT16}))
  208. .INPUT(mean, TensorType({DT_FLOAT, DT_FLOAT16}))
  209. .OUTPUT(pd_gamma, TensorType({DT_FLOAT, DT_FLOAT16}))
  210. .OUTPUT(pd_beta, TensorType({DT_FLOAT, DT_FLOAT16}))
  211. .REQUIRED_ATTR(shape_gamma, ListInt)
  212. .OP_END_FACTORY_REG(LayerNormBetaGammaBackprop)
  213. /**
  214. *@brief Return "output" according to the algorithm of dropout_do_mask: \n
  215. * scale_x = x *(1 / keep_prob)
  216. * output = select(mask == 1, scale_x, 0)
  217. *@par Inputs:
  218. *Three inputs, including: \n
  219. * @li x: A mutable Tensor. Must be one of the following types:
  220. * float16, float32
  221. * @li mask: A mutable Tensor. Must met all of the following rules:
  222. * shape of mask should be 1D.
  223. * dtype of mask should be uint8.
  224. * value of shape should met the following algorithm:
  225. * value = (size(x) + 128 - 1) // 128 * 128 //8
  226. * @li keep_prob: A mutable Tensor. Must met all of the following rules:
  227. * shape of "keep_prob" should be (1,) or [1,].
  228. * Has the same type as "x".
  229. *@par Output:
  230. *y: A mutable Tensor. Has the same type as "x".
  231. */
  232. REG_OP(DropOutDoMask)
  233. .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
  234. .INPUT(mask, TensorType({DT_UINT8}))
  235. .INPUT(keep_prob, TensorType({DT_FLOAT, DT_FLOAT16}))
  236. .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
  237. .OP_END_FACTORY_REG(DropOutDoMask)
  238. } // namespace ge
  239. #endif // GE_OP_NN_OTHER_OPS_H

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