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.

quantize_ops.h 9.7 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright 2019 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. /*!
  17. * \file quantize_ops.h
  18. * \brief
  19. */
  20. #ifndef OPS_BUILT_IN_OP_PROTO_INC_QUANTIZE_OPS_H_
  21. #define OPS_BUILT_IN_OP_PROTO_INC_QUANTIZE_OPS_H_
  22. #include "graph/operator_reg.h"
  23. namespace ge {
  24. /**
  25. * @brief Dequantizes the input tensor into a float tensor.
  26. * [min_range, max_range] are float32 tensors that specify the range
  27. * for "y".
  28. * The "mode" attribute controls exactly which calculations are used to convert
  29. * the float values to their quantized equivalents.
  30. * @par Inputs:
  31. * @li x: A Tensor. Must be one of the following types: int8, uint8,
  32. * int32.
  33. * @li min_range: A Tensor of type float32.
  34. * Specifies the minimum scalar value possibly produced for the input.
  35. * @li max_range: A Tensor of type float32.
  36. * Specifies the maximum scalar value possibly produced for the input . \n
  37. * @par Attributes:
  38. * mode: An optional string from: "MIN_COMBINED", "MIN_FIRST", and "SCALED".
  39. * Defaults to "MIN_COMBINED" . \n
  40. * @par Outputs:
  41. * y: A dictionary of type float32 . \n
  42. * @attention Constraints:
  43. * @li "min_range" and "max_range" have the same shapes.
  44. * @li "x" and "y" have the same shapes . \n
  45. * @par Third-party framework compatibility
  46. * Compatible with the TensorFlow operator Dequantize.
  47. */
  48. REG_OP(Dequantize)
  49. .INPUT(x, TensorType(DT_QINT8, DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16))
  50. .INPUT(min_range, TensorType{DT_FLOAT})
  51. .INPUT(max_range, TensorType{DT_FLOAT})
  52. .OUTPUT(y, TensorType({DT_FLOAT}))
  53. .ATTR(mode, String, "MIN_COMBINED")
  54. .OP_END_FACTORY_REG(Dequantize)
  55. /**
  56. * @brief Quantizes the input . \n
  57. * @par Inputs:
  58. * @li x: shape and dtype of input_x. \n
  59. * @li scales: shape and dtype of input_scales. \n
  60. * @li zero_points: shape and dtype of input_zero_points \n
  61. * @par Attributes:
  62. * @li dtype: required, type.
  63. * @li axis: the processed dim. \n
  64. * @par Outputs:
  65. * y: shape and dtype of output_y, should be same shape as input, dtype is same as the quantified type . \n
  66. */
  67. REG_OP(Quantize)
  68. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  69. .INPUT(scales, TensorType({DT_FLOAT}))
  70. .INPUT(zero_points, TensorType({DT_INT8,DT_UINT8,DT_INT32}))
  71. .OUTPUT(y, TensorType({DT_INT8,DT_UINT8,DT_INT32}))
  72. .REQUIRED_ATTR(dtype, String)
  73. .ATTR(axis, Int, 1)
  74. .OP_END_FACTORY_REG(Quantize)
  75. /**
  76. * @brief Quantizes the input . \n
  77. * @par Inputs:
  78. * x: An tensor of type float16 or float32, specifying the input . \n
  79. * @par Attributes:
  80. * @li scale: A required float32, specifying the scaling ratio.
  81. * @li offset: A required float16, specifying the offset.
  82. * @li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False".
  83. * Defaults to "False".
  84. * @li round_mode: An optional string, specifying the float16 to int8 cast type.
  85. * The value range is [Round, Floor, Ceil, Truncate]. Defaults to "Round" .
  86. * @li dst_type: A optional int32, specifying the output data type. Defaults to "DT_INT8" . \n
  87. * @par Outputs:
  88. * y: The quantized output tensor of type int8 or int4. \n
  89. * @attention Constraints:
  90. * round_mode value range is [Round, Floor, Ceil, Truncate].
  91. * @li Round: round to nearest, tie to even(c language rint).
  92. * @li Floor: round to minus infinity(c language floor).
  93. * @li Ceil: round to positive infinity(c language ceil).
  94. * @li Truncate: round to zero(c language trunc). \n
  95. * @par Third-party framework compatibility
  96. * It is a custom operator. It has no corresponding operator in Caffe.
  97. */
  98. REG_OP(AscendQuant)
  99. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  100. .OUTPUT(y, TensorType({DT_INT8, DT_INT4}))
  101. .REQUIRED_ATTR(scale, Float)
  102. .REQUIRED_ATTR(offset, Float)
  103. .ATTR(sqrt_mode, Bool, false)
  104. .ATTR(round_mode, String, "Round")
  105. .ATTR(dst_type, Int, DT_INT8)
  106. .OP_END_FACTORY_REG(AscendQuant)
  107. /**
  108. * @brief Dequantizes the input . \n
  109. *@par Inputs:
  110. * @li x: An tensor of type int32, specifying the input.
  111. * @li deq_scale: An tensor of type uint64, specifying the scaling ratio . \n
  112. * @par Attributes:
  113. * @li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False".
  114. * Defaults to "False".
  115. * @li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  116. * @li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT" . \n
  117. * @par Outputs:
  118. * y: The dequantized output tensor of type float16 or float32. \n
  119. * @par Third-party framework compatibility
  120. * It is a custom operator. It has no corresponding operator in Caffe.
  121. */
  122. REG_OP(AscendDequant)
  123. .INPUT(x, TensorType({DT_INT32}))
  124. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  125. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  126. .ATTR(sqrt_mode, Bool, false)
  127. .ATTR(relu_flag, Bool, false)
  128. .ATTR(dtype, Int, DT_FLOAT)
  129. .OP_END_FACTORY_REG(AscendDequant)
  130. /**
  131. * @brief Anti quantizes the input . \n
  132. * @par Inputs:
  133. * x: An tensor of type int8, specifying the input . \n
  134. * @par Attributes:
  135. * @li scale: A required float32 scale.
  136. * @li offset: A required float32 offset.
  137. * @li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  138. * @li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False".
  139. * Defaults to "False" . \n
  140. * @par Outputs:
  141. * y: The dequantized output tensor of type float16 or float32. \n
  142. * @par Third-party framework compatibility
  143. * It is a custom operator. It has no corresponding operator in Caffe.
  144. */
  145. REG_OP(AscendAntiQuant)
  146. .INPUT(x, TensorType({DT_INT8}))
  147. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  148. .REQUIRED_ATTR(scale, Float)
  149. .REQUIRED_ATTR(offset, Float)
  150. .ATTR(dtype, Int, DT_FLOAT)
  151. .ATTR(sqrt_mode, Bool, false)
  152. .OP_END_FACTORY_REG(AscendAntiQuant)
  153. /**
  154. * @brief Dequantizes the input of int16 . \n
  155. * @par Inputs:
  156. * @li x0: An tensor of type int32, specifying the input.
  157. * @li deq_scale: An tensor of type uint64, specifying the scaling ratio.
  158. * @li x1: An tensor of type int16, specifying the input . \n
  159. * @par Attributes:
  160. * relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  161. * @par Outputs:
  162. * y: The dequantized output tensor of type int16. \n
  163. * @par Third-party framework compatibility
  164. * It is a custom operator. It has no corresponding operator in Caffe.
  165. */
  166. REG_OP(AscendDequantS16)
  167. .INPUT(x0, TensorType({DT_INT32}))
  168. .INPUT(deq_scale, TensorType({DT_UINT64}))
  169. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  170. .OUTPUT(y, TensorType({DT_INT16}))
  171. .ATTR(relu_flag, Bool, false)
  172. .OP_END_FACTORY_REG(AscendDequantS16)
  173. /**
  174. * @brief Requantizes the input . \n
  175. * @par Inputs:
  176. * @li x: An tensor of type int32, specifying the input.
  177. * @li req_scale: An tensor of type uint64, specifying the scaling ratio . \n
  178. * @par Attributes:
  179. * relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  180. * @par Outputs:
  181. * y: The dequantized output tensor of type int8. \n
  182. * @par Third-party framework compatibility
  183. * It is a custom operator. It has no corresponding operator in Caffe.
  184. */
  185. REG_OP(AscendRequant)
  186. .INPUT(x, TensorType({DT_INT32}))
  187. .INPUT(req_scale, TensorType({DT_UINT64}))
  188. .OUTPUT(y, TensorType({DT_INT8}))
  189. .ATTR(relu_flag, Bool, false)
  190. .OP_END_FACTORY_REG(AscendRequant)
  191. /**
  192. * @brief Requantizes the input of int16 . \n
  193. * @par Inputs:
  194. * @li x0: An tensor of type int16, specifying the input.
  195. * @li req_scale: An tensor of type uint64, specifying the scaling ratio.
  196. * @li x1: An tensor of type int16 . \n
  197. * @par Attributes:
  198. * @li dual_output: A optional bool, specifying whether to perform dual ouput, either "True" or "False".
  199. * Defaults to "False".
  200. * @li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  201. * @par Outputs:
  202. * @li y0: The dequantized output tensor of type int8.
  203. * @li y1: The dequantized output tensor of type int16. \n
  204. * @par Third-party framework compatibility
  205. * It is a custom operator. It has no corresponding operator in Caffe.
  206. */
  207. REG_OP(AscendRequantS16)
  208. .INPUT(x0, TensorType({DT_INT16}))
  209. .INPUT(req_scale, TensorType({DT_UINT64}))
  210. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  211. .OUTPUT(y0, TensorType({DT_INT8}))
  212. .OUTPUT(y1, TensorType({DT_INT16}))
  213. .ATTR(dual_output, Bool, false)
  214. .ATTR(relu_flag, Bool, false)
  215. .OP_END_FACTORY_REG(AscendRequantS16)
  216. /**
  217. * @brief Quantizes the input of int8 . \n
  218. * @par Inputs:
  219. * @li x: A tensor of type int8, specifying the input.
  220. * @li offset: A tensor of type int8.
  221. * @par Attributes:
  222. * @li dst_type: A optional int from: DT_INT8, DT_INT4. Defaults to DT_INT8.
  223. * @par Outputs:
  224. * @li y: output tensor of type int4 or int8.
  225. * @par Third-party framework compatibility
  226. * It is a custom operator. It has no corresponding operator in Caffe, Onnx, Tensorflow or Pythorch.
  227. */
  228. REG_OP(AscendWeightQuant)
  229. .INPUT(x, TensorType({DT_INT8}))
  230. .INPUT(offset, TensorType({DT_INT8}))
  231. .OUTPUT(y, TensorType({DT_INT8, DT_INT4}))
  232. .ATTR(dst_type, Int, DT_INT8)
  233. .OP_END_FACTORY_REG(AscendWeightQuant)
  234. } // namespace ge
  235. #endif // OPS_BUILT_IN_OP_PROTO_INC_QUANTIZE_OPS_H_

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