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.4 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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". Defaults to "False".
  83. *@li round_mode: An optional string, specifying the float16 to int8 cast type.
  84. * The value range is [Round, Floor, Ceil, Truncate]. Defaults to "Round" .
  85. *@li dst_type: A optional int32, specifying the output data type. Defaults to "DT_INT8" . \n
  86. *@par Outputs:
  87. *y: The quantized output tensor of type int8 or int4. \n
  88. *@par Third-party framework compatibility
  89. * It is a custom operator. It has no corresponding operator in Caffe.
  90. */
  91. REG_OP(AscendQuant)
  92. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  93. .OUTPUT(y, TensorType({DT_INT8, DT_INT4}))
  94. .REQUIRED_ATTR(scale, Float)
  95. .REQUIRED_ATTR(offset, Float)
  96. .ATTR(sqrt_mode, Bool, false)
  97. .ATTR(round_mode, String, "Round")
  98. .ATTR(dst_type, Int, DT_INT8)
  99. .OP_END_FACTORY_REG(AscendQuant)
  100. /**
  101. *@brief Dequantizes the input . \n
  102. *@par Inputs:
  103. *@li x: An tensor of type int32, specifying the input.
  104. *@li deq_scale: An tensor of type float16 or uint64, specifying the scaling ratio . \n
  105. *@par Attributes:
  106. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  107. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  108. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT" . \n
  109. *@par Outputs:
  110. *y: The dequantized output tensor of type float16 or float32. \n
  111. *@par Third-party framework compatibility
  112. * It is a custom operator. It has no corresponding operator in Caffe.
  113. */
  114. REG_OP(AscendDequant)
  115. .INPUT(x, TensorType({DT_INT32}))
  116. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  117. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  118. .ATTR(sqrt_mode, Bool, false)
  119. .ATTR(relu_flag, Bool, false)
  120. .ATTR(dtype, Int, DT_FLOAT)
  121. .OP_END_FACTORY_REG(AscendDequant)
  122. /**
  123. *@brief Anti quantizes the input . \n
  124. *@par Inputs:
  125. *x: An tensor of type int8, specifying the input . \n
  126. *@par Attributes:
  127. *@li scale: A required float32 scale.
  128. *@li offset: A required float32 offset.
  129. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  130. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False" . \n
  131. *@par Outputs:
  132. *y: The dequantized output tensor of type float16 or float32. \n
  133. *@par Third-party framework compatibility
  134. * It is a custom operator. It has no corresponding operator in Caffe.
  135. */
  136. REG_OP(AscendAntiQuant)
  137. .INPUT(x, TensorType({DT_INT8}))
  138. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  139. .REQUIRED_ATTR(scale, Float)
  140. .REQUIRED_ATTR(offset, Float)
  141. .ATTR(dtype, Int, DT_FLOAT)
  142. .ATTR(sqrt_mode, Bool, false)
  143. .OP_END_FACTORY_REG(AscendAntiQuant)
  144. /**
  145. *@brief Dequantizes the input of int16 . \n
  146. *@par Inputs:
  147. *@li x0: An tensor of type int32, specifying the input.
  148. *@li deq_scale: An tensor of type uint64, specifying the scaling ratio.
  149. *@li x1: An tensor of type int16, specifying the input . \n
  150. *@par Attributes:
  151. *relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  152. *@par Outputs:
  153. *y: The dequantized output tensor of type int16. \n
  154. *@par Third-party framework compatibility
  155. * It is a custom operator. It has no corresponding operator in Caffe.
  156. */
  157. REG_OP(AscendDequantS16)
  158. .INPUT(x0, TensorType({DT_INT32}))
  159. .INPUT(deq_scale, TensorType({DT_UINT64}))
  160. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  161. .OUTPUT(y, TensorType({DT_INT16}))
  162. .ATTR(relu_flag, Bool, false)
  163. .OP_END_FACTORY_REG(AscendDequantS16)
  164. /**
  165. *@brief Requantizes the input . \n
  166. *@par Inputs:
  167. *@li x: An tensor of type int32, specifying the input.
  168. *@li req_scale: An tensor of type uint64, specifying the scaling ratio . \n
  169. *@par Attributes:
  170. *relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  171. *@par Outputs:
  172. *y: The dequantized output tensor of type int8. \n
  173. *@par Third-party framework compatibility
  174. * It is a custom operator. It has no corresponding operator in Caffe.
  175. */
  176. REG_OP(AscendRequant)
  177. .INPUT(x, TensorType({DT_INT32}))
  178. .INPUT(req_scale, TensorType({DT_UINT64}))
  179. .OUTPUT(y, TensorType({DT_INT8}))
  180. .ATTR(relu_flag, Bool, false)
  181. .OP_END_FACTORY_REG(AscendRequant)
  182. /**
  183. *@brief Requantizes the input of int16 . \n
  184. *@par Inputs:
  185. *@li x0: An tensor of type int16, specifying the input.
  186. *@li req_scale: An tensor of type uint64, specifying the scaling ratio.
  187. *@li x1: An tensor of type int16 . \n
  188. *@par Attributes:
  189. *@li dual_output: A optional bool, specifying whether to perform dual ouput, either "True" or "False". Defaults to "False".
  190. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  191. *@par Outputs:
  192. *@li y0: The dequantized output tensor of type int8.
  193. *@li y1: The dequantized output tensor of type int16. \n
  194. *@par Third-party framework compatibility
  195. * It is a custom operator. It has no corresponding operator in Caffe.
  196. */
  197. REG_OP(AscendRequantS16)
  198. .INPUT(x0, TensorType({DT_INT16}))
  199. .INPUT(req_scale, TensorType({DT_UINT64}))
  200. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  201. .OUTPUT(y0, TensorType({DT_INT8}))
  202. .OUTPUT(y1, TensorType({DT_INT16}))
  203. .ATTR(dual_output, Bool, false)
  204. .ATTR(relu_flag, Bool, false)
  205. .OP_END_FACTORY_REG(AscendRequantS16)
  206. /**
  207. * @brief Quantizes the input of int8 . \n
  208. * @par Inputs:
  209. * @li x: An FRACTAL_Z tensor of type int8, specifying the input.
  210. * @li offset: An FRACTAL_Z tensor of type int8.
  211. * @par Attributes:
  212. * @li dst_type: A optional int from: DT_INT8, DT_INT4. Defaults to DT_INT8.
  213. * @par Outputs:
  214. * @li y: output tensor of type int4 or int8 and with format FRACTAL_Z.
  215. * @par Third-party framework compatibility
  216. * It is a custom operator. It has no corresponding operator in Caffe, Onnx, Tensorflow or Pythorch.
  217. */
  218. REG_OP(AscendWeightQuant)
  219. .INPUT(x, TensorType({DT_INT8}))
  220. .INPUT(offset, TensorType({DT_INT8}))
  221. .OUTPUT(y, TensorType({DT_INT8, DT_INT4}))
  222. .ATTR(dst_type, Int, DT_INT8)
  223. .OP_END_FACTORY_REG(AscendWeightQuant)
  224. } // namespace ge
  225. #endif // OPS_BUILT_IN_OP_PROTO_INC_QUANTIZE_OPS_H_

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