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

5 years ago
3 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
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. *x: shape and dtype of input_x. \n
  59. *scales: shape and dtype of input_scales. \n
  60. *zero_points: shape and dtype of input_zero_points \n
  61. *@par Attributes:
  62. *@li axis: the processed dim. \n
  63. *@par Outputs:
  64. *y: shape and dtype of output_y, should be same shape as input, dtype is same as the quantified type . \n
  65. */
  66. REG_OP(Quantize)
  67. .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT}))
  68. .INPUT(scales, TensorType({DT_FLOAT}))
  69. .INPUT(zero_points, TensorType({DT_INT8,DT_UINT8,DT_INT32}))
  70. .OUTPUT(y, TensorType({DT_INT8,DT_UINT8,DT_INT32}))
  71. .REQUIRED_ATTR(dtype, String)
  72. .ATTR(axis, Int, 1)
  73. .OP_END_FACTORY_REG(Quantize)
  74. /**
  75. *@brief Quantizes the input . \n
  76. *@par Inputs:
  77. *x: An NC1HWC0 tensor of type float16 or float32, specifying the input . \n
  78. *@par Attributes:
  79. *@li scale: A required float32, specifying the scaling ratio.
  80. *@li offset: A required float16, specifying the offset.
  81. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  82. *@li round_mode: An optional string, specifying the float16 to int8 cast type.
  83. * The value range is [Round, Floor, Ceiling, Truncate]. Defaults to "Round" . \n
  84. *@par Outputs:
  85. *y: The quantized output tensor of type int8 and with format NC1HWC0 . \n
  86. *@par Third-party framework compatibility
  87. * It is a custom operator. It has no corresponding operator in Caffe.
  88. */
  89. REG_OP(AscendQuant)
  90. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  91. .OUTPUT(y, TensorType({DT_INT8}))
  92. .REQUIRED_ATTR(scale, Float)
  93. .REQUIRED_ATTR(offset, Float)
  94. .ATTR(sqrt_mode, Bool, false)
  95. .ATTR(round_mode, String, "Round")
  96. .OP_END_FACTORY_REG(AscendQuant)
  97. /**
  98. *@brief Dequantizes the input . \n
  99. *@par Inputs:
  100. *@li x: An NC1HWC0 tensor of type int32, specifying the input.
  101. *@li deq_scale: An NC1HWC0 tensor of type float16 or uint64, specifying the scaling ratio . \n
  102. *@par Attributes:
  103. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  104. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  105. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT" . \n
  106. *@par Outputs:
  107. *y: The dequantized output tensor of type float16 or float32 and with format NC1HWC0 . \n
  108. *@par Third-party framework compatibility
  109. * It is a custom operator. It has no corresponding operator in Caffe.
  110. */
  111. REG_OP(AscendDequant)
  112. .INPUT(x, TensorType({DT_INT32}))
  113. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  114. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  115. .ATTR(sqrt_mode, Bool, false)
  116. .ATTR(relu_flag, Bool, false)
  117. .ATTR(dtype, Int, DT_FLOAT)
  118. .OP_END_FACTORY_REG(AscendDequant)
  119. /**
  120. *@brief Anti quantizes the input . \n
  121. *@par Inputs:
  122. *x: An NC1HWC0 tensor of type int8, specifying the input . \n
  123. *@par Attributes:
  124. *@li scale: A required float32 scale.
  125. *@li offset: A required float32 offset.
  126. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  127. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False" . \n
  128. *@par Outputs:
  129. *y: The dequantized output tensor of type float16 or float32 and with format NC1HWC0 . \n
  130. *@par Third-party framework compatibility
  131. * It is a custom operator. It has no corresponding operator in Caffe.
  132. */
  133. REG_OP(AscendAntiQuant)
  134. .INPUT(x, TensorType({DT_INT8}))
  135. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  136. .REQUIRED_ATTR(scale, Float)
  137. .REQUIRED_ATTR(offset, Float)
  138. .ATTR(dtype, Int, DT_FLOAT)
  139. .ATTR(sqrt_mode, Bool, false)
  140. .OP_END_FACTORY_REG(AscendAntiQuant)
  141. /**
  142. *@brief Dequantizes the input of int16 . \n
  143. *@par Inputs:
  144. *@li x0: An NC1HWC0 tensor of type int32, specifying the input.
  145. *@li deq_scale: An NC1HWC0 tensor of type uint64, specifying the scaling ratio.
  146. *@li x1: An NC1HWC0 tensor of type int16, specifying the input . \n
  147. *@par Attributes:
  148. *relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  149. *@par Outputs:
  150. *y: The dequantized output tensor of type int16 and with format NC1HWC0 . \n
  151. *@par Third-party framework compatibility
  152. * It is a custom operator. It has no corresponding operator in Caffe.
  153. */
  154. REG_OP(AscendDequantS16)
  155. .INPUT(x0, TensorType({DT_INT32}))
  156. .INPUT(deq_scale, TensorType({DT_UINT64}))
  157. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  158. .OUTPUT(y, TensorType({DT_INT16}))
  159. .ATTR(relu_flag, Bool, false)
  160. .OP_END_FACTORY_REG(AscendDequantS16)
  161. /**
  162. *@brief Requantizes the input . \n
  163. *@par Inputs:
  164. *@li x: An NC1HWC0 tensor of type int32, specifying the input.
  165. *@li req_scale: An NC1HWC0 tensor of type uint64, specifying the scaling ratio . \n
  166. *@par Attributes:
  167. *relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  168. *@par Outputs:
  169. *y: The dequantized output tensor of type int8 and with format NC1HWC0 . \n
  170. *@par Third-party framework compatibility
  171. * It is a custom operator. It has no corresponding operator in Caffe.
  172. */
  173. REG_OP(AscendRequant)
  174. .INPUT(x, TensorType({DT_INT32}))
  175. .INPUT(req_scale, TensorType({DT_UINT64}))
  176. .OUTPUT(y, TensorType({DT_INT8}))
  177. .ATTR(relu_flag, Bool, false)
  178. .OP_END_FACTORY_REG(AscendRequant)
  179. /**
  180. *@brief Requantizes the input of int16 . \n
  181. *@par Inputs:
  182. *@li x0: An NC1HWC0 tensor of type int16, specifying the input.
  183. *@li req_scale: An NC1HWC0 tensor of type uint64, specifying the scaling ratio.
  184. *@li x1: An NC1HWC0 tensor of type int16 . \n
  185. *@par Attributes:
  186. *@li dual_output: A optional bool, specifying whether to perform dual ouput, either "True" or "False". Defaults to "False".
  187. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False" . \n
  188. *@par Outputs:
  189. *@li y0: The dequantized output tensor of type int8 and with format NC1HWC0.
  190. *@li y1: The dequantized output tensor of type int16 and with format NC1HWC0 . \n
  191. *@par Third-party framework compatibility
  192. * It is a custom operator. It has no corresponding operator in Caffe.
  193. */
  194. REG_OP(AscendRequantS16)
  195. .INPUT(x0, TensorType({DT_INT16}))
  196. .INPUT(req_scale, TensorType({DT_UINT64}))
  197. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  198. .OUTPUT(y0, TensorType({DT_INT8}))
  199. .OUTPUT(y1, TensorType({DT_INT16}))
  200. .ATTR(dual_output, Bool, false)
  201. .ATTR(relu_flag, Bool, false)
  202. .OP_END_FACTORY_REG(AscendRequantS16)
  203. } // namespace ge
  204. #endif // OPS_BUILT_IN_OP_PROTO_INC_QUANTIZE_OPS_H_

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