blob: 6ddecf2aaaf11432f149dbc45673197545977e03 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlin6bc85252022-12-06 20:43:06 +00002// Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
7#include <armnn/Tensor.hpp>
8#include <armnn/DescriptorsFwd.hpp>
9
Matthew Sloyan171214c2020-09-09 09:07:37 +010010#include <armnn/utility/NumericCast.hpp>
11
telsoa014fcda012018-03-09 14:13:49 +000012#include <arm_compute/core/ITensor.h>
13#include <arm_compute/core/TensorInfo.h>
surmeh013537c2c2018-05-18 16:31:43 +010014#include <arm_compute/core/Types.h>
telsoa014fcda012018-03-09 14:13:49 +000015
Mike Kelly0a08ec62019-07-25 08:39:31 +010016#include <Half.hpp>
17
telsoa014fcda012018-03-09 14:13:49 +000018namespace armnn
19{
20class ITensorHandle;
21
22namespace armcomputetensorutils
23{
24
telsoa01c577f2c2018-08-31 09:22:23 +010025/// Utility function to map an armnn::DataType to corresponding arm_compute::DataType.
Derek Lambertid466a542020-01-22 15:37:29 +000026arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales);
telsoa014fcda012018-03-09 14:13:49 +000027
Cathal Corbettfd5bec42022-03-03 15:13:23 +000028/// Utility function to map an arm_compute::DataType to corresponding armnn::DataType.
29armnn::DataType GetArmNNDataType(arm_compute::DataType datatype);
30
Matthew Benthamfd899962018-12-31 15:49:42 +000031/// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions
32arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
33 unsigned int originalInputRank,
34 const std::vector<unsigned int>& armnnAxes);
35
telsoa01c577f2c2018-08-31 09:22:23 +010036/// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape.
telsoa014fcda012018-03-09 14:13:49 +000037arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape);
38
39/// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
telsoa01c577f2c2018-08-31 09:22:23 +010040/// armnn::ITensorInfo.
telsoa014fcda012018-03-09 14:13:49 +000041arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo);
42
Francis Murtagh351d13d2018-09-24 15:01:18 +010043/// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
44/// armnn::ITensorInfo.
45/// armnn::DataLayout.
46arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
47 armnn::DataLayout dataLayout);
48
Matteo Martincigh747ef822018-12-18 09:26:39 +000049/// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout
50/// armnn::DataLayout.
51arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout);
52
Sadik Armagana3600ba2019-10-10 10:43:20 +010053/// Utility function used to setup an arm_compute::PoolingLayerInfo object from given
54/// armnn::Pooling2dDescriptor
55/// bool fpMixedPrecision
56arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
57 bool fpMixedPrecision = false);
telsoa014fcda012018-03-09 14:13:49 +000058
Ryan OSheabab8fa92022-03-09 10:29:02 +000059/// Utility function used to setup an arm_compute::Pooling3dLayerInfo object from given
60/// armnn::Pooling3dDescriptor
61/// bool fpMixedPrecision
62arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(const Pooling3dDescriptor& descriptor,
63 bool fpMixedPrecision = false);
64
telsoa01c577f2c2018-08-31 09:22:23 +010065/// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
telsoa014fcda012018-03-09 14:13:49 +000066arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
67
telsoa01c577f2c2018-08-31 09:22:23 +010068/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
Teresa Charlin6bc85252022-12-06 20:43:06 +000069/// \param perm PermutationVector used in Arm NN Permute layer
70/// \return PermutationVector used in ACL Transpose layer
71arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm);
telsoa014fcda012018-03-09 14:13:49 +000072
Mike Kellyc9ea45a2020-02-28 18:11:58 +000073/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
Teresa Charlin6bc85252022-12-06 20:43:06 +000074/// \param perm PermutationVector used in Arm NN Transpose layer
75/// \return PermutationVector used in ACL Transpose layer
76arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000077
Sadik Armaganf4464322018-12-20 16:19:12 +000078/// Utility function used to setup an arm_compute::Size2D object from width and height values.
79arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
80
Matthew Sloyan2e5d0b22021-10-21 14:05:31 +010081/// Gets the appropriate PixelValue for the TensorInfo DataType
Kevin May263d7092022-11-29 14:34:48 +000082arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float value);
Mike Kelly0a08ec62019-07-25 08:39:31 +010083
Cathal Corbett4b19d222022-05-11 20:12:17 +010084/// Computes the depth multiplier parameter for the Depthwise Conv2d ACL workload.
85unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout,
86 const arm_compute::TensorShape& weightsShape,
87 const arm_compute::TensorShape& inputShape);
88
telsoa01c577f2c2018-08-31 09:22:23 +010089/// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
surmeh013537c2c2018-05-18 16:31:43 +010090template <typename Descriptor>
91arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor)
92{
93 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
94 descriptor.m_StrideY,
95 descriptor.m_PadLeft,
96 descriptor.m_PadRight,
97 descriptor.m_PadTop,
98 descriptor.m_PadBottom,
99 arm_compute::DimensionRoundingType::FLOOR);
100}
101
telsoa014fcda012018-03-09 14:13:49 +0000102/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
103template <typename Tensor>
104void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
105{
106 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
107}
108
Francis Murtagh351d13d2018-09-24 15:01:18 +0100109/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
110template <typename Tensor>
111void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
112{
113 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
114}
115
telsoa014fcda012018-03-09 14:13:49 +0000116template <typename Tensor>
117void InitialiseArmComputeTensorEmpty(Tensor& tensor)
118{
119 tensor.allocator()->allocate();
120}
121
telsoa01c577f2c2018-08-31 09:22:23 +0100122/// Utility function to free unused tensors after a workload is configured and prepared
123template <typename Tensor>
124void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
125{
126 if (tensor && !tensor->is_used())
127 {
128 tensor.reset(nullptr);
129 }
130}
131
telsoa014fcda012018-03-09 14:13:49 +0000132// Helper function to obtain byte offset into tensor data
133inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100134 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000135 uint32_t batchIndex,
136 uint32_t channelIndex,
137 uint32_t y,
138 uint32_t x)
139{
140 arm_compute::Coordinates coords;
Matthew Jacksondba634f2019-08-15 15:14:18 +0100141 coords.set(4, static_cast<int>(depthIndex));
telsoa01c577f2c2018-08-31 09:22:23 +0100142 coords.set(3, static_cast<int>(batchIndex));
143 coords.set(2, static_cast<int>(channelIndex));
144 coords.set(1, static_cast<int>(y));
145 coords.set(0, static_cast<int>(x));
Matthew Sloyan171214c2020-09-09 09:07:37 +0100146 return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords));
telsoa014fcda012018-03-09 14:13:49 +0000147}
148
telsoa01c577f2c2018-08-31 09:22:23 +0100149// Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
telsoa014fcda012018-03-09 14:13:49 +0000150inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100151 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000152 uint32_t batchIndex,
153 uint32_t channelIndex,
154 uint32_t y,
155 uint32_t x)
156{
157 const arm_compute::TensorShape& shape = info.tensor_shape();
telsoa01c577f2c2018-08-31 09:22:23 +0100158 uint32_t width = static_cast<uint32_t>(shape[0]);
159 uint32_t height = static_cast<uint32_t>(shape[1]);
160 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100161 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
162 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
telsoa014fcda012018-03-09 14:13:49 +0000163}
164
165template <typename T>
166void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
167{
telsoa01c577f2c2018-08-31 09:22:23 +0100168 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100169 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000170 {
171 const arm_compute::ITensorInfo& info = *srcTensor.info();
172 const arm_compute::TensorShape& shape = info.tensor_shape();
173 const uint8_t* const bufferPtr = srcTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100174 uint32_t width = static_cast<uint32_t>(shape[0]);
175 uint32_t height = static_cast<uint32_t>(shape[1]);
176 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
177 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100178 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000179
Matthew Jacksondba634f2019-08-15 15:14:18 +0100180 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000181 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100182 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000183 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100184 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000185 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100186 for (unsigned int y = 0; y < height; ++y)
187 {
188 // Copies one row from arm_compute tensor buffer to linear memory buffer.
189 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
190 memcpy(
191 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
192 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
193 width * sizeof(T));
194 }
telsoa014fcda012018-03-09 14:13:49 +0000195 }
196 }
197 }
198 }
199}
200
201template <typename T>
202void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
203{
telsoa01c577f2c2018-08-31 09:22:23 +0100204 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100205 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000206 {
207 const arm_compute::ITensorInfo& info = *dstTensor.info();
208 const arm_compute::TensorShape& shape = info.tensor_shape();
209 uint8_t* const bufferPtr = dstTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100210 uint32_t width = static_cast<uint32_t>(shape[0]);
211 uint32_t height = static_cast<uint32_t>(shape[1]);
212 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
213 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100214 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000215
Matthew Jacksondba634f2019-08-15 15:14:18 +0100216 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000217 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100218 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000219 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100220 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000221 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100222 for (unsigned int y = 0; y < height; ++y)
223 {
224 // Copies one row from linear memory buffer to arm_compute tensor buffer.
225 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
226 memcpy(
227 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
228 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
229 width * sizeof(T));
230 }
telsoa014fcda012018-03-09 14:13:49 +0000231 }
232 }
233 }
234 }
235}
236
telsoa01c577f2c2018-08-31 09:22:23 +0100237/// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
238/// \tparam ArmComputeType Any type that implements the Dimensions interface
239/// \tparam T Shape value type
240/// \param shapelike An ArmCompute object that implements the Dimensions interface
241/// \param initial A default value to initialise the shape with
242/// \return A TensorShape object filled from the Acl shapelike object.
243template<typename ArmComputeType, typename T>
244TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
245{
246 std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
247 for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
248 {
Matthew Sloyan171214c2020-09-09 09:07:37 +0100249 s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
telsoa01c577f2c2018-08-31 09:22:23 +0100250 }
Matthew Sloyan171214c2020-09-09 09:07:37 +0100251 return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
telsoa01c577f2c2018-08-31 09:22:23 +0100252};
253
254/// Get the strides from an ACL strides object
255inline TensorShape GetStrides(const arm_compute::Strides& strides)
256{
257 return GetTensorShape(strides, 0U);
258}
259
260/// Get the shape from an ACL shape object
261inline TensorShape GetShape(const arm_compute::TensorShape& shape)
262{
263 return GetTensorShape(shape, 1U);
264}
265
telsoa014fcda012018-03-09 14:13:49 +0000266} // namespace armcomputetensorutils
267} // namespace armnn