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