telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 5 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 6 | #include <aclCommon/ArmComputeUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 7 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 8 | #include "armnn/Exceptions.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | #include <armnn/Descriptors.hpp> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | namespace armcomputetensorutils |
| 14 | { |
| 15 | |
| 16 | arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType) |
| 17 | { |
| 18 | switch(dataType) |
| 19 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | case armnn::DataType::Float16: |
| 21 | return arm_compute::DataType::F16; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | case armnn::DataType::Float32: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | return arm_compute::DataType::F32; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | case armnn::DataType::QuantisedAsymm8: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | return arm_compute::DataType::QASYMM8; |
Aron Virginas-Tar | 7a3e2fe | 2019-06-27 18:54:47 +0100 | [diff] [blame] | 26 | case armnn::DataType::QuantisedSymm16: |
| 27 | return arm_compute::DataType::QSYMM16; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | case armnn::DataType::Signed32: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | return arm_compute::DataType::S32; |
Nattapat Chaimanowong | 8c76cc1 | 2019-01-23 09:59:14 +0000 | [diff] [blame] | 30 | case armnn::DataType::Boolean: |
| 31 | return arm_compute::DataType::U8; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | default: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | BOOST_ASSERT_MSG(false, "Unknown data type"); |
| 34 | return arm_compute::DataType::UNKNOWN; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Matthew Bentham | fd89996 | 2018-12-31 15:49:42 +0000 | [diff] [blame] | 38 | arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions, |
| 39 | unsigned int originalInputRank, |
| 40 | const std::vector<unsigned int>& armnnAxes) |
| 41 | { |
| 42 | arm_compute::Coordinates outAclCoords; |
| 43 | |
| 44 | if (armnnAxes.empty()) |
| 45 | { |
| 46 | // If no reduction axes were provided, then the input must be reduced along all dimensions. |
| 47 | // Since Compute Library does not accept an empty vector as the reduction dimensions, we then |
| 48 | // manually create a vector including all the input dimensions (in reversed order) as: |
| 49 | // |
| 50 | // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 } |
| 51 | // |
| 52 | outAclCoords.set_num_dimensions(inputDimensions); |
| 53 | std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; }); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | // Create a vector of reduction dimensions (in reversed order) with the given reduction axes. |
| 58 | // |
| 59 | // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any |
| 60 | // dimension correction). |
| 61 | // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the |
| 62 | // new value for that reduction axis should be 1. |
| 63 | // |
| 64 | // Example: |
| 65 | // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 } |
| 66 | // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 } |
| 67 | // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 } |
| 68 | // |
| 69 | // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1 |
| 70 | // |
| 71 | outAclCoords.set_num_dimensions(armnnAxes.size()); |
| 72 | std::transform(armnnAxes.begin(), armnnAxes.end(), |
| 73 | outAclCoords.begin(), |
| 74 | [originalInputRank](unsigned int i){ return originalInputRank - i - 1; }); |
| 75 | } |
| 76 | |
| 77 | return outAclCoords; |
| 78 | } |
| 79 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape) |
| 81 | { |
| 82 | arm_compute::TensorShape shape; |
| 83 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 84 | // armnn tensors are (batch, channels, height, width). |
| 85 | // arm_compute tensors are (width, height, channels, batch). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 86 | for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++) |
| 87 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | // Note that our dimensions are stored in the opposite order to ACL's. |
Matthew Bentham | 8910528 | 2018-11-20 14:33:33 +0000 | [diff] [blame] | 89 | shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | |
| 91 | // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | // arm_compute tensors expect this. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // prevent arm_compute issue where tensor is flattened to nothing |
| 96 | if (shape.num_dimensions() == 0) |
| 97 | { |
| 98 | shape.set_num_dimensions(1); |
| 99 | } |
| 100 | |
| 101 | return shape; |
| 102 | } |
| 103 | |
| 104 | // Utility function used to build a TensorInfo object, that can be used to initialise |
| 105 | // ARM Compute Tensor and CLTensor allocators. |
| 106 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo) |
| 107 | { |
| 108 | const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape()); |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 109 | const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType()); |
| 110 | |
| 111 | const arm_compute::QuantizationInfo aclQuantizationInfo = tensorInfo.HasMultipleQuantizationScales() ? |
| 112 | arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScales()) : |
| 113 | arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScale(), tensorInfo.GetQuantizationOffset()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 114 | |
| 115 | return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo); |
| 116 | } |
| 117 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 118 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo, |
| 119 | armnn::DataLayout dataLayout) |
| 120 | { |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 121 | arm_compute::TensorInfo aclTensorInfo = BuildArmComputeTensorInfo(tensorInfo); |
| 122 | aclTensorInfo.set_data_layout(ConvertDataLayout(dataLayout)); |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 123 | |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 124 | return aclTensorInfo; |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 127 | arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout) |
| 128 | { |
| 129 | switch(dataLayout) |
| 130 | { |
| 131 | case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC; |
| 132 | |
| 133 | case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW; |
| 134 | |
| 135 | default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" + |
| 136 | std::to_string(static_cast<int>(dataLayout)) + "]"); |
| 137 | } |
| 138 | } |
| 139 | |
Sadik Armagan | a3600ba | 2019-10-10 10:43:20 +0100 | [diff] [blame] | 140 | arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor, |
| 141 | bool fpMixedPrecision) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 142 | { |
| 143 | using arm_compute::PoolingType; |
| 144 | using arm_compute::DimensionRoundingType; |
| 145 | using arm_compute::PadStrideInfo; |
| 146 | using arm_compute::PoolingLayerInfo; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 147 | using arm_compute::Size2D; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | // Resolve ARM Compute layer parameters. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 150 | const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 151 | |
| 152 | bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0); |
| 153 | //use specific constructor if global pooling |
| 154 | if(isGlobalPooling) |
| 155 | { |
| 156 | return arm_compute::PoolingLayerInfo(poolingType); |
| 157 | } |
| 158 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 159 | const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType( |
| 160 | descriptor.m_OutputShapeRounding); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | const PadStrideInfo padStrideInfo(descriptor.m_StrideX, |
| 162 | descriptor.m_StrideY, |
| 163 | descriptor.m_PadLeft, |
| 164 | descriptor.m_PadRight, |
| 165 | descriptor.m_PadTop, |
| 166 | descriptor.m_PadBottom, |
| 167 | rounding); |
| 168 | |
| 169 | const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude); |
| 170 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 171 | const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight); |
| 172 | |
Sadik Armagan | a3600ba | 2019-10-10 10:43:20 +0100 | [diff] [blame] | 173 | return arm_compute::PoolingLayerInfo(poolingType, poolSize, padStrideInfo, excludePadding, fpMixedPrecision); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor) |
| 177 | { |
| 178 | const arm_compute::NormType normType = |
| 179 | ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType); |
| 180 | return arm_compute::NormalizationLayerInfo(normType, |
| 181 | descriptor.m_NormSize, |
| 182 | descriptor.m_Alpha, |
| 183 | descriptor.m_Beta, |
| 184 | descriptor.m_K, |
| 185 | false); |
| 186 | } |
| 187 | |
| 188 | arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm) |
| 189 | { |
| 190 | arm_compute::PermutationVector aclPerm; |
| 191 | |
| 192 | unsigned int start = 0; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 193 | while ((start < perm.GetSize()) && (start == perm[start])) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 194 | { |
| 195 | ++start; |
| 196 | } |
| 197 | |
| 198 | for (unsigned int i = start; i < perm.GetSize(); ++i) |
| 199 | { |
| 200 | aclPerm.set(i - start, perm[i] - start); |
| 201 | } |
| 202 | |
| 203 | return aclPerm; |
| 204 | } |
| 205 | |
Sadik Armagan | f446432 | 2018-12-20 16:19:12 +0000 | [diff] [blame] | 206 | arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height) |
| 207 | { |
| 208 | return arm_compute::Size2D(width, height); |
| 209 | } |
| 210 | |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 211 | arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue) |
| 212 | { |
| 213 | switch (input.info()->data_type()) |
| 214 | { |
| 215 | case arm_compute::DataType::QASYMM8: |
| 216 | return arm_compute::PixelValue(static_cast<uint8_t>(pixelValue)); |
| 217 | case arm_compute::DataType::QSYMM16: |
| 218 | return arm_compute::PixelValue(static_cast<int16_t>(pixelValue)); |
| 219 | case arm_compute::DataType::F16: |
| 220 | return arm_compute::PixelValue(static_cast<Half>(pixelValue)); |
| 221 | case arm_compute::DataType::F32: |
| 222 | return arm_compute::PixelValue(pixelValue); |
| 223 | default: |
| 224 | throw InvalidArgumentException("Unsupported DataType: [" + |
| 225 | std::to_string(static_cast<int>(input.info()->data_type())) + "]"); |
| 226 | } |
| 227 | } |
| 228 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 229 | } // namespace armcomputetensorutils |
| 230 | } // namespace armnn |