telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 5 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 6 | #pragma once |
| 7 | |
| 8 | #include <armnn/ArmNN.hpp> |
| 9 | #include <armnn/TypesUtils.hpp> |
| 10 | |
Matthew Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 11 | #include <Half.hpp> |
| 12 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | #include <initializer_list> |
| 14 | #include <iterator> |
| 15 | #include <vector> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | #include <boost/core/ignore_unused.hpp> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 18 | #include <boost/numeric/conversion/cast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 20 | namespace armnnUtils |
| 21 | { |
| 22 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | template<typename T, bool DoQuantize=true> |
| 24 | struct 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 | |
| 37 | template<typename T> |
| 38 | struct 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 Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 53 | template<> |
| 54 | struct 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | template<typename T> |
| 70 | T SelectiveQuantize(float value, float scale, int32_t offset) |
| 71 | { |
| 72 | return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset); |
| 73 | }; |
| 74 | |
| 75 | template<typename T> |
| 76 | float SelectiveDequantize(T value, float scale, int32_t offset) |
| 77 | { |
| 78 | return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset); |
| 79 | }; |
| 80 | |
| 81 | template<typename ItType> |
| 82 | struct IsFloatingPointIterator |
| 83 | { |
| 84 | static constexpr bool value=std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value; |
| 85 | }; |
| 86 | |
| 87 | template <typename T, typename FloatIt, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | typename std::enable_if<IsFloatingPointIterator<FloatIt>::value, int>::type=0 // Makes sure fp iterator is valid. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 89 | > |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 90 | std::vector<T> QuantizedVector(FloatIt first, FloatIt last, float qScale, int32_t qOffset) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | { |
| 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-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 98 | T q = SelectiveQuantize<T>(f, qScale, qOffset); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | quantized.push_back(q); |
| 100 | } |
| 101 | |
| 102 | return quantized; |
| 103 | } |
| 104 | |
| 105 | template<typename T> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 106 | std::vector<T> QuantizedVector(const std::vector<float>& array, float qScale = 1.f, int32_t qOffset = 0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | { |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 108 | return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | template<typename T> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 112 | std::vector<T> QuantizedVector(std::initializer_list<float> array, float qScale = 1.f, int32_t qOffset = 0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | { |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 114 | return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | } |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 116 | |
| 117 | } // namespace armnnUtils |