blob: 231b8411cb7fb9b9d4472e3e0bfc40e52489dced [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
Jan Eilers8eb25602020-03-09 12:13:48 +00008#include <armnn/utility/IgnoreUnused.hpp>
Matthew Sloyan0663d662020-09-14 11:47:26 +01009#include <armnn/utility/NumericCast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000010#include <armnn/TypesUtils.hpp>
11
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000012#include <BFloat16.hpp>
Matthew Jackson9bff1442019-09-12 09:08:23 +010013#include <Half.hpp>
14
telsoa014fcda012018-03-09 14:13:49 +000015#include <initializer_list>
16#include <iterator>
17#include <vector>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010018
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 {
Jan Eilers8eb25602020-03-09 12:13:48 +000041 armnn::IgnoreUnused(scale, offset);
telsoa014fcda012018-03-09 14:13:49 +000042 return value;
43 }
44
45 static float Dequantize(T value, float scale, int32_t offset)
46 {
Jan Eilers8eb25602020-03-09 12:13:48 +000047 armnn::IgnoreUnused(scale, offset);
telsoa014fcda012018-03-09 14:13:49 +000048 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 {
Jan Eilers8eb25602020-03-09 12:13:48 +000057 armnn::IgnoreUnused(scale, offset);
Matthew Jackson9bff1442019-09-12 09:08:23 +010058 return armnn::Half(value);
59 }
60
61 static float Dequantize(armnn::Half value, float scale, int32_t offset)
62 {
Jan Eilers8eb25602020-03-09 12:13:48 +000063 armnn::IgnoreUnused(scale, offset);
Matthew Jackson9bff1442019-09-12 09:08:23 +010064 return value;
65 }
66};
67
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000068template<>
69struct SelectiveQuantizer<armnn::BFloat16, false>
70{
71 static armnn::BFloat16 Quantize(float value, float scale, int32_t offset)
72 {
73 armnn::IgnoreUnused(scale, offset);
74 return armnn::BFloat16(value);
75 }
76
77 static float Dequantize(armnn::BFloat16 value, float scale, int32_t offset)
78 {
79 armnn::IgnoreUnused(scale, offset);
80 return value;
81 }
82};
83
telsoa014fcda012018-03-09 14:13:49 +000084template<typename T>
85T SelectiveQuantize(float value, float scale, int32_t offset)
86{
87 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
88};
89
90template<typename T>
91float SelectiveDequantize(T value, float scale, int32_t offset)
92{
93 return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
94};
95
96template<typename ItType>
97struct IsFloatingPointIterator
98{
99 static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
100};
101
102template <typename T, typename FloatIt,
telsoa01c577f2c2018-08-31 09:22:23 +0100103typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
telsoa014fcda012018-03-09 14:13:49 +0000104>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100105std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
telsoa014fcda012018-03-09 14:13:49 +0000106{
107 std::vector<T> quantized;
Matthew Sloyan0663d662020-09-14 11:47:26 +0100108 quantized.reserve(armnn::numeric_cast<size_t>(std::distance(first, last)));
telsoa014fcda012018-03-09 14:13:49 +0000109
110 for (auto it = first; it != last; ++it)
111 {
112 auto f = *it;
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100113 T q = SelectiveQuantize<T>(f, qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000114 quantized.push_back(q);
115 }
116
117 return quantized;
118}
119
120template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100121std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000122{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100123 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000124}
125
126template<typename T>
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100127std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
telsoa014fcda012018-03-09 14:13:49 +0000128{
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100129 return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
telsoa014fcda012018-03-09 14:13:49 +0000130}
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100131
132} // namespace armnnUtils