Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 2 | // Copyright © 2017,2022 Arm Ltd and Contributors. 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" |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 9 | #include "ArmComputeUtils.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include <armnn/Descriptors.hpp> |
| 11 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 12 | #include <fmt/format.h> |
| 13 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | namespace armcomputetensorutils |
| 17 | { |
| 18 | |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 19 | arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | { |
| 21 | switch(dataType) |
| 22 | { |
Narumol Prangnawarat | 250d392 | 2020-03-30 16:11:04 +0100 | [diff] [blame] | 23 | case armnn::DataType::BFloat16: |
| 24 | return arm_compute::DataType::BFLOAT16; |
Mike Kelly | 130ec60 | 2019-11-08 12:08:35 +0000 | [diff] [blame] | 25 | case armnn::DataType::Boolean: |
| 26 | return arm_compute::DataType::U8; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 27 | case armnn::DataType::Float16: |
| 28 | return arm_compute::DataType::F16; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | case armnn::DataType::Float32: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | return arm_compute::DataType::F32; |
Ryan OShea | 9add120 | 2020-02-07 10:06:33 +0000 | [diff] [blame] | 31 | case armnn::DataType::QAsymmS8: |
| 32 | return arm_compute::DataType::QASYMM8_SIGNED; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 33 | case armnn::DataType::QAsymmU8: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 34 | return arm_compute::DataType::QASYMM8; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 35 | case armnn::DataType::QSymmS16: |
Aron Virginas-Tar | 7a3e2fe | 2019-06-27 18:54:47 +0100 | [diff] [blame] | 36 | return arm_compute::DataType::QSYMM16; |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 37 | case armnn::DataType::Signed64: |
| 38 | return arm_compute::DataType::S64; |
Finn Williams | fd27106 | 2019-12-04 14:27:27 +0000 | [diff] [blame] | 39 | case armnn::DataType::QSymmS8: |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 40 | { |
| 41 | return multiScales ? arm_compute::DataType::QSYMM8_PER_CHANNEL : arm_compute::DataType::QSYMM8; |
| 42 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 43 | case armnn::DataType::Signed32: |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | return arm_compute::DataType::S32; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 45 | default: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 46 | ARMNN_ASSERT_MSG(false, "Unknown data type"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | return arm_compute::DataType::UNKNOWN; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Cathal Corbett | fd5bec4 | 2022-03-03 15:13:23 +0000 | [diff] [blame] | 51 | armnn::DataType GetArmNNDataType(arm_compute::DataType dataType) |
| 52 | { |
| 53 | switch(dataType) |
| 54 | { |
| 55 | case arm_compute::DataType::BFLOAT16: |
| 56 | return armnn::DataType::BFloat16; |
| 57 | case arm_compute::DataType::U8: |
| 58 | return armnn::DataType::Boolean; |
| 59 | case arm_compute::DataType::F16: |
| 60 | return armnn::DataType::Float16; |
| 61 | case arm_compute::DataType::F32: |
| 62 | return armnn::DataType::Float32; |
| 63 | case arm_compute::DataType::QASYMM8_SIGNED: |
| 64 | return armnn::DataType::QAsymmS8; |
| 65 | case arm_compute::DataType::QASYMM8: |
| 66 | return armnn::DataType::QAsymmU8; |
| 67 | case arm_compute::DataType::QSYMM16: |
| 68 | return armnn::DataType::QSymmS16; |
| 69 | case arm_compute::DataType::S64: |
| 70 | return armnn::DataType::Signed64; |
| 71 | case arm_compute::DataType::QSYMM8_PER_CHANNEL: |
| 72 | return armnn::DataType::QSymmS8; |
| 73 | case arm_compute::DataType::QSYMM8: |
| 74 | return armnn::DataType::QSymmS8; |
| 75 | case arm_compute::DataType::S32: |
| 76 | return armnn::DataType::Signed32; |
| 77 | default: |
| 78 | ARMNN_ASSERT_MSG(false, "Unknown data type"); |
| 79 | return armnn::DataType::Float32; |
| 80 | } |
| 81 | } |
| 82 | |
Matthew Bentham | fd89996 | 2018-12-31 15:49:42 +0000 | [diff] [blame] | 83 | arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions, |
| 84 | unsigned int originalInputRank, |
| 85 | const std::vector<unsigned int>& armnnAxes) |
| 86 | { |
| 87 | arm_compute::Coordinates outAclCoords; |
| 88 | |
| 89 | if (armnnAxes.empty()) |
| 90 | { |
| 91 | // If no reduction axes were provided, then the input must be reduced along all dimensions. |
| 92 | // Since Compute Library does not accept an empty vector as the reduction dimensions, we then |
| 93 | // manually create a vector including all the input dimensions (in reversed order) as: |
| 94 | // |
| 95 | // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 } |
| 96 | // |
| 97 | outAclCoords.set_num_dimensions(inputDimensions); |
| 98 | std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; }); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | // Create a vector of reduction dimensions (in reversed order) with the given reduction axes. |
| 103 | // |
| 104 | // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any |
| 105 | // dimension correction). |
| 106 | // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the |
| 107 | // new value for that reduction axis should be 1. |
| 108 | // |
| 109 | // Example: |
| 110 | // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 } |
| 111 | // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 } |
| 112 | // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 } |
| 113 | // |
| 114 | // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1 |
| 115 | // |
| 116 | outAclCoords.set_num_dimensions(armnnAxes.size()); |
| 117 | std::transform(armnnAxes.begin(), armnnAxes.end(), |
| 118 | outAclCoords.begin(), |
| 119 | [originalInputRank](unsigned int i){ return originalInputRank - i - 1; }); |
| 120 | } |
| 121 | |
| 122 | return outAclCoords; |
| 123 | } |
| 124 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 125 | arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape) |
| 126 | { |
| 127 | arm_compute::TensorShape shape; |
| 128 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 129 | // armnn tensors are (batch, channels, height, width). |
| 130 | // arm_compute tensors are (width, height, channels, batch). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++) |
| 132 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | // 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] | 134 | shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | |
| 136 | // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 137 | // arm_compute tensors expect this. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // prevent arm_compute issue where tensor is flattened to nothing |
| 141 | if (shape.num_dimensions() == 0) |
| 142 | { |
| 143 | shape.set_num_dimensions(1); |
| 144 | } |
| 145 | |
| 146 | return shape; |
| 147 | } |
| 148 | |
| 149 | // Utility function used to build a TensorInfo object, that can be used to initialise |
| 150 | // ARM Compute Tensor and CLTensor allocators. |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 151 | // Note: this utility ignores the value of armnn::TensorInfo.IsConstant(). ACL tensors |
| 152 | // default to constant but Arm NN ones default to non constant. In the cases where |
| 153 | // we expect ACL to treat a tensor as constant that value must be set after this |
| 154 | // utility has been called. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo) |
| 156 | { |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 157 | bool multiScales = tensorInfo.HasMultipleQuantizationScales(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 158 | const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape()); |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 159 | const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType(), multiScales); |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 160 | |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 161 | const arm_compute::QuantizationInfo aclQuantizationInfo = multiScales ? |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 162 | arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScales()) : |
| 163 | arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScale(), tensorInfo.GetQuantizationOffset()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | |
| 165 | return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo); |
| 166 | } |
| 167 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 168 | arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo, |
| 169 | armnn::DataLayout dataLayout) |
| 170 | { |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 171 | arm_compute::TensorInfo aclTensorInfo = BuildArmComputeTensorInfo(tensorInfo); |
| 172 | aclTensorInfo.set_data_layout(ConvertDataLayout(dataLayout)); |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 173 | |
Aron Virginas-Tar | 13b653f | 2019-11-01 11:40:39 +0000 | [diff] [blame] | 174 | return aclTensorInfo; |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 177 | arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout) |
| 178 | { |
| 179 | switch(dataLayout) |
| 180 | { |
| 181 | case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC; |
| 182 | |
| 183 | case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW; |
| 184 | |
Teresa Charlin | ec5f7d1 | 2021-10-22 17:15:00 +0100 | [diff] [blame] | 185 | case armnn::DataLayout::NDHWC : return arm_compute::DataLayout::NDHWC; |
| 186 | |
| 187 | case armnn::DataLayout::NCDHW : return arm_compute::DataLayout::NCDHW; |
| 188 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 189 | default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" + |
| 190 | std::to_string(static_cast<int>(dataLayout)) + "]"); |
| 191 | } |
| 192 | } |
| 193 | |
Sadik Armagan | a3600ba | 2019-10-10 10:43:20 +0100 | [diff] [blame] | 194 | arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor, |
| 195 | bool fpMixedPrecision) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 196 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 197 | // Resolve ARM Compute layer parameters. |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 198 | const arm_compute::PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 200 | const arm_compute::DataLayout dataLayout = ConvertDataLayout(descriptor.m_DataLayout); |
Teresa Charlin | c809a29 | 2020-01-31 10:21:44 +0000 | [diff] [blame] | 201 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 202 | bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0); |
| 203 | //use specific constructor if global pooling |
| 204 | if(isGlobalPooling) |
| 205 | { |
Teresa Charlin | c809a29 | 2020-01-31 10:21:44 +0000 | [diff] [blame] | 206 | return arm_compute::PoolingLayerInfo(poolingType, dataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 209 | const arm_compute::DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 210 | descriptor.m_OutputShapeRounding); |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 211 | const arm_compute::PadStrideInfo padStrideInfo(descriptor.m_StrideX, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 212 | descriptor.m_StrideY, |
| 213 | descriptor.m_PadLeft, |
| 214 | descriptor.m_PadRight, |
| 215 | descriptor.m_PadTop, |
| 216 | descriptor.m_PadBottom, |
| 217 | rounding); |
| 218 | |
| 219 | const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude); |
| 220 | |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 221 | const arm_compute::Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 222 | |
Teresa Charlin | c809a29 | 2020-01-31 10:21:44 +0000 | [diff] [blame] | 223 | return arm_compute::PoolingLayerInfo(poolingType, poolSize, dataLayout, padStrideInfo, excludePadding, |
| 224 | fpMixedPrecision); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Ryan OShea | bab8fa9 | 2022-03-09 10:29:02 +0000 | [diff] [blame] | 227 | arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(const Pooling3dDescriptor& descriptor, |
| 228 | bool fpMixedPrecision) |
| 229 | { |
| 230 | const arm_compute::PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType); |
| 231 | |
| 232 | bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0 && descriptor.m_StrideZ==0); |
| 233 | //use specific constructor if global pooling |
| 234 | if(isGlobalPooling) |
| 235 | { |
| 236 | return arm_compute::Pooling3dLayerInfo(poolingType); |
| 237 | } |
| 238 | |
| 239 | const arm_compute::Size3D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight, descriptor.m_PoolDepth); |
| 240 | |
| 241 | const arm_compute::Size3D stride(descriptor.m_StrideX, |
| 242 | descriptor.m_StrideY, |
| 243 | descriptor.m_StrideZ); |
| 244 | |
| 245 | const arm_compute::Padding3D padding(descriptor.m_PadLeft, |
| 246 | descriptor.m_PadRight, |
| 247 | descriptor.m_PadTop, |
| 248 | descriptor.m_PadBottom, |
| 249 | descriptor.m_PadFront, |
| 250 | descriptor.m_PadBack); |
| 251 | |
| 252 | const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude); |
| 253 | |
| 254 | const arm_compute::DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType( |
| 255 | descriptor.m_OutputShapeRounding); |
| 256 | |
| 257 | return arm_compute::Pooling3dLayerInfo(poolingType, |
| 258 | poolSize, |
| 259 | stride, |
| 260 | padding, |
| 261 | excludePadding, |
| 262 | fpMixedPrecision, |
| 263 | rounding); |
| 264 | } |
| 265 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 266 | arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor) |
| 267 | { |
| 268 | const arm_compute::NormType normType = |
| 269 | ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType); |
| 270 | return arm_compute::NormalizationLayerInfo(normType, |
| 271 | descriptor.m_NormSize, |
| 272 | descriptor.m_Alpha, |
| 273 | descriptor.m_Beta, |
| 274 | descriptor.m_K, |
| 275 | false); |
| 276 | } |
| 277 | |
| 278 | arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm) |
| 279 | { |
| 280 | arm_compute::PermutationVector aclPerm; |
| 281 | |
| 282 | unsigned int start = 0; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 283 | while ((start < perm.GetSize()) && (start == perm[start])) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 284 | { |
| 285 | ++start; |
| 286 | } |
| 287 | |
| 288 | for (unsigned int i = start; i < perm.GetSize(); ++i) |
| 289 | { |
| 290 | aclPerm.set(i - start, perm[i] - start); |
| 291 | } |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 292 | return aclPerm; |
| 293 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 294 | |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 295 | arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm) |
| 296 | { |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 297 | // As ArmNN indexes are left to right and ACL indexes are right to left, |
| 298 | // the permutation vector has to be reversed and then translated into ACL axis. |
| 299 | // i.e. {1, 0, 2, 3} --> {3, 2, 0, 1} --> {0, 1, 3, 2} |
| 300 | |
| 301 | // Below an example of how the ArmNN and ACL index format work: |
| 302 | // ArmNN Format: |
| 303 | // Input Shape {1, 10, 20, 30} |
| 304 | // Permutation Vector {1, 0, 2, 3} |
| 305 | // Output Shape {10, 1, 20, 30} |
| 306 | // dim "1" of input goes into index 0 of the output ([ 10, X, X, X]) |
| 307 | // dim "0" of input goes into index 1 of the output ([ 10, 1, X, X ]) |
| 308 | // dim "2" of input goes into index 2 of the output ([ 10, 1, 20, X ]) |
| 309 | // dim "3" of input goes into index 3 of the output ([ 10, 1, 20, 30 ]) |
| 310 | // ACL Format: |
| 311 | // Input Shape {30, 20, 10, 1} |
| 312 | // Permutation Vector {0, 1, 3, 2} |
| 313 | // Output Shape {30, 20, 1, 10} |
| 314 | // dim "0" of input goes into index 0 of the output ([ 30, X, X, X]) |
| 315 | // dim "1" of input goes into index 1 of the output ([ 30, 20, X, X ]) |
| 316 | // dim "3" of input goes into index 2 of the output ([ 30, 20, 1, X ]) |
| 317 | // dim "2" of input goes into index 3 of the output ([ 30, 20, 1, 10 ]) |
| 318 | |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 319 | arm_compute::PermutationVector aclPerm; |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 320 | auto rank = perm.GetSize(); |
| 321 | |
| 322 | // Reverse the order. i.e. {1, 0, 2, 3} --> {3, 2, 0, 1} |
| 323 | std::vector<unsigned int> reversedPerm; |
| 324 | reversedPerm.reserve(rank); |
| 325 | for (unsigned int i = rank; i > 0; --i) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 326 | { |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 327 | reversedPerm.push_back(perm[i-1]); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 330 | // Translate from Arm NN axis to ACL axis. i.e. {3, 2, 0, 1} --> {0, 1, 3, 2} |
| 331 | for (unsigned int i = 0; i < rank; ++i) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 332 | { |
Teresa Charlin | 6bc8525 | 2022-12-06 20:43:06 +0000 | [diff] [blame^] | 333 | auto aclAxis = rank - 1 - reversedPerm[i]; |
| 334 | aclPerm.set(i, aclAxis); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 335 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 336 | return aclPerm; |
| 337 | } |
| 338 | |
Sadik Armagan | f446432 | 2018-12-20 16:19:12 +0000 | [diff] [blame] | 339 | arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height) |
| 340 | { |
| 341 | return arm_compute::Size2D(width, height); |
| 342 | } |
| 343 | |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 344 | arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float value) |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 345 | { |
Matthew Sloyan | 2e5d0b2 | 2021-10-21 14:05:31 +0100 | [diff] [blame] | 346 | switch (tensorInfo->data_type()) |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 347 | { |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 348 | case arm_compute::DataType::F16: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 349 | { |
| 350 | arm_compute::PixelValue pixelValue = arm_compute::PixelValue(static_cast<Half>(value)); |
| 351 | if (isinf(pixelValue.get<Half>())) { |
| 352 | throw InvalidArgumentException("Under/Overflow converting float value [" + std::to_string(value) + |
| 353 | "] to fp16: [" + std::to_string(pixelValue.get<Half>()) + "]"); |
| 354 | } |
| 355 | return pixelValue; |
| 356 | } |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 357 | case arm_compute::DataType::F32: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 358 | return arm_compute::PixelValue(value); |
Mike Kelly | 130ec60 | 2019-11-08 12:08:35 +0000 | [diff] [blame] | 359 | case arm_compute::DataType::QASYMM8: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 360 | return arm_compute::PixelValue(static_cast<uint8_t>(value)); |
Mike Kelly | 130ec60 | 2019-11-08 12:08:35 +0000 | [diff] [blame] | 361 | case arm_compute::DataType::QSYMM16: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 362 | return arm_compute::PixelValue(static_cast<int16_t>(value)); |
Tamas Nyiri | d3065d7 | 2021-11-12 11:22:50 +0000 | [diff] [blame] | 363 | case arm_compute::DataType::QSYMM8: |
Sadik Armagan | e5d0b93 | 2020-04-09 15:48:44 +0100 | [diff] [blame] | 364 | case arm_compute::DataType::QASYMM8_SIGNED: |
Mike Kelly | 130ec60 | 2019-11-08 12:08:35 +0000 | [diff] [blame] | 365 | case arm_compute::DataType::QSYMM8_PER_CHANNEL: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 366 | return arm_compute::PixelValue(static_cast<int8_t>(value)); |
Sadik Armagan | a792a05 | 2020-06-23 16:22:23 +0100 | [diff] [blame] | 367 | case arm_compute::DataType::S32: |
Kevin May | 263d709 | 2022-11-29 14:34:48 +0000 | [diff] [blame] | 368 | return arm_compute::PixelValue(static_cast<int32_t>(value)); |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 369 | default: |
| 370 | throw InvalidArgumentException("Unsupported DataType: [" + |
Matthew Sloyan | 2e5d0b2 | 2021-10-21 14:05:31 +0100 | [diff] [blame] | 371 | std::to_string(static_cast<int>(tensorInfo->data_type())) + "]"); |
Mike Kelly | 0a08ec6 | 2019-07-25 08:39:31 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 375 | unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout, |
| 376 | const arm_compute::TensorShape& weightsShape, |
| 377 | const arm_compute::TensorShape& inputShape) |
| 378 | { |
| 379 | unsigned int depthMultiplier; |
| 380 | if (layout == armnn::DataLayout::NHWC) |
| 381 | { |
| 382 | depthMultiplier = static_cast<uint32_t>(weightsShape[0]) / static_cast<uint32_t>(inputShape[0]); |
| 383 | } |
| 384 | else if (layout == armnn::DataLayout::NCHW) |
| 385 | { |
| 386 | depthMultiplier = static_cast<uint32_t>(weightsShape[2]) / static_cast<uint32_t>(inputShape[2]); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | throw InvalidArgumentException(fmt::format("Unknown data layout for tensor conversion: {}", |
| 391 | GetDataLayoutName(layout))); |
| 392 | } |
| 393 | return depthMultiplier; |
| 394 | } |
| 395 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 396 | } // namespace armcomputetensorutils |
| 397 | } // namespace armnn |