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 5.3 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_QUANTIZE_OPS_H
  17. #define GE_OP_QUANTIZE_OPS_H
  18. #include "graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. * @brief Dequantizes the input tensor into a float tensor.
  22. * [min_range, max_range] are float32 tensors that specify the range
  23. * for "y". \n
  24. * The "mode" attribute controls exactly which calculations are used to convert\n
  25. * the float values to their quantized equivalents.
  26. * @par Inputs:
  27. * @li x: A Tensor. Must be one of the following types: int8, uint8,
  28. * int32.
  29. * @li min_range: A Tensor of type float32.
  30. * Specifies the minimum scalar value possibly produced for the input.
  31. * @li max_range: A Tensor of type float32.
  32. * Specifies the maximum scalar value possibly produced for the input.
  33. * @par Attributes:
  34. * mode: An optional string from: "MIN_COMBINED", "MIN_FIRST", and "SCALED".
  35. * Defaults to "MIN_COMBINED".
  36. * @par Outputs:
  37. * y: A dictionary of type float32.
  38. * @attention Constraints:
  39. * @li "min_range" and "max_range" have the same shapes.
  40. * @li "x" and "y" have the same shapes.
  41. * @par Third-party framework compatibility
  42. * Compatible with the TensorFlow operator Dequantize.
  43. */
  44. REG_OP(Dequantize)
  45. .INPUT(x, TensorType(DT_QINT8, DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16))
  46. .INPUT(min_range, TensorType{DT_FLOAT})
  47. .INPUT(max_range, TensorType{DT_FLOAT})
  48. .OUTPUT(y, TensorType({DT_FLOAT}))
  49. .ATTR(mode, String, "MIN_COMBINED")
  50. .OP_END_FACTORY_REG(Dequantize)
  51. /**
  52. *@brief Quantizes the input.
  53. *@par Inputs:
  54. *x: An NC1HWC0 tensor of type float16 or float32, specifying the input.
  55. *@par Attributes:
  56. *@li scale: A required float32, specifying the scaling ratio.
  57. *@li offset: A required float16, specifying the offset.
  58. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  59. *@li round_mode: An optional string, specifying the float16 to int8 cast type.
  60. * The value range is [Round, Floor, Ceiling, Truncate]. Defaults to "Round".
  61. *@par Outputs:
  62. *y: The quantized output tensor of type int8 and with format NC1HWC0.
  63. *@par Third-party framework compatibility
  64. * It is a custom operator. It has no corresponding operator in Caffe.
  65. */
  66. REG_OP(AscendQuant)
  67. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  68. .OUTPUT(y, TensorType({DT_INT8}))
  69. .REQUIRED_ATTR(scale, Float)
  70. .REQUIRED_ATTR(offset, Float)
  71. .ATTR(sqrt_mode, Bool, false)
  72. .ATTR(round_mode, String, "Round")
  73. .OP_END_FACTORY_REG(AscendQuant)
  74. /**
  75. *@brief Dequantizes the input.
  76. *@par Inputs:
  77. *@li x: An NC1HWC0 tensor of type int32, specifying the input.
  78. *@li deq_scale: An NC1HWC0 tensor of type float16 or uint64, specifying the scaling ratio.
  79. *@par Attributes:
  80. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  81. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  82. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  83. *@par Outputs:
  84. *y: The dequantized output tensor of type float16 or float32 and with format NC1HWC0.
  85. *@par Third-party framework compatibility
  86. * It is a custom operator. It has no corresponding operator in Caffe.
  87. */
  88. REG_OP(AscendDequant)
  89. .INPUT(x, TensorType({DT_INT32}))
  90. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  91. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  92. .ATTR(sqrt_mode, Bool, false)
  93. .ATTR(relu_flag, Bool, false)
  94. .ATTR(dtype, Int, DT_FLOAT)
  95. .OP_END_FACTORY_REG(AscendDequant)
  96. REG_OP(AscendAntiQuant)
  97. .INPUT(x, TensorType({DT_INT8}))
  98. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  99. .REQUIRED_ATTR(scale, Float)
  100. .REQUIRED_ATTR(offset, Float)
  101. .ATTR(dtype, Int, DT_FLOAT)
  102. .ATTR(sqrt_mode, Bool, false)
  103. .OP_END_FACTORY_REG(AscendAntiQuant)
  104. REG_OP(AscendDequantS16)
  105. .INPUT(x0, TensorType({DT_INT32}))
  106. .INPUT(deq_scale, TensorType({DT_UINT64}))
  107. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  108. .OUTPUT(y, TensorType({DT_INT16}))
  109. .ATTR(relu_flag, Bool, false)
  110. .OP_END_FACTORY_REG(AscendDequantS16)
  111. REG_OP(AscendRequant)
  112. .INPUT(x, TensorType({DT_INT32}))
  113. .INPUT(req_scale, TensorType({DT_UINT64}))
  114. .OUTPUT(y, TensorType({DT_INT8}))
  115. .ATTR(relu_flag, Bool, false)
  116. .OP_END_FACTORY_REG(AscendRequant)
  117. REG_OP(AscendRequantS16)
  118. .INPUT(x, TensorType({DT_INT16}))
  119. .INPUT(req_scale, TensorType({DT_UINT64}))
  120. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  121. .OUTPUT(y, TensorType({DT_INT8}))
  122. .OUTPUT(y1, TensorType({DT_INT16}))
  123. .ATTR(dual_output, Bool, false)
  124. .ATTR(relu_flag, Bool, false)
  125. .OP_END_FACTORY_REG(AscendRequantS16)
  126. } // namespace ge
  127. #endif // GE_OP_QUANTIZE_OPS_H

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