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" |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 9 | #include "FloatingPointConverter.hpp" |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 10 | #include "TensorUtils.hpp" |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 11 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 12 | #include <boost/assert.hpp> |
| 13 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | inline std::unique_ptr<Decoder<float>> MakeSigned32PerAxisDecoder(const TensorInfo& info, const void* data) |
| 21 | { |
| 22 | auto params = armnnUtils::GetPerAxisParams(info); |
| 23 | return std::make_unique<ScaledInt32PerAxisDecoder>( |
| 24 | static_cast<const int32_t*>(data), |
| 25 | params.second, |
| 26 | params.first); |
| 27 | } |
| 28 | |
| 29 | inline std::unique_ptr<Decoder<float>> MakeSigned32Decoder(const TensorInfo& info, const void* data) |
| 30 | { |
| 31 | if(info.HasMultipleQuantizationScales()) |
| 32 | { |
| 33 | // NOTE: If we have multiple quantization scales, we create a ScaledInt32PerAxisDecoder. |
| 34 | // This will be used to decode per-axis quantized convolution biases. |
| 35 | return MakeSigned32PerAxisDecoder(info, data); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | if (info.GetQuantizationDim().has_value()) |
| 40 | { |
| 41 | // NOTE: Even though we only have a single quantization scale, if the quantization |
| 42 | // dimension is set, the tensor has per-axis quantization and we need to create a |
| 43 | // ScaledInt32PerAxisDecoder |
| 44 | return MakeSigned32PerAxisDecoder(info, data); |
| 45 | } |
| 46 | |
| 47 | const float scale = info.GetQuantizationScale(); |
| 48 | if (scale == 0.f) |
| 49 | { |
| 50 | // NOTE:: If no quantization scale is set, we create an Int32Decoder, which simply |
| 51 | // casts the int value to float. This will be used for any INT32 data other than |
| 52 | // convolution biases. |
| 53 | return std::make_unique<Int32Decoder>(static_cast<const int32_t*>(data)); |
| 54 | } |
| 55 | |
| 56 | // NOTE: If we only have a single (non-zero) quantization scale and no quantization |
| 57 | // dimension is specified, we need to create a ScaledInt32Decoder. This will be used |
| 58 | // to decode per-tensor quantized convolution biases. |
| 59 | return std::make_unique<ScaledInt32Decoder>(static_cast<const int32_t*>(data), scale); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | } // anonymous namespace |
| 64 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 65 | template<typename T> |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 66 | 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] | 67 | |
| 68 | template<> |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 69 | 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] | 70 | { |
| 71 | switch(info.GetDataType()) |
| 72 | { |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 73 | case armnn::DataType::QuantizedSymm8PerAxis: |
| 74 | { |
| 75 | std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info); |
| 76 | return std::make_unique<QSymm8PerAxisDecoder>( |
| 77 | static_cast<const int8_t*>(data), |
| 78 | params.second, |
| 79 | params.first); |
| 80 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 81 | case DataType::QuantisedAsymm8: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 82 | { |
| 83 | return std::make_unique<QASymm8Decoder>( |
| 84 | static_cast<const uint8_t*>(data), |
| 85 | info.GetQuantizationScale(), |
| 86 | info.GetQuantizationOffset()); |
| 87 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 88 | case DataType::QuantisedSymm16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 89 | { |
| 90 | return std::make_unique<QSymm16Decoder>( |
| 91 | static_cast<const int16_t*>(data), |
| 92 | info.GetQuantizationScale(), |
| 93 | info.GetQuantizationOffset()); |
| 94 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 95 | case DataType::Float16: |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 96 | { |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 97 | return std::make_unique<Float16Decoder>(static_cast<const Half*>(data)); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 98 | } |
Matthew Jackson | e69c399 | 2019-09-09 14:31:21 +0100 | [diff] [blame] | 99 | case DataType::Float32: |
| 100 | { |
| 101 | return std::make_unique<Float32Decoder>(static_cast<const float*>(data)); |
| 102 | } |
| 103 | case DataType::Signed32: |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 104 | { |
Aron Virginas-Tar | b67f957 | 2019-11-04 15:00:19 +0000 | [diff] [blame] | 105 | return MakeSigned32Decoder(info, data); |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 106 | } |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 107 | default: |
| 108 | { |
Keith Davis | 5236e1d | 2019-11-04 08:58:33 +0000 | [diff] [blame] | 109 | BOOST_ASSERT_MSG(false, "Unsupported Data Type!"); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | } |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
Nattapat Chaimanowong | ae2c5f0 | 2019-04-24 16:19:57 +0100 | [diff] [blame] | 116 | } //namespace armnn |