Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "BaseIterator.hpp" |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 9 | |
| 10 | #include <armnnUtils/FloatingPointConverter.hpp> |
| 11 | #include <armnnUtils/TensorUtils.hpp> |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 12 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 13 | #include <armnn/utility/Assert.hpp> |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 14 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 15 | namespace armnn |
| 16 | { |
| 17 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
| 21 | inline std::unique_ptr<Decoder<float>> MakeSigned32PerAxisDecoder(const TensorInfo& info, const void* data) |
| 22 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 23 | return std::make_unique<ScaledInt32PerAxisDecoder>(static_cast<const int32_t*>(data), info); |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | inline 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 Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 62 | template<typename T> |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 63 | inline std::unique_ptr<Decoder<T>> MakeDecoder(const TensorInfo& info, const void* data = nullptr); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 64 | |
| 65 | template<> |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 66 | inline std::unique_ptr<Decoder<float>> MakeDecoder(const TensorInfo& info, const void* data) |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 67 | { |
| 68 | switch(info.GetDataType()) |
| 69 | { |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 70 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 71 | case armnn::DataType::QuantizedSymm8PerAxis: |
| 72 | { |
| 73 | std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 74 | return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info); |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 75 | } |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 76 | ARMNN_NO_DEPRECATE_WARN_END |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 77 | case DataType::QAsymmS8: |
| 78 | { |
| 79 | return std::make_unique<QASymmS8Decoder>( |
| 80 | static_cast<const int8_t*>(data), |
| 81 | info.GetQuantizationScale(), |
| 82 | info.GetQuantizationOffset()); |
| 83 | } |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 84 | case DataType::QAsymmU8: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 85 | { |
| 86 | return std::make_unique<QASymm8Decoder>( |
| 87 | static_cast<const uint8_t*>(data), |
| 88 | info.GetQuantizationScale(), |
| 89 | info.GetQuantizationOffset()); |
| 90 | } |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 91 | case DataType::QSymmS16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 92 | { |
| 93 | return std::make_unique<QSymm16Decoder>( |
| 94 | static_cast<const int16_t*>(data), |
| 95 | info.GetQuantizationScale(), |
| 96 | info.GetQuantizationOffset()); |
| 97 | } |
Narumol Prangnawarat | 8832522 | 2020-03-06 14:45:57 +0000 | [diff] [blame] | 98 | case DataType::BFloat16: |
| 99 | { |
| 100 | return std::make_unique<BFloat16Decoder>(static_cast<const BFloat16*>(data)); |
| 101 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 102 | case DataType::Float16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 103 | { |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 104 | return std::make_unique<Float16Decoder>(static_cast<const Half*>(data)); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 105 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 106 | case DataType::Float32: |
| 107 | { |
| 108 | return std::make_unique<Float32Decoder>(static_cast<const float*>(data)); |
| 109 | } |
| 110 | case DataType::Signed32: |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 111 | { |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 112 | return MakeSigned32Decoder(info, data); |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 113 | } |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 114 | case DataType::QSymmS8: |
| 115 | { |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 116 | if (info.HasPerAxisQuantization()) |
| 117 | { |
| 118 | std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 119 | return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info); |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 120 | } |
| 121 | else |
| 122 | { |
| 123 | return std::make_unique<QSymmS8Decoder>( |
| 124 | static_cast<const int8_t*>(data), |
| 125 | info.GetQuantizationScale(), |
| 126 | info.GetQuantizationOffset()); |
| 127 | } |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 128 | } |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 129 | case armnn::DataType::Boolean: |
| 130 | { |
| 131 | return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data)); |
| 132 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 133 | default: |
| 134 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 135 | ARMNN_ASSERT_MSG(false, "Unsupported Data Type!"); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 142 | template<> |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 143 | inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data) |
| 144 | { |
| 145 | switch(info.GetDataType()) |
| 146 | { |
| 147 | case DataType::Boolean: |
| 148 | { |
| 149 | return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data)); |
| 150 | } |
| 151 | default: |
| 152 | { |
| 153 | ARMNN_ASSERT_MSG(false, "Unsupported Data Type!"); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | template<> |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 161 | inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data) |
| 162 | { |
| 163 | switch(info.GetDataType()) |
| 164 | { |
| 165 | case DataType::Signed32: |
| 166 | { |
| 167 | return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data)); |
| 168 | } |
| 169 | default: |
| 170 | { |
| 171 | ARMNN_ASSERT_MSG(false, "Unsupported Data Type!"); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 178 | } //namespace armnn |