aboutsummaryrefslogtreecommitdiff
path: root/src/armnnUtils
diff options
context:
space:
mode:
authorAron Virginas-Tar <Aron.Virginas-Tar@arm.com>2019-10-22 10:00:28 +0100
committerMatteo Martincigh <matteo.martincigh@arm.com>2019-10-25 15:01:24 +0000
commit48623a0f6f4681ce0d9525b1587b7f96bfd58519 (patch)
treef5dbf25937e7b6641274e0953d09ca84acb51772 /src/armnnUtils
parentc82c8732fb514b412012002bd951a84039eca696 (diff)
downloadarmnn-48623a0f6f4681ce0d9525b1587b7f96bfd58519.tar.gz
IVGCVSW-4018 Move QuantizeHelper.hpp to armnnUtils
* Moved QuntizeHelper.hpp to armnnUtils * Reordered parameters for QuantizedVector and added default values for qScale and qOffset to make life easier when using the function for non-quantized types such as Float16 Signed-off-by: Aron Virginas-Tar <Aron.Virginas-Tar@arm.com> Change-Id: I28c263dfa425f1316feccb4116839a84f5d568e5
Diffstat (limited to 'src/armnnUtils')
-rw-r--r--src/armnnUtils/QuantizeHelper.hpp117
-rw-r--r--src/armnnUtils/test/QuantizeHelperTest.cpp46
2 files changed, 163 insertions, 0 deletions
diff --git a/src/armnnUtils/QuantizeHelper.hpp b/src/armnnUtils/QuantizeHelper.hpp
new file mode 100644
index 0000000000..a7f68c5ef0
--- /dev/null
+++ b/src/armnnUtils/QuantizeHelper.hpp
@@ -0,0 +1,117 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <armnn/ArmNN.hpp>
+#include <armnn/TypesUtils.hpp>
+
+#include <Half.hpp>
+
+#include <initializer_list>
+#include <iterator>
+#include <vector>
+
+#include <boost/core/ignore_unused.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+
+namespace armnnUtils
+{
+
+template<typename T, bool DoQuantize=true>
+struct SelectiveQuantizer
+{
+ static T Quantize(float value, float scale, int32_t offset)
+ {
+ return armnn::Quantize<T>(value, scale, offset);
+ }
+
+ static float Dequantize(T value, float scale, int32_t offset)
+ {
+ return armnn::Dequantize(value, scale, offset);
+ }
+};
+
+template<typename T>
+struct SelectiveQuantizer<T, false>
+{
+ static T Quantize(float value, float scale, int32_t offset)
+ {
+ boost::ignore_unused(scale, offset);
+ return value;
+ }
+
+ static float Dequantize(T value, float scale, int32_t offset)
+ {
+ boost::ignore_unused(scale, offset);
+ return value;
+ }
+};
+
+template<>
+struct SelectiveQuantizer<armnn::Half, false>
+{
+ static armnn::Half Quantize(float value, float scale, int32_t offset)
+ {
+ boost::ignore_unused(scale, offset);
+ return armnn::Half(value);
+ }
+
+ static float Dequantize(armnn::Half value, float scale, int32_t offset)
+ {
+ boost::ignore_unused(scale, offset);
+ return value;
+ }
+};
+
+template<typename T>
+T SelectiveQuantize(float value, float scale, int32_t offset)
+{
+ return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
+};
+
+template<typename T>
+float SelectiveDequantize(T value, float scale, int32_t offset)
+{
+ return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
+};
+
+template<typename ItType>
+struct IsFloatingPointIterator
+{
+ static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
+};
+
+template <typename T, typename FloatIt,
+typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
+>
+std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
+{
+ std::vector<T> quantized;
+ quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
+
+ for (auto it = first; it != last; ++it)
+ {
+ auto f = *it;
+ T q = SelectiveQuantize<T>(f, qScale, qOffset);
+ quantized.push_back(q);
+ }
+
+ return quantized;
+}
+
+template<typename T>
+std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
+{
+ return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
+}
+
+template<typename T>
+std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
+{
+ return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
+}
+
+} // namespace armnnUtils
diff --git a/src/armnnUtils/test/QuantizeHelperTest.cpp b/src/armnnUtils/test/QuantizeHelperTest.cpp
new file mode 100644
index 0000000000..7e781d0b5d
--- /dev/null
+++ b/src/armnnUtils/test/QuantizeHelperTest.cpp
@@ -0,0 +1,46 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include <QuantizeHelper.hpp>
+
+#include <boost/core/ignore_unused.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include <vector>
+
+BOOST_AUTO_TEST_SUITE(QuantizeHelper)
+
+namespace
+{
+
+template<typename T>
+bool IsFloatIterFunc(T iter)
+{
+ boost::ignore_unused(iter);
+ return armnnUtils::IsFloatingPointIterator<T>::value;
+}
+
+} // anonymous namespace
+
+BOOST_AUTO_TEST_CASE(IsFloatIterFuncTest)
+{
+ std::vector<float> fArray;
+ BOOST_TEST(IsFloatIterFunc(fArray.begin()) == true);
+ BOOST_TEST(IsFloatIterFunc(fArray.cbegin()) == true);
+
+ std::vector<double> dArray;
+ BOOST_TEST(IsFloatIterFunc(dArray.begin()) == true);
+
+ std::vector<int> iArray;
+ BOOST_TEST(IsFloatIterFunc(iArray.begin()) == false);
+
+ float floats[5];
+ BOOST_TEST(IsFloatIterFunc(&floats[0]) == true);
+
+ int ints[5];
+ BOOST_TEST(IsFloatIterFunc(&ints[0]) == false);
+}
+
+BOOST_AUTO_TEST_SUITE_END()