blob: c5eef9dd77990e99a4ec1ffd6860e7b689bd2938 [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2017-2020 ARM Limited.
Chunosovd621bca2017-11-03 17:33:15 +07003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010025#include "arm_compute/core/Helpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000026#include "support/ToolchainSupport.h"
Chunosovd621bca2017-11-03 17:33:15 +070027
28#include <cmath>
29#include <limits>
30#include <numeric>
31
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010032namespace arm_compute
33{
34namespace quantization
35{
Michalis Spyrou299fdd32019-05-01 13:03:59 +010036constexpr int64_t fixed_point_one_Q0 = (1LL << 31);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000037constexpr float epsilon = 0.00001f;
Chunosovf450caa2017-11-08 16:09:35 +070038
Michalis Spyroue7be8a02019-12-12 16:16:09 +000039Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift)
Manuel Bottini07263982019-10-17 18:37:26 +010040{
Michele Di Giorgio35c37942019-12-03 19:34:30 +000041 if(multiplier >= 1.f)
Manuel Bottini07263982019-10-17 18:37:26 +010042 {
43 Status status = calculate_quantized_multiplier_greater_than_one(multiplier, quant_multiplier, shift);
44 *shift *= -1;
45 return status;
46 }
47 else
48 {
49 return calculate_quantized_multiplier_less_than_one(multiplier, quant_multiplier, shift);
50 }
51}
52
Michalis Spyroue7be8a02019-12-12 16:16:09 +000053Status calculate_quantized_multiplier_less_than_one(float multiplier,
54 int32_t *quant_multiplier,
55 int32_t *right_shift)
Chunosovd621bca2017-11-03 17:33:15 +070056{
57 ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr);
58 ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000059 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon);
60 ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000061 if(std::fabs(0.0f - multiplier) < epsilon)
Chunosovd621bca2017-11-03 17:33:15 +070062 {
63 *quant_multiplier = 0;
64 *right_shift = 0;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010065 return Status{};
Chunosovd621bca2017-11-03 17:33:15 +070066 }
Gian Marco Iodice3139f032018-11-05 14:26:32 +000067
Michalis Spyroue7be8a02019-12-12 16:16:09 +000068 int shift_exp = 0;
69 const double q = std::frexp(multiplier, &shift_exp);
70 *right_shift = -1 * shift_exp;
71 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070072 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
73 if(q_fixed == fixed_point_one_Q0)
Chunosovd621bca2017-11-03 17:33:15 +070074 {
75 q_fixed /= 2;
76 --*right_shift;
77 }
78 ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0);
79 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
Chunosovf450caa2017-11-08 16:09:35 +070080 *quant_multiplier = static_cast<int32_t>(q_fixed);
Chunosovd621bca2017-11-03 17:33:15 +070081
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010082 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +070083}
84
Michalis Spyroue7be8a02019-12-12 16:16:09 +000085Status calculate_quantized_multiplier_greater_than_one(float multiplier,
86 int32_t *quantized_multiplier,
87 int32_t *left_shift)
Chunosovf450caa2017-11-08 16:09:35 +070088{
89 ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr);
90 ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr);
91 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f);
Michalis Spyroue7be8a02019-12-12 16:16:09 +000092
93 int shift_exp = 0;
94 const double q = std::frexp(multiplier, &shift_exp);
95 *left_shift = shift_exp;
96 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070097 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
98 if(q_fixed == fixed_point_one_Q0)
99 {
100 q_fixed /= 2;
101 ++*left_shift;
102 }
103 ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0);
104 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
105 *quantized_multiplier = static_cast<int32_t>(q_fixed);
106
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100107 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +0700108}
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100109
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000110arm_compute::Status calculate_quantized_multipliers(const QuantizationInfo &iq_info,
111 const QuantizationInfo &wq_info,
112 const QuantizationInfo &oq_info,
113 GEMMLowpOutputStageInfo &stage_info)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100114{
115 ARM_COMPUTE_RETURN_ERROR_ON(iq_info.scale().empty());
116 ARM_COMPUTE_RETURN_ERROR_ON(wq_info.scale().empty());
117 ARM_COMPUTE_RETURN_ERROR_ON(oq_info.scale().empty());
118
119 const unsigned int size = wq_info.scale().size();
120
121 auto &quant_multipliers = stage_info.gemmlowp_multipliers;
122 auto &quant_shifts = stage_info.gemmlowp_shifts;
123 quant_multipliers.resize(size);
124 quant_shifts.resize(size);
125
126 const auto &w_scales = wq_info.scale();
127 const float i_scale = iq_info.scale().at(0);
128 const float o_scale = oq_info.scale().at(0);
129
130 for(unsigned int i = 0; i < size; ++i)
131 {
132 const float multiplier = i_scale * w_scales[i] / o_scale;
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000133 int32_t quant_multiplier = 0;
134 int32_t quant_shift = 0;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000135 ARM_COMPUTE_RETURN_ON_ERROR(calculate_quantized_multiplier(multiplier, &quant_multiplier, &quant_shift));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100136 quant_multipliers[i] = quant_multiplier;
137 quant_shifts[i] = quant_shift;
138 }
139
140 // Legacy part
141 stage_info.gemmlowp_shift = quant_shifts[0];
142 stage_info.gemmlowp_multiplier = quant_multipliers[0];
143
144 return Status{};
145}
146
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100147std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type)
148{
149 int min_quant_val = 0;
150 int max_quant_val = 0;
151 switch(data_type)
152 {
153 case DataType::QASYMM8:
154 min_quant_val = std::numeric_limits<uint8_t>::min();
155 max_quant_val = std::numeric_limits<uint8_t>::max();
156 break;
157 case DataType::QSYMM8:
Manuel Bottini8481d832019-12-10 15:28:40 +0000158 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100159 min_quant_val = std::numeric_limits<int8_t>::min();
160 max_quant_val = std::numeric_limits<int8_t>::max();
161 break;
162 case DataType::QASYMM16:
163 min_quant_val = std::numeric_limits<uint16_t>::min();
164 max_quant_val = std::numeric_limits<uint16_t>::max();
165 break;
166 case DataType::QSYMM16:
167 min_quant_val = std::numeric_limits<int16_t>::min();
168 max_quant_val = std::numeric_limits<int16_t>::max();
169 break;
170 default:
171 ARM_COMPUTE_ERROR("Unsupported data type");
172 }
173 return std::make_pair(min_quant_val, max_quant_val);
174}
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000175void compute_quantized_multipliers_and_shifts(const ITensorInfo *input,
176 const ITensorInfo *weights,
177 const ITensorInfo *output,
178 unsigned int idx_ofms,
179 int32_t *output_multipliers_ptr,
180 int32_t *output_shifts_ptr)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100181{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000182 const unsigned int num_filters = is_data_type_quantized_per_channel(weights->data_type()) ? weights->dimension(idx_ofms) : 1;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100183
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000184 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
185 const QuantizationInfo wq_info = weights->quantization_info();
186 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100187
188 for(unsigned int i = 0; i < num_filters; ++i)
189 {
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000190 int32_t output_multiplier = 0;
191 int32_t output_shift = 0;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100192 const float multiplier = iq_info.scale * wq_info.scale()[i] / oq_info.scale;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100193 calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100194
195 output_multipliers_ptr[i] = output_multiplier;
196 output_shifts_ptr[i] = output_shift;
197 }
198}
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000199
200int32_t saturating_rounding_doubling_highmul(int32_t a, int32_t b)
201{
202 bool overflow = a == b && a == std::numeric_limits<int32_t>::min();
203 int64_t a_64(a);
204 int64_t b_64(b);
205 int64_t ab_64 = a_64 * b_64;
206 int32_t nudge = ab_64 >= 0 ? (1 << 30) : (1 - (1 << 30));
207 int32_t ab_x2_high32 = static_cast<int32_t>((ab_64 + nudge) / (1ll << 31));
208 return overflow ? std::numeric_limits<int32_t>::max() : ab_x2_high32;
209}
210
211inline int32_t rounding_divide_by_pow2(int32_t x, int exponent)
212{
213 const int32_t mask = (1 << exponent) - 1;
214 const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0);
215 return (x >> exponent) + ((x & mask) > threshold ? 1 : 0);
216}
217
218int32_t multiply_by_quantized_multipler(int32_t input, int32_t qmul, int32_t shift)
219{
220 const auto left_shift = shift > 0 ? shift : 0;
221 const auto right_shift = shift > 0 ? 0 : -shift;
222 return rounding_divide_by_pow2(saturating_rounding_doubling_highmul(input * (1 << left_shift), qmul), right_shift);
223}
224
225int32_t saturating_rounding_multiply_by_pow2(int32_t exponent, int32_t v)
226{
227 if(exponent == 0)
228 {
229 return v;
230 }
231 else if(exponent < 0)
232 {
233 return rounding_divide_by_pow2(v, -exponent);
234 }
235 else
236 {
237 constexpr auto min = std::numeric_limits<int32_t>::min();
238 constexpr auto max = std::numeric_limits<int32_t>::max();
239 const auto width = sizeof(int32_t) * 8;
240
241 const int32_t threshold = ((1 << (width - 1 - exponent)) - 1);
242 bool pos_mask = v > threshold;
243 bool neg_mask = v < -threshold;
244 int32_t result = v << exponent;
245 result = pos_mask ? max : result;
246 result = neg_mask ? min : result;
247 return result;
248 }
249}
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100250} // quantization
251} // arm_compute