ArmNN
 25.11
Loading...
Searching...
No Matches
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
12namespace armnn
13{
14
15template<typename T>
16inline std::unique_ptr<Encoder<T>> MakeEncoder(const TensorInfo& info, void* data = nullptr);
17
18template<>
19inline 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 }
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
93template<>
94inline 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
111template<>
112inline 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
129template<>
130inline std::unique_ptr<Encoder<int32_t>> MakeEncoder(const TensorInfo& info, void* data)
131{
132 switch(info.GetDataType())
133 {
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
Copyright (c) 2021 ARM Limited and Contributors.
half_float::half Half
Definition Half.hpp:22
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
std::pair< unsigned int, std::vector< float > > GetPerAxisParams(const armnn::TensorInfo &info)