ArmNN
 24.02
BaseIterator.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/TypesUtils.hpp>
13 
14 #include <ResolveType.hpp>
15 
16 namespace armnn
17 {
18 
20 {
21 public:
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;
31 
32  virtual BaseIterator& operator[](const unsigned int index) = 0;
33 };
34 
35 template<typename IType>
36 class Decoder : public BaseIterator
37 {
38 public:
39  Decoder() {}
40 
41  virtual ~Decoder() {}
42 
43  virtual void Reset(void*) = 0;
44 
45  virtual IType Get() const = 0;
46 
47  virtual std::vector<float> DecodeTensor(const TensorShape &tensorShape, bool isDepthwise = false) = 0;
48 };
49 
50 template<typename IType>
51 class Encoder : public BaseIterator
52 {
53 public:
54  Encoder() {}
55 
56  virtual ~Encoder() {}
57 
58  virtual void Reset(void*) = 0;
59 
60  virtual void Set(IType right) = 0;
61 
62  virtual IType Get() const = 0;
63 };
64 
65 template<typename T, typename Base>
66 class TypedIterator : public Base
67 {
68 public:
69  TypedIterator(T* data = nullptr)
70  : m_Iterator(data), m_Start(data)
71  {}
72 
73  void Reset(void* data) override
74  {
75  m_Iterator = reinterpret_cast<T*>(data);
77  }
78 
80  {
82  ++m_Iterator;
83  return *this;
84  }
85 
86  TypedIterator& operator+=(const unsigned int increment) override
87  {
89  m_Iterator += increment;
90  return *this;
91  }
92 
93  TypedIterator& operator-=(const unsigned int increment) override
94  {
96  m_Iterator -= increment;
97  return *this;
98  }
99 
100  TypedIterator& operator[](const unsigned int index) override
101  {
103  m_Iterator = m_Start + index;
104  return *this;
105  }
106 
107 protected:
110 };
111 
112 class QASymm8Decoder : public TypedIterator<const uint8_t, Decoder<float>>
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 
118  QASymm8Decoder(const float scale, const int32_t offset)
119  : QASymm8Decoder(nullptr, scale, offset) {}
120 
121  float Get() const override
122  {
123  return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset);
124  }
125  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
126  {
127  const unsigned int size = tensorShape.GetNumElements();
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 
140 private:
141 
142  const float m_Scale;
143  const int32_t m_Offset;
144 };
145 
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  }
159  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
160  {
161  const unsigned int size = tensorShape.GetNumElements();
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 
174 private:
175  const float m_Scale;
176  const int32_t m_Offset;
177 
178 };
179 
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  }
193  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
194  {
195  const unsigned int size = tensorShape.GetNumElements();
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 
208 private:
209  const float m_Scale;
210  const int32_t m_Offset;
211 
212 };
213 
214 class QSymm16Decoder : public TypedIterator<const int16_t, Decoder<float>>
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 
220  QSymm16Decoder(const float scale, const int32_t offset)
221  : QSymm16Decoder(nullptr, scale, offset) {}
222 
223  float Get() const override
224  {
225  return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset);
226  }
227  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
228  {
229  const unsigned int size = tensorShape.GetNumElements();
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 
242 private:
243  const float m_Scale;
244  const int32_t m_Offset;
245 
246 };
247 
248 class Float16Decoder : public TypedIterator<const Half, Decoder<float>>
249 {
250 public:
251  Float16Decoder(const Half* data)
252  : TypedIterator(data) {}
253 
255  : Float16Decoder(nullptr) {}
256 
257  float Get() const override
258  {
259  float val = 0.f;
261  return val;
262  }
263  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool ) override
264  {
265  const unsigned int size = tensorShape.GetNumElements();
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);
274  decodedTensor.emplace_back(val);
275  }
276 
277  return decodedTensor;
278  }
279 
280 
281 };
282 
283 class Float32Decoder : public TypedIterator<const float, Decoder<float>>
284 {
285 public:
286  Float32Decoder(const float* data)
287  : TypedIterator(data) {}
288 
290  : Float32Decoder(nullptr) {}
291 
292  float Get() const override
293  {
294  return *m_Iterator;
295  }
296  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
297  {
298  const unsigned int size = tensorShape.GetNumElements();
299  std::vector<float> decodedTensor;
300 
301  decodedTensor.reserve(size);
302  decodedTensor.assign(m_Start, m_Start + size);
303 
304  return decodedTensor;
305  }
306 };
307 
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 
314  ScaledInt32Decoder(const float scale)
315  : ScaledInt32Decoder(nullptr, scale) {}
316 
317  float Get() const override
318  {
319  return static_cast<float>(*m_Iterator) * m_Scale;
320  }
321  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
322  {
323  const unsigned int size = tensorShape.GetNumElements();
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 
336 private:
337  const float m_Scale;
338 
339 };
340 
341 class Int32Decoder : public TypedIterator<const int32_t, Decoder<float>>
342 {
343 public:
344  Int32Decoder(const int32_t* data)
345  : TypedIterator(data) {}
346 
348  : Int32Decoder(nullptr) {}
349 
350  float Get() const override
351  {
352  return static_cast<float>(*m_Iterator);
353  }
354  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
355  {
356  const unsigned int size = tensorShape.GetNumElements();
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  }
368 };
369 
370 class Int32ToInt32tDecoder : public TypedIterator<const int32_t, Decoder<int32_t>>
371 {
372 public:
373  Int32ToInt32tDecoder(const int32_t* data)
374  : TypedIterator(data){}
375 
377  : Int32ToInt32tDecoder(nullptr) {}
378 
379  int32_t Get() const override
380  {
381  return *m_Iterator;
382  }
383  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
384  {
385  const unsigned int size = tensorShape.GetNumElements();
386  std::vector<float> decodedTensor;
387  decodedTensor.reserve(size);
388 
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 
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  {
414  const unsigned int size = tensorShape.GetNumElements();
415  std::vector<float> decodedTensor;
416  decodedTensor.reserve(size);
417 
418  for (uint32_t i = 0; i < size; ++i)
419  {
420  this->operator[](i);
421  decodedTensor.emplace_back(static_cast<float>(*m_Iterator));
422  }
423 
424  return decodedTensor;
425  }
426 };
427 
428 class BooleanDecoder : public TypedIterator<const uint8_t, Decoder<float>>
429 {
430 public:
431  BooleanDecoder(const uint8_t* data)
432  : TypedIterator(data) {}
433 
435  : BooleanDecoder(nullptr) {}
436 
437  float Get() const override
438  {
439  return *m_Iterator;
440  }
441  std::vector<float> DecodeTensor (const TensorShape& tensorShape, const bool) override
442  {
443  const unsigned int size = tensorShape.GetNumElements();
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  }
455 };
456 
457 class BooleanDecoderBool : public TypedIterator<const uint8_t, Decoder<bool>>
458 {
459 public:
460  BooleanDecoderBool(const uint8_t* data)
461  : TypedIterator(data) {}
462 
464  : BooleanDecoderBool(nullptr) {}
465 
466  bool Get() const override
467  {
468  return *m_Iterator;
469  }
470 
471  std::vector<float> DecodeTensor(const TensorShape& tensorShape, const bool) override
472  {
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 
487 class QASymm8Encoder : public TypedIterator<uint8_t, Encoder<float>>
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 
493  QASymm8Encoder(const float scale, const int32_t offset)
494  : QASymm8Encoder(nullptr, scale, offset) {}
495 
496  void Set(float right) override
497  {
498  *m_Iterator = armnn::Quantize<uint8_t>(right, m_Scale, m_Offset);
499  }
500 
501  float Get() const override
502  {
503  return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset);
504  }
505 
506 private:
507  const float m_Scale;
508  const int32_t m_Offset;
509 };
510 
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 
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 
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 
565  QSymm16Encoder(const float scale, const int32_t offset)
566  : QSymm16Encoder(nullptr, scale, offset) {}
567 
568  void Set(float right) override
569  {
570  *m_Iterator = armnn::Quantize<int16_t>(right, m_Scale, m_Offset);
571  }
572 
573  float Get() const override
574  {
575  return armnn::Dequantize(*m_Iterator, m_Scale, m_Offset);
576  }
577 
578 private:
579  const float m_Scale;
580  const int32_t m_Offset;
581 };
582 
583 class Float16Encoder : public TypedIterator<Half, Encoder<float>>
584 {
585 public:
587  : TypedIterator(data) {}
588 
590  : Float16Encoder(nullptr) {}
591 
592  void Set(float right) override
593  {
595  }
596 
597  float Get() const override
598  {
599  float val = 0.f;
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 
612  : Float32Encoder(nullptr) {}
613 
614  void Set(float right) override
615  {
616  *m_Iterator = right;
617  }
618 
619  float Get() const override
620  {
621  return *m_Iterator;
622  }
623 };
624 
625 class Int32Encoder : public TypedIterator<int32_t, Encoder<float>>
626 {
627 public:
628  Int32Encoder(int32_t* data)
629  : TypedIterator(data) {}
630 
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 
645 class Int32ToInt32tEncoder : public TypedIterator<int32_t, Encoder<int32_t>>
646 {
647 public:
648  Int32ToInt32tEncoder(int32_t* data)
649  : TypedIterator(data){}
650 
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 
665 class Int64Encoder : public TypedIterator<int64_t, Encoder<double>>
666 {
667 public:
668  Int64Encoder(int64_t* data)
669  : TypedIterator(data) {}
670 
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 
685 class BooleanEncoder : public TypedIterator<uint8_t, Encoder<bool>>
686 {
687 public:
688  BooleanEncoder(uint8_t* data)
689  : TypedIterator(data) {}
690 
692  : BooleanEncoder(nullptr) {}
693 
694  void Set(bool right) override
695  {
696  *m_Iterator = right;
697  }
698 
699  bool Get() const override
700  {
701  return *m_Iterator;
702  }
703 };
704 
705 /// PerAxisIterator for per-axis quantization. Iterates over a tensor as layed out in memory and keeps track
706 /// of the axis index.
707 template<typename T, typename Base>
708 class PerAxisIterator : public Base
709 {
710 public:
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)
720  {}
721 
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)
729  {
730  m_AxisDimensionality = tensorShape[axis];
731  m_AxisFactor = armnnUtils::GetNumElementsAfter(tensorShape, axis);
732  }
733 
734  void Reset(void* data) override
735  {
736  m_Iterator = reinterpret_cast<T*>(data);
738  m_AxisIndex = 0;
739  m_Index = 0;
740  }
741 
743  {
744  ++m_Index;
745  this -> operator[](m_Index);
746  return *this;
747  }
748 
749  PerAxisIterator& operator+=(const unsigned int increment) override
750  {
751  m_Index += increment;
752  this -> operator[](m_Index);
753  return *this;
754  }
755 
756  PerAxisIterator& operator-=(const unsigned int decrement) override
757  {
758  m_Index -= decrement;
759  this -> operator[](m_Index);
760  return *this;
761  }
762 
763 
764  inline PerAxisIterator& SetIndexOnMem(const unsigned int index)
765  {
767  m_Iterator = m_Start + index;
768  if (index < m_AxisFactor)
769  {
770  m_AxisIndex = 0;
771  }
772  else
773  {
775  }
776  m_Index = index;
777  return *this;
778  }
779 
780  PerAxisIterator& operator[](const unsigned int index) override
781  {
782  SetIndexOnMem(index);
783  return *this;
784  }
785 
786  protected:
789  unsigned int m_AxisIndex;
790  unsigned int m_AxisDimensionality; // tensorShape[quantization_dim]
791  unsigned int m_AxisFactor;
792  unsigned int m_Index;
793 };
794 
795 class QSymm8PerAxisDecoder : public PerAxisIterator<const int8_t, Decoder<float>>
796 {
797 public:
798  QSymm8PerAxisDecoder(const int8_t* data, const armnn::TensorInfo& tensorInfo)
799  : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()),
800  m_Scales(tensorInfo.GetQuantizationScales())
801  {}
802 
803  float Get() const override
804  {
805  return armnn::Dequantize(*m_Iterator, GetScale(), 0);
806  }
807 
808  // Get scale of the current value
809  float GetScale() const
810  {
811  return m_Scales[m_AxisIndex];
812  }
813 
814  std::vector<float> DecodeTensor(const TensorShape &tensorShape, const bool) override
815  {
816  const unsigned int size = tensorShape.GetNumElements();
817  std::vector<float> decodedTensor;
818  decodedTensor.reserve(size);
819 
820  for (uint32_t i = 0; i < size; ++i)
821  {
822  SetIndexOnMem(i);
823  decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, GetScale(), 0));
824  }
825  return decodedTensor;
826  }
827 
828 private:
829  std::vector<float> m_Scales;
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 
858 class ScaledInt32PerAxisDecoder : public PerAxisIterator<const int32_t, Decoder<float>>
859 {
860 public:
861  ScaledInt32PerAxisDecoder(const int32_t* data, const armnn::TensorInfo tensorInfo)
862  : PerAxisIterator(data, tensorInfo.GetShape(), tensorInfo.GetQuantizationDim().value()),
863  m_Scales(tensorInfo.GetQuantizationScales())
864  {}
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 
877  std::vector<float> DecodeTensor(const TensorShape &tensorShape,
878  bool isDepthwise) override
879  {
880  const uint32_t size = tensorShape.GetNumElements();
881 
882  const uint32_t stepSize = isDepthwise ?
883  tensorShape[2] * tensorShape[3] : tensorShape.GetNumElements() / tensorShape[0];
884 
885  const uint32_t stepNum = size / stepSize;
886 
887  std::vector<float> decodedTensor;
888  decodedTensor.reserve(size);
889 
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
893  for (uint32_t step = 0; step < stepNum; ++step)
894  {
895  //scale = (channelMultiplier * step + mult) % scaleSize;
896  for (uint32_t i = 0; i < stepSize; ++i)
897  {
898  unsigned int index = step * stepSize + i;
899  this->operator[](index);
900  decodedTensor.emplace_back(armnn::Dequantize(*m_Iterator, m_Scales[step], 0));
901  }
902  }
903  return decodedTensor;
904  }
905 
906 private:
907  std::vector<float> m_Scales;
908 };
909 
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 
937 } // namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::ScaledInt32PerAxisDecoder::ScaledInt32PerAxisDecoder
ScaledInt32PerAxisDecoder(const int32_t *data, const armnn::TensorInfo tensorInfo)
Definition: BaseIterator.hpp:861
armnn::QASymmS8Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:159
armnn::BaseIterator::operator-=
virtual BaseIterator & operator-=(const unsigned int increment)=0
armnn::BooleanDecoderBool::Get
bool Get() const override
Definition: BaseIterator.hpp:466
armnn::QSymm8PerAxisEncoder
Definition: BaseIterator.hpp:832
armnn::Decoder
Definition: BaseIterator.hpp:36
armnn::Float16Encoder::Float16Encoder
Float16Encoder()
Definition: BaseIterator.hpp:589
armnn::Float16Encoder
Definition: BaseIterator.hpp:583
armnn::PerAxisIterator::operator+=
PerAxisIterator & operator+=(const unsigned int increment) override
Definition: BaseIterator.hpp:749
armnn::QSymm8PerAxisDecoder
Definition: BaseIterator.hpp:795
armnn::Int32Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:639
armnn::Float32Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:296
armnn::TypedIterator::m_Iterator
T * m_Iterator
Definition: BaseIterator.hpp:108
armnn::Int32Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:354
armnn::QSymm8PerAxisDecoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:814
armnn::Int64Encoder::Get
double_t Get() const override
Definition: BaseIterator.hpp:679
armnn::QASymmS8Decoder::QASymmS8Decoder
QASymmS8Decoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:152
armnn::QSymm16PerAxisEncoder
Definition: BaseIterator.hpp:910
armnn::Encoder::Set
virtual void Set(IType right)=0
armnn::Int32ToInt32tEncoder::Set
void Set(int32_t right) override
Definition: BaseIterator.hpp:654
armnn::BooleanEncoder::BooleanEncoder
BooleanEncoder()
Definition: BaseIterator.hpp:691
armnn::QASymmS8Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:525
armnn::QSymmS8Encoder::QSymmS8Encoder
QSymmS8Encoder(int8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:538
armnn::Decoder::~Decoder
virtual ~Decoder()
Definition: BaseIterator.hpp:41
armnn::BaseIterator::operator++
virtual BaseIterator & operator++()=0
armnn::QSymmS8Decoder
Definition: BaseIterator.hpp:180
armnn::TypedIterator::operator+=
TypedIterator & operator+=(const unsigned int increment) override
Definition: BaseIterator.hpp:86
armnn::Int32ToInt32tDecoder::Int32ToInt32tDecoder
Int32ToInt32tDecoder()
Definition: BaseIterator.hpp:376
armnn::ScaledInt32Decoder::ScaledInt32Decoder
ScaledInt32Decoder(const int32_t *data, const float scale)
Definition: BaseIterator.hpp:311
armnn::BooleanEncoder::Get
bool Get() const override
Definition: BaseIterator.hpp:699
armnn::Encoder::Get
virtual IType Get() const =0
TypesUtils.hpp
armnn::QSymmS8Encoder::QSymmS8Encoder
QSymmS8Encoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:541
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::ScaledInt32Decoder
Definition: BaseIterator.hpp:308
armnn::Encoder::Encoder
Encoder()
Definition: BaseIterator.hpp:54
armnn::QASymmS8Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:155
armnn::QSymm8PerAxisDecoder::QSymm8PerAxisDecoder
QSymm8PerAxisDecoder(const int8_t *data, const armnn::TensorInfo &tensorInfo)
Definition: BaseIterator.hpp:798
armnn::Decoder::Decoder
Decoder()
Definition: BaseIterator.hpp:39
armnn::QSymm8PerAxisDecoder::Get
float Get() const override
Definition: BaseIterator.hpp:803
armnn::PerAxisIterator::m_AxisIndex
unsigned int m_AxisIndex
Definition: BaseIterator.hpp:789
armnn::Int64Decoder::Int64Decoder
Int64Decoder(const int64_t *data)
Definition: BaseIterator.hpp:402
armnn::QASymmS8Decoder::QASymmS8Decoder
QASymmS8Decoder(const int8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:149
armnn::TypedIterator::TypedIterator
TypedIterator(T *data=nullptr)
Definition: BaseIterator.hpp:69
armnn::TypedIterator::Reset
void Reset(void *data) override
Definition: BaseIterator.hpp:73
armnn::ScaledInt32Decoder::ScaledInt32Decoder
ScaledInt32Decoder(const float scale)
Definition: BaseIterator.hpp:314
armnn::QASymm8Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:125
armnn::BooleanDecoderBool::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:471
ResolveType.hpp
armnn::PerAxisIterator::operator-=
PerAxisIterator & operator-=(const unsigned int decrement) override
Definition: BaseIterator.hpp:756
armnn::Half
half_float::half Half
Definition: Half.hpp:22
armnn::QASymmS8Encoder::QASymmS8Encoder
QASymmS8Encoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:517
armnn::QSymmS8Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:544
armnn::TypedIterator::operator-=
TypedIterator & operator-=(const unsigned int increment) override
Definition: BaseIterator.hpp:93
armnn::QSymmS8Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:193
armnn::Decoder::Reset
virtual void Reset(void *)=0
armnn::BooleanDecoderBool
Definition: BaseIterator.hpp:457
NumericCast.hpp
armnn::Float16Decoder::Float16Decoder
Float16Decoder()
Definition: BaseIterator.hpp:254
armnn::TypedIterator::operator++
TypedIterator & operator++() override
Definition: BaseIterator.hpp:79
armnn::QSymm16Decoder
Definition: BaseIterator.hpp:214
TensorUtils.hpp
Assert.hpp
armnn::Int32Decoder::Int32Decoder
Int32Decoder()
Definition: BaseIterator.hpp:347
armnn::QSymm16PerAxisEncoder::Set
void Set(float right)
Definition: BaseIterator.hpp:917
armnn::Float16Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:592
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::Encoder
Definition: BaseIterator.hpp:51
armnn::Float32Decoder
Definition: BaseIterator.hpp:283
armnn::Int32ToInt32tDecoder::Int32ToInt32tDecoder
Int32ToInt32tDecoder(const int32_t *data)
Definition: BaseIterator.hpp:373
armnn::BooleanEncoder::Set
void Set(bool right) override
Definition: BaseIterator.hpp:694
armnn::QSymm16PerAxisEncoder::GetScale
float GetScale() const
Definition: BaseIterator.hpp:928
armnn::QSymmS8Decoder::QSymmS8Decoder
QSymmS8Decoder(const int8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:183
armnn::QSymm16Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:568
armnn::Int64Encoder::Int64Encoder
Int64Encoder()
Definition: BaseIterator.hpp:671
armnn::BooleanDecoder
Definition: BaseIterator.hpp:428
armnn::Float16Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:597
armnn::TypedIterator
Definition: BaseIterator.hpp:66
armnnUtils::FloatingPointConverter::ConvertFloat16To32
static void ConvertFloat16To32(const void *srcFloat16Buffer, size_t numElements, float *dstFloat32Buffer)
Definition: FloatingPointConverter.cpp:43
armnn::Int32ToInt32tEncoder
Definition: BaseIterator.hpp:645
armnn::QASymm8Encoder::QASymm8Encoder
QASymm8Encoder(uint8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:490
armnn::Float16Decoder
Definition: BaseIterator.hpp:248
armnn::Int32ToInt32tDecoder
Definition: BaseIterator.hpp:370
armnn::QSymm16Decoder::QSymm16Decoder
QSymm16Decoder(const int16_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:217
armnn::Float16Decoder::Float16Decoder
Float16Decoder(const Half *data)
Definition: BaseIterator.hpp:251
armnn::BaseIterator::operator+=
virtual BaseIterator & operator+=(const unsigned int increment)=0
armnn::Dequantize
float Dequantize(QuantizedType value, float scale, int32_t offset)
Dequantize an 8-bit data type into a floating point data type.
Definition: TypesUtils.cpp:52
armnn::BooleanDecoderBool::BooleanDecoderBool
BooleanDecoderBool(const uint8_t *data)
Definition: BaseIterator.hpp:460
armnn::PerAxisIterator::m_Iterator
T * m_Iterator
Definition: BaseIterator.hpp:787
armnn::TypedIterator::m_Start
T * m_Start
Definition: BaseIterator.hpp:109
armnn::TypedIterator::operator[]
TypedIterator & operator[](const unsigned int index) override
Definition: BaseIterator.hpp:100
armnn::Decoder::DecodeTensor
virtual std::vector< float > DecodeTensor(const TensorShape &tensorShape, bool isDepthwise=false)=0
armnn::ScaledInt32PerAxisDecoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, bool isDepthwise) override
Definition: BaseIterator.hpp:877
armnn::ScaledInt32PerAxisDecoder::GetScale
float GetScale() const
Definition: BaseIterator.hpp:872
armnn::QASymm8Decoder
Definition: BaseIterator.hpp:112
armnn::Int64Decoder
Definition: BaseIterator.hpp:399
armnn::Int32Encoder::Int32Encoder
Int32Encoder()
Definition: BaseIterator.hpp:631
armnn::BaseIterator::~BaseIterator
virtual ~BaseIterator()
Definition: BaseIterator.hpp:24
armnn::QASymm8Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:496
armnn::PerAxisIterator::operator++
PerAxisIterator & operator++() override
Definition: BaseIterator.hpp:742
armnn::Float32Encoder::Float32Encoder
Float32Encoder(float *data)
Definition: BaseIterator.hpp:608
armnn::PerAxisIterator::PerAxisIterator
PerAxisIterator(T *data=nullptr, const armnn::TensorShape &tensorShape=TensorShape(), const unsigned int axis=0)
Definition: BaseIterator.hpp:722
armnn::Float32Encoder::Float32Encoder
Float32Encoder()
Definition: BaseIterator.hpp:611
armnn::Int32Decoder
Definition: BaseIterator.hpp:341
armnn::Encoder::~Encoder
virtual ~Encoder()
Definition: BaseIterator.hpp:56
armnn::BooleanDecoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:441
armnn::PerAxisIterator::m_Start
T * m_Start
Definition: BaseIterator.hpp:788
armnn::Int64Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:412
armnn::Int32Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:350
armnn::Int64Encoder
Definition: BaseIterator.hpp:665
armnn::Decoder::Get
virtual IType Get() const =0
armnn::Float16Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:257
armnn::Float16Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:263
armnn::Int32ToInt32tDecoder::Get
int32_t Get() const override
Definition: BaseIterator.hpp:379
armnn::QSymm16Decoder::QSymm16Decoder
QSymm16Decoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:220
armnn::Float16Encoder::Float16Encoder
Float16Encoder(Half *data)
Definition: BaseIterator.hpp:586
armnn::QSymm16Encoder::QSymm16Encoder
QSymm16Encoder(int16_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:562
armnn::Int32Encoder
Definition: BaseIterator.hpp:625
armnn::Float32Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:619
armnn::QSymm16Encoder
Definition: BaseIterator.hpp:559
armnn::QSymmS8Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:549
armnn::QSymm8PerAxisDecoder::GetScale
float GetScale() const
Definition: BaseIterator.hpp:809
armnn::QSymmS8Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:189
armnn::QASymm8Encoder
Definition: BaseIterator.hpp:487
armnn::QASymmS8Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:520
armnn::QSymm16Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:227
armnn::Int32ToInt32tDecoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:383
armnn::PerAxisIterator::SetIndexOnMem
PerAxisIterator & SetIndexOnMem(const unsigned int index)
Definition: BaseIterator.hpp:764
armnn::QASymmS8Encoder
Definition: BaseIterator.hpp:511
armnn::Int64Decoder::Get
double_t Get() const override
Definition: BaseIterator.hpp:408
armnn::BooleanEncoder::BooleanEncoder
BooleanEncoder(uint8_t *data)
Definition: BaseIterator.hpp:688
armnn::QSymm8PerAxisEncoder::Get
float Get() const
Definition: BaseIterator.hpp:843
armnn::PerAxisIterator::operator[]
PerAxisIterator & operator[](const unsigned int index) override
Definition: BaseIterator.hpp:780
armnnUtils::FloatingPointConverter::ConvertFloat32To16
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.
Definition: FloatingPointConverter.cpp:17
armnn::BaseIterator::BaseIterator
BaseIterator()
Definition: BaseIterator.hpp:22
armnn::QASymm8Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:121
armnn::Int32ToInt32tEncoder::Int32ToInt32tEncoder
Int32ToInt32tEncoder()
Definition: BaseIterator.hpp:651
armnn::PerAxisIterator::Reset
void Reset(void *data) override
Definition: BaseIterator.hpp:734
armnn::ScaledInt32PerAxisDecoder::Get
float Get() const override
Definition: BaseIterator.hpp:866
armnn::QASymmS8Encoder::QASymmS8Encoder
QASymmS8Encoder(int8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:514
armnn::QSymm16Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:223
armnn::QSymmS8Decoder::QSymmS8Decoder
QSymmS8Decoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:186
armnn::QASymm8Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:501
armnn::QSymm16PerAxisEncoder::Get
float Get() const
Definition: BaseIterator.hpp:922
armnn::ScaledInt32Decoder::DecodeTensor
std::vector< float > DecodeTensor(const TensorShape &tensorShape, const bool) override
Definition: BaseIterator.hpp:321
FloatingPointConverter.hpp
armnn::Int64Encoder::Set
void Set(double right) override
Definition: BaseIterator.hpp:674
armnnUtils::GetNumElementsAfter
unsigned int GetNumElementsAfter(const armnn::TensorShape &shape, unsigned int axis)
Definition: TensorUtils.cpp:261
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::BooleanDecoderBool::BooleanDecoderBool
BooleanDecoderBool()
Definition: BaseIterator.hpp:463
armnn::Int32ToInt32tEncoder::Get
int32_t Get() const override
Definition: BaseIterator.hpp:659
armnn::BooleanDecoder::BooleanDecoder
BooleanDecoder(const uint8_t *data)
Definition: BaseIterator.hpp:431
armnn::BooleanDecoder::BooleanDecoder
BooleanDecoder()
Definition: BaseIterator.hpp:434
armnn::PerAxisIterator::m_AxisFactor
unsigned int m_AxisFactor
Definition: BaseIterator.hpp:791
armnn::QSymm8PerAxisEncoder::QSymm8PerAxisEncoder
QSymm8PerAxisEncoder(int8_t *data, const std::vector< float > &scale, unsigned int axisFactor)
Definition: BaseIterator.hpp:835
armnn::BooleanEncoder
Definition: BaseIterator.hpp:685
armnn::Int64Decoder::Int64Decoder
Int64Decoder()
Definition: BaseIterator.hpp:405
armnn::BaseIterator
Definition: BaseIterator.hpp:19
armnn::PerAxisIterator
PerAxisIterator for per-axis quantization.
Definition: BaseIterator.hpp:708
armnn::Float32Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:292
armnn::Int32ToInt32tEncoder::Int32ToInt32tEncoder
Int32ToInt32tEncoder(int32_t *data)
Definition: BaseIterator.hpp:648
armnn::PerAxisIterator::m_Index
unsigned int m_Index
Definition: BaseIterator.hpp:792
armnn::Int32Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:634
armnn::Float32Decoder::Float32Decoder
Float32Decoder()
Definition: BaseIterator.hpp:289
armnn::BooleanDecoder::Get
float Get() const override
Definition: BaseIterator.hpp:437
armnn::QSymm16Encoder::Get
float Get() const override
Definition: BaseIterator.hpp:573
armnn::ScaledInt32Decoder::Get
float Get() const override
Definition: BaseIterator.hpp:317
armnn::Int32Encoder::Int32Encoder
Int32Encoder(int32_t *data)
Definition: BaseIterator.hpp:628
armnn::QASymm8Encoder::QASymm8Encoder
QASymm8Encoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:493
armnn::Float32Decoder::Float32Decoder
Float32Decoder(const float *data)
Definition: BaseIterator.hpp:286
armnn::Encoder::Reset
virtual void Reset(void *)=0
armnn::PerAxisIterator::PerAxisIterator
PerAxisIterator(T *data=nullptr, unsigned int axisFactor=0, unsigned int axisDimensionality=0)
Definition: BaseIterator.hpp:711
armnn::TensorShape::GetNumElements
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified.
Definition: Tensor.cpp:181
armnn::Float32Encoder
Definition: BaseIterator.hpp:605
armnn::Int64Encoder::Int64Encoder
Int64Encoder(int64_t *data)
Definition: BaseIterator.hpp:668
armnn::Int32Decoder::Int32Decoder
Int32Decoder(const int32_t *data)
Definition: BaseIterator.hpp:344
armnn::QASymm8Decoder::QASymm8Decoder
QASymm8Decoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:118
armnn::QSymmS8Encoder
Definition: BaseIterator.hpp:535
armnn::QASymm8Decoder::QASymm8Decoder
QASymm8Decoder(const uint8_t *data, const float scale, const int32_t offset)
Definition: BaseIterator.hpp:115
armnn::BaseIterator::operator[]
virtual BaseIterator & operator[](const unsigned int index)=0
armnn::PerAxisIterator::m_AxisDimensionality
unsigned int m_AxisDimensionality
Definition: BaseIterator.hpp:790
armnn::ScaledInt32PerAxisDecoder
Definition: BaseIterator.hpp:858
armnn::QSymm8PerAxisEncoder::Set
void Set(float right)
Definition: BaseIterator.hpp:838
armnn::Float32Encoder::Set
void Set(float right) override
Definition: BaseIterator.hpp:614
armnn::QSymm16PerAxisEncoder::QSymm16PerAxisEncoder
QSymm16PerAxisEncoder(int16_t *data, const std::vector< float > &scale, unsigned int axisFactor, unsigned int axisDimensionality)
Definition: BaseIterator.hpp:913
armnn::QSymm8PerAxisEncoder::GetScale
float GetScale() const
Definition: BaseIterator.hpp:849
armnn::QASymmS8Decoder
Definition: BaseIterator.hpp:146
armnn::QSymm16Encoder::QSymm16Encoder
QSymm16Encoder(const float scale, const int32_t offset)
Definition: BaseIterator.hpp:565