diff options
author | Manuel Bottini <manuel.bottini@arm.com> | 2020-02-07 16:31:59 +0000 |
---|---|---|
committer | Georgios Pinitas <georgios.pinitas@arm.com> | 2020-02-26 11:06:52 +0000 |
commit | 4370cffc7fb0da7fb486b9d06d24e16169521876 (patch) | |
tree | 3f1ff71e631e3e14efc423a9fb3a4cf9b4b93b94 /arm_compute/core/QuantizationInfo.h | |
parent | 12f2b8c316155660f1e612fe7e8fab7861decc03 (diff) | |
download | ComputeLibrary-4370cffc7fb0da7fb486b9d06d24e16169521876.tar.gz |
COMPMID-3034: Add NERequantizationLayerKernel
Change-Id: I3f098c3c2c2031d8cbe7326eab88a4e78bda867f
Signed-off-by: Manuel Bottini <manuel.bottini@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/2704
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Sang-Hoon Park <sang-hoon.park@arm.com>
Diffstat (limited to 'arm_compute/core/QuantizationInfo.h')
-rw-r--r-- | arm_compute/core/QuantizationInfo.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/arm_compute/core/QuantizationInfo.h b/arm_compute/core/QuantizationInfo.h index 06ba665c6b..f859beb87a 100644 --- a/arm_compute/core/QuantizationInfo.h +++ b/arm_compute/core/QuantizationInfo.h @@ -516,5 +516,49 @@ inline float dequantize_qasymm16(uint16_t value, const QuantizationInfo &qinfo) { return dequantize_qasymm16(value, qinfo.uniform()); } + +/* + * In case of requantization of a quantized input tensor to an output tensor with another quantization + * instead of applying dequantization and then a quantization functions, we just compute new scale and + * offset. + * + * Assuming: + * - q_i as input quantized value + * - q_o as output quantized value + * - z_i as input quantization offset value + * - z_o as output quantization offset value + * - s_i as input quantization scale value + * - s_o as output quantization scale value + * - z_n as new quantization offset value + * - s_n as new quantization scale value + * + * q_o = ( q_i - z_i ) * s_i / s_o + z_o + * + * We can rewrite the formula as: + * + * q_o = ( q_i * s_i / s_o ) - z_i * s_i / s_o + z_o + * + * q_o = q_i / s_n + z_n + * + * Where: + * + * s_n = s_o / s_i + * + * z_n = - z_i * s_i / s_o + z_o + * + */ +inline UniformQuantizationInfo compute_requantization_scale_offset(const UniformQuantizationInfo &uqinfo_in, const UniformQuantizationInfo &uqinfo_out) +{ + float scale_to_apply = uqinfo_out.scale; + int32_t offset_to_apply = uqinfo_out.offset; + + scale_to_apply /= uqinfo_in.scale; + // In order to minimize flooring we convert the offset to a float, + // then compute the new offset in the float domain, + // finally we convert it back as int32_t + offset_to_apply -= static_cast<int32_t>(static_cast<float>(uqinfo_in.offset) * uqinfo_in.scale / uqinfo_out.scale); + return UniformQuantizationInfo(scale_to_apply, offset_to_apply); +} + } // namespace arm_compute #endif /* ARM_COMPUTE_QUANTIZATION_INFO_H */ |