blob: 061c459156fba27ff373e09ed54cddc97812b992 [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
telsoa014fcda012018-03-09 14:13:49 +00008#include <armnn/TypesUtils.hpp>
9
Matthew Jackson9bff1442019-09-12 09:08:23 +010010#include <Half.hpp>
11
telsoa014fcda012018-03-09 14:13:49 +000012#include <initializer_list>
13#include <iterator>
14#include <vector>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010015
telsoa014fcda012018-03-09 14:13:49 +000016#include <boost/core/ignore_unused.hpp>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010017#include <boost/numeric/conversion/cast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000018
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010019namespace armnnUtils
20{
21
telsoa014fcda012018-03-09 14:13:49 +000022template<typename T, bool DoQuantize=true>
23struct SelectiveQuantizer
24{
25 static T Quantize(float value, float scale, int32_t offset)
26 {
27 return armnn::Quantize<T>(value, scale, offset);
28 }
29
30 static float Dequantize(T value, float scale, int32_t offset)
31 {
32 return armnn::Dequantize(value, scale, offset);
33 }
34};
35
36template<typename T>
37struct SelectiveQuantizer<T, false>
38{
39 static T Quantize(float value, float scale, int32_t offset)
40 {
41 boost::ignore_unused(scale, offset);
42 return value;
43 }
44
45 static float Dequantize(T value, float scale, int32_t offset)
46 {
47 boost::ignore_unused(scale, offset);
48 return value;
49 }
50};
51
Matthew Jackson9bff1442019-09-12 09:08:23 +010052template<>
53struct SelectiveQuantizer<armnn::Half, false>
54{
55 static armnn::Half Quantize(float value, float scale, int32_t offset)
56 {
57 boost::ignore_unused(scale, offset);
58 return armnn::Half(value);
59 }
60
61 static float Dequantize(armnn::Half value, float scale, int32_t offset)
62 {
63 boost::ignore_unused(scale, offset);
64 return value;
65 }
66};
67
telsoa014fcda012018-03-09 14:13:49 +000068template<typename T>
69T SelectiveQuantize(float value, float scale, int32_t offset)
70{
71 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
72};
73
74template<typename T>
75float SelectiveDequantize(T value, float scale, int32_t offset)
76{
77 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
78};
79
80template<typename ItType>
81struct IsFloatingPointIterator
82{
83 static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
84};
85
86template <typename T, typename FloatIt,
telsoa01c577f2c2018-08-31 09:22:23 +010087typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
telsoa014fcda012018-03-09 14:13:49 +000088>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010089std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
telsoa014fcda012018-03-09 14:13:49 +000090{
91 std::vector<T> quantized;
92 quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
93
94 for (auto it = first; it != last; ++it)
95 {
96 auto f = *it;
Aron Virginas-Tar48623a02019-10-22 10:00:28 +010097 T q = SelectiveQuantize<T>(f, qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +000098 quantized.push_back(q);
99 }
100
101 return quantized;
102}
103
104template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100105std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000106{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100107 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000108}
109
110template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100111std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000112{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100113 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000114}
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100115
116} // namespace armnnUtils