blob: a7f68c5ef0a6a5fb5d51a656811dc5d535b1b54c [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
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010020namespace armnnUtils
21{
22
telsoa014fcda012018-03-09 14:13:49 +000023template<typename T, bool DoQuantize=true>
24struct SelectiveQuantizer
25{
26 static T Quantize(float value, float scale, int32_t offset)
27 {
28 return armnn::Quantize<T>(value, scale, offset);
29 }
30
31 static float Dequantize(T value, float scale, int32_t offset)
32 {
33 return armnn::Dequantize(value, scale, offset);
34 }
35};
36
37template<typename T>
38struct SelectiveQuantizer<T, false>
39{
40 static T Quantize(float value, float scale, int32_t offset)
41 {
42 boost::ignore_unused(scale, offset);
43 return value;
44 }
45
46 static float Dequantize(T value, float scale, int32_t offset)
47 {
48 boost::ignore_unused(scale, offset);
49 return value;
50 }
51};
52
Matthew Jackson9bff1442019-09-12 09:08:23 +010053template<>
54struct SelectiveQuantizer<armnn::Half, false>
55{
56 static armnn::Half Quantize(float value, float scale, int32_t offset)
57 {
58 boost::ignore_unused(scale, offset);
59 return armnn::Half(value);
60 }
61
62 static float Dequantize(armnn::Half value, float scale, int32_t offset)
63 {
64 boost::ignore_unused(scale, offset);
65 return value;
66 }
67};
68
telsoa014fcda012018-03-09 14:13:49 +000069template<typename T>
70T SelectiveQuantize(float value, float scale, int32_t offset)
71{
72 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
73};
74
75template<typename T>
76float SelectiveDequantize(T value, float scale, int32_t offset)
77{
78 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
79};
80
81template<typename ItType>
82struct IsFloatingPointIterator
83{
84 static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
85};
86
87template <typename T, typename FloatIt,
telsoa01c577f2c2018-08-31 09:22:23 +010088typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
telsoa014fcda012018-03-09 14:13:49 +000089>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010090std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
telsoa014fcda012018-03-09 14:13:49 +000091{
92 std::vector<T> quantized;
93 quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
94
95 for (auto it = first; it != last; ++it)
96 {
97 auto f = *it;
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010098 T q = SelectiveQuantize<T>(f, qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +000099 quantized.push_back(q);
100 }
101
102 return quantized;
103}
104
105template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100106std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000107{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100108 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000109}
110
111template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100112std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000113{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100114 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000115}
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100116
117} // namespace armnnUtils