blob: 61966624413d757f05a89e2fca879a230949a495 [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Michalis Spyrou299fdd32019-05-01 13:03:59 +01002 * Copyright (c) 2017-2019 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"
Chunosovd621bca2017-11-03 17:33:15 +070026
27#include <cmath>
28#include <limits>
29#include <numeric>
30
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010031namespace arm_compute
32{
33namespace quantization
34{
Michalis Spyrou299fdd32019-05-01 13:03:59 +010035constexpr int64_t fixed_point_one_Q0 = (1LL << 31);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000036constexpr float epsilon = 0.00001f;
Chunosovf450caa2017-11-08 16:09:35 +070037
Manuel Bottini07263982019-10-17 18:37:26 +010038Status calculate_quantized_multiplier(float multiplier, int *quant_multiplier, int *shift)
39{
Michele Di Giorgio35c37942019-12-03 19:34:30 +000040 if(multiplier >= 1.f)
Manuel Bottini07263982019-10-17 18:37:26 +010041 {
42 Status status = calculate_quantized_multiplier_greater_than_one(multiplier, quant_multiplier, shift);
43 *shift *= -1;
44 return status;
45 }
46 else
47 {
48 return calculate_quantized_multiplier_less_than_one(multiplier, quant_multiplier, shift);
49 }
50}
51
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010052Status calculate_quantized_multiplier_less_than_one(float multiplier,
53 int *quant_multiplier,
54 int *right_shift)
Chunosovd621bca2017-11-03 17:33:15 +070055{
56 ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr);
57 ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000058 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon);
59 ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000060 if(std::fabs(0.0f - multiplier) < epsilon)
Chunosovd621bca2017-11-03 17:33:15 +070061 {
62 *quant_multiplier = 0;
63 *right_shift = 0;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010064 return Status{};
Chunosovd621bca2017-11-03 17:33:15 +070065 }
Gian Marco Iodice3139f032018-11-05 14:26:32 +000066
Chunosovd621bca2017-11-03 17:33:15 +070067 const double q = std::frexp(multiplier, right_shift);
68 *right_shift *= -1;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010069 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070070 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
71 if(q_fixed == fixed_point_one_Q0)
Chunosovd621bca2017-11-03 17:33:15 +070072 {
73 q_fixed /= 2;
74 --*right_shift;
75 }
76 ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0);
77 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
Chunosovf450caa2017-11-08 16:09:35 +070078 *quant_multiplier = static_cast<int32_t>(q_fixed);
Chunosovd621bca2017-11-03 17:33:15 +070079
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010080 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +070081}
82
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010083Status calculate_quantized_multiplier_greater_than_one(float multiplier,
84 int *quantized_multiplier,
85 int *left_shift)
Chunosovf450caa2017-11-08 16:09:35 +070086{
87 ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr);
88 ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr);
89 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f);
90 const double q = std::frexp(multiplier, left_shift);
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010091 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070092 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
93 if(q_fixed == fixed_point_one_Q0)
94 {
95 q_fixed /= 2;
96 ++*left_shift;
97 }
98 ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0);
99 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
100 *quantized_multiplier = static_cast<int32_t>(q_fixed);
101
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100102 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +0700103}
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100104
105arm_compute::Status calculate_quantized_multipliers_less_than_one(const QuantizationInfo &iq_info,
106 const QuantizationInfo &wq_info,
107 const QuantizationInfo &oq_info,
108 GEMMLowpOutputStageInfo &stage_info)
109{
110 ARM_COMPUTE_RETURN_ERROR_ON(iq_info.scale().empty());
111 ARM_COMPUTE_RETURN_ERROR_ON(wq_info.scale().empty());
112 ARM_COMPUTE_RETURN_ERROR_ON(oq_info.scale().empty());
113
114 const unsigned int size = wq_info.scale().size();
115
116 auto &quant_multipliers = stage_info.gemmlowp_multipliers;
117 auto &quant_shifts = stage_info.gemmlowp_shifts;
118 quant_multipliers.resize(size);
119 quant_shifts.resize(size);
120
121 const auto &w_scales = wq_info.scale();
122 const float i_scale = iq_info.scale().at(0);
123 const float o_scale = oq_info.scale().at(0);
124
125 for(unsigned int i = 0; i < size; ++i)
126 {
127 const float multiplier = i_scale * w_scales[i] / o_scale;
128 int quant_multiplier = 0;
129 int quant_shift = 0;
130 ARM_COMPUTE_RETURN_ON_ERROR(calculate_quantized_multiplier_less_than_one(multiplier, &quant_multiplier, &quant_shift));
131 quant_multipliers[i] = quant_multiplier;
132 quant_shifts[i] = quant_shift;
133 }
134
135 // Legacy part
136 stage_info.gemmlowp_shift = quant_shifts[0];
137 stage_info.gemmlowp_multiplier = quant_multipliers[0];
138
139 return Status{};
140}
141
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100142std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type)
143{
144 int min_quant_val = 0;
145 int max_quant_val = 0;
146 switch(data_type)
147 {
148 case DataType::QASYMM8:
149 min_quant_val = std::numeric_limits<uint8_t>::min();
150 max_quant_val = std::numeric_limits<uint8_t>::max();
151 break;
152 case DataType::QSYMM8:
153 min_quant_val = std::numeric_limits<int8_t>::min();
154 max_quant_val = std::numeric_limits<int8_t>::max();
155 break;
156 case DataType::QASYMM16:
157 min_quant_val = std::numeric_limits<uint16_t>::min();
158 max_quant_val = std::numeric_limits<uint16_t>::max();
159 break;
160 case DataType::QSYMM16:
161 min_quant_val = std::numeric_limits<int16_t>::min();
162 max_quant_val = std::numeric_limits<int16_t>::max();
163 break;
164 default:
165 ARM_COMPUTE_ERROR("Unsupported data type");
166 }
167 return std::make_pair(min_quant_val, max_quant_val);
168}
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000169void compute_quantized_multipliers_and_shifts(const ITensorInfo *input,
170 const ITensorInfo *weights,
171 const ITensorInfo *output,
172 unsigned int idx_ofms,
173 int32_t *output_multipliers_ptr,
174 int32_t *output_shifts_ptr)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100175{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000176 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 +0100177
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000178 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
179 const QuantizationInfo wq_info = weights->quantization_info();
180 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100181
182 for(unsigned int i = 0; i < num_filters; ++i)
183 {
184 int output_multiplier = 0;
185 int output_shift = 0;
186 const float multiplier = iq_info.scale * wq_info.scale()[i] / oq_info.scale;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100187 calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100188
189 output_multipliers_ptr[i] = output_multiplier;
190 output_shifts_ptr[i] = output_shift;
191 }
192}
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100193} // quantization
194} // arm_compute