ArmNN
 24.02
ArmComputeTensorUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Tensor.hpp>
9 
11 
12 #include <arm_compute/core/ITensor.h>
13 #include <arm_compute/core/TensorInfo.h>
14 #include <arm_compute/core/Types.h>
15 
16 #include <Half.hpp>
17 
18 namespace armnn
19 {
20 class ITensorHandle;
21 
22 namespace armcomputetensorutils
23 {
24 
25 /// Utility function to map an armnn::DataType to corresponding arm_compute::DataType.
26 arm_compute::DataType GetArmComputeDataType(armnn::DataType dataType, bool multiScales);
27 
28 /// Utility function to map an arm_compute::DataType to corresponding armnn::DataType.
29 armnn::DataType GetArmNNDataType(arm_compute::DataType datatype);
30 
31 /// Utility function used to set up an arm_compute::Coordinates from a vector of ArmNN Axes for reduction functions
32 arm_compute::Coordinates BuildArmComputeReductionCoordinates(size_t inputDimensions,
33  unsigned int originalInputRank,
34  const std::vector<unsigned int>& armnnAxes);
35 
36 /// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape.
37 arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape);
38 
39 /// Utility function used to setup an arm_compute::TensorShape object from an armnn::TensorShape. This will
40 /// attempt to reduce the number of leading 1s until the dimension length is equal to the dimensions passed in.
41 arm_compute::TensorShape BuildArmComputeTensorShape(const armnn::TensorShape& tensorShape, unsigned int dimensions);
42 
43 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
44 /// armnn::ITensorInfo.
45 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo);
46 
47 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
48 /// armnn::ITensorInfo. This will attempt to reduce the number of leading 1s until the dimension length is equal
49 /// to the dimensions passed in.
50 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo, unsigned int dimensions);
51 
52 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
53 /// armnn::ITensorInfo. This will attempt to reduce the number of leading 1s until the dimension length is equal
54 /// to the dimensions passed in.
55 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
56  armnn::DataLayout dataLayout,
57  unsigned int dimensions);
58 
59 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
60 /// armnn::ITensorInfo.
61 /// armnn::DataLayout.
62 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
63  armnn::DataLayout dataLayout);
64 
65 /// Utility function used to setup an arm_compute::ITensorInfo object whose dimensions are based on the given
66 /// armnn::ITensorInfo. This will attempt to reduce the number of leading 1s until the dimension length is equal
67 /// to the dimensions passed in.
68 arm_compute::TensorInfo BuildArmComputeTensorInfo(const armnn::TensorInfo& tensorInfo,
69  armnn::DataLayout dataLayout, unsigned int dimensions);
70 
71 /// Utility function used to convert armnn::DataLayout to arm_compute::DataLayout
72 /// armnn::DataLayout.
73 arm_compute::DataLayout ConvertDataLayout(armnn::DataLayout dataLayout);
74 
75 /// Utility function used to setup an arm_compute::PoolingLayerInfo object from given
76 /// armnn::Pooling2dDescriptor
77 /// bool fpMixedPrecision
78 arm_compute::PoolingLayerInfo BuildArmComputePoolingLayerInfo(const Pooling2dDescriptor& descriptor,
79  bool fpMixedPrecision = false);
80 
81 /// Utility function used to setup an arm_compute::Pooling3dLayerInfo object from given
82 /// armnn::Pooling3dDescriptor
83 /// bool fpMixedPrecision
84 arm_compute::Pooling3dLayerInfo BuildArmComputePooling3dLayerInfo(const Pooling3dDescriptor& descriptor,
85  bool fpMixedPrecision = false);
86 
87 /// Utility function to setup an arm_compute::NormalizationLayerInfo object from an armnn::NormalizationDescriptor.
88 arm_compute::NormalizationLayerInfo BuildArmComputeNormalizationLayerInfo(const NormalizationDescriptor& desc);
89 
90 /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
91 /// \param perm PermutationVector used in Arm NN Permute layer
92 /// \return PermutationVector used in ACL Transpose layer
93 arm_compute::PermutationVector BuildArmComputePermutationVector(const armnn::PermutationVector& perm);
94 
95 /// Utility function used to setup an arm_compute::PermutationVector object from an armnn::PermutationVector.
96 /// \param perm PermutationVector used in Arm NN Transpose layer
97 /// \return PermutationVector used in ACL Transpose layer
98 arm_compute::PermutationVector BuildArmComputeTransposeVector(const armnn::PermutationVector& perm);
99 
100 /// Utility function used to setup an arm_compute::Size2D object from width and height values.
101 arm_compute::Size2D BuildArmComputeSize2D(const unsigned int width, const unsigned int height);
102 
103 /// Gets the appropriate PixelValue for the TensorInfo DataType
104 arm_compute::PixelValue GetPixelValue(const arm_compute::ITensorInfo* tensorInfo, float value);
105 
106 /// Computes the depth multiplier parameter for the Depthwise Conv2d ACL workload.
107 unsigned int ComputeDepthwiseConv2dDepthMultiplier(armnn::DataLayout layout,
108  const arm_compute::TensorShape& weightsShape,
109  const arm_compute::TensorShape& inputShape);
110 
111 /// Utility function used to setup an arm_compute::PadStrideInfo object from an ArmNN layer descriptor.
112 template <typename Descriptor>
113 arm_compute::PadStrideInfo BuildArmComputePadStrideInfo(const Descriptor& descriptor)
114 {
115  return arm_compute::PadStrideInfo(descriptor.m_StrideX,
116  descriptor.m_StrideY,
117  descriptor.m_PadLeft,
118  descriptor.m_PadRight,
119  descriptor.m_PadTop,
120  descriptor.m_PadBottom,
121  arm_compute::DimensionRoundingType::FLOOR);
122 }
123 
124 /// Utility function used to setup an arm_compute::Padding2D object from an armnn layer descriptor.
125 template <typename Descriptor>
126 arm_compute::Padding2D BuildArmComputePaddingInfo(const Descriptor &descriptor)
127 {
128  return arm_compute::Padding2D(descriptor.m_PadLeft,
129  descriptor.m_PadRight,
130  descriptor.m_PadTop,
131  descriptor.m_PadBottom);
132 }
133 
134 /// Utility function used to setup an arm_compute::CropInfo object from an ArmNN layer descriptor.
135 template <typename Descriptor>
136 arm_compute::CropInfo BuildArmComputeCropInfo(const Descriptor& descriptor, const unsigned int rank = 4)
137 {
138  if (rank == 3)
139  {
140  return arm_compute::CropInfo(0, 0,
141  descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
142  }
143  else if (rank == 4)
144  {
145  return arm_compute::CropInfo(descriptor.m_Crops[1].first, descriptor.m_Crops[1].second,
146  descriptor.m_Crops[0].first, descriptor.m_Crops[0].second);
147  }
148  else
149  {
150  throw InvalidArgumentException("Tensor rank must be either 3 or 4", CHECK_LOCATION());
151  }
152 }
153 
154 /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
155 template <typename Tensor>
156 void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo)
157 {
158  tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo));
159 }
160 
161 /// Sets up the given ArmCompute tensor's dimensions based on the given ArmNN tensor.
162 template <typename Tensor>
163 void BuildArmComputeTensor(Tensor& tensor, const armnn::TensorInfo& tensorInfo, DataLayout dataLayout)
164 {
165  tensor.allocator()->init(BuildArmComputeTensorInfo(tensorInfo, dataLayout));
166 }
167 
168 template <typename Tensor>
169 void InitialiseArmComputeTensorEmpty(Tensor& tensor)
170 {
171  tensor.allocator()->allocate();
172 }
173 
174 /// Utility function to free unused tensors after a workload is configured and prepared
175 template <typename Tensor>
176 void FreeTensorIfUnused(std::unique_ptr<Tensor>& tensor)
177 {
178  if (tensor && !tensor->is_used())
179  {
180  tensor.reset(nullptr);
181  }
182 }
183 
184 // Helper function to obtain byte offset into tensor data
185 inline size_t GetTensorOffset(const arm_compute::ITensorInfo& info,
186  uint32_t depthIndex,
187  uint32_t batchIndex,
188  uint32_t channelIndex,
189  uint32_t y,
190  uint32_t x)
191 {
193  coords.set(4, static_cast<int>(depthIndex));
194  coords.set(3, static_cast<int>(batchIndex));
195  coords.set(2, static_cast<int>(channelIndex));
196  coords.set(1, static_cast<int>(y));
197  coords.set(0, static_cast<int>(x));
198  return armnn::numeric_cast<size_t>(info.offset_element_in_bytes(coords));
199 }
200 
201 // Helper function to obtain element offset into data buffer representing tensor data (assuming no strides).
202 inline size_t GetLinearBufferOffset(const arm_compute::ITensorInfo& info,
203  uint32_t depthIndex,
204  uint32_t batchIndex,
205  uint32_t channelIndex,
206  uint32_t y,
207  uint32_t x)
208 {
209  const arm_compute::TensorShape& shape = info.tensor_shape();
210  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]);
214  return (((depthIndex * numBatches + batchIndex) * numChannels + channelIndex) * height + y) * width + x;
215 }
216 
217 template <typename T>
218 void CopyArmComputeITensorData(const arm_compute::ITensor& srcTensor, T* dstData)
219 {
220  // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
221  static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
222  {
223  const arm_compute::ITensorInfo& info = *srcTensor.info();
224  const arm_compute::TensorShape& shape = info.tensor_shape();
225  const uint8_t* const bufferPtr = srcTensor.buffer();
226  uint32_t width = static_cast<uint32_t>(shape[0]);
227  uint32_t height = static_cast<uint32_t>(shape[1]);
228  uint32_t numChannels = static_cast<uint32_t>(shape[2]);
229  uint32_t numBatches = static_cast<uint32_t>(shape[3]);
230  uint32_t depth = static_cast<uint32_t>(shape[4]);
231 
232  for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
233  {
234  for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
235  {
236  for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
237  {
238  for (unsigned int y = 0; y < height; ++y)
239  {
240  // Copies one row from arm_compute tensor buffer to linear memory buffer.
241  // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
242  memcpy(
243  dstData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
244  bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
245  width * sizeof(T));
246  }
247  }
248  }
249  }
250  }
251 }
252 
253 template <typename T>
254 void CopyArmComputeITensorData(const T* srcData, arm_compute::ITensor& dstTensor)
255 {
256  // If MaxNumOfTensorDimensions is increased, this loop will need fixing.
257  static_assert(MaxNumOfTensorDimensions == 5, "Please update CopyArmComputeITensorData");
258  {
259  const arm_compute::ITensorInfo& info = *dstTensor.info();
260  const arm_compute::TensorShape& shape = info.tensor_shape();
261  uint8_t* const bufferPtr = dstTensor.buffer();
262  uint32_t width = static_cast<uint32_t>(shape[0]);
263  uint32_t height = static_cast<uint32_t>(shape[1]);
264  uint32_t numChannels = static_cast<uint32_t>(shape[2]);
265  uint32_t numBatches = static_cast<uint32_t>(shape[3]);
266  uint32_t depth = static_cast<uint32_t>(shape[4]);
267 
268  for (unsigned int depthIndex = 0; depthIndex < depth; ++depthIndex)
269  {
270  for (unsigned int batchIndex = 0; batchIndex < numBatches; ++batchIndex)
271  {
272  for (unsigned int channelIndex = 0; channelIndex < numChannels; ++channelIndex)
273  {
274  for (unsigned int y = 0; y < height; ++y)
275  {
276  // Copies one row from linear memory buffer to arm_compute tensor buffer.
277  // A row is the largest contiguous region we can copy, as the tensor data may be using strides.
278  memcpy(
279  bufferPtr + GetTensorOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
280  srcData + GetLinearBufferOffset(info, depthIndex, batchIndex, channelIndex, y, 0),
281  width * sizeof(T));
282  }
283  }
284  }
285  }
286  }
287 }
288 
289 /// Construct a TensorShape object from an ArmCompute object based on arm_compute::Dimensions.
290 /// \tparam ArmComputeType Any type that implements the Dimensions interface
291 /// \tparam T Shape value type
292 /// \param shapelike An ArmCompute object that implements the Dimensions interface
293 /// \param initial A default value to initialise the shape with
294 /// \return A TensorShape object filled from the Acl shapelike object.
295 template<typename ArmComputeType, typename T>
296 TensorShape GetTensorShape(const ArmComputeType& shapelike, T initial)
297 {
298  std::vector<unsigned int> s(MaxNumOfTensorDimensions, initial);
299  for (unsigned int i=0; i < shapelike.num_dimensions(); ++i)
300  {
301  s[(shapelike.num_dimensions()-1)-i] = armnn::numeric_cast<unsigned int>(shapelike[i]);
302  }
303  return TensorShape(armnn::numeric_cast<unsigned int>(shapelike.num_dimensions()), s.data());
304 };
305 
306 /// Get the strides from an ACL strides object
307 inline TensorShape GetStrides(const arm_compute::Strides& strides)
308 {
309  return GetTensorShape(strides, 0U);
310 }
311 
312 /// Get the shape from an ACL shape object
313 inline TensorShape GetShape(const arm_compute::TensorShape& shape)
314 {
315  return GetTensorShape(shape, 1U);
316 }
317 
318 } // namespace armcomputetensorutils
319 } // namespace armnn
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::TensorInfo
Definition: Tensor.hpp:152
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::MaxNumOfTensorDimensions
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
armnn::Coordinates
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
Definition: InternalTypes.hpp:15
NumericCast.hpp
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::DataType
DataType
Definition: Types.hpp:48
DescriptorsFwd.hpp
armnn::PermutationVector
Definition: Types.hpp:314
armnn::BoostLogSeverityMapping::info
@ info
Half.hpp
Tensor.hpp
armnnDeserializer::Pooling3dDescriptor
const armnnSerializer::Pooling3dDescriptor * Pooling3dDescriptor
Definition: Deserializer.hpp:22
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnnUtils::GetTensorShape
armnn::TensorShape GetTensorShape(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout)
Definition: TensorUtils.cpp:21
armnnDeserializer::Pooling2dDescriptor
const armnnSerializer::Pooling2dDescriptor * Pooling2dDescriptor
Definition: Deserializer.hpp:21