ArmNN
 25.11
Loading...
Searching...
No Matches
Decoders.hpp
Go to the documentation of this file.
1//
2// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "BaseIterator.hpp"
9
12
13namespace armnn
14{
15
16namespace
17{
18
19inline 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
24inline 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
60template<typename T>
61inline std::unique_ptr<Decoder<T>> MakeDecoder(const TensorInfo& info, const void* data = nullptr);
62
63template<>
64inline std::unique_ptr<Decoder<float>> MakeDecoder(const TensorInfo& info, const void* data)
65{
66 switch(info.GetDataType())
67 {
69 {
70 return std::make_unique<QASymmS8Decoder>(
71 static_cast<const int8_t*>(data),
72 info.GetQuantizationScale(),
73 info.GetQuantizationOffset());
74 }
76 {
77 return std::make_unique<QASymm8Decoder>(
78 static_cast<const uint8_t*>(data),
79 info.GetQuantizationScale(),
80 info.GetQuantizationOffset());
81 }
83 {
84 return std::make_unique<QSymm16Decoder>(
85 static_cast<const int16_t*>(data),
86 info.GetQuantizationScale(),
87 info.GetQuantizationOffset());
88 }
90 {
91 return std::make_unique<Float16Decoder>(static_cast<const Half*>(data));
92 }
94 {
95 return std::make_unique<Float32Decoder>(static_cast<const float*>(data));
96 }
98 {
99 return MakeSigned32Decoder(info, data);
100 }
102 {
103 if (info.HasPerAxisQuantization())
104 {
105 return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info);
106 }
107 else
108 {
109 return std::make_unique<QSymmS8Decoder>(
110 static_cast<const int8_t*>(data),
111 info.GetQuantizationScale(),
112 info.GetQuantizationOffset());
113 }
114 }
116 {
117 return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data));
118 }
119 default:
120 {
121 throw InvalidArgumentException("Unsupported target Data Type!");
122 break;
123 }
124 }
125 return nullptr;
126}
127
128template<>
129inline std::unique_ptr<Decoder<double_t>> MakeDecoder(const TensorInfo& info, const void* data)
130{
131 switch(info.GetDataType())
132 {
134 {
135 return std::make_unique<Int64Decoder>(static_cast<const int64_t*>(data));
136 }
137 default:
138 {
139 throw InvalidArgumentException("Cannot decode to double. Unsupported origin Data Type!");
140 break;
141 }
142 }
143 return nullptr;
144}
145
146template<>
147inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data)
148{
149 switch(info.GetDataType())
150 {
152 {
153 return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data));
154 }
155 default:
156 {
157 throw InvalidArgumentException("Cannot decode to bool. Unsupported origin Data Type!");
158 break;
159 }
160 }
161 return nullptr;
162}
163
164template<>
165inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data)
166{
167 switch(info.GetDataType())
168 {
170 {
171 return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data));
172 }
173 default:
174 {
175 throw InvalidArgumentException("Cannot decode to int32. Unsupported origin Data Type!");
176 break;
177 }
178 }
179 return nullptr;
180}
181
182} //namespace armnn
Copyright (c) 2021 ARM Limited and Contributors.
half_float::half Half
Definition Half.hpp:22
std::unique_ptr< Decoder< T > > MakeDecoder(const TensorInfo &info, const void *data=nullptr)