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.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.\n
  22. * [input_min_range, input_max_range] are scalar floats that specify the range
  23. * for "output_data". \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 input_data: A Tensor. Must be one of the following types: int8, uint8,
  28. * int32.
  29. * @li input_min_range: A Tensor of type float32.
  30. * Specifies the minimum scalar value possibly produced for the input.
  31. * @li input_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. * output_data: A dictionary of type float32.
  38. * @attention Constraints:
  39. * @li "input_min_range" and "input_max_range" have the same shapes.
  40. * @li "input_data" and "output_data" have the same shapes.
  41. */
  42. REG_OP(Dequantize)
  43. .INPUT(x, TensorType(DT_QINT8, DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16))
  44. .INPUT(min_range, TensorType{DT_FLOAT})
  45. .INPUT(max_range, TensorType{DT_FLOAT})
  46. .OUTPUT(y, TensorType({DT_FLOAT}))
  47. .ATTR(mode, String, "MIN_COMBINED")
  48. .OP_END_FACTORY_REG(Dequantize)
  49. /**
  50. *@brief Quantizes the input.
  51. *@par Inputs:
  52. *x: An NC1HWC0 tensor of type float16 or float32, specifying the input.
  53. *@par Attributes:
  54. *@li scale: A required float32, specifying the scaling ratio.
  55. *@li offset: A required float16, specifying the offset.
  56. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  57. *@li round_mode: An optional string, specifying the float16 to int8 cast type.
  58. * The value range is [Round, Floor, Ceiling, Truncate]. Defaults to "Round".
  59. *@par Outputs:
  60. *y: The quantized output tensor of type int8 and with format NC1HWC0.
  61. */
  62. REG_OP(AscendQuant)
  63. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  64. .OUTPUT(y, TensorType({DT_INT8}))
  65. .REQUIRED_ATTR(scale, Float)
  66. .REQUIRED_ATTR(offset, Float)
  67. .ATTR(sqrt_mode, Bool, false)
  68. .ATTR(round_mode, String, "Round")
  69. .OP_END_FACTORY_REG(AscendQuant)
  70. /**
  71. *@brief Dequantizes the input.
  72. *@par Inputs:
  73. *@li x: An NC1HWC0 tensor of type int32, specifying the input.
  74. *@li deq_scale: An NC1HWC0 tensor of type float16 or uint64, specifying the scaling ratio.
  75. *@par Attributes:
  76. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  77. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  78. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  79. *@par Outputs:
  80. *y: The dequantized output tensor of type float16 or float32 and with format NC1HWC0.
  81. */
  82. REG_OP(AscendDequant)
  83. .INPUT(x, TensorType({DT_INT32}))
  84. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  85. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  86. .ATTR(sqrt_mode, Bool, false)
  87. .ATTR(relu_flag, Bool, false)
  88. .ATTR(dtype, Int, DT_FLOAT)
  89. .OP_END_FACTORY_REG(AscendDequant)
  90. REG_OP(AscendAntiQuant)
  91. .INPUT(x, TensorType({DT_INT8}))
  92. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  93. .REQUIRED_ATTR(scale, Float)
  94. .REQUIRED_ATTR(offset, Float)
  95. .ATTR(dtype, Int, DT_FLOAT)
  96. .ATTR(sqrt_mode, Bool, false)
  97. .OP_END_FACTORY_REG(AscendAntiQuant)
  98. REG_OP(AscendDequantS16)
  99. .INPUT(x0, TensorType({DT_INT32}))
  100. .INPUT(deq_scale, TensorType({DT_UINT64}))
  101. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  102. .OUTPUT(y, TensorType({DT_INT16}))
  103. .ATTR(relu_flag, Bool, false)
  104. .OP_END_FACTORY_REG(AscendDequantS16)
  105. REG_OP(AscendRequant)
  106. .INPUT(x, TensorType({DT_INT32}))
  107. .INPUT(req_scale, TensorType({DT_UINT64}))
  108. .OUTPUT(y, TensorType({DT_INT8}))
  109. .ATTR(relu_flag, Bool, false)
  110. .OP_END_FACTORY_REG(AscendRequant)
  111. REG_OP(AscendRequantS16)
  112. .INPUT(x, TensorType({DT_INT16}))
  113. .INPUT(req_scale, TensorType({DT_UINT64}))
  114. .OPTIONAL_INPUT(x1, TensorType({DT_INT16}))
  115. .OUTPUT(y, TensorType({DT_INT8}))
  116. .OUTPUT(y1, TensorType({DT_INT16}))
  117. .ATTR(dual_output, Bool, false)
  118. .ATTR(relu_flag, Bool, false)
  119. .OP_END_FACTORY_REG(AscendRequantS16)
  120. } // namespace ge
  121. #endif // GE_OP_QUANTIZE_OPS_H

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