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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. REG_OP(QuantizedInnerProduct)
  21. .INPUT(x, TensorType({DT_UINT8}))
  22. .INPUT(w, TensorType({DT_INT8}))
  23. .OPTIONAL_INPUT(b, TensorType({DT_INT32}))
  24. .OPTIONAL_INPUT(scale_q, TensorType({DT_FLOAT16}))
  25. .OPTIONAL_INPUT(offset_q, TensorType({DT_FLOAT16}))
  26. .OPTIONAL_INPUT(scale_deq_req, TensorType({DT_FLOAT16}))
  27. .OPTIONAL_INPUT(offset_req, TensorType({DT_FLOAT16}))
  28. .OUTPUT(y, TensorType({DT_FLOAT16}))
  29. .REQUIRED_ATTR(quant_algo, ListInt)
  30. .REQUIRED_ATTR(scale_sqrt, ListInt)
  31. .REQUIRED_ATTR(num_output, Int)
  32. .ATTR(transpose, Bool, false)
  33. .ATTR(bias_term, Bool, false)
  34. .ATTR(axis, Int, 1)
  35. .OP_END_FACTORY_REG(QuantizedInnerProduct)
  36. /**
  37. * @brief Dequantizes the input tensor into a float tensor.\n
  38. * [input_min_range, input_max_range] are scalar floats that specify the range
  39. * for "output_data". \n
  40. * The "mode" attribute controls exactly which calculations are used to convert\n
  41. * the float values to their quantized equivalents.
  42. * @par Inputs:
  43. * @li input_data: A Tensor. Must be one of the following types: int8, uint8,
  44. * int32.
  45. * @li input_min_range: A Tensor of type float32.
  46. * Specifies the minimum scalar value possibly produced for the input.
  47. * @li input_max_range: A Tensor of type float32.
  48. * Specifies the maximum scalar value possibly produced for the input.
  49. * @par Attributes:
  50. * mode: An optional string from: "MIN_COMBINED", "MIN_FIRST", and "SCALED".
  51. * Defaults to "MIN_COMBINED".
  52. * @par Outputs:
  53. * output_data: A dictionary of type float32.
  54. * @attention Constraints:
  55. * @li "input_min_range" and "input_max_range" have the same shapes.
  56. * @li "input_data" and "output_data" have the same shapes.
  57. */
  58. REG_OP(Dequantize)
  59. .INPUT(x, TensorType(DT_QINT8, DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16))
  60. .INPUT(min_range, TensorType{DT_FLOAT})
  61. .INPUT(max_range, TensorType{DT_FLOAT})
  62. .OUTPUT(y, TensorType({DT_FLOAT}))
  63. .ATTR(mode, String, "MIN_COMBINED")
  64. .OP_END_FACTORY_REG(Dequantize)
  65. /**
  66. *@brief Quantizes the input.
  67. *@par Inputs:
  68. *x: An NC1HWC0 tensor of type float16 or float32, specifying the input.
  69. *@par Attributes:
  70. *@li scale: A required float32, specifying the scaling ratio.
  71. *@li offset: A required float16, specifying the offset.
  72. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  73. *@li round_mode: An optional string, specifying the float16 to int8 cast type.
  74. * The value range is [Round, Floor, Ceiling, Truncate]. Defaults to "Round".
  75. *@par Outputs:
  76. *y: The quantized output tensor of type int8 and with format NC1HWC0.
  77. */
  78. REG_OP(AscendQuant)
  79. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT32}))
  80. .OUTPUT(y, TensorType({DT_INT8}))
  81. .REQUIRED_ATTR(scale, Float)
  82. .REQUIRED_ATTR(offset, Float)
  83. .ATTR(sqrt_mode, Bool, false)
  84. .ATTR(round_mode, String, "Round")
  85. .OP_END_FACTORY_REG(AscendQuant)
  86. /**
  87. *@brief Dequantizes the input.
  88. *@par Inputs:
  89. *@li x: An NC1HWC0 tensor of type int32, specifying the input.
  90. *@li deq_scale: An NC1HWC0 tensor of type float16 or uint64, specifying the scaling ratio.
  91. *@par Attributes:
  92. *@li sqrt_mode: A optional bool, specifying whether to perform square root on "scale", either "True" or "False". Defaults to "False".
  93. *@li relu_flag: A optional bool, specifying whether to perform ReLU, either "True" or "False". Defaults to "False".
  94. *@li dtype: A optional int32, specifying the output data type. Defaults to "DT_FLOAT".
  95. *@par Outputs:
  96. *y: The dequantized output tensor of type float16 or float32 and with format NC1HWC0.
  97. */
  98. REG_OP(AscendDequant)
  99. .INPUT(x, TensorType({DT_INT32}))
  100. .INPUT(deq_scale, TensorType({DT_FLOAT16, DT_UINT64}))
  101. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  102. .ATTR(sqrt_mode, Bool, false)
  103. .ATTR(relu_flag, Bool, false)
  104. .ATTR(dtype, Int, DT_FLOAT)
  105. .OP_END_FACTORY_REG(AscendDequant)
  106. } // namespace ge
  107. #endif // GE_OP_QUANTIZE_OPS_H

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