ArmNN
 24.02
Decoders.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
106  return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info);
107  }
108  else
109  {
110  return std::make_unique<QSymmS8Decoder>(
111  static_cast<const int8_t*>(data),
112  info.GetQuantizationScale(),
113  info.GetQuantizationOffset());
114  }
115  }
117  {
118  return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data));
119  }
120  default:
121  {
122  throw InvalidArgumentException("Unsupported target Data Type!");
123  break;
124  }
125  }
126  return nullptr;
127 }
128 
129 template<>
130 inline std::unique_ptr<Decoder<double_t>> MakeDecoder(const TensorInfo& info, const void* data)
131 {
132  switch(info.GetDataType())
133  {
134  case DataType::Signed64:
135  {
136  return std::make_unique<Int64Decoder>(static_cast<const int64_t*>(data));
137  }
138  default:
139  {
140  throw InvalidArgumentException("Cannot decode to double. Unsupported origin Data Type!");
141  break;
142  }
143  }
144  return nullptr;
145 }
146 
147 template<>
148 inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data)
149 {
150  switch(info.GetDataType())
151  {
152  case DataType::Boolean:
153  {
154  return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data));
155  }
156  default:
157  {
158  throw InvalidArgumentException("Cannot decode to bool. Unsupported origin Data Type!");
159  break;
160  }
161  }
162  return nullptr;
163 }
164 
165 template<>
166 inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data)
167 {
168  switch(info.GetDataType())
169  {
170  case DataType::Signed32:
171  {
172  return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data));
173  }
174  default:
175  {
176  throw InvalidArgumentException("Cannot decode to int32. Unsupported origin Data Type!");
177  break;
178  }
179  }
180  return nullptr;
181 }
182 
183 } //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
armnnUtils::GetPerAxisParams
std::pair< unsigned int, std::vector< float > > GetPerAxisParams(const armnn::TensorInfo &info)
Definition: TensorUtils.cpp:280
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