blob: f9335058c27358297aa569f3f2bb34645ea88558 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// 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 {
Narumol Prangnawarat250d3922020-03-30 16:11:04 +010020 case armnn::DataType::BFloat16:
21 return arm_compute::DataType::BFLOAT16;
Mike Kelly130ec602019-11-08 12:08:35 +000022 case armnn::DataType::Boolean:
23 return arm_compute::DataType::U8;
telsoa01c577f2c2018-08-31 09:22:23 +010024 case armnn::DataType::Float16:
25 return arm_compute::DataType::F16;
telsoa014fcda012018-03-09 14:13:49 +000026 case armnn::DataType::Float32:
telsoa014fcda012018-03-09 14:13:49 +000027 return arm_compute::DataType::F32;
Ryan OShea9add1202020-02-07 10:06:33 +000028 case armnn::DataType::QAsymmS8:
29 return arm_compute::DataType::QASYMM8_SIGNED;
Derek Lambertif90c56d2020-01-10 17:14:08 +000030 case armnn::DataType::QAsymmU8:
telsoa014fcda012018-03-09 14:13:49 +000031 return arm_compute::DataType::QASYMM8;
Derek Lambertif90c56d2020-01-10 17:14:08 +000032 case armnn::DataType::QSymmS16:
Aron Virginas-Tar7a3e2fe2019-06-27 18:54:47 +010033 return arm_compute::DataType::QSYMM16;
Finn Williamsfd271062019-12-04 14:27:27 +000034 case armnn::DataType::QSymmS8:
Derek Lambertid466a542020-01-22 15:37:29 +000035 {
36 return multiScales ? arm_compute::DataType::QSYMM8_PER_CHANNEL : arm_compute::DataType::QSYMM8;
37 }
38 ARMNN_NO_DEPRECATE_WARN_BEGIN
Mike Kelly130ec602019-11-08 12:08:35 +000039 case armnn::DataType::QuantizedSymm8PerAxis:
40 return arm_compute::DataType::QSYMM8_PER_CHANNEL;
Derek Lambertid466a542020-01-22 15:37:29 +000041 ARMNN_NO_DEPRECATE_WARN_END
telsoa014fcda012018-03-09 14:13:49 +000042 case armnn::DataType::Signed32:
telsoa014fcda012018-03-09 14:13:49 +000043 return arm_compute::DataType::S32;
telsoa014fcda012018-03-09 14:13:49 +000044 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010045 ARMNN_ASSERT_MSG(false, "Unknown data type");
telsoa014fcda012018-03-09 14:13:49 +000046 return arm_compute::DataType::UNKNOWN;
telsoa014fcda012018-03-09 14:13:49 +000047 }
48}
49
Matthew Benthamfd899962018-12-31 15:49:42 +000050arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
51 unsigned int originalInputRank,
52 const std::vector<unsigned int>& armnnAxes)
53{
54 arm_compute::Coordinates outAclCoords;
55
56 if (armnnAxes.empty())
57 {
58 // If no reduction axes were provided, then the input must be reduced along all dimensions.
59 // Since Compute Library does not accept an empty vector as the reduction dimensions, we then
60 // manually create a vector including all the input dimensions (in reversed order) as:
61 //
62 // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
63 //
64 outAclCoords.set_num_dimensions(inputDimensions);
65 std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
66 }
67 else
68 {
69 // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
70 //
71 // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
72 // dimension correction).
73 // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
74 // new value for that reduction axis should be 1.
75 //
76 // Example:
77 // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
78 // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 }
79 // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 }
80 //
81 // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
82 //
83 outAclCoords.set_num_dimensions(armnnAxes.size());
84 std::transform(armnnAxes.begin(), armnnAxes.end(),
85 outAclCoords.begin(),
86 [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
87 }
88
89 return outAclCoords;
90}
91
telsoa014fcda012018-03-09 14:13:49 +000092arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
93{
94 arm_compute::TensorShape shape;
95
telsoa01c577f2c2018-08-31 09:22:23 +010096 // armnn tensors are (batch, channels, height, width).
97 // arm_compute tensors are (width, height, channels, batch).
telsoa014fcda012018-03-09 14:13:49 +000098 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
99 {
telsoa01c577f2c2018-08-31 09:22:23 +0100100 // Note that our dimensions are stored in the opposite order to ACL's.
Matthew Bentham89105282018-11-20 14:33:33 +0000101 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false);
telsoa014fcda012018-03-09 14:13:49 +0000102
103 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
telsoa01c577f2c2018-08-31 09:22:23 +0100104 // arm_compute tensors expect this.
telsoa014fcda012018-03-09 14:13:49 +0000105 }
106
107 // prevent arm_compute issue where tensor is flattened to nothing
108 if (shape.num_dimensions() == 0)
109 {
110 shape.set_num_dimensions(1);
111 }
112
113 return shape;
114}
115
116// Utility function used to build a TensorInfo object, that can be used to initialise
117// ARM Compute Tensor and CLTensor allocators.
118arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
119{
Derek Lambertid466a542020-01-22 15:37:29 +0000120 bool multiScales = tensorInfo.HasMultipleQuantizationScales();
telsoa014fcda012018-03-09 14:13:49 +0000121 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
Derek Lambertid466a542020-01-22 15:37:29 +0000122 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType(), multiScales);
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000123
Derek Lambertid466a542020-01-22 15:37:29 +0000124 const arm_compute::QuantizationInfo aclQuantizationInfo = multiScales ?
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000125 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScales()) :
126 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScale(), tensorInfo.GetQuantizationOffset());
telsoa014fcda012018-03-09 14:13:49 +0000127
128 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
129}
130
Francis Murtagh351d13d2018-09-24 15:01:18 +0100131arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
132 armnn::DataLayout dataLayout)
133{
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000134 arm_compute::TensorInfo aclTensorInfo = BuildArmComputeTensorInfo(tensorInfo);
135 aclTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
Francis Murtagh351d13d2018-09-24 15:01:18 +0100136
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000137 return aclTensorInfo;
Francis Murtagh351d13d2018-09-24 15:01:18 +0100138}
139
Matteo Martincigh747ef822018-12-18 09:26:39 +0000140arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
141{
142 switch(dataLayout)
143 {
144 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
145
146 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
147
148 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
149 std::to_string(static_cast<int>(dataLayout)) + "]");
150 }
151}
152
Sadik Armagana3600ba2019-10-10 10:43:20 +0100153arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
154 bool fpMixedPrecision)
telsoa014fcda012018-03-09 14:13:49 +0000155{
156 using arm_compute::PoolingType;
157 using arm_compute::DimensionRoundingType;
158 using arm_compute::PadStrideInfo;
159 using arm_compute::PoolingLayerInfo;
surmeh01bceff2f2018-03-29 16:29:27 +0100160 using arm_compute::Size2D;
Teresa Charlinc809a292020-01-31 10:21:44 +0000161 using arm_compute::DataLayout;
telsoa014fcda012018-03-09 14:13:49 +0000162
telsoa01c577f2c2018-08-31 09:22:23 +0100163 // Resolve ARM Compute layer parameters.
telsoa014fcda012018-03-09 14:13:49 +0000164 const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType);
telsoa01c577f2c2018-08-31 09:22:23 +0100165
Teresa Charlinc809a292020-01-31 10:21:44 +0000166 const DataLayout dataLayout = ConvertDataLayout(descriptor.m_DataLayout);
167
telsoa01c577f2c2018-08-31 09:22:23 +0100168 bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0);
169 //use specific constructor if global pooling
170 if(isGlobalPooling)
171 {
Teresa Charlinc809a292020-01-31 10:21:44 +0000172 return arm_compute::PoolingLayerInfo(poolingType, dataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +0100173 }
174
telsoa014fcda012018-03-09 14:13:49 +0000175 const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType(
176 descriptor.m_OutputShapeRounding);
telsoa014fcda012018-03-09 14:13:49 +0000177 const PadStrideInfo padStrideInfo(descriptor.m_StrideX,
178 descriptor.m_StrideY,
179 descriptor.m_PadLeft,
180 descriptor.m_PadRight,
181 descriptor.m_PadTop,
182 descriptor.m_PadBottom,
183 rounding);
184
185 const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude);
186
surmeh01bceff2f2018-03-29 16:29:27 +0100187 const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight);
188
Teresa Charlinc809a292020-01-31 10:21:44 +0000189 return arm_compute::PoolingLayerInfo(poolingType, poolSize, dataLayout, padStrideInfo, excludePadding,
190 fpMixedPrecision);
telsoa014fcda012018-03-09 14:13:49 +0000191}
192
193arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor)
194{
195 const arm_compute::NormType normType =
196 ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType);
197 return arm_compute::NormalizationLayerInfo(normType,
198 descriptor.m_NormSize,
199 descriptor.m_Alpha,
200 descriptor.m_Beta,
201 descriptor.m_K,
202 false);
203}
204
205arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm)
206{
207 arm_compute::PermutationVector aclPerm;
208
209 unsigned int start = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100210 while ((start < perm.GetSize()) && (start == perm[start]))
telsoa014fcda012018-03-09 14:13:49 +0000211 {
212 ++start;
213 }
214
215 for (unsigned int i = start; i < perm.GetSize(); ++i)
216 {
217 aclPerm.set(i - start, perm[i] - start);
218 }
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000219 return aclPerm;
220}
telsoa014fcda012018-03-09 14:13:49 +0000221
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000222arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm)
223{
224 arm_compute::PermutationVector aclPerm;
225 std::map<unsigned int, unsigned int> permuteMappings;
226 for (unsigned int i = 0; i < perm.GetSize(); ++i)
227 {
228 permuteMappings[perm[i]] = i;
229 }
230
231 std::vector<unsigned int> permuteVector;
232 for (unsigned int i = 0; i < perm.GetSize(); ++i)
233 {
234 permuteVector.push_back(permuteMappings.at(i));
235 }
236
237 unsigned int start = 0;
238 while ((start < perm.GetSize()) && (start == permuteVector[start]))
239 {
240 ++start;
241 }
242
243 for (unsigned int i = start; i < perm.GetSize(); ++i)
244 {
245 aclPerm.set(i - start, permuteVector[i] - start);
246 }
telsoa014fcda012018-03-09 14:13:49 +0000247 return aclPerm;
248}
249
Sadik Armaganf4464322018-12-20 16:19:12 +0000250arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height)
251{
252 return arm_compute::Size2D(width, height);
253}
254
Mike Kelly0a08ec62019-07-25 08:39:31 +0100255arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue)
256{
257 switch (input.info()->data_type())
258 {
Mike Kelly0a08ec62019-07-25 08:39:31 +0100259 case arm_compute::DataType::F16:
260 return arm_compute::PixelValue(static_cast<Half>(pixelValue));
261 case arm_compute::DataType::F32:
262 return arm_compute::PixelValue(pixelValue);
Mike Kelly130ec602019-11-08 12:08:35 +0000263 case arm_compute::DataType::QASYMM8:
264 return arm_compute::PixelValue(static_cast<uint8_t>(pixelValue));
265 case arm_compute::DataType::QSYMM16:
266 return arm_compute::PixelValue(static_cast<int16_t>(pixelValue));
Sadik Armagane5d0b932020-04-09 15:48:44 +0100267 case arm_compute::DataType::QASYMM8_SIGNED:
Mike Kelly130ec602019-11-08 12:08:35 +0000268 case arm_compute::DataType::QSYMM8_PER_CHANNEL:
269 return arm_compute::PixelValue(static_cast<int8_t>(pixelValue));
Sadik Armagana792a052020-06-23 16:22:23 +0100270 case arm_compute::DataType::S32:
271 return arm_compute::PixelValue(static_cast<int32_t>(pixelValue));
Mike Kelly0a08ec62019-07-25 08:39:31 +0100272 default:
273 throw InvalidArgumentException("Unsupported DataType: [" +
274 std::to_string(static_cast<int>(input.info()->data_type())) + "]");
275 }
276}
277
telsoa014fcda012018-03-09 14:13:49 +0000278} // namespace armcomputetensorutils
279} // namespace armnn