ArmNN  NotReleased
QuantizeHelper.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/TypesUtils.hpp>
9 
10 #include <Half.hpp>
11 
12 #include <initializer_list>
13 #include <iterator>
14 #include <vector>
15 
16 #include <boost/core/ignore_unused.hpp>
17 #include <boost/numeric/conversion/cast.hpp>
18 
19 namespace armnnUtils
20 {
21 
22 template<typename T, bool DoQuantize=true>
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 
36 template<typename T>
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 
52 template<>
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 
68 template<typename T>
69 T SelectiveQuantize(float value, float scale, int32_t offset)
70 {
71  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
72 };
73 
74 template<typename T>
75 float SelectiveDequantize(T value, float scale, int32_t offset)
76 {
77  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
78 };
79 
80 template<typename ItType>
82 {
83  static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
84 };
85 
86 template <typename T, typename FloatIt,
87 typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
88 >
89 std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
90 {
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;
97  T q = SelectiveQuantize<T>(f, qScale, qOffset);
98  quantized.push_back(q);
99  }
100 
101  return quantized;
102 }
103 
104 template<typename T>
105 std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
106 {
107  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
108 }
109 
110 template<typename T>
111 std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
112 {
113  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
114 }
115 
116 } // namespace armnnUtils
static armnn::Half Quantize(float value, float scale, int32_t offset)
half_float::half Half
Definition: Half.hpp:16
DataLayout::NHWC false
float Dequantize(QuantizedType value, float scale, int32_t offset)
Definition: TypesUtils.cpp:47
std::vector< T > QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
static float Dequantize(T value, float scale, int32_t offset)
static T Quantize(float value, float scale, int32_t offset)
static T Quantize(float value, float scale, int32_t offset)
static float Dequantize(T value, float scale, int32_t offset)
static float Dequantize(armnn::Half value, float scale, int32_t offset)
float SelectiveDequantize(T value, float scale, int32_t offset)
T SelectiveQuantize(float value, float scale, int32_t offset)