blob: 84091e8fb3f4eb89257a09120d1ceebf58bb69b5 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00005#include <aclCommon/ArmComputeTensorUtils.hpp>
6#include <aclCommon/ArmComputeUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +00007
Francis Murtagh351d13d2018-09-24 15:01:18 +01008#include "armnn/Exceptions.hpp"
telsoa014fcda012018-03-09 14:13:49 +00009#include <armnn/Descriptors.hpp>
10
11namespace armnn
12{
13namespace armcomputetensorutils
14{
15
Derek Lambertid466a542020-01-22 15:37:29 +000016arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales)
telsoa014fcda012018-03-09 14:13:49 +000017{
18 switch(dataType)
19 {
Mike Kelly130ec602019-11-08 12:08:35 +000020 case armnn::DataType::Boolean:
21 return arm_compute::DataType::U8;
telsoa01c577f2c2018-08-31 09:22:23 +010022 case armnn::DataType::Float16:
23 return arm_compute::DataType::F16;
telsoa014fcda012018-03-09 14:13:49 +000024 case armnn::DataType::Float32:
telsoa014fcda012018-03-09 14:13:49 +000025 return arm_compute::DataType::F32;
Ryan OShea9add1202020-02-07 10:06:33 +000026 case armnn::DataType::QAsymmS8:
27 return arm_compute::DataType::QASYMM8_SIGNED;
Derek Lambertif90c56d2020-01-10 17:14:08 +000028 case armnn::DataType::QAsymmU8:
telsoa014fcda012018-03-09 14:13:49 +000029 return arm_compute::DataType::QASYMM8;
Derek Lambertif90c56d2020-01-10 17:14:08 +000030 case armnn::DataType::QSymmS16:
Aron Virginas-Tar7a3e2fe2019-06-27 18:54:47 +010031 return arm_compute::DataType::QSYMM16;
Finn Williamsfd271062019-12-04 14:27:27 +000032 case armnn::DataType::QSymmS8:
Derek Lambertid466a542020-01-22 15:37:29 +000033 {
34 return multiScales ? arm_compute::DataType::QSYMM8_PER_CHANNEL : arm_compute::DataType::QSYMM8;
35 }
36 ARMNN_NO_DEPRECATE_WARN_BEGIN
Mike Kelly130ec602019-11-08 12:08:35 +000037 case armnn::DataType::QuantizedSymm8PerAxis:
38 return arm_compute::DataType::QSYMM8_PER_CHANNEL;
Derek Lambertid466a542020-01-22 15:37:29 +000039 ARMNN_NO_DEPRECATE_WARN_END
telsoa014fcda012018-03-09 14:13:49 +000040 case armnn::DataType::Signed32:
telsoa014fcda012018-03-09 14:13:49 +000041 return arm_compute::DataType::S32;
telsoa014fcda012018-03-09 14:13:49 +000042 default:
telsoa014fcda012018-03-09 14:13:49 +000043 BOOST_ASSERT_MSG(false, "Unknown data type");
44 return arm_compute::DataType::UNKNOWN;
telsoa014fcda012018-03-09 14:13:49 +000045 }
46}
47
Matthew Benthamfd899962018-12-31 15:49:42 +000048arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
49 unsigned int originalInputRank,
50 const std::vector<unsigned int>& armnnAxes)
51{
52 arm_compute::Coordinates outAclCoords;
53
54 if (armnnAxes.empty())
55 {
56 // If no reduction axes were provided, then the input must be reduced along all dimensions.
57 // Since Compute Library does not accept an empty vector as the reduction dimensions, we then
58 // manually create a vector including all the input dimensions (in reversed order) as:
59 //
60 // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
61 //
62 outAclCoords.set_num_dimensions(inputDimensions);
63 std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
64 }
65 else
66 {
67 // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
68 //
69 // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
70 // dimension correction).
71 // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
72 // new value for that reduction axis should be 1.
73 //
74 // Example:
75 // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
76 // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 }
77 // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 }
78 //
79 // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
80 //
81 outAclCoords.set_num_dimensions(armnnAxes.size());
82 std::transform(armnnAxes.begin(), armnnAxes.end(),
83 outAclCoords.begin(),
84 [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
85 }
86
87 return outAclCoords;
88}
89
telsoa014fcda012018-03-09 14:13:49 +000090arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
91{
92 arm_compute::TensorShape shape;
93
telsoa01c577f2c2018-08-31 09:22:23 +010094 // armnn tensors are (batch, channels, height, width).
95 // arm_compute tensors are (width, height, channels, batch).
telsoa014fcda012018-03-09 14:13:49 +000096 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
97 {
telsoa01c577f2c2018-08-31 09:22:23 +010098 // Note that our dimensions are stored in the opposite order to ACL's.
Matthew Bentham89105282018-11-20 14:33:33 +000099 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false);
telsoa014fcda012018-03-09 14:13:49 +0000100
101 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
telsoa01c577f2c2018-08-31 09:22:23 +0100102 // arm_compute tensors expect this.
telsoa014fcda012018-03-09 14:13:49 +0000103 }
104
105 // prevent arm_compute issue where tensor is flattened to nothing
106 if (shape.num_dimensions() == 0)
107 {
108 shape.set_num_dimensions(1);
109 }
110
111 return shape;
112}
113
114// Utility function used to build a TensorInfo object, that can be used to initialise
115// ARM Compute Tensor and CLTensor allocators.
116arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
117{
Derek Lambertid466a542020-01-22 15:37:29 +0000118 bool multiScales = tensorInfo.HasMultipleQuantizationScales();
telsoa014fcda012018-03-09 14:13:49 +0000119 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
Derek Lambertid466a542020-01-22 15:37:29 +0000120 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType(), multiScales);
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000121
Derek Lambertid466a542020-01-22 15:37:29 +0000122 const arm_compute::QuantizationInfo aclQuantizationInfo = multiScales ?
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000123 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScales()) :
124 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScale(), tensorInfo.GetQuantizationOffset());
telsoa014fcda012018-03-09 14:13:49 +0000125
126 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
127}
128
Francis Murtagh351d13d2018-09-24 15:01:18 +0100129arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
130 armnn::DataLayout dataLayout)
131{
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000132 arm_compute::TensorInfo aclTensorInfo = BuildArmComputeTensorInfo(tensorInfo);
133 aclTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
Francis Murtagh351d13d2018-09-24 15:01:18 +0100134
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000135 return aclTensorInfo;
Francis Murtagh351d13d2018-09-24 15:01:18 +0100136}
137
Matteo Martincigh747ef822018-12-18 09:26:39 +0000138arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
139{
140 switch(dataLayout)
141 {
142 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
143
144 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
145
146 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
147 std::to_string(static_cast<int>(dataLayout)) + "]");
148 }
149}
150
Sadik Armagana3600ba2019-10-10 10:43:20 +0100151arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
152 bool fpMixedPrecision)
telsoa014fcda012018-03-09 14:13:49 +0000153{
154 using arm_compute::PoolingType;
155 using arm_compute::DimensionRoundingType;
156 using arm_compute::PadStrideInfo;
157 using arm_compute::PoolingLayerInfo;
surmeh01bceff2f2018-03-29 16:29:27 +0100158 using arm_compute::Size2D;
Teresa Charlinc809a292020-01-31 10:21:44 +0000159 using arm_compute::DataLayout;
telsoa014fcda012018-03-09 14:13:49 +0000160
telsoa01c577f2c2018-08-31 09:22:23 +0100161 // Resolve ARM Compute layer parameters.
telsoa014fcda012018-03-09 14:13:49 +0000162 const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType);
telsoa01c577f2c2018-08-31 09:22:23 +0100163
Teresa Charlinc809a292020-01-31 10:21:44 +0000164 const DataLayout dataLayout = ConvertDataLayout(descriptor.m_DataLayout);
165
telsoa01c577f2c2018-08-31 09:22:23 +0100166 bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0);
167 //use specific constructor if global pooling
168 if(isGlobalPooling)
169 {
Teresa Charlinc809a292020-01-31 10:21:44 +0000170 return arm_compute::PoolingLayerInfo(poolingType, dataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +0100171 }
172
telsoa014fcda012018-03-09 14:13:49 +0000173 const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType(
174 descriptor.m_OutputShapeRounding);
telsoa014fcda012018-03-09 14:13:49 +0000175 const PadStrideInfo padStrideInfo(descriptor.m_StrideX,
176 descriptor.m_StrideY,
177 descriptor.m_PadLeft,
178 descriptor.m_PadRight,
179 descriptor.m_PadTop,
180 descriptor.m_PadBottom,
181 rounding);
182
183 const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude);
184
surmeh01bceff2f2018-03-29 16:29:27 +0100185 const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight);
186
Teresa Charlinc809a292020-01-31 10:21:44 +0000187 return arm_compute::PoolingLayerInfo(poolingType, poolSize, dataLayout, padStrideInfo, excludePadding,
188 fpMixedPrecision);
telsoa014fcda012018-03-09 14:13:49 +0000189}
190
191arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor)
192{
193 const arm_compute::NormType normType =
194 ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType);
195 return arm_compute::NormalizationLayerInfo(normType,
196 descriptor.m_NormSize,
197 descriptor.m_Alpha,
198 descriptor.m_Beta,
199 descriptor.m_K,
200 false);
201}
202
203arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm)
204{
205 arm_compute::PermutationVector aclPerm;
206
207 unsigned int start = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100208 while ((start < perm.GetSize()) && (start == perm[start]))
telsoa014fcda012018-03-09 14:13:49 +0000209 {
210 ++start;
211 }
212
213 for (unsigned int i = start; i < perm.GetSize(); ++i)
214 {
215 aclPerm.set(i - start, perm[i] - start);
216 }
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000217 return aclPerm;
218}
telsoa014fcda012018-03-09 14:13:49 +0000219
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000220arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm)
221{
222 arm_compute::PermutationVector aclPerm;
223 std::map<unsigned int, unsigned int> permuteMappings;
224 for (unsigned int i = 0; i < perm.GetSize(); ++i)
225 {
226 permuteMappings[perm[i]] = i;
227 }
228
229 std::vector<unsigned int> permuteVector;
230 for (unsigned int i = 0; i < perm.GetSize(); ++i)
231 {
232 permuteVector.push_back(permuteMappings.at(i));
233 }
234
235 unsigned int start = 0;
236 while ((start < perm.GetSize()) && (start == permuteVector[start]))
237 {
238 ++start;
239 }
240
241 for (unsigned int i = start; i < perm.GetSize(); ++i)
242 {
243 aclPerm.set(i - start, permuteVector[i] - start);
244 }
telsoa014fcda012018-03-09 14:13:49 +0000245 return aclPerm;
246}
247
Sadik Armaganf4464322018-12-20 16:19:12 +0000248arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height)
249{
250 return arm_compute::Size2D(width, height);
251}
252
Mike Kelly0a08ec62019-07-25 08:39:31 +0100253arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue)
254{
255 switch (input.info()->data_type())
256 {
Mike Kelly0a08ec62019-07-25 08:39:31 +0100257 case arm_compute::DataType::F16:
258 return arm_compute::PixelValue(static_cast<Half>(pixelValue));
259 case arm_compute::DataType::F32:
260 return arm_compute::PixelValue(pixelValue);
Mike Kelly130ec602019-11-08 12:08:35 +0000261 case arm_compute::DataType::QASYMM8:
262 return arm_compute::PixelValue(static_cast<uint8_t>(pixelValue));
263 case arm_compute::DataType::QSYMM16:
264 return arm_compute::PixelValue(static_cast<int16_t>(pixelValue));
265 case arm_compute::DataType::QSYMM8_PER_CHANNEL:
266 return arm_compute::PixelValue(static_cast<int8_t>(pixelValue));
Mike Kelly0a08ec62019-07-25 08:39:31 +0100267 default:
268 throw InvalidArgumentException("Unsupported DataType: [" +
269 std::to_string(static_cast<int>(input.info()->data_type())) + "]");
270 }
271}
272
telsoa014fcda012018-03-09 14:13:49 +0000273} // namespace armcomputetensorutils
274} // namespace armnn