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