blob: fdcd867bbab4879a9ee2d793ef69ecb013c93bf5 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlinec5f7d12021-10-22 17:15:00 +01002// Copyright © 2017 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.
telsoa014fcda012018-03-09 14:13:49 +000069arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& vector);
70
Mike Kellyc9ea45a2020-02-28 18:11:58 +000071/// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
72arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& vector);
73
Sadik Armaganf4464322018-12-20 16:19:12 +000074/// Utility function used to setup an arm_compute::Size2D object from width and height values.
75arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
76
Matthew Sloyan2e5d0b22021-10-21 14:05:31 +010077/// Gets the appropriate PixelValue for the TensorInfo DataType
Kevin May263d7092022-11-29 14:34:48 +000078arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float value);
Mike Kelly0a08ec62019-07-25 08:39:31 +010079
Cathal Corbett4b19d222022-05-11 20:12:17 +010080/// Computes the depth multiplier parameter for the Depthwise Conv2d ACL workload.
81unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout,
82 const arm_compute::TensorShape& weightsShape,
83 const arm_compute::TensorShape& inputShape);
84
telsoa01c577f2c2018-08-31 09:22:23 +010085/// Utility function used to setup an arm_compute::PadStrideInfo object from an armnn layer descriptor.
surmeh013537c2c2018-05-18 16:31:43 +010086template <typename Descriptor>
87arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor &descriptor)
88{
89 return arm_compute::PadStrideInfo(descriptor.m_StrideX,
90 descriptor.m_StrideY,
91 descriptor.m_PadLeft,
92 descriptor.m_PadRight,
93 descriptor.m_PadTop,
94 descriptor.m_PadBottom,
95 arm_compute::DimensionRoundingType::FLOOR);
96}
97
telsoa014fcda012018-03-09 14:13:49 +000098/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
99template <typename Tensor>
100void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
101{
102 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
103}
104
Francis Murtagh351d13d2018-09-24 15:01:18 +0100105/// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
106template <typename Tensor>
107void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
108{
109 tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
110}
111
telsoa014fcda012018-03-09 14:13:49 +0000112template <typename Tensor>
113void InitialiseArmComputeTensorEmpty(Tensor& tensor)
114{
115 tensor.allocator()->allocate();
116}
117
telsoa01c577f2c2018-08-31 09:22:23 +0100118/// Utility function to free unused tensors after a workload is configured and prepared
119template <typename Tensor>
120void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
121{
122 if (tensor && !tensor->is_used())
123 {
124 tensor.reset(nullptr);
125 }
126}
127
telsoa014fcda012018-03-09 14:13:49 +0000128// Helper function to obtain byte offset into tensor data
129inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100130 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000131 uint32_t batchIndex,
132 uint32_t channelIndex,
133 uint32_t y,
134 uint32_t x)
135{
136 arm_compute::Coordinates coords;
Matthew Jacksondba634f2019-08-15 15:14:18 +0100137 coords.set(4, static_cast<int>(depthIndex));
telsoa01c577f2c2018-08-31 09:22:23 +0100138 coords.set(3, static_cast<int>(batchIndex));
139 coords.set(2, static_cast<int>(channelIndex));
140 coords.set(1, static_cast<int>(y));
141 coords.set(0, static_cast<int>(x));
Matthew Sloyan171214c2020-09-09 09:07:37 +0100142 return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords));
telsoa014fcda012018-03-09 14:13:49 +0000143}
144
telsoa01c577f2c2018-08-31 09:22:23 +0100145// Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
telsoa014fcda012018-03-09 14:13:49 +0000146inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
Matthew Jacksondba634f2019-08-15 15:14:18 +0100147 uint32_t depthIndex,
telsoa014fcda012018-03-09 14:13:49 +0000148 uint32_t batchIndex,
149 uint32_t channelIndex,
150 uint32_t y,
151 uint32_t x)
152{
153 const arm_compute::TensorShape& shape = info.tensor_shape();
telsoa01c577f2c2018-08-31 09:22:23 +0100154 uint32_t width = static_cast<uint32_t>(shape[0]);
155 uint32_t height = static_cast<uint32_t>(shape[1]);
156 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100157 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
158 return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
telsoa014fcda012018-03-09 14:13:49 +0000159}
160
161template <typename T>
162void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
163{
telsoa01c577f2c2018-08-31 09:22:23 +0100164 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100165 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000166 {
167 const arm_compute::ITensorInfo& info = *srcTensor.info();
168 const arm_compute::TensorShape& shape = info.tensor_shape();
169 const uint8_t* const bufferPtr = srcTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100170 uint32_t width = static_cast<uint32_t>(shape[0]);
171 uint32_t height = static_cast<uint32_t>(shape[1]);
172 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
173 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100174 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000175
Matthew Jacksondba634f2019-08-15 15:14:18 +0100176 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000177 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100178 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000179 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100180 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000181 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100182 for (unsigned int y = 0; y < height; ++y)
183 {
184 // Copies one row from arm_compute tensor buffer to linear memory buffer.
185 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
186 memcpy(
187 dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
188 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
189 width * sizeof(T));
190 }
telsoa014fcda012018-03-09 14:13:49 +0000191 }
192 }
193 }
194 }
195}
196
197template <typename T>
198void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
199{
telsoa01c577f2c2018-08-31 09:22:23 +0100200 // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
Matthew Jacksondba634f2019-08-15 15:14:18 +0100201 static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
telsoa014fcda012018-03-09 14:13:49 +0000202 {
203 const arm_compute::ITensorInfo& info = *dstTensor.info();
204 const arm_compute::TensorShape& shape = info.tensor_shape();
205 uint8_t* const bufferPtr = dstTensor.buffer();
telsoa01c577f2c2018-08-31 09:22:23 +0100206 uint32_t width = static_cast<uint32_t>(shape[0]);
207 uint32_t height = static_cast<uint32_t>(shape[1]);
208 uint32_t numChannels = static_cast<uint32_t>(shape[2]);
209 uint32_t numBatches = static_cast<uint32_t>(shape[3]);
Matthew Jacksondba634f2019-08-15 15:14:18 +0100210 uint32_t depth = static_cast<uint32_t>(shape[4]);
telsoa014fcda012018-03-09 14:13:49 +0000211
Matthew Jacksondba634f2019-08-15 15:14:18 +0100212 for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
telsoa014fcda012018-03-09 14:13:49 +0000213 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100214 for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
telsoa014fcda012018-03-09 14:13:49 +0000215 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100216 for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
telsoa014fcda012018-03-09 14:13:49 +0000217 {
Matthew Jacksondba634f2019-08-15 15:14:18 +0100218 for (unsigned int y = 0; y < height; ++y)
219 {
220 // Copies one row from linear memory buffer to arm_compute tensor buffer.
221 // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
222 memcpy(
223 bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
224 srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
225 width * sizeof(T));
226 }
telsoa014fcda012018-03-09 14:13:49 +0000227 }
228 }
229 }
230 }
231}
232
telsoa01c577f2c2018-08-31 09:22:23 +0100233/// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
234/// \tparam ArmComputeType Any type that implements the Dimensions interface
235/// \tparam T Shape value type
236/// \param shapelike An ArmCompute object that implements the Dimensions interface
237/// \param initial A default value to initialise the shape with
238/// \return A TensorShape object filled from the Acl shapelike object.
239template<typename ArmComputeType, typename T>
240TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
241{
242 std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
243 for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
244 {
Matthew Sloyan171214c2020-09-09 09:07:37 +0100245 s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
telsoa01c577f2c2018-08-31 09:22:23 +0100246 }
Matthew Sloyan171214c2020-09-09 09:07:37 +0100247 return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
telsoa01c577f2c2018-08-31 09:22:23 +0100248};
249
250/// Get the strides from an ACL strides object
251inline TensorShape GetStrides(const arm_compute::Strides& strides)
252{
253 return GetTensorShape(strides, 0U);
254}
255
256/// Get the shape from an ACL shape object
257inline TensorShape GetShape(const arm_compute::TensorShape& shape)
258{
259 return GetTensorShape(shape, 1U);
260}
261
telsoa014fcda012018-03-09 14:13:49 +0000262} // namespace armcomputetensorutils
263} // namespace armnn