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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "nnacl/quantization/quantize.h"
  17. const uint64_t dSignMask = 1ull << 63;
  18. const uint64_t dExponentMask = 0x7ffull << 52;
  19. const uint64_t dFractionMask = (1ull << 52) - 1;
  20. const int dExponentBias = 1022;
  21. const int dMantissaBits = 52;
  22. const int dInfiniteExponent = 0x7ff;
  23. const double dNormalizer = 0x1p54;
  24. const int dNormalizerBias = 54;
  25. const int iMantissaBits = 31;
  26. void QuantizeMultiplierSmallerThanOne(double double_multiplier, int32_t *quantized_multiplier, int *right_shift) {
  27. if (quantized_multiplier == NULL || right_shift == NULL) {
  28. return;
  29. }
  30. int shift = 0;
  31. QuantizeMultiplier(double_multiplier, quantized_multiplier, &shift);
  32. *right_shift = -shift;
  33. }
  34. void QuantizeRoundParameter(double double_multiplier, int32_t *quantized_multiplier, int *left_shift,
  35. int *right_shift) {
  36. int shift = 0;
  37. QuantizeMultiplierSmallerThanOne(double_multiplier, quantized_multiplier, &shift);
  38. shift = -shift;
  39. if (shift < 0) {
  40. *left_shift = 0;
  41. *right_shift = shift;
  42. } else {
  43. *left_shift = shift;
  44. *right_shift = 0;
  45. }
  46. }
  47. uint8_t QuantizeToUint8(float real_value, float scale, int32_t zp) { return round(real_value / scale + zp); }
  48. int32_t QuantizeToInt8(float real_value, float scale, int32_t zp) { return round(real_value / scale + zp); }
  49. void CalculateActivationRangeQuantized(bool is_relu, bool is_relu6, int32_t zp, float scale, int *mini, int *maxi) {
  50. int32_t min = INT8_MIN;
  51. int32_t max = INT8_MAX;
  52. int32_t quantized_zero = QuantizeToInt8(0, scale, zp);
  53. int32_t quantized_six = QuantizeToInt8(6, scale, zp);
  54. if (is_relu) {
  55. min = min > quantized_zero ? min : quantized_zero;
  56. } else if (is_relu6) {
  57. min = min > quantized_zero ? min : quantized_zero;
  58. max = max < quantized_six ? max : quantized_six;
  59. } else {
  60. // do nothing
  61. }
  62. *mini = min;
  63. *maxi = max;
  64. }
  65. // quantize from float to int8
  66. void Quantize(float *input_data, int length, float scale, int zero_point, int8_t *output_data) {
  67. for (int i = 0; i < length; ++i) {
  68. int q = (int)round(input_data[i] / scale + zero_point);
  69. q = q > SCHAR_MAX ? SCHAR_MAX : q;
  70. q = q < SCHAR_MIN ? SCHAR_MIN : q;
  71. output_data[i] = (int8_t)q;
  72. }
  73. }
  74. // dequantize from int8 to float
  75. void Dequantize(int8_t *input_data, int length, float scale, int zero_point, float *output_data) {
  76. for (int i = 0; i < length; ++i) {
  77. output_data[i] = scale * (input_data[i] - zero_point);
  78. }
  79. }
  80. void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift) {
  81. if (quantized_multiplier == NULL || shift == NULL) {
  82. return;
  83. }
  84. // we split a floating number into two parts: exponent and fraction
  85. // since fraction is stored as int32, only 31 bits of mantissa is remained
  86. union {
  87. double d;
  88. uint64_t ul;
  89. } dul;
  90. dul.d = double_multiplier;
  91. if (!(dul.ul & (~dSignMask))) {
  92. // multiplier is 0
  93. *quantized_multiplier = 0;
  94. *shift = 0;
  95. return;
  96. }
  97. int exponent = (int)((dul.ul & dExponentMask) >> dMantissaBits);
  98. if (exponent == dInfiniteExponent) {
  99. // multiplier is inf or NaN
  100. *shift = 0;
  101. if (!(dul.ul & dFractionMask)) {
  102. // inf
  103. *quantized_multiplier = (dul.ul & dSignMask) ? INT_MIN : INT_MAX;
  104. } else {
  105. // NaN
  106. *quantized_multiplier = 0;
  107. }
  108. return;
  109. }
  110. if (exponent == 0) {
  111. // multiplier is a subnormal number
  112. dul.d *= dNormalizer;
  113. exponent = (int)((dul.ul & dExponentMask) >> dMantissaBits);
  114. *shift = exponent - dExponentBias - dNormalizerBias;
  115. } else {
  116. *shift = exponent - dExponentBias;
  117. }
  118. uint64_t fraction = dul.ul & dFractionMask;
  119. fraction += (1ull << dMantissaBits);
  120. uint64_t rounded = ((fraction >> (dMantissaBits - iMantissaBits)) + 1ull) >> 1;
  121. // we get 31 rounded bits now
  122. if (rounded == (1ull << iMantissaBits)) {
  123. // rounding may cause a carry
  124. rounded >>= 1;
  125. ++*shift;
  126. }
  127. *quantized_multiplier = (dul.ul & dSignMask) ? (-(int32_t)(rounded)) : (int32_t)(rounded);
  128. }