From c42a987aa53d0fd842c34dee90abef5a9ff15fa4 Mon Sep 17 00:00:00 2001 From: Colm Donelan Date: Wed, 2 Feb 2022 16:35:09 +0000 Subject: IVGCVSW-6635 Move MemCopyTestImpl from acl to armnnTestUtils. * Move MemCopyTestImpl.hpp from src/backends/aclCommon/test/ to include/armnnTestutils. * Refactor MemCopyTests in aclCommon, cl and Neon. * Introduce RefMemCopyTests to exercise this utility in x86 builds. Signed-off-by: Colm Donelan Change-Id: I8824f013d3656658ed0a2904bb79384e3af68641 --- include/armnnUtils/QuantizeHelper.hpp | 132 ++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 include/armnnUtils/QuantizeHelper.hpp (limited to 'include/armnnUtils/QuantizeHelper.hpp') diff --git a/include/armnnUtils/QuantizeHelper.hpp b/include/armnnUtils/QuantizeHelper.hpp new file mode 100644 index 0000000000..231b8411cb --- /dev/null +++ b/include/armnnUtils/QuantizeHelper.hpp @@ -0,0 +1,132 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace armnnUtils +{ + +template +struct SelectiveQuantizer +{ + static T Quantize(float value, float scale, int32_t offset) + { + return armnn::Quantize(value, scale, offset); + } + + static float Dequantize(T value, float scale, int32_t offset) + { + return armnn::Dequantize(value, scale, offset); + } +}; + +template +struct SelectiveQuantizer +{ + static T Quantize(float value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return value; + } + + static float Dequantize(T value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return value; + } +}; + +template<> +struct SelectiveQuantizer +{ + static armnn::Half Quantize(float value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return armnn::Half(value); + } + + static float Dequantize(armnn::Half value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return value; + } +}; + +template<> +struct SelectiveQuantizer +{ + static armnn::BFloat16 Quantize(float value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return armnn::BFloat16(value); + } + + static float Dequantize(armnn::BFloat16 value, float scale, int32_t offset) + { + armnn::IgnoreUnused(scale, offset); + return value; + } +}; + +template +T SelectiveQuantize(float value, float scale, int32_t offset) +{ + return SelectiveQuantizer()>::Quantize(value, scale, offset); +}; + +template +float SelectiveDequantize(T value, float scale, int32_t offset) +{ + return SelectiveQuantizer()>::Dequantize(value, scale, offset); +}; + +template +struct IsFloatingPointIterator +{ + static constexpr bool value=std::is_floating_point::value_type>::value; +}; + +template ::value, int>::type=0 // Makes sure fp iterator is valid. +> +std::vector QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset) +{ + std::vector quantized; + quantized.reserve(armnn::numeric_cast(std::distance(first, last))); + + for (auto it = first; it != last; ++it) + { + auto f = *it; + T q = SelectiveQuantize(f, qScale, qOffset); + quantized.push_back(q); + } + + return quantized; +} + +template +std::vector QuantizedVector(const std::vector& array, float qScale = 1.f, int32_t qOffset = 0) +{ + return QuantizedVector(array.begin(), array.end(), qScale, qOffset); +} + +template +std::vector QuantizedVector(std::initializer_list array, float qScale = 1.f, int32_t qOffset = 0) +{ + return QuantizedVector(array.begin(), array.end(), qScale, qOffset); +} + +} // namespace armnnUtils -- cgit v1.2.1