ArmNN  NotReleased
Encoders.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 
11 
12 #include <boost/assert.hpp>
13 
14 namespace armnn
15 {
16 
17 template<typename T>
18 inline std::unique_ptr<Encoder<T>> MakeEncoder(const TensorInfo& info, void* data = nullptr);
19 
20 template<>
21 inline std::unique_ptr<Encoder<float>> MakeEncoder(const TensorInfo& info, void* data)
22 {
23  switch(info.GetDataType())
24  {
27  {
28  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
29  return std::make_unique<QSymm8PerAxisEncoder>(
30  static_cast<int8_t*>(data),
31  params.second,
32  params.first);
33  }
36  {
37  return std::make_unique<QASymmS8Encoder>(
38  static_cast<int8_t*>(data),
39  info.GetQuantizationScale(),
40  info.GetQuantizationOffset());
41  }
43  {
44  return std::make_unique<QASymm8Encoder>(
45  static_cast<uint8_t*>(data),
46  info.GetQuantizationScale(),
47  info.GetQuantizationOffset());
48  }
49  case DataType::QSymmS8:
50  {
51  if (info.HasPerAxisQuantization())
52  {
53  std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
54  return std::make_unique<QSymm8PerAxisEncoder>(
55  static_cast<int8_t*>(data),
56  params.second,
57  params.first);
58  }
59  else
60  {
61  return std::make_unique<QSymmS8Encoder>(
62  static_cast<int8_t*>(data),
63  info.GetQuantizationScale(),
64  info.GetQuantizationOffset());
65  }
66  }
68  {
69  return std::make_unique<QSymm16Encoder>(
70  static_cast<int16_t*>(data),
71  info.GetQuantizationScale(),
72  info.GetQuantizationOffset());
73  }
75  {
76  return std::make_unique<Int32Encoder>(static_cast<int32_t*>(data));
77  }
79  {
80  return std::make_unique<Float16Encoder>(static_cast<Half*>(data));
81  }
83  {
84  return std::make_unique<Float32Encoder>(static_cast<float*>(data));
85  }
86  default:
87  {
88  BOOST_ASSERT_MSG(false, "Unsupported target Data Type!");
89  break;
90  }
91  }
92  return nullptr;
93 }
94 
95 template<>
96 inline std::unique_ptr<Encoder<bool>> MakeEncoder(const TensorInfo& info, void* data)
97 {
98  switch(info.GetDataType())
99  {
101  {
102  return std::make_unique<BooleanEncoder>(static_cast<uint8_t*>(data));
103  }
104  default:
105  {
106  BOOST_ASSERT_MSG(false, "Cannot encode from boolean. Not supported target Data Type!");
107  break;
108  }
109  }
110  return nullptr;
111 }
112 
113 } //namespace armnn
bool HasPerAxisQuantization() const
Definition: Tensor.cpp:232
half_float::half Half
Definition: Half.hpp:16
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:264
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
Definition: Encoders.hpp:21
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
std::pair< unsigned int, std::vector< float > > GetPerAxisParams(const armnn::TensorInfo &info)
DataType GetDataType() const
Definition: Tensor.hpp:95
float GetQuantizationScale() const
Definition: Tensor.cpp:247
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34