Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 3 | * |
| 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 Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/Helpers.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 26 | |
| 27 | #include <cmath> |
| 28 | #include <limits> |
| 29 | #include <numeric> |
| 30 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace quantization |
| 34 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 35 | constexpr int64_t fixed_point_one_Q0 = (1LL << 31); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 36 | constexpr float epsilon = 0.00001f; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 37 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 38 | Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 39 | { |
Michele Di Giorgio | 35c3794 | 2019-12-03 19:34:30 +0000 | [diff] [blame] | 40 | if(multiplier >= 1.f) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 41 | { |
| 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 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 52 | Status calculate_quantized_multiplier_less_than_one(float multiplier, |
| 53 | int32_t *quant_multiplier, |
| 54 | int32_t *right_shift) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 60 | if(std::fabs(0.0f - multiplier) < epsilon) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 61 | { |
| 62 | *quant_multiplier = 0; |
| 63 | *right_shift = 0; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 64 | return Status{}; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 65 | } |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 66 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 67 | int shift_exp = 0; |
| 68 | const double q = std::frexp(multiplier, &shift_exp); |
| 69 | *right_shift = -1 * shift_exp; |
| 70 | auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 72 | if(q_fixed == fixed_point_one_Q0) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 73 | { |
| 74 | q_fixed /= 2; |
| 75 | --*right_shift; |
| 76 | } |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 79 | *quant_multiplier = static_cast<int32_t>(q_fixed); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 80 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 81 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 82 | } |
| 83 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 84 | Status calculate_quantized_multiplier_greater_than_one(float multiplier, |
| 85 | int32_t *quantized_multiplier, |
| 86 | int32_t *left_shift) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f); |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 91 | |
| 92 | int shift_exp = 0; |
| 93 | const double q = std::frexp(multiplier, &shift_exp); |
| 94 | *left_shift = shift_exp; |
| 95 | auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 96 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 97 | if(q_fixed == fixed_point_one_Q0) |
| 98 | { |
| 99 | q_fixed /= 2; |
| 100 | ++*left_shift; |
| 101 | } |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
| 104 | *quantized_multiplier = static_cast<int32_t>(q_fixed); |
| 105 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 106 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 107 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 108 | |
| 109 | arm_compute::Status calculate_quantized_multipliers_less_than_one(const QuantizationInfo &iq_info, |
| 110 | const QuantizationInfo &wq_info, |
| 111 | const QuantizationInfo &oq_info, |
| 112 | GEMMLowpOutputStageInfo &stage_info) |
| 113 | { |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON(iq_info.scale().empty()); |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON(wq_info.scale().empty()); |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON(oq_info.scale().empty()); |
| 117 | |
| 118 | const unsigned int size = wq_info.scale().size(); |
| 119 | |
| 120 | auto &quant_multipliers = stage_info.gemmlowp_multipliers; |
| 121 | auto &quant_shifts = stage_info.gemmlowp_shifts; |
| 122 | quant_multipliers.resize(size); |
| 123 | quant_shifts.resize(size); |
| 124 | |
| 125 | const auto &w_scales = wq_info.scale(); |
| 126 | const float i_scale = iq_info.scale().at(0); |
| 127 | const float o_scale = oq_info.scale().at(0); |
| 128 | |
| 129 | for(unsigned int i = 0; i < size; ++i) |
| 130 | { |
| 131 | const float multiplier = i_scale * w_scales[i] / o_scale; |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 132 | int32_t quant_multiplier = 0; |
| 133 | int32_t quant_shift = 0; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 134 | ARM_COMPUTE_RETURN_ON_ERROR(calculate_quantized_multiplier_less_than_one(multiplier, &quant_multiplier, &quant_shift)); |
| 135 | quant_multipliers[i] = quant_multiplier; |
| 136 | quant_shifts[i] = quant_shift; |
| 137 | } |
| 138 | |
| 139 | // Legacy part |
| 140 | stage_info.gemmlowp_shift = quant_shifts[0]; |
| 141 | stage_info.gemmlowp_multiplier = quant_multipliers[0]; |
| 142 | |
| 143 | return Status{}; |
| 144 | } |
| 145 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 146 | std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type) |
| 147 | { |
| 148 | int min_quant_val = 0; |
| 149 | int max_quant_val = 0; |
| 150 | switch(data_type) |
| 151 | { |
| 152 | case DataType::QASYMM8: |
| 153 | min_quant_val = std::numeric_limits<uint8_t>::min(); |
| 154 | max_quant_val = std::numeric_limits<uint8_t>::max(); |
| 155 | break; |
| 156 | case DataType::QSYMM8: |
| 157 | min_quant_val = std::numeric_limits<int8_t>::min(); |
| 158 | max_quant_val = std::numeric_limits<int8_t>::max(); |
| 159 | break; |
| 160 | case DataType::QASYMM16: |
| 161 | min_quant_val = std::numeric_limits<uint16_t>::min(); |
| 162 | max_quant_val = std::numeric_limits<uint16_t>::max(); |
| 163 | break; |
| 164 | case DataType::QSYMM16: |
| 165 | min_quant_val = std::numeric_limits<int16_t>::min(); |
| 166 | max_quant_val = std::numeric_limits<int16_t>::max(); |
| 167 | break; |
| 168 | default: |
| 169 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 170 | } |
| 171 | return std::make_pair(min_quant_val, max_quant_val); |
| 172 | } |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 173 | void compute_quantized_multipliers_and_shifts(const ITensorInfo *input, |
| 174 | const ITensorInfo *weights, |
| 175 | const ITensorInfo *output, |
| 176 | unsigned int idx_ofms, |
| 177 | int32_t *output_multipliers_ptr, |
| 178 | int32_t *output_shifts_ptr) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 179 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 180 | const unsigned int num_filters = is_data_type_quantized_per_channel(weights->data_type()) ? weights->dimension(idx_ofms) : 1; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 181 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 182 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 183 | const QuantizationInfo wq_info = weights->quantization_info(); |
| 184 | const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 185 | |
| 186 | for(unsigned int i = 0; i < num_filters; ++i) |
| 187 | { |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 188 | int32_t output_multiplier = 0; |
| 189 | int32_t output_shift = 0; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 190 | const float multiplier = iq_info.scale * wq_info.scale()[i] / oq_info.scale; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 191 | calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 192 | |
| 193 | output_multipliers_ptr[i] = output_multiplier; |
| 194 | output_shifts_ptr[i] = output_shift; |
| 195 | } |
| 196 | } |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 197 | } // quantization |
| 198 | } // arm_compute |