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 | |
Narumol Prangnawarat | 8832522 | 2020-03-06 14:45:57 +0000 | [diff] [blame] | 263 | class BFloat16Decoder : public TypedIterator<const BFloat16, Decoder<float>> |
| 264 | { |
| 265 | public: |
| 266 | BFloat16Decoder(const BFloat16* data) |
| 267 | : TypedIterator(data) {} |
| 268 | |
| 269 | BFloat16Decoder() |
| 270 | : BFloat16Decoder(nullptr) {} |
| 271 | |
| 272 | float Get() const override |
| 273 | { |
| 274 | float val = 0.f; |
| 275 | armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32(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 | this->operator[](i); |
| 290 | |
| 291 | float val = 0.f; |
| 292 | armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32(m_Iterator, 1, &val); |
| 293 | decodedTensor.emplace_back(val); |
| 294 | } |
| 295 | |
| 296 | return decodedTensor; |
| 297 | } |
| 298 | |
Narumol Prangnawarat | 8832522 | 2020-03-06 14:45:57 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 301 | class Float16Decoder : public TypedIterator<const Half, Decoder<float>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 302 | { |
| 303 | public: |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 304 | Float16Decoder(const Half* data) |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 305 | : TypedIterator(data) {} |
| 306 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 307 | Float16Decoder() |
| 308 | : Float16Decoder(nullptr) {} |
| 309 | |
| 310 | float Get() const override |
| 311 | { |
| 312 | float val = 0.f; |
| 313 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 314 | return val; |
| 315 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 316 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 317 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 318 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 319 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 320 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 321 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 322 | std::vector<float> decodedTensor; |
| 323 | decodedTensor.reserve(size); |
| 324 | |
| 325 | for (uint32_t i = 0; i < size; ++i) |
| 326 | { |
| 327 | float val = 0.f; |
| 328 | this->operator[](i); |
| 329 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 330 | decodedTensor.emplace_back(val); |
| 331 | } |
| 332 | |
| 333 | return decodedTensor; |
| 334 | } |
| 335 | |
| 336 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | class Float32Decoder : public TypedIterator<const float, Decoder<float>> |
| 340 | { |
| 341 | public: |
| 342 | Float32Decoder(const float* data) |
| 343 | : TypedIterator(data) {} |
| 344 | |
| 345 | Float32Decoder() |
| 346 | : Float32Decoder(nullptr) {} |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 347 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 348 | float Get() const override |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 349 | { |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 350 | return *m_Iterator; |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 351 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 352 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 353 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 354 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 355 | IgnoreUnused(isDepthwise); |
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 | |
| 359 | decodedTensor.reserve(size); |
| 360 | decodedTensor.assign(m_Start, m_Start + size); |
| 361 | |
| 362 | return decodedTensor; |
| 363 | } |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 364 | }; |
| 365 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 366 | class ScaledInt32Decoder : public TypedIterator<const int32_t, Decoder<float>> |
| 367 | { |
| 368 | public: |
| 369 | ScaledInt32Decoder(const int32_t* data, const float scale) |
| 370 | : TypedIterator(data), m_Scale(scale) {} |
| 371 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 372 | ScaledInt32Decoder(const float scale) |
| 373 | : ScaledInt32Decoder(nullptr, scale) {} |
| 374 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 375 | float Get() const override |
| 376 | { |
| 377 | return static_cast<float>(*m_Iterator) * m_Scale; |
| 378 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 379 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 380 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 381 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 382 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 383 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 384 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 385 | std::vector<float> decodedTensor; |
| 386 | decodedTensor.reserve(size); |
| 387 | |
| 388 | for (uint32_t i = 0; i < size; ++i) |
| 389 | { |
| 390 | this->operator[](i); |
| 391 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator) * m_Scale); |
| 392 | } |
| 393 | |
| 394 | return decodedTensor; |
| 395 | } |
| 396 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 397 | private: |
| 398 | const float m_Scale; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 399 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 400 | }; |
| 401 | |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 402 | class Int32Decoder : public TypedIterator<const int32_t, Decoder<float>> |
| 403 | { |
| 404 | public: |
| 405 | Int32Decoder(const int32_t* data) |
| 406 | : TypedIterator(data) {} |
| 407 | |
| 408 | Int32Decoder() |
| 409 | : Int32Decoder(nullptr) {} |
| 410 | |
| 411 | float Get() const override |
| 412 | { |
| 413 | return static_cast<float>(*m_Iterator); |
| 414 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 415 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 416 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 417 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 418 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 419 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 420 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 421 | std::vector<float> decodedTensor; |
| 422 | decodedTensor.reserve(size); |
| 423 | |
| 424 | for (uint32_t i = 0; i < size; ++i) |
| 425 | { |
| 426 | this->operator[](i); |
| 427 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator)); |
| 428 | } |
| 429 | |
| 430 | return decodedTensor; |
| 431 | } |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 432 | }; |
| 433 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 434 | class Int32ToInt32tDecoder : public TypedIterator<const int32_t, Decoder<int32_t>> |
| 435 | { |
| 436 | public: |
| 437 | Int32ToInt32tDecoder(const int32_t* data) |
| 438 | : TypedIterator(data){} |
| 439 | |
| 440 | Int32ToInt32tDecoder() |
| 441 | : Int32ToInt32tDecoder(nullptr) {} |
| 442 | |
| 443 | int32_t Get() const override |
| 444 | { |
| 445 | return *m_Iterator; |
| 446 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 447 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 448 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 449 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 450 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 451 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 452 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 453 | std::vector<float> decodedTensor; |
| 454 | decodedTensor.reserve(size); |
| 455 | |
| 456 | for (uint32_t i = 0; i < size; ++i) |
| 457 | { |
| 458 | this->operator[](i); |
Rob Hughes | c013bc8 | 2021-07-14 09:31:31 +0100 | [diff] [blame] | 459 | decodedTensor.emplace_back(static_cast<float>(*m_Iterator)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return decodedTensor; |
| 463 | } |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 464 | }; |
| 465 | |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 466 | class BooleanDecoder : public TypedIterator<const uint8_t, Decoder<float>> |
| 467 | { |
| 468 | public: |
| 469 | BooleanDecoder(const uint8_t* data) |
| 470 | : TypedIterator(data) {} |
| 471 | |
| 472 | BooleanDecoder() |
| 473 | : BooleanDecoder(nullptr) {} |
| 474 | |
| 475 | float Get() const override |
| 476 | { |
| 477 | return *m_Iterator; |
| 478 | } |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 479 | std::vector<float> DecodeTensor (const TensorShape& tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 480 | const bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 481 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 482 | IgnoreUnused(isDepthwise); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 483 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 484 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 485 | std::vector<float> decodedTensor; |
| 486 | decodedTensor.reserve(size); |
| 487 | |
| 488 | for (uint32_t i = 0; i < size; ++i) |
| 489 | { |
| 490 | this->operator[](i); |
| 491 | decodedTensor.emplace_back(*m_Iterator); |
| 492 | } |
| 493 | |
| 494 | return decodedTensor; |
| 495 | } |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 496 | }; |
| 497 | |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 498 | class BooleanDecoderBool : public TypedIterator<const uint8_t, Decoder<bool>> |
| 499 | { |
| 500 | public: |
| 501 | BooleanDecoderBool(const uint8_t* data) |
| 502 | : TypedIterator(data) {} |
| 503 | |
| 504 | BooleanDecoderBool() |
| 505 | : BooleanDecoderBool(nullptr) {} |
| 506 | |
| 507 | bool Get() const override |
| 508 | { |
| 509 | return *m_Iterator; |
| 510 | } |
| 511 | |
| 512 | std::vector<float> DecodeTensor(const TensorShape& tensorShape, |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 513 | const bool isDepthwise) override |
| 514 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 515 | IgnoreUnused(isDepthwise); |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 516 | |
| 517 | const unsigned int size = tensorShape.GetNumElements(); |
| 518 | std::vector<float> decodedTensor; |
| 519 | decodedTensor.reserve(size); |
| 520 | |
| 521 | for (uint32_t i = 0; i < size; ++i) |
| 522 | { |
| 523 | this->operator[](i); |
| 524 | decodedTensor.emplace_back(*m_Iterator); |
| 525 | } |
| 526 | |
| 527 | return decodedTensor; |
| 528 | } |
| 529 | }; |
| 530 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 531 | class QASymm8Encoder : public TypedIterator<uint8_t, Encoder<float>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 532 | { |
| 533 | public: |
| 534 | QASymm8Encoder(uint8_t* data, const float scale, const int32_t offset) |
| 535 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 536 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 537 | QASymm8Encoder(const float scale, const int32_t offset) |
| 538 | : QASymm8Encoder(nullptr, scale, offset) {} |
| 539 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 540 | void Set(float right) override |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 541 | { |
| 542 | *m_Iterator = armnn::Quantize<uint8_t>(right, m_Scale, m_Offset); |
| 543 | } |
| 544 | |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 545 | float Get() const override |
| 546 | { |
| 547 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 548 | } |
| 549 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 550 | private: |
| 551 | const float m_Scale; |
| 552 | const int32_t m_Offset; |
| 553 | }; |
| 554 | |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 555 | class QASymmS8Encoder : public TypedIterator<int8_t, Encoder<float>> |
| 556 | { |
| 557 | public: |
| 558 | QASymmS8Encoder(int8_t* data, const float scale, const int32_t offset) |
| 559 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 560 | |
| 561 | QASymmS8Encoder(const float scale, const int32_t offset) |
| 562 | : QASymmS8Encoder(nullptr, scale, offset) {} |
| 563 | |
| 564 | void Set(float right) override |
| 565 | { |
| 566 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale, m_Offset); |
| 567 | } |
| 568 | |
| 569 | float Get() const override |
| 570 | { |
| 571 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 572 | } |
| 573 | |
| 574 | private: |
| 575 | const float m_Scale; |
| 576 | const int32_t m_Offset; |
| 577 | }; |
| 578 | |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 579 | class QSymmS8Encoder : public TypedIterator<int8_t, Encoder<float>> |
| 580 | { |
| 581 | public: |
| 582 | QSymmS8Encoder(int8_t* data, const float scale, const int32_t offset) |
| 583 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 584 | |
| 585 | QSymmS8Encoder(const float scale, const int32_t offset) |
| 586 | : QSymmS8Encoder(nullptr, scale, offset) {} |
| 587 | |
| 588 | void Set(float right) override |
| 589 | { |
| 590 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale, m_Offset); |
| 591 | } |
| 592 | |
| 593 | float Get() const override |
| 594 | { |
| 595 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 596 | } |
| 597 | |
| 598 | private: |
| 599 | const float m_Scale; |
| 600 | const int32_t m_Offset; |
| 601 | }; |
| 602 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 603 | class QSymm16Encoder : public TypedIterator<int16_t, Encoder<float>> |
| 604 | { |
| 605 | public: |
| 606 | QSymm16Encoder(int16_t* data, const float scale, const int32_t offset) |
| 607 | : TypedIterator(data), m_Scale(scale), m_Offset(offset) {} |
| 608 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 609 | QSymm16Encoder(const float scale, const int32_t offset) |
| 610 | : QSymm16Encoder(nullptr, scale, offset) {} |
| 611 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 612 | void Set(float right) override |
| 613 | { |
| 614 | *m_Iterator = armnn::Quantize<int16_t>(right, m_Scale, m_Offset); |
| 615 | } |
| 616 | |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 617 | float Get() const override |
| 618 | { |
| 619 | return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset); |
| 620 | } |
| 621 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 622 | private: |
| 623 | const float m_Scale; |
| 624 | const int32_t m_Offset; |
| 625 | }; |
| 626 | |
Narumol Prangnawarat | 8832522 | 2020-03-06 14:45:57 +0000 | [diff] [blame] | 627 | class BFloat16Encoder : public TypedIterator<armnn::BFloat16, Encoder<float>> |
| 628 | { |
| 629 | public: |
| 630 | BFloat16Encoder(armnn::BFloat16* data) |
| 631 | : TypedIterator(data) {} |
| 632 | |
| 633 | BFloat16Encoder() |
| 634 | : BFloat16Encoder(nullptr) {} |
| 635 | |
| 636 | void Set(float right) override |
| 637 | { |
| 638 | armnnUtils::FloatingPointConverter::ConvertFloat32ToBFloat16(&right, 1, m_Iterator); |
| 639 | } |
| 640 | |
| 641 | float Get() const override |
| 642 | { |
| 643 | float val = 0.f; |
| 644 | armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32(m_Iterator, 1, &val); |
| 645 | return val; |
| 646 | } |
| 647 | }; |
| 648 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 649 | class Float16Encoder : public TypedIterator<Half, Encoder<float>> |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 650 | { |
| 651 | public: |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 652 | Float16Encoder(Half* data) |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 653 | : TypedIterator(data) {} |
| 654 | |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 655 | Float16Encoder() |
| 656 | : Float16Encoder(nullptr) {} |
| 657 | |
| 658 | void Set(float right) override |
| 659 | { |
| 660 | armnnUtils::FloatingPointConverter::ConvertFloat32To16(&right, 1, m_Iterator); |
| 661 | } |
| 662 | |
| 663 | float Get() const override |
| 664 | { |
| 665 | float val = 0.f; |
| 666 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(m_Iterator, 1, &val); |
| 667 | return val; |
| 668 | } |
| 669 | }; |
| 670 | |
| 671 | class Float32Encoder : public TypedIterator<float, Encoder<float>> |
| 672 | { |
| 673 | public: |
| 674 | Float32Encoder(float* data) |
| 675 | : TypedIterator(data) {} |
| 676 | |
| 677 | Float32Encoder() |
| 678 | : Float32Encoder(nullptr) {} |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 679 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 680 | void Set(float right) override |
| 681 | { |
| 682 | *m_Iterator = right; |
| 683 | } |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 684 | |
| 685 | float Get() const override |
| 686 | { |
| 687 | return *m_Iterator; |
| 688 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 689 | }; |
| 690 | |
Aron Virginas-Tar | 198ee40 | 2019-08-02 18:54:28 +0100 | [diff] [blame] | 691 | class Int32Encoder : public TypedIterator<int32_t, Encoder<float>> |
| 692 | { |
| 693 | public: |
| 694 | Int32Encoder(int32_t* data) |
| 695 | : TypedIterator(data) {} |
| 696 | |
| 697 | Int32Encoder() |
| 698 | : Int32Encoder(nullptr) {} |
| 699 | |
| 700 | void Set(float right) override |
| 701 | { |
| 702 | *m_Iterator = static_cast<int32_t>(right); |
| 703 | } |
| 704 | |
| 705 | float Get() const override |
| 706 | { |
| 707 | return static_cast<float>(*m_Iterator); |
| 708 | } |
| 709 | }; |
| 710 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 711 | class Int32ToInt32tEncoder : public TypedIterator<int32_t, Encoder<int32_t>> |
| 712 | { |
| 713 | public: |
| 714 | Int32ToInt32tEncoder(int32_t* data) |
| 715 | : TypedIterator(data){} |
| 716 | |
| 717 | Int32ToInt32tEncoder() |
| 718 | : Int32ToInt32tEncoder(nullptr) {} |
| 719 | |
| 720 | void Set(int32_t right) override |
| 721 | { |
| 722 | *m_Iterator = right; |
| 723 | } |
| 724 | |
| 725 | int32_t Get() const override |
| 726 | { |
| 727 | return *m_Iterator; |
| 728 | } |
| 729 | }; |
| 730 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 731 | class BooleanEncoder : public TypedIterator<uint8_t, Encoder<bool>> |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 732 | { |
| 733 | public: |
| 734 | BooleanEncoder(uint8_t* data) |
| 735 | : TypedIterator(data) {} |
| 736 | |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 737 | BooleanEncoder() |
| 738 | : BooleanEncoder(nullptr) {} |
| 739 | |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 740 | void Set(bool right) override |
| 741 | { |
| 742 | *m_Iterator = right; |
| 743 | } |
Nattapat Chaimanowong | eb2b329 | 2019-05-07 12:02:30 +0100 | [diff] [blame] | 744 | |
| 745 | bool Get() const override |
| 746 | { |
| 747 | return *m_Iterator; |
| 748 | } |
Sadik Armagan | 2e6dc3a | 2019-04-03 17:48:18 +0100 | [diff] [blame] | 749 | }; |
| 750 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 751 | /// PerAxisIterator for per-axis quantization. Iterates over a tensor as layed out in memory and keeps track |
| 752 | /// of the axis index. |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 753 | template<typename T, typename Base> |
| 754 | class PerAxisIterator : public Base |
| 755 | { |
| 756 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 757 | PerAxisIterator(T* data = nullptr, |
| 758 | unsigned int axisFactor = 0, |
| 759 | unsigned int axisDimensionality=0) |
| 760 | : m_Iterator(data), |
| 761 | m_Start(data), |
| 762 | m_AxisIndex(0), // iterates over the dimension of axis |
| 763 | m_AxisDimensionality(axisDimensionality), // tensorShape[quantization_dim] |
| 764 | m_AxisFactor(axisFactor), |
| 765 | m_Index(0) |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 766 | {} |
| 767 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 768 | PerAxisIterator(T* data = nullptr, |
| 769 | const armnn::TensorShape& tensorShape = TensorShape(), |
| 770 | const unsigned int axis = 0) |
| 771 | : m_Iterator(data), |
| 772 | m_Start(data), |
| 773 | m_AxisIndex(0), |
| 774 | m_Index(0) |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 775 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 776 | m_AxisDimensionality = tensorShape[axis]; |
| 777 | m_AxisFactor = armnnUtils::GetNumElementsAfter(tensorShape, axis); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void Reset(void* data) override |
| 781 | { |
| 782 | m_Iterator = reinterpret_cast<T*>(data); |
| 783 | m_Start = m_Iterator; |
| 784 | m_AxisIndex = 0; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 785 | m_Index = 0; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | PerAxisIterator& operator++() override |
| 789 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 790 | ++m_Index; |
| 791 | this -> operator[](m_Index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 792 | return *this; |
| 793 | } |
| 794 | |
| 795 | PerAxisIterator& operator+=(const unsigned int increment) override |
| 796 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 797 | m_Index += increment; |
| 798 | this -> operator[](m_Index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 799 | return *this; |
| 800 | } |
| 801 | |
| 802 | PerAxisIterator& operator-=(const unsigned int decrement) override |
| 803 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 804 | m_Index -= decrement; |
| 805 | this -> operator[](m_Index); |
| 806 | return *this; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | inline PerAxisIterator& SetIndexOnMem(const unsigned int index) |
| 811 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 812 | ARMNN_ASSERT(m_Iterator); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 813 | m_Iterator = m_Start + index; |
| 814 | if (index < m_AxisFactor) |
| 815 | { |
| 816 | m_AxisIndex = 0; |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | m_AxisIndex = (index / m_AxisFactor) % m_AxisDimensionality; |
| 821 | } |
| 822 | m_Index = index; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 823 | return *this; |
| 824 | } |
| 825 | |
| 826 | PerAxisIterator& operator[](const unsigned int index) override |
| 827 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 828 | SetIndexOnMem(index); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 829 | return *this; |
| 830 | } |
| 831 | |
| 832 | protected: |
| 833 | T* m_Iterator; |
| 834 | T* m_Start; |
| 835 | unsigned int m_AxisIndex; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 836 | unsigned int m_AxisDimensionality; // tensorShape[quantization_dim] |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 837 | unsigned int m_AxisFactor; |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 838 | unsigned int m_Index; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 839 | }; |
| 840 | |
| 841 | class QSymm8PerAxisDecoder : public PerAxisIterator<const int8_t, Decoder<float>> |
| 842 | { |
| 843 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 844 | QSymm8PerAxisDecoder(const int8_t* data, const armnn::TensorInfo& tensorInfo) |
| 845 | : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()), |
| 846 | m_Scales(tensorInfo.GetQuantizationScales()) |
| 847 | {} |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 848 | |
| 849 | float Get() const override |
| 850 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 851 | return armnn::Dequantize(*m_Iterator, GetScale(), 0); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | // Get scale of the current value |
| 855 | float GetScale() const |
| 856 | { |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 857 | return m_Scales[m_AxisIndex]; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 860 | std::vector<float> DecodeTensor(const TensorShape &tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 861 | bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 862 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 863 | IgnoreUnused(isDepthwise); |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 864 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 865 | const unsigned int size = tensorShape.GetNumElements(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 866 | std::vector<float> decodedTensor; |
| 867 | decodedTensor.reserve(size); |
| 868 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 869 | for (uint32_t i = 0; i < size; ++i) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 870 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 871 | SetIndexOnMem(i); |
| 872 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, GetScale(), 0)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 873 | } |
| 874 | return decodedTensor; |
| 875 | } |
| 876 | |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 877 | private: |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 878 | std::vector<float> m_Scales; |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 879 | }; |
| 880 | |
| 881 | class QSymm8PerAxisEncoder : public PerAxisIterator<int8_t, Encoder<float>> |
| 882 | { |
| 883 | public: |
| 884 | QSymm8PerAxisEncoder(int8_t* data, const std::vector<float>& scale, unsigned int axisFactor) |
| 885 | : PerAxisIterator(data, axisFactor), m_Scale(scale) {} |
| 886 | |
| 887 | void Set(float right) |
| 888 | { |
| 889 | *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale[m_AxisIndex], 0); |
| 890 | } |
| 891 | |
| 892 | float Get() const |
| 893 | { |
| 894 | return armnn::Dequantize(*m_Iterator, m_Scale[m_AxisIndex], 0); |
| 895 | } |
| 896 | |
| 897 | // Get scale of the current value |
| 898 | float GetScale() const |
| 899 | { |
| 900 | return m_Scale[m_AxisIndex]; |
| 901 | } |
| 902 | |
| 903 | private: |
| 904 | std::vector<float> m_Scale; |
| 905 | }; |
| 906 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 907 | class ScaledInt32PerAxisDecoder : public PerAxisIterator<const int32_t, Decoder<float>> |
| 908 | { |
| 909 | public: |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 910 | ScaledInt32PerAxisDecoder(const int32_t* data, const armnn::TensorInfo tensorInfo) |
| 911 | : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()), |
| 912 | m_Scales(tensorInfo.GetQuantizationScales()) |
| 913 | {} |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 914 | |
| 915 | float Get() const override |
| 916 | { |
| 917 | return armnn::Dequantize(*m_Iterator, m_Scales[m_AxisIndex], 0); |
| 918 | } |
| 919 | |
| 920 | // Get scale of the current value |
| 921 | float GetScale() const |
| 922 | { |
| 923 | return m_Scales[m_AxisIndex]; |
| 924 | } |
| 925 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 926 | std::vector<float> DecodeTensor(const TensorShape &tensorShape, |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 927 | bool isDepthwise) override |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 928 | { |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 929 | const uint32_t size = tensorShape.GetNumElements(); |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 930 | |
| 931 | const uint32_t stepSize = isDepthwise ? |
| 932 | tensorShape[2] * tensorShape[3] : tensorShape.GetNumElements() / tensorShape[0]; |
| 933 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 934 | const uint32_t stepNum = size / stepSize; |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 935 | |
| 936 | std::vector<float> decodedTensor; |
| 937 | decodedTensor.reserve(size); |
| 938 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 939 | // channelMultiplier is only used in depthwise convolutions and in other cases will have no effect |
| 940 | // stepSize is the length of a contiguous area sharing a quantization scale within a tensor |
| 941 | // stepNum is the number of those steps/blocks in the tensor |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 942 | for (uint32_t step = 0; step < stepNum; ++step) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 943 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 944 | //scale = (channelMultiplier * step + mult) % scaleSize; |
| 945 | for (uint32_t i = 0; i < stepSize; ++i) |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 946 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 947 | unsigned int index = step * stepSize + i; |
| 948 | this->operator[](index); |
| 949 | decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scales[step], 0)); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | return decodedTensor; |
| 953 | } |
| 954 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 955 | private: |
| 956 | std::vector<float> m_Scales; |
| 957 | }; |
| 958 | |
| 959 | } // namespace armnn |