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