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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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(BNInfer)
  53. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  54. .INPUT(scale, TensorType({DT_FLOAT}))
  55. .INPUT(offset, TensorType({DT_FLOAT}))
  56. .INPUT(mean, TensorType({DT_FLOAT}))
  57. .INPUT(variance, TensorType({DT_FLOAT}))
  58. .REQUIRED_ATTR(epsilon, Float)
  59. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  60. .OP_END_FACTORY_REG(BNInfer)
  61. REG_OP(BNTrainingUpdateV2)
  62. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  63. .INPUT(sum, TensorType({DT_FLOAT}))
  64. .INPUT(square_sum, TensorType({DT_FLOAT}))
  65. .INPUT(scale, TensorType({DT_FLOAT}))
  66. .INPUT(offset, TensorType({DT_FLOAT}))
  67. .REQUIRED_ATTR(epsilon, Float)
  68. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT}))
  69. .OUTPUT(batch_mean, TensorType({DT_FLOAT}))
  70. .OUTPUT(batch_variance, TensorType({DT_FLOAT}))
  71. .OP_END_FACTORY_REG(BNTrainingUpdateV2)
  72. REG_OP(BNTrainingUpdateGrad)
  73. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  74. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  75. .INPUT(batch_mean, TensorType({DT_FLOAT}))
  76. .INPUT(batch_variance, TensorType({DT_FLOAT}))
  77. .ATTR(epsilon, Float, 0.0001)
  78. .OUTPUT(diff_scale, TensorType({DT_FLOAT}))
  79. .OUTPUT(diff_offset, TensorType({DT_FLOAT}))
  80. .OP_END_FACTORY_REG(BNTrainingUpdateGrad)
  81. REG_OP(BNInferGrad)
  82. .INPUT(grads, TensorType({DT_FLOAT16,DT_FLOAT}))
  83. .INPUT(scale, TensorType({DT_FLOAT}))
  84. .INPUT(batch_variance, TensorType({DT_FLOAT}))
  85. .OUTPUT(x_backprop, TensorType({DT_FLOAT16,DT_FLOAT}))
  86. .ATTR(epsilon, Float, 0.0001)
  87. .OP_END_FACTORY_REG(BNInferGrad)
  88. REG_OP(ReduceSum)
  89. .INPUT(x, TensorType::NumberType())
  90. .INPUT(axis, TensorType::IndexNumberType())
  91. .OUTPUT(y, TensorType::NumberType())
  92. .ATTR(keep_dims, Bool, false)
  93. .OP_END_FACTORY_REG(ReduceSum)
  94. REG_OP(ReduceSumD)
  95. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8, DT_INT32}))
  96. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, DT_UINT8, DT_INT32}))
  97. .REQUIRED_ATTR(axis, ListInt)
  98. .ATTR(keep_dims, Bool, false)
  99. .OP_END_FACTORY_REG(ReduceSumD)
  100. /**
  101. *@brief Calculates the "logical sum" of elements of a tensor in a dimension.
  102. *@par Inputs:
  103. *One input:
  104. *x: A mutable Tensor. Must be one of the following types: float16,
  105. * float32, double. Should be a Variable Tensor.
  106. *@par Attributes:
  107. *@li keep_dims: A bool. If true, retains reduced dimensions with length 1.
  108. *@li axis: The dimensions to reduce. If None, reduces all dimensions.
  109. *Must be in the range [- rank (input_sensor), rank (input_sensor)).
  110. *@par Outputs:
  111. *y: The reduced tensor.
  112. */
  113. REG_OP(ReduceAllD)
  114. .INPUT(x, TensorType({DT_BOOL}))
  115. .OUTPUT(y, TensorType({DT_BOOL}))
  116. .REQUIRED_ATTR(axis, ListInt)
  117. .ATTR(keep_dims, Bool, false)
  118. .OP_END_FACTORY_REG(ReduceAllD)
  119. /**
  120. *@brief Calculates the "logical sum" of elements of a tensor in a dimension.
  121. *@par Inputs:
  122. *Two inputs, including:
  123. *@li x: A mutable Tensor. Must be one of the following types: float16, float32, double. Should be a Variable Tensor.
  124. *@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)).
  125. *@par Attributes:
  126. *keep_dims: A bool. If true, retains reduced dimensions with length 1.
  127. *@par Outputs:
  128. *y: The reduced tensor.
  129. */
  130. REG_OP(ReduceAll)
  131. .INPUT(x, TensorType({DT_BOOL}))
  132. .INPUT(axis, TensorType::IndexNumberType())
  133. .OUTPUT(y, TensorType({DT_BOOL}))
  134. .ATTR(keep_dims, Bool, false)
  135. .OP_END_FACTORY_REG(ReduceAll)
  136. REG_OP(ReduceProd)
  137. .INPUT(x,TensorType::NumberType())
  138. .INPUT(axis, TensorType::IndexNumberType())
  139. .OUTPUT(y,TensorType::NumberType())
  140. .ATTR(keep_dims, Bool, false)
  141. .OP_END_FACTORY_REG(ReduceProd)
  142. REG_OP(ReduceProdD)
  143. .INPUT(x,TensorType({DT_FLOAT, DT_UINT8, DT_INT8, DT_INT32, DT_FLOAT16}))
  144. .OUTPUT(y,TensorType({DT_FLOAT, DT_UINT8, DT_INT8, DT_INT32, DT_FLOAT16}))
  145. .REQUIRED_ATTR(axis, ListInt)
  146. .ATTR(keep_dims, Bool, false)
  147. .OP_END_FACTORY_REG(ReduceProdD)
  148. /**
  149. *@brief Reduces "x" along the dimensions according to "axis".
  150. *@par Inputs:
  151. *Two inputs, including:
  152. * @li x: A Tensor. Must be one of the following types: float16, float32, int8, uint8.
  153. * @li axis: The dimensions to reduce. Must be one of the following types: int, list, tuple, NoneType.\n
  154. * - If None (the default), reduces all dimensions.\n
  155. * - Must be in the range [-rank(x), rank(x)).
  156. *@par Attributes:
  157. *keep_dims: A bool or NoneType. \n
  158. * - If true, retains reduced dimensions with length 1. \n
  159. * - If false, the rank of the tensor is reduced by 1 for each entry in axis.
  160. *@par Outputs:
  161. *y: A Tensor. Has the same type as "x".
  162. */
  163. REG_OP(ReduceMean)
  164. .INPUT(x, TensorType::NumberType())
  165. .INPUT(axis, TensorType::IndexNumberType())
  166. .OUTPUT(y, TensorType::NumberType())
  167. .ATTR(keep_dims, Bool, false)
  168. .OP_END_FACTORY_REG(ReduceMean)
  169. /**
  170. *@brief Reduces "x" along the dimensions according to "axis".
  171. *@par Inputs:
  172. *One input:
  173. * @li x: A Tensor. Must be one of the following types: float16, float32, int8, uint8.
  174. *@par Attributes:
  175. *@li axis: The dimensions to reduce. Must be one of the following types: int, list, tuple, NoneType. \n
  176. * If None (the default), reduces all dimensions. \n
  177. * Must be in the range [-rank(x), rank(x)). \n
  178. *@li keep_dims: A bool or NoneType. \n
  179. * - If true, retains reduced dimensions with length 1. \n
  180. * - If false, the rank of the tensor is reduced by 1 for each entry in axis.
  181. *@par Outputs:
  182. *y: A Tensor. Has the same type as "x".
  183. */
  184. REG_OP(ReduceMeanD)
  185. .INPUT(x, TensorType({DT_FLOAT16, DT_INT32, DT_FLOAT, DT_INT8, DT_UINT8}))
  186. .OUTPUT(y, TensorType({DT_FLOAT16, DT_INT32, DT_FLOAT, DT_INT8, DT_UINT8}))
  187. .REQUIRED_ATTR(axis, ListInt)
  188. .ATTR(keep_dims, Bool, false)
  189. .OP_END_FACTORY_REG(ReduceMeanD)
  190. REG_OP(ReduceMax)
  191. .INPUT(x, TensorType::NumberType())
  192. .INPUT(axis, TensorType::IndexNumberType())
  193. .OUTPUT(y, TensorType::NumberType())
  194. .ATTR(keep_dims, Bool, false)
  195. .OP_END_FACTORY_REG(ReduceMax)
  196. /**
  197. *@brief Returns the maximum of elements across dimensions of a Tensor.
  198. *@par Inputs:
  199. *x: A multi-dimensional Tensor of type float16, float32, or int16.
  200. *@par Attributes:
  201. * Two attributes, including: \n
  202. *@li axis: A required listint, specifying the axis information of the index with the maximum value.
  203. *@li keep_dims: A bool, specifying whether to keep dimensions for the output Tensor. Defaults to "false".
  204. *@par Outputs:
  205. *y: A multi-dimensional Tensor, specifying the maximum value of the corresponding axis in the tensor. Has the same type as "x". (If "keep_dims" is set to "false", the output dimensions are reduced by "dimension" compared with that of "x". Otherwise, the output has one fewer dimension than "x".)
  206. *@attention Constraints:
  207. * The value range of "axis" is [-dims, dims - 1]. "dims" indicates the dimension length of "x".
  208. */
  209. REG_OP(ReduceMaxD)
  210. .INPUT(x, TensorType({DT_FLOAT, DT_UINT8, DT_INT8,
  211. DT_FLOAT16, DT_INT32}))
  212. .OUTPUT(y, TensorType({DT_FLOAT, DT_UINT8, DT_INT8,
  213. DT_FLOAT16, DT_INT32}))
  214. .REQUIRED_ATTR(axis, ListInt)
  215. .ATTR(keep_dims, Bool, false)
  216. .OP_END_FACTORY_REG(ReduceMaxD)
  217. REG_OP(ReduceMin)
  218. .INPUT(x, TensorType::NumberType())
  219. .INPUT(axis, TensorType::IndexNumberType())
  220. .OUTPUT(y, TensorType::NumberType())
  221. .ATTR(keep_dims, Bool, false)
  222. .OP_END_FACTORY_REG(ReduceMin)
  223. REG_OP(ReduceMinD)
  224. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT8,DT_UINT8}))
  225. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT8,DT_UINT8}))
  226. .REQUIRED_ATTR(axis, ListInt)
  227. .ATTR(keep_dims, Bool, false)
  228. .OP_END_FACTORY_REG(ReduceMinD)
  229. /**
  230. *@brief Computes the "logical or" of elements across dimensions of a tensor.\n
  231. * Reduces `x` along the dimensions given in `axis`.
  232. * Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each
  233. * entry in `axis`. If `keep_dims` is true, the reduced dimensions
  234. * are retained with length 1.
  235. *
  236. * If `axis` is None, all dimensions are reduced, and a
  237. * tensor with a single element is returned.
  238. *
  239. *@attention Constraints:\n
  240. * Only support bool
  241. *
  242. *@par Inputs:
  243. *@li x : The boolean tensor to reduce.
  244. *@li axis : The dimensions to reduce. If `None` (the default), reduces all
  245. * dimensions. Must be in the range `[-rank(x), rank(x))`.
  246. *
  247. *@par Attributes:
  248. * keep_dims : If true, retains reduced dimensions with length 1.
  249. *
  250. *@par Outputs:
  251. * y : The reduced tensor
  252. *
  253. */
  254. REG_OP(ReduceAny)
  255. .INPUT(x, TensorType({DT_BOOL}))
  256. .INPUT(axis, TensorType::IndexNumberType())
  257. .OUTPUT(y, TensorType({DT_BOOL}))
  258. .ATTR(keep_dims, Bool, false)
  259. .OP_END_FACTORY_REG(ReduceAny)
  260. /**
  261. *@brief Computes the "logical or" of elements across dimensions of a tensor.\n
  262. * Reduces `x` along the dimensions given in `axis`.
  263. * Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each
  264. * entry in `axis`. If `keep_dims` is true, the reduced dimensions
  265. * are retained with length 1.
  266. *
  267. * If `axis` is None, all dimensions are reduced, and a
  268. * tensor with a single element is returned.
  269. *
  270. *@attention Constraints:\n
  271. * Only support bool
  272. *
  273. *@par Inputs:
  274. * x : The boolean tensor to reduce.
  275. *
  276. *@par Attributes:
  277. *@li axis : The dimensions to reduce. If `None` (the default), reduces all
  278. * dimensions. Must be in the range `[-rank(x), rank(x))`.
  279. *@li keep_dims : If true, retains reduced dimensions with length 1.
  280. *
  281. *@par Outputs:
  282. * y : The reduced tensor
  283. *
  284. */
  285. REG_OP(ReduceAnyD)
  286. .INPUT(x, TensorType({DT_BOOL}))
  287. .OUTPUT(y, TensorType({DT_BOOL}))
  288. .REQUIRED_ATTR(axis, ListInt)
  289. .ATTR(keep_dims, Bool, false)
  290. .OP_END_FACTORY_REG(ReduceAnyD)
  291. } //namespace ge
  292. #endif /* GE_OP_REDUCE_OPS_H */

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