blob: 54e7008d500986aa3d478b4f6cb18ba9119cba8f [file] [log] [blame]
Derek Lambertif30f7d32019-04-09 10:25:02 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "BaseIterator.hpp"
Matteo Martincighe011d202019-11-28 11:35:47 +00009
10#include <armnnUtils/FloatingPointConverter.hpp>
11#include <armnnUtils/TensorUtils.hpp>
Derek Lambertif30f7d32019-04-09 10:25:02 +010012
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010013#include <armnn/utility/Assert.hpp>
Francis Murtagh43aec582019-05-27 12:14:10 +010014
Derek Lambertif30f7d32019-04-09 10:25:02 +010015namespace armnn
16{
17
Aron Virginas-Tarb67f9572019-11-04 15:00:19 +000018namespace
19{
20
21inline std::unique_ptr<Decoder<float>> MakeSigned32PerAxisDecoder(const TensorInfo& info, const void* data)
22{
Jan Eilers53ef7952021-06-02 12:01:25 +010023 return std::make_unique<ScaledInt32PerAxisDecoder>(static_cast<const int32_t*>(data), info);
Aron Virginas-Tarb67f9572019-11-04 15:00:19 +000024}
25
26inline std::unique_ptr<Decoder<float>> MakeSigned32Decoder(const TensorInfo& info, const void* data)
27{
28 if(info.HasMultipleQuantizationScales())
29 {
30 // NOTE: If we have multiple quantization scales, we create a ScaledInt32PerAxisDecoder.
31 // This will be used to decode per-axis quantized convolution biases.
32 return MakeSigned32PerAxisDecoder(info, data);
33 }
34 else
35 {
36 if (info.GetQuantizationDim().has_value())
37 {
38 // NOTE: Even though we only have a single quantization scale, if the quantization
39 // dimension is set, the tensor has per-axis quantization and we need to create a
40 // ScaledInt32PerAxisDecoder
41 return MakeSigned32PerAxisDecoder(info, data);
42 }
43
44 const float scale = info.GetQuantizationScale();
45 if (scale == 0.f)
46 {
47 // NOTE:: If no quantization scale is set, we create an Int32Decoder, which simply
48 // casts the int value to float. This will be used for any INT32 data other than
49 // convolution biases.
50 return std::make_unique<Int32Decoder>(static_cast<const int32_t*>(data));
51 }
52
53 // NOTE: If we only have a single (non-zero) quantization scale and no quantization
54 // dimension is specified, we need to create a ScaledInt32Decoder. This will be used
55 // to decode per-tensor quantized convolution biases.
56 return std::make_unique<ScaledInt32Decoder>(static_cast<const int32_t*>(data), scale);
57 }
58}
59
60} // anonymous namespace
61
Derek Lambertif30f7d32019-04-09 10:25:02 +010062template<typename T>
Matthew Benthamc394a6d2019-06-24 12:51:25 +010063inline std::unique_ptr<Decoder<T>> MakeDecoder(const TensorInfo& info, const void* data = nullptr);
Derek Lambertif30f7d32019-04-09 10:25:02 +010064
65template<>
Nattapat Chaimanowongae2c5f02019-04-24 16:19:57 +010066inline std::unique_ptr<Decoder<float>> MakeDecoder(const TensorInfo& info, const void* data)
Derek Lambertif30f7d32019-04-09 10:25:02 +010067{
68 switch(info.GetDataType())
69 {
Ryan OShea9add1202020-02-07 10:06:33 +000070 case DataType::QAsymmS8:
71 {
72 return std::make_unique<QASymmS8Decoder>(
73 static_cast<const int8_t*>(data),
74 info.GetQuantizationScale(),
75 info.GetQuantizationOffset());
76 }
Derek Lambertif90c56d2020-01-10 17:14:08 +000077 case DataType::QAsymmU8:
Derek Lambertif30f7d32019-04-09 10:25:02 +010078 {
79 return std::make_unique<QASymm8Decoder>(
80 static_cast<const uint8_t*>(data),
81 info.GetQuantizationScale(),
82 info.GetQuantizationOffset());
83 }
Derek Lambertif90c56d2020-01-10 17:14:08 +000084 case DataType::QSymmS16:
Derek Lambertif30f7d32019-04-09 10:25:02 +010085 {
86 return std::make_unique<QSymm16Decoder>(
87 static_cast<const int16_t*>(data),
88 info.GetQuantizationScale(),
89 info.GetQuantizationOffset());
90 }
Matthew Jacksone69c3992019-09-09 14:31:21 +010091 case DataType::Float16:
Derek Lambertif30f7d32019-04-09 10:25:02 +010092 {
Matthew Jacksone69c3992019-09-09 14:31:21 +010093 return std::make_unique<Float16Decoder>(static_cast<const Half*>(data));
Derek Lambertif30f7d32019-04-09 10:25:02 +010094 }
Matthew Jacksone69c3992019-09-09 14:31:21 +010095 case DataType::Float32:
96 {
97 return std::make_unique<Float32Decoder>(static_cast<const float*>(data));
98 }
99 case DataType::Signed32:
Mike Kelly9b398322019-05-22 17:21:49 +0100100 {
Aron Virginas-Tarb67f9572019-11-04 15:00:19 +0000101 return MakeSigned32Decoder(info, data);
Mike Kelly9b398322019-05-22 17:21:49 +0100102 }
Finn Williamsfd271062019-12-04 14:27:27 +0000103 case DataType::QSymmS8:
104 {
Derek Lambertid466a542020-01-22 15:37:29 +0000105 if (info.HasPerAxisQuantization())
106 {
107 std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
Jan Eilers53ef7952021-06-02 12:01:25 +0100108 return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info);
Derek Lambertid466a542020-01-22 15:37:29 +0000109 }
110 else
111 {
112 return std::make_unique<QSymmS8Decoder>(
113 static_cast<const int8_t*>(data),
114 info.GetQuantizationScale(),
115 info.GetQuantizationOffset());
116 }
Finn Williamsfd271062019-12-04 14:27:27 +0000117 }
Sadik Armaganb60dd242020-03-19 13:53:16 +0000118 case armnn::DataType::Boolean:
119 {
120 return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data));
121 }
Derek Lambertif30f7d32019-04-09 10:25:02 +0100122 default:
123 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100124 ARMNN_ASSERT_MSG(false, "Unsupported Data Type!");
Derek Lambertif30f7d32019-04-09 10:25:02 +0100125 break;
126 }
127 }
128 return nullptr;
129}
130
Finn Williamscbd2c232020-06-22 15:58:32 +0100131template<>
James Conroyaba90cd2020-11-06 16:28:18 +0000132inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data)
133{
134 switch(info.GetDataType())
135 {
136 case DataType::Boolean:
137 {
138 return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data));
139 }
140 default:
141 {
142 ARMNN_ASSERT_MSG(false, "Unsupported Data Type!");
143 break;
144 }
145 }
146 return nullptr;
147}
148
149template<>
Finn Williamscbd2c232020-06-22 15:58:32 +0100150inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data)
151{
152 switch(info.GetDataType())
153 {
154 case DataType::Signed32:
155 {
156 return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data));
157 }
158 default:
159 {
160 ARMNN_ASSERT_MSG(false, "Unsupported Data Type!");
161 break;
162 }
163 }
164 return nullptr;
165}
166
Nattapat Chaimanowongae2c5f02019-04-24 16:19:57 +0100167} //namespace armnn