ArmNN
 23.11
Encoders.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 "BaseIterator.hpp"
9 
11 
12 namespace armnn
13 {
14 
15 template<typename T>
16 inline std::unique_ptr<Encoder<T>> MakeEncoder(const TensorInfo& info, void* data = nullptr);
17 
18 template<>
19 inline std::unique_ptr<Encoder<float>> MakeEncoder(const TensorInfo& info, void* data)
20 {
21  switch(info.GetDataType())
22  {
24  {
25  return std::make_unique<QASymmS8Encoder>(
26  static_cast<int8_t*>(data),
27  info.GetQuantizationScale(),
28  info.GetQuantizationOffset());
29  }
31  {
32  return std::make_unique<QASymm8Encoder>(
33  static_cast<uint8_t*>(data),
34  info.GetQuantizationScale(),
35  info.GetQuantizationOffset());
36  }
37  case DataType::QSymmS8:
38  {
39  if (info.HasPerAxisQuantization())
40  {
41  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
42  return std::make_unique<QSymm8PerAxisEncoder>(
43  static_cast<int8_t*>(data),
44  params.second,
45  params.first);
46  }
47  else
48  {
49  return std::make_unique<QSymmS8Encoder>(
50  static_cast<int8_t*>(data),
51  info.GetQuantizationScale(),
52  info.GetQuantizationOffset());
53  }
54  }
56  {
57  if (info.HasPerAxisQuantization())
58  {
59  unsigned int axis = info.GetQuantizationDim().value();
60  auto axisDimensionality = info.GetShape()[axis];
61  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
62  return std::make_unique<QSymm16PerAxisEncoder>(
63  static_cast<int16_t*>(data),
64  params.second,
65  params.first,
66  axisDimensionality);
67  }
68  else
69  {
70  return std::make_unique<QSymm16Encoder>(
71  static_cast<int16_t *>(data),
72  info.GetQuantizationScale(),
73  info.GetQuantizationOffset());
74  }
75  }
77  {
78  return std::make_unique<Int32Encoder>(static_cast<int32_t*>(data));
79  }
81  {
82  return std::make_unique<Float16Encoder>(static_cast<Half*>(data));
83  }
85  {
86  return std::make_unique<Float32Encoder>(static_cast<float*>(data));
87  }
88  default:
89  {
90  throw InvalidArgumentException("Unsupported target Data Type!");
91  break;
92  }
93  }
94  return nullptr;
95 }
96 
97 template<>
98 inline std::unique_ptr<Encoder<double_t>> MakeEncoder(const TensorInfo& info, void* data)
99 {
100  switch(info.GetDataType())
101  {
103  {
104  return std::make_unique<Int64Encoder>(static_cast<int64_t*>(data));
105  }
106  default:
107  {
108  throw InvalidArgumentException("Cannot encode from double. Unsupported target Data Type!");
109  break;
110  }
111  }
112  return nullptr;
113 }
114 
115 template<>
116 inline std::unique_ptr<Encoder<bool>> MakeEncoder(const TensorInfo& info, void* data)
117 {
118  switch(info.GetDataType())
119  {
121  {
122  return std::make_unique<BooleanEncoder>(static_cast<uint8_t*>(data));
123  }
124  default:
125  {
126  throw InvalidArgumentException("Cannot encode from boolean. Unsupported target Data Type!");
127  break;
128  }
129  }
130  return nullptr;
131 }
132 
133 template<>
134 inline std::unique_ptr<Encoder<int32_t>> MakeEncoder(const TensorInfo& info, void* data)
135 {
136  switch(info.GetDataType())
137  {
138  case DataType::Signed32:
139  {
140  return std::make_unique<Int32ToInt32tEncoder>(static_cast<int32_t*>(data));
141  }
142  default:
143  {
144  throw InvalidArgumentException("Cannot encode from int32. Unsupported Data Type!");
145  break;
146  }
147  }
148  return nullptr;
149 }
150 
151 } //namespace armnn
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::MakeEncoder
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
Definition: Encoders.hpp:19
armnn::BoostLogSeverityMapping::info
@ info
armnn::DataType::Signed32
@ Signed32
armnn::DataType::QAsymmS8
@ QAsymmS8
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::DataType::Signed64
@ Signed64