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 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 13 | #include <boost/assert.hpp> |
| 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 | { |
| 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 | |
| 30 | inline 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 Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 66 | template<typename T> |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 67 | 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] | 68 | |
| 69 | template<> |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | switch(info.GetDataType()) |
| 73 | { |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 74 | case armnn::DataType::QuantizedSymm8PerAxis: |
| 75 | { |
| 76 | std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info); |
| 77 | return std::make_unique<QSymm8PerAxisDecoder>( |
| 78 | static_cast<const int8_t*>(data), |
| 79 | params.second, |
| 80 | params.first); |
| 81 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 82 | case DataType::QuantisedAsymm8: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 83 | { |
| 84 | return std::make_unique<QASymm8Decoder>( |
| 85 | static_cast<const uint8_t*>(data), |
| 86 | info.GetQuantizationScale(), |
| 87 | info.GetQuantizationOffset()); |
| 88 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 89 | case DataType::QuantisedSymm16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 90 | { |
| 91 | return std::make_unique<QSymm16Decoder>( |
| 92 | static_cast<const int16_t*>(data), |
| 93 | info.GetQuantizationScale(), |
| 94 | info.GetQuantizationOffset()); |
| 95 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 96 | case DataType::Float16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 97 | { |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 98 | return std::make_unique<Float16Decoder>(static_cast<const Half*>(data)); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 99 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 100 | case DataType::Float32: |
| 101 | { |
| 102 | return std::make_unique<Float32Decoder>(static_cast<const float*>(data)); |
| 103 | } |
| 104 | case DataType::Signed32: |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 105 | { |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 106 | return MakeSigned32Decoder(info, data); |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 107 | } |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 108 | case DataType::QSymmS8: |
| 109 | { |
| 110 | return std::make_unique<QSymmS8Decoder>( |
| 111 | static_cast<const int8_t*>(data), |
| 112 | info.GetQuantizationScale(), |
| 113 | info.GetQuantizationOffset()); |
| 114 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 115 | default: |
| 116 | { |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 117 | BOOST_ASSERT_MSG(false, "Unsupported Data Type!"); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | } |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 124 | } //namespace armnn |