blob: 2324fe1838dee48946832426fcca6876bab0fb43 [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +01002 * Copyright (c) 2017-2021, 2023 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_QUANTIZATION_ASYMM_HELPERS_H
25#define ARM_COMPUTE_QUANTIZATION_ASYMM_HELPERS_H
Chunosovd621bca2017-11-03 17:33:15 +070026
27#include "arm_compute/core/Error.h"
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010028#include "arm_compute/core/ITensor.h"
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010029#include "arm_compute/core/Types.h"
Chunosovd621bca2017-11-03 17:33:15 +070030
31namespace arm_compute
32{
33namespace quantization
34{
Manuel Bottini07263982019-10-17 18:37:26 +010035/** 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
Sang-Hoon Park30b46a62020-04-18 01:40:57 +010040 * @param[in] ignore_epsilon When true, ignore pre-defined epsilon value. Defaults to false
Manuel Bottini07263982019-10-17 18:37:26 +010041 *
42 * @return a status
43 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044Status calculate_quantized_multiplier(float multiplier,
45 int32_t *quant_multiplier,
46 int32_t *shift,
47 bool ignore_epsilon = false);
Chunosovd621bca2017-11-03 17:33:15 +070048/** Calculate quantized representation of multiplier with value less than one.
49 *
50 * @param[in] multiplier Real multiplier.
51 * @param[out] quant_multiplier Integer multiplier.
52 * @param[out] right_shift Right bit shift.
Sang-Hoon Park30b46a62020-04-18 01:40:57 +010053 * @param[in] ignore_epsilon When true, ignore pre-defined epsilon value. Defaults to false
Georgios Pinitas631c41a2017-12-06 11:53:03 +000054 *
55 * @return a status
Chunosovd621bca2017-11-03 17:33:15 +070056 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057Status calculate_quantized_multiplier_less_than_one(float multiplier,
58 int32_t *quant_multiplier,
59 int32_t *right_shift,
60 bool ignore_epsilon = false);
Chunosovf450caa2017-11-08 16:09:35 +070061/** Calculate quantized representation of multiplier having value greater than one.
62 *
63 * @param[in] multiplier Real multiplier.
64 * @param[out] quantized_multiplier Integer multiplier.
65 * @param[out] left_shift Left bit shift.
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066 *
67 * @return a status
Chunosovf450caa2017-11-08 16:09:35 +070068 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069Status
70calculate_quantized_multiplier_greater_than_one(float multiplier, int32_t *quantized_multiplier, int32_t *left_shift);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010071
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000072/** Calculate quantized representation of per-channel multipliers
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010073 *
74 * @param[in] iq_info Input quantization info.
75 * @param[in] wq_info Weights quantization info.
76 * @param[in] oq_info Output quantization info.
77 * @param[in, out] stage_info GemmLowp output stage info
78 *
79 * @return a status
80 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081Status calculate_quantized_multipliers(const QuantizationInfo &iq_info,
82 const QuantizationInfo &wq_info,
83 const QuantizationInfo &oq_info,
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000084 GEMMLowpOutputStageInfo &stage_info);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010085
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010086/** Get minimum and maximum values for the input quantized data type
87 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010088 * @return min and max values for the quantized data type
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010089 */
90std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type);
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +010091
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010092/** Compute quantized per-channel multipliers and shifts. As many multipliers
93 * and shifts as output channels are computed. If weights are not quantized
94 * per-channel, multipliers and shifts will end up being the same for each
95 * channel.
96 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000097 * @param[in] input Input tensor info.
98 * @param[in] weights Weights tensor info.
99 * @param[in] output Output tensor info.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100100 * @param[out] output_multipliers_ptr Pointer to the buffer where to store per-channel multipliers.
101 * @param[out] output_shifts_ptr Pointer to the buffer where to store per-channel shifts.
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100102 */
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000103void compute_quantized_multipliers_and_shifts(const ITensorInfo *input,
104 const ITensorInfo *weights,
105 const ITensorInfo *output,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000106 int32_t *output_multipliers_ptr,
107 int32_t *output_shifts_ptr);
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000108
109/** Round to the nearest division by a power-of-two using exponent, copied from NEMath
110 *
111 * @note This function calculates the following expression: (x + 2^n -1 ) / 2^n where n = exponent
112 *
113 * @param[in] x Element to divide.
114 * @param[in] exponent Integer value used to round to nearest division by a power-of-two
115 *
116 * @return the nearest division by a power-of-two using exponent
117 */
118int32_t rounding_divide_by_pow2(int32_t x, int exponent);
119
120/** Compute multiplication of two integers
121 *
122 * @param[in] a One integer to multiply
123 * @param[in] b Another integer to multiply
124 *
125 * @return The multiplied value
126 */
127int32_t saturating_rounding_doubling_highmul(int32_t a, int32_t b);
128
129/** Compute the value multiplied by given quantized multiplier and shift
130 *
131 * @param[in] input Target value to multiply.
132 * @param[in] qmul Quantized multipler
133 * @param[in] shift Left bit shift
134 *
135 * @return The multiplied value
136 */
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000137int32_t multiply_by_quantized_multiplier(int32_t input, int32_t qmul, int32_t shift);
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000138
139/** Compute the value multiplied the power-of-two
140 *
141 * @param[in] exponent Exponent used to calculate power-of-two
142 * @param[in] v Target value to multiply
143 *
144 * @return The multiplied value
145 */
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000146int32_t saturating_rounding_multiply_by_pow2(int32_t exponent, int32_t v);
147
148/** Compute quantized multiplier and shift for the inverse square root of input.
149 * Using 3-bit fixed point and 5 iteration of Newton-Raphson method.
150 *
151 * @param[in] input Input to use
152 * @param[in] reverse_shift -1 to reverse the shift direction
153 * @param[out] output_inv_sqrt Quantized multiplier for inverse square root
154 * @param[out] output_shift Shift for inverse square root
155 *
156 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157void get_invsqrt_quantized_multiplier_exp(int32_t input,
158 int32_t reverse_shift,
159 int32_t &output_inv_sqrt,
160 int32_t &output_shift);
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000161
Chunosovd621bca2017-11-03 17:33:15 +0700162} // namespace quantization
163} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000164#endif /* ARM_COMPUTE_IO_FILE_HANDLER_H */