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" |
| 25 | |
| 26 | #include <cmath> |
| 27 | #include <limits> |
| 28 | #include <numeric> |
| 29 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace quantization |
| 33 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 34 | constexpr int64_t fixed_point_one_Q0 = (1LL << 31); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 35 | constexpr float epsilon = 0.00001f; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 36 | |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 37 | Status calculate_quantized_multiplier(float multiplier, int *quant_multiplier, int *shift) |
| 38 | { |
| 39 | if(multiplier > 1.f) |
| 40 | { |
| 41 | Status status = calculate_quantized_multiplier_greater_than_one(multiplier, quant_multiplier, shift); |
| 42 | *shift *= -1; |
| 43 | return status; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | return calculate_quantized_multiplier_less_than_one(multiplier, quant_multiplier, shift); |
| 48 | } |
| 49 | } |
| 50 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 51 | Status calculate_quantized_multiplier_less_than_one(float multiplier, |
| 52 | int *quant_multiplier, |
| 53 | int *right_shift) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon); |
| 59 | if(std::fabs(1.0f - multiplier) < epsilon) |
| 60 | { |
| 61 | *quant_multiplier = 1; |
| 62 | *right_shift = 0; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 63 | return Status{}; |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if(std::fabs(0.0f - multiplier) < epsilon) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 67 | { |
| 68 | *quant_multiplier = 0; |
| 69 | *right_shift = 0; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 70 | return Status{}; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 71 | } |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 72 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 73 | const double q = std::frexp(multiplier, right_shift); |
| 74 | *right_shift *= -1; |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 75 | 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] | 76 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 77 | if(q_fixed == fixed_point_one_Q0) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 78 | { |
| 79 | q_fixed /= 2; |
| 80 | --*right_shift; |
| 81 | } |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0); |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 84 | *quant_multiplier = static_cast<int32_t>(q_fixed); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 85 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 86 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 87 | } |
| 88 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 89 | Status calculate_quantized_multiplier_greater_than_one(float multiplier, |
| 90 | int *quantized_multiplier, |
| 91 | int *left_shift) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 92 | { |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f); |
| 96 | const double q = std::frexp(multiplier, left_shift); |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 97 | 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] | 98 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 99 | if(q_fixed == fixed_point_one_Q0) |
| 100 | { |
| 101 | q_fixed /= 2; |
| 102 | ++*left_shift; |
| 103 | } |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
| 106 | *quantized_multiplier = static_cast<int32_t>(q_fixed); |
| 107 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 108 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 109 | } |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 110 | std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type) |
| 111 | { |
| 112 | int min_quant_val = 0; |
| 113 | int max_quant_val = 0; |
| 114 | switch(data_type) |
| 115 | { |
| 116 | case DataType::QASYMM8: |
| 117 | min_quant_val = std::numeric_limits<uint8_t>::min(); |
| 118 | max_quant_val = std::numeric_limits<uint8_t>::max(); |
| 119 | break; |
| 120 | case DataType::QSYMM8: |
| 121 | min_quant_val = std::numeric_limits<int8_t>::min(); |
| 122 | max_quant_val = std::numeric_limits<int8_t>::max(); |
| 123 | break; |
| 124 | case DataType::QASYMM16: |
| 125 | min_quant_val = std::numeric_limits<uint16_t>::min(); |
| 126 | max_quant_val = std::numeric_limits<uint16_t>::max(); |
| 127 | break; |
| 128 | case DataType::QSYMM16: |
| 129 | min_quant_val = std::numeric_limits<int16_t>::min(); |
| 130 | max_quant_val = std::numeric_limits<int16_t>::max(); |
| 131 | break; |
| 132 | default: |
| 133 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 134 | } |
| 135 | return std::make_pair(min_quant_val, max_quant_val); |
| 136 | } |
| 137 | } // quantization |
| 138 | } // arm_compute |