From 396cb95774bd7627254e3befec5e34844de701c9 Mon Sep 17 00:00:00 2001 From: Sang-Hoon Park Date: Thu, 26 Mar 2020 14:02:37 +0000 Subject: COMPMID-3284 add utilities for layer normalization of NEON QLSTM Change-Id: Ie98a8c4c30ac7859a989a29cbe7602c1c6fec26b Signed-off-by: Sang-Hoon Park Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/2934 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Reviewed-by: Georgios Pinitas --- src/core/utils/quantization/AsymmHelpers.cpp | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'src/core/utils/quantization/AsymmHelpers.cpp') diff --git a/src/core/utils/quantization/AsymmHelpers.cpp b/src/core/utils/quantization/AsymmHelpers.cpp index e1ba6413b4..c5eef9dd77 100644 --- a/src/core/utils/quantization/AsymmHelpers.cpp +++ b/src/core/utils/quantization/AsymmHelpers.cpp @@ -196,5 +196,56 @@ void compute_quantized_multipliers_and_shifts(const ITensorInfo *input, output_shifts_ptr[i] = output_shift; } } + +int32_t saturating_rounding_doubling_highmul(int32_t a, int32_t b) +{ + bool overflow = a == b && a == std::numeric_limits::min(); + int64_t a_64(a); + int64_t b_64(b); + int64_t ab_64 = a_64 * b_64; + int32_t nudge = ab_64 >= 0 ? (1 << 30) : (1 - (1 << 30)); + int32_t ab_x2_high32 = static_cast((ab_64 + nudge) / (1ll << 31)); + return overflow ? std::numeric_limits::max() : ab_x2_high32; +} + +inline int32_t rounding_divide_by_pow2(int32_t x, int exponent) +{ + const int32_t mask = (1 << exponent) - 1; + const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); + return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); +} + +int32_t multiply_by_quantized_multipler(int32_t input, int32_t qmul, int32_t shift) +{ + const auto left_shift = shift > 0 ? shift : 0; + const auto right_shift = shift > 0 ? 0 : -shift; + return rounding_divide_by_pow2(saturating_rounding_doubling_highmul(input * (1 << left_shift), qmul), right_shift); +} + +int32_t saturating_rounding_multiply_by_pow2(int32_t exponent, int32_t v) +{ + if(exponent == 0) + { + return v; + } + else if(exponent < 0) + { + return rounding_divide_by_pow2(v, -exponent); + } + else + { + constexpr auto min = std::numeric_limits::min(); + constexpr auto max = std::numeric_limits::max(); + const auto width = sizeof(int32_t) * 8; + + const int32_t threshold = ((1 << (width - 1 - exponent)) - 1); + bool pos_mask = v > threshold; + bool neg_mask = v < -threshold; + int32_t result = v << exponent; + result = pos_mask ? max : result; + result = neg_mask ? min : result; + return result; + } +} } // quantization } // arm_compute -- cgit v1.2.1