Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 ARM Limited. |
| 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 | */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_QUANTIZATION_INFO_H |
| 25 | #define ARM_COMPUTE_QUANTIZATION_INFO_H |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Rounding.h" |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 28 | #include "utils/misc/Utility.h" |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 29 | |
| 30 | #include <cstddef> |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 31 | #include <type_traits> |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 36 | using qasymm8_t = uint8_t; /**< 8 bit quantized asymmetric scalar value */ |
| 37 | using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */ |
| 38 | using qasymm16_t = uint16_t; /**< 16 bit quantized asymmetric scalar value */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 39 | |
| 40 | /** Quantization info when assuming per layer quantization */ |
| 41 | struct UniformQuantizationInfo |
| 42 | { |
| 43 | /** Default constructor */ |
| 44 | UniformQuantizationInfo() |
| 45 | : scale(0.f), offset(0) |
| 46 | { |
| 47 | } |
| 48 | /** Constructor |
| 49 | * |
| 50 | * @param[in] scale Quantization scale |
| 51 | * @param[in] offset Quantization offset |
| 52 | */ |
| 53 | UniformQuantizationInfo(float scale, int32_t offset) |
| 54 | : scale(scale), offset(offset) |
| 55 | { |
| 56 | } |
| 57 | /** Checks if the scale and offset are both zero */ |
| 58 | bool empty() const |
| 59 | { |
| 60 | return (scale == 0) && (offset == 0); |
| 61 | } |
| 62 | |
| 63 | float scale; |
| 64 | int32_t offset; |
| 65 | }; |
| 66 | |
| 67 | /** Quantization information */ |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 68 | class QuantizationInfo |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 69 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 70 | public: |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 71 | /** Default constructor */ |
| 72 | QuantizationInfo() noexcept |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 73 | : _scale(), |
| 74 | _offset() |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 75 | { |
| 76 | } |
| 77 | /** Construct quantization info. |
| 78 | * |
| 79 | * @note Used for symmetric quantization |
| 80 | * |
| 81 | * @param[in] scale Scale. |
| 82 | */ |
| 83 | QuantizationInfo(float scale) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 84 | : _scale(1, scale), _offset() |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | /** Construct quantization info. |
| 88 | * |
| 89 | * @note Used for asymmetric quantization |
| 90 | * |
| 91 | * @param[in] scale Scale. |
| 92 | * @param[in] offset Offset. |
| 93 | */ |
| 94 | QuantizationInfo(float scale, int offset) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 95 | : _scale(1, scale), _offset(1, offset) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | /** Construct quantization info. |
| 99 | * |
| 100 | * @note Used for symmetric per channel quantization |
| 101 | * |
| 102 | * @param[in] scale Scale. |
| 103 | */ |
| 104 | QuantizationInfo(std::vector<float> scale) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 105 | : _scale(scale), _offset() |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 106 | { |
| 107 | } |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 108 | /** Construct quantization info. |
| 109 | * |
| 110 | * @note Used for asymmetric per channel quantization |
| 111 | * |
| 112 | * @param[in] scale Scale. |
| 113 | * @param[in] offset Offset. |
| 114 | */ |
| 115 | QuantizationInfo(std::vector<float> scale, std::vector<int32_t> offset) |
| 116 | : _scale(scale), _offset(offset) |
| 117 | { |
| 118 | } |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 119 | /** Scale vector accessor |
| 120 | * |
| 121 | * @return A reference to quantization scale metadata |
| 122 | */ |
| 123 | const std::vector<float> &scale() const |
| 124 | { |
| 125 | return _scale; |
| 126 | } |
| 127 | /** Offset vector accessor |
| 128 | * |
| 129 | * @return A reference to quantization offset metadata |
| 130 | */ |
| 131 | const std::vector<int32_t> &offset() const |
| 132 | { |
| 133 | return _offset; |
| 134 | } |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 135 | /** Indicates whether this QuantizationInfo has valid settings or not |
| 136 | * |
| 137 | * @return True if the this has invalid settings. |
| 138 | */ |
| 139 | bool empty() const |
| 140 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 141 | return _scale.empty() && _offset.empty(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 142 | } |
| 143 | /** Return per layer quantization info |
| 144 | * |
| 145 | * @return Uniform quantization information in case of empty information zero is returned in the respective fields |
| 146 | */ |
| 147 | UniformQuantizationInfo uniform() const |
| 148 | { |
| 149 | UniformQuantizationInfo uqinfo; |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 150 | uqinfo.scale = _scale.empty() ? 0 : _scale[0]; |
| 151 | uqinfo.offset = _offset.empty() ? 0 : _offset[0]; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 152 | |
| 153 | return uqinfo; |
| 154 | } |
| 155 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 156 | private: |
| 157 | std::vector<float> _scale; /**< Vector containing scaling factors */ |
| 158 | std::vector<int32_t> _offset; /**< Vector containing zero offsets */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** Check whether two quantization info are equal. |
| 162 | * |
| 163 | * @param[in] lhs RHS quantization info. |
| 164 | * @param[in] rhs LHS quantization info. |
| 165 | * |
| 166 | * @return True if the given quantization info is the same. |
| 167 | */ |
| 168 | inline bool operator==(const QuantizationInfo &lhs, const QuantizationInfo &rhs) |
| 169 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 170 | return (lhs.scale() == rhs.scale()) && (lhs.offset() == rhs.offset()); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** Check whether two quantization info are not equal. |
| 174 | * |
| 175 | * @param[in] lhs RHS quantization info. |
| 176 | * @param[in] rhs LHS quantization info. |
| 177 | * |
| 178 | * @return True if the given quantization info is the same. |
| 179 | */ |
| 180 | inline bool operator!=(const QuantizationInfo &lhs, const QuantizationInfo &rhs) |
| 181 | { |
| 182 | return !(operator==(lhs, rhs)); |
| 183 | } |
| 184 | |
| 185 | /** Check whether two quantization info are equal. |
| 186 | * |
| 187 | * @param[in] lhs RHS quantization info. |
| 188 | * @param[in] rhs LHS quantization info. |
| 189 | * |
| 190 | * @return True if the given quantization info is the same. |
| 191 | */ |
| 192 | inline bool operator==(const UniformQuantizationInfo &lhs, const UniformQuantizationInfo &rhs) |
| 193 | { |
| 194 | return (lhs.scale == rhs.scale) && (lhs.offset == rhs.offset); |
| 195 | } |
| 196 | |
| 197 | /** Check whether two quantization info are not equal. |
| 198 | * |
| 199 | * @param[in] lhs RHS quantization info. |
| 200 | * @param[in] rhs LHS quantization info. |
| 201 | * |
| 202 | * @return True if the given quantization info is the same. |
| 203 | */ |
| 204 | inline bool operator!=(const UniformQuantizationInfo &lhs, const UniformQuantizationInfo &rhs) |
| 205 | { |
| 206 | return !(operator==(lhs, rhs)); |
| 207 | } |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 208 | template <typename QUANTIZED_TYPE = uint8_t> |
| 209 | struct Qasymm8QuantizationHelper |
| 210 | { |
| 211 | static_assert(std::is_same<QUANTIZED_TYPE, uint8_t>::value |
| 212 | || std::is_same<QUANTIZED_TYPE, int8_t>::value, |
| 213 | "quantized type should be either uint8_t or int8_t."); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 214 | |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 215 | /** Quantize a value given a 8-bit asymmetric quantization scheme |
| 216 | * |
| 217 | * @param[in] value Value to quantize |
| 218 | * @param[in] qinfo Quantization information to use for quantizing |
| 219 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 220 | * |
| 221 | * @return Quantized value |
| 222 | */ |
| 223 | static inline QUANTIZED_TYPE quantize(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
| 224 | { |
| 225 | ARM_COMPUTE_ERROR_ON(qinfo.scale == 0); |
| 226 | const int quantized = arm_compute::round(value / qinfo.scale, rounding_policy) + qinfo.offset; |
| 227 | return static_cast<QUANTIZED_TYPE>(arm_compute::utility::clamp<decltype(quantized), QUANTIZED_TYPE>(quantized)); |
| 228 | } |
| 229 | |
| 230 | /** Quantize a value given a 8-bit asymmetric quantization scheme |
| 231 | * |
| 232 | * @param[in] value Value to quantize |
| 233 | * @param[in] qinfo Quantization information to use for quantizing |
| 234 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 235 | * |
| 236 | * @return Quantized value |
| 237 | */ |
| 238 | static inline QUANTIZED_TYPE quantize(float value, const QuantizationInfo &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
| 239 | { |
| 240 | const UniformQuantizationInfo uqinfo = qinfo.uniform(); |
| 241 | ARM_COMPUTE_ERROR_ON(uqinfo.scale == 0); |
| 242 | const int quantized = arm_compute::round(value / uqinfo.scale, rounding_policy) + uqinfo.offset; |
| 243 | return static_cast<QUANTIZED_TYPE>(arm_compute::utility::clamp<decltype(quantized), QUANTIZED_TYPE>(quantized)); |
| 244 | } |
| 245 | |
| 246 | /** Dequantize a value given a 8-bit asymmetric quantization scheme |
| 247 | * |
| 248 | * @param[in] value Value to dequantize |
| 249 | * @param[in] qinfo Quantization information to use for dequantizing |
| 250 | * |
| 251 | * @return Dequantized value |
| 252 | */ |
| 253 | static inline float dequantize(QUANTIZED_TYPE value, const UniformQuantizationInfo &qinfo) |
| 254 | { |
| 255 | return (static_cast<int>(value) - qinfo.offset) * qinfo.scale; |
| 256 | } |
| 257 | |
| 258 | /** Dequantize a value given a 8-bit asymmetric quantization scheme |
| 259 | * |
| 260 | * @param[in] value Value to dequantize |
| 261 | * @param[in] qinfo Quantization information to use for dequantizing |
| 262 | * |
| 263 | * @return Dequantized value |
| 264 | */ |
| 265 | static inline float dequantize(QUANTIZED_TYPE value, const QuantizationInfo &qinfo) |
| 266 | { |
| 267 | const UniformQuantizationInfo uqinfo = qinfo.uniform(); |
| 268 | return (static_cast<int>(value) - uqinfo.offset) * uqinfo.scale; |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | /** Quantize a value given an unsigned 8-bit asymmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 273 | * |
| 274 | * @param[in] value Value to quantize |
| 275 | * @param[in] qinfo Quantization information to use for quantizing |
| 276 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 277 | * |
| 278 | * @return Quantized value |
| 279 | */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 280 | template <typename INFO_TYPE> |
| 281 | inline uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 282 | { |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 283 | return Qasymm8QuantizationHelper<uint8_t>::quantize(value, qinfo, rounding_policy); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 286 | /** Quantize a value given a signed 8-bit asymmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 287 | * |
| 288 | * @param[in] value Value to quantize |
| 289 | * @param[in] qinfo Quantization information to use for quantizing |
| 290 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 291 | * |
| 292 | * @return Quantized value |
| 293 | */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 294 | template <typename INFO_TYPE> |
| 295 | inline int8_t quantize_qasymm8_signed(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 296 | { |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 297 | return Qasymm8QuantizationHelper<int8_t>::quantize(value, qinfo, rounding_policy); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 300 | /** Quantize a value given a 8-bit symmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 301 | * |
| 302 | * @param[in] value Value to quantize |
| 303 | * @param[in] qinfo Quantization information to use for quantizing |
| 304 | * |
| 305 | * @return Quantized value |
| 306 | */ |
| 307 | inline int8_t quantize_qsymm8(float value, const QuantizationInfo &qinfo) |
| 308 | { |
| 309 | int quantized = arm_compute::round(value / qinfo.uniform().scale, RoundingPolicy::TO_NEAREST_UP); |
| 310 | quantized = std::max(-128, std::min(quantized, 127)); |
| 311 | return quantized; |
| 312 | } |
| 313 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 314 | /** Quantize a value given a 8-bit symmetric per channel quantization scheme |
| 315 | * |
| 316 | * @param[in] value Value to quantize |
| 317 | * @param[in] qinfo Quantization information to use for quantizing |
| 318 | * @param[in] channel_id channel index into the scale vector of quantization info |
| 319 | * |
| 320 | * @return Quantized value |
| 321 | */ |
| 322 | inline int8_t quantize_qsymm8_per_channel(float value, const QuantizationInfo &qinfo, size_t channel_id = 0) |
| 323 | { |
| 324 | int quantized = arm_compute::round(value / qinfo.scale()[channel_id], RoundingPolicy::TO_NEAREST_UP); |
| 325 | quantized = std::max(-128, std::min(quantized, 127)); |
| 326 | return quantized; |
| 327 | } |
| 328 | |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 329 | /** Dequantize a value given an unsigned 8-bit asymmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 330 | * |
| 331 | * @param[in] value Value to dequantize |
| 332 | * @param[in] qinfo Quantization information to use for dequantizing |
| 333 | * |
| 334 | * @return Dequantized value |
| 335 | */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 336 | template <typename INFO_TYPE> |
| 337 | inline float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 338 | { |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 339 | return Qasymm8QuantizationHelper<uint8_t>::dequantize(value, qinfo); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 342 | /** Dequantize a value given a signed 8-bit asymmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 343 | * |
| 344 | * @param[in] value Value to dequantize |
| 345 | * @param[in] qinfo Quantization information to use for dequantizing |
| 346 | * |
| 347 | * @return Dequantized value |
| 348 | */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 349 | template <typename INFO_TYPE> |
| 350 | inline float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 351 | { |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 352 | return Qasymm8QuantizationHelper<int8_t>::dequantize(value, qinfo); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 355 | /** Dequantize a value given an 8-bit asymmetric quantization scheme |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 356 | * |
| 357 | * @param[in] value Value to dequantize |
| 358 | * @param[in] scale Scale to use for dequantization |
| 359 | * @param[in] offset Zero-offset to use for dequantization |
| 360 | * |
| 361 | * @return Dequantized value |
| 362 | */ |
| 363 | inline float dequantize(uint8_t value, float scale, int32_t offset) |
| 364 | { |
| 365 | return (static_cast<int>(value) - offset) * scale; |
| 366 | } |
| 367 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 368 | /** Dequantize a value given a 8-bit symmetric quantization scheme |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 369 | * |
| 370 | * @param[in] value Value to dequantize |
| 371 | * @param[in] qinfo Quantization information to use for dequantizing |
| 372 | * |
| 373 | * @return Dequantized value |
| 374 | */ |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 375 | inline float dequantize_qsymm8(int8_t value, const UniformQuantizationInfo &qinfo) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 376 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 377 | return value * qinfo.scale; |
| 378 | } |
| 379 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 380 | /** Dequantize a value given a 8-bit symmetric quantization scheme |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 381 | * |
| 382 | * @param[in] value Value to dequantize |
| 383 | * @param[in] scale Scale to use for dequantization |
| 384 | * |
| 385 | * @return Dequantized value |
| 386 | */ |
| 387 | inline float dequantize(int8_t value, float scale) |
| 388 | { |
| 389 | return value * scale; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 390 | } |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 391 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 392 | /** Dequantize a value given a 16-bit symmetric quantization scheme |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 393 | * |
| 394 | * @param[in] value Value to dequantize |
| 395 | * @param[in] scale Scale to use for dequantization |
| 396 | * |
| 397 | * @return Dequantized value |
| 398 | */ |
| 399 | inline float dequantize(int16_t value, float scale) |
| 400 | { |
| 401 | return value * scale; |
| 402 | } |
| 403 | |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 404 | /** Dequantize a value given a 16-bit asymmetric quantization scheme |
| 405 | * |
| 406 | * @param[in] value Value to dequantize |
| 407 | * @param[in] scale Scale to use for dequantization |
| 408 | * @param[in] offset Zero-offset to use for dequantization |
| 409 | * |
| 410 | * @return Dequantized value |
| 411 | */ |
| 412 | inline float dequantize(uint16_t value, float scale, int32_t offset) |
| 413 | { |
| 414 | return (static_cast<int>(value) - offset) * scale; |
| 415 | } |
| 416 | |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 417 | /** Quantize a value given a 16-bit symmetric quantization scheme |
| 418 | * |
| 419 | * @param[in] value Value to quantize |
| 420 | * @param[in] qinfo Quantization information to use for quantizing |
| 421 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 422 | * |
| 423 | * @return Quantized value |
| 424 | */ |
| 425 | inline int16_t quantize_qsymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
| 426 | { |
| 427 | int quantized = arm_compute::round(value / qinfo.scale, rounding_policy); |
| 428 | quantized = arm_compute::utility::clamp<int, int16_t>(quantized); |
| 429 | return quantized; |
| 430 | } |
| 431 | |
| 432 | /** Dequantize a value given a 16-bit symmetric quantization scheme |
| 433 | * |
| 434 | * @param[in] value Value to dequantize |
| 435 | * @param[in] qinfo Quantization information to use for dequantizing |
| 436 | * |
| 437 | * @return Dequantized value |
| 438 | */ |
| 439 | inline float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo) |
| 440 | { |
| 441 | return value * qinfo.scale; |
| 442 | } |
| 443 | |
| 444 | /** Quantize a value given a 16-bit symmetric quantization scheme |
| 445 | * |
| 446 | * @param[in] value Value to quantize |
| 447 | * @param[in] qinfo Quantization information to use for quantizing |
| 448 | * |
| 449 | * @return Quantized value |
| 450 | */ |
| 451 | inline int16_t quantize_qsymm16(float value, const QuantizationInfo &qinfo) |
| 452 | { |
| 453 | return quantize_qsymm16(value, qinfo.uniform()); |
| 454 | } |
| 455 | |
| 456 | /** Dequantize a value given a 16-bit symmetric quantization scheme |
| 457 | * |
| 458 | * @param[in] value Value to dequantize |
| 459 | * @param[in] qinfo Quantization information to use for dequantizing |
| 460 | * |
| 461 | * @return Dequantized value |
| 462 | */ |
| 463 | inline float dequantize_qsymm16(int16_t value, const QuantizationInfo &qinfo) |
| 464 | { |
| 465 | return dequantize_qsymm16(value, qinfo.uniform()); |
| 466 | } |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 467 | |
| 468 | /** Quantize a value given a 16-bit asymmetric quantization scheme |
| 469 | * |
| 470 | * @param[in] value Value to quantize |
| 471 | * @param[in] qinfo Quantization information to use for quantizing |
| 472 | * @param[in] rounding_policy (Optional) Rounding policy to use. Default: nearest up |
| 473 | * |
| 474 | * @return Quantized value |
| 475 | */ |
| 476 | inline uint16_t quantize_qasymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_UP) |
| 477 | { |
| 478 | int quantized = arm_compute::round(value / qinfo.scale, rounding_policy) + qinfo.offset; |
| 479 | quantized = arm_compute::utility::clamp<int, uint16_t>(quantized); |
| 480 | return quantized; |
| 481 | } |
| 482 | |
| 483 | /** Dequantize a value given a 16-bit asymmetric quantization scheme |
| 484 | * |
| 485 | * @param[in] value Value to dequantize |
| 486 | * @param[in] qinfo Quantization information to use for dequantizing |
| 487 | * |
| 488 | * @return Dequantized value |
| 489 | */ |
| 490 | inline float dequantize_qasymm16(uint16_t value, const UniformQuantizationInfo &qinfo) |
| 491 | { |
| 492 | return (static_cast<int>(value) - qinfo.offset) * qinfo.scale; |
| 493 | } |
| 494 | |
| 495 | /** Quantize a value given a 16-bit asymmetric quantization scheme |
| 496 | * |
| 497 | * @param[in] value Value to quantize |
| 498 | * @param[in] qinfo Quantization information to use for quantizing |
| 499 | * |
| 500 | * @return Quantized value |
| 501 | */ |
| 502 | inline uint16_t quantize_qasymm16(float value, const QuantizationInfo &qinfo) |
| 503 | { |
| 504 | return quantize_qasymm16(value, qinfo.uniform()); |
| 505 | } |
| 506 | |
| 507 | /** Dequantize a value given a 16-bit asymmetric quantization scheme |
| 508 | * |
| 509 | * @param[in] value Value to dequantize |
| 510 | * @param[in] qinfo Quantization information to use for dequantizing |
| 511 | * |
| 512 | * @return Dequantized value |
| 513 | */ |
| 514 | inline float dequantize_qasymm16(uint16_t value, const QuantizationInfo &qinfo) |
| 515 | { |
| 516 | return dequantize_qasymm16(value, qinfo.uniform()); |
| 517 | } |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 518 | } // namespace arm_compute |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 519 | #endif /* ARM_COMPUTE_QUANTIZATION_INFO_H */ |