Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_QUANTIZATION_ASYMM_HELPERS_H |
| 25 | #define ARM_COMPUTE_QUANTIZATION_ASYMM_HELPERS_H |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Error.h" |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace quantization |
| 34 | { |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 35 | /** Calculate quantized representation of multiplier. |
| 36 | * |
| 37 | * @param[in] multiplier Real multiplier. |
| 38 | * @param[out] quant_multiplier Integer multiplier. |
| 39 | * @param[out] shift bit shift. A negative value indicates a left shift, while a positive value indicates a right shift |
| 40 | * |
| 41 | * @return a status |
| 42 | */ |
| 43 | Status calculate_quantized_multiplier(float multiplier, int *quant_multiplier, int *shift); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 44 | /** Calculate quantized representation of multiplier with value less than one. |
| 45 | * |
| 46 | * @param[in] multiplier Real multiplier. |
| 47 | * @param[out] quant_multiplier Integer multiplier. |
| 48 | * @param[out] right_shift Right bit shift. |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 49 | * |
| 50 | * @return a status |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 51 | */ |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 52 | Status calculate_quantized_multiplier_less_than_one(float multiplier, int *quant_multiplier, int *right_shift); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 53 | /** Calculate quantized representation of multiplier having value greater than one. |
| 54 | * |
| 55 | * @param[in] multiplier Real multiplier. |
| 56 | * @param[out] quantized_multiplier Integer multiplier. |
| 57 | * @param[out] left_shift Left bit shift. |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 58 | * |
| 59 | * @return a status |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 60 | */ |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 61 | Status calculate_quantized_multiplier_greater_than_one(float multiplier, int *quantized_multiplier, int *left_shift); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 62 | |
| 63 | /** Calculate quantized representation of per-channel multipliers with value less than one. |
| 64 | * |
| 65 | * @param[in] iq_info Input quantization info. |
| 66 | * @param[in] wq_info Weights quantization info. |
| 67 | * @param[in] oq_info Output quantization info. |
| 68 | * @param[in, out] stage_info GemmLowp output stage info |
| 69 | * |
| 70 | * @return a status |
| 71 | */ |
| 72 | Status calculate_quantized_multipliers_less_than_one(const QuantizationInfo &iq_info, |
| 73 | const QuantizationInfo &wq_info, |
| 74 | const QuantizationInfo &oq_info, |
| 75 | GEMMLowpOutputStageInfo &stage_info); |
| 76 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 77 | /** Get minimum and maximum values for the input quantized data type |
| 78 | * |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 79 | * @return min and max values for the quantized data type |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 80 | */ |
| 81 | std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 82 | /** Compute quantized per-channel multipliers and shifts. As many multipliers |
| 83 | * and shifts as output channels are computed. If weights are not quantized |
| 84 | * per-channel, multipliers and shifts will end up being the same for each |
| 85 | * channel. |
| 86 | * |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 87 | * @param[in] input Input tensor info. |
| 88 | * @param[in] weights Weights tensor info. |
| 89 | * @param[in] output Output tensor info. |
| 90 | * @param[in] idx_ofms Dimension index to get OFMs from the weights tensor. |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 91 | * @param[out] output_multipliers_ptr Pointer to the buffer where to store per-channel multipliers. |
| 92 | * @param[out] output_shifts_ptr Pointer to the buffer where to store per-channel shifts. |
| 93 | * |
| 94 | * @return min and max values for the quantized data type |
| 95 | */ |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 96 | void compute_quantized_multipliers_and_shifts(const ITensorInfo *input, |
| 97 | const ITensorInfo *weights, |
| 98 | const ITensorInfo *output, |
| 99 | unsigned int idx_ofms, |
| 100 | int32_t *output_multipliers_ptr, |
| 101 | int32_t *output_shifts_ptr); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 102 | } // namespace quantization |
| 103 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 104 | #endif /* ARM_COMPUTE_IO_FILE_HANDLER_H */ |