blob: 6a8c756048c2a025b91a0f96f30eb0f68cffb8b3 [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
Francis Murtagh43aec582019-05-27 12:14:10 +010013#include <boost/assert.hpp>
14
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{
23 auto params = armnnUtils::GetPerAxisParams(info);
24 return std::make_unique<ScaledInt32PerAxisDecoder>(
25 static_cast<const int32_t*>(data),
26 params.second,
27 params.first);
28}
29
30inline std::unique_ptr<Decoder<float>> MakeSigned32Decoder(const TensorInfo& info, const void* data)
31{
32 if(info.HasMultipleQuantizationScales())
33 {
34 // NOTE: If we have multiple quantization scales, we create a ScaledInt32PerAxisDecoder.
35 // This will be used to decode per-axis quantized convolution biases.
36 return MakeSigned32PerAxisDecoder(info, data);
37 }
38 else
39 {
40 if (info.GetQuantizationDim().has_value())
41 {
42 // NOTE: Even though we only have a single quantization scale, if the quantization
43 // dimension is set, the tensor has per-axis quantization and we need to create a
44 // ScaledInt32PerAxisDecoder
45 return MakeSigned32PerAxisDecoder(info, data);
46 }
47
48 const float scale = info.GetQuantizationScale();
49 if (scale == 0.f)
50 {
51 // NOTE:: If no quantization scale is set, we create an Int32Decoder, which simply
52 // casts the int value to float. This will be used for any INT32 data other than
53 // convolution biases.
54 return std::make_unique<Int32Decoder>(static_cast<const int32_t*>(data));
55 }
56
57 // NOTE: If we only have a single (non-zero) quantization scale and no quantization
58 // dimension is specified, we need to create a ScaledInt32Decoder. This will be used
59 // to decode per-tensor quantized convolution biases.
60 return std::make_unique<ScaledInt32Decoder>(static_cast<const int32_t*>(data), scale);
61 }
62}
63
64} // anonymous namespace
65
Derek Lambertif30f7d32019-04-09 10:25:02 +010066template<typename T>
Matthew Benthamc394a6d2019-06-24 12:51:25 +010067inline std::unique_ptr<Decoder<T>> MakeDecoder(const TensorInfo& info, const void* data = nullptr);
Derek Lambertif30f7d32019-04-09 10:25:02 +010068
69template<>
Nattapat Chaimanowongae2c5f02019-04-24 16:19:57 +010070inline std::unique_ptr<Decoder<float>> MakeDecoder(const TensorInfo& info, const void* data)
Derek Lambertif30f7d32019-04-09 10:25:02 +010071{
72 switch(info.GetDataType())
73 {
Derek Lambertid466a542020-01-22 15:37:29 +000074 ARMNN_NO_DEPRECATE_WARN_BEGIN
Keith Davis5236e1d2019-11-04 08:58:33 +000075 case armnn::DataType::QuantizedSymm8PerAxis:
76 {
77 std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
78 return std::make_unique<QSymm8PerAxisDecoder>(
79 static_cast<const int8_t*>(data),
80 params.second,
81 params.first);
82 }
Derek Lambertid466a542020-01-22 15:37:29 +000083 ARMNN_NO_DEPRECATE_WARN_END
Ryan OShea9add1202020-02-07 10:06:33 +000084 case DataType::QAsymmS8:
85 {
86 return std::make_unique<QASymmS8Decoder>(
87 static_cast<const int8_t*>(data),
88 info.GetQuantizationScale(),
89 info.GetQuantizationOffset());
90 }
Derek Lambertif90c56d2020-01-10 17:14:08 +000091 case DataType::QAsymmU8:
Derek Lambertif30f7d32019-04-09 10:25:02 +010092 {
93 return std::make_unique<QASymm8Decoder>(
94 static_cast<const uint8_t*>(data),
95 info.GetQuantizationScale(),
96 info.GetQuantizationOffset());
97 }
Derek Lambertif90c56d2020-01-10 17:14:08 +000098 case DataType::QSymmS16:
Derek Lambertif30f7d32019-04-09 10:25:02 +010099 {
100 return std::make_unique<QSymm16Decoder>(
101 static_cast<const int16_t*>(data),
102 info.GetQuantizationScale(),
103 info.GetQuantizationOffset());
104 }
Matthew Jacksone69c3992019-09-09 14:31:21 +0100105 case DataType::Float16:
Derek Lambertif30f7d32019-04-09 10:25:02 +0100106 {
Matthew Jacksone69c3992019-09-09 14:31:21 +0100107 return std::make_unique<Float16Decoder>(static_cast<const Half*>(data));
Derek Lambertif30f7d32019-04-09 10:25:02 +0100108 }
Matthew Jacksone69c3992019-09-09 14:31:21 +0100109 case DataType::Float32:
110 {
111 return std::make_unique<Float32Decoder>(static_cast<const float*>(data));
112 }
113 case DataType::Signed32:
Mike Kelly9b398322019-05-22 17:21:49 +0100114 {
Aron Virginas-Tarb67f9572019-11-04 15:00:19 +0000115 return MakeSigned32Decoder(info, data);
Mike Kelly9b398322019-05-22 17:21:49 +0100116 }
Finn Williamsfd271062019-12-04 14:27:27 +0000117 case DataType::QSymmS8:
118 {
Derek Lambertid466a542020-01-22 15:37:29 +0000119 if (info.HasPerAxisQuantization())
120 {
121 std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
122 return std::make_unique<QSymm8PerAxisDecoder>(
123 static_cast<const int8_t*>(data),
124 params.second,
125 params.first);
126 }
127 else
128 {
129 return std::make_unique<QSymmS8Decoder>(
130 static_cast<const int8_t*>(data),
131 info.GetQuantizationScale(),
132 info.GetQuantizationOffset());
133 }
Finn Williamsfd271062019-12-04 14:27:27 +0000134 }
Derek Lambertif30f7d32019-04-09 10:25:02 +0100135 default:
136 {
Keith Davis5236e1d2019-11-04 08:58:33 +0000137 BOOST_ASSERT_MSG(false, "Unsupported Data Type!");
Derek Lambertif30f7d32019-04-09 10:25:02 +0100138 break;
139 }
140 }
141 return nullptr;
142}
143
Nattapat Chaimanowongae2c5f02019-04-24 16:19:57 +0100144} //namespace armnn