From 14d7b535d48620f009efca576cc70fb6ea9ff20d Mon Sep 17 00:00:00 2001 From: Ramy Elgammal Date: Mon, 30 Jan 2023 04:56:47 +0000 Subject: Implementation of RSQRT for quantized int8 Resolves: COMPMID-5863 Change-Id: I9ff67face62826c1d335a6b941e8516be39bdac8 Signed-off-by: Ramy Elgammal Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/488768 Tested-by: bsgcomp Comments-Addressed: bsgcomp Reviewed-by: Gunes Bayir Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9225 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Benchmark: Arm Jenkins --- tests/validation/reference/ElementwiseUnary.cpp | 85 ++++++++++++++++++++++--- tests/validation/reference/ElementwiseUnary.h | 4 +- 2 files changed, 78 insertions(+), 11 deletions(-) (limited to 'tests/validation/reference') diff --git a/tests/validation/reference/ElementwiseUnary.cpp b/tests/validation/reference/ElementwiseUnary.cpp index 5333b53c15..d5218d772d 100644 --- a/tests/validation/reference/ElementwiseUnary.cpp +++ b/tests/validation/reference/ElementwiseUnary.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2020, 2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -22,7 +22,8 @@ * SOFTWARE. */ #include "ElementwiseUnary.h" - +#include "tests/validation/Helpers.h" +#include "utils/TypePrinter.h" namespace arm_compute { namespace test @@ -32,10 +33,8 @@ namespace validation namespace reference { template -SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary op) +SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op) { - SimpleTensor dst(src.shape(), src.data_type()); - for(int i = 0; i < src.num_elements(); ++i) { switch(op) @@ -65,13 +64,81 @@ SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary o ARM_COMPUTE_ERROR("Not implemented"); } } - return dst; } +template <> +SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op) +{ + if(dst.data_type() == DataType::QASYMM8_SIGNED) + { + SimpleTensor src_tmp = convert_from_asymmetric(src); + SimpleTensor dst_tmp(src.shape(), DataType::F32); + for(int i = 0; i < src.num_elements(); ++i) + { + switch(op) + { + case ElementWiseUnary::RSQRT: + if(src_tmp[i] != 0) + { + dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]); + } + else + { + // rsqrt(0) give 'inf' so set to the maximum in int8: 127 + dst_tmp[i] = (127.0f - dst.quantization_info().uniform().offset) * dst.quantization_info().uniform().scale ; + } + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } + } + dst = convert_to_asymmetric(dst_tmp, dst.quantization_info()); + } + else + { + ARM_COMPUTE_ERROR("Not implemented"); + } + return dst; +} +template <> +SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op) +{ + if(dst.data_type() == DataType::QASYMM8) + { + SimpleTensor src_tmp = convert_from_asymmetric(src); + SimpleTensor dst_tmp(src.shape(), DataType::F32); + for(int i = 0; i < src.num_elements(); ++i) + { + switch(op) + { + case ElementWiseUnary::RSQRT: + if(src_tmp[i] != 0) + { + dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]); + } + else + { + // rsqrt(0) give 'inf' so set to the maximum in uint8: 255 + dst_tmp[i] = (255.0f - dst.quantization_info().uniform().offset)* dst.quantization_info().uniform().scale; + } + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } + } + dst = convert_to_asymmetric(dst_tmp, dst.quantization_info()); + } + else + { + ARM_COMPUTE_ERROR("Not implemented"); + } + return dst; +} + +template SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op); +template SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op); +template SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op); -template SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary op); -template SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary op); -template SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary op); } // namespace reference } // namespace validation } // namespace test diff --git a/tests/validation/reference/ElementwiseUnary.h b/tests/validation/reference/ElementwiseUnary.h index be4a229a5b..ae7a49bce4 100644 --- a/tests/validation/reference/ElementwiseUnary.h +++ b/tests/validation/reference/ElementwiseUnary.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 Arm Limited. + * Copyright (c) 2018-2019, 2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -35,7 +35,7 @@ namespace validation namespace reference { template -SimpleTensor elementwise_unary(const SimpleTensor &src, ElementWiseUnary op); +SimpleTensor elementwise_unary(const SimpleTensor &src, SimpleTensor &dst, ElementWiseUnary op); } // namespace reference } // namespace validation } // namespace test -- cgit v1.2.1