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.h 7.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Copyright 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 MINDSPORE_LITE_NNACL_QUANTIZATION_QUANTIZE_H_
  17. #define MINDSPORE_LITE_NNACL_QUANTIZATION_QUANTIZE_H_
  18. #include <math.h>
  19. #include <limits.h>
  20. #include "nnacl/op_base.h"
  21. #define INPUT_ASYMMETRIC 0b001
  22. #define FILTER_ASYMMETRIC 0b010
  23. #define OUTPUT_ASYMMETRIC 0b100
  24. #define INPUT_PER_CHANNEL 0b001
  25. #define FILTER_PER_CHANNEL 0b010
  26. #define OUTPUT_PER_CHANNEL 0b100
  27. typedef struct QuantArg {
  28. float scale_;
  29. int32_t zp_;
  30. } QuantArg;
  31. typedef struct ConvQuantArg {
  32. QuantArg *input_quant_args_;
  33. QuantArg *filter_quant_args_;
  34. QuantArg *output_quant_args_;
  35. double *real_multiplier_;
  36. int32_t *left_shift_;
  37. int32_t *right_shift_;
  38. int32_t *quant_multiplier_;
  39. int32_t *out_act_min_;
  40. int32_t *out_act_max_;
  41. size_t input_arg_num_;
  42. size_t filter_arg_num_;
  43. size_t output_arg_num_;
  44. uint8_t asymmetric_;
  45. uint8_t per_channel_;
  46. } ConvQuantArg;
  47. typedef struct ConcatQuantArg {
  48. QuantArg *in_args_;
  49. QuantArg out_args_;
  50. int8_t output_activation_min_;
  51. int8_t output_activation_max_;
  52. } ConcatQuantArg;
  53. typedef struct SqueezeQuantArg {
  54. int *input_sizes_;
  55. int output_size_;
  56. int **input_shapes_;
  57. int *output_shape_;
  58. float alpha;
  59. int axis_;
  60. size_t input_num_;
  61. size_t output_dim_;
  62. QuantArg *in_quant_args_;
  63. QuantArg out_quant_args_;
  64. } SqueezeQuantArg;
  65. typedef struct UnSqueezeQuantArg {
  66. int *input_sizes_;
  67. int output_size_;
  68. int **input_shapes_;
  69. int *output_shape_;
  70. float alpha;
  71. int axis_;
  72. size_t input_num_;
  73. size_t output_dim_;
  74. QuantArg in_quant_args_;
  75. QuantArg out_quant_args_;
  76. } UnSqueezeQuantArg;
  77. typedef struct PreluQuantArg {
  78. int *input_sizes_;
  79. int output_size_;
  80. int **input_shapes_;
  81. int *output_shape_;
  82. size_t input_num_;
  83. size_t output_dim_;
  84. float alpha_;
  85. QuantArg in_args_;
  86. QuantArg out_args_;
  87. int output_activation_min_;
  88. int output_activation_max_;
  89. QuantArg *in_quant_args_;
  90. QuantArg out_quant_args_;
  91. } PreluQuantArg;
  92. typedef struct MatmulQuantArg {
  93. QuantArg input;
  94. QuantArg weight;
  95. QuantArg output;
  96. int32_t out_act_min;
  97. int32_t out_act_max;
  98. int32_t left_shift;
  99. int32_t right_shift;
  100. int32_t quant_multiplier;
  101. } MatmulQuantArg;
  102. typedef struct PadQuantArg {
  103. QuantArg *in_quant_args_;
  104. QuantArg *out_quanr_args_;
  105. int8_t *constant_value_;
  106. } PadQuantArg;
  107. typedef struct MulQuantArg {
  108. QuantArg in_quant_args_[2];
  109. QuantArg out_quant_arg_;
  110. int output_multiplier_;
  111. int output_activation_min_;
  112. int output_activation_max_;
  113. int shift_left_;
  114. int shift_right_;
  115. } MulQuantArg;
  116. typedef struct CropQuantArg {
  117. QuantArg in_args_;
  118. QuantArg out_args_;
  119. int output_activation_min_;
  120. int output_activation_max_;
  121. } CropQuantArg;
  122. typedef struct ArithSelfQuantArg {
  123. QuantArg in_args_;
  124. QuantArg out_args_;
  125. int output_activation_min_;
  126. int output_activation_max_;
  127. int output_multiplier_;
  128. int shift_left_;
  129. int shift_right_;
  130. } ArithSelfQuantArg;
  131. typedef struct GatherQuantArg {
  132. double alpha_;
  133. int zp_in_;
  134. int zp_out_;
  135. } GatherQuantArg;
  136. typedef struct SplitQuantArg {
  137. QuantArg in_args_;
  138. QuantArg out_args_[20];
  139. int output_activation_min_;
  140. int output_activation_max_;
  141. } SplitQuantArg;
  142. typedef struct SoftmaxQuantArg {
  143. QuantArg in_quant_args_;
  144. QuantArg out_quant_arg_;
  145. int output_activation_min_;
  146. int output_activation_max_;
  147. int output_multiplier_;
  148. int shift_left_;
  149. int shift_right_;
  150. } SoftmaxQuantArg;
  151. typedef struct ReshapeQuantArg {
  152. QuantArg in_args_;
  153. QuantArg out_args_;
  154. int output_activation_min_;
  155. int output_activation_max_;
  156. } ReshapeQuantArg;
  157. typedef struct QuantMulArg {
  158. int32_t multiplier_;
  159. int left_shift_;
  160. int right_shift_;
  161. } QuantMulArg;
  162. typedef struct SubQuantArg {
  163. QuantArg in0_args_;
  164. QuantArg in1_args_;
  165. QuantArg out_args_;
  166. int output_activation_min_;
  167. int output_activation_max_;
  168. int input0_multiplier_;
  169. int input1_multiplier_;
  170. int output_multiplier_;
  171. int input0_shift_;
  172. int input1_shift_;
  173. int output_shift_;
  174. int left_shift_result0_;
  175. int left_shift_result1_;
  176. int right_shift0_;
  177. int right_shift1_;
  178. int left_shift_out_;
  179. int right_shift_out_;
  180. } SubQuantArg;
  181. typedef struct ArithmeticQuantArg {
  182. QuantArg in0_args_;
  183. QuantArg in1_args_;
  184. QuantArg out_args_;
  185. } ArithmeticQuantArg;
  186. typedef struct DivQuantArg {
  187. QuantArg in0_args_;
  188. QuantArg in1_args_;
  189. QuantArg out_args_;
  190. int output_activation_min_;
  191. int output_activation_max_;
  192. int output_multiplier_;
  193. int output_shift_;
  194. } DivQuantArg;
  195. typedef struct ReduceQuantArg {
  196. double in_scale_;
  197. int32_t in_zp_;
  198. double out_scale_;
  199. int32_t out_zp_;
  200. int32_t in_out_multiplier_;
  201. int in_out_left_shift_;
  202. int in_out_right_shift_;
  203. int32_t mean_multiplier_;
  204. int mean_left_shift_;
  205. int mean_right_shift_;
  206. int32_t prod_multiplier_;
  207. int prod_left_shift_;
  208. int prod_right_shift_;
  209. int32_t sum_square_multiplier_;
  210. int sum_square_left_shift_;
  211. int sum_square_right_shift_;
  212. } ReduceQuantArg;
  213. typedef struct SliceQuantArg {
  214. QuantArg in_args_;
  215. QuantArg out_args_;
  216. int output_activation_min_;
  217. int output_activation_max_;
  218. } SliceQuantArg;
  219. typedef struct PowerQuantArg {
  220. QuantArg in_args_;
  221. QuantArg exp_args_;
  222. QuantArg out_args_;
  223. int output_activation_min_;
  224. int output_activation_max_;
  225. } PowerQuantArg;
  226. typedef struct LeakyReluQuantArg {
  227. OpParameter op_parameter_;
  228. PreluQuantArg quant_arg;
  229. float slope_;
  230. int64_t axis_;
  231. const int *in_shape_;
  232. const int *out_shape_;
  233. int input_dim_;
  234. int element_num;
  235. } LeakyReluQuantArg;
  236. #ifdef __cplusplus
  237. extern "C" {
  238. #endif
  239. void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift);
  240. void QuantizeMultiplierSmallerThanOne(double double_multiplier, int32_t *quantized_multiplier, int *right_shift);
  241. void QuantizeRoundParameter(double double_multiplier, int32_t *quantized_multiplier, int *left_shift, int *right_shift);
  242. uint8_t QuantizeToUint8(float real_value, float scale, int32_t zp);
  243. int32_t QuantizeToInt8(float real_value, float scale, int32_t zp);
  244. void CalculateActivationRangeQuantized(bool is_relu, bool is_relu6, int32_t zp, float scale, int *mini, int *maxi);
  245. // quantize from float to int8
  246. void Quantize(float *input_data, int length, float scale, int zero_point, int8_t *output_data);
  247. // dequantize from int8 to float
  248. void Dequantize(int8_t *input_data, int length, float scale, int zero_point, float *output_data);
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif // MINDSPORE_LITE_NNACL_QUANTIZATION_QUANTIZE_H_