// // 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