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