blob: 4f69c0b7db88ee1a043e381af92df5aa61591b3c [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;
Nattapat Chaimanowong8c76cc12019-01-23 09:59:14 +000028 case armnn::DataType::Boolean:
29 return arm_compute::DataType::U8;
telsoa014fcda012018-03-09 14:13:49 +000030 default:
telsoa014fcda012018-03-09 14:13:49 +000031 BOOST_ASSERT_MSG(false, "Unknown data type");
32 return arm_compute::DataType::UNKNOWN;
telsoa014fcda012018-03-09 14:13:49 +000033 }
34}
35
Matthew Benthamfd899962018-12-31 15:49:42 +000036arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
37 unsigned int originalInputRank,
38 const std::vector<unsigned int>& armnnAxes)
39{
40 arm_compute::Coordinates outAclCoords;
41
42 if (armnnAxes.empty())
43 {
44 // If no reduction axes were provided, then the input must be reduced along all dimensions.
45 // Since Compute Library does not accept an empty vector as the reduction dimensions, we then
46 // manually create a vector including all the input dimensions (in reversed order) as:
47 //
48 // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
49 //
50 outAclCoords.set_num_dimensions(inputDimensions);
51 std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
52 }
53 else
54 {
55 // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
56 //
57 // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
58 // dimension correction).
59 // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
60 // new value for that reduction axis should be 1.
61 //
62 // Example:
63 // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
64 // ArmNN reduction axis = { 2 } -> ACL reduction axis = { 1 }
65 // ArmNN reduction axis = { 3 } -> ACL reduction axis = { 0 }
66 //
67 // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
68 //
69 outAclCoords.set_num_dimensions(armnnAxes.size());
70 std::transform(armnnAxes.begin(), armnnAxes.end(),
71 outAclCoords.begin(),
72 [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
73 }
74
75 return outAclCoords;
76}
77
telsoa014fcda012018-03-09 14:13:49 +000078arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape)
79{
80 arm_compute::TensorShape shape;
81
telsoa01c577f2c2018-08-31 09:22:23 +010082 // armnn tensors are (batch, channels, height, width).
83 // arm_compute tensors are (width, height, channels, batch).
telsoa014fcda012018-03-09 14:13:49 +000084 for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); i++)
85 {
telsoa01c577f2c2018-08-31 09:22:23 +010086 // Note that our dimensions are stored in the opposite order to ACL's.
Matthew Bentham89105282018-11-20 14:33:33 +000087 shape.set(tensorShape.GetNumDimensions() - i - 1, tensorShape[i], false);
telsoa014fcda012018-03-09 14:13:49 +000088
89 // TensorShape::set() flattens leading ones, so that batch size 1 cannot happen.
telsoa01c577f2c2018-08-31 09:22:23 +010090 // arm_compute tensors expect this.
telsoa014fcda012018-03-09 14:13:49 +000091 }
92
93 // prevent arm_compute issue where tensor is flattened to nothing
94 if (shape.num_dimensions() == 0)
95 {
96 shape.set_num_dimensions(1);
97 }
98
99 return shape;
100}
101
102// Utility function used to build a TensorInfo object, that can be used to initialise
103// ARM Compute Tensor and CLTensor allocators.
104arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo)
105{
106 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
107 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
108 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
109 tensorInfo.GetQuantizationOffset());
110
111 return arm_compute::TensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
112}
113
Francis Murtagh351d13d2018-09-24 15:01:18 +0100114arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
115 armnn::DataLayout dataLayout)
116{
117 const arm_compute::TensorShape aclTensorShape = BuildArmComputeTensorShape(tensorInfo.GetShape());
118 const arm_compute::DataType aclDataType = GetArmComputeDataType(tensorInfo.GetDataType());
119 const arm_compute::QuantizationInfo aclQuantizationInfo(tensorInfo.GetQuantizationScale(),
120 tensorInfo.GetQuantizationOffset());
121
122 arm_compute::TensorInfo clTensorInfo(aclTensorShape, 1, aclDataType, aclQuantizationInfo);
123 clTensorInfo.set_data_layout(ConvertDataLayout(dataLayout));
124
125 return clTensorInfo;
126}
127
Matteo Martincigh747ef822018-12-18 09:26:39 +0000128arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout)
129{
130 switch(dataLayout)
131 {
132 case armnn::DataLayout::NHWC : return arm_compute::DataLayout::NHWC;
133
134 case armnn::DataLayout::NCHW : return arm_compute::DataLayout::NCHW;
135
136 default: throw InvalidArgumentException("Unknown armnn::DataLayout: [" +
137 std::to_string(static_cast<int>(dataLayout)) + "]");
138 }
139}
140
telsoa014fcda012018-03-09 14:13:49 +0000141arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor)
142{
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
173 return arm_compute::PoolingLayerInfo(poolingType, poolSize, padStrideInfo, excludePadding);
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
telsoa014fcda012018-03-09 14:13:49 +0000211} // namespace armcomputetensorutils
212} // namespace armnn