ArmNN
 20.02
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 
9 #include <armnn/TypesUtils.hpp>
10 
11 #include <BFloat16.hpp>
12 #include <Half.hpp>
13 
14 #include <initializer_list>
15 #include <iterator>
16 #include <vector>
17 
18 #include <boost/numeric/conversion/cast.hpp>
19 
20 namespace armnnUtils
21 {
22 
23 template<typename T, bool DoQuantize=true>
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 
37 template<typename T>
39 {
40  static T Quantize(float value, float scale, int32_t offset)
41  {
42  armnn::IgnoreUnused(scale, offset);
43  return value;
44  }
45 
46  static float Dequantize(T value, float scale, int32_t offset)
47  {
48  armnn::IgnoreUnused(scale, offset);
49  return value;
50  }
51 };
52 
53 template<>
55 {
56  static armnn::Half Quantize(float value, float scale, int32_t offset)
57  {
58  armnn::IgnoreUnused(scale, offset);
59  return armnn::Half(value);
60  }
61 
62  static float Dequantize(armnn::Half value, float scale, int32_t offset)
63  {
64  armnn::IgnoreUnused(scale, offset);
65  return value;
66  }
67 };
68 
69 template<>
71 {
72  static armnn::BFloat16 Quantize(float value, float scale, int32_t offset)
73  {
74  armnn::IgnoreUnused(scale, offset);
75  return armnn::BFloat16(value);
76  }
77 
78  static float Dequantize(armnn::BFloat16 value, float scale, int32_t offset)
79  {
80  armnn::IgnoreUnused(scale, offset);
81  return value;
82  }
83 };
84 
85 template<typename T>
86 T SelectiveQuantize(float value, float scale, int32_t offset)
87 {
88  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
89 };
90 
91 template<typename T>
92 float SelectiveDequantize(T value, float scale, int32_t offset)
93 {
94  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
95 };
96 
97 template<typename ItType>
99 {
100  static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value;
101 };
102 
103 template <typename T, typename FloatIt,
104 typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid.
105 >
106 std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
107 {
108  std::vector<T> quantized;
109  quantized.reserve(boost::numeric_cast<size_t>(std::distance(first, last)));
110 
111  for (auto it = first; it != last; ++it)
112  {
113  auto f = *it;
114  T q = SelectiveQuantize<T>(f, qScale, qOffset);
115  quantized.push_back(q);
116  }
117 
118  return quantized;
119 }
120 
121 template<typename T>
122 std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0)
123 {
124  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
125 }
126 
127 template<typename T>
128 std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0)
129 {
130  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
131 }
132 
133 } // namespace armnnUtils
float Dequantize(QuantizedType value, float scale, int32_t offset)
Dequantize an 8-bit data type into a floating point data type.
Definition: TypesUtils.cpp:47
T SelectiveQuantize(float value, float scale, int32_t offset)
static T Quantize(float value, float scale, int32_t offset)
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
DataLayout::NHWC false
static float Dequantize(armnn::Half value, float scale, int32_t offset)
std::vector< T > QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset)
static armnn::BFloat16 Quantize(float value, float scale, int32_t offset)
static float Dequantize(T value, float scale, int32_t offset)
static float Dequantize(armnn::BFloat16 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)
float SelectiveDequantize(T value, float scale, int32_t offset)
half_float::half Half
Definition: Half.hpp:16
static armnn::Half Quantize(float value, float scale, int32_t offset)