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 | |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 38 | Status calculate_quantized_multiplier(float multiplier, int *quant_multiplier, int *shift) |
| 39 | { |
| 40 | if(multiplier > 1.f) |
| 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 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 52 | Status calculate_quantized_multiplier_less_than_one(float multiplier, |
| 53 | int *quant_multiplier, |
| 54 | int *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); |
| 60 | if(std::fabs(1.0f - multiplier) < epsilon) |
| 61 | { |
| 62 | *quant_multiplier = 1; |
| 63 | *right_shift = 0; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 64 | return Status{}; |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if(std::fabs(0.0f - multiplier) < epsilon) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 68 | { |
| 69 | *quant_multiplier = 0; |
| 70 | *right_shift = 0; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 71 | return Status{}; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 72 | } |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 73 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 74 | const double q = std::frexp(multiplier, right_shift); |
| 75 | *right_shift *= -1; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 76 | 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] | 77 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 78 | if(q_fixed == fixed_point_one_Q0) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 79 | { |
| 80 | q_fixed /= 2; |
| 81 | --*right_shift; |
| 82 | } |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 85 | *quant_multiplier = static_cast<int32_t>(q_fixed); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 86 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 87 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 88 | } |
| 89 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 90 | Status calculate_quantized_multiplier_greater_than_one(float multiplier, |
| 91 | int *quantized_multiplier, |
| 92 | int *left_shift) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f); |
| 97 | const double q = std::frexp(multiplier, left_shift); |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 98 | 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] | 99 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 100 | if(q_fixed == fixed_point_one_Q0) |
| 101 | { |
| 102 | q_fixed /= 2; |
| 103 | ++*left_shift; |
| 104 | } |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
| 107 | *quantized_multiplier = static_cast<int32_t>(q_fixed); |
| 108 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 109 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 110 | } |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 111 | std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type) |
| 112 | { |
| 113 | int min_quant_val = 0; |
| 114 | int max_quant_val = 0; |
| 115 | switch(data_type) |
| 116 | { |
| 117 | case DataType::QASYMM8: |
| 118 | min_quant_val = std::numeric_limits<uint8_t>::min(); |
| 119 | max_quant_val = std::numeric_limits<uint8_t>::max(); |
| 120 | break; |
| 121 | case DataType::QSYMM8: |
| 122 | min_quant_val = std::numeric_limits<int8_t>::min(); |
| 123 | max_quant_val = std::numeric_limits<int8_t>::max(); |
| 124 | break; |
| 125 | case DataType::QASYMM16: |
| 126 | min_quant_val = std::numeric_limits<uint16_t>::min(); |
| 127 | max_quant_val = std::numeric_limits<uint16_t>::max(); |
| 128 | break; |
| 129 | case DataType::QSYMM16: |
| 130 | min_quant_val = std::numeric_limits<int16_t>::min(); |
| 131 | max_quant_val = std::numeric_limits<int16_t>::max(); |
| 132 | break; |
| 133 | default: |
| 134 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 135 | } |
| 136 | return std::make_pair(min_quant_val, max_quant_val); |
| 137 | } |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 138 | void compute_quantized_multipliers_and_shifts(const ITensor *input, const ITensor *weights, const ITensor *output, int32_t *output_multipliers_ptr, int32_t *output_shifts_ptr) |
| 139 | { |
| 140 | const unsigned int idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL); |
| 141 | const unsigned int num_filters = is_data_type_quantized_per_channel(weights->info()->data_type()) ? weights->info()->dimension(idx_c) : 1; |
| 142 | |
| 143 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
| 144 | const QuantizationInfo wq_info = weights->info()->quantization_info(); |
| 145 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 146 | |
| 147 | for(unsigned int i = 0; i < num_filters; ++i) |
| 148 | { |
| 149 | int output_multiplier = 0; |
| 150 | int output_shift = 0; |
| 151 | const float multiplier = iq_info.scale * wq_info.scale()[i] / oq_info.scale; |
| 152 | ARM_COMPUTE_ERROR_ON(multiplier > 1.0f); |
| 153 | calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 154 | |
| 155 | output_multipliers_ptr[i] = output_multiplier; |
| 156 | output_shifts_ptr[i] = output_shift; |
| 157 | } |
| 158 | } |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 159 | } // quantization |
| 160 | } // arm_compute |