ArmNN
 24.08
Encoders.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 
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  return std::make_unique<QSymm8PerAxisEncoder>(static_cast<int8_t*>(data), info);
42  }
43  else
44  {
45  return std::make_unique<QSymmS8Encoder>(
46  static_cast<int8_t*>(data),
47  info.GetQuantizationScale(),
48  info.GetQuantizationOffset());
49  }
50  }
52  {
53  if (info.HasPerAxisQuantization())
54  {
55  unsigned int axis = info.GetQuantizationDim().value();
56  auto axisDimensionality = info.GetShape()[axis];
57  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
58  return std::make_unique<QSymm16PerAxisEncoder>(
59  static_cast<int16_t*>(data),
60  params.second,
61  params.first,
62  axisDimensionality);
63  }
64  else
65  {
66  return std::make_unique<QSymm16Encoder>(
67  static_cast<int16_t *>(data),
68  info.GetQuantizationScale(),
69  info.GetQuantizationOffset());
70  }
71  }
73  {
74  return std::make_unique<Int32Encoder>(static_cast<int32_t*>(data));
75  }
77  {
78  return std::make_unique<Float16Encoder>(static_cast<Half*>(data));
79  }
81  {
82  return std::make_unique<Float32Encoder>(static_cast<float*>(data));
83  }
84  default:
85  {
86  throw InvalidArgumentException("Unsupported target Data Type!");
87  break;
88  }
89  }
90  return nullptr;
91 }
92 
93 template<>
94 inline std::unique_ptr<Encoder<double_t>> MakeEncoder(const TensorInfo& info, void* data)
95 {
96  switch(info.GetDataType())
97  {
99  {
100  return std::make_unique<Int64Encoder>(static_cast<int64_t*>(data));
101  }
102  default:
103  {
104  throw InvalidArgumentException("Cannot encode from double. Unsupported target Data Type!");
105  break;
106  }
107  }
108  return nullptr;
109 }
110 
111 template<>
112 inline std::unique_ptr<Encoder<bool>> MakeEncoder(const TensorInfo& info, void* data)
113 {
114  switch(info.GetDataType())
115  {
117  {
118  return std::make_unique<BooleanEncoder>(static_cast<uint8_t*>(data));
119  }
120  default:
121  {
122  throw InvalidArgumentException("Cannot encode from boolean. Unsupported target Data Type!");
123  break;
124  }
125  }
126  return nullptr;
127 }
128 
129 template<>
130 inline std::unique_ptr<Encoder<int32_t>> MakeEncoder(const TensorInfo& info, void* data)
131 {
132  switch(info.GetDataType())
133  {
134  case DataType::Signed32:
135  {
136  return std::make_unique<Int32ToInt32tEncoder>(static_cast<int32_t*>(data));
137  }
138  default:
139  {
140  throw InvalidArgumentException("Cannot encode from int32. Unsupported Data Type!");
141  break;
142  }
143  }
144  return nullptr;
145 }
146 
147 } //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