2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <aclCommon/ArmComputeTensorUtils.hpp>
6 #include <aclCommon/ArmComputeUtils.hpp>
8 #include "armnn/Exceptions.hpp"
9 #include <armnn/Descriptors.hpp>
13 namespace armcomputetensorutils
16 arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType)
20 case armnn::DataType::Float16:
21 return arm_compute::DataType::F16;
22 case armnn::DataType::Float32:
23 return arm_compute::DataType::F32;
24 case armnn::DataType::QuantisedAsymm8:
25 return arm_compute::DataType::QASYMM8;
26 case armnn::DataType::Signed32:
27 return arm_compute::DataType::S32;
29 BOOST_ASSERT_MSG(false, "Unknown data type");
30 return arm_compute::DataType::UNKNOWN;
34 arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
36 arm_compute::TensorShape shape;
38 // armnn tensors are (batch, channels, height, width).
39 // arm_compute tensors are (width, height, channels, batch).
40 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
42 // Note that our dimensions are stored in the opposite order to ACL's.
43 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i]);
45 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
46 // arm_compute tensors expect this.
49 // prevent arm_compute issue where tensor is flattened to nothing
50 if (shape.num_dimensions() == 0)
52 shape.set_num_dimensions(1);
58 // Utility function used to build a TensorInfo object, that can be used to initialise
59 // ARM Compute Tensor and CLTensor allocators.
60 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
62 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
63 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
64 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
65 tensorInfo.GetQuantizationOffset());
67 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
70 arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
74 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
76 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
78 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
79 std::to_string(static_cast<int>(dataLayout)) + "]");
83 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
84 armnn::DataLayout dataLayout)
86 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
87 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
88 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
89 tensorInfo.GetQuantizationOffset());
91 arm_compute::TensorInfo clTensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
92 clTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
97 arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor)
99 using arm_compute::PoolingType;
100 using arm_compute::DimensionRoundingType;
101 using arm_compute::PadStrideInfo;
102 using arm_compute::PoolingLayerInfo;
103 using arm_compute::Size2D;
105 // Resolve ARM Compute layer parameters.
106 const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType);
108 bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0);
109 //use specific constructor if global pooling
112 return arm_compute::PoolingLayerInfo(poolingType);
115 const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType(
116 descriptor.m_OutputShapeRounding);
117 const PadStrideInfo padStrideInfo(descriptor.m_StrideX,
118 descriptor.m_StrideY,
119 descriptor.m_PadLeft,
120 descriptor.m_PadRight,
122 descriptor.m_PadBottom,
125 const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude);
127 const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight);
129 return arm_compute::PoolingLayerInfo(poolingType, poolSize, padStrideInfo, excludePadding);
132 arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor)
134 const arm_compute::NormType normType =
135 ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType);
136 return arm_compute::NormalizationLayerInfo(normType,
137 descriptor.m_NormSize,
144 arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm)
146 arm_compute::PermutationVector aclPerm;
148 unsigned int start = 0;
149 while ((start < perm.GetSize()) && (start == perm[start]))
154 for (unsigned int i = start; i < perm.GetSize(); ++i)
156 aclPerm.set(i - start, perm[i] - start);
162 } // namespace armcomputetensorutils