blob: a0c6553e24a08d64e99710cd96853f667feb4a23 [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
11#include <initializer_list>
12#include <iterator>
13#include <vector>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010014
telsoa014fcda012018-03-09 14:13:49 +000015#include <boost/core/ignore_unused.hpp>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010016#include <boost/numeric/conversion/cast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000017
18template<typename T, bool DoQuantize=true>
19struct SelectiveQuantizer
20{
21 static T Quantize(float value, float scale, int32_t offset)
22 {
23 return armnn::Quantize<T>(value, scale, offset);
24 }
25
26 static float Dequantize(T value, float scale, int32_t offset)
27 {
28 return armnn::Dequantize(value, scale, offset);
29 }
30};
31
32template<typename T>
33struct SelectiveQuantizer<T, false>
34{
35 static T Quantize(float value, float scale, int32_t offset)
36 {
37 boost::ignore_unused(scale, offset);
38 return value;
39 }
40
41 static float Dequantize(T value, float scale, int32_t offset)
42 {
43 boost::ignore_unused(scale, offset);
44 return value;
45 }
46};
47
48template<typename T>
49T SelectiveQuantize(float value, float scale, int32_t offset)
50{
51 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
52};
53
54template<typename T>
55float SelectiveDequantize(T value, float scale, int32_t offset)
56{
57 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
58};
59
60template<typename ItType>
61struct IsFloatingPointIterator
62{
63 static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
64};
65
66template <typename T, typename FloatIt,
telsoa01c577f2c2018-08-31 09:22:23 +010067typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
telsoa014fcda012018-03-09 14:13:49 +000068>
69std::vector<T> QuantizedVector(float qScale, int32_t qOffset, FloatIt first, FloatIt last)
70{
71 std::vector<T> quantized;
72 quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
73
74 for (auto it = first; it != last; ++it)
75 {
76 auto f = *it;
77 T q =SelectiveQuantize<T>(f, qScale, qOffset);
78 quantized.push_back(q);
79 }
80
81 return quantized;
82}
83
84template<typename T>
85std::vector<T> QuantizedVector(float qScale, int32_t qOffset, const std::vector<float>& array)
86{
87 return QuantizedVector<T>(qScale, qOffset, array.begin(), array.end());
88}
89
90template<typename T>
91std::vector<T> QuantizedVector(float qScale, int32_t qOffset, std::initializer_list<float> array)
92{
93 return QuantizedVector<T>(qScale, qOffset, array.begin(), array.end());
94}