ArmNN
 24.08
Decoders.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "BaseIterator.hpp"
9 
12 
13 namespace armnn
14 {
15 
16 namespace
17 {
18 
19 inline std::unique_ptr<Decoder<float>> MakeSigned32PerAxisDecoder(const TensorInfo& info, const void* data)
20 {
21  return std::make_unique<ScaledInt32PerAxisDecoder>(static_cast<const int32_t*>(data), info);
22 }
23 
24 inline std::unique_ptr<Decoder<float>> MakeSigned32Decoder(const TensorInfo& info, const void* data)
25 {
26  if(info.HasMultipleQuantizationScales())
27  {
28  // NOTE: If we have multiple quantization scales, we create a ScaledInt32PerAxisDecoder.
29  // This will be used to decode per-axis quantized convolution biases.
30  return MakeSigned32PerAxisDecoder(info, data);
31  }
32  else
33  {
34  if (info.GetQuantizationDim().has_value())
35  {
36  // NOTE: Even though we only have a single quantization scale, if the quantization
37  // dimension is set, the tensor has per-axis quantization and we need to create a
38  // ScaledInt32PerAxisDecoder
39  return MakeSigned32PerAxisDecoder(info, data);
40  }
41 
42  const float scale = info.GetQuantizationScale();
43  if (scale == 0.f)
44  {
45  // NOTE:: If no quantization scale is set, we create an Int32Decoder, which simply
46  // casts the int value to float. This will be used for any INT32 data other than
47  // convolution biases.
48  return std::make_unique<Int32Decoder>(static_cast<const int32_t*>(data));
49  }
50 
51  // NOTE: If we only have a single (non-zero) quantization scale and no quantization
52  // dimension is specified, we need to create a ScaledInt32Decoder. This will be used
53  // to decode per-tensor quantized convolution biases.
54  return std::make_unique<ScaledInt32Decoder>(static_cast<const int32_t*>(data), scale);
55  }
56 }
57 
58 } // anonymous namespace
59 
60 template<typename T>
61 inline std::unique_ptr<Decoder<T>> MakeDecoder(const TensorInfo& info, const void* data = nullptr);
62 
63 template<>
64 inline std::unique_ptr<Decoder<float>> MakeDecoder(const TensorInfo& info, const void* data)
65 {
66  switch(info.GetDataType())
67  {
68  case DataType::QAsymmS8:
69  {
70  return std::make_unique<QASymmS8Decoder>(
71  static_cast<const int8_t*>(data),
72  info.GetQuantizationScale(),
73  info.GetQuantizationOffset());
74  }
75  case DataType::QAsymmU8:
76  {
77  return std::make_unique<QASymm8Decoder>(
78  static_cast<const uint8_t*>(data),
79  info.GetQuantizationScale(),
80  info.GetQuantizationOffset());
81  }
82  case DataType::QSymmS16:
83  {
84  return std::make_unique<QSymm16Decoder>(
85  static_cast<const int16_t*>(data),
86  info.GetQuantizationScale(),
87  info.GetQuantizationOffset());
88  }
89  case DataType::Float16:
90  {
91  return std::make_unique<Float16Decoder>(static_cast<const Half*>(data));
92  }
93  case DataType::Float32:
94  {
95  return std::make_unique<Float32Decoder>(static_cast<const float*>(data));
96  }
97  case DataType::Signed32:
98  {
99  return MakeSigned32Decoder(info, data);
100  }
101  case DataType::QSymmS8:
102  {
103  if (info.HasPerAxisQuantization())
104  {
105  return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info);
106  }
107  else
108  {
109  return std::make_unique<QSymmS8Decoder>(
110  static_cast<const int8_t*>(data),
111  info.GetQuantizationScale(),
112  info.GetQuantizationOffset());
113  }
114  }
116  {
117  return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data));
118  }
119  default:
120  {
121  throw InvalidArgumentException("Unsupported target Data Type!");
122  break;
123  }
124  }
125  return nullptr;
126 }
127 
128 template<>
129 inline std::unique_ptr<Decoder<double_t>> MakeDecoder(const TensorInfo& info, const void* data)
130 {
131  switch(info.GetDataType())
132  {
133  case DataType::Signed64:
134  {
135  return std::make_unique<Int64Decoder>(static_cast<const int64_t*>(data));
136  }
137  default:
138  {
139  throw InvalidArgumentException("Cannot decode to double. Unsupported origin Data Type!");
140  break;
141  }
142  }
143  return nullptr;
144 }
145 
146 template<>
147 inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data)
148 {
149  switch(info.GetDataType())
150  {
151  case DataType::Boolean:
152  {
153  return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data));
154  }
155  default:
156  {
157  throw InvalidArgumentException("Cannot decode to bool. Unsupported origin Data Type!");
158  break;
159  }
160  }
161  return nullptr;
162 }
163 
164 template<>
165 inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data)
166 {
167  switch(info.GetDataType())
168  {
169  case DataType::Signed32:
170  {
171  return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data));
172  }
173  default:
174  {
175  throw InvalidArgumentException("Cannot decode to int32. Unsupported origin Data Type!");
176  break;
177  }
178  }
179  return nullptr;
180 }
181 
182 } //namespace armnn
armnn::MakeDecoder
std::unique_ptr< Decoder< T > > MakeDecoder(const TensorInfo &info, const void *data=nullptr)
Definition: Decoders.hpp:64
armnn::DataType::Boolean
@ Boolean
BaseIterator.hpp
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::DataType::Float32
@ Float32
armnn::DataType::QAsymmU8
@ QAsymmU8
armnn::DataType::QSymmS8
@ QSymmS8
armnn::Half
half_float::half Half
Definition: Half.hpp:22
armnn::DataType::QSymmS16
@ QSymmS16
TensorUtils.hpp
armnn::DataType::Float16
@ Float16
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::BoostLogSeverityMapping::info
@ info
armnn::DataType::Signed32
@ Signed32
armnn::DataType::QAsymmS8
@ QAsymmS8
FloatingPointConverter.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::DataType::Signed64
@ Signed64