blob: b2955b9259e12cfca70bb02f24c8027981bac31b [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
16arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType)
17{
18 switch(dataType)
19 {
telsoa01c577f2c2018-08-31 09:22:23 +010020 case armnn::DataType::Float16:
21 return arm_compute::DataType::F16;
telsoa014fcda012018-03-09 14:13:49 +000022 case armnn::DataType::Float32:
telsoa014fcda012018-03-09 14:13:49 +000023 return arm_compute::DataType::F32;
telsoa014fcda012018-03-09 14:13:49 +000024 case armnn::DataType::QuantisedAsymm8:
telsoa014fcda012018-03-09 14:13:49 +000025 return arm_compute::DataType::QASYMM8;
Aron Virginas-Tar7a3e2fe2019-06-27 18:54:47 +010026 case armnn::DataType::QuantisedSymm16:
27 return arm_compute::DataType::QSYMM16;
telsoa014fcda012018-03-09 14:13:49 +000028 case armnn::DataType::Signed32:
telsoa014fcda012018-03-09 14:13:49 +000029 return arm_compute::DataType::S32;
Nattapat Chaimanowong8c76cc12019-01-23 09:59:14 +000030 case armnn::DataType::Boolean:
31 return arm_compute::DataType::U8;
telsoa014fcda012018-03-09 14:13:49 +000032 default:
telsoa014fcda012018-03-09 14:13:49 +000033 BOOST_ASSERT_MSG(false, "Unknown data type");
34 return arm_compute::DataType::UNKNOWN;
telsoa014fcda012018-03-09 14:13:49 +000035 }
36}
37
Matthew Benthamfd899962018-12-31 15:49:42 +000038arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
39 unsigned int originalInputRank,
40 const std::vector<unsigned int>& armnnAxes)
41{
42 arm_compute::Coordinates outAclCoords;
43
44 if (armnnAxes.empty())
45 {
46 // If no reduction axes were provided, then the input must be reduced along all dimensions.
47 // Since Compute Library does not accept an empty vector as the reduction dimensions, we then
48 // manually create a vector including all the input dimensions (in reversed order) as:
49 //
50 // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
51 //
52 outAclCoords.set_num_dimensions(inputDimensions);
53 std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
54 }
55 else
56 {
57 // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
58 //
59 // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
60 // dimension correction).
61 // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
62 // new value for that reduction axis should be 1.
63 //
64 // Example:
65 // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
66 // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 }
67 // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 }
68 //
69 // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
70 //
71 outAclCoords.set_num_dimensions(armnnAxes.size());
72 std::transform(armnnAxes.begin(), armnnAxes.end(),
73 outAclCoords.begin(),
74 [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
75 }
76
77 return outAclCoords;
78}
79
telsoa014fcda012018-03-09 14:13:49 +000080arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
81{
82 arm_compute::TensorShape shape;
83
telsoa01c577f2c2018-08-31 09:22:23 +010084 // armnn tensors are (batch, channels, height, width).
85 // arm_compute tensors are (width, height, channels, batch).
telsoa014fcda012018-03-09 14:13:49 +000086 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
87 {
telsoa01c577f2c2018-08-31 09:22:23 +010088 // Note that our dimensions are stored in the opposite order to ACL's.
Matthew Bentham89105282018-11-20 14:33:33 +000089 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false);
telsoa014fcda012018-03-09 14:13:49 +000090
91 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
telsoa01c577f2c2018-08-31 09:22:23 +010092 // arm_compute tensors expect this.
telsoa014fcda012018-03-09 14:13:49 +000093 }
94
95 // prevent arm_compute issue where tensor is flattened to nothing
96 if (shape.num_dimensions() == 0)
97 {
98 shape.set_num_dimensions(1);
99 }
100
101 return shape;
102}
103
104// Utility function used to build a TensorInfo object, that can be used to initialise
105// ARM Compute Tensor and CLTensor allocators.
106arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
107{
108 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000109 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
110
111 const arm_compute::QuantizationInfo aclQuantizationInfo = tensorInfo.HasMultipleQuantizationScales() ?
112 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScales()) :
113 arm_compute::QuantizationInfo(tensorInfo.GetQuantizationScale(), tensorInfo.GetQuantizationOffset());
telsoa014fcda012018-03-09 14:13:49 +0000114
115 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
116}
117
Francis Murtagh351d13d2018-09-24 15:01:18 +0100118arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
119 armnn::DataLayout dataLayout)
120{
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000121 arm_compute::TensorInfo aclTensorInfo = BuildArmComputeTensorInfo(tensorInfo);
122 aclTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
Francis Murtagh351d13d2018-09-24 15:01:18 +0100123
Aron Virginas-Tar13b653f2019-11-01 11:40:39 +0000124 return aclTensorInfo;
Francis Murtagh351d13d2018-09-24 15:01:18 +0100125}
126
Matteo Martincigh747ef822018-12-18 09:26:39 +0000127arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
128{
129 switch(dataLayout)
130 {
131 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
132
133 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
134
135 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
136 std::to_string(static_cast<int>(dataLayout)) + "]");
137 }
138}
139
Sadik Armagana3600ba2019-10-10 10:43:20 +0100140arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
141 bool fpMixedPrecision)
telsoa014fcda012018-03-09 14:13:49 +0000142{
143 using arm_compute::PoolingType;
144 using arm_compute::DimensionRoundingType;
145 using arm_compute::PadStrideInfo;
146 using arm_compute::PoolingLayerInfo;
surmeh01bceff2f2018-03-29 16:29:27 +0100147 using arm_compute::Size2D;
telsoa014fcda012018-03-09 14:13:49 +0000148
telsoa01c577f2c2018-08-31 09:22:23 +0100149 // Resolve ARM Compute layer parameters.
telsoa014fcda012018-03-09 14:13:49 +0000150 const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType);
telsoa01c577f2c2018-08-31 09:22:23 +0100151
152 bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0);
153 //use specific constructor if global pooling
154 if(isGlobalPooling)
155 {
156 return arm_compute::PoolingLayerInfo(poolingType);
157 }
158
telsoa014fcda012018-03-09 14:13:49 +0000159 const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType(
160 descriptor.m_OutputShapeRounding);
telsoa014fcda012018-03-09 14:13:49 +0000161 const PadStrideInfo padStrideInfo(descriptor.m_StrideX,
162 descriptor.m_StrideY,
163 descriptor.m_PadLeft,
164 descriptor.m_PadRight,
165 descriptor.m_PadTop,
166 descriptor.m_PadBottom,
167 rounding);
168
169 const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude);
170
surmeh01bceff2f2018-03-29 16:29:27 +0100171 const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight);
172
Sadik Armagana3600ba2019-10-10 10:43:20 +0100173 return arm_compute::PoolingLayerInfo(poolingType, poolSize, padStrideInfo, excludePadding, fpMixedPrecision);
telsoa014fcda012018-03-09 14:13:49 +0000174}
175
176arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor)
177{
178 const arm_compute::NormType normType =
179 ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType);
180 return arm_compute::NormalizationLayerInfo(normType,
181 descriptor.m_NormSize,
182 descriptor.m_Alpha,
183 descriptor.m_Beta,
184 descriptor.m_K,
185 false);
186}
187
188arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm)
189{
190 arm_compute::PermutationVector aclPerm;
191
192 unsigned int start = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100193 while ((start < perm.GetSize()) && (start == perm[start]))
telsoa014fcda012018-03-09 14:13:49 +0000194 {
195 ++start;
196 }
197
198 for (unsigned int i = start; i < perm.GetSize(); ++i)
199 {
200 aclPerm.set(i - start, perm[i] - start);
201 }
202
203 return aclPerm;
204}
205
Sadik Armaganf4464322018-12-20 16:19:12 +0000206arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height)
207{
208 return arm_compute::Size2D(width, height);
209}
210
Mike Kelly0a08ec62019-07-25 08:39:31 +0100211arm_compute::PixelValue GetPixelValue(arm_compute::ITensor& input, float pixelValue)
212{
213 switch (input.info()->data_type())
214 {
215 case arm_compute::DataType::QASYMM8:
216 return arm_compute::PixelValue(static_cast<uint8_t>(pixelValue));
217 case arm_compute::DataType::QSYMM16:
218 return arm_compute::PixelValue(static_cast<int16_t>(pixelValue));
219 case arm_compute::DataType::F16:
220 return arm_compute::PixelValue(static_cast<Half>(pixelValue));
221 case arm_compute::DataType::F32:
222 return arm_compute::PixelValue(pixelValue);
223 default:
224 throw InvalidArgumentException("Unsupported DataType: [" +
225 std::to_string(static_cast<int>(input.info()->data_type())) + "]");
226 }
227}
228
telsoa014fcda012018-03-09 14:13:49 +0000229} // namespace armcomputetensorutils
230} // namespace armnn