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 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 16 | namespace |
| 17 | { |
| 18 | |
| 19 | inline std::unique_ptr<Decoder<float>> MakeSigned32PerAxisDecoder(const TensorInfo& info, const void* data) |
| 20 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 21 | 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] | 22 | } |
| 23 | |
| 24 | inline 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 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 60 | template<typename T> |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 61 | 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] | 62 | |
| 63 | template<> |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 64 | 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] | 65 | { |
| 66 | switch(info.GetDataType()) |
| 67 | { |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 68 | case DataType::QAsymmS8: |
| 69 | { |
| 70 | return std::make_unique<QASymmS8Decoder>( |
| 71 | static_cast<const int8_t*>(data), |
| 72 | info.GetQuantizationScale(), |
| 73 | info.GetQuantizationOffset()); |
| 74 | } |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 75 | case DataType::QAsymmU8: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 76 | { |
| 77 | return std::make_unique<QASymm8Decoder>( |
| 78 | static_cast<const uint8_t*>(data), |
| 79 | info.GetQuantizationScale(), |
| 80 | info.GetQuantizationOffset()); |
| 81 | } |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 82 | case DataType::QSymmS16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 83 | { |
| 84 | return std::make_unique<QSymm16Decoder>( |
| 85 | static_cast<const int16_t*>(data), |
| 86 | info.GetQuantizationScale(), |
| 87 | info.GetQuantizationOffset()); |
| 88 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 89 | case DataType::Float16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 90 | { |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 91 | return std::make_unique<Float16Decoder>(static_cast<const Half*>(data)); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 92 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 93 | case DataType::Float32: |
| 94 | { |
| 95 | return std::make_unique<Float32Decoder>(static_cast<const float*>(data)); |
| 96 | } |
| 97 | case DataType::Signed32: |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 98 | { |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 99 | return MakeSigned32Decoder(info, data); |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 100 | } |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 101 | case DataType::QSymmS8: |
| 102 | { |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 103 | if (info.HasPerAxisQuantization()) |
| 104 | { |
| 105 | std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 106 | return std::make_unique<QSymm8PerAxisDecoder>(static_cast<const int8_t*>(data), info); |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
| 110 | return std::make_unique<QSymmS8Decoder>( |
| 111 | static_cast<const int8_t*>(data), |
| 112 | info.GetQuantizationScale(), |
| 113 | info.GetQuantizationOffset()); |
| 114 | } |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 115 | } |
Sadik Armagan | b60dd24 | 2020-03-19 13:53:16 +0000 | [diff] [blame] | 116 | case armnn::DataType::Boolean: |
| 117 | { |
| 118 | return std::make_unique<BooleanDecoder>(static_cast<const uint8_t*>(data)); |
| 119 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 120 | default: |
| 121 | { |
Teresa Charlin | 5306dc8 | 2023-10-30 22:29:58 +0000 | [diff] [blame] | 122 | throw InvalidArgumentException("Unsupported target Data Type!"); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | template<> |
| 130 | inline std::unique_ptr<Decoder<double_t>> MakeDecoder(const TensorInfo& info, const void* data) |
| 131 | { |
| 132 | switch(info.GetDataType()) |
| 133 | { |
| 134 | case DataType::Signed64: |
| 135 | { |
| 136 | return std::make_unique<Int64Decoder>(static_cast<const int64_t*>(data)); |
| 137 | } |
| 138 | default: |
| 139 | { |
| 140 | throw InvalidArgumentException("Cannot decode to double. Unsupported origin Data Type!"); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | } |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 147 | template<> |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 148 | inline std::unique_ptr<Decoder<bool>> MakeDecoder(const TensorInfo& info, const void* data) |
| 149 | { |
| 150 | switch(info.GetDataType()) |
| 151 | { |
| 152 | case DataType::Boolean: |
| 153 | { |
| 154 | return std::make_unique<BooleanDecoderBool>(static_cast<const uint8_t*>(data)); |
| 155 | } |
| 156 | default: |
| 157 | { |
Teresa Charlin | 5306dc8 | 2023-10-30 22:29:58 +0000 | [diff] [blame] | 158 | throw InvalidArgumentException("Cannot decode to bool. Unsupported origin Data Type!"); |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | } |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | template<> |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 166 | inline std::unique_ptr<Decoder<int32_t>> MakeDecoder(const TensorInfo& info, const void* data) |
| 167 | { |
| 168 | switch(info.GetDataType()) |
| 169 | { |
| 170 | case DataType::Signed32: |
| 171 | { |
| 172 | return std::make_unique<Int32ToInt32tDecoder>(static_cast<const int32_t*>(data)); |
| 173 | } |
| 174 | default: |
| 175 | { |
Teresa Charlin | 5306dc8 | 2023-10-30 22:29:58 +0000 | [diff] [blame] | 176 | throw InvalidArgumentException("Cannot decode to int32. Unsupported origin Data Type!"); |
Finn Williams | cbd2c23 | 2020-06-22 15:58:32 +0100 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | } |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 183 | } //namespace armnn |