From bb88f89b7a12e83eea2fc701f1f82aabf7dfcf7a Mon Sep 17 00:00:00 2001 From: SiCong Li Date: Fri, 28 Aug 2020 11:18:47 +0100 Subject: COMPMID-3581 Add S32 support to NEPixelWiseMultiplication * Add S32 support to NEPixelWiseMultiplication and NEPixelWiseMultiplicationKernel * Scale == 1/255 is not supported for S32, as on non-aarch64 the precision requirement is not met, and scale is a non-standard parameter anyway. * Fix the data types validation logics to also test for all invalid data type combinations. * Add validation tests for S32 NEON PixelWiseMultiplication * The wrap tolerance for ScaleOther (scale == 1/2^n) cases is set to 1 instead of 0 because the reference uses floating point division followed by rounding, which is isn't bit accurate. Change-Id: I28839afda7a4f98c985d1763620e08d98f740142 Signed-off-by: SiCong Li Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3923 Tested-by: Arm Jenkins Reviewed-by: Michele Di Giorgio Reviewed-by: Georgios Pinitas Comments-Addressed: Arm Jenkins --- .../reference/PixelWiseMultiplication.cpp | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'tests/validation/reference/PixelWiseMultiplication.cpp') diff --git a/tests/validation/reference/PixelWiseMultiplication.cpp b/tests/validation/reference/PixelWiseMultiplication.cpp index 9f70b1c2af..0450991f61 100644 --- a/tests/validation/reference/PixelWiseMultiplication.cpp +++ b/tests/validation/reference/PixelWiseMultiplication.cpp @@ -43,6 +43,8 @@ struct is_floating_point namespace { +constexpr float scale1_constant = 1.f; + /** Compute the result of `src1 * src2 * scale`. The result type always matches the type of @p src2. * * @param[in] src1 An input value. Data types supported: U8/S16/F16/F32. @@ -89,6 +91,90 @@ T3 mul(const T1 src1, const T2 src2, float scale, ConvertPolicy convert_policy, } } +template <> +int32_t mul(const int32_t src1, const int32_t src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy) +{ + const int64_t intermediate_val = static_cast(src1) * static_cast(src2); + + if(std::abs(scale - scale1_constant) < 0.00001f) + { + // Use bit-accurate integer arithmetic for scale == 1 + // Apply conversion + if(convert_policy == ConvertPolicy::SATURATE) + { + return saturate_cast(intermediate_val); + } + else + { + // Correct wrapping behaviour for int32_t + const auto i32_hi = static_cast(std::numeric_limits::max()); + const auto i32_lo = static_cast(std::numeric_limits::lowest()); + const auto i32_wi = static_cast(1) << 32; + int64_t wrapped_rounded_val = intermediate_val - i32_wi * static_cast(support::cpp11::trunc(static_cast(intermediate_val) / i32_wi)); + if(wrapped_rounded_val <= i32_hi) + { + return static_cast(wrapped_rounded_val); + } + else + { + // Values beyond i32_hi wrap around to negatives + return static_cast((wrapped_rounded_val - i32_hi) + i32_lo - 1); + } + } + } + else + { + // Use double arithmetic for scale != 1; may not be bit-accurate + // Apply scaling + // scale == 1 / 2^scale_exponent + int scale_exponent = 0; + std::frexp(scale, &scale_exponent); + // Store the positive exponent. We know that we compute 1/2^n + // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 + scale_exponent = std::abs(scale_exponent - 1); + const double scale_inv = static_cast(1) << scale_exponent; + const double val = intermediate_val / scale_inv; + // Apply rounding + double rounded_val = 0; + switch(rounding_policy) + { + case(RoundingPolicy::TO_ZERO): + rounded_val = support::cpp11::trunc(val); + break; + case(RoundingPolicy::TO_NEAREST_UP): + rounded_val = round_half_up(val); + break; + case(RoundingPolicy::TO_NEAREST_EVEN): + rounded_val = round_half_even(val); + break; + default: + ARM_COMPUTE_ERROR("Unsupported rounding policy"); + } + // Apply conversion + if(convert_policy == ConvertPolicy::SATURATE) + { + return saturate_cast(rounded_val); + } + else + { + // Correct wrapping behaviour for int32_t + const auto i32_hi = static_cast(std::numeric_limits::max()); + const auto i32_lo = static_cast(std::numeric_limits::lowest()); + const auto i32_wi = static_cast(static_cast(1) << 32); + double wrapped_rounded_val = rounded_val - i32_wi * std::floor(rounded_val / i32_wi); + if(wrapped_rounded_val <= i32_hi) + { + return static_cast(wrapped_rounded_val); + } + else + { + // Values beyond i32_hi wrap around to negatives + return static_cast((wrapped_rounded_val - i32_hi) + i32_lo - 1); + } + } + } +} + template struct BroadcastUnroll { @@ -264,6 +350,7 @@ SimpleTensor pixel_wise_multiplication(const SimpleTensor &src // clang-format off template SimpleTensor pixel_wise_multiplication(const SimpleTensor &src1, const SimpleTensor &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, DataType dt_out, const QuantizationInfo &qout); template SimpleTensor pixel_wise_multiplication(const SimpleTensor &src1, const SimpleTensor &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, DataType dt_out, const QuantizationInfo &qout); +template SimpleTensor pixel_wise_multiplication(const SimpleTensor &src1, const SimpleTensor &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, DataType dt_out, const QuantizationInfo &qout); template SimpleTensor pixel_wise_multiplication(const SimpleTensor &src1, const SimpleTensor &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, DataType dt_out, const QuantizationInfo &qout); template SimpleTensor pixel_wise_multiplication(const SimpleTensor &src1, const SimpleTensor &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, DataType dt_out, const QuantizationInfo &qout); // clang-format on -- cgit v1.2.1