Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Bentham | 246bd46 | 2020-01-20 16:16:06 +0000 | [diff] [blame] | 8 | #include <armnn/TypesUtils.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 9 | #include <armnn/utility/Assert.hpp> |
| 10 | #include <armnn/utility/IgnoreUnused.hpp> |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 11 | #include <armnn/utility/NumericCast.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 12 | #include <armnnUtils/FloatingPointConverter.hpp> |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 13 | #include <armnnUtils/TensorUtils.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 14 | |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 15 | #include <ResolveType.hpp> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | class BaseIterator |
| 21 | { |
| 22 | public: |
| 23 | BaseIterator() {} |
| 24 | |
| 25 | virtual ~BaseIterator() {} |
| 26 | |
| 27 | virtual BaseIterator& operator++() = 0; |
| 28 | |
| 29 | virtual BaseIterator& operator+=(const unsigned int increment) = 0; |
| 30 | |
| 31 | virtual BaseIterator& operator-=(const unsigned int increment) = 0; |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 32 | |
| 33 | virtual BaseIterator& operator[](const unsigned int index) = 0; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 36 | template<typename IType> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 37 | class Decoder : public BaseIterator |
| 38 | { |
| 39 | public: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 40 | Decoder() {} |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 41 | |
| 42 | virtual ~Decoder() {} |
| 43 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 44 | virtual void Reset(void*) = 0; |
| 45 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 46 | virtual IType Get() const = 0; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 47 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 48 | virtual std::vector<float> |
| 49 | DecodeTensor(const TensorShape &tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 50 | bool isDepthwise = false) = 0; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 53 | template<typename IType> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 54 | class Encoder : public BaseIterator |
| 55 | { |
| 56 | public: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 57 | Encoder() {} |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 58 | |
| 59 | virtual ~Encoder() {} |
| 60 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 61 | virtual void Reset(void*) = 0; |
| 62 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 63 | virtual void Set(IType right) = 0; |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 64 | |
| 65 | virtual IType Get() const = 0; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | template<typename T, typename Base> |
| 69 | class TypedIterator : public Base |
| 70 | { |
| 71 | public: |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 72 | TypedIterator(T* data = nullptr) |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 73 | : m_Iterator(data), m_Start(data) |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 74 | {} |
| 75 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 76 | void Reset(void* data) override |
| 77 | { |
| 78 | m_Iterator = reinterpret_cast<T*>(data); |
| 79 | m_Start = m_Iterator; |
| 80 | } |
| 81 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 82 | TypedIterator& operator++() override |
| 83 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 84 | ARMNN_ASSERT(m_Iterator); |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 85 | ++m_Iterator; |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | TypedIterator& operator+=(const unsigned int increment) override |
| 90 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 91 | ARMNN_ASSERT(m_Iterator); |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 92 | m_Iterator += increment; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | TypedIterator& operator-=(const unsigned int increment) override |
| 97 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 98 | ARMNN_ASSERT(m_Iterator); |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 99 | m_Iterator -= increment; |
| 100 | return *this; |
| 101 | } |
| 102 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 103 | TypedIterator& operator[](const unsigned int index) override |
| 104 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 105 | ARMNN_ASSERT(m_Iterator); |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 106 | m_Iterator = m_Start + index; |
| 107 | return *this; |
| 108 | } |
| 109 | |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 110 | protected: |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 111 | T* m_Iterator; |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 112 | T* m_Start; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 115 | class QASymm8Decoder : public TypedIterator<const uint8_t, Decoder<float>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 116 | { |
| 117 | public: |
| 118 | QASymm8Decoder(const uint8_t* data, const float scale, const int32_t offset) |
| 119 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 120 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 121 | QASymm8Decoder(const float scale, const int32_t offset) |
| 122 | : QASymm8Decoder(nullptr, scale, offset) {} |
| 123 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 124 | float Get() const override |
| 125 | { |
| 126 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 127 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 128 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 129 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 130 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 131 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 132 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 133 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 134 | std::vector<float> decodedTensor; |
| 135 | decodedTensor.reserve(size); |
| 136 | |
| 137 | for (uint32_t i = 0; i < size; ++i) |
| 138 | { |
| 139 | this->operator[](i); |
| 140 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scale, m_Offset)); |
| 141 | } |
| 142 | |
| 143 | return decodedTensor; |
| 144 | } |
| 145 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 146 | private: |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 147 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 148 | const float m_Scale; |
| 149 | const int32_t m_Offset; |
| 150 | }; |
| 151 | |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 152 | class QASymmS8Decoder : public TypedIterator<const int8_t, Decoder<float>> |
| 153 | { |
| 154 | public: |
| 155 | QASymmS8Decoder(const int8_t* data, const float scale, const int32_t offset) |
| 156 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 157 | |
| 158 | QASymmS8Decoder(const float scale, const int32_t offset) |
| 159 | : QASymmS8Decoder(nullptr, scale, offset) {} |
| 160 | |
| 161 | float Get() const override |
| 162 | { |
| 163 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 164 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 165 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 166 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 167 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 168 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 169 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 170 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 171 | std::vector<float> decodedTensor; |
| 172 | decodedTensor.reserve(size); |
| 173 | |
| 174 | for (uint32_t i = 0; i < size; ++i) |
| 175 | { |
| 176 | this->operator[](i); |
| 177 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scale, m_Offset)); |
| 178 | } |
| 179 | |
| 180 | return decodedTensor; |
| 181 | } |
| 182 | |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 183 | private: |
| 184 | const float m_Scale; |
| 185 | const int32_t m_Offset; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 186 | |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 189 | class QSymmS8Decoder : public TypedIterator<const int8_t, Decoder<float>> |
| 190 | { |
| 191 | public: |
| 192 | QSymmS8Decoder(const int8_t* data, const float scale, const int32_t offset) |
| 193 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 194 | |
| 195 | QSymmS8Decoder(const float scale, const int32_t offset) |
| 196 | : QSymmS8Decoder(nullptr, scale, offset) {} |
| 197 | |
| 198 | float Get() const override |
| 199 | { |
| 200 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 201 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 202 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 203 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 204 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 205 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 206 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 207 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 208 | std::vector<float> decodedTensor; |
| 209 | decodedTensor.reserve(size); |
| 210 | |
| 211 | for (uint32_t i = 0; i < size; ++i) |
| 212 | { |
| 213 | this->operator[](i); |
| 214 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scale, m_Offset)); |
| 215 | } |
| 216 | |
| 217 | return decodedTensor; |
| 218 | } |
| 219 | |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 220 | private: |
| 221 | const float m_Scale; |
| 222 | const int32_t m_Offset; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 223 | |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 226 | class QSymm16Decoder : public TypedIterator<const int16_t, Decoder<float>> |
Sadik Armagan | 2999a02 | 2019-04-09 14:20:12 +0100 | [diff] [blame] | 227 | { |
| 228 | public: |
| 229 | QSymm16Decoder(const int16_t* data, const float scale, const int32_t offset) |
| 230 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 231 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 232 | QSymm16Decoder(const float scale, const int32_t offset) |
| 233 | : QSymm16Decoder(nullptr, scale, offset) {} |
| 234 | |
Sadik Armagan | 2999a02 | 2019-04-09 14:20:12 +0100 | [diff] [blame] | 235 | float Get() const override |
| 236 | { |
| 237 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 238 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 239 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 240 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 241 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 242 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 243 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 244 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 245 | std::vector<float> decodedTensor; |
| 246 | decodedTensor.reserve(size); |
| 247 | |
| 248 | for (uint32_t i = 0; i < size; ++i) |
| 249 | { |
| 250 | this->operator[](i); |
| 251 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scale, m_Offset)); |
| 252 | } |
| 253 | |
| 254 | return decodedTensor; |
| 255 | } |
| 256 | |
Sadik Armagan | 2999a02 | 2019-04-09 14:20:12 +0100 | [diff] [blame] | 257 | private: |
| 258 | const float m_Scale; |
| 259 | const int32_t m_Offset; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 260 | |
Sadik Armagan | 2999a02 | 2019-04-09 14:20:12 +0100 | [diff] [blame] | 261 | }; |
| 262 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 263 | class Float16Decoder : public TypedIterator<const Half, Decoder<float>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 264 | { |
| 265 | public: |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 266 | Float16Decoder(const Half* data) |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 267 | : TypedIterator(data) {} |
| 268 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 269 | Float16Decoder() |
| 270 | : Float16Decoder(nullptr) {} |
| 271 | |
| 272 | float Get() const override |
| 273 | { |
| 274 | float val = 0.f; |
| 275 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 276 | return val; |
| 277 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 278 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 279 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 280 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 281 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 282 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 283 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 284 | std::vector<float> decodedTensor; |
| 285 | decodedTensor.reserve(size); |
| 286 | |
| 287 | for (uint32_t i = 0; i < size; ++i) |
| 288 | { |
| 289 | float val = 0.f; |
| 290 | this->operator[](i); |
| 291 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 292 | decodedTensor.emplace_back(val); |
| 293 | } |
| 294 | |
| 295 | return decodedTensor; |
| 296 | } |
| 297 | |
| 298 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | class Float32Decoder : public TypedIterator<const float, Decoder<float>> |
| 302 | { |
| 303 | public: |
| 304 | Float32Decoder(const float* data) |
| 305 | : TypedIterator(data) {} |
| 306 | |
| 307 | Float32Decoder() |
| 308 | : Float32Decoder(nullptr) {} |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 309 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 310 | float Get() const override |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 311 | { |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 312 | return *m_Iterator; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 313 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 314 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 315 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 316 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 317 | IgnoreUnused(isDepthwise); |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 318 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 319 | std::vector<float> decodedTensor; |
| 320 | |
| 321 | decodedTensor.reserve(size); |
| 322 | decodedTensor.assign(m_Start, m_Start + size); |
| 323 | |
| 324 | return decodedTensor; |
| 325 | } |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 326 | }; |
| 327 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 328 | class ScaledInt32Decoder : public TypedIterator<const int32_t, Decoder<float>> |
| 329 | { |
| 330 | public: |
| 331 | ScaledInt32Decoder(const int32_t* data, const float scale) |
| 332 | : TypedIterator(data), m_Scale(scale) {} |
| 333 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 334 | ScaledInt32Decoder(const float scale) |
| 335 | : ScaledInt32Decoder(nullptr, scale) {} |
| 336 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 337 | float Get() const override |
| 338 | { |
| 339 | return static_cast<float>(*m_Iterator) * m_Scale; |
| 340 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 341 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 342 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 343 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 344 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 345 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 346 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 347 | std::vector<float> decodedTensor; |
| 348 | decodedTensor.reserve(size); |
| 349 | |
| 350 | for (uint32_t i = 0; i < size; ++i) |
| 351 | { |
| 352 | this->operator[](i); |
| 353 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator) * m_Scale); |
| 354 | } |
| 355 | |
| 356 | return decodedTensor; |
| 357 | } |
| 358 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 359 | private: |
| 360 | const float m_Scale; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 361 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 364 | class Int32Decoder : public TypedIterator<const int32_t, Decoder<float>> |
| 365 | { |
| 366 | public: |
| 367 | Int32Decoder(const int32_t* data) |
| 368 | : TypedIterator(data) {} |
| 369 | |
| 370 | Int32Decoder() |
| 371 | : Int32Decoder(nullptr) {} |
| 372 | |
| 373 | float Get() const override |
| 374 | { |
| 375 | return static_cast<float>(*m_Iterator); |
| 376 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 377 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 378 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 379 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 380 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 381 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 382 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 383 | std::vector<float> decodedTensor; |
| 384 | decodedTensor.reserve(size); |
| 385 | |
| 386 | for (uint32_t i = 0; i < size; ++i) |
| 387 | { |
| 388 | this->operator[](i); |
| 389 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator)); |
| 390 | } |
| 391 | |
| 392 | return decodedTensor; |
| 393 | } |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 394 | }; |
| 395 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 396 | class Int32ToInt32tDecoder : public TypedIterator<const int32_t, Decoder<int32_t>> |
| 397 | { |
| 398 | public: |
| 399 | Int32ToInt32tDecoder(const int32_t* data) |
| 400 | : TypedIterator(data){} |
| 401 | |
| 402 | Int32ToInt32tDecoder() |
| 403 | : Int32ToInt32tDecoder(nullptr) {} |
| 404 | |
| 405 | int32_t Get() const override |
| 406 | { |
| 407 | return *m_Iterator; |
| 408 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 409 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 410 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 411 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 412 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 413 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 414 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 415 | std::vector<float> decodedTensor; |
| 416 | decodedTensor.reserve(size); |
| 417 | |
| 418 | for (uint32_t i = 0; i < size; ++i) |
| 419 | { |
| 420 | this->operator[](i); |
Rob Hughes | c013bc8 | 2021-07-14 09:31:31 +0100 | [diff] [blame] | 421 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return decodedTensor; |
| 425 | } |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 426 | }; |
| 427 | |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 428 | class BooleanDecoder : public TypedIterator<const uint8_t, Decoder<float>> |
| 429 | { |
| 430 | public: |
| 431 | BooleanDecoder(const uint8_t* data) |
| 432 | : TypedIterator(data) {} |
| 433 | |
| 434 | BooleanDecoder() |
| 435 | : BooleanDecoder(nullptr) {} |
| 436 | |
| 437 | float Get() const override |
| 438 | { |
| 439 | return *m_Iterator; |
| 440 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 441 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 442 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 443 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 444 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 445 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 446 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 447 | std::vector<float> decodedTensor; |
| 448 | decodedTensor.reserve(size); |
| 449 | |
| 450 | for (uint32_t i = 0; i < size; ++i) |
| 451 | { |
| 452 | this->operator[](i); |
| 453 | decodedTensor.emplace_back(*m_Iterator); |
| 454 | } |
| 455 | |
| 456 | return decodedTensor; |
| 457 | } |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 460 | class BooleanDecoderBool : public TypedIterator<const uint8_t, Decoder<bool>> |
| 461 | { |
| 462 | public: |
| 463 | BooleanDecoderBool(const uint8_t* data) |
| 464 | : TypedIterator(data) {} |
| 465 | |
| 466 | BooleanDecoderBool() |
| 467 | : BooleanDecoderBool(nullptr) {} |
| 468 | |
| 469 | bool Get() const override |
| 470 | { |
| 471 | return *m_Iterator; |
| 472 | } |
| 473 | |
| 474 | std::vector<float> DecodeTensor(const TensorShape& tensorShape, |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 475 | const bool isDepthwise) override |
| 476 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 477 | IgnoreUnused(isDepthwise); |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 478 | |
| 479 | const unsigned int size = tensorShape.GetNumElements(); |
| 480 | std::vector<float> decodedTensor; |
| 481 | decodedTensor.reserve(size); |
| 482 | |
| 483 | for (uint32_t i = 0; i < size; ++i) |
| 484 | { |
| 485 | this->operator[](i); |
| 486 | decodedTensor.emplace_back(*m_Iterator); |
| 487 | } |
| 488 | |
| 489 | return decodedTensor; |
| 490 | } |
| 491 | }; |
| 492 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 493 | class QASymm8Encoder : public TypedIterator<uint8_t, Encoder<float>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 494 | { |
| 495 | public: |
| 496 | QASymm8Encoder(uint8_t* data, const float scale, const int32_t offset) |
| 497 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 498 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 499 | QASymm8Encoder(const float scale, const int32_t offset) |
| 500 | : QASymm8Encoder(nullptr, scale, offset) {} |
| 501 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 502 | void Set(float right) override |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 503 | { |
| 504 | *m_Iterator = armnn::Quantize<uint8_t>(right, m_Scale, m_Offset); |
| 505 | } |
| 506 | |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 507 | float Get() const override |
| 508 | { |
| 509 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 510 | } |
| 511 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 512 | private: |
| 513 | const float m_Scale; |
| 514 | const int32_t m_Offset; |
| 515 | }; |
| 516 | |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 517 | class QASymmS8Encoder : public TypedIterator<int8_t, Encoder<float>> |
| 518 | { |
| 519 | public: |
| 520 | QASymmS8Encoder(int8_t* data, const float scale, const int32_t offset) |
| 521 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 522 | |
| 523 | QASymmS8Encoder(const float scale, const int32_t offset) |
| 524 | : QASymmS8Encoder(nullptr, scale, offset) {} |
| 525 | |
| 526 | void Set(float right) override |
| 527 | { |
| 528 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale, m_Offset); |
| 529 | } |
| 530 | |
| 531 | float Get() const override |
| 532 | { |
| 533 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 534 | } |
| 535 | |
| 536 | private: |
| 537 | const float m_Scale; |
| 538 | const int32_t m_Offset; |
| 539 | }; |
| 540 | |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 541 | class QSymmS8Encoder : public TypedIterator<int8_t, Encoder<float>> |
| 542 | { |
| 543 | public: |
| 544 | QSymmS8Encoder(int8_t* data, const float scale, const int32_t offset) |
| 545 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 546 | |
| 547 | QSymmS8Encoder(const float scale, const int32_t offset) |
| 548 | : QSymmS8Encoder(nullptr, scale, offset) {} |
| 549 | |
| 550 | void Set(float right) override |
| 551 | { |
| 552 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale, m_Offset); |
| 553 | } |
| 554 | |
| 555 | float Get() const override |
| 556 | { |
| 557 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 558 | } |
| 559 | |
| 560 | private: |
| 561 | const float m_Scale; |
| 562 | const int32_t m_Offset; |
| 563 | }; |
| 564 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 565 | class QSymm16Encoder : public TypedIterator<int16_t, Encoder<float>> |
| 566 | { |
| 567 | public: |
| 568 | QSymm16Encoder(int16_t* data, const float scale, const int32_t offset) |
| 569 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 570 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 571 | QSymm16Encoder(const float scale, const int32_t offset) |
| 572 | : QSymm16Encoder(nullptr, scale, offset) {} |
| 573 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 574 | void Set(float right) override |
| 575 | { |
| 576 | *m_Iterator = armnn::Quantize<int16_t>(right, m_Scale, m_Offset); |
| 577 | } |
| 578 | |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 579 | float Get() const override |
| 580 | { |
| 581 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 582 | } |
| 583 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 584 | private: |
| 585 | const float m_Scale; |
| 586 | const int32_t m_Offset; |
| 587 | }; |
| 588 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 589 | class Float16Encoder : public TypedIterator<Half, Encoder<float>> |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 590 | { |
| 591 | public: |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 592 | Float16Encoder(Half* data) |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 593 | : TypedIterator(data) {} |
| 594 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 595 | Float16Encoder() |
| 596 | : Float16Encoder(nullptr) {} |
| 597 | |
| 598 | void Set(float right) override |
| 599 | { |
| 600 | armnnUtils::FloatingPointConverter::ConvertFloat32To16(&right, 1, m_Iterator); |
| 601 | } |
| 602 | |
| 603 | float Get() const override |
| 604 | { |
| 605 | float val = 0.f; |
| 606 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 607 | return val; |
| 608 | } |
| 609 | }; |
| 610 | |
| 611 | class Float32Encoder : public TypedIterator<float, Encoder<float>> |
| 612 | { |
| 613 | public: |
| 614 | Float32Encoder(float* data) |
| 615 | : TypedIterator(data) {} |
| 616 | |
| 617 | Float32Encoder() |
| 618 | : Float32Encoder(nullptr) {} |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 619 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 620 | void Set(float right) override |
| 621 | { |
| 622 | *m_Iterator = right; |
| 623 | } |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 624 | |
| 625 | float Get() const override |
| 626 | { |
| 627 | return *m_Iterator; |
| 628 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 629 | }; |
| 630 | |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 631 | class Int32Encoder : public TypedIterator<int32_t, Encoder<float>> |
| 632 | { |
| 633 | public: |
| 634 | Int32Encoder(int32_t* data) |
| 635 | : TypedIterator(data) {} |
| 636 | |
| 637 | Int32Encoder() |
| 638 | : Int32Encoder(nullptr) {} |
| 639 | |
| 640 | void Set(float right) override |
| 641 | { |
| 642 | *m_Iterator = static_cast<int32_t>(right); |
| 643 | } |
| 644 | |
| 645 | float Get() const override |
| 646 | { |
| 647 | return static_cast<float>(*m_Iterator); |
| 648 | } |
| 649 | }; |
| 650 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 651 | class Int32ToInt32tEncoder : public TypedIterator<int32_t, Encoder<int32_t>> |
| 652 | { |
| 653 | public: |
| 654 | Int32ToInt32tEncoder(int32_t* data) |
| 655 | : TypedIterator(data){} |
| 656 | |
| 657 | Int32ToInt32tEncoder() |
| 658 | : Int32ToInt32tEncoder(nullptr) {} |
| 659 | |
| 660 | void Set(int32_t right) override |
| 661 | { |
| 662 | *m_Iterator = right; |
| 663 | } |
| 664 | |
| 665 | int32_t Get() const override |
| 666 | { |
| 667 | return *m_Iterator; |
| 668 | } |
| 669 | }; |
| 670 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 671 | class BooleanEncoder : public TypedIterator<uint8_t, Encoder<bool>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 672 | { |
| 673 | public: |
| 674 | BooleanEncoder(uint8_t* data) |
| 675 | : TypedIterator(data) {} |
| 676 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 677 | BooleanEncoder() |
| 678 | : BooleanEncoder(nullptr) {} |
| 679 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 680 | void Set(bool right) override |
| 681 | { |
| 682 | *m_Iterator = right; |
| 683 | } |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 684 | |
| 685 | bool Get() const override |
| 686 | { |
| 687 | return *m_Iterator; |
| 688 | } |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 689 | }; |
| 690 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 691 | /// PerAxisIterator for per-axis quantization. Iterates over a tensor as layed out in memory and keeps track |
| 692 | /// of the axis index. |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 693 | template<typename T, typename Base> |
| 694 | class PerAxisIterator : public Base |
| 695 | { |
| 696 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 697 | PerAxisIterator(T* data = nullptr, |
| 698 | unsigned int axisFactor = 0, |
| 699 | unsigned int axisDimensionality=0) |
| 700 | : m_Iterator(data), |
| 701 | m_Start(data), |
| 702 | m_AxisIndex(0), // iterates over the dimension of axis |
| 703 | m_AxisDimensionality(axisDimensionality), // tensorShape[quantization_dim] |
| 704 | m_AxisFactor(axisFactor), |
| 705 | m_Index(0) |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 706 | {} |
| 707 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 708 | PerAxisIterator(T* data = nullptr, |
| 709 | const armnn::TensorShape& tensorShape = TensorShape(), |
| 710 | const unsigned int axis = 0) |
| 711 | : m_Iterator(data), |
| 712 | m_Start(data), |
| 713 | m_AxisIndex(0), |
| 714 | m_Index(0) |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 715 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 716 | m_AxisDimensionality = tensorShape[axis]; |
| 717 | m_AxisFactor = armnnUtils::GetNumElementsAfter(tensorShape, axis); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void Reset(void* data) override |
| 721 | { |
| 722 | m_Iterator = reinterpret_cast<T*>(data); |
| 723 | m_Start = m_Iterator; |
| 724 | m_AxisIndex = 0; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 725 | m_Index = 0; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | PerAxisIterator& operator++() override |
| 729 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 730 | ++m_Index; |
| 731 | this -> operator[](m_Index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 732 | return *this; |
| 733 | } |
| 734 | |
| 735 | PerAxisIterator& operator+=(const unsigned int increment) override |
| 736 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 737 | m_Index += increment; |
| 738 | this -> operator[](m_Index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 739 | return *this; |
| 740 | } |
| 741 | |
| 742 | PerAxisIterator& operator-=(const unsigned int decrement) override |
| 743 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 744 | m_Index -= decrement; |
| 745 | this -> operator[](m_Index); |
| 746 | return *this; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | inline PerAxisIterator& SetIndexOnMem(const unsigned int index) |
| 751 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 752 | ARMNN_ASSERT(m_Iterator); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 753 | m_Iterator = m_Start + index; |
| 754 | if (index < m_AxisFactor) |
| 755 | { |
| 756 | m_AxisIndex = 0; |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | m_AxisIndex = (index / m_AxisFactor) % m_AxisDimensionality; |
| 761 | } |
| 762 | m_Index = index; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 763 | return *this; |
| 764 | } |
| 765 | |
| 766 | PerAxisIterator& operator[](const unsigned int index) override |
| 767 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 768 | SetIndexOnMem(index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 769 | return *this; |
| 770 | } |
| 771 | |
| 772 | protected: |
| 773 | T* m_Iterator; |
| 774 | T* m_Start; |
| 775 | unsigned int m_AxisIndex; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 776 | unsigned int m_AxisDimensionality; // tensorShape[quantization_dim] |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 777 | unsigned int m_AxisFactor; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 778 | unsigned int m_Index; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 779 | }; |
| 780 | |
| 781 | class QSymm8PerAxisDecoder : public PerAxisIterator<const int8_t, Decoder<float>> |
| 782 | { |
| 783 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 784 | QSymm8PerAxisDecoder(const int8_t* data, const armnn::TensorInfo& tensorInfo) |
| 785 | : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()), |
| 786 | m_Scales(tensorInfo.GetQuantizationScales()) |
| 787 | {} |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 788 | |
| 789 | float Get() const override |
| 790 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 791 | return armnn::Dequantize(*m_Iterator, GetScale(), 0); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // Get scale of the current value |
| 795 | float GetScale() const |
| 796 | { |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 797 | return m_Scales[m_AxisIndex]; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 800 | std::vector<float> DecodeTensor(const TensorShape &tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 801 | bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 802 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 803 | IgnoreUnused(isDepthwise); |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 804 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 805 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 806 | std::vector<float> decodedTensor; |
| 807 | decodedTensor.reserve(size); |
| 808 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 809 | for (uint32_t i = 0; i < size; ++i) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 810 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 811 | SetIndexOnMem(i); |
| 812 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, GetScale(), 0)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 813 | } |
| 814 | return decodedTensor; |
| 815 | } |
| 816 | |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 817 | private: |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 818 | std::vector<float> m_Scales; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 819 | }; |
| 820 | |
| 821 | class QSymm8PerAxisEncoder : public PerAxisIterator<int8_t, Encoder<float>> |
| 822 | { |
| 823 | public: |
| 824 | QSymm8PerAxisEncoder(int8_t* data, const std::vector<float>& scale, unsigned int axisFactor) |
| 825 | : PerAxisIterator(data, axisFactor), m_Scale(scale) {} |
| 826 | |
| 827 | void Set(float right) |
| 828 | { |
| 829 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale[m_AxisIndex], 0); |
| 830 | } |
| 831 | |
| 832 | float Get() const |
| 833 | { |
| 834 | return armnn::Dequantize(*m_Iterator, m_Scale[m_AxisIndex], 0); |
| 835 | } |
| 836 | |
| 837 | // Get scale of the current value |
| 838 | float GetScale() const |
| 839 | { |
| 840 | return m_Scale[m_AxisIndex]; |
| 841 | } |
| 842 | |
| 843 | private: |
| 844 | std::vector<float> m_Scale; |
| 845 | }; |
| 846 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 847 | class ScaledInt32PerAxisDecoder : public PerAxisIterator<const int32_t, Decoder<float>> |
| 848 | { |
| 849 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 850 | ScaledInt32PerAxisDecoder(const int32_t* data, const armnn::TensorInfo tensorInfo) |
| 851 | : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()), |
| 852 | m_Scales(tensorInfo.GetQuantizationScales()) |
| 853 | {} |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 854 | |
| 855 | float Get() const override |
| 856 | { |
| 857 | return armnn::Dequantize(*m_Iterator, m_Scales[m_AxisIndex], 0); |
| 858 | } |
| 859 | |
| 860 | // Get scale of the current value |
| 861 | float GetScale() const |
| 862 | { |
| 863 | return m_Scales[m_AxisIndex]; |
| 864 | } |
| 865 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 866 | std::vector<float> DecodeTensor(const TensorShape &tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 867 | bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 868 | { |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 869 | const uint32_t size = tensorShape.GetNumElements(); |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 870 | |
| 871 | const uint32_t stepSize = isDepthwise ? |
| 872 | tensorShape[2] * tensorShape[3] : tensorShape.GetNumElements() / tensorShape[0]; |
| 873 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 874 | const uint32_t stepNum = size / stepSize; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 875 | |
| 876 | std::vector<float> decodedTensor; |
| 877 | decodedTensor.reserve(size); |
| 878 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 879 | // channelMultiplier is only used in depthwise convolutions and in other cases will have no effect |
| 880 | // stepSize is the length of a contiguous area sharing a quantization scale within a tensor |
| 881 | // stepNum is the number of those steps/blocks in the tensor |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 882 | for (uint32_t step = 0; step < stepNum; ++step) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 883 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 884 | //scale = (channelMultiplier * step + mult) % scaleSize; |
| 885 | for (uint32_t i = 0; i < stepSize; ++i) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 886 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 887 | unsigned int index = step * stepSize + i; |
| 888 | this->operator[](index); |
| 889 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scales[step], 0)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | return decodedTensor; |
| 893 | } |
| 894 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 895 | private: |
| 896 | std::vector<float> m_Scales; |
| 897 | }; |
| 898 | |
| 899 | } // namespace armnn |