blob: 32af42f7e14b8f8eb408208cbbbc5b5f0e671c66 [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;
telsoa014fcda012018-03-09 14:13:49 +000026 case armnn::DataType::Signed32:
telsoa014fcda012018-03-09 14:13:49 +000027 return arm_compute::DataType::S32;
telsoa014fcda012018-03-09 14:13:49 +000028 default:
telsoa014fcda012018-03-09 14:13:49 +000029 BOOST_ASSERT_MSG(false, "Unknown data type");
30 return arm_compute::DataType::UNKNOWN;
telsoa014fcda012018-03-09 14:13:49 +000031 }
32}
33
Matthew Benthamfd899962018-12-31 15:49:42 +000034arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
35 unsigned int originalInputRank,
36 const std::vector<unsigned int>& armnnAxes)
37{
38 arm_compute::Coordinates outAclCoords;
39
40 if (armnnAxes.empty())
41 {
42 // If no reduction axes were provided, then the input must be reduced along all dimensions.
43 // Since Compute Library does not accept an empty vector as the reduction dimensions, we then
44 // manually create a vector including all the input dimensions (in reversed order) as:
45 //
46 // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
47 //
48 outAclCoords.set_num_dimensions(inputDimensions);
49 std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
50 }
51 else
52 {
53 // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
54 //
55 // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
56 // dimension correction).
57 // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
58 // new value for that reduction axis should be 1.
59 //
60 // Example:
61 // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
62 // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 }
63 // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 }
64 //
65 // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
66 //
67 outAclCoords.set_num_dimensions(armnnAxes.size());
68 std::transform(armnnAxes.begin(), armnnAxes.end(),
69 outAclCoords.begin(),
70 [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
71 }
72
73 return outAclCoords;
74}
75
telsoa014fcda012018-03-09 14:13:49 +000076arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
77{
78 arm_compute::TensorShape shape;
79
telsoa01c577f2c2018-08-31 09:22:23 +010080 // armnn tensors are (batch, channels, height, width).
81 // arm_compute tensors are (width, height, channels, batch).
telsoa014fcda012018-03-09 14:13:49 +000082 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
83 {
telsoa01c577f2c2018-08-31 09:22:23 +010084 // Note that our dimensions are stored in the opposite order to ACL's.
Matthew Bentham89105282018-11-20 14:33:33 +000085 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false);
telsoa014fcda012018-03-09 14:13:49 +000086
87 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
telsoa01c577f2c2018-08-31 09:22:23 +010088 // arm_compute tensors expect this.
telsoa014fcda012018-03-09 14:13:49 +000089 }
90
91 // prevent arm_compute issue where tensor is flattened to nothing
92 if (shape.num_dimensions() == 0)
93 {
94 shape.set_num_dimensions(1);
95 }
96
97 return shape;
98}
99
100// Utility function used to build a TensorInfo object, that can be used to initialise
101// ARM Compute Tensor and CLTensor allocators.
102arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
103{
104 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
105 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
106 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
107 tensorInfo.GetQuantizationOffset());
108
109 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
110}
111
Francis Murtagh351d13d2018-09-24 15:01:18 +0100112arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
113 armnn::DataLayout dataLayout)
114{
115 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
116 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
117 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
118 tensorInfo.GetQuantizationOffset());
119
120 arm_compute::TensorInfo clTensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
121 clTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
122
123 return clTensorInfo;
124}
125
Matteo Martincigh747ef822018-12-18 09:26:39 +0000126arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
127{
128 switch(dataLayout)
129 {
130 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
131
132 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
133
134 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
135 std::to_string(static_cast<int>(dataLayout)) + "]");
136 }
137}
138
telsoa014fcda012018-03-09 14:13:49 +0000139arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor)
140{
141 using arm_compute::PoolingType;
142 using arm_compute::DimensionRoundingType;
143 using arm_compute::PadStrideInfo;
144 using arm_compute::PoolingLayerInfo;
surmeh01bceff2f2018-03-29 16:29:27 +0100145 using arm_compute::Size2D;
telsoa014fcda012018-03-09 14:13:49 +0000146
telsoa01c577f2c2018-08-31 09:22:23 +0100147 // Resolve ARM Compute layer parameters.
telsoa014fcda012018-03-09 14:13:49 +0000148 const PoolingType poolingType = ConvertPoolingAlgorithmToAclPoolingType(descriptor.m_PoolType);
telsoa01c577f2c2018-08-31 09:22:23 +0100149
150 bool isGlobalPooling = (descriptor.m_StrideX==0 && descriptor.m_StrideY==0);
151 //use specific constructor if global pooling
152 if(isGlobalPooling)
153 {
154 return arm_compute::PoolingLayerInfo(poolingType);
155 }
156
telsoa014fcda012018-03-09 14:13:49 +0000157 const DimensionRoundingType rounding = ConvertOutputShapeRoundingToAclDimensionRoundingType(
158 descriptor.m_OutputShapeRounding);
telsoa014fcda012018-03-09 14:13:49 +0000159 const PadStrideInfo padStrideInfo(descriptor.m_StrideX,
160 descriptor.m_StrideY,
161 descriptor.m_PadLeft,
162 descriptor.m_PadRight,
163 descriptor.m_PadTop,
164 descriptor.m_PadBottom,
165 rounding);
166
167 const bool excludePadding = (descriptor.m_PaddingMethod == PaddingMethod::Exclude);
168
surmeh01bceff2f2018-03-29 16:29:27 +0100169 const Size2D poolSize(descriptor.m_PoolWidth, descriptor.m_PoolHeight);
170
171 return arm_compute::PoolingLayerInfo(poolingType, poolSize, padStrideInfo, excludePadding);
telsoa014fcda012018-03-09 14:13:49 +0000172}
173
174arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& descriptor)
175{
176 const arm_compute::NormType normType =
177 ConvertNormalizationAlgorithmChannelToAclNormType(descriptor.m_NormChannelType);
178 return arm_compute::NormalizationLayerInfo(normType,
179 descriptor.m_NormSize,
180 descriptor.m_Alpha,
181 descriptor.m_Beta,
182 descriptor.m_K,
183 false);
184}
185
186arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm)
187{
188 arm_compute::PermutationVector aclPerm;
189
190 unsigned int start = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100191 while ((start < perm.GetSize()) && (start == perm[start]))
telsoa014fcda012018-03-09 14:13:49 +0000192 {
193 ++start;
194 }
195
196 for (unsigned int i = start; i < perm.GetSize(); ++i)
197 {
198 aclPerm.set(i - start, perm[i] - start);
199 }
200
201 return aclPerm;
202}
203
Sadik Armaganf4464322018-12-20 16:19:12 +0000204arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height)
205{
206 return arm_compute::Size2D(width, height);
207}
208
telsoa014fcda012018-03-09 14:13:49 +0000209} // namespace armcomputetensorutils
210} // namespace armnn