blob: b7ca3b34c0e48b2d0db0cef9570df738aa19be86 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
Jim Flynne242f2d2019-05-22 14:24:13 +01005
telsoa014fcda012018-03-09 14:13:49 +00006#pragma once
7
8#include <armnn/ArmNN.hpp>
9#include <armnn/TypesUtils.hpp>
10
Matthew Jackson9bff1442019-09-12 09:08:23 +010011#include <Half.hpp>
12
telsoa014fcda012018-03-09 14:13:49 +000013#include <initializer_list>
14#include <iterator>
15#include <vector>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010016
telsoa014fcda012018-03-09 14:13:49 +000017#include <boost/core/ignore_unused.hpp>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010018#include <boost/numeric/conversion/cast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000019
20template<typename T, bool DoQuantize=true>
21struct SelectiveQuantizer
22{
23 static T Quantize(float value, float scale, int32_t offset)
24 {
25 return armnn::Quantize<T>(value, scale, offset);
26 }
27
28 static float Dequantize(T value, float scale, int32_t offset)
29 {
30 return armnn::Dequantize(value, scale, offset);
31 }
32};
33
34template<typename T>
35struct SelectiveQuantizer<T, false>
36{
37 static T Quantize(float value, float scale, int32_t offset)
38 {
39 boost::ignore_unused(scale, offset);
40 return value;
41 }
42
43 static float Dequantize(T value, float scale, int32_t offset)
44 {
45 boost::ignore_unused(scale, offset);
46 return value;
47 }
48};
49
Matthew Jackson9bff1442019-09-12 09:08:23 +010050template<>
51struct SelectiveQuantizer<armnn::Half, false>
52{
53 static armnn::Half Quantize(float value, float scale, int32_t offset)
54 {
55 boost::ignore_unused(scale, offset);
56 return armnn::Half(value);
57 }
58
59 static float Dequantize(armnn::Half value, float scale, int32_t offset)
60 {
61 boost::ignore_unused(scale, offset);
62 return value;
63 }
64};
65
telsoa014fcda012018-03-09 14:13:49 +000066template<typename T>
67T SelectiveQuantize(float value, float scale, int32_t offset)
68{
69 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
70};
71
72template<typename T>
73float SelectiveDequantize(T value, float scale, int32_t offset)
74{
75 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
76};
77
78template<typename ItType>
79struct IsFloatingPointIterator
80{
81 static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
82};
83
84template <typename T, typename FloatIt,
telsoa01c577f2c2018-08-31 09:22:23 +010085typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
telsoa014fcda012018-03-09 14:13:49 +000086>
87std::vector<T> QuantizedVector(float qScale, int32_t qOffset, FloatIt first, FloatIt last)
88{
89 std::vector<T> quantized;
90 quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
91
92 for (auto it = first; it != last; ++it)
93 {
94 auto f = *it;
95 T q =SelectiveQuantize<T>(f, qScale, qOffset);
96 quantized.push_back(q);
97 }
98
99 return quantized;
100}
101
102template<typename T>
103std::vector<T> QuantizedVector(float qScale, int32_t qOffset, const std::vector<float>& array)
104{
105 return QuantizedVector<T>(qScale, qOffset, array.begin(), array.end());
106}
107
108template<typename T>
109std::vector<T> QuantizedVector(float qScale, int32_t qOffset, std::initializer_list<float> array)
110{
111 return QuantizedVector<T>(qScale, qOffset, array.begin(), array.end());
112}