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.

reduce_ops.h 11 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_REDUCE_OPS_H
  17. #define GE_OP_REDUCE_OPS_H
  18. #include "../graph/operator_reg.h"
  19. namespace ge {
  20. REG_OP(BNTrainingReduce)
  21. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  22. .OUTPUT(sum, TensorType({DT_FLOAT}))
  23. .OUTPUT(square_sum, TensorType({DT_FLOAT}))
  24. .OP_END_FACTORY_REG(BNTrainingReduce)
  25. REG_OP(BNTrainingReduceGrad)
  26. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  27. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  28. .INPUT(diff_scale, TensorType({DT_FLOAT}))
  29. .INPUT(diff_offset, TensorType({DT_FLOAT}))
  30. .INPUT(scale, TensorType({DT_FLOAT}))
  31. .INPUT(batch_mean, TensorType({DT_FLOAT}))
  32. .INPUT(batch_variance, TensorType({DT_FLOAT}))
  33. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  34. .ATTR(epsilon, Float, 0.0001)
  35. .OP_END_FACTORY_REG(BNTrainingReduceGrad)
  36. REG_OP(BNTrainingUpdate)
  37. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  38. .INPUT(sum, TensorType({DT_FLOAT}))
  39. .INPUT(square_sum, TensorType({DT_FLOAT}))
  40. .INPUT(scale, TensorType({DT_FLOAT}))
  41. .INPUT(offset, TensorType({DT_FLOAT}))
  42. .INPUT(mean, TensorType({DT_FLOAT}))
  43. .INPUT(variance, TensorType({DT_FLOAT}))
  44. .REQUIRED_ATTR(factor, Float)
  45. .REQUIRED_ATTR(epsilon, Float)
  46. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  47. .OUTPUT(mean, TensorType({DT_FLOAT}))
  48. .OUTPUT(variance, TensorType({DT_FLOAT}))
  49. .OUTPUT(batch_mean, TensorType({DT_FLOAT}))
  50. .OUTPUT(batch_variance, TensorType({DT_FLOAT}))
  51. .OP_END_FACTORY_REG(BNTrainingUpdate)
  52. REG_OP(BNTrainingUpdateV2)
  53. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  54. .INPUT(sum, TensorType({DT_FLOAT}))
  55. .INPUT(square_sum, TensorType({DT_FLOAT}))
  56. .INPUT(scale, TensorType({DT_FLOAT}))
  57. .INPUT(offset, TensorType({DT_FLOAT}))
  58. .REQUIRED_ATTR(epsilon, Float)
  59. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  60. .OUTPUT(batch_mean, TensorType({DT_FLOAT}))
  61. .OUTPUT(batch_variance, TensorType({DT_FLOAT}))
  62. .OP_END_FACTORY_REG(BNTrainingUpdateV2)
  63. REG_OP(BNTrainingUpdateGrad)
  64. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  65. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  66. .INPUT(batch_mean, TensorType({DT_FLOAT}))
  67. .INPUT(batch_variance, TensorType({DT_FLOAT}))
  68. .ATTR(epsilon, Float, 0.0001)
  69. .OUTPUT(diff_scale, TensorType({DT_FLOAT}))
  70. .OUTPUT(diff_offset, TensorType({DT_FLOAT}))
  71. .OP_END_FACTORY_REG(BNTrainingUpdateGrad)
  72. REG_OP(BNInferGrad)
  73. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  74. .INPUT(scale, TensorType({DT_FLOAT}))
  75. .INPUT(batch_variance, TensorType({DT_FLOAT}))
  76. .OUTPUT(x_backprop, TensorType({DT_FLOAT16,DT_FLOAT}))
  77. .ATTR(epsilon, Float, 0.0001)
  78. .OP_END_FACTORY_REG(BNInferGrad)
  79. REG_OP(ReduceSum)
  80. .INPUT(x, TensorType::NumberType())
  81. .INPUT(axis, TensorType::IndexNumberType())
  82. .OUTPUT(y, TensorType::NumberType())
  83. .ATTR(keep_dims, Bool, false)
  84. .OP_END_FACTORY_REG(ReduceSum)
  85. REG_OP(ReduceSumD)
  86. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8, DT_INT32}))
  87. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8, DT_INT32}))
  88. .REQUIRED_ATTR(axis, ListInt)
  89. .ATTR(keep_dims, Bool, false)
  90. .OP_END_FACTORY_REG(ReduceSumD)
  91. /**
  92. *@brief Calculates the "logical sum" of elements of a tensor in a dimension.
  93. *@par Inputs:
  94. *One input:
  95. *x: A mutable Tensor. Must be one of the following types: float16,
  96. * float32, double. Should be a Variable Tensor.
  97. *@par Attributes:
  98. *@li keep_dims: A bool. If true, retains reduced dimensions with length 1.
  99. *@li axis: The dimensions to reduce. If None, reduces all dimensions.
  100. *Must be in the range [- rank (input_sensor), rank (input_sensor)).
  101. *@par Outputs:
  102. *y: The reduced tensor.
  103. */
  104. REG_OP(ReduceAllD)
  105. .INPUT(x, TensorType({DT_BOOL}))
  106. .OUTPUT(y, TensorType({DT_BOOL}))
  107. .REQUIRED_ATTR(axis, ListInt)
  108. .ATTR(keep_dims, Bool, false)
  109. .OP_END_FACTORY_REG(ReduceAllD)
  110. /**
  111. *@brief Calculates the "logical sum" of elements of a tensor in a dimension.
  112. *@par Inputs:
  113. *Two inputs, including:
  114. *@li x: A mutable Tensor. Must be one of the following types: float16, float32, double. Should be a Variable Tensor.
  115. *@li axis: A mutable Tensor. The dimensions to reduce. If None, reduces all dimensions. Must be in the range [- rank (input_sensor), rank (input_sensor)).
  116. *@par Attributes:
  117. *keep_dims: A bool. If true, retains reduced dimensions with length 1.
  118. *@par Outputs:
  119. *y: The reduced tensor.
  120. */
  121. REG_OP(ReduceAll)
  122. .INPUT(x, TensorType({DT_BOOL}))
  123. .INPUT(axis, TensorType::IndexNumberType())
  124. .OUTPUT(y, TensorType({DT_BOOL}))
  125. .ATTR(keep_dims, Bool, false)
  126. .OP_END_FACTORY_REG(ReduceAll)
  127. REG_OP(ReduceProd)
  128. .INPUT(x,TensorType::NumberType())
  129. .INPUT(axis, TensorType::IndexNumberType())
  130. .OUTPUT(y,TensorType::NumberType())
  131. .ATTR(keep_dims, Bool, false)
  132. .OP_END_FACTORY_REG(ReduceProd)
  133. REG_OP(ReduceProdD)
  134. .INPUT(x,TensorType({DT_FLOAT, DT_UINT8, DT_INT8, DT_INT32, DT_FLOAT16}))
  135. .OUTPUT(y,TensorType({DT_FLOAT, DT_UINT8, DT_INT8, DT_INT32, DT_FLOAT16}))
  136. .REQUIRED_ATTR(axis, ListInt)
  137. .ATTR(keep_dims, Bool, false)
  138. .OP_END_FACTORY_REG(ReduceProdD)
  139. /**
  140. *@brief Reduces "x" along the dimensions according to "axis".
  141. *@par Inputs:
  142. *Two inputs, including:
  143. * @li x: A Tensor. Must be one of the following types: float16, float32, int8, uint8.
  144. * @li axis: The dimensions to reduce. Must be one of the following types: int, list, tuple, NoneType.\n
  145. * - If None (the default), reduces all dimensions.\n
  146. * - Must be in the range [-rank(x), rank(x)).
  147. *@par Attributes:
  148. *keep_dims: A bool or NoneType. \n
  149. * - If true, retains reduced dimensions with length 1. \n
  150. * - If false, the rank of the tensor is reduced by 1 for each entry in axis.
  151. *@par Outputs:
  152. *y: A Tensor. Has the same type as "x".
  153. */
  154. REG_OP(ReduceMean)
  155. .INPUT(x, TensorType::NumberType())
  156. .INPUT(axis, TensorType::IndexNumberType())
  157. .OUTPUT(y, TensorType::NumberType())
  158. .ATTR(keep_dims, Bool, false)
  159. .OP_END_FACTORY_REG(ReduceMean)
  160. /**
  161. *@brief Reduces "x" along the dimensions according to "axis".
  162. *@par Inputs:
  163. *One input:
  164. * @li x: A Tensor. Must be one of the following types: float16, float32, int8, uint8.
  165. *@par Attributes:
  166. *@li axis: The dimensions to reduce. Must be one of the following types: int, list, tuple, NoneType. \n
  167. * If None (the default), reduces all dimensions. \n
  168. * Must be in the range [-rank(x), rank(x)). \n
  169. *@li keep_dims: A bool or NoneType. \n
  170. * - If true, retains reduced dimensions with length 1. \n
  171. * - If false, the rank of the tensor is reduced by 1 for each entry in axis.
  172. *@par Outputs:
  173. *y: A Tensor. Has the same type as "x".
  174. */
  175. REG_OP(ReduceMeanD)
  176. .INPUT(x, TensorType({DT_FLOAT16, DT_INT32, DT_FLOAT, DT_INT8, DT_UINT8}))
  177. .OUTPUT(y, TensorType({DT_FLOAT16, DT_INT32, DT_FLOAT, DT_INT8, DT_UINT8}))
  178. .REQUIRED_ATTR(axis, ListInt)
  179. .ATTR(keep_dims, Bool, false)
  180. .OP_END_FACTORY_REG(ReduceMeanD)
  181. REG_OP(ReduceMax)
  182. .INPUT(x, TensorType::NumberType())
  183. .INPUT(axis, TensorType::IndexNumberType())
  184. .OUTPUT(y, TensorType::NumberType())
  185. .ATTR(keep_dims, Bool, false)
  186. .OP_END_FACTORY_REG(ReduceMax)
  187. REG_OP(ReduceMaxD)
  188. .INPUT(x, TensorType({DT_FLOAT, DT_UINT8, DT_INT8,
  189. DT_FLOAT16, DT_INT32}))
  190. .OUTPUT(y, TensorType({DT_FLOAT, DT_UINT8, DT_INT8,
  191. DT_FLOAT16, DT_INT32}))
  192. .REQUIRED_ATTR(axis, ListInt)
  193. .ATTR(keep_dims, Bool, false)
  194. .OP_END_FACTORY_REG(ReduceMaxD)
  195. REG_OP(ReduceMin)
  196. .INPUT(x, TensorType::NumberType())
  197. .INPUT(axis, TensorType::IndexNumberType())
  198. .OUTPUT(y, TensorType::NumberType())
  199. .ATTR(keep_dims, Bool, false)
  200. .OP_END_FACTORY_REG(ReduceMin)
  201. REG_OP(ReduceMinD)
  202. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT8,DT_UINT8}))
  203. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT8,DT_UINT8}))
  204. .REQUIRED_ATTR(axis, ListInt)
  205. .ATTR(keep_dims, Bool, false)
  206. .OP_END_FACTORY_REG(ReduceMinD)
  207. /**
  208. *@brief Computes the "logical or" of elements across dimensions of a tensor.\n
  209. * Reduces `x` along the dimensions given in `axis`.
  210. * Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each
  211. * entry in `axis`. If `keep_dims` is true, the reduced dimensions
  212. * are retained with length 1.
  213. *
  214. * If `axis` is None, all dimensions are reduced, and a
  215. * tensor with a single element is returned.
  216. *
  217. *@attention Constraints:\n
  218. * Only support bool
  219. *
  220. *@par Inputs:
  221. *@li x : The boolean tensor to reduce.
  222. *@li axis : The dimensions to reduce. If `None` (the default), reduces all
  223. * dimensions. Must be in the range `[-rank(x), rank(x))`.
  224. *
  225. *@par Attributes:
  226. * keep_dims : If true, retains reduced dimensions with length 1.
  227. *
  228. *@par Outputs:
  229. * y : The reduced tensor
  230. *
  231. */
  232. REG_OP(ReduceAny)
  233. .INPUT(x, TensorType({DT_BOOL}))
  234. .INPUT(axis, TensorType::IndexNumberType())
  235. .OUTPUT(y, TensorType({DT_BOOL}))
  236. .ATTR(keep_dims, Bool, false)
  237. .OP_END_FACTORY_REG(ReduceAny)
  238. /**
  239. *@brief Computes the "logical or" of elements across dimensions of a tensor.\n
  240. * Reduces `x` along the dimensions given in `axis`.
  241. * Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each
  242. * entry in `axis`. If `keep_dims` is true, the reduced dimensions
  243. * are retained with length 1.
  244. *
  245. * If `axis` is None, all dimensions are reduced, and a
  246. * tensor with a single element is returned.
  247. *
  248. *@attention Constraints:\n
  249. * Only support bool
  250. *
  251. *@par Inputs:
  252. * x : The boolean tensor to reduce.
  253. *
  254. *@par Attributes:
  255. *@li axis : The dimensions to reduce. If `None` (the default), reduces all
  256. * dimensions. Must be in the range `[-rank(x), rank(x))`.
  257. *@li keep_dims : If true, retains reduced dimensions with length 1.
  258. *
  259. *@par Outputs:
  260. * y : The reduced tensor
  261. *
  262. */
  263. REG_OP(ReduceAnyD)
  264. .INPUT(x, TensorType({DT_BOOL}))
  265. .OUTPUT(y, TensorType({DT_BOOL}))
  266. .REQUIRED_ATTR(axis, ListInt)
  267. .ATTR(keep_dims, Bool, false)
  268. .OP_END_FACTORY_REG(ReduceAnyD)
  269. } //namespace ge
  270. #endif /* GE_OP_REDUCE_OPS_H */

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